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

Side by Side Diff: content/public/android/java/src/org/chromium/content/app/ChildProcessServiceImpl.java

Issue 2049843004: Upstream: Renderers are running in WebAPKs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits. 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 2016 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.app; 5 package org.chromium.content.app;
6 6
7 import android.app.Service;
8 import android.content.Context; 7 import android.content.Context;
9 import android.content.Intent; 8 import android.content.Intent;
10 import android.graphics.SurfaceTexture; 9 import android.graphics.SurfaceTexture;
11 import android.os.Bundle; 10 import android.os.Bundle;
12 import android.os.IBinder; 11 import android.os.IBinder;
13 import android.os.Parcelable; 12 import android.os.Parcelable;
14 import android.os.Process; 13 import android.os.Process;
15 import android.os.RemoteException; 14 import android.os.RemoteException;
16 import android.view.Surface; 15 import android.view.Surface;
17 16
18 import org.chromium.base.BaseSwitches; 17 import org.chromium.base.BaseSwitches;
19 import org.chromium.base.CommandLine; 18 import org.chromium.base.CommandLine;
20 import org.chromium.base.ContextUtils; 19 import org.chromium.base.ContextUtils;
21 import org.chromium.base.Log; 20 import org.chromium.base.Log;
22 import org.chromium.base.annotations.CalledByNative; 21 import org.chromium.base.annotations.CalledByNative;
23 import org.chromium.base.annotations.JNINamespace; 22 import org.chromium.base.annotations.JNINamespace;
24 import org.chromium.base.annotations.SuppressFBWarnings; 23 import org.chromium.base.annotations.SuppressFBWarnings;
25 import org.chromium.base.library_loader.LibraryLoader; 24 import org.chromium.base.library_loader.LibraryLoader;
26 import org.chromium.base.library_loader.Linker; 25 import org.chromium.base.library_loader.Linker;
27 import org.chromium.base.library_loader.ProcessInitException; 26 import org.chromium.base.library_loader.ProcessInitException;
28 import org.chromium.content.browser.ChildProcessConstants; 27 import org.chromium.content.browser.ChildProcessConstants;
29 import org.chromium.content.browser.ChildProcessCreationParams; 28 import org.chromium.content.browser.ChildProcessCreationParams;
30 import org.chromium.content.browser.ChildProcessLauncher;
31 import org.chromium.content.browser.FileDescriptorInfo; 29 import org.chromium.content.browser.FileDescriptorInfo;
32 import org.chromium.content.common.ContentSwitches; 30 import org.chromium.content.common.ContentSwitches;
33 import org.chromium.content.common.IChildProcessCallback; 31 import org.chromium.content.common.IChildProcessCallback;
34 import org.chromium.content.common.IChildProcessService; 32 import org.chromium.content.common.IChildProcessService;
35 import org.chromium.content.common.SurfaceWrapper; 33 import org.chromium.content.common.SurfaceWrapper;
36 34
37 import java.util.concurrent.Semaphore; 35 import java.util.concurrent.Semaphore;
38 import java.util.concurrent.atomic.AtomicReference; 36 import java.util.concurrent.atomic.AtomicReference;
39 37
40 /** 38 /**
41 * This is the base class for child services; the [Non]SandboxedProcessService0, 1.. etc 39 * This class implements all of the functionality for {@link ChildProcessService } which owns an
42 * subclasses provide the concrete service entry points, to enable the browser t o connect 40 * object of {@link ChildProcessServiceImpl}.
43 * to more than one distinct process (i.e. one process per service number, up to limit of N). 41 * It makes possible that WebAPK's ChildProcessService owns a ChildProcessServic eImpl object
44 * The embedding application must declare these service instances in the applica tion section 42 * and uses the same functionalities to create renderer process for WebAPKs when "--enable-webapk"
45 * of its AndroidManifest.xml, for example with N entries of the form:- 43 * flag is turned on.
46 * <service android:name="org.chromium.content.app.[Non]SandboxedProcessServ iceX"
47 * android:process=":[non]sandboxed_processX" />
48 * for X in 0...N-1 (where N is {@link ChildProcessLauncher#MAX_REGISTERED_SERVI CES})
49 */ 44 */
50 @JNINamespace("content") 45 @JNINamespace("content")
51 @SuppressWarnings("SynchronizeOnNonFinalField") 46 @SuppressWarnings("SynchronizeOnNonFinalField")
52 public class ChildProcessService extends Service { 47 public class ChildProcessServiceImpl {
53 private static final String MAIN_THREAD_NAME = "ChildProcessMain"; 48 private static final String MAIN_THREAD_NAME = "ChildProcessMain";
54 private static final String TAG = "ChildProcessService"; 49 private static final String TAG = "ChildProcessService";
55 protected static final FileDescriptorInfo[] EMPTY_FILE_DESCRIPTOR_INFO = {}; 50 protected static final FileDescriptorInfo[] EMPTY_FILE_DESCRIPTOR_INFO = {};
56 private IChildProcessCallback mCallback; 51 private IChildProcessCallback mCallback;
57 52
58 // This is the native "Main" thread for the renderer / utility process. 53 // This is the native "Main" thread for the renderer / utility process.
59 private Thread mMainThread; 54 private Thread mMainThread;
60 // Parameters received via IPC, only accessed while holding the mMainThread monitor. 55 // Parameters received via IPC, only accessed while holding the mMainThread monitor.
61 private String[] mCommandLineParams; 56 private String[] mCommandLineParams;
62 private int mCpuCount; 57 private int mCpuCount;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 getServiceInfo(args); 92 getServiceInfo(args);
98 return Process.myPid(); 93 return Process.myPid();
99 } 94 }
100 95
101 @Override 96 @Override
102 public void crashIntentionallyForTesting() { 97 public void crashIntentionallyForTesting() {
103 Process.killProcess(Process.myPid()); 98 Process.killProcess(Process.myPid());
104 } 99 }
105 }; 100 };
106 101
102 // The class loader of the Host Browser.
pkotwicz 2016/06/15 14:13:50 How about: "The ClassLoader for the host browser c
Xi Han 2016/06/15 17:10:39 Done.
103 private ClassLoader mHostClassLoader;
104
107 /* package */ static Context getContext() { 105 /* package */ static Context getContext() {
108 return sContext.get(); 106 return sContext.get();
109 } 107 }
110 108
111 @Override 109 /**
112 public void onCreate() { 110 * Loads Chrome's native libraries and initializes a ChildProcessServiceImap l.
111 * @param context The application context.
112 * @param hostBrowserContext The context of the Host Browser (i.e. Chrome).
pkotwicz 2016/06/15 14:13:50 Nit: host browser (no capitals)
Xi Han 2016/06/15 17:10:39 Done.
113 */
114 public void create(final Context context, final Context hostBrowserContext) {
115 mHostClassLoader = hostBrowserContext.getClassLoader();
113 Log.i(TAG, "Creating new ChildProcessService pid=%d", Process.myPid()); 116 Log.i(TAG, "Creating new ChildProcessService pid=%d", Process.myPid());
114 if (sContext.get() != null) { 117 if (sContext.get() != null) {
115 throw new RuntimeException("Illegal child process reuse."); 118 throw new RuntimeException("Illegal child process reuse.");
116 } 119 }
117 sContext.set(this); 120 sContext.set(context);
118 super.onCreate();
119 121
120 ContextUtils.initApplicationContext(getApplicationContext()); 122 // Initialize the context for the application who owns this ChildProcess ServiceImpl object.
pkotwicz 2016/06/15 14:13:51 Nit: who -> that "who" is for people only. Accord
Xi Han 2016/06/15 17:10:39 Done.
123 // The application could a WebAPK.
pkotwicz 2016/06/15 14:13:50 Nit: Remove line 123. I do not think that it makes
Xi Han 2016/06/15 17:10:39 Done.
124 ContextUtils.initApplicationContext(context);
121 125
122 mMainThread = new Thread(new Runnable() { 126 mMainThread = new Thread(new Runnable() {
123 @Override 127 @Override
124 @SuppressFBWarnings("DM_EXIT") 128 @SuppressFBWarnings("DM_EXIT")
125 public void run() { 129 public void run() {
126 try { 130 try {
127 // CommandLine must be initialized before everything else. 131 // CommandLine must be initialized before everything else.
128 synchronized (mMainThread) { 132 synchronized (mMainThread) {
129 while (mCommandLineParams == null) { 133 while (mCommandLineParams == null) {
130 mMainThread.wait(); 134 mMainThread.wait();
(...skipping 18 matching lines...) Expand all
149 } 153 }
150 } 154 }
151 boolean isLoaded = false; 155 boolean isLoaded = false;
152 if (CommandLine.getInstance().hasSwitch( 156 if (CommandLine.getInstance().hasSwitch(
153 BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER)) { 157 BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER)) {
154 android.os.Debug.waitForDebugger(); 158 android.os.Debug.waitForDebugger();
155 } 159 }
156 160
157 boolean loadAtFixedAddressFailed = false; 161 boolean loadAtFixedAddressFailed = false;
158 try { 162 try {
159 LibraryLoader.get(mLibraryProcessType).loadNow(getApplic ationContext()); 163 LibraryLoader.get(mLibraryProcessType).loadNow(hostBrows erContext);
160 isLoaded = true; 164 isLoaded = true;
161 } catch (ProcessInitException e) { 165 } catch (ProcessInitException e) {
162 if (requestedSharedRelro) { 166 if (requestedSharedRelro) {
163 Log.w(TAG, "Failed to load native library with share d RELRO, " 167 Log.w(TAG, "Failed to load native library with share d RELRO, "
164 + "retrying without"); 168 + "retrying without");
165 loadAtFixedAddressFailed = true; 169 loadAtFixedAddressFailed = true;
166 } else { 170 } else {
167 Log.e(TAG, "Failed to load native library", e); 171 Log.e(TAG, "Failed to load native library", e);
168 } 172 }
169 } 173 }
170 if (!isLoaded && requestedSharedRelro) { 174 if (!isLoaded && requestedSharedRelro) {
171 linker.disableSharedRelros(); 175 linker.disableSharedRelros();
172 try { 176 try {
173 LibraryLoader.get(mLibraryProcessType).loadNow(getAp plicationContext()); 177 LibraryLoader.get(mLibraryProcessType).loadNow(hostB rowserContext);
174 isLoaded = true; 178 isLoaded = true;
175 } catch (ProcessInitException e) { 179 } catch (ProcessInitException e) {
176 Log.e(TAG, "Failed to load native library on retry", e); 180 Log.e(TAG, "Failed to load native library on retry", e);
177 } 181 }
178 } 182 }
179 if (!isLoaded) { 183 if (!isLoaded) {
180 System.exit(-1); 184 System.exit(-1);
181 } 185 }
182 LibraryLoader.get(mLibraryProcessType) 186 LibraryLoader.get(mLibraryProcessType)
183 .registerRendererProcessHistogram(requestedSharedRel ro, 187 .registerRendererProcessHistogram(requestedSharedRel ro,
184 loadAtFixedAddressFailed); 188 loadAtFixedAddressFailed);
185 LibraryLoader.get(mLibraryProcessType).initialize(); 189 LibraryLoader.get(mLibraryProcessType).initialize();
186 synchronized (mMainThread) { 190 synchronized (mMainThread) {
187 mLibraryInitialized = true; 191 mLibraryInitialized = true;
188 mMainThread.notifyAll(); 192 mMainThread.notifyAll();
189 while (mFdInfos == null) { 193 while (mFdInfos == null) {
190 mMainThread.wait(); 194 mMainThread.wait();
191 } 195 }
192 } 196 }
193 for (FileDescriptorInfo fdInfo : mFdInfos) { 197 for (FileDescriptorInfo fdInfo : mFdInfos) {
194 nativeRegisterGlobalFileDescriptor( 198 nativeRegisterGlobalFileDescriptor(
195 fdInfo.mId, fdInfo.mFd.detachFd(), fdInfo.mOffse t, fdInfo.mSize); 199 fdInfo.mId, fdInfo.mFd.detachFd(), fdInfo.mOffse t, fdInfo.mSize);
196 } 200 }
197 nativeInitChildProcess(ChildProcessService.this, mCpuCount, mCpuFeatures); 201 nativeInitChildProcessImpl(ChildProcessServiceImpl.this, mCp uCount,
202 mCpuFeatures);
198 if (mActivitySemaphore.tryAcquire()) { 203 if (mActivitySemaphore.tryAcquire()) {
199 ContentMain.start(); 204 ContentMain.start();
200 nativeExitChildProcess(); 205 nativeExitChildProcess();
201 } 206 }
202 } catch (InterruptedException e) { 207 } catch (InterruptedException e) {
203 Log.w(TAG, "%s startup failed: %s", MAIN_THREAD_NAME, e); 208 Log.w(TAG, "%s startup failed: %s", MAIN_THREAD_NAME, e);
204 } catch (ProcessInitException e) { 209 } catch (ProcessInitException e) {
205 Log.w(TAG, "%s startup failed: %s", MAIN_THREAD_NAME, e); 210 Log.w(TAG, "%s startup failed: %s", MAIN_THREAD_NAME, e);
206 } 211 }
207 } 212 }
208 }, MAIN_THREAD_NAME); 213 }, MAIN_THREAD_NAME);
209 mMainThread.start(); 214 mMainThread.start();
210 } 215 }
211 216
212 @Override
213 @SuppressFBWarnings("DM_EXIT") 217 @SuppressFBWarnings("DM_EXIT")
214 public void onDestroy() { 218 public void destroy() {
215 Log.i(TAG, "Destroying ChildProcessService pid=%d", Process.myPid()); 219 Log.i(TAG, "Destroying ChildProcessService pid=%d", Process.myPid());
216 super.onDestroy();
217 if (mActivitySemaphore.tryAcquire()) { 220 if (mActivitySemaphore.tryAcquire()) {
218 // TODO(crbug.com/457406): This is a bit hacky, but there is no know n better solution 221 // TODO(crbug.com/457406): This is a bit hacky, but there is no know n better solution
219 // as this service will get reused (at least if not sandboxed). 222 // as this service will get reused (at least if not sandboxed).
220 // In fact, we might really want to always exit() from onDestroy(), not just from 223 // In fact, we might really want to always exit() from onDestroy(), not just from
221 // the early return here. 224 // the early return here.
222 System.exit(0); 225 System.exit(0);
223 return; 226 return;
224 } 227 }
225 synchronized (mMainThread) { 228 synchronized (mMainThread) {
226 try { 229 try {
227 while (!mLibraryInitialized) { 230 while (!mLibraryInitialized) {
228 // Avoid a potential race in calling through to native code before the library 231 // Avoid a potential race in calling through to native code before the library
229 // has loaded. 232 // has loaded.
230 mMainThread.wait(); 233 mMainThread.wait();
231 } 234 }
232 } catch (InterruptedException e) { 235 } catch (InterruptedException e) {
233 // Ignore 236 // Ignore
234 } 237 }
235 } 238 }
236 // Try to shutdown the MainThread gracefully, but it might not 239 // Try to shutdown the MainThread gracefully, but it might not
237 // have chance to exit normally. 240 // have chance to exit normally.
238 nativeShutdownMainThread(); 241 nativeShutdownMainThread();
239 } 242 }
240 243
241 @Override 244 public IBinder bind(Intent intent) {
242 public IBinder onBind(Intent intent) {
243 // We call stopSelf() to request that this service be stopped as soon as the client
244 // unbinds. Otherwise the system may keep it around and available for a reconnect. The
245 // child processes do not currently support reconnect; they must be init ialized from
246 // scratch every time.
247 stopSelf();
248 initializeParams(intent); 245 initializeParams(intent);
249 return mBinder; 246 return mBinder;
250 } 247 }
251 248
252 /** 249 /**
253 * Helper method to initialize the params from intent. 250 * Helper method to initialize the params from intent.
254 * @param intent Intent to launch the service. 251 * @param intent Intent to launch the service.
255 */ 252 */
256 protected void initializeParams(Intent intent) { 253 protected void initializeParams(Intent intent) {
257 synchronized (mMainThread) { 254 synchronized (mMainThread) {
258 mCommandLineParams = 255 mCommandLineParams =
259 intent.getStringArrayExtra(ChildProcessConstants.EXTRA_COMMA ND_LINE); 256 intent.getStringArrayExtra(ChildProcessConstants.EXTRA_COMMA ND_LINE);
260 // mLinkerParams is never used if Linker.isUsed() returns false. 257 // mLinkerParams is never used if Linker.isUsed() returns false.
261 // See onCreate(). 258 // See onCreate().
262 mLinkerParams = new ChromiumLinkerParams(intent); 259 mLinkerParams = new ChromiumLinkerParams(intent);
263 mLibraryProcessType = ChildProcessCreationParams.getLibraryProcessTy pe(intent); 260 mLibraryProcessType = ChildProcessCreationParams.getLibraryProcessTy pe(intent);
264 mIsBound = true; 261 mIsBound = true;
265 mMainThread.notifyAll(); 262 mMainThread.notifyAll();
266 } 263 }
267 } 264 }
268 265
269 /** 266 /**
270 * Helper method to get the information about the service from a given bundl e. 267 * Helper method to get the information about the service from a given bundl e.
271 * @param bundle Bundle that contains the information to start the service. 268 * @param bundle Bundle that contains the information to start the service.
272 */ 269 */
273 void getServiceInfo(Bundle bundle) { 270 void getServiceInfo(Bundle bundle) {
274 // Required to unparcel FileDescriptorInfo. 271 // Required to unparcel FileDescriptorInfo.
275 bundle.setClassLoader(getClassLoader()); 272 bundle.setClassLoader(mHostClassLoader);
276 synchronized (mMainThread) { 273 synchronized (mMainThread) {
277 // Allow the command line to be set via bind() intent or setupConnec tion, but 274 // Allow the command line to be set via bind() intent or setupConnec tion, but
278 // the FD can only be transferred here. 275 // the FD can only be transferred here.
279 if (mCommandLineParams == null) { 276 if (mCommandLineParams == null) {
280 mCommandLineParams = 277 mCommandLineParams =
281 bundle.getStringArray(ChildProcessConstants.EXTRA_COMMAN D_LINE); 278 bundle.getStringArray(ChildProcessConstants.EXTRA_COMMAN D_LINE);
282 } 279 }
283 // We must have received the command line by now 280 // We must have received the command line by now
284 assert mCommandLineParams != null; 281 assert mCommandLineParams != null;
285 mCpuCount = bundle.getInt(ChildProcessConstants.EXTRA_CPU_COUNT); 282 mCpuCount = bundle.getInt(ChildProcessConstants.EXTRA_CPU_COUNT);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 * This includes the IPC channel, the crash dump signals and resource relate d 417 * This includes the IPC channel, the crash dump signals and resource relate d
421 * files. 418 * files.
422 */ 419 */
423 private static native void nativeRegisterGlobalFileDescriptor( 420 private static native void nativeRegisterGlobalFileDescriptor(
424 int id, int fd, long offset, long size); 421 int id, int fd, long offset, long size);
425 422
426 /** 423 /**
427 * The main entry point for a child process. This should be called from a ne w thread since 424 * The main entry point for a child process. This should be called from a ne w thread since
428 * it will not return until the child process exits. See child_process_servi ce.{h,cc} 425 * it will not return until the child process exits. See child_process_servi ce.{h,cc}
429 * 426 *
430 * @param service The current ChildProcessService object. 427 * @param serviceImpl The current ChildProcessServiceImpl object.
431 * renderer. 428 * renderer.
432 */ 429 */
433 private static native void nativeInitChildProcess( 430 private static native void nativeInitChildProcessImpl(
434 ChildProcessService service, int cpuCount, long cpuFeatures); 431 ChildProcessServiceImpl serviceImpl, int cpuCount, long cpuFeatures) ;
435 432
436 /** 433 /**
437 * Force the child process to exit. 434 * Force the child process to exit.
438 */ 435 */
439 private static native void nativeExitChildProcess(); 436 private static native void nativeExitChildProcess();
440 437
441 private native void nativeShutdownMainThread(); 438 private native void nativeShutdownMainThread();
442 } 439 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698