Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(675)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/ChildProcessLauncher.java

Issue 2017963003: Upstream: ChildProcessLauncher connects renderer processes of WebAPKs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove PROCESS_WEBAPK_CHILD. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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) {
277 // TODO(mariakhomenko): Uses an Object to lock the access.
266 synchronized (ChildProcessLauncher.class) { 278 synchronized (ChildProcessLauncher.class) {
267 if (sSandboxedChildConnectionAllocator == null) { 279 if (inSandbox) {
268 sSandboxedChildConnectionAllocator = 280 if (sSandboxedChildConnectionAllocatorMap == null) {
269 new ChildConnectionAllocator(true, getNumberOfServices(c ontext, true)); 281 sSandboxedChildConnectionAllocatorMap =
282 new ConcurrentHashMap<String, ChildConnectionAllocat or>();
283 }
284 if (!sSandboxedChildConnectionAllocatorMap.containsKey(packageNa me)) {
285 Log.w(TAG, "Create a new ChildConnectionAllocator with packa ge name = %s,"
286 + " inSandbox = true",
287 packageName);
288 sSandboxedChildConnectionAllocatorMap.put(packageName,
289 new ChildConnectionAllocator(true,
290 getNumberOfServices(context, true, packageNa me)));
291 }
292 } else if (sPrivilegedChildConnectionAllocator == null) {
293 sPrivilegedChildConnectionAllocator = new ChildConnectionAllocat or(
294 false, getNumberOfServices(context, false, packageName)) ;
270 } 295 }
271 if (sPrivilegedChildConnectionAllocator == null) { 296 // TODO(pkotwicz|hanxi): Figure out when old allocators should be re moved from
272 sPrivilegedChildConnectionAllocator = 297 // {@code sSandboxedChildConnectionAllocatorMap}.
273 new ChildConnectionAllocator(false, getNumberOfServices( context, false));
274 }
275 } 298 }
276 } 299 }
277 300
278 private static ChildConnectionAllocator getConnectionAllocator(boolean inSan dbox) { 301 /**
279 return inSandbox 302 * Note: please make sure that the Allocator has been initialized before cal ling this function.
280 ? sSandboxedChildConnectionAllocator : sPrivilegedChildConnectio nAllocator; 303 * Otherwise, always calls {@link initConnectionAllocatorsIfNecessary} first .
304 */
305 private static ChildConnectionAllocator getConnectionAllocator(
306 String packageName, boolean inSandbox) {
307 if (!inSandbox) {
308 return sPrivilegedChildConnectionAllocator;
309 }
310 return sSandboxedChildConnectionAllocatorMap.get(packageName);
311 }
312
313 /**
314 * Get the PendingSpawnQueue of the Allocator. Initialize the Allocator if n eeded.
315 */
316 private static PendingSpawnQueue getPendingSpawnQueue(Context context, Strin g packageName,
317 boolean inSandbox) {
318 initConnectionAllocatorsIfNecessary(context, inSandbox, packageName);
319 return getConnectionAllocator(packageName, inSandbox).getPendingSpawnQue ue();
281 } 320 }
282 321
283 private static ChildProcessConnection allocateConnection(Context context, bo olean inSandbox, 322 private static ChildProcessConnection allocateConnection(Context context, bo olean inSandbox,
284 ChromiumLinkerParams chromiumLinkerParams, boolean alwaysInForegroun d) { 323 ChromiumLinkerParams chromiumLinkerParams, boolean alwaysInForegroun d,
324 ChildProcessCreationParams creationParams) {
285 ChildProcessConnection.DeathCallback deathCallback = 325 ChildProcessConnection.DeathCallback deathCallback =
286 new ChildProcessConnection.DeathCallback() { 326 new ChildProcessConnection.DeathCallback() {
287 @Override 327 @Override
288 public void onChildProcessDied(ChildProcessConnection connec tion) { 328 public void onChildProcessDied(ChildProcessConnection connec tion) {
289 if (connection.getPid() != 0) { 329 if (connection.getPid() != 0) {
290 stop(connection.getPid()); 330 stop(connection.getPid());
291 } else { 331 } else {
292 freeConnection(connection); 332 freeConnection(connection);
293 } 333 }
294 } 334 }
295 }; 335 };
296 initConnectionAllocatorsIfNecessary(context); 336 String packageName = creationParams != null ? creationParams.getPackageN ame()
297 return getConnectionAllocator(inSandbox).allocate(context, deathCallback , 337 : context.getPackageName();
298 chromiumLinkerParams, alwaysInForeground, ChildProcessCreationPa rams.get()); 338 initConnectionAllocatorsIfNecessary(context, inSandbox, packageName);
339 return getConnectionAllocator(packageName, inSandbox)
340 .allocate(context, deathCallback, chromiumLinkerParams, alwaysIn Foreground,
341 creationParams);
299 } 342 }
300 343
301 private static boolean sLinkerInitialized = false; 344 private static boolean sLinkerInitialized = false;
302 private static long sLinkerLoadAddress = 0; 345 private static long sLinkerLoadAddress = 0;
303 346
304 private static ChromiumLinkerParams getLinkerParamsForNewConnection() { 347 private static ChromiumLinkerParams getLinkerParamsForNewConnection() {
305 if (!sLinkerInitialized) { 348 if (!sLinkerInitialized) {
306 if (Linker.isUsed()) { 349 if (Linker.isUsed()) {
307 sLinkerLoadAddress = Linker.getInstance().getBaseLoadAddress(); 350 sLinkerLoadAddress = Linker.getInstance().getBaseLoadAddress();
308 if (sLinkerLoadAddress == 0) { 351 if (sLinkerLoadAddress == 0) {
(...skipping 13 matching lines...) Expand all
322 waitForSharedRelros, 365 waitForSharedRelros,
323 linker.getTestRunnerClassNameForTest ing(), 366 linker.getTestRunnerClassNameForTest ing(),
324 linker.getImplementationForTesting() ); 367 linker.getImplementationForTesting() );
325 } else { 368 } else {
326 return new ChromiumLinkerParams(sLinkerLoadAddress, 369 return new ChromiumLinkerParams(sLinkerLoadAddress,
327 waitForSharedRelros); 370 waitForSharedRelros);
328 } 371 }
329 } 372 }
330 373
331 private static ChildProcessConnection allocateBoundConnection(Context contex t, 374 private static ChildProcessConnection allocateBoundConnection(Context contex t,
332 String[] commandLine, boolean inSandbox, boolean alwaysInForeground) { 375 String[] commandLine, boolean inSandbox, boolean alwaysInForeground,
376 ChildProcessCreationParams creationParams) {
333 ChromiumLinkerParams chromiumLinkerParams = getLinkerParamsForNewConnect ion(); 377 ChromiumLinkerParams chromiumLinkerParams = getLinkerParamsForNewConnect ion();
334 ChildProcessConnection connection = allocateConnection(context, inSandbo x, 378 ChildProcessConnection connection = allocateConnection(
335 chromiumLinkerParams, alwaysInForeground); 379 context, inSandbox, chromiumLinkerParams, alwaysInForeground, cr eationParams);
336 if (connection != null) { 380 if (connection != null) {
337 connection.start(commandLine); 381 connection.start(commandLine);
338 382
339 if (inSandbox && !sSandboxedChildConnectionAllocator.isFreeConnectio nAvailable()) { 383 String packageName = creationParams != null ? creationParams.getPack ageName()
384 : context.getPackageName();
385 if (inSandbox && !getConnectionAllocator(packageName, inSandbox)
386 .isFreeConnectionAvailable()) {
340 // Proactively releases all the moderate bindings once all the s andboxed services 387 // 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 388 // are allocated, which will be very likely to have some of them killed by OOM
342 // killer. 389 // killer.
343 sBindingManager.releaseAllModerateBindings(); 390 sBindingManager.releaseAllModerateBindings();
344 } 391 }
345 } 392 }
346 return connection; 393 return connection;
347 } 394 }
348 395
349 private static final long FREE_CONNECTION_DELAY_MILLIS = 1; 396 private static final long FREE_CONNECTION_DELAY_MILLIS = 1;
(...skipping 13 matching lines...) Expand all
363 @Override 410 @Override
364 public void run() { 411 public void run() {
365 final PendingSpawnData pendingSpawn = freeConnectionAndDequeuePe nding(conn); 412 final PendingSpawnData pendingSpawn = freeConnectionAndDequeuePe nding(conn);
366 if (pendingSpawn != null) { 413 if (pendingSpawn != null) {
367 new Thread(new Runnable() { 414 new Thread(new Runnable() {
368 @Override 415 @Override
369 public void run() { 416 public void run() {
370 startInternal(pendingSpawn.context(), pendingSpawn.c ommandLine(), 417 startInternal(pendingSpawn.context(), pendingSpawn.c ommandLine(),
371 pendingSpawn.childProcessId(), pendingSpawn. filesToBeMapped(), 418 pendingSpawn.childProcessId(), pendingSpawn. filesToBeMapped(),
372 pendingSpawn.clientContext(), pendingSpawn.c allbackType(), 419 pendingSpawn.clientContext(), pendingSpawn.c allbackType(),
373 pendingSpawn.inSandbox()); 420 pendingSpawn.inSandbox(), pendingSpawn.getCr eationParams());
374 } 421 }
375 }).start(); 422 }).start();
376 } 423 }
377 } 424 }
378 }, FREE_CONNECTION_DELAY_MILLIS); 425 }, FREE_CONNECTION_DELAY_MILLIS);
379 } 426 }
380 427
381 private static PendingSpawnData freeConnectionAndDequeuePending(ChildProcess Connection conn) { 428 private static PendingSpawnData freeConnectionAndDequeuePending(ChildProcess Connection conn) {
382 synchronized (PendingSpawnQueue.sPendingSpawnsLock) { 429 ChildConnectionAllocator allocator = getConnectionAllocator(
383 getConnectionAllocator(conn.isInSandbox()).free(conn); 430 conn.getPackageName(), conn.isInSandbox());
384 return sPendingSpawnQueue.dequeueLocked(); 431 assert allocator != null;
432 PendingSpawnQueue pendingSpawnQueue = allocator.getPendingSpawnQueue();
433 synchronized (pendingSpawnQueue.mPendingSpawnsLock) {
434 allocator.free(conn);
435 return pendingSpawnQueue.dequeueLocked();
385 } 436 }
386 } 437 }
387 438
388 // Represents an invalid process handle; same as base/process/process.h kNul lProcessHandle. 439 // Represents an invalid process handle; same as base/process/process.h kNul lProcessHandle.
389 private static final int NULL_PROCESS_HANDLE = 0; 440 private static final int NULL_PROCESS_HANDLE = 0;
390 441
391 // Map from pid to ChildService connection. 442 // Map from pid to ChildService connection.
392 private static Map<Integer, ChildProcessConnection> sServiceMap = 443 private static Map<Integer, ChildProcessConnection> sServiceMap =
393 new ConcurrentHashMap<Integer, ChildProcessConnection>(); 444 new ConcurrentHashMap<Integer, ChildProcessConnection>();
394 445
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 /** 557 /**
507 * Called when the embedding application is sent to background. 558 * Called when the embedding application is sent to background.
508 */ 559 */
509 public static void onSentToBackground() { 560 public static void onSentToBackground() {
510 sApplicationInForeground = false; 561 sApplicationInForeground = false;
511 sBindingManager.onSentToBackground(); 562 sBindingManager.onSentToBackground();
512 } 563 }
513 564
514 /** 565 /**
515 * Starts moderate binding management. 566 * Starts moderate binding management.
567 * Note: WebAPKs and non WebAPKs share the same moderate binding pool, so th e size of the
568 * shared moderate binding pool is always set based on the number of sandbox es processes
569 * used by Chrome.
516 * @param context Android's context. 570 * @param context Android's context.
517 * @param moderateBindingTillBackgrounded true if the BindingManager should add a moderate 571 * @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 572 * binding to a render process when it is created and remove the moderate bi nding when Chrome is
519 * sent to the background. 573 * sent to the background.
520 */ 574 */
521 public static void startModerateBindingManagement( 575 public static void startModerateBindingManagement(
522 Context context, boolean moderateBindingTillBackgrounded) { 576 Context context, boolean moderateBindingTillBackgrounded) {
523 sBindingManager.startModerateBindingManagement( 577 sBindingManager.startModerateBindingManagement(context,
524 context, getNumberOfServices(context, true), moderateBindingTill Backgrounded); 578 getNumberOfServices(context, true, context.getPackageName()),
579 moderateBindingTillBackgrounded);
525 } 580 }
526 581
527 /** 582 /**
528 * Called when the embedding application is brought to foreground. 583 * Called when the embedding application is brought to foreground.
529 */ 584 */
530 public static void onBroughtToForeground() { 585 public static void onBroughtToForeground() {
531 sApplicationInForeground = true; 586 sApplicationInForeground = true;
532 sBindingManager.onBroughtToForeground(); 587 sBindingManager.onBroughtToForeground();
533 } 588 }
534 589
535 /** 590 /**
536 * Returns whether the application is currently in the foreground. 591 * Returns whether the application is currently in the foreground.
537 */ 592 */
538 static boolean isApplicationInForeground() { 593 static boolean isApplicationInForeground() {
539 return sApplicationInForeground; 594 return sApplicationInForeground;
540 } 595 }
541 596
542 /** 597 /**
543 * Should be called early in startup so the work needed to spawn the child p rocess can be done 598 * 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 599 * in parallel to other startup work. Must not be called on the UI thread. S pare connection is
545 * created in sandboxed child process. 600 * created in sandboxed child process.
546 * @param context the application context used for the connection. 601 * @param context the application context used for the connection.
547 * @param params child process creation params.
548 */ 602 */
549 public static void warmUp(Context context, ChildProcessCreationParams params ) { 603 public static void warmUp(Context context) {
550 ChildProcessCreationParams.set(params);
551 synchronized (ChildProcessLauncher.class) { 604 synchronized (ChildProcessLauncher.class) {
552 assert !ThreadUtils.runningOnUiThread(); 605 assert !ThreadUtils.runningOnUiThread();
553 if (sSpareSandboxedConnection == null) { 606 if (sSpareSandboxedConnection == null) {
554 sSpareSandboxedConnection = allocateBoundConnection(context, nul l, true, false); 607 ChildProcessCreationParams params = ChildProcessCreationParams.g et();
608 if (params != null) {
609 params = params.copy();
610 }
611 sSpareSandboxedConnection = allocateBoundConnection(context, nul l, true, false,
612 params);
555 } 613 }
556 } 614 }
557 } 615 }
558 616
559 @CalledByNative 617 @CalledByNative
560 private static FileDescriptorInfo makeFdInfo( 618 private static FileDescriptorInfo makeFdInfo(
561 int id, int fd, boolean autoClose, long offset, long size) { 619 int id, int fd, boolean autoClose, long offset, long size) {
562 ParcelFileDescriptor pFd; 620 ParcelFileDescriptor pFd;
563 if (autoClose) { 621 if (autoClose) {
564 // Adopt the FD, it will be closed when we close the ParcelFileDescr iptor. 622 // Adopt the FD, it will be closed when we close the ParcelFileDescr iptor.
(...skipping 22 matching lines...) Expand all
587 */ 645 */
588 @CalledByNative 646 @CalledByNative
589 private static void start(Context context, final String[] commandLine, int c hildProcessId, 647 private static void start(Context context, final String[] commandLine, int c hildProcessId,
590 FileDescriptorInfo[] filesToBeMapped, long clientContext) { 648 FileDescriptorInfo[] filesToBeMapped, long clientContext) {
591 assert clientContext != 0; 649 assert clientContext != 0;
592 650
593 int callbackType = CALLBACK_FOR_UNKNOWN_PROCESS; 651 int callbackType = CALLBACK_FOR_UNKNOWN_PROCESS;
594 boolean inSandbox = true; 652 boolean inSandbox = true;
595 String processType = 653 String processType =
596 ContentSwitches.getSwitchValue(commandLine, ContentSwitches.SWIT CH_PROCESS_TYPE); 654 ContentSwitches.getSwitchValue(commandLine, ContentSwitches.SWIT CH_PROCESS_TYPE);
655 ChildProcessCreationParams params = null;
597 if (ContentSwitches.SWITCH_RENDERER_PROCESS.equals(processType)) { 656 if (ContentSwitches.SWITCH_RENDERER_PROCESS.equals(processType)) {
598 callbackType = CALLBACK_FOR_RENDERER_PROCESS; 657 callbackType = CALLBACK_FOR_RENDERER_PROCESS;
658 if (ChildProcessCreationParams.get() != null) {
659 params = ChildProcessCreationParams.get().copy();
660 }
599 } else if (ContentSwitches.SWITCH_GPU_PROCESS.equals(processType)) { 661 } else if (ContentSwitches.SWITCH_GPU_PROCESS.equals(processType)) {
600 callbackType = CALLBACK_FOR_GPU_PROCESS; 662 callbackType = CALLBACK_FOR_GPU_PROCESS;
601 inSandbox = false; 663 inSandbox = false;
602 } else if (ContentSwitches.SWITCH_UTILITY_PROCESS.equals(processType)) { 664 } else if (ContentSwitches.SWITCH_UTILITY_PROCESS.equals(processType)) {
603 // We only support sandboxed right now. 665 // We only support sandboxed right now.
604 callbackType = CALLBACK_FOR_UTILITY_PROCESS; 666 callbackType = CALLBACK_FOR_UTILITY_PROCESS;
605 } else { 667 } else {
606 assert false; 668 assert false;
607 } 669 }
608 670
609 startInternal(context, commandLine, childProcessId, filesToBeMapped, cli entContext, 671 startInternal(context, commandLine, childProcessId, filesToBeMapped, cli entContext,
610 callbackType, inSandbox); 672 callbackType, inSandbox, params);
611 } 673 }
612 674
613 /** 675 /**
614 * Spawns a background download process if it hasn't been started. The downl oad process will 676 * 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. 677 * manage its own lifecyle and can outlive chrome.
616 * 678 *
617 * @param context Context used to obtain the application context. 679 * @param context Context used to obtain the application context.
618 * @param commandLine The child process command line argv. 680 * @param commandLine The child process command line argv.
619 */ 681 */
620 @SuppressLint("NewApi") 682 @SuppressLint("NewApi")
(...skipping 22 matching lines...) Expand all
643 context.startService(intent); 705 context.startService(intent);
644 } 706 }
645 707
646 private static void startInternal( 708 private static void startInternal(
647 Context context, 709 Context context,
648 final String[] commandLine, 710 final String[] commandLine,
649 int childProcessId, 711 int childProcessId,
650 FileDescriptorInfo[] filesToBeMapped, 712 FileDescriptorInfo[] filesToBeMapped,
651 long clientContext, 713 long clientContext,
652 int callbackType, 714 int callbackType,
653 boolean inSandbox) { 715 boolean inSandbox,
716 ChildProcessCreationParams creationParams) {
654 try { 717 try {
655 TraceEvent.begin("ChildProcessLauncher.startInternal"); 718 TraceEvent.begin("ChildProcessLauncher.startInternal");
656 719
657 ChildProcessConnection allocatedConnection = null; 720 ChildProcessConnection allocatedConnection = null;
721 String packageName = creationParams != null ? creationParams.getPack ageName()
722 : context.getPackageName();
658 synchronized (ChildProcessLauncher.class) { 723 synchronized (ChildProcessLauncher.class) {
659 if (inSandbox) { 724 if (inSandbox && sSpareSandboxedConnection != null
725 && sSpareSandboxedConnection.getPackageName().equals(pac kageName)) {
660 allocatedConnection = sSpareSandboxedConnection; 726 allocatedConnection = sSpareSandboxedConnection;
661 sSpareSandboxedConnection = null; 727 sSpareSandboxedConnection = null;
662 } 728 }
663 } 729 }
664 if (allocatedConnection == null) { 730 if (allocatedConnection == null) {
665 boolean alwaysInForeground = false; 731 boolean alwaysInForeground = false;
666 if (callbackType == CALLBACK_FOR_GPU_PROCESS) alwaysInForeground = true; 732 if (callbackType == CALLBACK_FOR_GPU_PROCESS) alwaysInForeground = true;
667 synchronized (PendingSpawnQueue.sPendingSpawnsLock) { 733 PendingSpawnQueue pendingSpawnQueue = getPendingSpawnQueue(
734 context, packageName, inSandbox);
735 synchronized (pendingSpawnQueue.mPendingSpawnsLock) {
668 allocatedConnection = allocateBoundConnection( 736 allocatedConnection = allocateBoundConnection(
669 context, commandLine, inSandbox, alwaysInForeground) ; 737 context, commandLine, inSandbox, alwaysInForeground, creationParams);
670 if (allocatedConnection == null) { 738 if (allocatedConnection == null) {
671 Log.d(TAG, "Allocation of new service failed. Queuing up pending spawn."); 739 Log.d(TAG, "Allocation of new service failed. Queuing up pending spawn.");
672 sPendingSpawnQueue.enqueueLocked(new PendingSpawnData(co ntext, commandLine, 740 pendingSpawnQueue.enqueueLocked(new PendingSpawnData(con text, commandLine,
673 childProcessId, filesToBeMapped, clientContext, 741 childProcessId, filesToBeMapped, clientContext,
674 callbackType, inSandbox)); 742 callbackType, inSandbox, creationParams));
675 return; 743 return;
676 } 744 }
677 } 745 }
678 } 746 }
679 747
680 Log.d(TAG, "Setting up connection to process: slot=%d", 748 Log.d(TAG, "Setting up connection to process: slot=%d",
681 allocatedConnection.getServiceNumber()); 749 allocatedConnection.getServiceNumber());
682 triggerConnectionSetup(allocatedConnection, commandLine, childProces sId, 750 triggerConnectionSetup(allocatedConnection, commandLine, childProces sId,
683 filesToBeMapped, callbackType, clientContext); 751 filesToBeMapped, callbackType, clientContext);
684 } finally { 752 } finally {
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 } 909 }
842 910
843 static void logPidWarning(int pid, String message) { 911 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. 912 // This class is effectively a no-op in single process mode, so don't lo g warnings there.
845 if (pid > 0 && !nativeIsSingleProcess()) { 913 if (pid > 0 && !nativeIsSingleProcess()) {
846 Log.w(TAG, "%s, pid=%d", message, pid); 914 Log.w(TAG, "%s, pid=%d", message, pid);
847 } 915 }
848 } 916 }
849 917
850 @VisibleForTesting 918 @VisibleForTesting
851 static ChildProcessConnection allocateBoundConnectionForTesting(Context cont ext) { 919 static ChildProcessConnection allocateBoundConnectionForTesting(Context cont ext,
852 return allocateBoundConnection(context, null, true, false); 920 ChildProcessCreationParams creationParams) {
921 return allocateBoundConnection(context, null, true, false, creationParam s);
853 } 922 }
854 923
855 /** 924 /**
856 * Queue up a spawn requests for testing. 925 * Queue up a spawn requests for testing.
857 */ 926 */
858 @VisibleForTesting 927 @VisibleForTesting
859 static void enqueuePendingSpawnForTesting(Context context, String[] commandL ine) { 928 static void enqueuePendingSpawnForTesting(Context context, String[] commandL ine,
860 synchronized (PendingSpawnQueue.sPendingSpawnsLock) { 929 ChildProcessCreationParams creationParams, boolean inSandbox) {
861 sPendingSpawnQueue.enqueueLocked(new PendingSpawnData(context, comma ndLine, 1, 930 PendingSpawnQueue pendingSpawnQueue = getPendingSpawnQueue(context,
862 new FileDescriptorInfo[0], 0, CALLBACK_FOR_RENDERER_PROCESS, true)); 931 creationParams.getPackageName(), inSandbox);
932 synchronized (pendingSpawnQueue.mPendingSpawnsLock) {
933 pendingSpawnQueue.enqueueLocked(new PendingSpawnData(context, comman dLine, 1,
934 new FileDescriptorInfo[0], 0, CALLBACK_FOR_RENDERER_PROCESS, true,
935 creationParams));
863 } 936 }
864 } 937 }
865 938
866 /** @return the count of sandboxed connections managed by the allocator */ 939 /**
940 * @return the number of sandboxed connections of given {@link packageName} managed by the
941 * allocator.
942 */
867 @VisibleForTesting 943 @VisibleForTesting
868 static int allocatedConnectionsCountForTesting(Context context) { 944 static int allocatedSandboxedConnectionsCountForTesting(Context context, Str ing packageName) {
869 initConnectionAllocatorsIfNecessary(context); 945 initConnectionAllocatorsIfNecessary(context, true, packageName);
870 return sSandboxedChildConnectionAllocator.allocatedConnectionsCountForTe sting(); 946 return sSandboxedChildConnectionAllocatorMap.get(packageName)
947 .allocatedConnectionsCountForTesting();
871 } 948 }
872 949
873 /** @return the count of services set up and working */ 950 /** @return the count of services set up and working */
874 @VisibleForTesting 951 @VisibleForTesting
875 static int connectedServicesCountForTesting() { 952 static int connectedServicesCountForTesting() {
876 return sServiceMap.size(); 953 return sServiceMap.size();
877 } 954 }
878 955
879 /** @return the count of pending spawns in the queue */ 956 /**
957 * @param context The context.
958 * @param packageName The package name of the {@link ChildProcessAlocator}.
959 * @param inSandbox Whether the connection is sandboxed.
960 * @return the count of pending spawns in the queue.
961 */
880 @VisibleForTesting 962 @VisibleForTesting
881 static int pendingSpawnsCountForTesting() { 963 static int pendingSpawnsCountForTesting(Context context, String packageName,
882 synchronized (PendingSpawnQueue.sPendingSpawnsLock) { 964 boolean inSandbox) {
883 return sPendingSpawnQueue.sizeLocked(); 965 PendingSpawnQueue pendingSpawnQueue = getPendingSpawnQueue(context, pack ageName, inSandbox);
966 synchronized (pendingSpawnQueue.mPendingSpawnsLock) {
967 return pendingSpawnQueue.sizeLocked();
884 } 968 }
885 } 969 }
886 970
887 /** 971 /**
888 * Kills the child process for testing. 972 * Kills the child process for testing.
889 * @return true iff the process was killed as expected 973 * @return true iff the process was killed as expected
890 */ 974 */
891 @VisibleForTesting 975 @VisibleForTesting
892 public static boolean crashProcessForTesting(int pid) { 976 public static boolean crashProcessForTesting(int pid) {
893 if (sServiceMap.get(pid) == null) return false; 977 if (sServiceMap.get(pid) == null) return false;
894 978
895 try { 979 try {
896 ((ChildProcessConnectionImpl) sServiceMap.get(pid)).crashServiceForT esting(); 980 ((ChildProcessConnectionImpl) sServiceMap.get(pid)).crashServiceForT esting();
897 } catch (RemoteException ex) { 981 } catch (RemoteException ex) {
898 return false; 982 return false;
899 } 983 }
900 984
901 return true; 985 return true;
902 } 986 }
903 987
904 private static native void nativeOnChildProcessStarted(long clientContext, i nt pid); 988 private static native void nativeOnChildProcessStarted(long clientContext, i nt pid);
905 private static native void nativeEstablishSurfacePeer( 989 private static native void nativeEstablishSurfacePeer(
906 int pid, Surface surface, int primaryID, int secondaryID); 990 int pid, Surface surface, int primaryID, int secondaryID);
907 private static native boolean nativeIsSingleProcess(); 991 private static native boolean nativeIsSingleProcess();
908 } 992 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698