Grab lock when accessing mutable state

am: 442ed57e4b

Change-Id: Ic8610ed5f0584150d7cda88761bff69dca8d1b42
This commit is contained in:
Svet Ganov
2016-08-18 19:12:50 +00:00
committed by android-build-merger

View File

@@ -875,7 +875,7 @@ public class AppOpsService extends IAppOpsService.Stub {
return AppOpsManager.MODE_IGNORED; return AppOpsManager.MODE_IGNORED;
} }
synchronized (this) { synchronized (this) {
if (isOpRestricted(uid, code, resolvedPackageName)) { if (isOpRestrictedLocked(uid, code, resolvedPackageName)) {
return AppOpsManager.MODE_IGNORED; return AppOpsManager.MODE_IGNORED;
} }
code = AppOpsManager.opToSwitch(code); code = AppOpsManager.opToSwitch(code);
@@ -1024,7 +1024,7 @@ public class AppOpsService extends IAppOpsService.Stub {
return AppOpsManager.MODE_ERRORED; return AppOpsManager.MODE_ERRORED;
} }
Op op = getOpLocked(ops, code, true); Op op = getOpLocked(ops, code, true);
if (isOpRestricted(uid, code, packageName)) { if (isOpRestrictedLocked(uid, code, packageName)) {
return AppOpsManager.MODE_IGNORED; return AppOpsManager.MODE_IGNORED;
} }
if (op.duration == -1) { if (op.duration == -1) {
@@ -1082,7 +1082,7 @@ public class AppOpsService extends IAppOpsService.Stub {
return AppOpsManager.MODE_ERRORED; return AppOpsManager.MODE_ERRORED;
} }
Op op = getOpLocked(ops, code, true); Op op = getOpLocked(ops, code, true);
if (isOpRestricted(uid, code, resolvedPackageName)) { if (isOpRestrictedLocked(uid, code, resolvedPackageName)) {
return AppOpsManager.MODE_IGNORED; return AppOpsManager.MODE_IGNORED;
} }
final int switchCode = AppOpsManager.opToSwitch(code); final int switchCode = AppOpsManager.opToSwitch(code);
@@ -1308,7 +1308,7 @@ public class AppOpsService extends IAppOpsService.Stub {
return op; return op;
} }
private boolean isOpRestricted(int uid, int code, String packageName) { private boolean isOpRestrictedLocked(int uid, int code, String packageName) {
int userHandle = UserHandle.getUserId(uid); int userHandle = UserHandle.getUserId(uid);
final int restrictionSetCount = mOpUserRestrictions.size(); final int restrictionSetCount = mOpUserRestrictions.size();
@@ -2210,25 +2210,33 @@ public class AppOpsService extends IAppOpsService.Stub {
private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token, private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token,
int userHandle, String[] exceptionPackages) { int userHandle, String[] exceptionPackages) {
ClientRestrictionState restrictionState = mOpUserRestrictions.get(token); boolean notifyChange = false;
if (restrictionState == null) { synchronized (AppOpsService.this) {
try { ClientRestrictionState restrictionState = mOpUserRestrictions.get(token);
restrictionState = new ClientRestrictionState(token);
} catch (RemoteException e) { if (restrictionState == null) {
return; try {
restrictionState = new ClientRestrictionState(token);
} catch (RemoteException e) {
return;
}
mOpUserRestrictions.put(token, restrictionState);
}
if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) {
notifyChange = true;
}
if (restrictionState.isDefault()) {
mOpUserRestrictions.remove(token);
restrictionState.destroy();
} }
mOpUserRestrictions.put(token, restrictionState);
} }
if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) { if (notifyChange) {
notifyWatchersOfChange(code); notifyWatchersOfChange(code);
} }
if (restrictionState.isDefault()) {
mOpUserRestrictions.remove(token);
restrictionState.destroy();
}
} }
private void notifyWatchersOfChange(int code) { private void notifyWatchersOfChange(int code) {
@@ -2263,10 +2271,12 @@ public class AppOpsService extends IAppOpsService.Stub {
@Override @Override
public void removeUser(int userHandle) throws RemoteException { public void removeUser(int userHandle) throws RemoteException {
checkSystemUid("removeUser"); checkSystemUid("removeUser");
final int tokenCount = mOpUserRestrictions.size(); synchronized (AppOpsService.this) {
for (int i = tokenCount - 1; i >= 0; i--) { final int tokenCount = mOpUserRestrictions.size();
ClientRestrictionState opRestrictions = mOpUserRestrictions.valueAt(i); for (int i = tokenCount - 1; i >= 0; i--) {
opRestrictions.removeUser(userHandle); ClientRestrictionState opRestrictions = mOpUserRestrictions.valueAt(i);
opRestrictions.removeUser(userHandle);
}
} }
} }