@@ -30,6 +30,12 @@ constexpr bool kUseCache = true;
3030constexpr bool kUseCache = false ;
3131#endif
3232
33+ #ifdef LIBBINDER_ADDSERVICE_CACHE
34+ constexpr bool kUseCacheInAddService = true ;
35+ #else
36+ constexpr bool kUseCacheInAddService = false ;
37+ #endif
38+
3339using AidlServiceManager = android::os::IServiceManager;
3440using android::os::IAccessor;
3541using binder::Status;
@@ -125,31 +131,36 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
125131 if (!kUseCache ) {
126132 return Status::ok ();
127133 }
134+
135+ if (service.getTag () == os::Service::Tag::binder) {
136+ return updateCache (serviceName, service.get <os::Service::Tag::binder>());
137+ }
138+ return Status::ok ();
139+ }
140+
141+ Status BackendUnifiedServiceManager::updateCache (const std::string& serviceName,
142+ const sp<IBinder>& binder) {
128143 std::string traceStr;
129144 if (atrace_is_tag_enabled (ATRACE_TAG_AIDL)) {
130145 traceStr = " BinderCacheWithInvalidation::updateCache : " + serviceName;
131146 }
132147 binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL, traceStr.c_str ());
133-
134- if (service.getTag () == os::Service::Tag::binder) {
135- sp<IBinder> binder = service.get <os::Service::Tag::binder>();
136- if (!binder) {
137- binder::ScopedTrace
138- aidlTrace (ATRACE_TAG_AIDL,
139- " BinderCacheWithInvalidation::updateCache failed: binder_null" );
140- } else if (!binder->isBinderAlive ()) {
141- binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL,
142- " BinderCacheWithInvalidation::updateCache failed: "
143- " isBinderAlive_false" );
144- } else if (mCacheForGetService ->isClientSideCachingEnabled (serviceName)) {
145- binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL,
146- " BinderCacheWithInvalidation::updateCache successful" );
147- return mCacheForGetService ->setItem (serviceName, binder);
148- } else {
149- binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL,
150- " BinderCacheWithInvalidation::updateCache failed: "
151- " caching_not_enabled" );
152- }
148+ if (!binder) {
149+ binder::ScopedTrace
150+ aidlTrace (ATRACE_TAG_AIDL,
151+ " BinderCacheWithInvalidation::updateCache failed: binder_null" );
152+ } else if (!binder->isBinderAlive ()) {
153+ binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL,
154+ " BinderCacheWithInvalidation::updateCache failed: "
155+ " isBinderAlive_false" );
156+ } else if (mCacheForGetService ->isClientSideCachingEnabled (serviceName)) {
157+ binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL,
158+ " BinderCacheWithInvalidation::updateCache successful" );
159+ return mCacheForGetService ->setItem (serviceName, binder);
160+ } else {
161+ binder::ScopedTrace aidlTrace (ATRACE_TAG_AIDL,
162+ " BinderCacheWithInvalidation::updateCache failed: "
163+ " caching_not_enabled" );
153164 }
154165 return Status::ok ();
155166}
@@ -277,7 +288,12 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
277288Status BackendUnifiedServiceManager::addService (const ::std::string& name,
278289 const sp<IBinder>& service, bool allowIsolated,
279290 int32_t dumpPriority) {
280- return mTheRealServiceManager ->addService (name, service, allowIsolated, dumpPriority);
291+ Status status = mTheRealServiceManager ->addService (name, service, allowIsolated, dumpPriority);
292+ // mEnableAddServiceCache is true by default.
293+ if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk ()) {
294+ return updateCache (name, service);
295+ }
296+ return status;
281297}
282298Status BackendUnifiedServiceManager::listServices (int32_t dumpPriority,
283299 ::std::vector<::std::string>* _aidl_return) {
0 commit comments