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

Commit 5f89c0c3 authored by Steven Moreland's avatar Steven Moreland Committed by Automerger Merge Worker
Browse files

Merge "libbinder_ndk_unit_test: show cookie can own data" am: 4a0ed553 am:...

Merge "libbinder_ndk_unit_test: show cookie can own data" am: 4a0ed553 am: 888f52f0 am: 4dea7442

Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/2583679



Change-Id: I11c971f35b896d0ba4d8c7136d0a56bf6a9b068f
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 6b9fe247 4dea7442
Loading
Loading
Loading
Loading
+16 −2
Original line number Original line Diff line number Diff line
@@ -497,14 +497,28 @@ TEST(NdkBinder, ActiveServicesCallbackTest) {


struct DeathRecipientCookie {
struct DeathRecipientCookie {
    std::function<void(void)>*onDeath, *onUnlink;
    std::function<void(void)>*onDeath, *onUnlink;

    // may contain additional data
    // - if it contains AIBinder, then you must call AIBinder_unlinkToDeath manually,
    //   because it would form a strong reference cycle
    // - if it points to a data member of another structure, this should have a weak
    //   promotable reference or a strong reference, in case that object is deleted
    //   while the death recipient is firing
};
};
void LambdaOnDeath(void* cookie) {
void LambdaOnDeath(void* cookie) {
    auto funcs = static_cast<DeathRecipientCookie*>(cookie);
    auto funcs = static_cast<DeathRecipientCookie*>(cookie);

    // may reference other cookie members

    (*funcs->onDeath)();
    (*funcs->onDeath)();
};
};
void LambdaOnUnlink(void* cookie) {
void LambdaOnUnlink(void* cookie) {
    auto funcs = static_cast<DeathRecipientCookie*>(cookie);
    auto funcs = static_cast<DeathRecipientCookie*>(cookie);
    (*funcs->onUnlink)();
    (*funcs->onUnlink)();

    // may reference other cookie members

    delete funcs;
};
};
TEST(NdkBinder, DeathRecipient) {
TEST(NdkBinder, DeathRecipient) {
    using namespace std::chrono_literals;
    using namespace std::chrono_literals;
@@ -536,12 +550,12 @@ TEST(NdkBinder, DeathRecipient) {
        unlinkCv.notify_one();
        unlinkCv.notify_one();
    };
    };


    DeathRecipientCookie cookie = {&onDeath, &onUnlink};
    DeathRecipientCookie* cookie = new DeathRecipientCookie{&onDeath, &onUnlink};


    AIBinder_DeathRecipient* recipient = AIBinder_DeathRecipient_new(LambdaOnDeath);
    AIBinder_DeathRecipient* recipient = AIBinder_DeathRecipient_new(LambdaOnDeath);
    AIBinder_DeathRecipient_setOnUnlinked(recipient, LambdaOnUnlink);
    AIBinder_DeathRecipient_setOnUnlinked(recipient, LambdaOnUnlink);


    EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, static_cast<void*>(&cookie)));
    EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, static_cast<void*>(cookie)));


    // the binder driver should return this if the service dies during the transaction
    // the binder driver should return this if the service dies during the transaction
    EXPECT_EQ(STATUS_DEAD_OBJECT, foo->die());
    EXPECT_EQ(STATUS_DEAD_OBJECT, foo->die());