LazyServiceRegistrar: Add flag to prevent shutdown
This flag can be used to temporarily prevent dynamic services from being killed. For example, if a service needs to perform a task in the background it can set this flag and then disable it once the task is complete. The service will then go back to standard lazy behavior.
Bug: 150967269
Test: aidl_lazy_test
Change-Id: I840bbe4b6b0406f0e4de610367290448e7edd3de
diff --git a/libs/binder/LazyServiceRegistrar.cpp b/libs/binder/LazyServiceRegistrar.cpp
index 71d8130..74aece8 100644
--- a/libs/binder/LazyServiceRegistrar.cpp
+++ b/libs/binder/LazyServiceRegistrar.cpp
@@ -31,11 +31,16 @@
class ClientCounterCallback : public ::android::os::BnClientCallback {
public:
- ClientCounterCallback() : mNumConnectedServices(0) {}
+ ClientCounterCallback() : mNumConnectedServices(0), mForcePersist(false) {}
bool registerService(const sp<IBinder>& service, const std::string& name,
bool allowIsolated, int dumpFlags);
+ /**
+ * Set a flag to prevent services from automatically shutting down
+ */
+ void forcePersist(bool persist);
+
protected:
Status onClients(const sp<IBinder>& service, bool clients) override;
@@ -60,6 +65,8 @@
* Map of registered names and services
*/
std::map<std::string, Service> mRegisteredServices;
+
+ bool mForcePersist;
};
bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
@@ -88,6 +95,14 @@
return true;
}
+void ClientCounterCallback::forcePersist(bool persist) {
+ mForcePersist = persist;
+ if(!mForcePersist) {
+ // Attempt a shutdown in case the number of clients hit 0 while the flag was on
+ tryShutdown();
+ }
+}
+
/**
* onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
* invocations could occur on different threads however.
@@ -103,14 +118,21 @@
mNumConnectedServices, mRegisteredServices.size(),
String8(service->getInterfaceDescriptor()).string(), clients);
- if (mNumConnectedServices == 0) {
- tryShutdown();
- }
-
+ tryShutdown();
return Status::ok();
}
void ClientCounterCallback::tryShutdown() {
+ if(mNumConnectedServices > 0) {
+ // Should only shut down if there are no clients
+ return;
+ }
+
+ if(mForcePersist) {
+ ALOGI("Shutdown prevented by forcePersist override flag.");
+ return;
+ }
+
ALOGI("Trying to shut down the service. No clients in use for any service in process.");
auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
@@ -165,5 +187,9 @@
return OK;
}
+void LazyServiceRegistrar::forcePersist(bool persist) {
+ mClientCC->forcePersist(persist);
+}
+
} // namespace hardware
} // namespace android
\ No newline at end of file