diff options
| author | 2020-01-09 14:29:35 -0800 | |
|---|---|---|
| committer | 2020-01-09 14:29:35 -0800 | |
| commit | 473f905b3324da95871aeaaaed0c97ceb7fe742c (patch) | |
| tree | 69d65c6b0a564c6428c62c3a7b6ae9fe454907fa | |
| parent | bc7e63f317ec0e220226611b6c4c9a2cfefcfd82 (diff) | |
| parent | 7c615582a00a4b722a010d2fc6789b2ca039d591 (diff) | |
libbinder_ndk: AParcel_fromJavaParcel
am: 7c615582a0
Change-Id: I10b3cc5940e1efb6dbf248023520a7a860e7db0e
| -rw-r--r-- | libs/android_runtime_lazy/android_runtime_lazy.cpp | 22 | ||||
| -rw-r--r-- | libs/android_runtime_lazy/include/android_os_Parcel.h | 30 | ||||
| -rw-r--r-- | libs/binder/ndk/Android.bp | 1 | ||||
| -rw-r--r-- | libs/binder/ndk/include_ndk/android/binder_parcel_jni.h | 56 | ||||
| -rw-r--r-- | libs/binder/ndk/libbinder_ndk.map.txt | 1 | ||||
| -rw-r--r-- | libs/binder/ndk/parcel_jni.cpp | 37 |
6 files changed, 147 insertions, 0 deletions
diff --git a/libs/android_runtime_lazy/android_runtime_lazy.cpp b/libs/android_runtime_lazy/android_runtime_lazy.cpp index 98d8e8a511..8062be676d 100644 --- a/libs/android_runtime_lazy/android_runtime_lazy.cpp +++ b/libs/android_runtime_lazy/android_runtime_lazy.cpp @@ -15,6 +15,7 @@ */ #define LOG_TAG "ANDROID_RUNTIME_LAZY" #include "android_runtime/AndroidRuntime.h" +#include "android_os_Parcel.h" #include "android_util_Binder.h" #include <dlfcn.h> @@ -28,12 +29,18 @@ namespace { std::once_flag loadFlag; typedef JNIEnv* (*getJNIEnv_t)(); + +// android_util_Binder.h typedef sp<IBinder> (*ibinderForJavaObject_t)(JNIEnv* env, jobject obj); typedef jobject (*javaObjectForIBinder_t)(JNIEnv* env, const sp<IBinder>& val); +// android_os_Parcel.h +typedef Parcel* (*parcelForJavaObject_t)(JNIEnv* env, jobject obj); + getJNIEnv_t _getJNIEnv; ibinderForJavaObject_t _ibinderForJavaObject; javaObjectForIBinder_t _javaObjectForIBinder; +parcelForJavaObject_t _parcelForJavaObject; void load() { std::call_once(loadFlag, []() { @@ -64,6 +71,13 @@ void load() { ALOGW("Could not find javaObjectForIBinder."); // no return } + + _parcelForJavaObject = reinterpret_cast<parcelForJavaObject_t>( + dlsym(handle, "_ZN7android19parcelForJavaObjectEP7_JNIEnvP8_jobject")); + if (_parcelForJavaObject == nullptr) { + ALOGW("Could not find parcelForJavaObject."); + // no return + } }); } @@ -95,4 +109,12 @@ jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val) { return _javaObjectForIBinder(env, val); } +Parcel* parcelForJavaObject(JNIEnv* env, jobject obj) { + load(); + if (_parcelForJavaObject == nullptr) { + return nullptr; + } + return _parcelForJavaObject(env, obj); +} + } // namespace android diff --git a/libs/android_runtime_lazy/include/android_os_Parcel.h b/libs/android_runtime_lazy/include/android_os_Parcel.h new file mode 100644 index 0000000000..19b094d02a --- /dev/null +++ b/libs/android_runtime_lazy/include/android_os_Parcel.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2020 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. + */ + +#pragma once + +#include <binder/Parcel.h> +#include "jni.h" + +namespace android { + +// The name of this file is same with the file in frameworks/base/core/jni/ +// This is intentional to make the client use these exported functions +// in the same way with the original. + +Parcel* parcelForJavaObject(JNIEnv* env, jobject obj); + +} // namespace android diff --git a/libs/binder/ndk/Android.bp b/libs/binder/ndk/Android.bp index c0ea6d7b1e..e66e425aee 100644 --- a/libs/binder/ndk/Android.bp +++ b/libs/binder/ndk/Android.bp @@ -50,6 +50,7 @@ cc_library_shared { "ibinder.cpp", "ibinder_jni.cpp", "parcel.cpp", + "parcel_jni.cpp", "process.cpp", "stability.cpp", "status.cpp", diff --git a/libs/binder/ndk/include_ndk/android/binder_parcel_jni.h b/libs/binder/ndk/include_ndk/android/binder_parcel_jni.h new file mode 100644 index 0000000000..65e1704439 --- /dev/null +++ b/libs/binder/ndk/include_ndk/android/binder_parcel_jni.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2020 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_parcel_jni.h + * @brief Conversions between AParcel and android.os.Parcel + */ + +#pragma once + +#include <android/binder_parcel.h> + +#include <jni.h> + +__BEGIN_DECLS +#if __ANDROID_API__ >= 30 + +/** + * Converts an android.os.Parcel object into an AParcel* object. + * + * If the parcel is null, null is returned. + * + * Available since API level 30. + * + * \param env Java environment. Must not be null. + * \param parcel android.os.Parcel java object. + * + * \return an AParcel object representing the Java parcel object. If either parameter is null, this + * will return null. This must be deleted with AParcel_delete. This does not take ownership of the + * jobject and is only good for as long as the jobject is alive. + */ +__attribute__((warn_unused_result)) AParcel* AParcel_fromJavaParcel(JNIEnv* env, jobject parcel) + __INTRODUCED_IN(30); + +#endif //__ANDROID_API__ >= 30 +__END_DECLS + +/** @} */ diff --git a/libs/binder/ndk/libbinder_ndk.map.txt b/libs/binder/ndk/libbinder_ndk.map.txt index 71d8103161..f3158d7e18 100644 --- a/libs/binder/ndk/libbinder_ndk.map.txt +++ b/libs/binder/ndk/libbinder_ndk.map.txt @@ -105,6 +105,7 @@ LIBBINDER_NDK30 { # introduced=30 AIBinder_setExtension; AStatus_getDescription; AStatus_deleteDescription; + AParcel_fromJavaParcel; AIBinder_markSystemStability; # apex AIBinder_markVendorStability; # llndk diff --git a/libs/binder/ndk/parcel_jni.cpp b/libs/binder/ndk/parcel_jni.cpp new file mode 100644 index 0000000000..53b2d7c4da --- /dev/null +++ b/libs/binder/ndk/parcel_jni.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2020 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_parcel_jni.h> +#include "parcel_internal.h" + +#include <android_os_Parcel.h> + +using ::android::Parcel; +using ::android::parcelForJavaObject; + +AParcel* AParcel_fromJavaParcel(JNIEnv* env, jobject jbinder) { + if (jbinder == nullptr) { + return nullptr; + } + + Parcel* parcel = parcelForJavaObject(env, jbinder); + + if (parcel == nullptr) { + return nullptr; + } + + return new AParcel(nullptr /*binder*/, parcel, false /*shouldOwn*/); +} |