Merge "Remove BinderDeathRecipient from virtual sensor binder token" into udc-dev

This commit is contained in:
Ján Sebechlebský
2023-05-10 14:09:15 +00:00
committed by Android (Google) Code Review

View File

@@ -36,7 +36,6 @@ import com.android.server.LocalServices;
import com.android.server.sensors.SensorManagerInternal; import com.android.server.sensors.SensorManagerInternal;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@@ -73,15 +72,10 @@ public class SensorController {
void close() { void close() {
synchronized (mLock) { synchronized (mLock) {
final Iterator<Map.Entry<IBinder, SensorDescriptor>> iterator = mSensorDescriptors.values().forEach(
mSensorDescriptors.entrySet().iterator(); descriptor -> mSensorManagerInternal.removeRuntimeSensor(
if (iterator.hasNext()) { descriptor.getHandle()));
final Map.Entry<IBinder, SensorDescriptor> entry = iterator.next(); mSensorDescriptors.clear();
final IBinder token = entry.getKey();
final SensorDescriptor sensorDescriptor = entry.getValue();
iterator.remove();
closeSensorDescriptorLocked(token, sensorDescriptor);
}
} }
} }
@@ -110,19 +104,9 @@ public class SensorController {
throw new SensorCreationException("Received an invalid virtual sensor handle."); throw new SensorCreationException("Received an invalid virtual sensor handle.");
} }
// The handle is valid from here, so ensure that all failures clean it up.
final BinderDeathRecipient binderDeathRecipient;
try {
binderDeathRecipient = new BinderDeathRecipient(sensorToken);
sensorToken.linkToDeath(binderDeathRecipient, /* flags= */ 0);
} catch (RemoteException e) {
mSensorManagerInternal.removeRuntimeSensor(handle);
throw new SensorCreationException("Client died before sensor could be created.", e);
}
synchronized (mLock) { synchronized (mLock) {
SensorDescriptor sensorDescriptor = new SensorDescriptor( SensorDescriptor sensorDescriptor = new SensorDescriptor(
handle, config.getType(), config.getName(), binderDeathRecipient); handle, config.getType(), config.getName());
mSensorDescriptors.put(sensorToken, sensorDescriptor); mSensorDescriptors.put(sensorToken, sensorDescriptor);
} }
return handle; return handle;
@@ -149,17 +133,10 @@ public class SensorController {
if (sensorDescriptor == null) { if (sensorDescriptor == null) {
throw new IllegalArgumentException("Could not unregister sensor for given token"); throw new IllegalArgumentException("Could not unregister sensor for given token");
} }
closeSensorDescriptorLocked(token, sensorDescriptor); mSensorManagerInternal.removeRuntimeSensor(sensorDescriptor.getHandle());
} }
} }
@GuardedBy("mLock")
private void closeSensorDescriptorLocked(IBinder token, SensorDescriptor sensorDescriptor) {
token.unlinkToDeath(sensorDescriptor.getDeathRecipient(), /* flags= */ 0);
final int handle = sensorDescriptor.getHandle();
mSensorManagerInternal.removeRuntimeSensor(handle);
}
void dump(@NonNull PrintWriter fout) { void dump(@NonNull PrintWriter fout) {
fout.println(" SensorController: "); fout.println(" SensorController: ");
@@ -177,7 +154,7 @@ public class SensorController {
void addSensorForTesting(IBinder deviceToken, int handle, int type, String name) { void addSensorForTesting(IBinder deviceToken, int handle, int type, String name) {
synchronized (mLock) { synchronized (mLock) {
mSensorDescriptors.put(deviceToken, mSensorDescriptors.put(deviceToken,
new SensorDescriptor(handle, type, name, () -> {})); new SensorDescriptor(handle, type, name));
} }
} }
@@ -285,13 +262,11 @@ public class SensorController {
static final class SensorDescriptor { static final class SensorDescriptor {
private final int mHandle; private final int mHandle;
private final IBinder.DeathRecipient mDeathRecipient;
private final int mType; private final int mType;
private final String mName; private final String mName;
SensorDescriptor(int handle, int type, String name, IBinder.DeathRecipient deathRecipient) { SensorDescriptor(int handle, int type, String name) {
mHandle = handle; mHandle = handle;
mDeathRecipient = deathRecipient;
mType = type; mType = type;
mName = name; mName = name;
} }
@@ -304,26 +279,6 @@ public class SensorController {
public String getName() { public String getName() {
return mName; return mName;
} }
public IBinder.DeathRecipient getDeathRecipient() {
return mDeathRecipient;
}
}
private final class BinderDeathRecipient implements IBinder.DeathRecipient {
private final IBinder mSensorToken;
BinderDeathRecipient(IBinder sensorToken) {
mSensorToken = sensorToken;
}
@Override
public void binderDied() {
// All callers are expected to call {@link VirtualDevice#unregisterSensor} before
// quitting, which removes this death recipient. If this is invoked, the remote end
// died, or they disposed of the object without properly unregistering.
Slog.e(TAG, "Virtual sensor controller binder died");
unregisterSensor(mSensorToken);
}
} }
/** An internal exception that is thrown to indicate an error when opening a virtual sensor. */ /** An internal exception that is thrown to indicate an error when opening a virtual sensor. */