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

Commit 02f75659 authored by Steven Moreland's avatar Steven Moreland
Browse files

libbinder_ndk: to/fromJavaBinder

This allows for a conversion between android.os.IBinder and AIBinder.

Bug: 111445392
Test: android.binder.cts

Change-Id: Icd3949d298bf602f7d24dc5ec92048df902095cb
parent 85eaa63b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ cc_library {

    srcs: [
        "ibinder.cpp",
        "ibinder_jni.cpp",
        "parcel.cpp",
        "process.cpp",
        "status.cpp",
@@ -32,6 +33,7 @@ cc_library {
    ],

    shared_libs: [
        "libandroid_runtime",
        "libbase",
        "libbinder",
        "libutils",
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android/binder_ibinder_jni.h>
#include "ibinder_internal.h"

#include <android_util_Binder.h>

using ::android::IBinder;
using ::android::ibinderForJavaObject;
using ::android::javaObjectForIBinder;
using ::android::sp;

AIBinder* AIBinder_fromJavaBinder(JNIEnv* env, jobject binder) {
    sp<IBinder> ibinder = ibinderForJavaObject(env, binder);

    sp<AIBinder> cbinder = ABpBinder::lookupOrCreateFromBinder(ibinder);
    AIBinder_incStrong(cbinder.get());

    return cbinder.get();
}

jobject AIBinder_toJavaBinder(JNIEnv* env, AIBinder* binder) {
    if (binder == nullptr) {
        return nullptr;
    }

    return javaObjectForIBinder(env, binder->getBinder());
}
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @addtogroup NdkBinder
 * @{
 */

/**
 * @file binder_ibinder_jni.h
 * @brief Conversions between AIBinder and android.os.IBinder
 */

#pragma once

#include <android/binder_ibinder.h>

#include <jni.h>

__BEGIN_DECLS

/**
 * Converts an android.os.IBinder object into an AIBinder* object.
 *
 * If either env or the binder is null, null is returned. If this binder object was originally an
 * AIBinder object, the original object is returned. The returned object has one refcount
 * associated with it, and so this should be accompanied with an AIBinder_decStrong call.
 */
__attribute__((warn_unused_result)) AIBinder* AIBinder_fromJavaBinder(JNIEnv* env, jobject binder);

/**
 * Converts an AIBinder* object into an android.os.IBinder object.
 *
 * If either env or the binder is null, null is returned. If this binder object was originally an
 * IBinder object, the original java object will be returned.
 */
__attribute__((warn_unused_result)) jobject AIBinder_toJavaBinder(JNIEnv* env, AIBinder* binder);

__END_DECLS

/** @} */
+2 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ LIBBINDER_NDK { # introduced=29
    AIBinder_DeathRecipient_new;
    AIBinder_debugGetRefCount;
    AIBinder_decStrong;
    AIBinder_fromJavaBinder;
    AIBinder_getClass;
    AIBinder_getUserData;
    AIBinder_incStrong;
@@ -15,6 +16,7 @@ LIBBINDER_NDK { # introduced=29
    AIBinder_new;
    AIBinder_ping;
    AIBinder_prepareTransaction;
    AIBinder_toJavaBinder;
    AIBinder_transact;
    AIBinder_unlinkToDeath;
    AIBinder_Weak_delete;
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ echo "LIBBINDER_NDK { # introduced=29"
echo "  global:"
{
    grep -oP "AIBinder_[a-zA-Z0-9_]+(?=\()" include_ndk/android/binder_ibinder.h;
    grep -oP "AIBinder_[a-zA-Z0-9_]+(?=\()" include_ndk/android/binder_ibinder_jni.h;
    grep -oP "AParcel_[a-zA-Z0-9_]+(?=\()" include_ndk/android/binder_parcel.h;
    grep -oP "AStatus_[a-zA-Z0-9_]+(?=\()" include_ndk/android/binder_status.h;
} | sort | uniq | awk '{ print "    " $0 ";"; }'
Loading