[incremental/pm] register progress listener
Incremental Serivce periodically polls loading progress and sends to
Package Manager Service. Package Manager provides APIs for other
interested parties to listen to the loading progress.
BUG: 165841827
Test: unit test
Change-Id: I44b9e17c2240b9efe53bc09fc728b6671f1f7dfe
diff --git a/services/incremental/BinderIncrementalService.cpp b/services/incremental/BinderIncrementalService.cpp
index 87ae4d7..bf3a896 100644
--- a/services/incremental/BinderIncrementalService.cpp
+++ b/services/incremental/BinderIncrementalService.cpp
@@ -301,6 +301,20 @@
return ok();
}
+binder::Status BinderIncrementalService::registerLoadingProgressListener(
+ int32_t storageId,
+ const ::android::sp<::android::os::incremental::IStorageLoadingProgressListener>&
+ progressListener,
+ bool* _aidl_return) {
+ *_aidl_return = mImpl.registerLoadingProgressListener(storageId, progressListener);
+ return ok();
+}
+binder::Status BinderIncrementalService::unregisterLoadingProgressListener(int32_t storageId,
+ bool* _aidl_return) {
+ *_aidl_return = mImpl.unregisterLoadingProgressListener(storageId);
+ return ok();
+}
+
} // namespace android::os::incremental
jlong Incremental_IncrementalService_Start(JNIEnv* env) {
diff --git a/services/incremental/BinderIncrementalService.h b/services/incremental/BinderIncrementalService.h
index 8478142..1238498 100644
--- a/services/incremental/BinderIncrementalService.h
+++ b/services/incremental/BinderIncrementalService.h
@@ -81,6 +81,12 @@
const std::string& abi, bool extractNativeLibs,
bool* _aidl_return) final;
binder::Status waitForNativeBinariesExtraction(int storageId, bool* _aidl_return) final;
+ binder::Status registerLoadingProgressListener(
+ int32_t storageId,
+ const ::android::sp<::android::os::incremental::IStorageLoadingProgressListener>&
+ progressListener,
+ bool* _aidl_return) final;
+ binder::Status unregisterLoadingProgressListener(int32_t storageId, bool* _aidl_return) final;
private:
android::incremental::IncrementalService mImpl;
diff --git a/services/incremental/IncrementalService.cpp b/services/incremental/IncrementalService.cpp
index 447ee55..10a508b 100644
--- a/services/incremental/IncrementalService.cpp
+++ b/services/incremental/IncrementalService.cpp
@@ -275,6 +275,7 @@
mJni(sm.getJni()),
mLooper(sm.getLooper()),
mTimedQueue(sm.getTimedQueue()),
+ mProgressUpdateJobQueue(sm.getProgressUpdateJobQueue()),
mFs(sm.getFs()),
mIncrementalDir(rootDir) {
CHECK(mVold) << "Vold service is unavailable";
@@ -283,6 +284,7 @@
CHECK(mJni) << "JNI is unavailable";
CHECK(mLooper) << "Looper is unavailable";
CHECK(mTimedQueue) << "TimedQueue is unavailable";
+ CHECK(mProgressUpdateJobQueue) << "mProgressUpdateJobQueue is unavailable";
CHECK(mFs) << "Fs is unavailable";
mJobQueue.reserve(16);
@@ -308,6 +310,7 @@
mJobProcessor.join();
mCmdLooperThread.join();
mTimedQueue->stop();
+ mProgressUpdateJobQueue->stop();
// Ensure that mounts are destroyed while the service is still valid.
mBindsByPath.clear();
mMounts.clear();
@@ -1744,6 +1747,35 @@
return (float)filledBlocks / (float)totalBlocks;
}
+bool IncrementalService::updateLoadingProgress(
+ StorageId storage, const StorageLoadingProgressListener& progressListener) {
+ const auto progress = getLoadingProgress(storage);
+ if (progress < 0) {
+ // Failed to get progress from incfs, abort.
+ return false;
+ }
+ progressListener->onStorageLoadingProgressChanged(storage, progress);
+ if (progress > 1 - 0.001f) {
+ // Stop updating progress once it is fully loaded
+ return true;
+ }
+ static constexpr auto kProgressUpdateInterval = 1000ms;
+ addTimedJob(*mProgressUpdateJobQueue, storage, kProgressUpdateInterval /* repeat after 1s */,
+ [storage, progressListener, this]() {
+ updateLoadingProgress(storage, progressListener);
+ });
+ return true;
+}
+
+bool IncrementalService::registerLoadingProgressListener(
+ StorageId storage, const StorageLoadingProgressListener& progressListener) {
+ return updateLoadingProgress(storage, progressListener);
+}
+
+bool IncrementalService::unregisterLoadingProgressListener(StorageId storage) {
+ return removeTimedJobs(*mProgressUpdateJobQueue, storage);
+}
+
bool IncrementalService::perfLoggingEnabled() {
static const bool enabled = base::GetBoolProperty("incremental.perflogging", false);
return enabled;
@@ -1826,18 +1858,21 @@
}
}
-void IncrementalService::addTimedJob(MountId id, Milliseconds after, Job what) {
+bool IncrementalService::addTimedJob(TimedQueueWrapper& timedQueue, MountId id, Milliseconds after,
+ Job what) {
if (id == kInvalidStorageId) {
- return;
+ return false;
}
- mTimedQueue->addJob(id, after, std::move(what));
+ timedQueue.addJob(id, after, std::move(what));
+ return true;
}
-void IncrementalService::removeTimedJobs(MountId id) {
+bool IncrementalService::removeTimedJobs(TimedQueueWrapper& timedQueue, MountId id) {
if (id == kInvalidStorageId) {
- return;
+ return false;
}
- mTimedQueue->removeJobs(id);
+ timedQueue.removeJobs(id);
+ return true;
}
IncrementalService::DataLoaderStub::DataLoaderStub(IncrementalService& service, MountId id,
@@ -1879,7 +1914,7 @@
mHealthPath.clear();
unregisterFromPendingReads();
resetHealthControl();
- mService.removeTimedJobs(mId);
+ mService.removeTimedJobs(*mService.mTimedQueue, mId);
}
requestDestroy();
@@ -2169,7 +2204,8 @@
}
LOG(DEBUG) << id() << ": updateHealthStatus in " << double(checkBackAfter.count()) / 1000.0
<< "secs";
- mService.addTimedJob(id(), checkBackAfter, [this]() { updateHealthStatus(); });
+ mService.addTimedJob(*mService.mTimedQueue, id(), checkBackAfter,
+ [this]() { updateHealthStatus(); });
}
// With kTolerance we are expecting these to execute before the next update.
diff --git a/services/incremental/IncrementalService.h b/services/incremental/IncrementalService.h
index 267458d..a49e0f3 100644
--- a/services/incremental/IncrementalService.h
+++ b/services/incremental/IncrementalService.h
@@ -22,6 +22,7 @@
#include <android/content/pm/IDataLoaderStatusListener.h>
#include <android/os/incremental/BnIncrementalServiceConnector.h>
#include <android/os/incremental/BnStorageHealthListener.h>
+#include <android/os/incremental/BnStorageLoadingProgressListener.h>
#include <android/os/incremental/StorageHealthCheckParams.h>
#include <binder/IAppOpsCallback.h>
#include <utils/String16.h>
@@ -65,6 +66,8 @@
using StorageHealthCheckParams = ::android::os::incremental::StorageHealthCheckParams;
using IStorageHealthListener = ::android::os::incremental::IStorageHealthListener;
using StorageHealthListener = ::android::sp<IStorageHealthListener>;
+using IStorageLoadingProgressListener = ::android::os::incremental::IStorageLoadingProgressListener;
+using StorageLoadingProgressListener = ::android::sp<IStorageLoadingProgressListener>;
class IncrementalService final {
public:
@@ -134,6 +137,9 @@
int isFileFullyLoaded(StorageId storage, const std::string& path) const;
float getLoadingProgress(StorageId storage) const;
+ bool registerLoadingProgressListener(StorageId storage,
+ const StorageLoadingProgressListener& progressListener);
+ bool unregisterLoadingProgressListener(StorageId storage);
RawMetadata getMetadata(StorageId storage, std::string_view path) const;
RawMetadata getMetadata(StorageId storage, FileId node) const;
@@ -354,8 +360,10 @@
void runCmdLooper();
- void addTimedJob(MountId id, Milliseconds after, Job what);
- void removeTimedJobs(MountId id);
+ bool addTimedJob(TimedQueueWrapper& timedQueue, MountId id, Milliseconds after, Job what);
+ bool removeTimedJobs(TimedQueueWrapper& timedQueue, MountId id);
+ bool updateLoadingProgress(int32_t storageId,
+ const StorageLoadingProgressListener& progressListener);
private:
const std::unique_ptr<VoldServiceWrapper> mVold;
@@ -365,6 +373,7 @@
const std::unique_ptr<JniWrapper> mJni;
const std::unique_ptr<LooperWrapper> mLooper;
const std::unique_ptr<TimedQueueWrapper> mTimedQueue;
+ const std::unique_ptr<TimedQueueWrapper> mProgressUpdateJobQueue;
const std::unique_ptr<FsWrapper> mFs;
const std::string mIncrementalDir;
diff --git a/services/incremental/ServiceWrappers.cpp b/services/incremental/ServiceWrappers.cpp
index f6d89c5..144c466 100644
--- a/services/incremental/ServiceWrappers.cpp
+++ b/services/incremental/ServiceWrappers.cpp
@@ -357,6 +357,10 @@
return std::make_unique<RealTimedQueueWrapper>(mJvm);
}
+std::unique_ptr<TimedQueueWrapper> RealServiceManager::getProgressUpdateJobQueue() {
+ return std::make_unique<RealTimedQueueWrapper>(mJvm);
+}
+
std::unique_ptr<FsWrapper> RealServiceManager::getFs() {
return std::make_unique<RealFsWrapper>();
}
diff --git a/services/incremental/ServiceWrappers.h b/services/incremental/ServiceWrappers.h
index 6376d86..4815caf 100644
--- a/services/incremental/ServiceWrappers.h
+++ b/services/incremental/ServiceWrappers.h
@@ -154,6 +154,7 @@
virtual std::unique_ptr<JniWrapper> getJni() = 0;
virtual std::unique_ptr<LooperWrapper> getLooper() = 0;
virtual std::unique_ptr<TimedQueueWrapper> getTimedQueue() = 0;
+ virtual std::unique_ptr<TimedQueueWrapper> getProgressUpdateJobQueue() = 0;
virtual std::unique_ptr<FsWrapper> getFs() = 0;
};
@@ -170,6 +171,7 @@
std::unique_ptr<JniWrapper> getJni() final;
std::unique_ptr<LooperWrapper> getLooper() final;
std::unique_ptr<TimedQueueWrapper> getTimedQueue() final;
+ std::unique_ptr<TimedQueueWrapper> getProgressUpdateJobQueue() final;
std::unique_ptr<FsWrapper> getFs() final;
private:
diff --git a/services/incremental/test/IncrementalServiceTest.cpp b/services/incremental/test/IncrementalServiceTest.cpp
index a290a17..aec9fa1 100644
--- a/services/incremental/test/IncrementalServiceTest.cpp
+++ b/services/incremental/test/IncrementalServiceTest.cpp
@@ -504,6 +504,14 @@
int32_t mStatus = -1;
};
+class MockStorageLoadingProgressListener : public IStorageLoadingProgressListener {
+public:
+ MockStorageLoadingProgressListener() = default;
+ MOCK_METHOD2(onStorageLoadingProgressChanged,
+ binder::Status(int32_t storageId, float progress));
+ MOCK_METHOD0(onAsBinder, IBinder*());
+};
+
class MockServiceManager : public ServiceManagerWrapper {
public:
MockServiceManager(std::unique_ptr<MockVoldService> vold,
@@ -513,6 +521,7 @@
std::unique_ptr<MockJniWrapper> jni,
std::unique_ptr<MockLooperWrapper> looper,
std::unique_ptr<MockTimedQueueWrapper> timedQueue,
+ std::unique_ptr<MockTimedQueueWrapper> progressUpdateJobQueue,
std::unique_ptr<MockFsWrapper> fs)
: mVold(std::move(vold)),
mDataLoaderManager(std::move(dataLoaderManager)),
@@ -521,6 +530,7 @@
mJni(std::move(jni)),
mLooper(std::move(looper)),
mTimedQueue(std::move(timedQueue)),
+ mProgressUpdateJobQueue(std::move(progressUpdateJobQueue)),
mFs(std::move(fs)) {}
std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
@@ -533,6 +543,9 @@
std::unique_ptr<JniWrapper> getJni() final { return std::move(mJni); }
std::unique_ptr<LooperWrapper> getLooper() final { return std::move(mLooper); }
std::unique_ptr<TimedQueueWrapper> getTimedQueue() final { return std::move(mTimedQueue); }
+ std::unique_ptr<TimedQueueWrapper> getProgressUpdateJobQueue() final {
+ return std::move(mProgressUpdateJobQueue);
+ }
std::unique_ptr<FsWrapper> getFs() final { return std::move(mFs); }
private:
@@ -543,6 +556,7 @@
std::unique_ptr<MockJniWrapper> mJni;
std::unique_ptr<MockLooperWrapper> mLooper;
std::unique_ptr<MockTimedQueueWrapper> mTimedQueue;
+ std::unique_ptr<MockTimedQueueWrapper> mProgressUpdateJobQueue;
std::unique_ptr<MockFsWrapper> mFs;
};
@@ -567,19 +581,19 @@
mLooper = looper.get();
auto timedQueue = std::make_unique<NiceMock<MockTimedQueueWrapper>>();
mTimedQueue = timedQueue.get();
+ auto progressUpdateJobQueue = std::make_unique<NiceMock<MockTimedQueueWrapper>>();
+ mProgressUpdateJobQueue = progressUpdateJobQueue.get();
auto fs = std::make_unique<NiceMock<MockFsWrapper>>();
mFs = fs.get();
- mIncrementalService =
- std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
- std::move(
- dataloaderManager),
- std::move(incFs),
- std::move(appOps),
- std::move(jni),
- std::move(looper),
- std::move(timedQueue),
- std::move(fs)),
- mRootDir.path);
+ mIncrementalService = std::make_unique<
+ IncrementalService>(MockServiceManager(std::move(vold),
+ std::move(dataloaderManager),
+ std::move(incFs), std::move(appOps),
+ std::move(jni), std::move(looper),
+ std::move(timedQueue),
+ std::move(progressUpdateJobQueue),
+ std::move(fs)),
+ mRootDir.path);
mDataLoaderParcel.packageName = "com.test";
mDataLoaderParcel.arguments = "uri";
mDataLoaderManager->unbindFromDataLoaderSuccess();
@@ -624,6 +638,7 @@
NiceMock<MockJniWrapper>* mJni = nullptr;
NiceMock<MockLooperWrapper>* mLooper = nullptr;
NiceMock<MockTimedQueueWrapper>* mTimedQueue = nullptr;
+ NiceMock<MockTimedQueueWrapper>* mProgressUpdateJobQueue = nullptr;
NiceMock<MockFsWrapper>* mFs = nullptr;
NiceMock<MockDataLoader>* mDataLoader = nullptr;
std::unique_ptr<IncrementalService> mIncrementalService;
@@ -1166,4 +1181,44 @@
EXPECT_CALL(*mIncFs, countFilledBlocks(_, _)).Times(3);
ASSERT_EQ(0.5, mIncrementalService->getLoadingProgress(storageId));
}
+
+TEST_F(IncrementalServiceTest, testRegisterLoadingProgressListenerSuccess) {
+ mIncFs->countFilledBlocksSuccess();
+ mFs->hasFiles();
+
+ TemporaryDir tempDir;
+ int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
+ IncrementalService::CreateOptions::CreateNew,
+ {}, {}, {});
+ sp<NiceMock<MockStorageLoadingProgressListener>> listener{
+ new NiceMock<MockStorageLoadingProgressListener>};
+ NiceMock<MockStorageLoadingProgressListener>* listenerMock = listener.get();
+ EXPECT_CALL(*listenerMock, onStorageLoadingProgressChanged(_, _)).Times(2);
+ EXPECT_CALL(*mProgressUpdateJobQueue, addJob(_, _, _)).Times(2);
+ mIncrementalService->registerLoadingProgressListener(storageId, listener);
+ // Timed callback present.
+ ASSERT_EQ(storageId, mProgressUpdateJobQueue->mId);
+ ASSERT_EQ(mProgressUpdateJobQueue->mAfter, 1000ms);
+ auto timedCallback = mProgressUpdateJobQueue->mWhat;
+ timedCallback();
+ ASSERT_EQ(storageId, mProgressUpdateJobQueue->mId);
+ ASSERT_EQ(mProgressUpdateJobQueue->mAfter, 1000ms);
+ mIncrementalService->unregisterLoadingProgressListener(storageId);
+ ASSERT_EQ(mProgressUpdateJobQueue->mAfter, Milliseconds{});
+}
+
+TEST_F(IncrementalServiceTest, testRegisterLoadingProgressListenerFailsToGetProgress) {
+ mIncFs->countFilledBlocksFails();
+ mFs->hasFiles();
+
+ TemporaryDir tempDir;
+ int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
+ IncrementalService::CreateOptions::CreateNew,
+ {}, {}, {});
+ sp<NiceMock<MockStorageLoadingProgressListener>> listener{
+ new NiceMock<MockStorageLoadingProgressListener>};
+ NiceMock<MockStorageLoadingProgressListener>* listenerMock = listener.get();
+ EXPECT_CALL(*listenerMock, onStorageLoadingProgressChanged(_, _)).Times(0);
+ mIncrementalService->registerLoadingProgressListener(storageId, listener);
+}
} // namespace android::os::incremental