FileDocCategorySizeDatePackage
UriPermission.javaAPI DocAndroid 5.1 API12986Thu Mar 12 22:22:42 GMT 2015com.android.server.am

UriPermission

public final class UriPermission extends Object
Description of a permission granted to an app to access a particular URI. CTS tests for this functionality can be run with "runtest cts-appsecurity". Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java

Fields Summary
private static final String
TAG
public static final int
STRENGTH_NONE
public static final int
STRENGTH_OWNED
public static final int
STRENGTH_GLOBAL
public static final int
STRENGTH_PERSISTABLE
final int
targetUserId
final String
sourcePkg
final String
targetPkg
final int
targetUid
Cached UID of {@link #targetPkg}; should not be persisted
final com.android.server.am.ActivityManagerService.GrantUri
uri
int
modeFlags
Allowed modes. All permission enforcement should use this field. Must always be a combination of {@link #ownedModeFlags}, {@link #globalModeFlags}, {@link #persistableModeFlags}, and {@link #persistedModeFlags}. Mutations must only be performed by the owning class.
int
ownedModeFlags
Allowed modes with active owner.
int
globalModeFlags
Allowed modes without explicit owner.
int
persistableModeFlags
Allowed modes that have been offered for possible persisting.
int
persistedModeFlags
Allowed modes that should be persisted across device boots.
long
persistedCreateTime
Timestamp when {@link #persistedModeFlags} was first defined in {@link System#currentTimeMillis()} time base.
private static final long
INVALID_TIME
private android.util.ArraySet
mReadOwners
private android.util.ArraySet
mWriteOwners
private String
stringName
Constructors Summary
UriPermission(String sourcePkg, String targetPkg, int targetUid, com.android.server.am.ActivityManagerService.GrantUri uri)


            
        this.targetUserId = UserHandle.getUserId(targetUid);
        this.sourcePkg = sourcePkg;
        this.targetPkg = targetPkg;
        this.targetUid = targetUid;
        this.uri = uri;
    
Methods Summary
private voidaddReadOwner(UriPermissionOwner owner)

        if (mReadOwners == null) {
            mReadOwners = Sets.newArraySet();
            ownedModeFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
            updateModeFlags();
        }
        if (mReadOwners.add(owner)) {
            owner.addReadPermission(this);
        }
    
private voidaddWriteOwner(UriPermissionOwner owner)

        if (mWriteOwners == null) {
            mWriteOwners = Sets.newArraySet();
            ownedModeFlags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            updateModeFlags();
        }
        if (mWriteOwners.add(owner)) {
            owner.addWritePermission(this);
        }
    
public android.content.UriPermissionbuildPersistedPublicApiObject()

        return new android.content.UriPermission(uri.uri, persistedModeFlags, persistedCreateTime);
    
voiddump(java.io.PrintWriter pw, java.lang.String prefix)

        pw.print(prefix);
        pw.print("targetUserId=" + targetUserId);
        pw.print(" sourcePkg=" + sourcePkg);
        pw.println(" targetPkg=" + targetPkg);

        pw.print(prefix);
        pw.print("mode=0x" + Integer.toHexString(modeFlags));
        pw.print(" owned=0x" + Integer.toHexString(ownedModeFlags));
        pw.print(" global=0x" + Integer.toHexString(globalModeFlags));
        pw.print(" persistable=0x" + Integer.toHexString(persistableModeFlags));
        pw.print(" persisted=0x" + Integer.toHexString(persistedModeFlags));
        if (persistedCreateTime != INVALID_TIME) {
            pw.print(" persistedCreate=" + persistedCreateTime);
        }
        pw.println();

        if (mReadOwners != null) {
            pw.print(prefix);
            pw.println("readOwners:");
            for (UriPermissionOwner owner : mReadOwners) {
                pw.print(prefix);
                pw.println("  * " + owner);
            }
        }
        if (mWriteOwners != null) {
            pw.print(prefix);
            pw.println("writeOwners:");
            for (UriPermissionOwner owner : mReadOwners) {
                pw.print(prefix);
                pw.println("  * " + owner);
            }
        }
    
public intgetStrength(int modeFlags)
Return strength of this permission grant for the given flags.

        modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        if ((persistableModeFlags & modeFlags) == modeFlags) {
            return STRENGTH_PERSISTABLE;
        } else if ((globalModeFlags & modeFlags) == modeFlags) {
            return STRENGTH_GLOBAL;
        } else if ((ownedModeFlags & modeFlags) == modeFlags) {
            return STRENGTH_OWNED;
        } else {
            return STRENGTH_NONE;
        }
    
voidgrantModes(int modeFlags, UriPermissionOwner owner)

        final boolean persistable = (modeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0;
        modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        if (persistable) {
            persistableModeFlags |= modeFlags;
        }

        if (owner == null) {
            globalModeFlags |= modeFlags;
        } else {
            if ((modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
                addReadOwner(owner);
            }
            if ((modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
                addWriteOwner(owner);
            }
        }

        updateModeFlags();
    
voidinitPersistedModes(int modeFlags, long createdTime)
Initialize persisted modes as read from file. This doesn't issue any global or owner grants.

        modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        persistableModeFlags = modeFlags;
        persistedModeFlags = modeFlags;
        persistedCreateTime = createdTime;

        updateModeFlags();
    
booleanreleasePersistableModes(int modeFlags)

        modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        final int before = persistedModeFlags;

        persistableModeFlags &= ~modeFlags;
        persistedModeFlags &= ~modeFlags;

        if (persistedModeFlags == 0) {
            persistedCreateTime = INVALID_TIME;
        }

        updateModeFlags();
        return persistedModeFlags != before;
    
voidremoveReadOwner(UriPermissionOwner owner)
Remove given read owner, updating {@Link #modeFlags} as needed.

        if (!mReadOwners.remove(owner)) {
            Slog.wtf(TAG, "Unknown read owner " + owner + " in " + this);
        }
        if (mReadOwners.size() == 0) {
            mReadOwners = null;
            ownedModeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
            updateModeFlags();
        }
    
voidremoveWriteOwner(UriPermissionOwner owner)
Remove given write owner, updating {@Link #modeFlags} as needed.

        if (!mWriteOwners.remove(owner)) {
            Slog.wtf(TAG, "Unknown write owner " + owner + " in " + this);
        }
        if (mWriteOwners.size() == 0) {
            mWriteOwners = null;
            ownedModeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            updateModeFlags();
        }
    
booleanrevokeModes(int modeFlags, boolean includingOwners)

return
if mode changes should trigger persisting.

        final boolean persistable = (modeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0;
        modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        final int before = persistedModeFlags;

        if ((modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
            if (persistable) {
                persistableModeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
                persistedModeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
            }
            globalModeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
            if (mReadOwners != null && includingOwners) {
                ownedModeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
                for (UriPermissionOwner r : mReadOwners) {
                    r.removeReadPermission(this);
                }
                mReadOwners = null;
            }
        }
        if ((modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
            if (persistable) {
                persistableModeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
                persistedModeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            }
            globalModeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            if (mWriteOwners != null && includingOwners) {
                ownedModeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
                for (UriPermissionOwner r : mWriteOwners) {
                    r.removeWritePermission(this);
                }
                mWriteOwners = null;
            }
        }

        if (persistedModeFlags == 0) {
            persistedCreateTime = INVALID_TIME;
        }

        updateModeFlags();
        return persistedModeFlags != before;
    
public com.android.server.am.UriPermission$Snapshotsnapshot()

        return new Snapshot(this);
    
booleantakePersistableModes(int modeFlags)

return
if mode changes should trigger persisting.

        modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        if ((modeFlags & persistableModeFlags) != modeFlags) {
            Slog.w(TAG, "Requested flags 0x"
                    + Integer.toHexString(modeFlags) + ", but only 0x"
                    + Integer.toHexString(persistableModeFlags) + " are allowed");
            return false;
        }

        final int before = persistedModeFlags;
        persistedModeFlags |= (persistableModeFlags & modeFlags);

        if (persistedModeFlags != 0) {
            persistedCreateTime = System.currentTimeMillis();
        }

        updateModeFlags();
        return persistedModeFlags != before;
    
public java.lang.StringtoString()

        if (stringName != null) {
            return stringName;
        }
        StringBuilder sb = new StringBuilder(128);
        sb.append("UriPermission{");
        sb.append(Integer.toHexString(System.identityHashCode(this)));
        sb.append(' ");
        sb.append(uri);
        sb.append('}");
        return stringName = sb.toString();
    
private voidupdateModeFlags()

        modeFlags = ownedModeFlags | globalModeFlags | persistableModeFlags | persistedModeFlags;