diff options
author | 2024-04-22 14:21:07 +0000 | |
---|---|---|
committer | 2024-06-20 10:17:34 +0000 | |
commit | 56a0471ed60e551925dfc8d76cb061ffa8d10122 (patch) | |
tree | 33fcbf6d48abc9d08351b130c8a6371ba3e942c0 /libs/binder/IServiceManager.cpp | |
parent | 9f0a7d383bfa8fae53d9e362179b38d8be3f8d13 (diff) |
Add a BackendUnifiedServiceManager wrapper
This can be directly used by java and rust ffi
Test: atest aidl_integration_test
Bug: 333854840
Flag: EXEMPT refactor
Change-Id: I12c3730a89422bf08ab723a82888431f291bd7b3
Diffstat (limited to 'libs/binder/IServiceManager.cpp')
-rw-r--r-- | libs/binder/IServiceManager.cpp | 70 |
1 files changed, 24 insertions, 46 deletions
diff --git a/libs/binder/IServiceManager.cpp b/libs/binder/IServiceManager.cpp index fbcf823045..5844c85d21 100644 --- a/libs/binder/IServiceManager.cpp +++ b/libs/binder/IServiceManager.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "ServiceManagerCppClient" +#include <BackendUnifiedServiceManager.h> #include <binder/IServiceManager.h> #include <inttypes.h> @@ -111,14 +112,12 @@ public: std::vector<IServiceManager::ServiceDebugInfo> getServiceDebugInfo() override; // for legacy ABI const String16& getInterfaceDescriptor() const override { - return mTheRealServiceManager->getInterfaceDescriptor(); - } - IBinder* onAsBinder() override { - return IInterface::asBinder(mTheRealServiceManager).get(); + return mUnifiedServiceManager->getInterfaceDescriptor(); } + IBinder* onAsBinder() override { return IInterface::asBinder(mUnifiedServiceManager).get(); } protected: - sp<AidlServiceManager> mTheRealServiceManager; + sp<BackendUnifiedServiceManager> mUnifiedServiceManager; // AidlRegistrationCallback -> services that its been registered for // notifications. using LocalRegistrationAndWaiter = @@ -136,9 +135,9 @@ protected: // will still have the 5s delay that is expected by a large amount of Android code. // // When implementing ServiceManagerShim, use realGetService instead of - // mTheRealServiceManager->getService so that it can be overridden in ServiceManagerHostShim. + // mUnifiedServiceManager->getService so that it can be overridden in ServiceManagerHostShim. virtual Status realGetService(const std::string& name, sp<IBinder>* _aidl_return) { - return mTheRealServiceManager->getService(name, _aidl_return); + return mUnifiedServiceManager->getService(name, _aidl_return); } }; @@ -148,26 +147,7 @@ protected: sp<IServiceManager> defaultServiceManager() { std::call_once(gSmOnce, []() { -#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) - /* wait for service manager */ { - using std::literals::chrono_literals::operator""s; - using android::base::WaitForProperty; - while (!WaitForProperty("servicemanager.ready", "true", 1s)) { - ALOGE("Waited for servicemanager.ready for a second, waiting another..."); - } - } -#endif - - sp<AidlServiceManager> sm = nullptr; - while (sm == nullptr) { - sm = interface_cast<AidlServiceManager>(ProcessState::self()->getContextObject(nullptr)); - if (sm == nullptr) { - ALOGE("Waiting 1s on context object on %s.", ProcessState::self()->getDriverName().c_str()); - sleep(1); - } - } - - gDefaultServiceManager = sp<ServiceManagerShim>::make(sm); + gDefaultServiceManager = sp<ServiceManagerShim>::make(getBackendUnifiedServiceManager()); }); return gDefaultServiceManager; @@ -290,9 +270,9 @@ void* openDeclaredPassthroughHal(const String16& interface, const String16& inst // ---------------------------------------------------------------------- -ServiceManagerShim::ServiceManagerShim(const sp<AidlServiceManager>& impl) - : mTheRealServiceManager(impl) -{} +ServiceManagerShim::ServiceManagerShim(const sp<AidlServiceManager>& impl) { + mUnifiedServiceManager = sp<BackendUnifiedServiceManager>::make(impl); +} // This implementation could be simplified and made more efficient by delegating // to waitForService. However, this changes the threading structure in some @@ -345,7 +325,7 @@ sp<IBinder> ServiceManagerShim::getService(const String16& name) const sp<IBinder> ServiceManagerShim::checkService(const String16& name) const { sp<IBinder> ret; - if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) { + if (!mUnifiedServiceManager->checkService(String8(name).c_str(), &ret).isOk()) { return nullptr; } return ret; @@ -354,15 +334,15 @@ sp<IBinder> ServiceManagerShim::checkService(const String16& name) const status_t ServiceManagerShim::addService(const String16& name, const sp<IBinder>& service, bool allowIsolated, int dumpsysPriority) { - Status status = mTheRealServiceManager->addService( - String8(name).c_str(), service, allowIsolated, dumpsysPriority); + Status status = mUnifiedServiceManager->addService(String8(name).c_str(), service, + allowIsolated, dumpsysPriority); return status.exceptionCode(); } Vector<String16> ServiceManagerShim::listServices(int dumpsysPriority) { std::vector<std::string> ret; - if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) { + if (!mUnifiedServiceManager->listServices(dumpsysPriority, &ret).isOk()) { return {}; } @@ -420,15 +400,13 @@ sp<IBinder> ServiceManagerShim::waitForService(const String16& name16) if (out != nullptr) return out; sp<Waiter> waiter = sp<Waiter>::make(); - if (Status status = mTheRealServiceManager->registerForNotifications(name, waiter); + if (Status status = mUnifiedServiceManager->registerForNotifications(name, waiter); !status.isOk()) { ALOGW("Failed to registerForNotifications in waitForService for %s: %s", name.c_str(), status.toString8().c_str()); return nullptr; } - Defer unregister ([&] { - mTheRealServiceManager->unregisterForNotifications(name, waiter); - }); + Defer unregister([&] { mUnifiedServiceManager->unregisterForNotifications(name, waiter); }); while(true) { { @@ -469,7 +447,7 @@ sp<IBinder> ServiceManagerShim::waitForService(const String16& name16) bool ServiceManagerShim::isDeclared(const String16& name) { bool declared; - if (Status status = mTheRealServiceManager->isDeclared(String8(name).c_str(), &declared); + if (Status status = mUnifiedServiceManager->isDeclared(String8(name).c_str(), &declared); !status.isOk()) { ALOGW("Failed to get isDeclared for %s: %s", String8(name).c_str(), status.toString8().c_str()); @@ -481,7 +459,7 @@ bool ServiceManagerShim::isDeclared(const String16& name) { Vector<String16> ServiceManagerShim::getDeclaredInstances(const String16& interface) { std::vector<std::string> out; if (Status status = - mTheRealServiceManager->getDeclaredInstances(String8(interface).c_str(), &out); + mUnifiedServiceManager->getDeclaredInstances(String8(interface).c_str(), &out); !status.isOk()) { ALOGW("Failed to getDeclaredInstances for %s: %s", String8(interface).c_str(), status.toString8().c_str()); @@ -498,7 +476,7 @@ Vector<String16> ServiceManagerShim::getDeclaredInstances(const String16& interf std::optional<String16> ServiceManagerShim::updatableViaApex(const String16& name) { std::optional<std::string> declared; - if (Status status = mTheRealServiceManager->updatableViaApex(String8(name).c_str(), &declared); + if (Status status = mUnifiedServiceManager->updatableViaApex(String8(name).c_str(), &declared); !status.isOk()) { ALOGW("Failed to get updatableViaApex for %s: %s", String8(name).c_str(), status.toString8().c_str()); @@ -509,7 +487,7 @@ std::optional<String16> ServiceManagerShim::updatableViaApex(const String16& nam Vector<String16> ServiceManagerShim::getUpdatableNames(const String16& apexName) { std::vector<std::string> out; - if (Status status = mTheRealServiceManager->getUpdatableNames(String8(apexName).c_str(), &out); + if (Status status = mUnifiedServiceManager->getUpdatableNames(String8(apexName).c_str(), &out); !status.isOk()) { ALOGW("Failed to getUpdatableNames for %s: %s", String8(apexName).c_str(), status.toString8().c_str()); @@ -528,7 +506,7 @@ std::optional<IServiceManager::ConnectionInfo> ServiceManagerShim::getConnection const String16& name) { std::optional<os::ConnectionInfo> connectionInfo; if (Status status = - mTheRealServiceManager->getConnectionInfo(String8(name).c_str(), &connectionInfo); + mUnifiedServiceManager->getConnectionInfo(String8(name).c_str(), &connectionInfo); !status.isOk()) { ALOGW("Failed to get ConnectionInfo for %s: %s", String8(name).c_str(), status.toString8().c_str()); @@ -549,7 +527,7 @@ status_t ServiceManagerShim::registerForNotifications(const String16& name, sp<RegistrationWaiter> registrationWaiter = sp<RegistrationWaiter>::make(cb); std::lock_guard<std::mutex> lock(mNameToRegistrationLock); if (Status status = - mTheRealServiceManager->registerForNotifications(nameStr, registrationWaiter); + mUnifiedServiceManager->registerForNotifications(nameStr, registrationWaiter); !status.isOk()) { ALOGW("Failed to registerForNotifications for %s: %s", nameStr.c_str(), status.toString8().c_str()); @@ -600,7 +578,7 @@ status_t ServiceManagerShim::unregisterForNotifications(const String16& name, ALOGE("%s Callback passed wasn't used to register for notifications", __FUNCTION__); return BAD_VALUE; } - if (Status status = mTheRealServiceManager->unregisterForNotifications(String8(name).c_str(), + if (Status status = mUnifiedServiceManager->unregisterForNotifications(String8(name).c_str(), registrationWaiter); !status.isOk()) { ALOGW("Failed to get service manager to unregisterForNotifications for %s: %s", @@ -613,7 +591,7 @@ status_t ServiceManagerShim::unregisterForNotifications(const String16& name, std::vector<IServiceManager::ServiceDebugInfo> ServiceManagerShim::getServiceDebugInfo() { std::vector<os::ServiceDebugInfo> serviceDebugInfos; std::vector<IServiceManager::ServiceDebugInfo> ret; - if (Status status = mTheRealServiceManager->getServiceDebugInfo(&serviceDebugInfos); + if (Status status = mUnifiedServiceManager->getServiceDebugInfo(&serviceDebugInfos); !status.isOk()) { ALOGW("%s Failed to get ServiceDebugInfo", __FUNCTION__); return ret; |