michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
michael@0: * This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: package org.mozilla.gecko;
michael@0:
michael@0: import org.mozilla.gecko.mozglue.GeckoLoader;
michael@0: import org.mozilla.gecko.mozglue.RobocopTarget;
michael@0: import org.mozilla.gecko.util.GeckoEventListener;
michael@0: import org.mozilla.gecko.util.ThreadUtils;
michael@0:
michael@0: import org.json.JSONObject;
michael@0:
michael@0: import android.content.Context;
michael@0: import android.content.res.Configuration;
michael@0: import android.content.res.Resources;
michael@0: import android.os.Handler;
michael@0: import android.os.Looper;
michael@0: import android.os.SystemClock;
michael@0: import android.util.Log;
michael@0:
michael@0: import java.io.IOException;
michael@0: import java.util.Locale;
michael@0:
michael@0: public class GeckoThread extends Thread implements GeckoEventListener {
michael@0: private static final String LOGTAG = "GeckoThread";
michael@0:
michael@0: @RobocopTarget
michael@0: public enum LaunchState {
michael@0: Launching,
michael@0: WaitForDebugger,
michael@0: Launched,
michael@0: GeckoRunning,
michael@0: GeckoExiting,
michael@0: GeckoExited
michael@0: };
michael@0:
michael@0: private static LaunchState sLaunchState = LaunchState.Launching;
michael@0:
michael@0: private static GeckoThread sGeckoThread;
michael@0:
michael@0: private final String mArgs;
michael@0: private final String mAction;
michael@0: private final String mUri;
michael@0:
michael@0: public static boolean ensureInit() {
michael@0: ThreadUtils.assertOnUiThread();
michael@0: if (isCreated())
michael@0: return false;
michael@0: sGeckoThread = new GeckoThread(sArgs, sAction, sUri);
michael@0: return true;
michael@0: }
michael@0:
michael@0: public static String sArgs;
michael@0: public static String sAction;
michael@0: public static String sUri;
michael@0:
michael@0: public static void setArgs(String args) {
michael@0: sArgs = args;
michael@0: }
michael@0:
michael@0: public static void setAction(String action) {
michael@0: sAction = action;
michael@0: }
michael@0:
michael@0: public static void setUri(String uri) {
michael@0: sUri = uri;
michael@0: }
michael@0:
michael@0: GeckoThread(String args, String action, String uri) {
michael@0: mArgs = args;
michael@0: mAction = action;
michael@0: mUri = uri;
michael@0: setName("Gecko");
michael@0: GeckoAppShell.getEventDispatcher().registerEventListener("Gecko:Ready", this);
michael@0: }
michael@0:
michael@0: public static boolean isCreated() {
michael@0: return sGeckoThread != null;
michael@0: }
michael@0:
michael@0: public static void createAndStart() {
michael@0: if (ensureInit())
michael@0: sGeckoThread.start();
michael@0: }
michael@0:
michael@0: private String initGeckoEnvironment() {
michael@0: // At some point while loading the gecko libs our default locale gets set
michael@0: // so just save it to locale here and reset it as default after the join
michael@0: Locale locale = Locale.getDefault();
michael@0:
michael@0: if (locale.toString().equalsIgnoreCase("zh_hk")) {
michael@0: locale = Locale.TRADITIONAL_CHINESE;
michael@0: Locale.setDefault(locale);
michael@0: }
michael@0:
michael@0: Context context = GeckoAppShell.getContext();
michael@0: String resourcePath = "";
michael@0: Resources res = null;
michael@0: String[] pluginDirs = null;
michael@0: try {
michael@0: pluginDirs = GeckoAppShell.getPluginDirectories();
michael@0: } catch (Exception e) {
michael@0: Log.w(LOGTAG, "Caught exception getting plugin dirs.", e);
michael@0: }
michael@0:
michael@0: resourcePath = context.getPackageResourcePath();
michael@0: res = context.getResources();
michael@0: GeckoLoader.setupGeckoEnvironment(context, pluginDirs, context.getFilesDir().getPath());
michael@0:
michael@0: GeckoLoader.loadSQLiteLibs(context, resourcePath);
michael@0: GeckoLoader.loadNSSLibs(context, resourcePath);
michael@0: GeckoLoader.loadGeckoLibs(context, resourcePath);
michael@0: GeckoJavaSampler.setLibsLoaded();
michael@0:
michael@0: Locale.setDefault(locale);
michael@0:
michael@0: Configuration config = res.getConfiguration();
michael@0: config.locale = locale;
michael@0: res.updateConfiguration(config, res.getDisplayMetrics());
michael@0:
michael@0: return resourcePath;
michael@0: }
michael@0:
michael@0: private String getTypeFromAction(String action) {
michael@0: if (action != null && action.startsWith(GeckoApp.ACTION_WEBAPP_PREFIX)) {
michael@0: return "-webapp";
michael@0: }
michael@0: if (GeckoApp.ACTION_BOOKMARK.equals(action)) {
michael@0: return "-bookmark";
michael@0: }
michael@0: return null;
michael@0: }
michael@0:
michael@0: private String addCustomProfileArg(String args) {
michael@0: String profile = "";
michael@0: String guest = "";
michael@0: if (GeckoAppShell.getGeckoInterface() != null) {
michael@0: if (GeckoAppShell.getGeckoInterface().getProfile().inGuestMode()) {
michael@0: try {
michael@0: profile = " -profile " + GeckoAppShell.getGeckoInterface().getProfile().getDir().getCanonicalPath();
michael@0: } catch (IOException ioe) { Log.e(LOGTAG, "error getting guest profile path", ioe); }
michael@0:
michael@0: if (args == null || !args.contains(BrowserApp.GUEST_BROWSING_ARG)) {
michael@0: guest = " " + BrowserApp.GUEST_BROWSING_ARG;
michael@0: }
michael@0: } else if (!GeckoProfile.sIsUsingCustomProfile) {
michael@0: // If nothing was passed in in the intent, force Gecko to use the default profile for
michael@0: // for this activity
michael@0: profile = " -P " + GeckoAppShell.getGeckoInterface().getProfile().getName();
michael@0: }
michael@0: }
michael@0:
michael@0: return (args != null ? args : "") + profile + guest;
michael@0: }
michael@0:
michael@0: @Override
michael@0: public void run() {
michael@0: Looper.prepare();
michael@0: ThreadUtils.sGeckoThread = this;
michael@0: ThreadUtils.sGeckoHandler = new Handler();
michael@0: ThreadUtils.sGeckoQueue = Looper.myQueue();
michael@0:
michael@0: String path = initGeckoEnvironment();
michael@0:
michael@0: Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - runGecko");
michael@0:
michael@0: String args = addCustomProfileArg(mArgs);
michael@0: String type = getTypeFromAction(mAction);
michael@0:
michael@0: // and then fire us up
michael@0: Log.i(LOGTAG, "RunGecko - args = " + args);
michael@0: GeckoAppShell.runGecko(path, args, mUri, type);
michael@0: }
michael@0:
michael@0: private static Object sLock = new Object();
michael@0:
michael@0: @Override
michael@0: public void handleMessage(String event, JSONObject message) {
michael@0: if ("Gecko:Ready".equals(event)) {
michael@0: GeckoAppShell.getEventDispatcher().unregisterEventListener(event, this);
michael@0: setLaunchState(LaunchState.GeckoRunning);
michael@0: GeckoAppShell.sendPendingEventsToGecko();
michael@0: }
michael@0: }
michael@0:
michael@0: @RobocopTarget
michael@0: public static boolean checkLaunchState(LaunchState checkState) {
michael@0: synchronized (sLock) {
michael@0: return sLaunchState == checkState;
michael@0: }
michael@0: }
michael@0:
michael@0: static void setLaunchState(LaunchState setState) {
michael@0: synchronized (sLock) {
michael@0: sLaunchState = setState;
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Set the launch state to setState
and return true if the current launch
michael@0: * state is checkState
; otherwise do nothing and return false.
michael@0: */
michael@0: static boolean checkAndSetLaunchState(LaunchState checkState, LaunchState setState) {
michael@0: synchronized (sLock) {
michael@0: if (sLaunchState != checkState)
michael@0: return false;
michael@0: sLaunchState = setState;
michael@0: return true;
michael@0: }
michael@0: }
michael@0: }