summaryrefslogtreecommitdiff
path: root/core/java/android/content/ContentProvider.java
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2018-10-19 15:40:03 -0600
committerJeff Sharkey <jsharkey@android.com>2018-10-20 15:14:22 -0600
commitd2b64d70189f3451aa1f7e018274dbc4c3529bf1 (patch)
treec6fdc839fc2ad6c2ce488ee611ae73a2510d555a /core/java/android/content/ContentProvider.java
parent5b3a6a81871925ff9a6fdcee2f90aa8a09b87670 (diff)
Add ContentProvider.clearCallingIdentity().
ContentProvider has a getCallingPackage() method, which verifies the remote package name against the current Binder identity. When a provider wants to clear that IPC identity, they need to clear both the Binder state and the ContentProvider.getCallingPackage() state together, so add methods to facilitate that. Also fix subtle bug so we don't try translating relative paths. Bug: 117627072 Test: atest cts/tests/tests/provider/src/android/provider/cts/MediaStore* Change-Id: Ifa3e1f745334abf625fdcc314b308a047c49ce73
Diffstat (limited to 'core/java/android/content/ContentProvider.java')
-rw-r--r--core/java/android/content/ContentProvider.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/core/java/android/content/ContentProvider.java b/core/java/android/content/ContentProvider.java
index 2a03787f134c..145c92731458 100644
--- a/core/java/android/content/ContentProvider.java
+++ b/core/java/android/content/ContentProvider.java
@@ -822,6 +822,48 @@ public abstract class ContentProvider implements ComponentCallbacks2 {
}
/**
+ * Opaque token representing the identity of an incoming IPC.
+ */
+ public final class CallingIdentity {
+ /** {@hide} */
+ public final long binderToken;
+ /** {@hide} */
+ public final String callingPackage;
+
+ /** {@hide} */
+ public CallingIdentity(long binderToken, String callingPackage) {
+ this.binderToken = binderToken;
+ this.callingPackage = callingPackage;
+ }
+ }
+
+ /**
+ * Reset the identity of the incoming IPC on the current thread.
+ * <p>
+ * Internally this calls {@link Binder#clearCallingIdentity()} and also
+ * clears any value stored in {@link #getCallingPackage()}.
+ *
+ * @return Returns an opaque token that can be used to restore the original
+ * calling identity by passing it to
+ * {@link #restoreCallingIdentity}.
+ */
+ public final @NonNull CallingIdentity clearCallingIdentity() {
+ return new CallingIdentity(Binder.clearCallingIdentity(), setCallingPackage(null));
+ }
+
+ /**
+ * Restore the identity of the incoming IPC on the current thread back to a
+ * previously identity that was returned by {@link #clearCallingIdentity}.
+ * <p>
+ * Internally this calls {@link Binder#restoreCallingIdentity(long)} and
+ * also restores any value stored in {@link #getCallingPackage()}.
+ */
+ public final void restoreCallingIdentity(@NonNull CallingIdentity identity) {
+ Binder.restoreCallingIdentity(identity.binderToken);
+ mCallingPackage.set(identity.callingPackage);
+ }
+
+ /**
* Change the authorities of the ContentProvider.
* This is normally set for you from its manifest information when the provider is first
* created.