Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / android_webview / java / src / org / chromium / android_webview / AwBrowserProcess.java
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
3 // found in the LICENSE file.
4
5 package org.chromium.android_webview;
6
7 import android.content.Context;
8
9 import org.chromium.base.PathUtils;
10 import org.chromium.base.ThreadUtils;
11 import org.chromium.base.library_loader.LibraryLoader;
12 import org.chromium.base.library_loader.ProcessInitException;
13 import org.chromium.content.browser.BrowserStartupController;
14
15
16 /**
17  * Wrapper for the steps needed to initialize the java and native sides of webview chromium.
18  */
19 public abstract class AwBrowserProcess {
20     private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "webview";
21     private static final String TAG = "AwBrowserProcess";
22
23     /**
24      * Loads the native library, and performs basic static construction of objects needed
25      * to run webview in this process. Does not create threads; safe to call from zygote.
26      * Note: it is up to the caller to ensure this is only called once.
27      */
28     public static void loadLibrary() {
29         PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
30         try {
31             LibraryLoader.loadNow();
32         } catch (ProcessInitException e) {
33             throw new RuntimeException("Cannot load WebView", e);
34         }
35     }
36
37     /**
38      * Starts the chromium browser process running within this process. Creates threads
39      * and performs other per-app resource allocations; must not be called from zygote.
40      * Note: it is up to the caller to ensure this is only called once.
41      * @param context The Android application context
42      */
43     public static void start(final Context context) {
44         // We must post to the UI thread to cover the case that the user
45         // has invoked Chromium startup by using the (thread-safe)
46         // CookieManager rather than creating a WebView.
47         ThreadUtils.runOnUiThreadBlocking(new Runnable() {
48             @Override
49             public void run() {
50                 try {
51                     BrowserStartupController.get(context).startBrowserProcessesSync(
52                                 BrowserStartupController.MAX_RENDERERS_SINGLE_PROCESS);
53                 } catch (ProcessInitException e) {
54                     throw new RuntimeException("Cannot initialize WebView", e);
55                 }
56             }
57         });
58     }
59 }