Merge "Add an extra level of indirection to make sure that native IHwBinder"

This commit is contained in:
Treehugger Robot
2017-06-19 21:55:04 +00:00
committed by Gerrit Code Review
6 changed files with 46 additions and 33 deletions

View File

@@ -58,6 +58,29 @@ static struct fields_t {
jmethodID onTransactID; jmethodID onTransactID;
} gFields; } gFields;
struct JHwBinderHolder : public RefBase {
JHwBinderHolder() {}
sp<JHwBinder> get(JNIEnv *env, jobject obj) {
Mutex::Autolock autoLock(mLock);
sp<JHwBinder> binder = mBinder.promote();
if (binder == NULL) {
binder = new JHwBinder(env, obj);
mBinder = binder;
}
return binder;
}
private:
Mutex mLock;
wp<JHwBinder> mBinder;
DISALLOW_COPY_AND_ASSIGN(JHwBinderHolder);
};
// static // static
void JHwBinder::InitClass(JNIEnv *env) { void JHwBinder::InitClass(JNIEnv *env) {
ScopedLocalRef<jclass> clazz( ScopedLocalRef<jclass> clazz(
@@ -75,10 +98,10 @@ void JHwBinder::InitClass(JNIEnv *env) {
} }
// static // static
sp<JHwBinder> JHwBinder::SetNativeContext( sp<JHwBinderHolder> JHwBinder::SetNativeContext(
JNIEnv *env, jobject thiz, const sp<JHwBinder> &context) { JNIEnv *env, jobject thiz, const sp<JHwBinderHolder> &context) {
sp<JHwBinder> old = sp<JHwBinderHolder> old =
(JHwBinder *)env->GetLongField(thiz, gFields.contextID); (JHwBinderHolder *)env->GetLongField(thiz, gFields.contextID);
if (context != NULL) { if (context != NULL) {
context->incStrong(NULL /* id */); context->incStrong(NULL /* id */);
@@ -94,27 +117,27 @@ sp<JHwBinder> JHwBinder::SetNativeContext(
} }
// static // static
sp<JHwBinder> JHwBinder::GetNativeContext( sp<JHwBinder> JHwBinder::GetNativeBinder(
JNIEnv *env, jobject thiz) { JNIEnv *env, jobject thiz) {
return (JHwBinder *)env->GetLongField(thiz, gFields.contextID); JHwBinderHolder *holder =
reinterpret_cast<JHwBinderHolder *>(
env->GetLongField(thiz, gFields.contextID));
return holder->get(env, thiz);
} }
JHwBinder::JHwBinder(JNIEnv *env, jobject thiz) { JHwBinder::JHwBinder(JNIEnv *env, jobject thiz) {
jclass clazz = env->GetObjectClass(thiz); jclass clazz = env->GetObjectClass(thiz);
CHECK(clazz != NULL); CHECK(clazz != NULL);
mClass = (jclass)env->NewGlobalRef(clazz); mObject = env->NewGlobalRef(thiz);
mObject = env->NewWeakGlobalRef(thiz);
} }
JHwBinder::~JHwBinder() { JHwBinder::~JHwBinder() {
JNIEnv *env = AndroidRuntime::getJNIEnv(); JNIEnv *env = AndroidRuntime::getJNIEnv();
env->DeleteWeakGlobalRef(mObject); env->DeleteGlobalRef(mObject);
mObject = NULL; mObject = NULL;
env->DeleteGlobalRef(mClass);
mClass = NULL;
} }
status_t JHwBinder::onTransact( status_t JHwBinder::onTransact(
@@ -201,10 +224,10 @@ status_t JHwBinder::onTransact(
using namespace android; using namespace android;
static void releaseNativeContext(void *nativeContext) { static void releaseNativeContext(void *nativeContext) {
sp<JHwBinder> binder = (JHwBinder *)nativeContext; sp<JHwBinderHolder> context = static_cast<JHwBinderHolder *>(nativeContext);
if (binder != NULL) { if (context != NULL) {
binder->decStrong(NULL /* id */); context->decStrong(NULL /* id */);
} }
} }
@@ -215,8 +238,7 @@ static jlong JHwBinder_native_init(JNIEnv *env) {
} }
static void JHwBinder_native_setup(JNIEnv *env, jobject thiz) { static void JHwBinder_native_setup(JNIEnv *env, jobject thiz) {
sp<JHwBinder> context = new JHwBinder(env, thiz); sp<JHwBinderHolder> context = new JHwBinderHolder;
JHwBinder::SetNativeContext(env, thiz, context); JHwBinder::SetNativeContext(env, thiz, context);
} }
@@ -244,7 +266,7 @@ static void JHwBinder_native_registerService(
return; // XXX exception already pending? return; // XXX exception already pending?
} }
sp<hardware::IBinder> binder = JHwBinder::GetNativeContext(env, thiz); sp<hardware::IBinder> binder = JHwBinder::GetNativeBinder(env, thiz);
/* TODO(b/33440494) this is not right */ /* TODO(b/33440494) this is not right */
sp<hidl::base::V1_0::IBase> base = new hidl::base::V1_0::BpHwBase(binder); sp<hidl::base::V1_0::IBase> base = new hidl::base::V1_0::BpHwBase(binder);

View File

@@ -24,13 +24,15 @@
namespace android { namespace android {
struct JHwBinderHolder;
struct JHwBinder : public hardware::BHwBinder { struct JHwBinder : public hardware::BHwBinder {
static void InitClass(JNIEnv *env); static void InitClass(JNIEnv *env);
static sp<JHwBinder> SetNativeContext( static sp<JHwBinderHolder> SetNativeContext(
JNIEnv *env, jobject thiz, const sp<JHwBinder> &context); JNIEnv *env, jobject thiz, const sp<JHwBinderHolder> &context);
static sp<JHwBinder> GetNativeContext(JNIEnv *env, jobject thiz); static sp<JHwBinder> GetNativeBinder(JNIEnv *env, jobject thiz);
JHwBinder(JNIEnv *env, jobject thiz); JHwBinder(JNIEnv *env, jobject thiz);
@@ -45,7 +47,6 @@ protected:
TransactCallback callback); TransactCallback callback);
private: private:
jclass mClass;
jobject mObject; jobject mObject;
DISALLOW_COPY_AND_ASSIGN(JHwBinder); DISALLOW_COPY_AND_ASSIGN(JHwBinder);

View File

@@ -169,7 +169,6 @@ JHwParcel::JHwParcel(JNIEnv *env, jobject thiz)
jclass clazz = env->GetObjectClass(thiz); jclass clazz = env->GetObjectClass(thiz);
CHECK(clazz != NULL); CHECK(clazz != NULL);
mClass = (jclass)env->NewGlobalRef(clazz);
mObject = env->NewWeakGlobalRef(thiz); mObject = env->NewWeakGlobalRef(thiz);
} }
@@ -182,9 +181,6 @@ JHwParcel::~JHwParcel() {
env->DeleteWeakGlobalRef(mObject); env->DeleteWeakGlobalRef(mObject);
mObject = NULL; mObject = NULL;
env->DeleteGlobalRef(mClass);
mClass = NULL;
} }
hardware::Parcel *JHwParcel::getParcel() { hardware::Parcel *JHwParcel::getParcel() {
@@ -542,7 +538,7 @@ static void JHwParcel_native_writeStrongBinder(
env, FindClassOrDie(env, PACKAGE_PATH "/HwRemoteBinder")); env, FindClassOrDie(env, PACKAGE_PATH "/HwRemoteBinder"));
if (env->IsInstanceOf(binderObj, hwBinderKlass.get())) { if (env->IsInstanceOf(binderObj, hwBinderKlass.get())) {
binder = JHwBinder::GetNativeContext(env, binderObj); binder = JHwBinder::GetNativeBinder(env, binderObj);
} else if (env->IsInstanceOf(binderObj, hwRemoteBinderKlass.get())) { } else if (env->IsInstanceOf(binderObj, hwRemoteBinderKlass.get())) {
binder = JHwRemoteBinder::GetNativeContext( binder = JHwRemoteBinder::GetNativeContext(
env, binderObj)->getBinder(); env, binderObj)->getBinder();

View File

@@ -53,7 +53,6 @@ protected:
virtual ~JHwParcel(); virtual ~JHwParcel();
private: private:
jclass mClass;
jobject mObject; jobject mObject;
hardware::Parcel *mParcel; hardware::Parcel *mParcel;

View File

@@ -272,7 +272,6 @@ JHwRemoteBinder::JHwRemoteBinder(
jclass clazz = env->GetObjectClass(thiz); jclass clazz = env->GetObjectClass(thiz);
CHECK(clazz != NULL); CHECK(clazz != NULL);
mClass = (jclass)env->NewGlobalRef(clazz);
mObject = env->NewWeakGlobalRef(thiz); mObject = env->NewWeakGlobalRef(thiz);
} }
@@ -281,9 +280,6 @@ JHwRemoteBinder::~JHwRemoteBinder() {
env->DeleteWeakGlobalRef(mObject); env->DeleteWeakGlobalRef(mObject);
mObject = NULL; mObject = NULL;
env->DeleteGlobalRef(mClass);
mClass = NULL;
} }
sp<hardware::IBinder> JHwRemoteBinder::getBinder() const { sp<hardware::IBinder> JHwRemoteBinder::getBinder() const {

View File

@@ -68,7 +68,6 @@ protected:
virtual ~JHwRemoteBinder(); virtual ~JHwRemoteBinder();
private: private:
jclass mClass;
jobject mObject; jobject mObject;
sp<hardware::IBinder> mBinder; sp<hardware::IBinder> mBinder;