Merge "Add getExtension to android.os.IBinder interface" am: 935dfd2ce9

Change-Id: I0379693d0c87e77eaed7dd133edb00c410d2dfd5
This commit is contained in:
Automerger Merge Worker
2020-02-04 22:28:29 +00:00
8 changed files with 177 additions and 1 deletions

View File

@@ -913,6 +913,18 @@ public class Binder implements IBinder {
resultReceiver.send(0, null);
}
/** @hide */
@Override
public final native @Nullable IBinder getExtension();
/**
* Set the binder extension.
* This should be called immediately when the object is created.
*
* @hide
*/
public final native void setExtension(@Nullable IBinder extension);
/**
* Default implementation rewinds the parcels and calls onTransact. On
* the remote side, transact calls into the binder to do the IPC.

View File

@@ -454,6 +454,10 @@ public final class BinderProxy implements IBinder {
return null;
}
/** @hide */
@Override
public native @Nullable IBinder getExtension() throws RemoteException;
/**
* Perform a binder transaction on a proxy.
*

View File

@@ -243,6 +243,18 @@ public interface IBinder {
@NonNull String[] args, @Nullable ShellCallback shellCallback,
@NonNull ResultReceiver resultReceiver) throws RemoteException;
/**
* Get the binder extension of this binder interface.
* This allows one to customize an interface without having to modify the original interface.
*
* @return null if don't have binder extension
* @throws RemoteException
* @hide
*/
public default @Nullable IBinder getExtension() throws RemoteException {
throw new IllegalStateException("Method is not implemented");
}
/**
* Perform a generic operation with the object.
*

View File

@@ -463,6 +463,9 @@ public:
if (mVintf) {
::android::internal::Stability::markVintf(b.get());
}
if (mExtension != nullptr) {
b.get()->setExtension(mExtension);
}
mBinder = b;
ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n",
b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount());
@@ -481,6 +484,24 @@ public:
mVintf = true;
}
sp<IBinder> getExtension() {
AutoMutex _l(mLock);
sp<JavaBBinder> b = mBinder.promote();
if (b != nullptr) {
return b.get()->getExtension();
}
return mExtension;
}
void setExtension(const sp<IBinder>& extension) {
AutoMutex _l(mLock);
mExtension = extension;
sp<JavaBBinder> b = mBinder.promote();
if (b != nullptr) {
b.get()->setExtension(mExtension);
}
}
private:
Mutex mLock;
wp<JavaBBinder> mBinder;
@@ -489,6 +510,8 @@ private:
// is too much binder state here, we can think about making JavaBBinder an
// sp here (avoid recreating it)
bool mVintf = false;
sp<IBinder> mExtension;
};
// ----------------------------------------------------------------------------
@@ -1033,6 +1056,17 @@ static jobject android_os_Binder_waitForService(
return javaObjectForIBinder(env, service);
}
static jobject android_os_Binder_getExtension(JNIEnv* env, jobject obj) {
JavaBBinderHolder* jbh = (JavaBBinderHolder*) env->GetLongField(obj, gBinderOffsets.mObject);
return javaObjectForIBinder(env, jbh->getExtension());
}
static void android_os_Binder_setExtension(JNIEnv* env, jobject obj, jobject extensionObject) {
JavaBBinderHolder* jbh = (JavaBBinderHolder*) env->GetLongField(obj, gBinderOffsets.mObject);
sp<IBinder> extension = ibinderForJavaObject(env, extensionObject);
jbh->setExtension(extension);
}
// ----------------------------------------------------------------------------
static const JNINativeMethod gBinderMethods[] = {
@@ -1062,7 +1096,9 @@ static const JNINativeMethod gBinderMethods[] = {
{ "getNativeBBinderHolder", "()J", (void*)android_os_Binder_getNativeBBinderHolder },
{ "getNativeFinalizer", "()J", (void*)android_os_Binder_getNativeFinalizer },
{ "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable },
{ "waitForService", "(Ljava/lang/String;)Landroid/os/IBinder;", (void*)android_os_Binder_waitForService }
{ "waitForService", "(Ljava/lang/String;)Landroid/os/IBinder;", (void*)android_os_Binder_waitForService },
{ "getExtension", "()Landroid/os/IBinder;", (void*)android_os_Binder_getExtension },
{ "setExtension", "(Landroid/os/IBinder;)V", (void*)android_os_Binder_setExtension },
};
const char* const kBinderPathName = "android/os/Binder";
@@ -1502,6 +1538,21 @@ JNIEXPORT jlong JNICALL android_os_BinderProxy_getNativeFinalizer(JNIEnv*, jclas
return (jlong) BinderProxy_destroy;
}
static jobject android_os_BinderProxy_getExtension(JNIEnv* env, jobject obj) {
IBinder* binder = getBPNativeData(env, obj)->mObject.get();
if (binder == nullptr) {
jniThrowException(env, "java/lang/IllegalStateException", "Native IBinder is null");
return nullptr;
}
sp<IBinder> extension;
status_t err = binder->getExtension(&extension);
if (err != OK) {
signalExceptionForError(env, obj, err, true /* canThrowRemoteException */);
return nullptr;
}
return javaObjectForIBinder(env, extension);
}
// ----------------------------------------------------------------------------
static const JNINativeMethod gBinderProxyMethods[] = {
@@ -1513,6 +1564,7 @@ static const JNINativeMethod gBinderProxyMethods[] = {
{"linkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)V", (void*)android_os_BinderProxy_linkToDeath},
{"unlinkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)Z", (void*)android_os_BinderProxy_unlinkToDeath},
{"getNativeFinalizer", "()J", (void*)android_os_BinderProxy_getNativeFinalizer},
{"getExtension", "()Landroid/os/IBinder;", (void*)android_os_BinderProxy_getExtension},
};
const char* const kBinderProxyPathName = "android/os/BinderProxy";

View File

@@ -1340,6 +1340,10 @@
<service android:name="android.os.BinderWorkSourceNestedService"
android:process=":BinderWorkSourceNestedService" />
<!-- Used by BinderProxyTest -->
<service android:name="android.os.BinderProxyService"
android:process=":BinderProxyService" />
<!-- Application components used for search manager tests -->
<activity android:name="android.app.activity.SearchableActivity"

View File

@@ -0,0 +1,35 @@
/*
* 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.
*/
package android.os;
import android.app.Service;
import android.content.Intent;
public class BinderProxyService extends Service {
private final Binder mBinder = new Binder();
@Override
public void onCreate() {
super.onCreate();
mBinder.setExtension(new Binder());
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}

View File

@@ -17,11 +17,17 @@
package android.os;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.test.AndroidTestCase;
import androidx.test.filters.MediumTest;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class BinderProxyTest extends AndroidTestCase {
private static class CountingListener implements Binder.ProxyTransactListener {
int mStartedCount;
@@ -86,4 +92,41 @@ public class BinderProxyTest extends AndroidTestCase {
// Check it does not throw..
mPowerManager.isInteractive();
}
private IBinder mRemoteBinder = null;
@MediumTest
public void testGetExtension() throws Exception {
final CountDownLatch bindLatch = new CountDownLatch(1);
ServiceConnection connection =
new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mRemoteBinder = service;
bindLatch.countDown();
}
@Override
public void onServiceDisconnected(ComponentName name) {}
};
try {
mContext.bindService(
new Intent(mContext, BinderProxyService.class),
connection,
Context.BIND_AUTO_CREATE);
if (!bindLatch.await(500, TimeUnit.MILLISECONDS)) {
fail(
"Timed out while binding service: "
+ BinderProxyService.class.getSimpleName());
}
assertTrue(mRemoteBinder instanceof BinderProxy);
assertNotNull(mRemoteBinder);
IBinder extension = mRemoteBinder.getExtension();
assertNotNull(extension);
assertTrue(extension.pingBinder());
} finally {
mContext.unbindService(connection);
}
}
}

View File

@@ -52,4 +52,18 @@ public class BinderTest extends TestCase {
} catch (IllegalStateException expected) {
}
}
@SmallTest
public void testGetExtension() throws Exception {
Binder binder = new Binder();
assertNull(binder.getExtension());
IBinder extension = new Binder();
binder.setExtension(extension);
assertNotNull(binder.getExtension());
assertSame(binder.getExtension(), extension);
binder.setExtension(null);
assertNull(binder.getExtension());
}
}