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

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

Issue 2049843004: Upstream: Renderers are running in WebAPKs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « content/public/android/java/src/org/chromium/content/browser/ChildProcessCreationParams.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.app.Service;
8 import android.content.Context; 9 import android.content.Context;
9 import android.content.Intent; 10 import android.content.Intent;
10 import android.content.pm.ApplicationInfo; 11 import android.content.pm.ApplicationInfo;
11 import android.content.pm.PackageManager; 12 import android.content.pm.PackageManager;
12 import android.graphics.SurfaceTexture; 13 import android.graphics.SurfaceTexture;
13 import android.os.Build; 14 import android.os.Build;
14 import android.os.Bundle; 15 import android.os.Bundle;
15 import android.os.ParcelFileDescriptor; 16 import android.os.ParcelFileDescriptor;
16 import android.os.RemoteException; 17 import android.os.RemoteException;
17 import android.text.TextUtils; 18 import android.text.TextUtils;
18 import android.util.Pair; 19 import android.util.Pair;
19 import android.view.Surface; 20 import android.view.Surface;
20 21
21 import org.chromium.base.CommandLine; 22 import org.chromium.base.CommandLine;
22 import org.chromium.base.CpuFeatures; 23 import org.chromium.base.CpuFeatures;
23 import org.chromium.base.Log; 24 import org.chromium.base.Log;
24 import org.chromium.base.ThreadUtils; 25 import org.chromium.base.ThreadUtils;
25 import org.chromium.base.TraceEvent; 26 import org.chromium.base.TraceEvent;
26 import org.chromium.base.VisibleForTesting; 27 import org.chromium.base.VisibleForTesting;
27 import org.chromium.base.annotations.CalledByNative; 28 import org.chromium.base.annotations.CalledByNative;
28 import org.chromium.base.annotations.JNINamespace; 29 import org.chromium.base.annotations.JNINamespace;
29 import org.chromium.base.library_loader.Linker; 30 import org.chromium.base.library_loader.Linker;
30 import org.chromium.content.app.ChildProcessService;
31 import org.chromium.content.app.ChromiumLinkerParams; 31 import org.chromium.content.app.ChromiumLinkerParams;
32 import org.chromium.content.app.DownloadProcessService; 32 import org.chromium.content.app.DownloadProcessService;
33 import org.chromium.content.app.PrivilegedProcessService; 33 import org.chromium.content.app.PrivilegedProcessService;
34 import org.chromium.content.app.SandboxedProcessService; 34 import org.chromium.content.app.SandboxedProcessService;
35 import org.chromium.content.common.ContentSwitches; 35 import org.chromium.content.common.ContentSwitches;
36 import org.chromium.content.common.IChildProcessCallback; 36 import org.chromium.content.common.IChildProcessCallback;
37 import org.chromium.content.common.SurfaceWrapper; 37 import org.chromium.content.common.SurfaceWrapper;
38 38
39 import java.io.IOException; 39 import java.io.IOException;
40 import java.util.ArrayList; 40 import java.util.ArrayList;
(...skipping 17 matching lines...) Expand all
58 58
59 private static class ChildConnectionAllocator { 59 private static class ChildConnectionAllocator {
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 final Class<? extends Service> 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 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. 71 // dequeue the pending spawn data from the same allocator as the connect ion.
72 private final PendingSpawnQueue mPendingSpawnQueue = new PendingSpawnQue ue(); 72 private final PendingSpawnQueue mPendingSpawnQueue = new PendingSpawnQue ue();
73 73
74 public ChildConnectionAllocator(boolean inSandbox, int numChildServices) { 74 public ChildConnectionAllocator(boolean inSandbox, int numChildServices,
75 Class<? extends Service> serviceClass) {
75 mChildProcessConnections = new ChildProcessConnectionImpl[numChildSe rvices]; 76 mChildProcessConnections = new ChildProcessConnectionImpl[numChildSe rvices];
76 mFreeConnectionIndices = new ArrayList<Integer>(numChildServices); 77 mFreeConnectionIndices = new ArrayList<Integer>(numChildServices);
77 for (int i = 0; i < numChildServices; i++) { 78 for (int i = 0; i < numChildServices; i++) {
78 mFreeConnectionIndices.add(i); 79 mFreeConnectionIndices.add(i);
79 } 80 }
80 mChildClass = 81 mChildClass = serviceClass;
81 inSandbox ? SandboxedProcessService.class : PrivilegedProces sService.class;
82 mInSandbox = inSandbox; 82 mInSandbox = inSandbox;
83 } 83 }
84 84
85 public ChildProcessConnection allocate( 85 public ChildProcessConnection allocate(
86 Context context, ChildProcessConnection.DeathCallback deathCallb ack, 86 Context context, ChildProcessConnection.DeathCallback deathCallb ack,
87 ChromiumLinkerParams chromiumLinkerParams, 87 ChromiumLinkerParams chromiumLinkerParams,
88 boolean alwaysInForeground, 88 boolean alwaysInForeground,
89 ChildProcessCreationParams creationParams) { 89 ChildProcessCreationParams creationParams) {
90 synchronized (mConnectionLock) { 90 synchronized (mConnectionLock) {
91 if (mFreeConnectionIndices.isEmpty()) { 91 if (mFreeConnectionIndices.isEmpty()) {
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 // Service class for child process. 228 // Service class for child process.
229 // Map from package name to ChildConnectionAllocator. 229 // Map from package name to ChildConnectionAllocator.
230 private static Map<String, ChildConnectionAllocator> sSandboxedChildConnecti onAllocatorMap; 230 private static Map<String, ChildConnectionAllocator> sSandboxedChildConnecti onAllocatorMap;
231 // As the default value it uses PrivilegedProcessService0. 231 // As the default value it uses PrivilegedProcessService0.
232 private static ChildConnectionAllocator sPrivilegedChildConnectionAllocator; 232 private static ChildConnectionAllocator sPrivilegedChildConnectionAllocator;
233 233
234 private static final String NUM_SANDBOXED_SERVICES_KEY = 234 private static final String NUM_SANDBOXED_SERVICES_KEY =
235 "org.chromium.content.browser.NUM_SANDBOXED_SERVICES"; 235 "org.chromium.content.browser.NUM_SANDBOXED_SERVICES";
236 private static final String NUM_PRIVILEGED_SERVICES_KEY = 236 private static final String NUM_PRIVILEGED_SERVICES_KEY =
237 "org.chromium.content.browser.NUM_PRIVILEGED_SERVICES"; 237 "org.chromium.content.browser.NUM_PRIVILEGED_SERVICES";
238 private static final String SANDBOXED_SERVICES_NAME_KEY =
239 "org.chromium.content.browser.SANDBOXED_SERVICES_NAME";
238 // Overrides the number of available sandboxed services. 240 // Overrides the number of available sandboxed services.
239 @VisibleForTesting 241 @VisibleForTesting
240 public static final String SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING = "num- sandboxed-services"; 242 public static final String SWITCH_NUM_SANDBOXED_SERVICES_FOR_TESTING = "num- sandboxed-services";
241 243
242 private static int getNumberOfServices(Context context, boolean inSandbox, S tring packageName) { 244 private static int getNumberOfServices(Context context, boolean inSandbox, S tring packageName) {
243 try { 245 try {
244 PackageManager packageManager = context.getPackageManager(); 246 PackageManager packageManager = context.getPackageManager();
245 ApplicationInfo appInfo = packageManager.getApplicationInfo(packageN ame, 247 ApplicationInfo appInfo = packageManager.getApplicationInfo(packageN ame,
246 PackageManager.GET_META_DATA); 248 PackageManager.GET_META_DATA);
247 int numServices = -1; 249 int numServices = -1;
(...skipping 17 matching lines...) Expand all
265 } 267 }
266 if (numServices < 0) { 268 if (numServices < 0) {
267 throw new RuntimeException("Illegal meta data value for number o f child services"); 269 throw new RuntimeException("Illegal meta data value for number o f child services");
268 } 270 }
269 return numServices; 271 return numServices;
270 } catch (PackageManager.NameNotFoundException e) { 272 } catch (PackageManager.NameNotFoundException e) {
271 throw new RuntimeException("Could not get application info"); 273 throw new RuntimeException("Could not get application info");
272 } 274 }
273 } 275 }
274 276
277 private static Class<? extends Service> getClassOfService(Context context, b oolean inSandbox,
278 String packageName) {
279 if (!inSandbox) {
280 return PrivilegedProcessService.class;
281 }
282 try {
283 PackageManager packageManager = context.getPackageManager();
284 ApplicationInfo appInfo = packageManager.getApplicationInfo(packageN ame,
285 PackageManager.GET_META_DATA);
286 if (appInfo.metaData != null) {
287 String serviceName = appInfo.metaData.getString(SANDBOXED_SERVIC ES_NAME_KEY);
288 if (serviceName != null) {
289 try {
290 Class<? extends Service> service =
291 (Class<? extends Service>) Class.forName(service Name);
292 return service;
293 } catch (ClassNotFoundException e) {
294 Log.v(TAG, "The child service class doesn't exist");
295 return SandboxedProcessService.class;
no sievers 2016/06/24 19:54:32 Should this throw an exception since it's a malfor
Xi Han 2016/06/24 20:57:47 Done.
296 }
297 }
298 }
299 return SandboxedProcessService.class;
300 } catch (PackageManager.NameNotFoundException e) {
301 Log.v(TAG, "Could not get application info.");
302 return SandboxedProcessService.class;
no sievers 2016/06/24 19:54:32 And maybe this also?
Xi Han 2016/06/24 20:57:48 This causes ChildProcessLauncherTest which with a
303 }
304 }
305
275 private static void initConnectionAllocatorsIfNecessary( 306 private static void initConnectionAllocatorsIfNecessary(
276 Context context, boolean inSandbox, String packageName) { 307 Context context, boolean inSandbox, String packageName) {
277 // TODO(mariakhomenko): Uses an Object to lock the access. 308 // TODO(mariakhomenko): Uses an Object to lock the access.
278 synchronized (ChildProcessLauncher.class) { 309 synchronized (ChildProcessLauncher.class) {
279 if (inSandbox) { 310 if (inSandbox) {
280 if (sSandboxedChildConnectionAllocatorMap == null) { 311 if (sSandboxedChildConnectionAllocatorMap == null) {
281 sSandboxedChildConnectionAllocatorMap = 312 sSandboxedChildConnectionAllocatorMap =
282 new ConcurrentHashMap<String, ChildConnectionAllocat or>(); 313 new ConcurrentHashMap<String, ChildConnectionAllocat or>();
283 } 314 }
284 if (!sSandboxedChildConnectionAllocatorMap.containsKey(packageNa me)) { 315 if (!sSandboxedChildConnectionAllocatorMap.containsKey(packageNa me)) {
285 Log.w(TAG, "Create a new ChildConnectionAllocator with packa ge name = %s," 316 Log.w(TAG, "Create a new ChildConnectionAllocator with packa ge name = %s,"
286 + " inSandbox = true", 317 + " inSandbox = true",
287 packageName); 318 packageName);
288 sSandboxedChildConnectionAllocatorMap.put(packageName, 319 sSandboxedChildConnectionAllocatorMap.put(packageName,
289 new ChildConnectionAllocator(true, 320 new ChildConnectionAllocator(true,
290 getNumberOfServices(context, true, packageNa me))); 321 getNumberOfServices(context, true, packageNa me),
322 getClassOfService(context, true, packageName )));
no sievers 2016/06/24 19:54:32 Doesn't it ignore the class name you set in ChildP
Xi Han 2016/06/24 20:57:47 Forgot to clean up the WebApkActivity.
291 } 323 }
292 } else if (sPrivilegedChildConnectionAllocator == null) { 324 } else if (sPrivilegedChildConnectionAllocator == null) {
293 sPrivilegedChildConnectionAllocator = new ChildConnectionAllocat or( 325 sPrivilegedChildConnectionAllocator = new ChildConnectionAllocat or(
294 false, getNumberOfServices(context, false, packageName)) ; 326 false, getNumberOfServices(context, false, packageName),
327 getClassOfService(context, false, packageName));
295 } 328 }
296 // TODO(pkotwicz|hanxi): Figure out when old allocators should be re moved from 329 // TODO(pkotwicz|hanxi): Figure out when old allocators should be re moved from
297 // {@code sSandboxedChildConnectionAllocatorMap}. 330 // {@code sSandboxedChildConnectionAllocatorMap}.
298 } 331 }
299 } 332 }
300 333
301 /** 334 /**
302 * Note: please make sure that the Allocator has been initialized before cal ling this function. 335 * Note: please make sure that the Allocator has been initialized before cal ling this function.
303 * Otherwise, always calls {@link initConnectionAllocatorsIfNecessary} first . 336 * Otherwise, always calls {@link initConnectionAllocatorsIfNecessary} first .
304 */ 337 */
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
937 ChildProcessCreationParams creationParams) { 970 ChildProcessCreationParams creationParams) {
938 return allocateBoundConnection(context, null, true, false, creationParam s); 971 return allocateBoundConnection(context, null, true, false, creationParam s);
939 } 972 }
940 973
941 /** 974 /**
942 * Queue up a spawn requests for testing. 975 * Queue up a spawn requests for testing.
943 */ 976 */
944 @VisibleForTesting 977 @VisibleForTesting
945 static void enqueuePendingSpawnForTesting(Context context, String[] commandL ine, 978 static void enqueuePendingSpawnForTesting(Context context, String[] commandL ine,
946 ChildProcessCreationParams creationParams, boolean inSandbox) { 979 ChildProcessCreationParams creationParams, boolean inSandbox) {
980 String packageName = creationParams != null ? creationParams.getPackageN ame()
981 : context.getPackageName();
947 PendingSpawnQueue pendingSpawnQueue = getPendingSpawnQueue(context, 982 PendingSpawnQueue pendingSpawnQueue = getPendingSpawnQueue(context,
948 creationParams.getPackageName(), inSandbox); 983 packageName, inSandbox);
949 synchronized (pendingSpawnQueue.mPendingSpawnsLock) { 984 synchronized (pendingSpawnQueue.mPendingSpawnsLock) {
950 pendingSpawnQueue.enqueueLocked(new PendingSpawnData(context, comman dLine, 1, 985 pendingSpawnQueue.enqueueLocked(new PendingSpawnData(context, comman dLine, 1,
951 new FileDescriptorInfo[0], 0, CALLBACK_FOR_RENDERER_PROCESS, true, 986 new FileDescriptorInfo[0], 0, CALLBACK_FOR_RENDERER_PROCESS, true,
952 creationParams)); 987 creationParams));
953 } 988 }
954 } 989 }
955 990
956 /** 991 /**
957 * @return the number of sandboxed connections of given {@link packageName} managed by the 992 * @return the number of sandboxed connections of given {@link packageName} managed by the
958 * allocator. 993 * allocator.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1000 } 1035 }
1001 1036
1002 return true; 1037 return true;
1003 } 1038 }
1004 1039
1005 private static native void nativeOnChildProcessStarted(long clientContext, i nt pid); 1040 private static native void nativeOnChildProcessStarted(long clientContext, i nt pid);
1006 private static native void nativeEstablishSurfacePeer( 1041 private static native void nativeEstablishSurfacePeer(
1007 int pid, Surface surface, int primaryID, int secondaryID); 1042 int pid, Surface surface, int primaryID, int secondaryID);
1008 private static native boolean nativeIsSingleProcess(); 1043 private static native boolean nativeIsSingleProcess();
1009 } 1044 }
OLDNEW
« no previous file with comments | « content/public/android/java/src/org/chromium/content/browser/ChildProcessCreationParams.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698