Upstream version 5.34.97.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkViewDelegate.java
1 // Copyright (c) 2013 Intel Corporation. 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.xwalk.core;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.os.Build;
10
11 import org.chromium.base.ActivityStatus;
12 import org.chromium.base.CommandLine;
13 import org.chromium.base.PathUtils;
14 import org.chromium.base.ThreadUtils;
15 import org.chromium.content.app.LibraryLoader;
16 import org.chromium.content.browser.BrowserStartupController;
17 import org.chromium.content.browser.DeviceUtils;
18 import org.chromium.content.browser.ResourceExtractor;
19 import org.chromium.content.common.ProcessInitException;
20
21 class XWalkViewDelegate {
22     private static boolean sInitialized = false;
23     private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "xwalkcore";
24     private static final String[] MANDATORY_PAKS = {
25             "xwalk.pak", "en-US.pak" };
26     private static final String[] MANDATORY_LIBRARIES = {
27             "libxwalkcore.so"
28     };
29
30     public static void init(XWalkView xwalkView) {
31         if (sInitialized) {
32             return;
33         }
34
35         // Initialize the ActivityStatus. This is needed and used by many internal
36         // features such as location provider to listen to activity status.
37         ActivityStatus.initialize(xwalkView.getActivity().getApplication());
38
39         Context context = xwalkView.getViewContext();
40
41         // Last place to initialize CommandLine object. If you haven't initialize
42         // the CommandLine object before XWalkViewContent is created, here will create
43         // the object to guarantee the CommandLine object is not null and the
44         // consequent prodedure does not crash.
45         if (!CommandLine.isInitialized())
46             CommandLine.init(null);
47
48         // If context's applicationContext is not the same package with itself,
49         // It's a cross package invoking, load core library from library apk.
50         // Only load the native library from /data/data if the Android version is
51         // lower than 4.2. Android enables a system path /data/app-lib to store native
52         // libraries starting from 4.2 and load them automatically.
53         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 &&
54                 !context.getApplicationContext().getPackageName().equals(context.getPackageName())) {
55             try {
56                 for (String library : MANDATORY_LIBRARIES) {
57                     System.load("/data/data/" + context.getPackageName() + "/lib/" + library);
58                 }
59             } catch (UnsatisfiedLinkError e) {
60                 throw new RuntimeException("Cannot initialize Crosswalk Core", e);
61             }
62         }
63         loadLibrary();
64         DeviceUtils.addDeviceSpecificUserAgentSwitch(context);
65
66         ResourceExtractor.setMandatoryPaksToExtract(MANDATORY_PAKS);
67         ResourceExtractor.setExtractImplicitLocaleForTesting(false);
68         // Use MixedContext to initialize the ResourceExtractor, as the pak file
69         // is in the library apk if in shared apk mode.
70         ResourceExtractor.get(context);
71
72         startBrowserProcess(context);
73         sInitialized = true;
74     }
75
76     private static void loadLibrary() {
77         PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
78         try {
79             LibraryLoader.loadNow();
80         } catch (ProcessInitException e) {
81             throw new RuntimeException("Cannot load Crosswalk Core", e);
82         }
83     }
84
85     private static void startBrowserProcess(final Context context) {
86         ThreadUtils.runOnUiThreadBlocking(new Runnable() {
87             @Override
88             public void run() {
89                 try {
90                     LibraryLoader.ensureInitialized();
91                 } catch (ProcessInitException e) {
92                     throw new RuntimeException("Cannot initialize Crosswalk Core", e);
93                 }
94                 try {
95                     BrowserStartupController.get(context).startBrowserProcessesSync(
96                         BrowserStartupController.MAX_RENDERERS_SINGLE_PROCESS);
97                 } catch (ProcessInitException e) {
98                     throw new RuntimeException("Cannot initialize Crosswalk Core", e);
99                 }
100             }
101         });
102     }
103 }