Loading libs/binder/LazyServiceRegistrar.cpp +37 −13 Original line number Diff line number Diff line Loading @@ -29,16 +29,12 @@ namespace internal { using AidlServiceManager = android::os::IServiceManager; class ClientCounterCallback : public ::android::os::BnClientCallback { class ClientCounterCallbackImpl : public ::android::os::BnClientCallback { public: ClientCounterCallback() : mNumConnectedServices(0), mForcePersist(false) {} ClientCounterCallbackImpl() : 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: Loading Loading @@ -75,7 +71,23 @@ private: bool mForcePersist; }; bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name, class ClientCounterCallback { public: ClientCounterCallback(); 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); private: sp<ClientCounterCallbackImpl> mImpl; }; bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name, bool allowIsolated, int dumpFlags) { auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager())); Loading Loading @@ -105,7 +117,7 @@ bool ClientCounterCallback::registerService(const sp<IBinder>& service, const st return true; } std::map<std::string, ClientCounterCallback::Service>::iterator ClientCounterCallback::assertRegisteredService(const sp<IBinder>& service) { std::map<std::string, ClientCounterCallbackImpl::Service>::iterator ClientCounterCallbackImpl::assertRegisteredService(const sp<IBinder>& service) { LOG_ALWAYS_FATAL_IF(service == nullptr, "Got onClients callback for null service"); for (auto it = mRegisteredServices.begin(); it != mRegisteredServices.end(); ++it) { auto const& [name, registered] = *it; Loading @@ -117,7 +129,7 @@ std::map<std::string, ClientCounterCallback::Service>::iterator ClientCounterCal __builtin_unreachable(); } void ClientCounterCallback::forcePersist(bool persist) { void ClientCounterCallbackImpl::forcePersist(bool persist) { mForcePersist = persist; if(!mForcePersist) { // Attempt a shutdown in case the number of clients hit 0 while the flag was on Loading @@ -129,7 +141,7 @@ void ClientCounterCallback::forcePersist(bool persist) { * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple * invocations could occur on different threads however. */ Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients) { Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) { auto & [name, registered] = *assertRegisteredService(service); if (registered.clients == clients) { LOG_ALWAYS_FATAL("Process already thought %s had clients: %d but servicemanager has " Loading @@ -154,7 +166,7 @@ Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients return Status::ok(); } void ClientCounterCallback::tryShutdown() { void ClientCounterCallbackImpl::tryShutdown() { if(mNumConnectedServices > 0) { // Should only shut down if there are no clients return; Loading @@ -175,7 +187,6 @@ void ClientCounterCallback::tryShutdown() { bool success = manager->tryUnregisterService(entry.first, entry.second.service).isOk(); if (!success) { ALOGI("Failed to unregister service %s", entry.first.c_str()); break; Loading @@ -200,6 +211,19 @@ void ClientCounterCallback::tryShutdown() { } } ClientCounterCallback::ClientCounterCallback() { mImpl = sp<ClientCounterCallbackImpl>::make(); } bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name, bool allowIsolated, int dumpFlags) { return mImpl->registerService(service, name, allowIsolated, dumpFlags); } void ClientCounterCallback::forcePersist(bool persist) { mImpl->forcePersist(persist); } } // namespace internal LazyServiceRegistrar::LazyServiceRegistrar() { Loading Loading
libs/binder/LazyServiceRegistrar.cpp +37 −13 Original line number Diff line number Diff line Loading @@ -29,16 +29,12 @@ namespace internal { using AidlServiceManager = android::os::IServiceManager; class ClientCounterCallback : public ::android::os::BnClientCallback { class ClientCounterCallbackImpl : public ::android::os::BnClientCallback { public: ClientCounterCallback() : mNumConnectedServices(0), mForcePersist(false) {} ClientCounterCallbackImpl() : 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: Loading Loading @@ -75,7 +71,23 @@ private: bool mForcePersist; }; bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name, class ClientCounterCallback { public: ClientCounterCallback(); 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); private: sp<ClientCounterCallbackImpl> mImpl; }; bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name, bool allowIsolated, int dumpFlags) { auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager())); Loading Loading @@ -105,7 +117,7 @@ bool ClientCounterCallback::registerService(const sp<IBinder>& service, const st return true; } std::map<std::string, ClientCounterCallback::Service>::iterator ClientCounterCallback::assertRegisteredService(const sp<IBinder>& service) { std::map<std::string, ClientCounterCallbackImpl::Service>::iterator ClientCounterCallbackImpl::assertRegisteredService(const sp<IBinder>& service) { LOG_ALWAYS_FATAL_IF(service == nullptr, "Got onClients callback for null service"); for (auto it = mRegisteredServices.begin(); it != mRegisteredServices.end(); ++it) { auto const& [name, registered] = *it; Loading @@ -117,7 +129,7 @@ std::map<std::string, ClientCounterCallback::Service>::iterator ClientCounterCal __builtin_unreachable(); } void ClientCounterCallback::forcePersist(bool persist) { void ClientCounterCallbackImpl::forcePersist(bool persist) { mForcePersist = persist; if(!mForcePersist) { // Attempt a shutdown in case the number of clients hit 0 while the flag was on Loading @@ -129,7 +141,7 @@ void ClientCounterCallback::forcePersist(bool persist) { * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple * invocations could occur on different threads however. */ Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients) { Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) { auto & [name, registered] = *assertRegisteredService(service); if (registered.clients == clients) { LOG_ALWAYS_FATAL("Process already thought %s had clients: %d but servicemanager has " Loading @@ -154,7 +166,7 @@ Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients return Status::ok(); } void ClientCounterCallback::tryShutdown() { void ClientCounterCallbackImpl::tryShutdown() { if(mNumConnectedServices > 0) { // Should only shut down if there are no clients return; Loading @@ -175,7 +187,6 @@ void ClientCounterCallback::tryShutdown() { bool success = manager->tryUnregisterService(entry.first, entry.second.service).isOk(); if (!success) { ALOGI("Failed to unregister service %s", entry.first.c_str()); break; Loading @@ -200,6 +211,19 @@ void ClientCounterCallback::tryShutdown() { } } ClientCounterCallback::ClientCounterCallback() { mImpl = sp<ClientCounterCallbackImpl>::make(); } bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name, bool allowIsolated, int dumpFlags) { return mImpl->registerService(service, name, allowIsolated, dumpFlags); } void ClientCounterCallback::forcePersist(bool persist) { mImpl->forcePersist(persist); } } // namespace internal LazyServiceRegistrar::LazyServiceRegistrar() { Loading