Clear and restore the calling ID.

am: 29ed4a99bf

Change-Id: Ie5752ca772e6f5c8fed8092d23e0604f44598b24
This commit is contained in:
Jeremy Joslin
2016-12-22 19:01:43 +00:00
committed by android-build-merger
2 changed files with 117 additions and 82 deletions

View File

@@ -40,7 +40,7 @@ import android.net.RecommendationRequest;
import android.net.RecommendationResult; import android.net.RecommendationResult;
import android.net.ScoredNetwork; import android.net.ScoredNetwork;
import android.net.Uri; import android.net.Uri;
import android.net.wifi.WifiConfiguration; import android.os.Binder;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.IRemoteCallback; import android.os.IRemoteCallback;
@@ -319,6 +319,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
" is not the active scorer."); " is not the active scorer.");
} }
final long token = Binder.clearCallingIdentity();
try {
// Separate networks by type. // Separate networks by type.
Map<Integer, List<ScoredNetwork>> networksByType = new ArrayMap<>(); Map<Integer, List<ScoredNetwork>> networksByType = new ArrayMap<>();
for (ScoredNetwork network : networks) { for (ScoredNetwork network : networks) {
@@ -336,11 +338,13 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
final boolean isEmpty; final boolean isEmpty;
synchronized (mScoreCaches) { synchronized (mScoreCaches) {
callbackList = mScoreCaches.get(entry.getKey()); callbackList = mScoreCaches.get(entry.getKey());
isEmpty = callbackList == null || callbackList.getRegisteredCallbackCount() == 0; isEmpty = callbackList == null
|| callbackList.getRegisteredCallbackCount() == 0;
} }
if (isEmpty) { if (isEmpty) {
if (Log.isLoggable(TAG, Log.VERBOSE)) { if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "No scorer registered for type " + entry.getKey() + ", discarding"); Log.v(TAG, "No scorer registered for type " + entry.getKey()
+ ", discarding");
} }
continue; continue;
} }
@@ -360,6 +364,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
} }
return true; return true;
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override
@@ -369,8 +376,13 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
if (mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid()) || if (mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid()) ||
mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) == mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) ==
PackageManager.PERMISSION_GRANTED) { PackageManager.PERMISSION_GRANTED) {
final long token = Binder.clearCallingIdentity();
try {
clearInternal(); clearInternal();
return true; return true;
} finally {
Binder.restoreCallingIdentity(token);
}
} else { } else {
throw new SecurityException( throw new SecurityException(
"Caller is neither the active scorer nor the scorer manager."); "Caller is neither the active scorer nor the scorer manager.");
@@ -428,6 +440,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
INetworkScoreCache scoreCache, INetworkScoreCache scoreCache,
int filterType) { int filterType) {
mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG);
final long token = Binder.clearCallingIdentity();
try {
synchronized (mScoreCaches) { synchronized (mScoreCaches) {
RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType);
if (callbackList == null) { if (callbackList == null) {
@@ -443,27 +457,38 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
} }
} }
} }
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override
public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) {
mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG);
final long token = Binder.clearCallingIdentity();
try {
synchronized (mScoreCaches) { synchronized (mScoreCaches) {
RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType);
if (callbackList == null || !callbackList.unregister(scoreCache)) { if (callbackList == null || !callbackList.unregister(scoreCache)) {
if (Log.isLoggable(TAG, Log.VERBOSE)) { if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Unable to unregister NetworkScoreCache for type " + networkType); Log.v(TAG, "Unable to unregister NetworkScoreCache for type "
+ networkType);
} }
} else if (callbackList.getRegisteredCallbackCount() == 0) { } else if (callbackList.getRegisteredCallbackCount() == 0) {
mScoreCaches.remove(networkType); mScoreCaches.remove(networkType);
} }
} }
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override
public RecommendationResult requestRecommendation(RecommendationRequest request) { public RecommendationResult requestRecommendation(RecommendationRequest request) {
mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG);
throwIfCalledOnMainThread(); throwIfCalledOnMainThread();
final long token = Binder.clearCallingIdentity();
try {
final INetworkRecommendationProvider provider = getRecommendationProvider(); final INetworkRecommendationProvider provider = getRecommendationProvider();
if (provider != null) { if (provider != null) {
try { try {
@@ -483,17 +508,22 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
request.getCurrentSelectedConfig()); request.getCurrentSelectedConfig());
} }
return RecommendationResult.createDoNotConnectRecommendation(); return RecommendationResult.createDoNotConnectRecommendation();
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override
public boolean requestScores(NetworkKey[] networks) { public boolean requestScores(NetworkKey[] networks) {
mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG);
final long token = Binder.clearCallingIdentity();
try {
final INetworkRecommendationProvider provider = getRecommendationProvider(); final INetworkRecommendationProvider provider = getRecommendationProvider();
if (provider != null) { if (provider != null) {
try { try {
provider.requestScores(networks); provider.requestScores(networks);
// TODO(jjoslin): 12/15/16 - Consider pushing null scores into the cache to prevent // TODO(jjoslin): 12/15/16 - Consider pushing null scores into the cache to
// repeated requests for the same scores. // prevent repeated requests for the same scores.
return true; return true;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, "Failed to request scores.", e); Log.w(TAG, "Failed to request scores.", e);
@@ -501,6 +531,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
} }
} }
return false; return false;
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override

View File

@@ -122,6 +122,8 @@ public class NetworkScoreServiceTest {
when(mContext.getResources()).thenReturn(mResources); when(mContext.getResources()).thenReturn(mResources);
mNetworkScoreService = new NetworkScoreService(mContext, mNetworkScorerAppManager); mNetworkScoreService = new NetworkScoreService(mContext, mNetworkScorerAppManager);
WifiConfiguration configuration = new WifiConfiguration(); WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = "NetworkScoreServiceTest_SSID";
configuration.BSSID = "NetworkScoreServiceTest_BSSID";
mRecommendationRequest = new RecommendationRequest.Builder() mRecommendationRequest = new RecommendationRequest.Builder()
.setCurrentRecommendedWifiConfig(configuration).build(); .setCurrentRecommendedWifiConfig(configuration).build();
} }