summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Soonil Nagarkar <sooniln@google.com> 2020-07-09 16:57:25 -0700
committer Soonil Nagarkar <sooniln@google.com> 2020-07-09 16:57:25 -0700
commit00af9a3205cbc8ad39975c3d1775d16f148cddff (patch)
treed29939fa69782c2a03a1f9f9fe3d6f2afed3a9a7
parente4e09739641df9eddafb469deda695d3c996393b (diff)
Ask location provider for its package name
The package name from the service watcher may be inconsistent with the package name of the currently bound service. Test: none Change-Id: Iede437d2e61a0da0b853fdbb133a29add04f6bf3
-rw-r--r--location/java/com/android/internal/location/ILocationProviderManager.aidl9
-rw-r--r--location/lib/java/com/android/location/provider/LocationProviderBase.java11
-rw-r--r--packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java2
-rw-r--r--services/core/java/com/android/server/ServiceWatcher.java27
-rw-r--r--services/core/java/com/android/server/location/LocationProviderProxy.java35
5 files changed, 45 insertions, 39 deletions
diff --git a/location/java/com/android/internal/location/ILocationProviderManager.aidl b/location/java/com/android/internal/location/ILocationProviderManager.aidl
index 4036d63905ea..2a6cef812db1 100644
--- a/location/java/com/android/internal/location/ILocationProviderManager.aidl
+++ b/location/java/com/android/internal/location/ILocationProviderManager.aidl
@@ -25,15 +25,8 @@ import com.android.internal.location.ProviderProperties;
* @hide
*/
interface ILocationProviderManager {
-
- void onSetAttributionTag(String attributionTag);
-
- @UnsupportedAppUsage
+ void onSetIdentity(@nullable String packageName, @nullable String attributionTag);
void onSetAllowed(boolean allowed);
-
- @UnsupportedAppUsage
void onSetProperties(in ProviderProperties properties);
-
- @UnsupportedAppUsage
void onReportLocation(in Location location);
}
diff --git a/location/lib/java/com/android/location/provider/LocationProviderBase.java b/location/lib/java/com/android/location/provider/LocationProviderBase.java
index 25b4090187a4..7a3a4b23d532 100644
--- a/location/lib/java/com/android/location/provider/LocationProviderBase.java
+++ b/location/lib/java/com/android/location/provider/LocationProviderBase.java
@@ -79,7 +79,8 @@ public abstract class LocationProviderBase {
public static final String FUSED_PROVIDER = LocationManager.FUSED_PROVIDER;
final String mTag;
- final String mAttributionTag;
+ @Nullable final String mPackageName;
+ @Nullable final String mAttributionTag;
final IBinder mBinder;
/**
@@ -93,8 +94,7 @@ public abstract class LocationProviderBase {
protected final ILocationManager mLocationManager;
// write locked on mBinder, read lock is optional depending on atomicity requirements
- @Nullable
- volatile ILocationProviderManager mManager;
+ @Nullable volatile ILocationProviderManager mManager;
volatile ProviderProperties mProperties;
volatile boolean mAllowed;
@@ -116,6 +116,7 @@ public abstract class LocationProviderBase {
public LocationProviderBase(Context context, String tag,
ProviderPropertiesUnbundled properties) {
mTag = tag;
+ mPackageName = context != null ? context.getPackageName() : null;
mAttributionTag = context != null ? context.getAttributionTag() : null;
mBinder = new Service();
@@ -332,8 +333,8 @@ public abstract class LocationProviderBase {
public void setLocationProviderManager(ILocationProviderManager manager) {
synchronized (mBinder) {
try {
- if (mAttributionTag != null) {
- manager.onSetAttributionTag(mAttributionTag);
+ if (mPackageName != null || mAttributionTag != null) {
+ manager.onSetIdentity(mPackageName, mAttributionTag);
}
manager.onSetProperties(mProperties);
manager.onSetAllowed(mAllowed);
diff --git a/packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java b/packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java
index ed7f3df5222d..8efbad64f3d2 100644
--- a/packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java
+++ b/packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java
@@ -199,7 +199,7 @@ public class FusedLocationServiceTest {
}
@Override
- public void onSetAttributionTag(String attributionTag) {
+ public void onSetIdentity(String packageName, String attributionTag) {
}
diff --git a/services/core/java/com/android/server/ServiceWatcher.java b/services/core/java/com/android/server/ServiceWatcher.java
index a91f2f60c02d..e5d0021a2bdf 100644
--- a/services/core/java/com/android/server/ServiceWatcher.java
+++ b/services/core/java/com/android/server/ServiceWatcher.java
@@ -56,8 +56,7 @@ import java.util.Objects;
/**
* Maintains a binding to the best service that matches the given intent information. Bind and
- * unbind callbacks, as well as all binder operations, will all be run on a single thread, but the
- * exact thread is left undefined.
+ * unbind callbacks, as well as all binder operations, will all be run on a single thread.
*/
public class ServiceWatcher implements ServiceConnection {
@@ -73,7 +72,11 @@ public class ServiceWatcher implements ServiceConnection {
public interface BinderRunner {
/** Called to run client code with the binder. */
void run(IBinder binder) throws RemoteException;
- /** Called if an error occurred and the function could not be run. */
+ /**
+ * Called if an error occurred and the function could not be run. This callback is only
+ * intended for resource deallocation and cleanup in response to a single binder operation,
+ * it should not be used to propagate errors further.
+ */
default void onError() {}
}
@@ -189,8 +192,15 @@ public class ServiceWatcher implements ServiceConnection {
public ServiceWatcher(Context context, String action,
@Nullable BinderRunner onBind, @Nullable Runnable onUnbind,
@BoolRes int enableOverlayResId, @StringRes int nonOverlayPackageResId) {
+ this(context, FgThread.getHandler(), action, onBind, onUnbind, enableOverlayResId,
+ nonOverlayPackageResId);
+ }
+
+ public ServiceWatcher(Context context, Handler handler, String action,
+ @Nullable BinderRunner onBind, @Nullable Runnable onUnbind,
+ @BoolRes int enableOverlayResId, @StringRes int nonOverlayPackageResId) {
mContext = context;
- mHandler = FgThread.getHandler();
+ mHandler = handler;
mIntent = new Intent(Objects.requireNonNull(action));
Resources resources = context.getResources();
@@ -278,13 +288,6 @@ public class ServiceWatcher implements ServiceConnection {
return true;
}
- /**
- * Returns information on the currently selected service.
- */
- public ServiceInfo getBoundService() {
- return mServiceInfo;
- }
-
private void onBestServiceChanged(boolean forceRebind) {
Preconditions.checkState(Looper.myLooper() == mHandler.getLooper());
@@ -380,7 +383,7 @@ public class ServiceWatcher implements ServiceConnection {
}
@Override
- public void onBindingDied(ComponentName component) {
+ public final void onBindingDied(ComponentName component) {
Preconditions.checkState(Looper.myLooper() == mHandler.getLooper());
if (D) {
diff --git a/services/core/java/com/android/server/location/LocationProviderProxy.java b/services/core/java/com/android/server/location/LocationProviderProxy.java
index 8843e522f9a1..2bcee2faa813 100644
--- a/services/core/java/com/android/server/location/LocationProviderProxy.java
+++ b/services/core/java/com/android/server/location/LocationProviderProxy.java
@@ -22,6 +22,7 @@ import android.annotation.Nullable;
import android.content.Context;
import android.location.Location;
import android.location.util.identity.CallerIdentity;
+import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
@@ -128,27 +129,38 @@ public class LocationProviderProxy extends AbstractLocationProvider {
mServiceWatcher.dump(fd, pw, args);
}
+ private static String guessPackageName(Context context, int uid) {
+ String[] packageNames = context.getPackageManager().getPackagesForUid(uid);
+ if (packageNames == null || packageNames.length == 0) {
+ // illegal state exception will propagate back through binders
+ throw new IllegalStateException(
+ "location provider from uid " + uid + " has no package information");
+ } else {
+ return packageNames[0];
+ }
+ }
+
private class Proxy extends ILocationProviderManager.Stub {
Proxy() {}
// executed on binder thread
@Override
- public void onSetAttributionTag(String attributionTag) {
+ public void onSetIdentity(@Nullable String packageName, @Nullable String attributionTag) {
synchronized (mLock) {
if (mProxy != this) {
return;
}
- String packageName = mServiceWatcher.getBoundService().getPackageName();
+ CallerIdentity identity;
if (packageName == null) {
- return;
+ packageName = guessPackageName(mContext, Binder.getCallingUid());
+ // unsafe is ok since the package is coming direct from the package manager here
+ identity = CallerIdentity.fromBinderUnsafe(packageName, attributionTag);
+ } else {
+ identity = CallerIdentity.fromBinder(mContext, packageName, attributionTag);
}
- // we don't need to verify the package name because we're getting it straight from
- // the service watcher
- CallerIdentity identity = CallerIdentity.fromBinderUnsafe(packageName,
- attributionTag);
setIdentity(identity);
}
}
@@ -163,12 +175,9 @@ public class LocationProviderProxy extends AbstractLocationProvider {
// if no identity is set yet, set it now
if (getIdentity() == null) {
- String packageName = mServiceWatcher.getBoundService().getPackageName();
- if (packageName != null) {
- // we don't need to verify the package name because we're getting it
- // straight from the service watcher
- setIdentity(CallerIdentity.fromBinderUnsafe(packageName, null));
- }
+ String packageName = guessPackageName(mContext, Binder.getCallingUid());
+ // unsafe is ok since the package is coming direct from the package manager here
+ setIdentity(CallerIdentity.fromBinderUnsafe(packageName, null));
}
setProperties(properties);