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