blob: 28344df285f2f455356dc39375facc6f29259d9b [file] [log] [blame]
Dianne Hackborn7e269642010-08-25 19:50:20 -07001/*
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
17package com.android.server.am;
18
19import android.content.Intent;
Dianne Hackborn7e269642010-08-25 19:50:20 -070020import android.os.Binder;
21import android.os.IBinder;
Jeff Sharkey846318a2014-04-04 12:12:41 -070022import android.util.ArraySet;
Dianne Hackborn7e269642010-08-25 19:50:20 -070023
Jeff Sharkey846318a2014-04-04 12:12:41 -070024import com.google.android.collect.Sets;
25
26import java.io.PrintWriter;
Dianne Hackborn7e269642010-08-25 19:50:20 -070027import java.util.Iterator;
28
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070029final class UriPermissionOwner {
Dianne Hackborn7e269642010-08-25 19:50:20 -070030 final ActivityManagerService service;
31 final Object owner;
32
33 Binder externalToken;
34
Jeff Sharkey846318a2014-04-04 12:12:41 -070035 private ArraySet<UriPermission> mReadPerms;
36 private ArraySet<UriPermission> mWritePerms;
Dianne Hackborn7e269642010-08-25 19:50:20 -070037
38 class ExternalToken extends Binder {
39 UriPermissionOwner getOwner() {
40 return UriPermissionOwner.this;
41 }
42 }
43
Jeff Sharkey846318a2014-04-04 12:12:41 -070044 UriPermissionOwner(ActivityManagerService service, Object owner) {
45 this.service = service;
46 this.owner = owner;
Dianne Hackborn7e269642010-08-25 19:50:20 -070047 }
48
49 Binder getExternalTokenLocked() {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -070050 if (externalToken == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -070051 externalToken = new ExternalToken();
52 }
53 return externalToken;
54 }
55
56 static UriPermissionOwner fromExternalToken(IBinder token) {
57 if (token instanceof ExternalToken) {
58 return ((ExternalToken)token).getOwner();
59 }
60 return null;
61 }
62
63 void removeUriPermissionsLocked() {
64 removeUriPermissionsLocked(Intent.FLAG_GRANT_READ_URI_PERMISSION
65 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
66 }
67
68 void removeUriPermissionsLocked(int mode) {
Jeff Sharkey846318a2014-04-04 12:12:41 -070069 removeUriPermissionLocked(null, mode);
Dianne Hackborn7e269642010-08-25 19:50:20 -070070 }
71
Nicolas Prevotd85fc722014-04-16 19:52:08 +010072 void removeUriPermissionLocked(ActivityManagerService.GrantUri grantUri, int mode) {
Jeff Sharkey846318a2014-04-04 12:12:41 -070073 if ((mode & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0
74 && mReadPerms != null) {
75 Iterator<UriPermission> it = mReadPerms.iterator();
Dianne Hackborn7e269642010-08-25 19:50:20 -070076 while (it.hasNext()) {
77 UriPermission perm = it.next();
Nicolas Prevotd85fc722014-04-16 19:52:08 +010078 if (grantUri == null || grantUri.equals(perm.uri)) {
Jeff Sharkey328ebf22013-03-21 18:09:39 -070079 perm.removeReadOwner(this);
80 service.removeUriPermissionIfNeededLocked(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -070081 it.remove();
82 }
83 }
Jeff Sharkey846318a2014-04-04 12:12:41 -070084 if (mReadPerms.isEmpty()) {
85 mReadPerms = null;
Dianne Hackborn7e269642010-08-25 19:50:20 -070086 }
87 }
Jeff Sharkey846318a2014-04-04 12:12:41 -070088 if ((mode & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
89 && mWritePerms != null) {
90 Iterator<UriPermission> it = mWritePerms.iterator();
Dianne Hackborn7e269642010-08-25 19:50:20 -070091 while (it.hasNext()) {
92 UriPermission perm = it.next();
Nicolas Prevotd85fc722014-04-16 19:52:08 +010093 if (grantUri == null || grantUri.equals(perm.uri)) {
Jeff Sharkey328ebf22013-03-21 18:09:39 -070094 perm.removeWriteOwner(this);
95 service.removeUriPermissionIfNeededLocked(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -070096 it.remove();
97 }
98 }
Jeff Sharkey846318a2014-04-04 12:12:41 -070099 if (mWritePerms.isEmpty()) {
100 mWritePerms = null;
Dianne Hackborn7e269642010-08-25 19:50:20 -0700101 }
102 }
103 }
104
105 public void addReadPermission(UriPermission perm) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700106 if (mReadPerms == null) {
107 mReadPerms = Sets.newArraySet();
Dianne Hackborn7e269642010-08-25 19:50:20 -0700108 }
Jeff Sharkey846318a2014-04-04 12:12:41 -0700109 mReadPerms.add(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -0700110 }
111
112 public void addWritePermission(UriPermission perm) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700113 if (mWritePerms == null) {
114 mWritePerms = Sets.newArraySet();
Dianne Hackborn7e269642010-08-25 19:50:20 -0700115 }
Jeff Sharkey846318a2014-04-04 12:12:41 -0700116 mWritePerms.add(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -0700117 }
118
119 public void removeReadPermission(UriPermission perm) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700120 mReadPerms.remove(perm);
121 if (mReadPerms.isEmpty()) {
122 mReadPerms = null;
Dianne Hackborn7e269642010-08-25 19:50:20 -0700123 }
124 }
125
126 public void removeWritePermission(UriPermission perm) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700127 mWritePerms.remove(perm);
128 if (mWritePerms.isEmpty()) {
129 mWritePerms = null;
130 }
131 }
132
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);
Dianne Hackborn7e269642010-08-25 19:50:20 -0700139 }
140 }
141
142 @Override
143 public String toString() {
144 return owner.toString();
145 }
146}