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.os.Binder;
21 import android.os.IBinder;
22 import android.util.ArraySet;
23 
24 import com.google.android.collect.Sets;
25 
26 import java.io.PrintWriter;
27 import java.util.Iterator;
28 
29 final class UriPermissionOwner {
30     final ActivityManagerService service;
31     final Object owner;
32 
33     Binder externalToken;
34 
35     private ArraySet<UriPermission> mReadPerms;
36     private ArraySet<UriPermission> mWritePerms;
37 
38     class ExternalToken extends Binder {
getOwner()39         UriPermissionOwner getOwner() {
40             return UriPermissionOwner.this;
41         }
42     }
43 
UriPermissionOwner(ActivityManagerService service, Object owner)44     UriPermissionOwner(ActivityManagerService service, Object owner) {
45         this.service = service;
46         this.owner = owner;
47     }
48 
getExternalTokenLocked()49     Binder getExternalTokenLocked() {
50         if (externalToken == null) {
51             externalToken = new ExternalToken();
52         }
53         return externalToken;
54     }
55 
fromExternalToken(IBinder token)56     static UriPermissionOwner fromExternalToken(IBinder token) {
57         if (token instanceof ExternalToken) {
58             return ((ExternalToken)token).getOwner();
59         }
60         return null;
61     }
62 
removeUriPermissionsLocked()63     void removeUriPermissionsLocked() {
64         removeUriPermissionsLocked(Intent.FLAG_GRANT_READ_URI_PERMISSION
65                 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
66     }
67 
removeUriPermissionsLocked(int mode)68     void removeUriPermissionsLocked(int mode) {
69         removeUriPermissionLocked(null, mode);
70     }
71 
removeUriPermissionLocked(ActivityManagerService.GrantUri grantUri, int mode)72     void removeUriPermissionLocked(ActivityManagerService.GrantUri grantUri, int mode) {
73         if ((mode & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0
74                 && mReadPerms != null) {
75             Iterator<UriPermission> it = mReadPerms.iterator();
76             while (it.hasNext()) {
77                 UriPermission perm = it.next();
78                 if (grantUri == null || grantUri.equals(perm.uri)) {
79                     perm.removeReadOwner(this);
80                     service.removeUriPermissionIfNeededLocked(perm);
81                     it.remove();
82                 }
83             }
84             if (mReadPerms.isEmpty()) {
85                 mReadPerms = null;
86             }
87         }
88         if ((mode & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
89                 && mWritePerms != null) {
90             Iterator<UriPermission> it = mWritePerms.iterator();
91             while (it.hasNext()) {
92                 UriPermission perm = it.next();
93                 if (grantUri == null || grantUri.equals(perm.uri)) {
94                     perm.removeWriteOwner(this);
95                     service.removeUriPermissionIfNeededLocked(perm);
96                     it.remove();
97                 }
98             }
99             if (mWritePerms.isEmpty()) {
100                 mWritePerms = null;
101             }
102         }
103     }
104 
addReadPermission(UriPermission perm)105     public void addReadPermission(UriPermission perm) {
106         if (mReadPerms == null) {
107             mReadPerms = Sets.newArraySet();
108         }
109         mReadPerms.add(perm);
110     }
111 
addWritePermission(UriPermission perm)112     public void addWritePermission(UriPermission perm) {
113         if (mWritePerms == null) {
114             mWritePerms = Sets.newArraySet();
115         }
116         mWritePerms.add(perm);
117     }
118 
removeReadPermission(UriPermission perm)119     public void removeReadPermission(UriPermission perm) {
120         mReadPerms.remove(perm);
121         if (mReadPerms.isEmpty()) {
122             mReadPerms = null;
123         }
124     }
125 
removeWritePermission(UriPermission perm)126     public void removeWritePermission(UriPermission perm) {
127         mWritePerms.remove(perm);
128         if (mWritePerms.isEmpty()) {
129             mWritePerms = null;
130         }
131     }
132 
dump(PrintWriter pw, String prefix)133     public void dump(PrintWriter pw, String prefix) {
134         if (mReadPerms != null) {
135             pw.print(prefix); pw.print("readUriPermissions="); pw.println(mReadPerms);
136         }
137         if (mWritePerms != null) {
138             pw.print(prefix); pw.print("writeUriPermissions="); pw.println(mWritePerms);
139         }
140     }
141 
142     @Override
toString()143     public String toString() {
144         return owner.toString();
145     }
146 }
147