diff options
| author | Treehugger Robot <treehugger-gerrit@google.com> | 2020-07-06 20:09:22 +0000 |
|---|---|---|
| committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-07-06 20:09:22 +0000 |
| commit | 24b8620956366b0b3c9b0056eaad7da89fab5fd6 (patch) | |
| tree | f33331d09dd36191e79902ac901f1d7083a7f982 | |
| parent | 1ac66d5a98c667c342b118dea7b98977db420af4 (diff) | |
| parent | 82c23c10cbb8b3843a370bd20d006381bf1dea20 (diff) | |
| download | platform_libnativehelper-24b8620956366b0b3c9b0056eaad7da89fab5fd6.tar.gz platform_libnativehelper-24b8620956366b0b3c9b0056eaad7da89fab5fd6.tar.bz2 platform_libnativehelper-24b8620956366b0b3c9b0056eaad7da89fab5fd6.zip | |
Merge "Add FileDescriptor NDK methods" am: 4d480bccc1 am: 82c23c10cb
Original change: https://android-review.googlesource.com/c/platform/libnativehelper/+/1354105
Change-Id: Iee3ba65dba28602bb5ec98b3707e8712b0e349ec
| -rw-r--r-- | Android.bp | 22 | ||||
| -rw-r--r-- | TEST_MAPPING | 3 | ||||
| -rw-r--r-- | file_descriptor_jni.c | 47 | ||||
| -rw-r--r-- | include/android/file_descriptor_jni.h | 88 | ||||
| -rw-r--r-- | libnativehelper.map.txt | 12 |
5 files changed, 168 insertions, 4 deletions
@@ -110,13 +110,15 @@ cc_library_shared { "include_platform_header_only", ], stl: "none", - // The ART module is anticipated in 31(S). - min_sdk_version: "31", stubs: { symbol_file: "libnativehelper.map.txt", - versions: ["30"], + versions: ["31"], }, target: { + android: { + // FileDescriptor NDK API is Android-only. + srcs: ["file_descriptor_jni.c"], + }, windows: { enabled: true, }, @@ -182,6 +184,20 @@ ndk_headers { license: "NOTICE", } +ndk_headers { + name: "libnativehelper_ndk_headers", + from: "include", + to: "", + srcs: ["include/android/*.h"], + license: "NOTICE", +} + +ndk_library { + name: "libnativehelper", + symbol_file: "libnativehelper.map.txt", + first_version: "31", +} + // // Tests. // diff --git a/TEST_MAPPING b/TEST_MAPPING index 0e3a29f..018b6b5 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -5,6 +5,9 @@ }, { "name": "MtsLibnativehelperTestCases" + }, + { + "name": "CtsLibnativehelperTestCases" } ] } diff --git a/file_descriptor_jni.c b/file_descriptor_jni.c new file mode 100644 index 0000000..7218a18 --- /dev/null +++ b/file_descriptor_jni.c @@ -0,0 +1,47 @@ +/* + * 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/file_descriptor_jni.h> + +#include <stddef.h> + +#define LOG_TAG "file_descriptor_jni" +#include "ALog-priv.h" + +#include "JniConstants.h" + +static void EnsureArgumentIsFileDescriptor(JNIEnv* env, jobject instance) { + ALOG_ALWAYS_FATAL_IF(instance == NULL, "FileDescriptor is NULL"); + jclass jifd = JniConstants_FileDescriptorClass(env); + ALOG_ALWAYS_FATAL_IF(!(*env)->IsInstanceOf(env, instance, jifd), + "Argument is not a FileDescriptor"); +} + +JNIEXPORT _Nullable jobject AFileDescriptor_create(JNIEnv* env) { + return (*env)->NewObject(env, + JniConstants_FileDescriptorClass(env), + JniConstants_FileDescriptor_init(env)); +} + +JNIEXPORT int AFileDescriptor_getFD(JNIEnv* env, jobject fileDescriptor) { + EnsureArgumentIsFileDescriptor(env, fileDescriptor); + return (*env)->GetIntField(env, fileDescriptor, JniConstants_FileDescriptor_descriptor(env)); +} + +JNIEXPORT void AFileDescriptor_setFD(JNIEnv* env, jobject fileDescriptor, int fd) { + EnsureArgumentIsFileDescriptor(env, fileDescriptor); + (*env)->SetIntField(env, fileDescriptor, JniConstants_FileDescriptor_descriptor(env), fd); +} diff --git a/include/android/file_descriptor_jni.h b/include/android/file_descriptor_jni.h new file mode 100644 index 0000000..a375be1 --- /dev/null +++ b/include/android/file_descriptor_jni.h @@ -0,0 +1,88 @@ +/* + * 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 FileDescriptor File Descriptor + * @{ + */ + +/** + * @file file_descriptor_jni.h + */ + +#pragma once + +#include <sys/cdefs.h> + +#include <jni.h> + +__BEGIN_DECLS + +#if __ANDROID_API__ >= __ANDROID_API_S__ + +/** + * Returns a new java.io.FileDescriptor. + * + * The FileDescriptor created represents an invalid Unix file descriptor (represented by + * a file descriptor value of -1). + * + * Callers of this method should be aware that it can fail, returning NULL with a pending Java + * exception. + * + * Available since API level 31. + * + * \param env a pointer to the JNI Native Interface of the current thread. + * \return a java.io.FileDescriptor on success, nullptr if insufficient heap memory is available. + */ +jobject AFileDescriptor_create(JNIEnv* env) __INTRODUCED_IN(31); + +/** + * Returns the Unix file descriptor represented by the given java.io.FileDescriptor. + * + * A return value of -1 indicates that \a fileDescriptor represents an invalid file descriptor. + * + * Aborts the program if \a fileDescriptor is not a java.io.FileDescriptor instance. + * + * Available since API level 31. + * + * \param env a pointer to the JNI Native Interface of the current thread. + * \param fileDescriptor a java.io.FileDescriptor instance. + * \return the Unix file descriptor wrapped by \a fileDescriptor. + */ +int AFileDescriptor_getFD(JNIEnv* env, jobject fileDescriptor) __INTRODUCED_IN(31); + +/** + * Sets the Unix file descriptor represented by the given java.io.FileDescriptor. + * + * This function performs no validation of the Unix file descriptor argument, \a fd. Android uses + * the value -1 to represent an invalid file descriptor, all other values are considered valid. + * The validity of a file descriptor can be checked with FileDescriptor#valid(). + * + * Aborts the program if \a fileDescriptor is not a java.io.FileDescriptor instance. + * + * Available since API level 31. + * + * \param env a pointer to the JNI Native Interface of the current thread. + * \param fileDescriptor a java.io.FileDescriptor instance. + * \param fd a Unix file descriptor that \a fileDescriptor will subsequently represent. + */ +void AFileDescriptor_setFD(JNIEnv* env, jobject fileDescriptor, int fd) __INTRODUCED_IN(31); + +#endif // __ANDROID_API__ >= 31 + +__END_DECLS + +/** @} */ diff --git a/libnativehelper.map.txt b/libnativehelper.map.txt index 822edad..d1f5911 100644 --- a/libnativehelper.map.txt +++ b/libnativehelper.map.txt @@ -1,7 +1,7 @@ # This library should only export C linkage definitions. # # VERSION string that follows is derived from <library_name>_<version>. -LIBNATIVEHELPER_30 { +LIBNATIVEHELPER_31 { global: JNI_GetDefaultJavaVMInitArgs; JNI_CreateJavaVM; @@ -34,3 +34,13 @@ LIBNATIVEHELPER_30 { local: *; }; + +# NDK API for libnativehelper. +LIBNATIVEHELPER_S { # introduced=31 + global: + AFileDescriptor_create; + AFileDescriptor_getFD; + AFileDescriptor_setFD; + local: + *; +}; |
