Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 33ea854b authored by Steven Moreland's avatar Steven Moreland Committed by Gerrit Code Review
Browse files

Merge "libbinder_ndk: expose declared services list"

parents 82a8c40f d687f33d
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -95,6 +95,22 @@ __attribute__((warn_unused_result)) AIBinder* AServiceManager_waitForService(con
 */
bool AServiceManager_isDeclared(const char* instance) __INTRODUCED_IN(31);

/**
 * Returns all declared instances for a particular interface.
 *
 * For instance, if 'android.foo.IFoo/foo' is declared, and 'android.foo.IFoo' is
 * passed here, then ["foo"] would be returned.
 *
 * See also AServiceManager_isDeclared.
 *
 * \param interface interface, e.g. 'android.foo.IFoo'
 * \param context to pass to callback
 * \param callback taking instance (e.g. 'foo') and context
 */
void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
                                             void (*callback)(const char*, void*))
        __INTRODUCED_IN(31);

/**
 * Prevent lazy services without client from shutting down their process
 *
+1 −0
Original line number Diff line number Diff line
@@ -118,6 +118,7 @@ LIBBINDER_NDK31 { # introduced=31
    AIBinder_getCallingSid; # apex
    AIBinder_setRequestingSid; # apex
    AServiceManager_isDeclared; # apex llndk
    AServiceManager_forEachDeclaredInstance; # apex llndk
    AServiceManager_registerLazyService; # llndk
    AServiceManager_waitForService; # apex llndk
    AServiceManager_forceLazyServicesPersist; # llndk
+14 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include "ibinder_internal.h"
#include "status_internal.h"

#include <android-base/logging.h>
#include <binder/IServiceManager.h>
#include <binder/LazyServiceRegistrar.h>

@@ -28,6 +29,7 @@ using ::android::IServiceManager;
using ::android::sp;
using ::android::status_t;
using ::android::String16;
using ::android::String8;

binder_exception_t AServiceManager_addService(AIBinder* binder, const char* instance) {
    if (binder == nullptr || instance == nullptr) {
@@ -92,6 +94,17 @@ bool AServiceManager_isDeclared(const char* instance) {
    sp<IServiceManager> sm = defaultServiceManager();
    return sm->isDeclared(String16(instance));
}
void AServiceManager_forEachDeclaredInstance(const char* interface, void* context,
                                             void (*callback)(const char*, void*)) {
    CHECK(interface != nullptr);
    // context may be nullptr
    CHECK(callback != nullptr);

    sp<IServiceManager> sm = defaultServiceManager();
    for (const String16& instance : sm->getDeclaredInstances(String16(interface))) {
        callback(String8(instance).c_str(), context);
    }
}
void AServiceManager_forceLazyServicesPersist(bool persist) {
    auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
    serviceRegistrar.forcePersist(persist);
+19 −0
Original line number Diff line number Diff line
@@ -270,6 +270,25 @@ TEST(NdkBinder, DoubleNumber) {
    EXPECT_EQ(2, out);
}

void defaultInstanceCounter(const char* instance, void* context) {
    if (strcmp(instance, "default") == 0) {
        ++*(size_t*)(context);
    }
}

TEST(NdkBinder, GetDeclaredInstances) {
    bool hasLight = AServiceManager_isDeclared("android.hardware.light.ILights/default");

    size_t count;
    AServiceManager_forEachDeclaredInstance("android.hardware.light.ILights", &count,
                                            defaultInstanceCounter);

    // At the time of writing this test, there is no good interface guaranteed
    // to be on all devices. Cuttlefish has light, so this will generally test
    // things.
    EXPECT_EQ(count, hasLight ? 1 : 0);
}

TEST(NdkBinder, GetLazyService) {
    // Not declared in the vintf manifest
    ASSERT_FALSE(AServiceManager_isDeclared(kLazyBinderNdkUnitTestService));