Home | History | Annotate | Download | only in am
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.server.am;
     18 
     19 import android.content.Intent;
     20 import android.net.Uri;
     21 
     22 import java.io.PrintWriter;
     23 import java.util.HashSet;
     24 
     25 /**
     26  * Description of a permission granted to an app to access a particular URI.
     27  *
     28  * CTS tests for this functionality can be run with "runtest cts-appsecurity".
     29  *
     30  * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/
     31  *      src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java
     32  */
     33 class UriPermission {
     34     final int uid;
     35     final Uri uri;
     36     int modeFlags = 0;
     37     int globalModeFlags = 0;
     38     final HashSet<UriPermissionOwner> readOwners = new HashSet<UriPermissionOwner>();
     39     final HashSet<UriPermissionOwner> writeOwners = new HashSet<UriPermissionOwner>();
     40 
     41     String stringName;
     42 
     43     UriPermission(int _uid, Uri _uri) {
     44         uid = _uid;
     45         uri = _uri;
     46     }
     47 
     48     void clearModes(int modeFlagsToClear) {
     49         if ((modeFlagsToClear&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
     50             globalModeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
     51             modeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
     52             if (readOwners.size() > 0) {
     53                 for (UriPermissionOwner r : readOwners) {
     54                     r.removeReadPermission(this);
     55                 }
     56                 readOwners.clear();
     57             }
     58         }
     59         if ((modeFlagsToClear&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
     60             globalModeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
     61             modeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
     62             if (readOwners.size() > 0) {
     63                 for (UriPermissionOwner r : writeOwners) {
     64                     r.removeWritePermission(this);
     65                 }
     66                 readOwners.clear();
     67             }
     68         }
     69     }
     70 
     71     public String toString() {
     72         if (stringName != null) {
     73             return stringName;
     74         }
     75         StringBuilder sb = new StringBuilder(128);
     76         sb.append("UriPermission{");
     77         sb.append(Integer.toHexString(System.identityHashCode(this)));
     78         sb.append(' ');
     79         sb.append(uri);
     80         sb.append('}');
     81         return stringName = sb.toString();
     82     }
     83 
     84     void dump(PrintWriter pw, String prefix) {
     85         pw.print(prefix); pw.print("modeFlags=0x");
     86                 pw.print(Integer.toHexString(modeFlags));
     87                 pw.print(" uid="); pw.print(uid);
     88                 pw.print(" globalModeFlags=0x");
     89                 pw.println(Integer.toHexString(globalModeFlags));
     90         if (readOwners.size() != 0) {
     91             pw.print(prefix); pw.println("readOwners:");
     92             for (UriPermissionOwner owner : readOwners) {
     93                 pw.print(prefix); pw.print("  * "); pw.println(owner);
     94             }
     95         }
     96         if (writeOwners.size() != 0) {
     97             pw.print(prefix); pw.println("writeOwners:");
     98             for (UriPermissionOwner owner : writeOwners) {
     99                 pw.print(prefix); pw.print("  * "); pw.println(owner);
    100             }
    101         }
    102     }
    103 }
    104