f13ecac4583143e94b8371d5076d8126571b7874
[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 import android.util.Log;
9
10 import org.chromium.base.PathUtils;
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.base.library_loader.LibraryLoader;
13 import org.chromium.base.library_loader.ProcessInitException;
14 import org.chromium.content.browser.BrowserStartupController;
15 import org.chromium.media.MediaDrmBridge;
16
17 import java.util.UUID;
18
19 /**
20  * Wrapper for the steps needed to initialize the java and native sides of webview chromium.
21  */
22 public abstract class AwBrowserProcess {
23     public static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "webview";
24
25     private static final String TAG = "AwBrowserProcess";
26
27     /**
28      * Loads the native library, and performs basic static construction of objects needed
29      * to run webview in this process. Does not create threads; safe to call from zygote.
30      * Note: it is up to the caller to ensure this is only called once.
31      */
32     public static void loadLibrary() {
33         PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
34         try {
35             LibraryLoader.loadNow();
36         } catch (ProcessInitException e) {
37             throw new RuntimeException("Cannot load WebView", e);
38         }
39         // Switch the command line implementation from Java to native.
40         // It's okay for the WebView to do this before initialization because we have
41         // setup the JNI bindings by this point.
42         LibraryLoader.switchCommandLineForWebView();
43     }
44
45     /**
46      * Starts the chromium browser process running within this process. Creates threads
47      * and performs other per-app resource allocations; must not be called from zygote.
48      * Note: it is up to the caller to ensure this is only called once.
49      * @param context The Android application context
50      */
51     public static void start(final Context context) {
52         // We must post to the UI thread to cover the case that the user
53         // has invoked Chromium startup by using the (thread-safe)
54         // CookieManager rather than creating a WebView.
55         ThreadUtils.runOnUiThreadBlocking(new Runnable() {
56             @Override
57             public void run() {
58                 try {
59                     BrowserStartupController.get(context).startBrowserProcessesSync(true);
60                     initializePlatformKeySystem();
61                 } catch (ProcessInitException e) {
62                     throw new RuntimeException("Cannot initialize WebView", e);
63                 }
64             }
65         });
66     }
67
68     private static void initializePlatformKeySystem() {
69         String[] mappings = AwResource.getConfigKeySystemUuidMapping();
70         for (String mapping : mappings) {
71             try {
72                 String fragments[] = mapping.split(",");
73                 String keySystem = fragments[0].trim();
74                 UUID uuid = UUID.fromString(fragments[1]);
75                 MediaDrmBridge.addKeySystemUuidMapping(keySystem, uuid);
76             } catch (java.lang.RuntimeException e) {
77                 Log.e(TAG, "Can't parse key-system mapping: " + mapping);
78             }
79         }
80     }
81 }