// Copyright (c) 2013 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core;

import android.content.Context;

import org.chromium.base.PathUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.content.app.LibraryLoader;
import org.chromium.content.browser.AndroidBrowserProcess;
import org.chromium.content.browser.DeviceUtils;
import org.chromium.content.browser.ResourceExtractor;
import org.chromium.content.common.ProcessInitException;

class XWalkViewDelegate {
    private static boolean sInitialized = false;
    private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "xwalkcore";
    private static final String[] MANDATORY_PAKS = {
            "xwalk.pak", "en-US.pak" };

    public static void init(Context context) {
        if (sInitialized) {
            return;
        }

        ResourceExtractor.setMandatoryPaksToExtract(MANDATORY_PAKS);
        ResourceExtractor.setExtractImplicitLocaleForTesting(false);
        loadLibrary();
        DeviceUtils.addDeviceSpecificUserAgentSwitch(context);
        startBrowserProcess(context);
        sInitialized = true;
    }

    private static void loadLibrary() {
        PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
        try {
            LibraryLoader.loadNow();
        } catch (ProcessInitException e) {
            throw new RuntimeException("Cannot load Crosswalk Core", e);
        }
    }

    private static void startBrowserProcess(final Context context) {
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                try {
                    LibraryLoader.ensureInitialized();
                    AndroidBrowserProcess.init(context,
                            AndroidBrowserProcess.MAX_RENDERERS_AUTOMATIC);
                } catch (ProcessInitException e) {
                    throw new RuntimeException("Cannot initialize Crosswalk Core", e);
                }
            }
        });
    }
}

@@ -804,16 +804,111 @@
 .pak%22 %7D;
+%0A    private static final String%5B%5D MANDATORY_LIBRARIES = %7B%0A            %22libxwalkcore.so%22%0A    %7D;
 %0A%0A    pu
@@ -1141,24 +1141,626 @@
 ing(false);%0A
+        // If context's applicationContext is not the same package with itself,%0A        // It's a cross package invoking, load core library from library apk.%0A        if (!context.getApplicationContext().getPackageName().equals(context.getPackageName())) %7B%0A            try %7B%0A                for (String library : MANDATORY_LIBRARIES) %7B%0A                    System.load(%22/data/data/%22 + context.getPackageName() + %22/lib/%22 + library);%0A                %7D%0A            %7D catch (UnsatisfiedLinkError e) %7B%0A                throw new RuntimeException(%22Cannot initialize Crosswalk Core%22, e);%0A            %7D%0A        %7D%0A
         load

Load libxwalkcore.so across package for webapps