Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.content.pm.ApplicationInfo; | 10 import android.content.pm.ApplicationInfo; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 // Connections to services. Indices of the array correspond to the servi ce numbers. | 60 // Connections to services. Indices of the array correspond to the servi ce numbers. |
| 61 private final ChildProcessConnection[] mChildProcessConnections; | 61 private final ChildProcessConnection[] mChildProcessConnections; |
| 62 | 62 |
| 63 // The list of free (not bound) service indices. | 63 // The list of free (not bound) service indices. |
| 64 // SHOULD BE ACCESSED WITH mConnectionLock. | 64 // SHOULD BE ACCESSED WITH mConnectionLock. |
| 65 private final ArrayList<Integer> mFreeConnectionIndices; | 65 private final ArrayList<Integer> mFreeConnectionIndices; |
| 66 private final Object mConnectionLock = new Object(); | 66 private final Object mConnectionLock = new Object(); |
| 67 | 67 |
| 68 private Class<? extends ChildProcessService> mChildClass; | 68 private Class<? extends ChildProcessService> mChildClass; |
| 69 private final boolean mInSandbox; | 69 private final boolean mInSandbox; |
| 70 // Each Allocator keeps a queue for the pending spawn data. Once a conne ction is free, we | |
| 71 // dequeue the pending spawn data from the same allocator as the connect ion. | |
| 72 private final PendingSpawnQueue mPendingSpawnQueue = new PendingSpawnQue ue(); | |
| 70 | 73 |
| 71 public ChildConnectionAllocator(boolean inSandbox, int numChildServices) { | 74 public ChildConnectionAllocator(boolean inSandbox, int numChildServices) { |
| 72 mChildProcessConnections = new ChildProcessConnectionImpl[numChildSe rvices]; | 75 mChildProcessConnections = new ChildProcessConnectionImpl[numChildSe rvices]; |
| 73 mFreeConnectionIndices = new ArrayList<Integer>(numChildServices); | 76 mFreeConnectionIndices = new ArrayList<Integer>(numChildServices); |
| 74 for (int i = 0; i < numChildServices; i++) { | 77 for (int i = 0; i < numChildServices; i++) { |
| 75 mFreeConnectionIndices.add(i); | 78 mFreeConnectionIndices.add(i); |
| 76 } | 79 } |
| 77 mChildClass = | 80 mChildClass = |
| 78 inSandbox ? SandboxedProcessService.class : PrivilegedProces sService.class; | 81 inSandbox ? SandboxedProcessService.class : PrivilegedProces sService.class; |
| 79 mInSandbox = inSandbox; | 82 mInSandbox = inSandbox; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 } | 121 } |
| 119 } | 122 } |
| 120 } | 123 } |
| 121 | 124 |
| 122 public boolean isFreeConnectionAvailable() { | 125 public boolean isFreeConnectionAvailable() { |
| 123 synchronized (mConnectionLock) { | 126 synchronized (mConnectionLock) { |
| 124 return !mFreeConnectionIndices.isEmpty(); | 127 return !mFreeConnectionIndices.isEmpty(); |
| 125 } | 128 } |
| 126 } | 129 } |
| 127 | 130 |
| 131 public PendingSpawnQueue getPendingSpawnQueue() { | |
| 132 return mPendingSpawnQueue; | |
| 133 } | |
| 134 | |
| 128 /** @return the count of connections managed by the allocator */ | 135 /** @return the count of connections managed by the allocator */ |
| 129 @VisibleForTesting | 136 @VisibleForTesting |
| 130 int allocatedConnectionsCountForTesting() { | 137 int allocatedConnectionsCountForTesting() { |
| 131 return mChildProcessConnections.length - mFreeConnectionIndices.size (); | 138 return mChildProcessConnections.length - mFreeConnectionIndices.size (); |
| 132 } | 139 } |
| 133 } | 140 } |
| 134 | 141 |
| 135 private static class PendingSpawnData { | 142 private static class PendingSpawnData { |
| 136 private final Context mContext; | 143 private final Context mContext; |
| 137 private final String[] mCommandLine; | 144 private final String[] mCommandLine; |
| 138 private final int mChildProcessId; | 145 private final int mChildProcessId; |
| 139 private final FileDescriptorInfo[] mFilesToBeMapped; | 146 private final FileDescriptorInfo[] mFilesToBeMapped; |
| 140 private final long mClientContext; | 147 private final long mClientContext; |
| 141 private final int mCallbackType; | 148 private final int mCallbackType; |
| 142 private final boolean mInSandbox; | 149 private final boolean mInSandbox; |
| 150 private final ChildProcessCreationParams mCreationParams; | |
| 143 | 151 |
| 144 private PendingSpawnData( | 152 private PendingSpawnData( |
| 145 Context context, | 153 Context context, |
| 146 String[] commandLine, | 154 String[] commandLine, |
| 147 int childProcessId, | 155 int childProcessId, |
| 148 FileDescriptorInfo[] filesToBeMapped, | 156 FileDescriptorInfo[] filesToBeMapped, |
| 149 long clientContext, | 157 long clientContext, |
| 150 int callbackType, | 158 int callbackType, |
| 151 boolean inSandbox) { | 159 boolean inSandbox, |
| 160 ChildProcessCreationParams creationParams) { | |
| 152 mContext = context; | 161 mContext = context; |
| 153 mCommandLine = commandLine; | 162 mCommandLine = commandLine; |
| 154 mChildProcessId = childProcessId; | 163 mChildProcessId = childProcessId; |
| 155 mFilesToBeMapped = filesToBeMapped; | 164 mFilesToBeMapped = filesToBeMapped; |
| 156 mClientContext = clientContext; | 165 mClientContext = clientContext; |
| 157 mCallbackType = callbackType; | 166 mCallbackType = callbackType; |
| 158 mInSandbox = inSandbox; | 167 mInSandbox = inSandbox; |
| 168 mCreationParams = creationParams; | |
| 159 } | 169 } |
| 160 | 170 |
| 161 private Context context() { | 171 private Context context() { |
| 162 return mContext; | 172 return mContext; |
| 163 } | 173 } |
| 164 private String[] commandLine() { | 174 private String[] commandLine() { |
| 165 return mCommandLine; | 175 return mCommandLine; |
| 166 } | 176 } |
| 167 private int childProcessId() { | 177 private int childProcessId() { |
| 168 return mChildProcessId; | 178 return mChildProcessId; |
| 169 } | 179 } |
| 170 private FileDescriptorInfo[] filesToBeMapped() { | 180 private FileDescriptorInfo[] filesToBeMapped() { |
| 171 return mFilesToBeMapped; | 181 return mFilesToBeMapped; |
| 172 } | 182 } |
| 173 private long clientContext() { | 183 private long clientContext() { |
| 174 return mClientContext; | 184 return mClientContext; |
| 175 } | 185 } |
| 176 private int callbackType() { | 186 private int callbackType() { |
| 177 return mCallbackType; | 187 return mCallbackType; |
| 178 } | 188 } |
| 179 private boolean inSandbox() { | 189 private boolean inSandbox() { |
| 180 return mInSandbox; | 190 return mInSandbox; |
| 181 } | 191 } |
| 192 private ChildProcessCreationParams getCreationParams() { | |
| 193 return mCreationParams; | |
| 194 } | |
| 182 } | 195 } |
| 183 | 196 |
| 184 private static class PendingSpawnQueue { | 197 private static class PendingSpawnQueue { |
| 185 // The list of pending process spawn requests and its lock. | 198 // The list of pending process spawn requests and its lock. |
| 186 // Operations on this queue must be atomical w.r.t. free connections upd ates. | 199 // Operations on this queue must be atomical w.r.t. free connections upd ates. |
| 187 private static Queue<PendingSpawnData> sPendingSpawns = | 200 private Queue<PendingSpawnData> mPendingSpawns = new LinkedList<PendingS pawnData>(); |
| 188 new LinkedList<PendingSpawnData>(); | 201 final Object mPendingSpawnsLock = new Object(); |
| 189 static final Object sPendingSpawnsLock = new Object(); | |
| 190 | 202 |
| 191 /** | 203 /** |
| 192 * Queue up a spawn requests to be processed once a free service is avai lable. | 204 * Queue up a spawn requests to be processed once a free service is avai lable. |
| 193 * Called when a spawn is requested while we are at the capacity. | 205 * Called when a spawn is requested while we are at the capacity. |
| 194 */ | 206 */ |
| 195 public void enqueueLocked(final PendingSpawnData pendingSpawn) { | 207 public void enqueueLocked(final PendingSpawnData pendingSpawn) { |
| 196 assert Thread.holdsLock(sPendingSpawnsLock); | 208 assert Thread.holdsLock(mPendingSpawnsLock); |
| 197 sPendingSpawns.add(pendingSpawn); | 209 mPendingSpawns.add(pendingSpawn); |
| 198 } | 210 } |
| 199 | 211 |
| 200 /** | 212 /** |
| 201 * Pop the next request from the queue. Called when a free service is av ailable. | 213 * Pop the next request from the queue. Called when a free service is av ailable. |
| 202 * @return the next spawn request waiting in the queue. | 214 * @return the next spawn request waiting in the queue. |
| 203 */ | 215 */ |
| 204 public PendingSpawnData dequeueLocked() { | 216 public PendingSpawnData dequeueLocked() { |
| 205 assert Thread.holdsLock(sPendingSpawnsLock); | 217 assert Thread.holdsLock(mPendingSpawnsLock); |
| 206 return sPendingSpawns.poll(); | 218 return mPendingSpawns.poll(); |
| 207 } | 219 } |
| 208 | 220 |
| 209 /** @return the count of pending spawns in the queue */ | 221 /** @return the count of pending spawns in the queue */ |
| 210 public int sizeLocked() { | 222 public int sizeLocked() { |
| 211 assert Thread.holdsLock(sPendingSpawnsLock); | 223 assert Thread.holdsLock(mPendingSpawnsLock); |
| 212 return sPendingSpawns.size(); | 224 return mPendingSpawns.size(); |
| 213 } | 225 } |
| 214 } | 226 } |
| 215 | 227 |
| 216 private static final PendingSpawnQueue sPendingSpawnQueue = new PendingSpawn Queue(); | 228 // Service class for child process. |
| 217 | 229 // Map from package name to ChildConnectionAllocator. |
| 218 // Service class for child process. As the default value it uses SandboxedPr ocessService0 and | 230 private static Map<String, ChildConnectionAllocator> sSandboxedChildConnecti onAllocatorMap; |
| 219 // PrivilegedProcessService0. | 231 // As the default value it uses PrivilegedProcessService0. |
| 220 private static ChildConnectionAllocator sSandboxedChildConnectionAllocator; | |
| 221 private static ChildConnectionAllocator sPrivilegedChildConnectionAllocator; | 232 private static ChildConnectionAllocator sPrivilegedChildConnectionAllocator; |
| 222 | 233 |
| 223 private static final String NUM_SANDBOXED_SERVICES_KEY = | 234 private static final String NUM_SANDBOXED_SERVICES_KEY = |
| 224 "org.chromium.content.browser.NUM_SANDBOXED_SERVICES"; | 235 "org.chromium.content.browser.NUM_SANDBOXED_SERVICES"; |
| 225 private static final String NUM_PRIVILEGED_SERVICES_KEY = | 236 private static final String NUM_PRIVILEGED_SERVICES_KEY = |
| 226 "org.chromium.content.browser.NUM_PRIVILEGED_SERVICES"; | 237 "org.chromium.content.browser.NUM_PRIVILEGED_SERVICES"; |
| 227 // Overrides the number of available sandboxed services. | 238 // Overrides the number of available sandboxed services. |
| 228 @VisibleForTesting | 239 @VisibleForTesting |
| 229 public static final String SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING = "num- sandboxed-services"; | 240 public static final String SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING = "num- sandboxed-services"; |
| 230 | 241 |
| 231 private static int getNumberOfServices(Context context, boolean inSandbox) { | 242 private static int getNumberOfServices(Context context, boolean inSandbox, S tring packageName) { |
| 232 try { | 243 try { |
| 233 PackageManager packageManager = context.getPackageManager(); | 244 PackageManager packageManager = context.getPackageManager(); |
| 234 ChildProcessCreationParams childProcessCreationParams = | |
| 235 ChildProcessCreationParams.get(); | |
| 236 final String packageName = childProcessCreationParams != null | |
| 237 ? childProcessCreationParams.getPackageName() : context.getP ackageName(); | |
| 238 ApplicationInfo appInfo = packageManager.getApplicationInfo(packageN ame, | 245 ApplicationInfo appInfo = packageManager.getApplicationInfo(packageN ame, |
| 239 PackageManager.GET_META_DATA); | 246 PackageManager.GET_META_DATA); |
| 240 int numServices = appInfo.metaData.getInt(inSandbox ? NUM_SANDBOXED_ SERVICES_KEY | 247 int numServices = -1; |
| 241 : NUM_PRIVILEGED_SERVICES_KEY, -1); | 248 if (appInfo.metaData != null) { |
| 249 numServices = appInfo.metaData.getInt( | |
| 250 inSandbox ? NUM_SANDBOXED_SERVICES_KEY : NUM_PRIVILEGED_ SERVICES_KEY, -1); | |
| 251 } | |
| 242 if (inSandbox | 252 if (inSandbox |
| 243 && CommandLine.getInstance().hasSwitch( | 253 && CommandLine.getInstance().hasSwitch( |
| 244 SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING)) { | 254 SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING)) { |
| 245 String value = CommandLine.getInstance().getSwitchValue( | 255 String value = CommandLine.getInstance().getSwitchValue( |
| 246 SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING); | 256 SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING); |
| 247 if (!TextUtils.isEmpty(value)) { | 257 if (!TextUtils.isEmpty(value)) { |
| 248 try { | 258 try { |
| 249 numServices = Integer.parseInt(value); | 259 numServices = Integer.parseInt(value); |
| 250 } catch (NumberFormatException e) { | 260 } catch (NumberFormatException e) { |
| 251 Log.w(TAG, "The value of --num-sandboxed-services is for matted wrongly: " | 261 Log.w(TAG, "The value of --num-sandboxed-services is for matted wrongly: " |
| 252 + value); | 262 + value); |
| 253 } | 263 } |
| 254 } | 264 } |
| 255 } | 265 } |
| 256 if (numServices < 0) { | 266 if (numServices < 0) { |
| 257 throw new RuntimeException("Illegal meta data value for number o f child services"); | 267 throw new RuntimeException("Illegal meta data value for number o f child services"); |
| 258 } | 268 } |
| 259 return numServices; | 269 return numServices; |
| 260 } catch (PackageManager.NameNotFoundException e) { | 270 } catch (PackageManager.NameNotFoundException e) { |
| 261 throw new RuntimeException("Could not get application info"); | 271 throw new RuntimeException("Could not get application info"); |
| 262 } | 272 } |
| 263 } | 273 } |
| 264 | 274 |
| 265 private static void initConnectionAllocatorsIfNecessary(Context context) { | 275 private static void initConnectionAllocatorsIfNecessary( |
| 276 Context context, boolean inSandbox, String packageName) { | |
| 266 synchronized (ChildProcessLauncher.class) { | 277 synchronized (ChildProcessLauncher.class) { |
|
Maria
2016/06/08 18:25:08
Please add a TODO with my name here to move to usi
Xi Han
2016/06/08 18:42:10
Done.
| |
| 267 if (sSandboxedChildConnectionAllocator == null) { | 278 if (inSandbox) { |
| 268 sSandboxedChildConnectionAllocator = | 279 if (sSandboxedChildConnectionAllocatorMap == null) { |
| 269 new ChildConnectionAllocator(true, getNumberOfServices(c ontext, true)); | 280 sSandboxedChildConnectionAllocatorMap = |
| 281 new ConcurrentHashMap<String, ChildConnectionAllocat or>(); | |
| 282 } | |
| 283 if (!sSandboxedChildConnectionAllocatorMap.containsKey(packageNa me)) { | |
| 284 Log.w(TAG, "Create a new ChildConnectionAllocator with packa ge name = %s," | |
| 285 + " inSandbox = true", | |
| 286 packageName); | |
| 287 sSandboxedChildConnectionAllocatorMap.put(packageName, | |
| 288 new ChildConnectionAllocator(true, | |
| 289 getNumberOfServices(context, true, packageNa me))); | |
| 290 } | |
| 291 } else if (sPrivilegedChildConnectionAllocator == null) { | |
| 292 sPrivilegedChildConnectionAllocator = new ChildConnectionAllocat or( | |
| 293 false, getNumberOfServices(context, false, packageName)) ; | |
| 270 } | 294 } |
| 271 if (sPrivilegedChildConnectionAllocator == null) { | 295 // TODO(pkotwicz|hanxi): Figure out when old allocators should be re moved from |
| 272 sPrivilegedChildConnectionAllocator = | 296 // {@code sSandboxedChildConnectionAllocatorMap}. |
| 273 new ChildConnectionAllocator(false, getNumberOfServices( context, false)); | |
| 274 } | |
| 275 } | 297 } |
| 276 } | 298 } |
| 277 | 299 |
| 278 private static ChildConnectionAllocator getConnectionAllocator(boolean inSan dbox) { | 300 /** |
| 279 return inSandbox | 301 * Note: please make sure that the Allocator has been initialized before cal ling this function. |
| 280 ? sSandboxedChildConnectionAllocator : sPrivilegedChildConnectio nAllocator; | 302 * Otherwise, always calls {@link initConnectionAllocatorsIfNecessary} first . |
| 303 */ | |
| 304 private static ChildConnectionAllocator getConnectionAllocator( | |
| 305 String packageName, boolean inSandbox) { | |
| 306 if (!inSandbox) { | |
| 307 return sPrivilegedChildConnectionAllocator; | |
| 308 } | |
| 309 return sSandboxedChildConnectionAllocatorMap.get(packageName); | |
| 310 } | |
| 311 | |
| 312 /** | |
| 313 * Get the PendingSpawnQueue of the Allocator. Initialize the Allocator if n eeded. | |
| 314 */ | |
| 315 private static PendingSpawnQueue getPendingSpawnQueue(Context context, Strin g packageName, | |
| 316 boolean inSandbox) { | |
| 317 initConnectionAllocatorsIfNecessary(context, inSandbox, packageName); | |
| 318 return getConnectionAllocator(packageName, inSandbox).getPendingSpawnQue ue(); | |
| 281 } | 319 } |
| 282 | 320 |
| 283 private static ChildProcessConnection allocateConnection(Context context, bo olean inSandbox, | 321 private static ChildProcessConnection allocateConnection(Context context, bo olean inSandbox, |
| 284 ChromiumLinkerParams chromiumLinkerParams, boolean alwaysInForegroun d) { | 322 ChromiumLinkerParams chromiumLinkerParams, boolean alwaysInForegroun d, |
| 323 ChildProcessCreationParams creationParams) { | |
| 285 ChildProcessConnection.DeathCallback deathCallback = | 324 ChildProcessConnection.DeathCallback deathCallback = |
| 286 new ChildProcessConnection.DeathCallback() { | 325 new ChildProcessConnection.DeathCallback() { |
| 287 @Override | 326 @Override |
| 288 public void onChildProcessDied(ChildProcessConnection connec tion) { | 327 public void onChildProcessDied(ChildProcessConnection connec tion) { |
| 289 if (connection.getPid() != 0) { | 328 if (connection.getPid() != 0) { |
| 290 stop(connection.getPid()); | 329 stop(connection.getPid()); |
| 291 } else { | 330 } else { |
| 292 freeConnection(connection); | 331 freeConnection(connection); |
| 293 } | 332 } |
| 294 } | 333 } |
| 295 }; | 334 }; |
| 296 initConnectionAllocatorsIfNecessary(context); | 335 String packageName = creationParams != null ? creationParams.getPackageN ame() |
| 297 return getConnectionAllocator(inSandbox).allocate(context, deathCallback , | 336 : context.getPackageName(); |
| 298 chromiumLinkerParams, alwaysInForeground, ChildProcessCreationPa rams.get()); | 337 initConnectionAllocatorsIfNecessary(context, inSandbox, packageName); |
| 338 return getConnectionAllocator(packageName, inSandbox) | |
| 339 .allocate(context, deathCallback, chromiumLinkerParams, alwaysIn Foreground, | |
| 340 creationParams); | |
| 299 } | 341 } |
| 300 | 342 |
| 301 private static boolean sLinkerInitialized = false; | 343 private static boolean sLinkerInitialized = false; |
| 302 private static long sLinkerLoadAddress = 0; | 344 private static long sLinkerLoadAddress = 0; |
| 303 | 345 |
| 304 private static ChromiumLinkerParams getLinkerParamsForNewConnection() { | 346 private static ChromiumLinkerParams getLinkerParamsForNewConnection() { |
| 305 if (!sLinkerInitialized) { | 347 if (!sLinkerInitialized) { |
| 306 if (Linker.isUsed()) { | 348 if (Linker.isUsed()) { |
| 307 sLinkerLoadAddress = Linker.getInstance().getBaseLoadAddress(); | 349 sLinkerLoadAddress = Linker.getInstance().getBaseLoadAddress(); |
| 308 if (sLinkerLoadAddress == 0) { | 350 if (sLinkerLoadAddress == 0) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 322 waitForSharedRelros, | 364 waitForSharedRelros, |
| 323 linker.getTestRunnerClassNameForTest ing(), | 365 linker.getTestRunnerClassNameForTest ing(), |
| 324 linker.getImplementationForTesting() ); | 366 linker.getImplementationForTesting() ); |
| 325 } else { | 367 } else { |
| 326 return new ChromiumLinkerParams(sLinkerLoadAddress, | 368 return new ChromiumLinkerParams(sLinkerLoadAddress, |
| 327 waitForSharedRelros); | 369 waitForSharedRelros); |
| 328 } | 370 } |
| 329 } | 371 } |
| 330 | 372 |
| 331 private static ChildProcessConnection allocateBoundConnection(Context contex t, | 373 private static ChildProcessConnection allocateBoundConnection(Context contex t, |
| 332 String[] commandLine, boolean inSandbox, boolean alwaysInForeground) { | 374 String[] commandLine, boolean inSandbox, boolean alwaysInForeground, |
| 375 ChildProcessCreationParams creationParams) { | |
| 333 ChromiumLinkerParams chromiumLinkerParams = getLinkerParamsForNewConnect ion(); | 376 ChromiumLinkerParams chromiumLinkerParams = getLinkerParamsForNewConnect ion(); |
| 334 ChildProcessConnection connection = allocateConnection(context, inSandbo x, | 377 ChildProcessConnection connection = allocateConnection( |
| 335 chromiumLinkerParams, alwaysInForeground); | 378 context, inSandbox, chromiumLinkerParams, alwaysInForeground, cr eationParams); |
| 336 if (connection != null) { | 379 if (connection != null) { |
| 337 connection.start(commandLine); | 380 connection.start(commandLine); |
| 338 | 381 |
| 339 if (inSandbox && !sSandboxedChildConnectionAllocator.isFreeConnectio nAvailable()) { | 382 String packageName = creationParams != null ? creationParams.getPack ageName() |
| 383 : context.getPackageName(); | |
| 384 if (inSandbox && !getConnectionAllocator(packageName, inSandbox) | |
| 385 .isFreeConnectionAvailable()) { | |
| 340 // Proactively releases all the moderate bindings once all the s andboxed services | 386 // Proactively releases all the moderate bindings once all the s andboxed services |
| 341 // are allocated, which will be very likely to have some of them killed by OOM | 387 // are allocated, which will be very likely to have some of them killed by OOM |
| 342 // killer. | 388 // killer. |
| 343 sBindingManager.releaseAllModerateBindings(); | 389 sBindingManager.releaseAllModerateBindings(); |
| 344 } | 390 } |
| 345 } | 391 } |
| 346 return connection; | 392 return connection; |
| 347 } | 393 } |
| 348 | 394 |
| 349 private static final long FREE_CONNECTION_DELAY_MILLIS = 1; | 395 private static final long FREE_CONNECTION_DELAY_MILLIS = 1; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 363 @Override | 409 @Override |
| 364 public void run() { | 410 public void run() { |
| 365 final PendingSpawnData pendingSpawn = freeConnectionAndDequeuePe nding(conn); | 411 final PendingSpawnData pendingSpawn = freeConnectionAndDequeuePe nding(conn); |
| 366 if (pendingSpawn != null) { | 412 if (pendingSpawn != null) { |
| 367 new Thread(new Runnable() { | 413 new Thread(new Runnable() { |
| 368 @Override | 414 @Override |
| 369 public void run() { | 415 public void run() { |
| 370 startInternal(pendingSpawn.context(), pendingSpawn.c ommandLine(), | 416 startInternal(pendingSpawn.context(), pendingSpawn.c ommandLine(), |
| 371 pendingSpawn.childProcessId(), pendingSpawn. filesToBeMapped(), | 417 pendingSpawn.childProcessId(), pendingSpawn. filesToBeMapped(), |
| 372 pendingSpawn.clientContext(), pendingSpawn.c allbackType(), | 418 pendingSpawn.clientContext(), pendingSpawn.c allbackType(), |
| 373 pendingSpawn.inSandbox()); | 419 pendingSpawn.inSandbox(), pendingSpawn.getCr eationParams()); |
| 374 } | 420 } |
| 375 }).start(); | 421 }).start(); |
| 376 } | 422 } |
| 377 } | 423 } |
| 378 }, FREE_CONNECTION_DELAY_MILLIS); | 424 }, FREE_CONNECTION_DELAY_MILLIS); |
| 379 } | 425 } |
| 380 | 426 |
| 381 private static PendingSpawnData freeConnectionAndDequeuePending(ChildProcess Connection conn) { | 427 private static PendingSpawnData freeConnectionAndDequeuePending(ChildProcess Connection conn) { |
| 382 synchronized (PendingSpawnQueue.sPendingSpawnsLock) { | 428 ChildConnectionAllocator allocator = getConnectionAllocator( |
| 383 getConnectionAllocator(conn.isInSandbox()).free(conn); | 429 conn.getPackageName(), conn.isInSandbox()); |
| 384 return sPendingSpawnQueue.dequeueLocked(); | 430 assert allocator != null; |
| 431 PendingSpawnQueue pendingSpawnQueue = allocator.getPendingSpawnQueue(); | |
| 432 synchronized (pendingSpawnQueue.mPendingSpawnsLock) { | |
| 433 allocator.free(conn); | |
| 434 return pendingSpawnQueue.dequeueLocked(); | |
| 385 } | 435 } |
| 386 } | 436 } |
| 387 | 437 |
| 388 // Represents an invalid process handle; same as base/process/process.h kNul lProcessHandle. | 438 // Represents an invalid process handle; same as base/process/process.h kNul lProcessHandle. |
| 389 private static final int NULL_PROCESS_HANDLE = 0; | 439 private static final int NULL_PROCESS_HANDLE = 0; |
| 390 | 440 |
| 391 // Map from pid to ChildService connection. | 441 // Map from pid to ChildService connection. |
| 392 private static Map<Integer, ChildProcessConnection> sServiceMap = | 442 private static Map<Integer, ChildProcessConnection> sServiceMap = |
| 393 new ConcurrentHashMap<Integer, ChildProcessConnection>(); | 443 new ConcurrentHashMap<Integer, ChildProcessConnection>(); |
| 394 | 444 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 /** | 556 /** |
| 507 * Called when the embedding application is sent to background. | 557 * Called when the embedding application is sent to background. |
| 508 */ | 558 */ |
| 509 public static void onSentToBackground() { | 559 public static void onSentToBackground() { |
| 510 sApplicationInForeground = false; | 560 sApplicationInForeground = false; |
| 511 sBindingManager.onSentToBackground(); | 561 sBindingManager.onSentToBackground(); |
| 512 } | 562 } |
| 513 | 563 |
| 514 /** | 564 /** |
| 515 * Starts moderate binding management. | 565 * Starts moderate binding management. |
| 566 * Note: WebAPKs and non WebAPKs share the same moderate binding pool, so th e size of the | |
| 567 * shared moderate binding pool is always set based on the number of sandbox es processes | |
| 568 * used by Chrome. | |
| 516 * @param context Android's context. | 569 * @param context Android's context. |
| 517 * @param moderateBindingTillBackgrounded true if the BindingManager should add a moderate | 570 * @param moderateBindingTillBackgrounded true if the BindingManager should add a moderate |
| 518 * binding to a render process when it is created and remove the moderate bi nding when Chrome is | 571 * binding to a render process when it is created and remove the moderate bi nding when Chrome is |
| 519 * sent to the background. | 572 * sent to the background. |
| 520 */ | 573 */ |
| 521 public static void startModerateBindingManagement( | 574 public static void startModerateBindingManagement( |
| 522 Context context, boolean moderateBindingTillBackgrounded) { | 575 Context context, boolean moderateBindingTillBackgrounded) { |
| 523 sBindingManager.startModerateBindingManagement( | 576 sBindingManager.startModerateBindingManagement(context, |
| 524 context, getNumberOfServices(context, true), moderateBindingTill Backgrounded); | 577 getNumberOfServices(context, true, context.getPackageName()), |
| 578 moderateBindingTillBackgrounded); | |
| 525 } | 579 } |
| 526 | 580 |
| 527 /** | 581 /** |
| 528 * Called when the embedding application is brought to foreground. | 582 * Called when the embedding application is brought to foreground. |
| 529 */ | 583 */ |
| 530 public static void onBroughtToForeground() { | 584 public static void onBroughtToForeground() { |
| 531 sApplicationInForeground = true; | 585 sApplicationInForeground = true; |
| 532 sBindingManager.onBroughtToForeground(); | 586 sBindingManager.onBroughtToForeground(); |
| 533 } | 587 } |
| 534 | 588 |
| 535 /** | 589 /** |
| 536 * Returns whether the application is currently in the foreground. | 590 * Returns whether the application is currently in the foreground. |
| 537 */ | 591 */ |
| 538 static boolean isApplicationInForeground() { | 592 static boolean isApplicationInForeground() { |
| 539 return sApplicationInForeground; | 593 return sApplicationInForeground; |
| 540 } | 594 } |
| 541 | 595 |
| 542 /** | 596 /** |
| 543 * Should be called early in startup so the work needed to spawn the child p rocess can be done | 597 * Should be called early in startup so the work needed to spawn the child p rocess can be done |
| 544 * in parallel to other startup work. Must not be called on the UI thread. S pare connection is | 598 * in parallel to other startup work. Must not be called on the UI thread. S pare connection is |
| 545 * created in sandboxed child process. | 599 * created in sandboxed child process. |
| 546 * @param context the application context used for the connection. | 600 * @param context the application context used for the connection. |
| 547 * @param params child process creation params. | |
| 548 */ | 601 */ |
| 549 public static void warmUp(Context context, ChildProcessCreationParams params ) { | 602 public static void warmUp(Context context) { |
| 550 ChildProcessCreationParams.set(params); | |
| 551 synchronized (ChildProcessLauncher.class) { | 603 synchronized (ChildProcessLauncher.class) { |
| 552 assert !ThreadUtils.runningOnUiThread(); | 604 assert !ThreadUtils.runningOnUiThread(); |
| 553 if (sSpareSandboxedConnection == null) { | 605 if (sSpareSandboxedConnection == null) { |
| 554 sSpareSandboxedConnection = allocateBoundConnection(context, nul l, true, false); | 606 ChildProcessCreationParams params = ChildProcessCreationParams.g et(); |
| 607 if (params != null) { | |
| 608 params = params.copy(); | |
| 609 } | |
| 610 sSpareSandboxedConnection = allocateBoundConnection(context, nul l, true, false, | |
| 611 params); | |
| 555 } | 612 } |
| 556 } | 613 } |
| 557 } | 614 } |
| 558 | 615 |
| 559 @CalledByNative | 616 @CalledByNative |
| 560 private static FileDescriptorInfo makeFdInfo( | 617 private static FileDescriptorInfo makeFdInfo( |
| 561 int id, int fd, boolean autoClose, long offset, long size) { | 618 int id, int fd, boolean autoClose, long offset, long size) { |
| 562 ParcelFileDescriptor pFd; | 619 ParcelFileDescriptor pFd; |
| 563 if (autoClose) { | 620 if (autoClose) { |
| 564 // Adopt the FD, it will be closed when we close the ParcelFileDescr iptor. | 621 // Adopt the FD, it will be closed when we close the ParcelFileDescr iptor. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 587 */ | 644 */ |
| 588 @CalledByNative | 645 @CalledByNative |
| 589 private static void start(Context context, final String[] commandLine, int c hildProcessId, | 646 private static void start(Context context, final String[] commandLine, int c hildProcessId, |
| 590 FileDescriptorInfo[] filesToBeMapped, long clientContext) { | 647 FileDescriptorInfo[] filesToBeMapped, long clientContext) { |
| 591 assert clientContext != 0; | 648 assert clientContext != 0; |
| 592 | 649 |
| 593 int callbackType = CALLBACK_FOR_UNKNOWN_PROCESS; | 650 int callbackType = CALLBACK_FOR_UNKNOWN_PROCESS; |
| 594 boolean inSandbox = true; | 651 boolean inSandbox = true; |
| 595 String processType = | 652 String processType = |
| 596 ContentSwitches.getSwitchValue(commandLine, ContentSwitches.SWIT CH_PROCESS_TYPE); | 653 ContentSwitches.getSwitchValue(commandLine, ContentSwitches.SWIT CH_PROCESS_TYPE); |
| 654 ChildProcessCreationParams params = null; | |
| 597 if (ContentSwitches.SWITCH_RENDERER_PROCESS.equals(processType)) { | 655 if (ContentSwitches.SWITCH_RENDERER_PROCESS.equals(processType)) { |
| 598 callbackType = CALLBACK_FOR_RENDERER_PROCESS; | 656 callbackType = CALLBACK_FOR_RENDERER_PROCESS; |
| 657 if (ChildProcessCreationParams.get() != null) { | |
| 658 params = ChildProcessCreationParams.get().copy(); | |
| 659 } | |
| 599 } else if (ContentSwitches.SWITCH_GPU_PROCESS.equals(processType)) { | 660 } else if (ContentSwitches.SWITCH_GPU_PROCESS.equals(processType)) { |
| 600 callbackType = CALLBACK_FOR_GPU_PROCESS; | 661 callbackType = CALLBACK_FOR_GPU_PROCESS; |
| 601 inSandbox = false; | 662 inSandbox = false; |
| 602 } else if (ContentSwitches.SWITCH_UTILITY_PROCESS.equals(processType)) { | 663 } else if (ContentSwitches.SWITCH_UTILITY_PROCESS.equals(processType)) { |
| 603 // We only support sandboxed right now. | 664 // We only support sandboxed right now. |
| 604 callbackType = CALLBACK_FOR_UTILITY_PROCESS; | 665 callbackType = CALLBACK_FOR_UTILITY_PROCESS; |
| 605 } else { | 666 } else { |
| 606 assert false; | 667 assert false; |
| 607 } | 668 } |
| 608 | 669 |
| 609 startInternal(context, commandLine, childProcessId, filesToBeMapped, cli entContext, | 670 startInternal(context, commandLine, childProcessId, filesToBeMapped, cli entContext, |
| 610 callbackType, inSandbox); | 671 callbackType, inSandbox, params); |
| 611 } | 672 } |
| 612 | 673 |
| 613 /** | 674 /** |
| 614 * Spawns a background download process if it hasn't been started. The downl oad process will | 675 * Spawns a background download process if it hasn't been started. The downl oad process will |
| 615 * manage its own lifecyle and can outlive chrome. | 676 * manage its own lifecyle and can outlive chrome. |
| 616 * | 677 * |
| 617 * @param context Context used to obtain the application context. | 678 * @param context Context used to obtain the application context. |
| 618 * @param commandLine The child process command line argv. | 679 * @param commandLine The child process command line argv. |
| 619 */ | 680 */ |
| 620 @SuppressLint("NewApi") | 681 @SuppressLint("NewApi") |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 643 context.startService(intent); | 704 context.startService(intent); |
| 644 } | 705 } |
| 645 | 706 |
| 646 private static void startInternal( | 707 private static void startInternal( |
| 647 Context context, | 708 Context context, |
| 648 final String[] commandLine, | 709 final String[] commandLine, |
| 649 int childProcessId, | 710 int childProcessId, |
| 650 FileDescriptorInfo[] filesToBeMapped, | 711 FileDescriptorInfo[] filesToBeMapped, |
| 651 long clientContext, | 712 long clientContext, |
| 652 int callbackType, | 713 int callbackType, |
| 653 boolean inSandbox) { | 714 boolean inSandbox, |
| 715 ChildProcessCreationParams creationParams) { | |
| 654 try { | 716 try { |
| 655 TraceEvent.begin("ChildProcessLauncher.startInternal"); | 717 TraceEvent.begin("ChildProcessLauncher.startInternal"); |
| 656 | 718 |
| 657 ChildProcessConnection allocatedConnection = null; | 719 ChildProcessConnection allocatedConnection = null; |
| 720 String packageName = creationParams != null ? creationParams.getPack ageName() | |
| 721 : context.getPackageName(); | |
| 658 synchronized (ChildProcessLauncher.class) { | 722 synchronized (ChildProcessLauncher.class) { |
| 659 if (inSandbox) { | 723 if (inSandbox && sSpareSandboxedConnection != null |
| 724 && sSpareSandboxedConnection.getPackageName().equals(pac kageName)) { | |
| 660 allocatedConnection = sSpareSandboxedConnection; | 725 allocatedConnection = sSpareSandboxedConnection; |
| 661 sSpareSandboxedConnection = null; | 726 sSpareSandboxedConnection = null; |
| 662 } | 727 } |
| 663 } | 728 } |
| 664 if (allocatedConnection == null) { | 729 if (allocatedConnection == null) { |
| 665 boolean alwaysInForeground = false; | 730 boolean alwaysInForeground = false; |
| 666 if (callbackType == CALLBACK_FOR_GPU_PROCESS) alwaysInForeground = true; | 731 if (callbackType == CALLBACK_FOR_GPU_PROCESS) alwaysInForeground = true; |
| 667 synchronized (PendingSpawnQueue.sPendingSpawnsLock) { | 732 PendingSpawnQueue pendingSpawnQueue = getPendingSpawnQueue( |
| 733 context, packageName, inSandbox); | |
| 734 synchronized (pendingSpawnQueue.mPendingSpawnsLock) { | |
| 668 allocatedConnection = allocateBoundConnection( | 735 allocatedConnection = allocateBoundConnection( |
| 669 context, commandLine, inSandbox, alwaysInForeground) ; | 736 context, commandLine, inSandbox, alwaysInForeground, creationParams); |
| 670 if (allocatedConnection == null) { | 737 if (allocatedConnection == null) { |
| 671 Log.d(TAG, "Allocation of new service failed. Queuing up pending spawn."); | 738 Log.d(TAG, "Allocation of new service failed. Queuing up pending spawn."); |
| 672 sPendingSpawnQueue.enqueueLocked(new PendingSpawnData(co ntext, commandLine, | 739 pendingSpawnQueue.enqueueLocked(new PendingSpawnData(con text, commandLine, |
| 673 childProcessId, filesToBeMapped, clientContext, | 740 childProcessId, filesToBeMapped, clientContext, |
| 674 callbackType, inSandbox)); | 741 callbackType, inSandbox, creationParams)); |
| 675 return; | 742 return; |
| 676 } | 743 } |
| 677 } | 744 } |
| 678 } | 745 } |
| 679 | 746 |
| 680 Log.d(TAG, "Setting up connection to process: slot=%d", | 747 Log.d(TAG, "Setting up connection to process: slot=%d", |
| 681 allocatedConnection.getServiceNumber()); | 748 allocatedConnection.getServiceNumber()); |
| 682 triggerConnectionSetup(allocatedConnection, commandLine, childProces sId, | 749 triggerConnectionSetup(allocatedConnection, commandLine, childProces sId, |
| 683 filesToBeMapped, callbackType, clientContext); | 750 filesToBeMapped, callbackType, clientContext); |
| 684 } finally { | 751 } finally { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 841 } | 908 } |
| 842 | 909 |
| 843 static void logPidWarning(int pid, String message) { | 910 static void logPidWarning(int pid, String message) { |
| 844 // This class is effectively a no-op in single process mode, so don't lo g warnings there. | 911 // This class is effectively a no-op in single process mode, so don't lo g warnings there. |
| 845 if (pid > 0 && !nativeIsSingleProcess()) { | 912 if (pid > 0 && !nativeIsSingleProcess()) { |
| 846 Log.w(TAG, "%s, pid=%d", message, pid); | 913 Log.w(TAG, "%s, pid=%d", message, pid); |
| 847 } | 914 } |
| 848 } | 915 } |
| 849 | 916 |
| 850 @VisibleForTesting | 917 @VisibleForTesting |
| 851 static ChildProcessConnection allocateBoundConnectionForTesting(Context cont ext) { | 918 static ChildProcessConnection allocateBoundConnectionForTesting(Context cont ext, |
| 852 return allocateBoundConnection(context, null, true, false); | 919 ChildProcessCreationParams creationParams) { |
| 920 return allocateBoundConnection(context, null, true, false, creationParam s); | |
| 853 } | 921 } |
| 854 | 922 |
| 855 /** | 923 /** |
| 856 * Queue up a spawn requests for testing. | 924 * Queue up a spawn requests for testing. |
| 857 */ | 925 */ |
| 858 @VisibleForTesting | 926 @VisibleForTesting |
| 859 static void enqueuePendingSpawnForTesting(Context context, String[] commandL ine) { | 927 static void enqueuePendingSpawnForTesting(Context context, String[] commandL ine, |
| 860 synchronized (PendingSpawnQueue.sPendingSpawnsLock) { | 928 ChildProcessCreationParams creationParams, boolean inSandbox) { |
| 861 sPendingSpawnQueue.enqueueLocked(new PendingSpawnData(context, comma ndLine, 1, | 929 PendingSpawnQueue pendingSpawnQueue = getPendingSpawnQueue(context, |
| 862 new FileDescriptorInfo[0], 0, CALLBACK_FOR_RENDERER_PROCESS, true)); | 930 creationParams.getPackageName(), inSandbox); |
| 931 synchronized (pendingSpawnQueue.mPendingSpawnsLock) { | |
| 932 pendingSpawnQueue.enqueueLocked(new PendingSpawnData(context, comman dLine, 1, | |
| 933 new FileDescriptorInfo[0], 0, CALLBACK_FOR_RENDERER_PROCESS, true, | |
| 934 creationParams)); | |
| 863 } | 935 } |
| 864 } | 936 } |
| 865 | 937 |
| 866 /** @return the count of sandboxed connections managed by the allocator */ | 938 /** |
| 939 * @return the number of sandboxed connections of given {@link packageName} managed by the | |
| 940 * allocator. | |
| 941 */ | |
| 867 @VisibleForTesting | 942 @VisibleForTesting |
| 868 static int allocatedConnectionsCountForTesting(Context context) { | 943 static int allocatedSandboxedConnectionsCountForTesting(Context context, Str ing packageName) { |
| 869 initConnectionAllocatorsIfNecessary(context); | 944 initConnectionAllocatorsIfNecessary(context, true, packageName); |
| 870 return sSandboxedChildConnectionAllocator.allocatedConnectionsCountForTe sting(); | 945 return sSandboxedChildConnectionAllocatorMap.get(packageName) |
| 946 .allocatedConnectionsCountForTesting(); | |
| 871 } | 947 } |
| 872 | 948 |
| 873 /** @return the count of services set up and working */ | 949 /** @return the count of services set up and working */ |
| 874 @VisibleForTesting | 950 @VisibleForTesting |
| 875 static int connectedServicesCountForTesting() { | 951 static int connectedServicesCountForTesting() { |
| 876 return sServiceMap.size(); | 952 return sServiceMap.size(); |
| 877 } | 953 } |
| 878 | 954 |
| 879 /** @return the count of pending spawns in the queue */ | 955 /** |
| 956 * @param context The context. | |
| 957 * @param packageName The package name of the {@link ChildProcessAlocator}. | |
| 958 * @param inSandbox Whether the connection is sandboxed. | |
| 959 * @return the count of pending spawns in the queue. | |
| 960 */ | |
| 880 @VisibleForTesting | 961 @VisibleForTesting |
| 881 static int pendingSpawnsCountForTesting() { | 962 static int pendingSpawnsCountForTesting(Context context, String packageName, |
| 882 synchronized (PendingSpawnQueue.sPendingSpawnsLock) { | 963 boolean inSandbox) { |
| 883 return sPendingSpawnQueue.sizeLocked(); | 964 PendingSpawnQueue pendingSpawnQueue = getPendingSpawnQueue(context, pack ageName, inSandbox); |
| 965 synchronized (pendingSpawnQueue.mPendingSpawnsLock) { | |
| 966 return pendingSpawnQueue.sizeLocked(); | |
| 884 } | 967 } |
| 885 } | 968 } |
| 886 | 969 |
| 887 /** | 970 /** |
| 888 * Kills the child process for testing. | 971 * Kills the child process for testing. |
| 889 * @return true iff the process was killed as expected | 972 * @return true iff the process was killed as expected |
| 890 */ | 973 */ |
| 891 @VisibleForTesting | 974 @VisibleForTesting |
| 892 public static boolean crashProcessForTesting(int pid) { | 975 public static boolean crashProcessForTesting(int pid) { |
| 893 if (sServiceMap.get(pid) == null) return false; | 976 if (sServiceMap.get(pid) == null) return false; |
| 894 | 977 |
| 895 try { | 978 try { |
| 896 ((ChildProcessConnectionImpl) sServiceMap.get(pid)).crashServiceForT esting(); | 979 ((ChildProcessConnectionImpl) sServiceMap.get(pid)).crashServiceForT esting(); |
| 897 } catch (RemoteException ex) { | 980 } catch (RemoteException ex) { |
| 898 return false; | 981 return false; |
| 899 } | 982 } |
| 900 | 983 |
| 901 return true; | 984 return true; |
| 902 } | 985 } |
| 903 | 986 |
| 904 private static native void nativeOnChildProcessStarted(long clientContext, i nt pid); | 987 private static native void nativeOnChildProcessStarted(long clientContext, i nt pid); |
| 905 private static native void nativeEstablishSurfacePeer( | 988 private static native void nativeEstablishSurfacePeer( |
| 906 int pid, Surface surface, int primaryID, int secondaryID); | 989 int pid, Surface surface, int primaryID, int secondaryID); |
| 907 private static native boolean nativeIsSingleProcess(); | 990 private static native boolean nativeIsSingleProcess(); |
| 908 } | 991 } |
| OLD | NEW |