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

Commit 8fde87fe authored by Stephen Crane's avatar Stephen Crane
Browse files

libbinder_ndk: Expose UTF-8 interface descriptor string

We want to be able to expose the original char* interface descriptor
string from AIBinder_Class to NDK clients. Rather than converting the
String16 representation, which presents difficulties regarding
allocation cleanup, we just store an extra copy of the string for now.

Eventually, we would like to transition interface descriptors to UTF-8
and avoid String16 entirely, so this change is a partial step in that
direction and avoids exposing a UTF-16 string through the NDK API.

Bug: 167723746
Test: atest libbinder_ndk_unit_test CtsBinderNdkTestCases
Change-Id: I79d11f26245e229acde753f8c76c53cd0970c8ed
parent 47dcaf7c
Loading
Loading
Loading
Loading
+8 −1
Original line number Original line Diff line number Diff line
@@ -307,7 +307,8 @@ AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_o
    : onCreate(onCreate),
    : onCreate(onCreate),
      onDestroy(onDestroy),
      onDestroy(onDestroy),
      onTransact(onTransact),
      onTransact(onTransact),
      mInterfaceDescriptor(interfaceDescriptor) {}
      mInterfaceDescriptor(interfaceDescriptor),
      mWideInterfaceDescriptor(interfaceDescriptor) {}


AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
                                      AIBinder_Class_onCreate onCreate,
                                      AIBinder_Class_onCreate onCreate,
@@ -335,6 +336,12 @@ void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
    clazz->handleShellCommand = handleShellCommand;
    clazz->handleShellCommand = handleShellCommand;
}
}


const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
    CHECK(clazz != nullptr) << "getDescriptor requires non-null clazz";

    return clazz->getInterfaceDescriptorUtf8();
}

void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
    CHECK(who == mWho);
    CHECK(who == mWho);


+5 −2
Original line number Original line Diff line number Diff line
@@ -112,7 +112,8 @@ struct AIBinder_Class {
    AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
    AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
                   AIBinder_Class_onDestroy onDestroy, AIBinder_Class_onTransact onTransact);
                   AIBinder_Class_onDestroy onDestroy, AIBinder_Class_onTransact onTransact);


    const ::android::String16& getInterfaceDescriptor() const { return mInterfaceDescriptor; }
    const ::android::String16& getInterfaceDescriptor() const { return mWideInterfaceDescriptor; }
    const char* getInterfaceDescriptorUtf8() const { return mInterfaceDescriptor.c_str(); }


    // required to be non-null, implemented for every class
    // required to be non-null, implemented for every class
    const AIBinder_Class_onCreate onCreate = nullptr;
    const AIBinder_Class_onCreate onCreate = nullptr;
@@ -124,9 +125,11 @@ struct AIBinder_Class {
    AIBinder_handleShellCommand handleShellCommand = nullptr;
    AIBinder_handleShellCommand handleShellCommand = nullptr;


   private:
   private:
    // Copy of the raw char string for when we don't have to return UTF-16
    const std::string mInterfaceDescriptor;
    // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to
    // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to
    // one.
    // one.
    const ::android::String16 mInterfaceDescriptor;
    const ::android::String16 mWideInterfaceDescriptor;
};
};


// Ownership is like this (when linked to death):
// Ownership is like this (when linked to death):
+17 −0
Original line number Original line Diff line number Diff line
@@ -642,6 +642,23 @@ binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) __INTRODU


#endif  //__ANDROID_API__ >= 30
#endif  //__ANDROID_API__ >= 30


#if __ANDROID_API__ >= 31

/**
 * Retrieve the class descriptor for the class.
 *
 * Available since API level 31.
 *
 * \param clazz the class to fetch the descriptor from
 *
 * \return the class descriptor string. This pointer will never be null; a
 * descriptor is required to define a class. The pointer is owned by the class
 * and will remain valid as long as the class does.
 */
const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) __INTRODUCED_IN(31);

#endif  //__ANDROID_API__ >= 31

__END_DECLS
__END_DECLS


/** @} */
/** @} */
+1 −0
Original line number Original line Diff line number Diff line
@@ -121,6 +121,7 @@ LIBBINDER_NDK31 { # introduced=31
    AServiceManager_registerLazyService; # llndk
    AServiceManager_registerLazyService; # llndk
    AServiceManager_waitForService; # apex llndk
    AServiceManager_waitForService; # apex llndk


    AIBinder_Class_getDescriptor;
    AParcel_appendFrom;
    AParcel_appendFrom;
    AParcel_create;
    AParcel_create;
    AParcel_getDataSize;
    AParcel_getDataSize;
+1 −1
Original line number Original line Diff line number Diff line
@@ -25,7 +25,7 @@ using ::android::wp;


const char* IFoo::kSomeInstanceName = "libbinder_ndk-test-IFoo";
const char* IFoo::kSomeInstanceName = "libbinder_ndk-test-IFoo";
const char* IFoo::kInstanceNameToDieFor = "libbinder_ndk-test-IFoo-to-die";
const char* IFoo::kInstanceNameToDieFor = "libbinder_ndk-test-IFoo-to-die";
const char* kIFooDescriptor = "my-special-IFoo-class";
const char* IFoo::kIFooDescriptor = "my-special-IFoo-class";


struct IFoo_Class_Data {
struct IFoo_Class_Data {
    sp<IFoo> foo;
    sp<IFoo> foo;
Loading