1616#include " BackendUnifiedServiceManager.h"
1717
1818#include < android/os/IAccessor.h>
19+ #include < android/os/IServiceManager.h>
1920#include < binder/RpcSession.h>
2021
2122#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
@@ -36,6 +37,12 @@ constexpr bool kUseCacheInAddService = true;
3637constexpr bool kUseCacheInAddService = false ;
3738#endif
3839
40+ #ifdef LIBBINDER_REMOVE_CACHE_STATIC_LIST
41+ constexpr bool kRemoveStaticList = true ;
42+ #else
43+ constexpr bool kRemoveStaticList = false ;
44+ #endif
45+
3946using AidlServiceManager = android::os::IServiceManager;
4047using android::os::IAccessor;
4148using binder::Status;
@@ -110,6 +117,13 @@ static const char* kStaticCachableList[] = {
110117 // go/keep-sorted end
111118};
112119
120+ os::ServiceWithMetadata createServiceWithMetadata (const sp<IBinder>& service, bool isLazyService) {
121+ os::ServiceWithMetadata out = os::ServiceWithMetadata ();
122+ out.service = service;
123+ out.isLazyService = isLazyService;
124+ return out;
125+ }
126+
113127bool BinderCacheWithInvalidation::isClientSideCachingEnabled (const std::string& serviceName) {
114128 sp<ProcessState> self = ProcessState::selfOrNull ();
115129 if (!self || self->getThreadPoolMaxTotalThreadCount () <= 0 ) {
@@ -132,15 +146,21 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
132146 return Status::ok ();
133147 }
134148
135- if (service.getTag () == os::Service::Tag::binder) {
136- return updateCache (serviceName, service.get <os::Service::Tag::binder>());
149+ if (service.getTag () == os::Service::Tag::serviceWithMetadata) {
150+ auto serviceWithMetadata = service.get <os::Service::Tag::serviceWithMetadata>();
151+ return updateCache (serviceName, serviceWithMetadata.service ,
152+ serviceWithMetadata.isLazyService );
137153 }
138154 return Status::ok ();
139155}
140156
141157Status BackendUnifiedServiceManager::updateCache (const std::string& serviceName,
142- const sp<IBinder>& binder) {
158+ const sp<IBinder>& binder, bool isServiceLazy ) {
143159 std::string traceStr;
160+ // Don't cache if service is lazy
161+ if (kRemoveStaticList && isServiceLazy) {
162+ return Status::ok ();
163+ }
144164 if (atrace_is_tag_enabled (ATRACE_TAG_AIDL)) {
145165 traceStr = " BinderCacheWithInvalidation::updateCache : " + serviceName;
146166 }
@@ -153,7 +173,9 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
153173 binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL,
154174 " BinderCacheWithInvalidation::updateCache failed: "
155175 " isBinderAlive_false" );
156- } else if (mCacheForGetService ->isClientSideCachingEnabled (serviceName)) {
176+ }
177+ // If we reach here with kRemoveStaticList=true then we know service isn't lazy
178+ else if (kRemoveStaticList || mCacheForGetService ->isClientSideCachingEnabled (serviceName)) {
157179 binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL,
158180 " BinderCacheWithInvalidation::updateCache successful" );
159181 return mCacheForGetService ->setItem (serviceName, binder);
@@ -173,7 +195,7 @@ bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName
173195 sp<IBinder> item = mCacheForGetService ->getItem (serviceName);
174196 // TODO(b/363177618): Enable caching for binders which are always null.
175197 if (item != nullptr && item->isBinderAlive ()) {
176- *_out = os::Service::make<os::Service::Tag::binder> (item);
198+ *_out = createServiceWithMetadata (item, false );
177199 return true ;
178200 }
179201 return false ;
@@ -188,7 +210,7 @@ Status BackendUnifiedServiceManager::getService(const ::std::string& name,
188210 sp<IBinder>* _aidl_return) {
189211 os::Service service;
190212 Status status = getService2 (name, &service);
191- *_aidl_return = service.get <os::Service::Tag::binder >();
213+ *_aidl_return = service.get <os::Service::Tag::serviceWithMetadata >(). service ;
192214 return status;
193215}
194216
@@ -227,14 +249,16 @@ Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os:
227249Status BackendUnifiedServiceManager::toBinderService (const ::std::string& name,
228250 const os::Service& in, os::Service* _out) {
229251 switch (in.getTag ()) {
230- case os::Service::Tag::binder: {
231- if (in.get <os::Service::Tag::binder>() == nullptr ) {
252+ case os::Service::Tag::serviceWithMetadata: {
253+ auto serviceWithMetadata = in.get <os::Service::Tag::serviceWithMetadata>();
254+ if (serviceWithMetadata.service == nullptr ) {
232255 // failed to find a service. Check to see if we have any local
233256 // injected Accessors for this service.
234257 os::Service accessor;
235258 Status status = getInjectedAccessor (name, &accessor);
236259 if (!status.isOk ()) {
237- *_out = os::Service::make<os::Service::Tag::binder>(nullptr );
260+ *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
261+ createServiceWithMetadata (nullptr , false ));
238262 return status;
239263 }
240264 if (accessor.getTag () == os::Service::Tag::accessor &&
@@ -255,7 +279,8 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
255279 sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
256280 if (accessor == nullptr ) {
257281 ALOGE (" Service#accessor doesn't have accessor. VM is maybe starting..." );
258- *_out = os::Service::make<os::Service::Tag::binder>(nullptr );
282+ *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
283+ createServiceWithMetadata (nullptr , false ));
259284 return Status::ok ();
260285 }
261286 auto request = [=] {
@@ -276,7 +301,8 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
276301 return Status::fromStatusT (status);
277302 }
278303 session->setSessionSpecificRoot (accessorBinder);
279- *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject ());
304+ *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
305+ createServiceWithMetadata (session->getRootObject (), false ));
280306 return Status::ok ();
281307 }
282308 default : {
@@ -291,7 +317,8 @@ Status BackendUnifiedServiceManager::addService(const ::std::string& name,
291317 Status status = mTheRealServiceManager ->addService (name, service, allowIsolated, dumpPriority);
292318 // mEnableAddServiceCache is true by default.
293319 if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk ()) {
294- return updateCache (name, service);
320+ return updateCache (name, service,
321+ dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
295322 }
296323 return status;
297324}
0 commit comments