Merge "Revert "Use BinderDeathDispatcher for alarm listeners""

This commit is contained in:
Treehugger Robot
2021-05-28 00:45:37 +00:00
committed by Gerrit Code Review
4 changed files with 23 additions and 67 deletions

View File

@@ -63,10 +63,6 @@ public class BinderDeathDispatcher<T extends IInterface> {
@Override @Override
public void binderDied() { public void binderDied() {
}
@Override
public void binderDied(IBinder who) {
final ArraySet<DeathRecipient> copy; final ArraySet<DeathRecipient> copy;
synchronized (mLock) { synchronized (mLock) {
copy = mRecipients; copy = mRecipients;
@@ -81,7 +77,7 @@ public class BinderDeathDispatcher<T extends IInterface> {
// Let's call it without holding the lock. // Let's call it without holding the lock.
final int size = copy.size(); final int size = copy.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
copy.valueAt(i).binderDied(who); copy.valueAt(i).binderDied();
} }
} }
} }

View File

@@ -1,15 +1,5 @@
{ {
"presubmit": [ "presubmit": [
{
"file_patterns": [
"BinderDeathDispatcher\\.java"
],
"name": "FrameworksCoreTests",
"options": [
{ "include-filter": "com.android.internal.os.BinderDeathDispatcherTest" },
{ "exclude-annotation": "com.android.internal.os.SkipPresubmit" }
]
},
{ {
"name": "FrameworksCoreTests", "name": "FrameworksCoreTests",
"options": [ "options": [

View File

@@ -17,7 +17,6 @@ package com.android.internal.os;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset; import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@@ -32,14 +31,14 @@ import android.os.RemoteException;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.os.ShellCallback; import android.os.ShellCallback;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
@SmallTest @SmallTest
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class BinderDeathDispatcherTest { public class BinderDeathDispatcherTest {
@@ -121,7 +120,7 @@ public class BinderDeathDispatcherTest {
public void die() { public void die() {
isAlive = false; isAlive = false;
if (mRecipient != null) { if (mRecipient != null) {
mRecipient.binderDied(this); mRecipient.binderDied();
} }
mRecipient = null; mRecipient = null;
} }
@@ -228,33 +227,33 @@ public class BinderDeathDispatcherTest {
// Kill the targets. // Kill the targets.
t1.die(); t1.die();
verify(r1, times(1)).binderDied(t1); verify(r1, times(1)).binderDied();
verify(r2, times(1)).binderDied(t1); verify(r2, times(1)).binderDied();
verify(r3, times(1)).binderDied(t1); verify(r3, times(1)).binderDied();
verify(r4, times(0)).binderDied(any()); verify(r4, times(0)).binderDied();
verify(r5, times(0)).binderDied(any()); verify(r5, times(0)).binderDied();
assertThat(d.getTargetsForTest().size()).isEqualTo(2); assertThat(d.getTargetsForTest().size()).isEqualTo(2);
reset(r1, r2, r3, r4, r5); reset(r1, r2, r3, r4, r5);
t2.die(); t2.die();
verify(r1, times(1)).binderDied(t2); verify(r1, times(1)).binderDied();
verify(r2, times(0)).binderDied(any()); verify(r2, times(0)).binderDied();
verify(r3, times(0)).binderDied(any()); verify(r3, times(0)).binderDied();
verify(r4, times(0)).binderDied(any()); verify(r4, times(0)).binderDied();
verify(r5, times(0)).binderDied(any()); verify(r5, times(0)).binderDied();
assertThat(d.getTargetsForTest().size()).isEqualTo(1); assertThat(d.getTargetsForTest().size()).isEqualTo(1);
reset(r1, r2, r3, r4, r5); reset(r1, r2, r3, r4, r5);
t3.die(); t3.die();
verify(r1, times(0)).binderDied(any()); verify(r1, times(0)).binderDied();
verify(r2, times(0)).binderDied(any()); verify(r2, times(0)).binderDied();
verify(r3, times(1)).binderDied(t3); verify(r3, times(1)).binderDied();
verify(r4, times(0)).binderDied(any()); verify(r4, times(0)).binderDied();
verify(r5, times(1)).binderDied(t3); verify(r5, times(1)).binderDied();
assertThat(d.getTargetsForTest().size()).isEqualTo(0); assertThat(d.getTargetsForTest().size()).isEqualTo(0);
@@ -263,27 +262,4 @@ public class BinderDeathDispatcherTest {
assertThat(d.getTargetsForTest().size()).isEqualTo(0); assertThat(d.getTargetsForTest().size()).isEqualTo(0);
} }
@Test
public void duplicateRegistrations() {
BinderDeathDispatcher<MyTarget> d = new BinderDeathDispatcher<>();
MyTarget t1 = new MyTarget();
DeathRecipient r1 = mock(DeathRecipient.class);
DeathRecipient r2 = mock(DeathRecipient.class);
for (int i = 0; i < 5; i++) {
assertThat(d.linkToDeath(t1, r1)).isEqualTo(1);
}
assertThat(d.linkToDeath(t1, r2)).isEqualTo(2);
t1.die();
verify(r1, times(1)).binderDied(t1);
verify(r2, times(1)).binderDied(t1);
d.unlinkToDeath(t1, r1);
d.unlinkToDeath(t1, r2);
assertThat(d.getTargetsForTest()).isEmpty();
}
} }

View File

@@ -91,7 +91,6 @@ import android.util.proto.ProtoOutputStream;
import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BinderDeathDispatcher;
import com.android.internal.util.ArrayUtils; import com.android.internal.util.ArrayUtils;
import com.android.internal.util.DumpUtils; import com.android.internal.util.DumpUtils;
import com.android.internal.util.FrameworkStatsLog; import com.android.internal.util.FrameworkStatsLog;
@@ -177,8 +176,6 @@ class AlarmManagerService extends SystemService {
.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
| Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
private static final BinderDeathDispatcher<IAlarmListener> sListenerDeathDispatcher =
new BinderDeathDispatcher<>();
final LocalLog mLog = new LocalLog(TAG); final LocalLog mLog = new LocalLog(TAG);
AppOpsManager mAppOps; AppOpsManager mAppOps;
@@ -1704,8 +1701,9 @@ class AlarmManagerService extends SystemService {
} }
if (directReceiver != null) { if (directReceiver != null) {
if (sListenerDeathDispatcher.linkToDeath(directReceiver, mListenerDeathRecipient) try {
<= 0) { directReceiver.asBinder().linkToDeath(mListenerDeathRecipient, 0);
} catch (RemoteException e) {
Slog.w(TAG, "Dropping unreachable alarm listener " + listenerTag); Slog.w(TAG, "Dropping unreachable alarm listener " + listenerTag);
return; return;
} }
@@ -2466,10 +2464,6 @@ class AlarmManagerService extends SystemService {
pw.println("]"); pw.println("]");
pw.println(); pw.println();
pw.println("Listener death dispatcher state:");
sListenerDeathDispatcher.dump(pw, " ");
pw.println();
if (mLog.dump(pw, " Recent problems", " ")) { if (mLog.dump(pw, " Recent problems", " ")) {
pw.println(); pw.println();
} }