OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.content.browser; |
| 6 |
| 7 import android.app.ActivityManager; |
| 8 import android.content.Context; |
| 9 import android.content.res.Resources; |
| 10 import android.util.Log; |
| 11 |
| 12 import org.chromium.content.app.ContentMain; |
| 13 import org.chromium.content.browser.ContentView; |
| 14 import org.chromium.content.browser.LibraryLoader; |
| 15 |
| 16 // NOTE: This file hasn't been fully upstreamed, please don't merge to downstrea
m. |
| 17 public class AndroidBrowserProcess { |
| 18 |
| 19 private static final String TAG = "BrowserProcessMain"; |
| 20 |
| 21 // Prevents initializing the process more than once. |
| 22 private static boolean sInitialized = false; |
| 23 |
| 24 // Computes the actual max renderer processes used. |
| 25 private static int normalizeMaxRendererProcesses(Context context, int maxRen
dererProcesses) { |
| 26 if (maxRendererProcesses == MAX_RENDERERS_AUTOMATIC) { |
| 27 // We use the device's memory class to decide the maximum renderer |
| 28 // processes. For the baseline devices the memory class is 16 and we
will |
| 29 // limit it to one render process. For the devices with memory class
24, |
| 30 // we allow two render processes. |
| 31 ActivityManager am = (ActivityManager)context.getSystemService( |
| 32 Context.ACTIVITY_SERVICE); |
| 33 maxRendererProcesses = Math.max(((am.getMemoryClass() - 8) / 8), 1); |
| 34 } |
| 35 if (maxRendererProcesses > MAX_RENDERERS_LIMIT) { |
| 36 Log.w(TAG, "Excessive maxRendererProcesses value: " + maxRendererPro
cesses); |
| 37 return MAX_RENDERERS_LIMIT; |
| 38 } |
| 39 return Math.max(0, maxRendererProcesses); |
| 40 } |
| 41 |
| 42 // Automatically decide the number of renderer processes to use based on dev
ice memory class. |
| 43 public static final int MAX_RENDERERS_AUTOMATIC = -1; |
| 44 // Use single-process mode that runs the renderer on a separate thread in th
e main application. |
| 45 public static final int MAX_RENDERERS_SINGLE_PROCESS = 0; |
| 46 // Cap on the maximum number of renderer processes that can be requested. |
| 47 public static final int MAX_RENDERERS_LIMIT = 3; // TODO(tedbo): Raise limi
t |
| 48 |
| 49 /** |
| 50 * Initialize the process as a ContentView host. This must be called from th
e main UI thread. |
| 51 * This should be called by the ContentView constructor to prepare this proc
ess for ContentView |
| 52 * use outside of the browser. In the case where ContentView is used in the
browser then |
| 53 * initBrowserProcess() should already have been called and this is a no-op. |
| 54 * |
| 55 * @param context Context used to obtain the application context. |
| 56 * @param maxRendererProcesses See ContentView.enableMultiProcess(). |
| 57 */ |
| 58 public static void initContentViewProcess(Context context, int maxRendererPr
ocesses) { |
| 59 genericChromiumProcessInit(context, maxRendererProcesses, false); |
| 60 } |
| 61 |
| 62 /** |
| 63 * Initialize the platform browser process. This must be called from the mai
n UI thread before |
| 64 * accessing ContentView in order to treat this as a browser process. |
| 65 * |
| 66 * @param context Context used to obtain the application context. |
| 67 * @param maxRendererProcesses See ContentView.enableMultiProcess(). |
| 68 */ |
| 69 public static void initChromiumBrowserProcess(Context context, int maxRender
erProcesses) { |
| 70 genericChromiumProcessInit(context, maxRendererProcesses, true); |
| 71 } |
| 72 |
| 73 /** |
| 74 * Shared implementation for the initXxx methods. |
| 75 * @param context Context used to obtain the application context |
| 76 * @param maxRendererProcesses See ContentView.enableMultiProcess() |
| 77 * @param hostIsChrome pass true if running as the system browser process. |
| 78 */ |
| 79 private static void genericChromiumProcessInit(Context context, int maxRende
rerProcesses, |
| 80 boolean hostIsChrome) { |
| 81 if (sInitialized) { |
| 82 return; |
| 83 } |
| 84 sInitialized = true; |
| 85 |
| 86 // Normally Main.java will have already loaded the library asynchronousl
y, we only |
| 87 // need to load it here if we arrived via another flow, e.g. bookmark ac
cess & sync setup. |
| 88 LibraryLoader.loadAndInitSync(); |
| 89 |
| 90 Context appContext = context.getApplicationContext(); |
| 91 |
| 92 int maxRenderers = normalizeMaxRendererProcesses(appContext, maxRenderer
Processes); |
| 93 Log.i(TAG, "Initializing chromium process, renderers=" + maxRenderers + |
| 94 " hostIsChrome=" + hostIsChrome); |
| 95 |
| 96 nativeSetCommandLineFlags(maxRenderers, getPlugins(context)); |
| 97 ContentMain.initApplicationContext(appContext); |
| 98 ContentMain.start(); |
| 99 } |
| 100 |
| 101 private static String getPlugins(final Context context) { |
| 102 return ""; |
| 103 } |
| 104 |
| 105 private static native void nativeSetCommandLineFlags( |
| 106 int maxRenderProcesses, String plugin_descriptor); |
| 107 |
| 108 // Is this an official build of Chrome? Only native code knows |
| 109 // for sure. Official build knowledge is needed very early in |
| 110 // process startup. |
| 111 private static native boolean nativeIsOfficialBuild(); |
| 112 } |
OLD | NEW |