blob: 9dbf5f51cbd7b06121c0ff047bc0609d433eca16 [file] [log] [blame]
Amith Yamasani742a6712011-05-04 14:49:28 -07001/*
2 * Copyright (C) 2011 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.ComponentName;
Amith Yamasani742a6712011-05-04 14:49:28 -070020import android.os.Binder;
Marco Nelissende7408c2012-02-08 14:57:38 -080021import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070022import android.os.UserHandle;
Amith Yamasani742a6712011-05-04 14:49:28 -070023import android.util.Slog;
24import android.util.SparseArray;
25
Marco Nelissende7408c2012-02-08 14:57:38 -080026import java.io.FileDescriptor;
27import java.io.IOException;
Amith Yamasani742a6712011-05-04 14:49:28 -070028import java.io.PrintWriter;
Marco Nelissende7408c2012-02-08 14:57:38 -080029import java.util.ArrayList;
Amith Yamasani742a6712011-05-04 14:49:28 -070030import java.util.HashMap;
31import java.util.Iterator;
32import java.util.Map;
33
34/**
35 * Keeps track of content providers by authority (name) and class. It separates the mapping by
36 * user and ones that are not user-specific (system providers).
37 */
38public class ProviderMap {
39
40 private static final String TAG = "ProviderMap";
41
42 private static final boolean DBG = false;
43
Dianne Hackborn2d1b3782012-09-09 17:49:39 -070044 private final ActivityManagerService mAm;
45
Dianne Hackborn7d19e022012-08-07 19:12:33 -070046 private final HashMap<String, ContentProviderRecord> mSingletonByName
Amith Yamasani742a6712011-05-04 14:49:28 -070047 = new HashMap<String, ContentProviderRecord>();
Dianne Hackborn7d19e022012-08-07 19:12:33 -070048 private final HashMap<ComponentName, ContentProviderRecord> mSingletonByClass
Amith Yamasani742a6712011-05-04 14:49:28 -070049 = new HashMap<ComponentName, ContentProviderRecord>();
50
51 private final SparseArray<HashMap<String, ContentProviderRecord>> mProvidersByNamePerUser
52 = new SparseArray<HashMap<String, ContentProviderRecord>>();
53 private final SparseArray<HashMap<ComponentName, ContentProviderRecord>> mProvidersByClassPerUser
54 = new SparseArray<HashMap<ComponentName, ContentProviderRecord>>();
55
Dianne Hackborn2d1b3782012-09-09 17:49:39 -070056 ProviderMap(ActivityManagerService am) {
57 mAm = am;
58 }
59
Amith Yamasani742a6712011-05-04 14:49:28 -070060 ContentProviderRecord getProviderByName(String name) {
61 return getProviderByName(name, -1);
62 }
63
64 ContentProviderRecord getProviderByName(String name, int userId) {
65 if (DBG) {
66 Slog.i(TAG, "getProviderByName: " + name + " , callingUid = " + Binder.getCallingUid());
67 }
68 // Try to find it in the global list
Dianne Hackborn7d19e022012-08-07 19:12:33 -070069 ContentProviderRecord record = mSingletonByName.get(name);
Amith Yamasani742a6712011-05-04 14:49:28 -070070 if (record != null) {
71 return record;
72 }
73
74 // Check the current user's list
75 return getProvidersByName(userId).get(name);
76 }
77
78 ContentProviderRecord getProviderByClass(ComponentName name) {
79 return getProviderByClass(name, -1);
80 }
81
82 ContentProviderRecord getProviderByClass(ComponentName name, int userId) {
83 if (DBG) {
84 Slog.i(TAG, "getProviderByClass: " + name + ", callingUid = " + Binder.getCallingUid());
85 }
86 // Try to find it in the global list
Dianne Hackborn7d19e022012-08-07 19:12:33 -070087 ContentProviderRecord record = mSingletonByClass.get(name);
Amith Yamasani742a6712011-05-04 14:49:28 -070088 if (record != null) {
89 return record;
90 }
91
92 // Check the current user's list
93 return getProvidersByClass(userId).get(name);
94 }
95
96 void putProviderByName(String name, ContentProviderRecord record) {
97 if (DBG) {
98 Slog.i(TAG, "putProviderByName: " + name + " , callingUid = " + Binder.getCallingUid()
99 + ", record uid = " + record.appInfo.uid);
100 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700101 if (record.singleton) {
102 mSingletonByName.put(name, record);
Amith Yamasani742a6712011-05-04 14:49:28 -0700103 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700104 final int userId = UserHandle.getUserId(record.appInfo.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700105 getProvidersByName(userId).put(name, record);
106 }
107 }
108
109 void putProviderByClass(ComponentName name, ContentProviderRecord record) {
110 if (DBG) {
111 Slog.i(TAG, "putProviderByClass: " + name + " , callingUid = " + Binder.getCallingUid()
112 + ", record uid = " + record.appInfo.uid);
113 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700114 if (record.singleton) {
115 mSingletonByClass.put(name, record);
Amith Yamasani742a6712011-05-04 14:49:28 -0700116 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700117 final int userId = UserHandle.getUserId(record.appInfo.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700118 getProvidersByClass(userId).put(name, record);
119 }
120 }
121
Dianne Hackborn41203752012-08-31 14:05:51 -0700122 void removeProviderByName(String name, int userId) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700123 if (mSingletonByName.containsKey(name)) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700124 if (DBG)
125 Slog.i(TAG, "Removing from globalByName name=" + name);
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700126 mSingletonByName.remove(name);
Amith Yamasani742a6712011-05-04 14:49:28 -0700127 } else {
Dianne Hackborn41203752012-08-31 14:05:51 -0700128 if (userId < 0) throw new IllegalArgumentException("Bad user " + userId);
Amith Yamasani742a6712011-05-04 14:49:28 -0700129 if (DBG)
130 Slog.i(TAG,
Dianne Hackborn41203752012-08-31 14:05:51 -0700131 "Removing from providersByName name=" + name + " user=" + userId);
132 HashMap<String, ContentProviderRecord> map = getProvidersByName(userId);
Vairavan Srinivasan88090042012-08-15 23:37:45 -0700133 // map returned by getProvidersByName wouldn't be null
134 map.remove(name);
135 if (map.size() == 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700136 mProvidersByNamePerUser.remove(userId);
Vairavan Srinivasan88090042012-08-15 23:37:45 -0700137 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700138 }
139 }
140
Dianne Hackborn41203752012-08-31 14:05:51 -0700141 void removeProviderByClass(ComponentName name, int userId) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700142 if (mSingletonByClass.containsKey(name)) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700143 if (DBG)
144 Slog.i(TAG, "Removing from globalByClass name=" + name);
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700145 mSingletonByClass.remove(name);
Amith Yamasani742a6712011-05-04 14:49:28 -0700146 } else {
Dianne Hackborn41203752012-08-31 14:05:51 -0700147 if (userId < 0) throw new IllegalArgumentException("Bad user " + userId);
Amith Yamasani742a6712011-05-04 14:49:28 -0700148 if (DBG)
149 Slog.i(TAG,
Dianne Hackborn41203752012-08-31 14:05:51 -0700150 "Removing from providersByClass name=" + name + " user=" + userId);
151 HashMap<ComponentName, ContentProviderRecord> map = getProvidersByClass(userId);
Vairavan Srinivasan88090042012-08-15 23:37:45 -0700152 // map returned by getProvidersByClass wouldn't be null
153 map.remove(name);
154 if (map.size() == 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700155 mProvidersByClassPerUser.remove(userId);
Vairavan Srinivasan88090042012-08-15 23:37:45 -0700156 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700157 }
158 }
159
Dianne Hackborn41203752012-08-31 14:05:51 -0700160 private HashMap<String, ContentProviderRecord> getProvidersByName(int userId) {
161 if (userId < 0) throw new IllegalArgumentException("Bad user " + userId);
Amith Yamasani742a6712011-05-04 14:49:28 -0700162 final HashMap<String, ContentProviderRecord> map = mProvidersByNamePerUser.get(userId);
163 if (map == null) {
164 HashMap<String, ContentProviderRecord> newMap = new HashMap<String, ContentProviderRecord>();
165 mProvidersByNamePerUser.put(userId, newMap);
166 return newMap;
167 } else {
168 return map;
169 }
170 }
171
Dianne Hackborn41203752012-08-31 14:05:51 -0700172 HashMap<ComponentName, ContentProviderRecord> getProvidersByClass(int userId) {
173 if (userId < 0) throw new IllegalArgumentException("Bad user " + userId);
174 final HashMap<ComponentName, ContentProviderRecord> map
175 = mProvidersByClassPerUser.get(userId);
Amith Yamasani742a6712011-05-04 14:49:28 -0700176 if (map == null) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700177 HashMap<ComponentName, ContentProviderRecord> newMap
178 = new HashMap<ComponentName, ContentProviderRecord>();
Amith Yamasani742a6712011-05-04 14:49:28 -0700179 mProvidersByClassPerUser.put(userId, newMap);
180 return newMap;
181 } else {
182 return map;
183 }
184 }
185
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -0700186 private boolean collectForceStopProvidersLocked(String name, int appId,
187 boolean doit, boolean evenPersistent, int userId,
188 HashMap<ComponentName, ContentProviderRecord> providers,
189 ArrayList<ContentProviderRecord> result) {
190 boolean didSomething = false;
191 for (ContentProviderRecord provider : providers.values()) {
192 if ((name == null || provider.info.packageName.equals(name))
193 && (provider.proc == null || evenPersistent || !provider.proc.persistent)) {
194 if (!doit) {
195 return true;
196 }
197 didSomething = true;
198 result.add(provider);
199 }
200 }
201 return didSomething;
202 }
203
204 boolean collectForceStopProviders(String name, int appId,
205 boolean doit, boolean evenPersistent, int userId,
206 ArrayList<ContentProviderRecord> result) {
207 boolean didSomething = collectForceStopProvidersLocked(name, appId, doit,
208 evenPersistent, userId, mSingletonByClass, result);
209 if (!doit && didSomething) {
210 return true;
211 }
212 if (userId == UserHandle.USER_ALL) {
213 for (int i=0; i<mProvidersByClassPerUser.size(); i++) {
214 if (collectForceStopProvidersLocked(name, appId, doit, evenPersistent,
215 userId, mProvidersByClassPerUser.valueAt(i), result)) {
216 if (!doit) {
217 return true;
218 }
219 didSomething = true;
220 }
221 }
222 } else {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700223 HashMap<ComponentName, ContentProviderRecord> items
224 = getProvidersByClass(userId);
225 if (items != null) {
226 didSomething |= collectForceStopProvidersLocked(name, appId, doit,
227 evenPersistent, userId, items, result);
228 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -0700229 }
230 return didSomething;
231 }
232
Amith Yamasani742a6712011-05-04 14:49:28 -0700233 private void dumpProvidersByClassLocked(PrintWriter pw, boolean dumpAll,
234 HashMap<ComponentName, ContentProviderRecord> map) {
235 Iterator<Map.Entry<ComponentName, ContentProviderRecord>> it = map.entrySet().iterator();
236 while (it.hasNext()) {
237 Map.Entry<ComponentName, ContentProviderRecord> e = it.next();
238 ContentProviderRecord r = e.getValue();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700239 pw.print(" * ");
240 pw.println(r);
241 r.dump(pw, " ", dumpAll);
Amith Yamasani742a6712011-05-04 14:49:28 -0700242 }
243 }
244
245 private void dumpProvidersByNameLocked(PrintWriter pw,
246 HashMap<String, ContentProviderRecord> map) {
247 Iterator<Map.Entry<String, ContentProviderRecord>> it = map.entrySet().iterator();
248 while (it.hasNext()) {
249 Map.Entry<String, ContentProviderRecord> e = it.next();
250 ContentProviderRecord r = e.getValue();
251 pw.print(" ");
252 pw.print(e.getKey());
253 pw.print(": ");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700254 pw.println(r.toShortString());
Amith Yamasani742a6712011-05-04 14:49:28 -0700255 }
256 }
257
258 void dumpProvidersLocked(PrintWriter pw, boolean dumpAll) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700259 if (mSingletonByClass.size() > 0) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700260 pw.println(" Published single-user content providers (by class):");
261 dumpProvidersByClassLocked(pw, dumpAll, mSingletonByClass);
Amith Yamasani742a6712011-05-04 14:49:28 -0700262 }
263
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700264 pw.println("");
265 for (int i = 0; i < mProvidersByClassPerUser.size(); i++) {
266 HashMap<ComponentName, ContentProviderRecord> map = mProvidersByClassPerUser.valueAt(i);
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -0700267 pw.println("");
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700268 pw.println(" Published user " + mProvidersByClassPerUser.keyAt(i)
269 + " content providers (by class):");
Amith Yamasani742a6712011-05-04 14:49:28 -0700270 dumpProvidersByClassLocked(pw, dumpAll, map);
271 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700272
273 if (dumpAll) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700274 pw.println("");
275 pw.println(" Single-user authority to provider mappings:");
276 dumpProvidersByNameLocked(pw, mSingletonByName);
Amith Yamasani742a6712011-05-04 14:49:28 -0700277
278 for (int i = 0; i < mProvidersByNamePerUser.size(); i++) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700279 pw.println("");
280 pw.println(" User " + mProvidersByNamePerUser.keyAt(i)
281 + " authority to provider mappings:");
Amith Yamasani742a6712011-05-04 14:49:28 -0700282 dumpProvidersByNameLocked(pw, mProvidersByNamePerUser.valueAt(i));
283 }
284 }
285 }
Marco Nelissende7408c2012-02-08 14:57:38 -0800286
287 protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args,
288 int opti, boolean dumpAll) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700289 ArrayList<ContentProviderRecord> allProviders = new ArrayList<ContentProviderRecord>();
Marco Nelissende7408c2012-02-08 14:57:38 -0800290 ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>();
291
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700292 synchronized (mAm) {
293 allProviders.addAll(mSingletonByClass.values());
294 for (int i=0; i<mProvidersByClassPerUser.size(); i++) {
295 allProviders.addAll(mProvidersByClassPerUser.valueAt(i).values());
Marco Nelissende7408c2012-02-08 14:57:38 -0800296 }
297
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700298 if ("all".equals(name)) {
299 providers.addAll(allProviders);
300 } else {
301 ComponentName componentName = name != null
302 ? ComponentName.unflattenFromString(name) : null;
303 int objectId = 0;
304 if (componentName == null) {
305 // Not a '/' separated full component name; maybe an object ID?
306 try {
307 objectId = Integer.parseInt(name, 16);
308 name = null;
309 componentName = null;
310 } catch (RuntimeException e) {
311 }
312 }
313
314 for (int i=0; i<allProviders.size(); i++) {
315 ContentProviderRecord r1 = allProviders.get(i);
Marco Nelissende7408c2012-02-08 14:57:38 -0800316 if (componentName != null) {
317 if (r1.name.equals(componentName)) {
318 providers.add(r1);
319 }
320 } else if (name != null) {
321 if (r1.name.flattenToString().contains(name)) {
322 providers.add(r1);
323 }
324 } else if (System.identityHashCode(r1) == objectId) {
325 providers.add(r1);
326 }
327 }
328 }
329 }
330
331 if (providers.size() <= 0) {
332 return false;
333 }
334
335 boolean needSep = false;
336 for (int i=0; i<providers.size(); i++) {
337 if (needSep) {
338 pw.println();
339 }
340 needSep = true;
341 dumpProvider("", fd, pw, providers.get(i), args, dumpAll);
342 }
343 return true;
344 }
345
346 /**
347 * Invokes IApplicationThread.dumpProvider() on the thread of the specified provider if
348 * there is a thread associated with the provider.
349 */
350 private void dumpProvider(String prefix, FileDescriptor fd, PrintWriter pw,
351 final ContentProviderRecord r, String[] args, boolean dumpAll) {
352 String innerPrefix = prefix + " ";
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700353 synchronized (mAm) {
Marco Nelissende7408c2012-02-08 14:57:38 -0800354 pw.print(prefix); pw.print("PROVIDER ");
355 pw.print(r);
356 pw.print(" pid=");
357 if (r.proc != null) pw.println(r.proc.pid);
358 else pw.println("(not running)");
359 if (dumpAll) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700360 r.dump(pw, innerPrefix, true);
Marco Nelissende7408c2012-02-08 14:57:38 -0800361 }
362 }
363 if (r.proc != null && r.proc.thread != null) {
364 pw.println(" Client:");
365 pw.flush();
366 try {
367 TransferPipe tp = new TransferPipe();
368 try {
369 r.proc.thread.dumpProvider(
370 tp.getWriteFd().getFileDescriptor(), r.provider.asBinder(), args);
371 tp.setBufferPrefix(" ");
372 // Short timeout, since blocking here can
373 // deadlock with the application.
374 tp.go(fd, 2000);
375 } finally {
376 tp.kill();
377 }
378 } catch (IOException ex) {
379 pw.println(" Failure while dumping the provider: " + ex);
380 } catch (RemoteException ex) {
381 pw.println(" Got a RemoteException while dumping the service");
382 }
383 }
384 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700385}