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.
5 package org.xwalk.core;
7 import android.app.Activity;
8 import android.content.Context;
9 import android.os.Build;
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;
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 = {
30 public static void init(XWalkView xwalkView) {
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());
39 Context context = xwalkView.getViewContext();
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);
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())) {
56 for (String library : MANDATORY_LIBRARIES) {
57 System.load("/data/data/" + context.getPackageName() + "/lib/" + library);
59 } catch (UnsatisfiedLinkError e) {
60 throw new RuntimeException("Cannot initialize Crosswalk Core", e);
64 DeviceUtils.addDeviceSpecificUserAgentSwitch(context);
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);
72 startBrowserProcess(context);
76 private static void loadLibrary() {
77 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
79 LibraryLoader.loadNow();
80 } catch (ProcessInitException e) {
81 throw new RuntimeException("Cannot load Crosswalk Core", e);
85 private static void startBrowserProcess(final Context context) {
86 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
90 LibraryLoader.ensureInitialized();
91 } catch (ProcessInitException e) {
92 throw new RuntimeException("Cannot initialize Crosswalk Core", e);
95 BrowserStartupController.get(context).startBrowserProcessesSync(
96 BrowserStartupController.MAX_RENDERERS_SINGLE_PROCESS);
97 } catch (ProcessInitException e) {
98 throw new RuntimeException("Cannot initialize Crosswalk Core", e);