Prefer ScopedUtfChars where possible

Minor refactor of input-related files to use safer ScopedUtfChars
constructs.

Fixes: 119214506
Test: make sure the device boots with an -eng build
Change-Id: I7980100be616c921d522484277444546ebdab56b
This commit is contained in:
Siarhei Vishniakou
2018-10-15 18:53:06 -07:00
parent e2841c523b
commit e890f59ecd
4 changed files with 25 additions and 28 deletions

View File

@@ -17,6 +17,7 @@
#define LOG_TAG "InputApplicationHandle"
#include <nativehelper/JNIHelp.h>
#include "core_jni_helpers.h"
#include "jni.h"
#include <android_runtime/AndroidRuntime.h>
#include <utils/threads.h>
@@ -63,16 +64,7 @@ bool NativeInputApplicationHandle::updateInfo() {
mInfo = new InputApplicationInfo();
}
jstring nameObj = jstring(env->GetObjectField(obj,
gInputApplicationHandleClassInfo.name));
if (nameObj) {
const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
mInfo->name = nameStr;
env->ReleaseStringUTFChars(nameObj, nameStr);
env->DeleteLocalRef(nameObj);
} else {
mInfo->name = "<null>";
}
mInfo->name = getStringField(env, obj, gInputApplicationHandleClassInfo.name, "<null>");
mInfo->dispatchingTimeout = env->GetLongField(obj,
gInputApplicationHandleClassInfo.dispatchingTimeoutNanos);

View File

@@ -17,6 +17,7 @@
#define LOG_TAG "InputWindowHandle"
#include <nativehelper/JNIHelp.h>
#include "core_jni_helpers.h"
#include "jni.h"
#include <android_runtime/AndroidRuntime.h>
#include <utils/threads.h>
@@ -86,24 +87,14 @@ bool NativeInputWindowHandle::updateInfo() {
mInfo.touchableRegion.clear();
jobject tokenObj = env->GetObjectField(obj,
gInputWindowHandleClassInfo.token);
jobject tokenObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.token);
if (tokenObj) {
mInfo.token = ibinderForJavaObject(env, tokenObj);
} else {
mInfo.token.clear();
}
jstring nameObj = jstring(env->GetObjectField(obj,
gInputWindowHandleClassInfo.name));
if (nameObj) {
const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
mInfo.name = nameStr;
env->ReleaseStringUTFChars(nameObj, nameStr);
env->DeleteLocalRef(nameObj);
} else {
mInfo.name = "<null>";
}
mInfo.name = getStringField(env, obj, gInputWindowHandleClassInfo.name, "<null>");
mInfo.layoutParamsFlags = env->GetIntField(obj,
gInputWindowHandleClassInfo.layoutParamsFlags);
@@ -241,8 +232,7 @@ int register_android_view_InputWindowHandle(JNIEnv* env) {
GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, clazz,
"ptr", "J");
GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle,
clazz,
GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle, clazz,
"inputApplicationHandle", "Landroid/view/InputApplicationHandle;");
GET_FIELD_ID(gInputWindowHandleClassInfo.token, clazz,

View File

@@ -17,7 +17,7 @@
#define LOG_TAG "InputChannel-JNI"
#include <nativehelper/JNIHelp.h>
#include "nativehelper/scoped_utf_chars.h"
#include <android_runtime/AndroidRuntime.h>
#include <binder/Parcel.h>
#include <utils/Log.h>
@@ -123,9 +123,8 @@ static jobject android_view_InputChannel_createInputChannel(JNIEnv* env,
static jobjectArray android_view_InputChannel_nativeOpenInputChannelPair(JNIEnv* env,
jclass clazz, jstring nameObj) {
const char* nameChars = env->GetStringUTFChars(nameObj, NULL);
std::string name = nameChars;
env->ReleaseStringUTFChars(nameObj, nameChars);
ScopedUtfChars nameChars(env, nameObj);
std::string name = nameChars.c_str();
sp<InputChannel> serverChannel;
sp<InputChannel> clientChannel;

View File

@@ -18,6 +18,8 @@
#define CORE_JNI_HELPERS
#include <nativehelper/JNIHelp.h>
#include <nativehelper/scoped_local_ref.h>
#include <nativehelper/scoped_utf_chars.h>
#include <android_runtime/AndroidRuntime.h>
namespace android {
@@ -72,6 +74,20 @@ static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
return res;
}
/**
* Read the specified field from jobject, and convert to std::string.
* If the field cannot be obtained, return defaultValue.
*/
static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
const char* defaultValue) {
ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
if (strObj != nullptr) {
ScopedUtfChars chars(env, strObj.get());
return std::string(chars.c_str());
}
return std::string(defaultValue);
}
} // namespace android
#endif // CORE_JNI_HELPERS