blob: 65d70478442256cbef9e3593d0e1ea0938f289f8 [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;
20import android.net.Uri;
21import android.os.Binder;
22import android.os.IBinder;
Jeff Sharkey846318a2014-04-04 12:12:41 -070023import android.util.ArraySet;
Dianne Hackborn7e269642010-08-25 19:50:20 -070024
Jeff Sharkey846318a2014-04-04 12:12:41 -070025import com.google.android.collect.Sets;
26
27import java.io.PrintWriter;
Dianne Hackborn7e269642010-08-25 19:50:20 -070028import java.util.Iterator;
29
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070030final class UriPermissionOwner {
Dianne Hackborn7e269642010-08-25 19:50:20 -070031 final ActivityManagerService service;
32 final Object owner;
33
34 Binder externalToken;
35
Jeff Sharkey846318a2014-04-04 12:12:41 -070036 private ArraySet<UriPermission> mReadPerms;
37 private ArraySet<UriPermission> mWritePerms;
Dianne Hackborn7e269642010-08-25 19:50:20 -070038
39 class ExternalToken extends Binder {
40 UriPermissionOwner getOwner() {
41 return UriPermissionOwner.this;
42 }
43 }
44
Jeff Sharkey846318a2014-04-04 12:12:41 -070045 UriPermissionOwner(ActivityManagerService service, Object owner) {
46 this.service = service;
47 this.owner = owner;
Dianne Hackborn7e269642010-08-25 19:50:20 -070048 }
49
50 Binder getExternalTokenLocked() {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -070051 if (externalToken == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -070052 externalToken = new ExternalToken();
53 }
54 return externalToken;
55 }
56
57 static UriPermissionOwner fromExternalToken(IBinder token) {
58 if (token instanceof ExternalToken) {
59 return ((ExternalToken)token).getOwner();
60 }
61 return null;
62 }
63
64 void removeUriPermissionsLocked() {
65 removeUriPermissionsLocked(Intent.FLAG_GRANT_READ_URI_PERMISSION
66 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
67 }
68
69 void removeUriPermissionsLocked(int mode) {
Jeff Sharkey846318a2014-04-04 12:12:41 -070070 removeUriPermissionLocked(null, mode);
Dianne Hackborn7e269642010-08-25 19:50:20 -070071 }
72
73 void removeUriPermissionLocked(Uri uri, int mode) {
Jeff Sharkey846318a2014-04-04 12:12:41 -070074 if ((mode & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0
75 && mReadPerms != null) {
76 Iterator<UriPermission> it = mReadPerms.iterator();
Dianne Hackborn7e269642010-08-25 19:50:20 -070077 while (it.hasNext()) {
78 UriPermission perm = it.next();
Jeff Sharkey846318a2014-04-04 12:12:41 -070079 if (uri == null || uri.equals(perm.uri)) {
Jeff Sharkey328ebf22013-03-21 18:09:39 -070080 perm.removeReadOwner(this);
81 service.removeUriPermissionIfNeededLocked(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -070082 it.remove();
83 }
84 }
Jeff Sharkey846318a2014-04-04 12:12:41 -070085 if (mReadPerms.isEmpty()) {
86 mReadPerms = null;
Dianne Hackborn7e269642010-08-25 19:50:20 -070087 }
88 }
Jeff Sharkey846318a2014-04-04 12:12:41 -070089 if ((mode & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
90 && mWritePerms != null) {
91 Iterator<UriPermission> it = mWritePerms.iterator();
Dianne Hackborn7e269642010-08-25 19:50:20 -070092 while (it.hasNext()) {
93 UriPermission perm = it.next();
Jeff Sharkey846318a2014-04-04 12:12:41 -070094 if (uri == null || uri.equals(perm.uri)) {
Jeff Sharkey328ebf22013-03-21 18:09:39 -070095 perm.removeWriteOwner(this);
96 service.removeUriPermissionIfNeededLocked(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -070097 it.remove();
98 }
99 }
Jeff Sharkey846318a2014-04-04 12:12:41 -0700100 if (mWritePerms.isEmpty()) {
101 mWritePerms = null;
Dianne Hackborn7e269642010-08-25 19:50:20 -0700102 }
103 }
104 }
105
106 public void addReadPermission(UriPermission perm) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700107 if (mReadPerms == null) {
108 mReadPerms = Sets.newArraySet();
Dianne Hackborn7e269642010-08-25 19:50:20 -0700109 }
Jeff Sharkey846318a2014-04-04 12:12:41 -0700110 mReadPerms.add(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -0700111 }
112
113 public void addWritePermission(UriPermission perm) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700114 if (mWritePerms == null) {
115 mWritePerms = Sets.newArraySet();
Dianne Hackborn7e269642010-08-25 19:50:20 -0700116 }
Jeff Sharkey846318a2014-04-04 12:12:41 -0700117 mWritePerms.add(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -0700118 }
119
120 public void removeReadPermission(UriPermission perm) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700121 mReadPerms.remove(perm);
122 if (mReadPerms.isEmpty()) {
123 mReadPerms = null;
Dianne Hackborn7e269642010-08-25 19:50:20 -0700124 }
125 }
126
127 public void removeWritePermission(UriPermission perm) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700128 mWritePerms.remove(perm);
129 if (mWritePerms.isEmpty()) {
130 mWritePerms = null;
131 }
132 }
133
134 public void dump(PrintWriter pw, String prefix) {
135 if (mReadPerms != null) {
136 pw.print(prefix); pw.print("readUriPermissions="); pw.println(mReadPerms);
137 }
138 if (mWritePerms != null) {
139 pw.print(prefix); pw.print("writeUriPermissions="); pw.println(mWritePerms);
Dianne Hackborn7e269642010-08-25 19:50:20 -0700140 }
141 }
142
143 @Override
144 public String toString() {
145 return owner.toString();
146 }
147}