| 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.android_webview; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | import android.content.SharedPreferences; |
| 9 | |
| 10 | import org.chromium.base.PathUtils; |
| 11 | import org.chromium.base.ThreadUtils; |
| 12 | import org.chromium.content.app.LibraryLoader; |
| 13 | import org.chromium.content.browser.AndroidBrowserProcess; |
| 14 | import org.chromium.content.common.ProcessInitException; |
| 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 | |
| 22 | /** |
| 23 | * Loads the native library, and performs basic static construction of objects needed |
| 24 | * to run webview in this process. Does not create threads; safe to call from zygote. |
| 25 | * Note: it is up to the caller to ensure this is only called once. |
| 26 | */ |
| 27 | public static void loadLibrary() { |
| 28 | PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); |
| 29 | try { |
| 30 | LibraryLoader.loadNow(); |
| 31 | } catch (ProcessInitException e) { |
| 32 | throw new RuntimeException("Cannot load WebView", e); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Starts the chromium browser process running within this process. Creates threads |
| 38 | * and performs other per-app resource allocations; must not be called from zygote. |
| 39 | * Note: it is up to the caller to ensure this is only called once. |
| 40 | * @param context The Android application context |
| 41 | */ |
| 42 | public static void start(final Context context) { |
| 43 | // We must post to the UI thread to cover the case that the user |
| 44 | // has invoked Chromium startup by using the (thread-safe) |
| 45 | // CookieManager rather than creating a WebView. |
| 46 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 47 | @Override |
| 48 | public void run() { |
| 49 | try { |
| 50 | LibraryLoader.ensureInitialized(); |
| 51 | AndroidBrowserProcess.init(context, |
| 52 | AndroidBrowserProcess.MAX_RENDERERS_SINGLE_PROCESS); |
| 53 | } catch (ProcessInitException e) { |
| 54 | throw new RuntimeException("Cannot initialize WebView", e); |
| 55 | } |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | } |