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