OLD | NEW |
---|---|
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 Loading... | |
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 ClassLoader for the host browser context. | |
103 private ClassLoader mHostClassLoader; | |
Maria
2016/06/17 17:09:24
final?
Xi Han
2016/06/17 18:35:20
It is still because |mHostClassLoader| isn't initi
| |
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. |
Maria
2016/06/17 17:09:24
nit: Iampl -> Impl
Xi Han
2016/06/17 18:35:20
Sorry.
| |
111 * @param context The application context. | |
112 * @param hostBrowserContext The context of the host browser (i.e. Chrome). | |
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 that owns this ChildProces sServiceImpl object. |
123 ContextUtils.initApplicationContext(context); | |
121 | 124 |
122 mMainThread = new Thread(new Runnable() { | 125 mMainThread = new Thread(new Runnable() { |
123 @Override | 126 @Override |
124 @SuppressFBWarnings("DM_EXIT") | 127 @SuppressFBWarnings("DM_EXIT") |
125 public void run() { | 128 public void run() { |
126 try { | 129 try { |
127 // CommandLine must be initialized before everything else. | 130 // CommandLine must be initialized before everything else. |
128 synchronized (mMainThread) { | 131 synchronized (mMainThread) { |
129 while (mCommandLineParams == null) { | 132 while (mCommandLineParams == null) { |
130 mMainThread.wait(); | 133 mMainThread.wait(); |
(...skipping 18 matching lines...) Expand all Loading... | |
149 } | 152 } |
150 } | 153 } |
151 boolean isLoaded = false; | 154 boolean isLoaded = false; |
152 if (CommandLine.getInstance().hasSwitch( | 155 if (CommandLine.getInstance().hasSwitch( |
153 BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER)) { | 156 BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER)) { |
154 android.os.Debug.waitForDebugger(); | 157 android.os.Debug.waitForDebugger(); |
155 } | 158 } |
156 | 159 |
157 boolean loadAtFixedAddressFailed = false; | 160 boolean loadAtFixedAddressFailed = false; |
158 try { | 161 try { |
159 LibraryLoader.get(mLibraryProcessType).loadNow(getApplic ationContext()); | 162 LibraryLoader.get(mLibraryProcessType).loadNow(hostBrows erContext); |
160 isLoaded = true; | 163 isLoaded = true; |
161 } catch (ProcessInitException e) { | 164 } catch (ProcessInitException e) { |
162 if (requestedSharedRelro) { | 165 if (requestedSharedRelro) { |
163 Log.w(TAG, "Failed to load native library with share d RELRO, " | 166 Log.w(TAG, "Failed to load native library with share d RELRO, " |
164 + "retrying without"); | 167 + "retrying without"); |
165 loadAtFixedAddressFailed = true; | 168 loadAtFixedAddressFailed = true; |
166 } else { | 169 } else { |
167 Log.e(TAG, "Failed to load native library", e); | 170 Log.e(TAG, "Failed to load native library", e); |
168 } | 171 } |
169 } | 172 } |
170 if (!isLoaded && requestedSharedRelro) { | 173 if (!isLoaded && requestedSharedRelro) { |
171 linker.disableSharedRelros(); | 174 linker.disableSharedRelros(); |
172 try { | 175 try { |
173 LibraryLoader.get(mLibraryProcessType).loadNow(getAp plicationContext()); | 176 LibraryLoader.get(mLibraryProcessType).loadNow(hostB rowserContext); |
174 isLoaded = true; | 177 isLoaded = true; |
175 } catch (ProcessInitException e) { | 178 } catch (ProcessInitException e) { |
176 Log.e(TAG, "Failed to load native library on retry", e); | 179 Log.e(TAG, "Failed to load native library on retry", e); |
177 } | 180 } |
178 } | 181 } |
179 if (!isLoaded) { | 182 if (!isLoaded) { |
180 System.exit(-1); | 183 System.exit(-1); |
181 } | 184 } |
182 LibraryLoader.get(mLibraryProcessType) | 185 LibraryLoader.get(mLibraryProcessType) |
183 .registerRendererProcessHistogram(requestedSharedRel ro, | 186 .registerRendererProcessHistogram(requestedSharedRel ro, |
184 loadAtFixedAddressFailed); | 187 loadAtFixedAddressFailed); |
185 LibraryLoader.get(mLibraryProcessType).initialize(); | 188 LibraryLoader.get(mLibraryProcessType).initialize(); |
186 synchronized (mMainThread) { | 189 synchronized (mMainThread) { |
187 mLibraryInitialized = true; | 190 mLibraryInitialized = true; |
188 mMainThread.notifyAll(); | 191 mMainThread.notifyAll(); |
189 while (mFdInfos == null) { | 192 while (mFdInfos == null) { |
190 mMainThread.wait(); | 193 mMainThread.wait(); |
191 } | 194 } |
192 } | 195 } |
193 for (FileDescriptorInfo fdInfo : mFdInfos) { | 196 for (FileDescriptorInfo fdInfo : mFdInfos) { |
194 nativeRegisterGlobalFileDescriptor( | 197 nativeRegisterGlobalFileDescriptor( |
195 fdInfo.mId, fdInfo.mFd.detachFd(), fdInfo.mOffse t, fdInfo.mSize); | 198 fdInfo.mId, fdInfo.mFd.detachFd(), fdInfo.mOffse t, fdInfo.mSize); |
196 } | 199 } |
197 nativeInitChildProcess(ChildProcessService.this, mCpuCount, mCpuFeatures); | 200 nativeInitChildProcessImpl(ChildProcessServiceImpl.this, mCp uCount, |
201 mCpuFeatures); | |
198 if (mActivitySemaphore.tryAcquire()) { | 202 if (mActivitySemaphore.tryAcquire()) { |
199 ContentMain.start(); | 203 ContentMain.start(); |
200 nativeExitChildProcess(); | 204 nativeExitChildProcess(); |
201 } | 205 } |
202 } catch (InterruptedException e) { | 206 } catch (InterruptedException e) { |
203 Log.w(TAG, "%s startup failed: %s", MAIN_THREAD_NAME, e); | 207 Log.w(TAG, "%s startup failed: %s", MAIN_THREAD_NAME, e); |
204 } catch (ProcessInitException e) { | 208 } catch (ProcessInitException e) { |
205 Log.w(TAG, "%s startup failed: %s", MAIN_THREAD_NAME, e); | 209 Log.w(TAG, "%s startup failed: %s", MAIN_THREAD_NAME, e); |
206 } | 210 } |
207 } | 211 } |
208 }, MAIN_THREAD_NAME); | 212 }, MAIN_THREAD_NAME); |
209 mMainThread.start(); | 213 mMainThread.start(); |
210 } | 214 } |
211 | 215 |
212 @Override | |
213 @SuppressFBWarnings("DM_EXIT") | 216 @SuppressFBWarnings("DM_EXIT") |
214 public void onDestroy() { | 217 public void destroy() { |
215 Log.i(TAG, "Destroying ChildProcessService pid=%d", Process.myPid()); | 218 Log.i(TAG, "Destroying ChildProcessService pid=%d", Process.myPid()); |
216 super.onDestroy(); | |
217 if (mActivitySemaphore.tryAcquire()) { | 219 if (mActivitySemaphore.tryAcquire()) { |
218 // TODO(crbug.com/457406): This is a bit hacky, but there is no know n better solution | 220 // 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). | 221 // 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 | 222 // In fact, we might really want to always exit() from onDestroy(), not just from |
221 // the early return here. | 223 // the early return here. |
222 System.exit(0); | 224 System.exit(0); |
223 return; | 225 return; |
224 } | 226 } |
225 synchronized (mMainThread) { | 227 synchronized (mMainThread) { |
226 try { | 228 try { |
227 while (!mLibraryInitialized) { | 229 while (!mLibraryInitialized) { |
228 // Avoid a potential race in calling through to native code before the library | 230 // Avoid a potential race in calling through to native code before the library |
229 // has loaded. | 231 // has loaded. |
230 mMainThread.wait(); | 232 mMainThread.wait(); |
231 } | 233 } |
232 } catch (InterruptedException e) { | 234 } catch (InterruptedException e) { |
233 // Ignore | 235 // Ignore |
234 } | 236 } |
235 } | 237 } |
236 // Try to shutdown the MainThread gracefully, but it might not | 238 // Try to shutdown the MainThread gracefully, but it might not |
237 // have chance to exit normally. | 239 // have chance to exit normally. |
238 nativeShutdownMainThread(); | 240 nativeShutdownMainThread(); |
239 } | 241 } |
240 | 242 |
241 @Override | 243 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); | 244 initializeParams(intent); |
249 return mBinder; | 245 return mBinder; |
250 } | 246 } |
251 | 247 |
252 /** | 248 /** |
253 * Helper method to initialize the params from intent. | 249 * Helper method to initialize the params from intent. |
254 * @param intent Intent to launch the service. | 250 * @param intent Intent to launch the service. |
255 */ | 251 */ |
256 protected void initializeParams(Intent intent) { | 252 protected void initializeParams(Intent intent) { |
257 synchronized (mMainThread) { | 253 synchronized (mMainThread) { |
258 mCommandLineParams = | 254 mCommandLineParams = |
259 intent.getStringArrayExtra(ChildProcessConstants.EXTRA_COMMA ND_LINE); | 255 intent.getStringArrayExtra(ChildProcessConstants.EXTRA_COMMA ND_LINE); |
260 // mLinkerParams is never used if Linker.isUsed() returns false. | 256 // mLinkerParams is never used if Linker.isUsed() returns false. |
261 // See onCreate(). | 257 // See onCreate(). |
262 mLinkerParams = new ChromiumLinkerParams(intent); | 258 mLinkerParams = new ChromiumLinkerParams(intent); |
263 mLibraryProcessType = ChildProcessCreationParams.getLibraryProcessTy pe(intent); | 259 mLibraryProcessType = ChildProcessCreationParams.getLibraryProcessTy pe(intent); |
264 mIsBound = true; | 260 mIsBound = true; |
265 mMainThread.notifyAll(); | 261 mMainThread.notifyAll(); |
266 } | 262 } |
267 } | 263 } |
268 | 264 |
269 /** | 265 /** |
270 * Helper method to get the information about the service from a given bundl e. | 266 * 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. | 267 * @param bundle Bundle that contains the information to start the service. |
272 */ | 268 */ |
273 void getServiceInfo(Bundle bundle) { | 269 void getServiceInfo(Bundle bundle) { |
274 // Required to unparcel FileDescriptorInfo. | 270 // Required to unparcel FileDescriptorInfo. |
275 bundle.setClassLoader(getClassLoader()); | 271 bundle.setClassLoader(mHostClassLoader); |
276 synchronized (mMainThread) { | 272 synchronized (mMainThread) { |
277 // Allow the command line to be set via bind() intent or setupConnec tion, but | 273 // Allow the command line to be set via bind() intent or setupConnec tion, but |
278 // the FD can only be transferred here. | 274 // the FD can only be transferred here. |
279 if (mCommandLineParams == null) { | 275 if (mCommandLineParams == null) { |
280 mCommandLineParams = | 276 mCommandLineParams = |
281 bundle.getStringArray(ChildProcessConstants.EXTRA_COMMAN D_LINE); | 277 bundle.getStringArray(ChildProcessConstants.EXTRA_COMMAN D_LINE); |
282 } | 278 } |
283 // We must have received the command line by now | 279 // We must have received the command line by now |
284 assert mCommandLineParams != null; | 280 assert mCommandLineParams != null; |
285 mCpuCount = bundle.getInt(ChildProcessConstants.EXTRA_CPU_COUNT); | 281 mCpuCount = bundle.getInt(ChildProcessConstants.EXTRA_CPU_COUNT); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 * This includes the IPC channel, the crash dump signals and resource relate d | 416 * This includes the IPC channel, the crash dump signals and resource relate d |
421 * files. | 417 * files. |
422 */ | 418 */ |
423 private static native void nativeRegisterGlobalFileDescriptor( | 419 private static native void nativeRegisterGlobalFileDescriptor( |
424 int id, int fd, long offset, long size); | 420 int id, int fd, long offset, long size); |
425 | 421 |
426 /** | 422 /** |
427 * The main entry point for a child process. This should be called from a ne w thread since | 423 * 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} | 424 * it will not return until the child process exits. See child_process_servi ce.{h,cc} |
429 * | 425 * |
430 * @param service The current ChildProcessService object. | 426 * @param serviceImpl The current ChildProcessServiceImpl object. |
431 * renderer. | 427 * renderer. |
432 */ | 428 */ |
433 private static native void nativeInitChildProcess( | 429 private static native void nativeInitChildProcessImpl( |
434 ChildProcessService service, int cpuCount, long cpuFeatures); | 430 ChildProcessServiceImpl serviceImpl, int cpuCount, long cpuFeatures) ; |
435 | 431 |
436 /** | 432 /** |
437 * Force the child process to exit. | 433 * Force the child process to exit. |
438 */ | 434 */ |
439 private static native void nativeExitChildProcess(); | 435 private static native void nativeExitChildProcess(); |
440 | 436 |
441 private native void nativeShutdownMainThread(); | 437 private native void nativeShutdownMainThread(); |
442 } | 438 } |
OLD | NEW |