1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/GeckoThread.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,213 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko; 1.10 + 1.11 +import org.mozilla.gecko.mozglue.GeckoLoader; 1.12 +import org.mozilla.gecko.mozglue.RobocopTarget; 1.13 +import org.mozilla.gecko.util.GeckoEventListener; 1.14 +import org.mozilla.gecko.util.ThreadUtils; 1.15 + 1.16 +import org.json.JSONObject; 1.17 + 1.18 +import android.content.Context; 1.19 +import android.content.res.Configuration; 1.20 +import android.content.res.Resources; 1.21 +import android.os.Handler; 1.22 +import android.os.Looper; 1.23 +import android.os.SystemClock; 1.24 +import android.util.Log; 1.25 + 1.26 +import java.io.IOException; 1.27 +import java.util.Locale; 1.28 + 1.29 +public class GeckoThread extends Thread implements GeckoEventListener { 1.30 + private static final String LOGTAG = "GeckoThread"; 1.31 + 1.32 + @RobocopTarget 1.33 + public enum LaunchState { 1.34 + Launching, 1.35 + WaitForDebugger, 1.36 + Launched, 1.37 + GeckoRunning, 1.38 + GeckoExiting, 1.39 + GeckoExited 1.40 + }; 1.41 + 1.42 + private static LaunchState sLaunchState = LaunchState.Launching; 1.43 + 1.44 + private static GeckoThread sGeckoThread; 1.45 + 1.46 + private final String mArgs; 1.47 + private final String mAction; 1.48 + private final String mUri; 1.49 + 1.50 + public static boolean ensureInit() { 1.51 + ThreadUtils.assertOnUiThread(); 1.52 + if (isCreated()) 1.53 + return false; 1.54 + sGeckoThread = new GeckoThread(sArgs, sAction, sUri); 1.55 + return true; 1.56 + } 1.57 + 1.58 + public static String sArgs; 1.59 + public static String sAction; 1.60 + public static String sUri; 1.61 + 1.62 + public static void setArgs(String args) { 1.63 + sArgs = args; 1.64 + } 1.65 + 1.66 + public static void setAction(String action) { 1.67 + sAction = action; 1.68 + } 1.69 + 1.70 + public static void setUri(String uri) { 1.71 + sUri = uri; 1.72 + } 1.73 + 1.74 + GeckoThread(String args, String action, String uri) { 1.75 + mArgs = args; 1.76 + mAction = action; 1.77 + mUri = uri; 1.78 + setName("Gecko"); 1.79 + GeckoAppShell.getEventDispatcher().registerEventListener("Gecko:Ready", this); 1.80 + } 1.81 + 1.82 + public static boolean isCreated() { 1.83 + return sGeckoThread != null; 1.84 + } 1.85 + 1.86 + public static void createAndStart() { 1.87 + if (ensureInit()) 1.88 + sGeckoThread.start(); 1.89 + } 1.90 + 1.91 + private String initGeckoEnvironment() { 1.92 + // At some point while loading the gecko libs our default locale gets set 1.93 + // so just save it to locale here and reset it as default after the join 1.94 + Locale locale = Locale.getDefault(); 1.95 + 1.96 + if (locale.toString().equalsIgnoreCase("zh_hk")) { 1.97 + locale = Locale.TRADITIONAL_CHINESE; 1.98 + Locale.setDefault(locale); 1.99 + } 1.100 + 1.101 + Context context = GeckoAppShell.getContext(); 1.102 + String resourcePath = ""; 1.103 + Resources res = null; 1.104 + String[] pluginDirs = null; 1.105 + try { 1.106 + pluginDirs = GeckoAppShell.getPluginDirectories(); 1.107 + } catch (Exception e) { 1.108 + Log.w(LOGTAG, "Caught exception getting plugin dirs.", e); 1.109 + } 1.110 + 1.111 + resourcePath = context.getPackageResourcePath(); 1.112 + res = context.getResources(); 1.113 + GeckoLoader.setupGeckoEnvironment(context, pluginDirs, context.getFilesDir().getPath()); 1.114 + 1.115 + GeckoLoader.loadSQLiteLibs(context, resourcePath); 1.116 + GeckoLoader.loadNSSLibs(context, resourcePath); 1.117 + GeckoLoader.loadGeckoLibs(context, resourcePath); 1.118 + GeckoJavaSampler.setLibsLoaded(); 1.119 + 1.120 + Locale.setDefault(locale); 1.121 + 1.122 + Configuration config = res.getConfiguration(); 1.123 + config.locale = locale; 1.124 + res.updateConfiguration(config, res.getDisplayMetrics()); 1.125 + 1.126 + return resourcePath; 1.127 + } 1.128 + 1.129 + private String getTypeFromAction(String action) { 1.130 + if (action != null && action.startsWith(GeckoApp.ACTION_WEBAPP_PREFIX)) { 1.131 + return "-webapp"; 1.132 + } 1.133 + if (GeckoApp.ACTION_BOOKMARK.equals(action)) { 1.134 + return "-bookmark"; 1.135 + } 1.136 + return null; 1.137 + } 1.138 + 1.139 + private String addCustomProfileArg(String args) { 1.140 + String profile = ""; 1.141 + String guest = ""; 1.142 + if (GeckoAppShell.getGeckoInterface() != null) { 1.143 + if (GeckoAppShell.getGeckoInterface().getProfile().inGuestMode()) { 1.144 + try { 1.145 + profile = " -profile " + GeckoAppShell.getGeckoInterface().getProfile().getDir().getCanonicalPath(); 1.146 + } catch (IOException ioe) { Log.e(LOGTAG, "error getting guest profile path", ioe); } 1.147 + 1.148 + if (args == null || !args.contains(BrowserApp.GUEST_BROWSING_ARG)) { 1.149 + guest = " " + BrowserApp.GUEST_BROWSING_ARG; 1.150 + } 1.151 + } else if (!GeckoProfile.sIsUsingCustomProfile) { 1.152 + // If nothing was passed in in the intent, force Gecko to use the default profile for 1.153 + // for this activity 1.154 + profile = " -P " + GeckoAppShell.getGeckoInterface().getProfile().getName(); 1.155 + } 1.156 + } 1.157 + 1.158 + return (args != null ? args : "") + profile + guest; 1.159 + } 1.160 + 1.161 + @Override 1.162 + public void run() { 1.163 + Looper.prepare(); 1.164 + ThreadUtils.sGeckoThread = this; 1.165 + ThreadUtils.sGeckoHandler = new Handler(); 1.166 + ThreadUtils.sGeckoQueue = Looper.myQueue(); 1.167 + 1.168 + String path = initGeckoEnvironment(); 1.169 + 1.170 + Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - runGecko"); 1.171 + 1.172 + String args = addCustomProfileArg(mArgs); 1.173 + String type = getTypeFromAction(mAction); 1.174 + 1.175 + // and then fire us up 1.176 + Log.i(LOGTAG, "RunGecko - args = " + args); 1.177 + GeckoAppShell.runGecko(path, args, mUri, type); 1.178 + } 1.179 + 1.180 + private static Object sLock = new Object(); 1.181 + 1.182 + @Override 1.183 + public void handleMessage(String event, JSONObject message) { 1.184 + if ("Gecko:Ready".equals(event)) { 1.185 + GeckoAppShell.getEventDispatcher().unregisterEventListener(event, this); 1.186 + setLaunchState(LaunchState.GeckoRunning); 1.187 + GeckoAppShell.sendPendingEventsToGecko(); 1.188 + } 1.189 + } 1.190 + 1.191 + @RobocopTarget 1.192 + public static boolean checkLaunchState(LaunchState checkState) { 1.193 + synchronized (sLock) { 1.194 + return sLaunchState == checkState; 1.195 + } 1.196 + } 1.197 + 1.198 + static void setLaunchState(LaunchState setState) { 1.199 + synchronized (sLock) { 1.200 + sLaunchState = setState; 1.201 + } 1.202 + } 1.203 + 1.204 + /** 1.205 + * Set the launch state to <code>setState</code> and return true if the current launch 1.206 + * state is <code>checkState</code>; otherwise do nothing and return false. 1.207 + */ 1.208 + static boolean checkAndSetLaunchState(LaunchState checkState, LaunchState setState) { 1.209 + synchronized (sLock) { 1.210 + if (sLaunchState != checkState) 1.211 + return false; 1.212 + sLaunchState = setState; 1.213 + return true; 1.214 + } 1.215 + } 1.216 +}