Home | History | Annotate | Download | only in am
      1 /*
      2  * Copyright (C) 2010 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 import android.os.Binder;
     22 import android.os.IBinder;
     23 
     24 import java.util.HashSet;
     25 import java.util.Iterator;
     26 
     27 final class UriPermissionOwner {
     28     final ActivityManagerService service;
     29     final Object owner;
     30 
     31     Binder externalToken;
     32 
     33     HashSet<UriPermission> readUriPermissions; // special access to reading uris.
     34     HashSet<UriPermission> writeUriPermissions; // special access to writing uris.
     35 
     36     class ExternalToken extends Binder {
     37         UriPermissionOwner getOwner() {
     38             return UriPermissionOwner.this;
     39         }
     40     }
     41 
     42     UriPermissionOwner(ActivityManagerService _service, Object _owner) {
     43         service = _service;
     44         owner = _owner;
     45     }
     46 
     47     Binder getExternalTokenLocked() {
     48         if (externalToken == null) {
     49             externalToken = new ExternalToken();
     50         }
     51         return externalToken;
     52     }
     53 
     54     static UriPermissionOwner fromExternalToken(IBinder token) {
     55         if (token instanceof ExternalToken) {
     56             return ((ExternalToken)token).getOwner();
     57         }
     58         return null;
     59     }
     60 
     61     void removeUriPermissionsLocked() {
     62         removeUriPermissionsLocked(Intent.FLAG_GRANT_READ_URI_PERMISSION
     63                 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
     64     }
     65 
     66     void removeUriPermissionsLocked(int mode) {
     67         if ((mode&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0
     68                 && readUriPermissions != null) {
     69             for (UriPermission perm : readUriPermissions) {
     70                 perm.removeReadOwner(this);
     71                 service.removeUriPermissionIfNeededLocked(perm);
     72             }
     73             readUriPermissions = null;
     74         }
     75         if ((mode&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
     76                 && writeUriPermissions != null) {
     77             for (UriPermission perm : writeUriPermissions) {
     78                 perm.removeWriteOwner(this);
     79                 service.removeUriPermissionIfNeededLocked(perm);
     80             }
     81             writeUriPermissions = null;
     82         }
     83     }
     84 
     85     void removeUriPermissionLocked(Uri uri, int mode) {
     86         if ((mode&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0
     87                 && readUriPermissions != null) {
     88             Iterator<UriPermission> it = readUriPermissions.iterator();
     89             while (it.hasNext()) {
     90                 UriPermission perm = it.next();
     91                 if (uri.equals(perm.uri)) {
     92                     perm.removeReadOwner(this);
     93                     service.removeUriPermissionIfNeededLocked(perm);
     94                     it.remove();
     95                 }
     96             }
     97             if (readUriPermissions.size() == 0) {
     98                 readUriPermissions = null;
     99             }
    100         }
    101         if ((mode&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
    102                 && writeUriPermissions != null) {
    103             Iterator<UriPermission> it = writeUriPermissions.iterator();
    104             while (it.hasNext()) {
    105                 UriPermission perm = it.next();
    106                 if (uri.equals(perm.uri)) {
    107                     perm.removeWriteOwner(this);
    108                     service.removeUriPermissionIfNeededLocked(perm);
    109                     it.remove();
    110                 }
    111             }
    112             if (writeUriPermissions.size() == 0) {
    113                 writeUriPermissions = null;
    114             }
    115         }
    116     }
    117 
    118     public void addReadPermission(UriPermission perm) {
    119         if (readUriPermissions == null) {
    120             readUriPermissions = new HashSet<UriPermission>();
    121         }
    122         readUriPermissions.add(perm);
    123     }
    124 
    125     public void addWritePermission(UriPermission perm) {
    126         if (writeUriPermissions == null) {
    127             writeUriPermissions = new HashSet<UriPermission>();
    128         }
    129         writeUriPermissions.add(perm);
    130     }
    131 
    132     public void removeReadPermission(UriPermission perm) {
    133         readUriPermissions.remove(perm);
    134         if (readUriPermissions.size() == 0) {
    135             readUriPermissions = null;
    136         }
    137     }
    138 
    139     public void removeWritePermission(UriPermission perm) {
    140         writeUriPermissions.remove(perm);
    141         if (writeUriPermissions.size() == 0) {
    142             writeUriPermissions = null;
    143         }
    144     }
    145 
    146     @Override
    147     public String toString() {
    148         return owner.toString();
    149     }
    150 }
    151