blob: 7bbd3bcc2eb9101fb169e399881b20ffa72d5037 [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;
23
24import java.util.HashSet;
25import java.util.Iterator;
26
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070027final class UriPermissionOwner {
Dianne Hackborn7e269642010-08-25 19:50:20 -070028 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() {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -070048 if (externalToken == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -070049 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) {
Jeff Sharkey328ebf22013-03-21 18:09:39 -070070 perm.removeReadOwner(this);
71 service.removeUriPermissionIfNeededLocked(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -070072 }
73 readUriPermissions = null;
74 }
75 if ((mode&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
76 && writeUriPermissions != null) {
77 for (UriPermission perm : writeUriPermissions) {
Jeff Sharkey328ebf22013-03-21 18:09:39 -070078 perm.removeWriteOwner(this);
79 service.removeUriPermissionIfNeededLocked(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -070080 }
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)) {
Jeff Sharkey328ebf22013-03-21 18:09:39 -070092 perm.removeReadOwner(this);
93 service.removeUriPermissionIfNeededLocked(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -070094 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)) {
Jeff Sharkey328ebf22013-03-21 18:09:39 -0700107 perm.removeWriteOwner(this);
108 service.removeUriPermissionIfNeededLocked(perm);
Dianne Hackborn7e269642010-08-25 19:50:20 -0700109 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}