| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Handler; | 8 import android.os.Handler; |
| 9 import android.util.Log; | 9 import android.util.Log; |
| 10 | 10 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // initialization has been started, since it is not needed anymore. This is
to ensure the | 87 // initialization has been started, since it is not needed anymore. This is
to ensure the |
| 88 // context is not leaked. | 88 // context is not leaked. |
| 89 private final Context mContext; | 89 private final Context mContext; |
| 90 | 90 |
| 91 // Whether the async startup of the browser process has started. | 91 // Whether the async startup of the browser process has started. |
| 92 private boolean mHasStartedInitializingBrowserProcess; | 92 private boolean mHasStartedInitializingBrowserProcess; |
| 93 | 93 |
| 94 // Whether the async startup of the browser process is complete. | 94 // Whether the async startup of the browser process is complete. |
| 95 private boolean mStartupDone; | 95 private boolean mStartupDone; |
| 96 | 96 |
| 97 // Use single-process mode that runs the renderer on a separate thread in | |
| 98 // the main application. | |
| 99 public static final int MAX_RENDERERS_SINGLE_PROCESS = 0; | |
| 100 | |
| 101 // Cap on the maximum number of renderer processes that can be requested. | |
| 102 // This is currently set to account for: | |
| 103 // 13: The maximum number of sandboxed processes we have available | |
| 104 // - 1: The regular New Tab Page | |
| 105 // - 1: The incognito New Tab Page | |
| 106 // - 1: A regular incognito tab | |
| 107 // - 1: Safety buffer (http://crbug.com/251279) | |
| 108 public static final int MAX_RENDERERS_LIMIT = | |
| 109 ChildProcessLauncher.MAX_REGISTERED_SANDBOXED_SERVICES - 4; | |
| 110 | |
| 111 // This field is set after startup has been completed based on whether the s
tartup was a success | 97 // This field is set after startup has been completed based on whether the s
tartup was a success |
| 112 // or not. It is used when later requests to startup come in that happen aft
er the initial set | 98 // or not. It is used when later requests to startup come in that happen aft
er the initial set |
| 113 // of enqueued callbacks have been executed. | 99 // of enqueued callbacks have been executed. |
| 114 private boolean mStartupSuccess; | 100 private boolean mStartupSuccess; |
| 115 | 101 |
| 116 BrowserStartupController(Context context) { | 102 BrowserStartupController(Context context) { |
| 117 mContext = context; | 103 mContext = context; |
| 118 mAsyncStartupCallbacks = new ArrayList<StartupCallback>(); | 104 mAsyncStartupCallbacks = new ArrayList<StartupCallback>(); |
| 119 } | 105 } |
| 120 | 106 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 } | 140 } |
| 155 | 141 |
| 156 // Browser process has not been fully started yet, so we defer executing
the callback. | 142 // Browser process has not been fully started yet, so we defer executing
the callback. |
| 157 mAsyncStartupCallbacks.add(callback); | 143 mAsyncStartupCallbacks.add(callback); |
| 158 | 144 |
| 159 if (!mHasStartedInitializingBrowserProcess) { | 145 if (!mHasStartedInitializingBrowserProcess) { |
| 160 // This is the first time we have been asked to start the browser pr
ocess. We set the | 146 // This is the first time we have been asked to start the browser pr
ocess. We set the |
| 161 // flag that indicates that we have kicked off starting the browser
process. | 147 // flag that indicates that we have kicked off starting the browser
process. |
| 162 mHasStartedInitializingBrowserProcess = true; | 148 mHasStartedInitializingBrowserProcess = true; |
| 163 | 149 |
| 164 prepareToStartBrowserProcess(MAX_RENDERERS_LIMIT); | 150 prepareToStartBrowserProcess(false); |
| 165 | 151 |
| 166 setAsynchronousStartup(true); | 152 setAsynchronousStartup(true); |
| 167 if (contentStart() > 0) { | 153 if (contentStart() > 0) { |
| 168 // Failed. The callbacks may not have run, so run them. | 154 // Failed. The callbacks may not have run, so run them. |
| 169 enqueueCallbackExecution(STARTUP_FAILURE, NOT_ALREADY_STARTED); | 155 enqueueCallbackExecution(STARTUP_FAILURE, NOT_ALREADY_STARTED); |
| 170 } | 156 } |
| 171 } | 157 } |
| 172 } | 158 } |
| 173 | 159 |
| 174 /** | 160 /** |
| 175 * Start the browser process synchronously. If the browser is already being
started | 161 * Start the browser process synchronously. If the browser is already being
started |
| 176 * asynchronously then complete startup synchronously | 162 * asynchronously then complete startup synchronously |
| 177 * | 163 * |
| 178 * <p/> | 164 * <p/> |
| 179 * Note that this can only be called on the UI thread. | 165 * Note that this can only be called on the UI thread. |
| 180 * | 166 * |
| 181 * @param maxRenderers The maximum number of renderer processes the browser
may | 167 * @param inProcess true iff the browser should run single-process, ie. keep
renderers in the |
| 182 * create. Zero for single process mode. | 168 * browser process |
| 183 * @throws ProcessInitException | 169 * @throws ProcessInitException |
| 184 */ | 170 */ |
| 185 public void startBrowserProcessesSync(int maxRenderers) throws ProcessInitEx
ception { | 171 public void startBrowserProcessesSync(boolean inProcess) throws ProcessInitE
xception { |
| 186 // If already started skip to checking the result | 172 // If already started skip to checking the result |
| 187 if (!mStartupDone) { | 173 if (!mStartupDone) { |
| 188 if (!mHasStartedInitializingBrowserProcess) { | 174 if (!mHasStartedInitializingBrowserProcess) { |
| 189 prepareToStartBrowserProcess(maxRenderers); | 175 prepareToStartBrowserProcess(inProcess); |
| 190 } | 176 } |
| 191 | 177 |
| 192 setAsynchronousStartup(false); | 178 setAsynchronousStartup(false); |
| 193 if (contentStart() > 0) { | 179 if (contentStart() > 0) { |
| 194 // Failed. The callbacks may not have run, so run them. | 180 // Failed. The callbacks may not have run, so run them. |
| 195 enqueueCallbackExecution(STARTUP_FAILURE, NOT_ALREADY_STARTED); | 181 enqueueCallbackExecution(STARTUP_FAILURE, NOT_ALREADY_STARTED); |
| 196 } | 182 } |
| 197 } | 183 } |
| 198 | 184 |
| 199 // Startup should now be complete | 185 // Startup should now be complete |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 if (mStartupSuccess) { | 239 if (mStartupSuccess) { |
| 254 callback.onSuccess(ALREADY_STARTED); | 240 callback.onSuccess(ALREADY_STARTED); |
| 255 } else { | 241 } else { |
| 256 callback.onFailure(); | 242 callback.onFailure(); |
| 257 } | 243 } |
| 258 } | 244 } |
| 259 }); | 245 }); |
| 260 } | 246 } |
| 261 | 247 |
| 262 @VisibleForTesting | 248 @VisibleForTesting |
| 263 void prepareToStartBrowserProcess(int maxRendererProcesses) throws ProcessIn
itException { | 249 void prepareToStartBrowserProcess(boolean inProcess) throws ProcessInitExcep
tion { |
| 264 Log.i(TAG, "Initializing chromium process, renderers=" + maxRendererProc
esses); | 250 Log.i(TAG, "Initializing chromium process, inProcess=" + inProcess); |
| 265 | 251 |
| 266 // Normally Main.java will have kicked this off asynchronously for Chrom
e. But other | 252 // Normally Main.java will have kicked this off asynchronously for Chrom
e. But other |
| 267 // ContentView apps like tests also need them so we make sure we've extr
acted resources | 253 // ContentView apps like tests also need them so we make sure we've extr
acted resources |
| 268 // here. We can still make it a little async (wait until the library is
loaded). | 254 // here. We can still make it a little async (wait until the library is
loaded). |
| 269 ResourceExtractor resourceExtractor = ResourceExtractor.get(mContext); | 255 ResourceExtractor resourceExtractor = ResourceExtractor.get(mContext); |
| 270 resourceExtractor.startExtractingResources(); | 256 resourceExtractor.startExtractingResources(); |
| 271 | 257 |
| 272 // Normally Main.java will have already loaded the library asynchronousl
y, we only need | 258 // Normally Main.java will have already loaded the library asynchronousl
y, we only need |
| 273 // to load it here if we arrived via another flow, e.g. bookmark access
& sync setup. | 259 // to load it here if we arrived via another flow, e.g. bookmark access
& sync setup. |
| 274 LibraryLoader.ensureInitialized(mContext, true); | 260 LibraryLoader.ensureInitialized(mContext, true); |
| 275 | 261 |
| 276 // TODO(yfriedman): Remove dependency on a command line flag for this. | 262 // TODO(yfriedman): Remove dependency on a command line flag for this. |
| 277 DeviceUtils.addDeviceSpecificUserAgentSwitch(mContext); | 263 DeviceUtils.addDeviceSpecificUserAgentSwitch(mContext); |
| 278 | 264 |
| 279 Context appContext = mContext.getApplicationContext(); | 265 Context appContext = mContext.getApplicationContext(); |
| 280 // Now we really need to have the resources ready. | 266 // Now we really need to have the resources ready. |
| 281 resourceExtractor.waitForCompletion(); | 267 resourceExtractor.waitForCompletion(); |
| 282 | 268 |
| 283 nativeSetCommandLineFlags(maxRendererProcesses, | 269 nativeSetCommandLineFlags(inProcess, nativeIsPluginEnabled() ? getPlugin
s() : null); |
| 284 nativeIsPluginEnabled() ? getPlugins() : null); | |
| 285 ContentMain.initApplicationContext(appContext); | 270 ContentMain.initApplicationContext(appContext); |
| 286 } | 271 } |
| 287 | 272 |
| 288 /** | 273 /** |
| 289 * Initialization needed for tests. Mainly used by content browsertests. | 274 * Initialization needed for tests. Mainly used by content browsertests. |
| 290 */ | 275 */ |
| 291 public void initChromiumBrowserProcessForTests() { | 276 public void initChromiumBrowserProcessForTests() { |
| 292 ResourceExtractor resourceExtractor = ResourceExtractor.get(mContext); | 277 ResourceExtractor resourceExtractor = ResourceExtractor.get(mContext); |
| 293 resourceExtractor.startExtractingResources(); | 278 resourceExtractor.startExtractingResources(); |
| 294 resourceExtractor.waitForCompletion(); | 279 resourceExtractor.waitForCompletion(); |
| 295 | 280 nativeSetCommandLineFlags(false, null); |
| 296 // Having a single renderer should be sufficient for tests. We can't hav
e more than | |
| 297 // MAX_RENDERERS_LIMIT. | |
| 298 nativeSetCommandLineFlags(1 /* maxRenderers */, null); | |
| 299 } | 281 } |
| 300 | 282 |
| 301 private String getPlugins() { | 283 private String getPlugins() { |
| 302 return PepperPluginManager.getPlugins(mContext); | 284 return PepperPluginManager.getPlugins(mContext); |
| 303 } | 285 } |
| 304 | 286 |
| 305 private static native void nativeSetCommandLineFlags(int maxRenderProcesses, | 287 private static native void nativeSetCommandLineFlags( |
| 306 String pluginDescriptor); | 288 boolean inProcess, String pluginDescriptor); |
| 307 | 289 |
| 308 // Is this an official build of Chrome? Only native code knows for sure. Off
icial build | 290 // Is this an official build of Chrome? Only native code knows for sure. Off
icial build |
| 309 // knowledge is needed very early in process startup. | 291 // knowledge is needed very early in process startup. |
| 310 private static native boolean nativeIsOfficialBuild(); | 292 private static native boolean nativeIsOfficialBuild(); |
| 311 | 293 |
| 312 private static native boolean nativeIsPluginEnabled(); | 294 private static native boolean nativeIsPluginEnabled(); |
| 313 } | 295 } |
| OLD | NEW |