ProviderMap.java revision f88dd0b32ea2042eb2011170be465259a21d2563
1/* 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; 20import android.os.Binder; 21import android.os.Process; 22import android.os.RemoteException; 23import android.os.UserId; 24import android.util.Slog; 25import android.util.SparseArray; 26 27import java.io.FileDescriptor; 28import java.io.IOException; 29import java.io.PrintWriter; 30import java.util.ArrayList; 31import java.util.HashMap; 32import java.util.Iterator; 33import java.util.Map; 34import java.util.Map.Entry; 35import java.util.Set; 36 37/** 38 * Keeps track of content providers by authority (name) and class. It separates the mapping by 39 * user and ones that are not user-specific (system providers). 40 */ 41public class ProviderMap { 42 43 private static final String TAG = "ProviderMap"; 44 45 private static final boolean DBG = false; 46 47 private final HashMap<String, ContentProviderRecord> mSingletonByName 48 = new HashMap<String, ContentProviderRecord>(); 49 private final HashMap<ComponentName, ContentProviderRecord> mSingletonByClass 50 = new HashMap<ComponentName, ContentProviderRecord>(); 51 52 private final SparseArray<HashMap<String, ContentProviderRecord>> mProvidersByNamePerUser 53 = new SparseArray<HashMap<String, ContentProviderRecord>>(); 54 private final SparseArray<HashMap<ComponentName, ContentProviderRecord>> mProvidersByClassPerUser 55 = new SparseArray<HashMap<ComponentName, ContentProviderRecord>>(); 56 57 ContentProviderRecord getProviderByName(String name) { 58 return getProviderByName(name, -1); 59 } 60 61 ContentProviderRecord getProviderByName(String name, int userId) { 62 if (DBG) { 63 Slog.i(TAG, "getProviderByName: " + name + " , callingUid = " + Binder.getCallingUid()); 64 } 65 // Try to find it in the global list 66 ContentProviderRecord record = mSingletonByName.get(name); 67 if (record != null) { 68 return record; 69 } 70 71 // Check the current user's list 72 return getProvidersByName(userId).get(name); 73 } 74 75 ContentProviderRecord getProviderByClass(ComponentName name) { 76 return getProviderByClass(name, -1); 77 } 78 79 ContentProviderRecord getProviderByClass(ComponentName name, int userId) { 80 if (DBG) { 81 Slog.i(TAG, "getProviderByClass: " + name + ", callingUid = " + Binder.getCallingUid()); 82 } 83 // Try to find it in the global list 84 ContentProviderRecord record = mSingletonByClass.get(name); 85 if (record != null) { 86 return record; 87 } 88 89 // Check the current user's list 90 return getProvidersByClass(userId).get(name); 91 } 92 93 void putProviderByName(String name, ContentProviderRecord record) { 94 if (DBG) { 95 Slog.i(TAG, "putProviderByName: " + name + " , callingUid = " + Binder.getCallingUid() 96 + ", record uid = " + record.appInfo.uid); 97 } 98 if (record.singleton) { 99 mSingletonByName.put(name, record); 100 } else { 101 final int userId = UserId.getUserId(record.appInfo.uid); 102 getProvidersByName(userId).put(name, record); 103 } 104 } 105 106 void putProviderByClass(ComponentName name, ContentProviderRecord record) { 107 if (DBG) { 108 Slog.i(TAG, "putProviderByClass: " + name + " , callingUid = " + Binder.getCallingUid() 109 + ", record uid = " + record.appInfo.uid); 110 } 111 if (record.singleton) { 112 mSingletonByClass.put(name, record); 113 } else { 114 final int userId = UserId.getUserId(record.appInfo.uid); 115 getProvidersByClass(userId).put(name, record); 116 } 117 } 118 119 void removeProviderByName(String name, int optionalUserId) { 120 if (mSingletonByName.containsKey(name)) { 121 if (DBG) 122 Slog.i(TAG, "Removing from globalByName name=" + name); 123 mSingletonByName.remove(name); 124 } else { 125 // TODO: Verify this works, i.e., the caller happens to be from the correct user 126 if (DBG) 127 Slog.i(TAG, 128 "Removing from providersByName name=" + name + " user=" 129 + (optionalUserId == -1 ? Binder.getOrigCallingUser() : optionalUserId)); 130 getProvidersByName(optionalUserId).remove(name); 131 } 132 } 133 134 void removeProviderByClass(ComponentName name, int optionalUserId) { 135 if (mSingletonByClass.containsKey(name)) { 136 if (DBG) 137 Slog.i(TAG, "Removing from globalByClass name=" + name); 138 mSingletonByClass.remove(name); 139 } else { 140 if (DBG) 141 Slog.i(TAG, 142 "Removing from providersByClass name=" + name + " user=" 143 + (optionalUserId == -1 ? Binder.getOrigCallingUser() : optionalUserId)); 144 getProvidersByClass(optionalUserId).remove(name); 145 } 146 } 147 148 private HashMap<String, ContentProviderRecord> getProvidersByName(int optionalUserId) { 149 final int userId = optionalUserId >= 0 150 ? optionalUserId : Binder.getOrigCallingUser(); 151 final HashMap<String, ContentProviderRecord> map = mProvidersByNamePerUser.get(userId); 152 if (map == null) { 153 HashMap<String, ContentProviderRecord> newMap = new HashMap<String, ContentProviderRecord>(); 154 mProvidersByNamePerUser.put(userId, newMap); 155 return newMap; 156 } else { 157 return map; 158 } 159 } 160 161 HashMap<ComponentName, ContentProviderRecord> getProvidersByClass(int optionalUserId) { 162 final int userId = optionalUserId >= 0 163 ? optionalUserId : Binder.getOrigCallingUser(); 164 final HashMap<ComponentName, ContentProviderRecord> map = mProvidersByClassPerUser.get(userId); 165 if (map == null) { 166 HashMap<ComponentName, ContentProviderRecord> newMap = new HashMap<ComponentName, ContentProviderRecord>(); 167 mProvidersByClassPerUser.put(userId, newMap); 168 return newMap; 169 } else { 170 return map; 171 } 172 } 173 174 private void dumpProvidersByClassLocked(PrintWriter pw, boolean dumpAll, 175 HashMap<ComponentName, ContentProviderRecord> map) { 176 Iterator<Map.Entry<ComponentName, ContentProviderRecord>> it = map.entrySet().iterator(); 177 while (it.hasNext()) { 178 Map.Entry<ComponentName, ContentProviderRecord> e = it.next(); 179 ContentProviderRecord r = e.getValue(); 180 pw.print(" * "); 181 pw.println(r); 182 r.dump(pw, " ", dumpAll); 183 } 184 } 185 186 private void dumpProvidersByNameLocked(PrintWriter pw, 187 HashMap<String, ContentProviderRecord> map) { 188 Iterator<Map.Entry<String, ContentProviderRecord>> it = map.entrySet().iterator(); 189 while (it.hasNext()) { 190 Map.Entry<String, ContentProviderRecord> e = it.next(); 191 ContentProviderRecord r = e.getValue(); 192 pw.print(" "); 193 pw.print(e.getKey()); 194 pw.print(": "); 195 pw.println(r.toShortString()); 196 } 197 } 198 199 void dumpProvidersLocked(PrintWriter pw, boolean dumpAll) { 200 if (mSingletonByClass.size() > 0) { 201 pw.println(" Published single-user content providers (by class):"); 202 dumpProvidersByClassLocked(pw, dumpAll, mSingletonByClass); 203 } 204 205 pw.println(""); 206 for (int i = 0; i < mProvidersByClassPerUser.size(); i++) { 207 HashMap<ComponentName, ContentProviderRecord> map = mProvidersByClassPerUser.valueAt(i); 208 pw.println(""); 209 pw.println(" Published user " + mProvidersByClassPerUser.keyAt(i) 210 + " content providers (by class):"); 211 dumpProvidersByClassLocked(pw, dumpAll, map); 212 } 213 214 if (dumpAll) { 215 pw.println(""); 216 pw.println(" Single-user authority to provider mappings:"); 217 dumpProvidersByNameLocked(pw, mSingletonByName); 218 219 for (int i = 0; i < mProvidersByNamePerUser.size(); i++) { 220 pw.println(""); 221 pw.println(" User " + mProvidersByNamePerUser.keyAt(i) 222 + " authority to provider mappings:"); 223 dumpProvidersByNameLocked(pw, mProvidersByNamePerUser.valueAt(i)); 224 } 225 } 226 } 227 228 protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, 229 int opti, boolean dumpAll) { 230 ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); 231 232 if ("all".equals(name)) { 233 synchronized (this) { 234 for (ContentProviderRecord r1 : getProvidersByClass(-1).values()) { 235 providers.add(r1); 236 } 237 } 238 } else { 239 ComponentName componentName = name != null 240 ? ComponentName.unflattenFromString(name) : null; 241 int objectId = 0; 242 if (componentName == null) { 243 // Not a '/' separated full component name; maybe an object ID? 244 try { 245 objectId = Integer.parseInt(name, 16); 246 name = null; 247 componentName = null; 248 } catch (RuntimeException e) { 249 } 250 } 251 252 synchronized (this) { 253 for (ContentProviderRecord r1 : getProvidersByClass(-1).values()) { 254 if (componentName != null) { 255 if (r1.name.equals(componentName)) { 256 providers.add(r1); 257 } 258 } else if (name != null) { 259 if (r1.name.flattenToString().contains(name)) { 260 providers.add(r1); 261 } 262 } else if (System.identityHashCode(r1) == objectId) { 263 providers.add(r1); 264 } 265 } 266 } 267 } 268 269 if (providers.size() <= 0) { 270 return false; 271 } 272 273 boolean needSep = false; 274 for (int i=0; i<providers.size(); i++) { 275 if (needSep) { 276 pw.println(); 277 } 278 needSep = true; 279 dumpProvider("", fd, pw, providers.get(i), args, dumpAll); 280 } 281 return true; 282 } 283 284 /** 285 * Invokes IApplicationThread.dumpProvider() on the thread of the specified provider if 286 * there is a thread associated with the provider. 287 */ 288 private void dumpProvider(String prefix, FileDescriptor fd, PrintWriter pw, 289 final ContentProviderRecord r, String[] args, boolean dumpAll) { 290 String innerPrefix = prefix + " "; 291 synchronized (this) { 292 pw.print(prefix); pw.print("PROVIDER "); 293 pw.print(r); 294 pw.print(" pid="); 295 if (r.proc != null) pw.println(r.proc.pid); 296 else pw.println("(not running)"); 297 if (dumpAll) { 298 r.dump(pw, innerPrefix, true); 299 } 300 } 301 if (r.proc != null && r.proc.thread != null) { 302 pw.println(" Client:"); 303 pw.flush(); 304 try { 305 TransferPipe tp = new TransferPipe(); 306 try { 307 r.proc.thread.dumpProvider( 308 tp.getWriteFd().getFileDescriptor(), r.provider.asBinder(), args); 309 tp.setBufferPrefix(" "); 310 // Short timeout, since blocking here can 311 // deadlock with the application. 312 tp.go(fd, 2000); 313 } finally { 314 tp.kill(); 315 } 316 } catch (IOException ex) { 317 pw.println(" Failure while dumping the provider: " + ex); 318 } catch (RemoteException ex) { 319 pw.println(" Got a RemoteException while dumping the service"); 320 } 321 } 322 } 323} 324