mobile/android/base/GeckoThread.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko;
michael@0 7
michael@0 8 import org.mozilla.gecko.mozglue.GeckoLoader;
michael@0 9 import org.mozilla.gecko.mozglue.RobocopTarget;
michael@0 10 import org.mozilla.gecko.util.GeckoEventListener;
michael@0 11 import org.mozilla.gecko.util.ThreadUtils;
michael@0 12
michael@0 13 import org.json.JSONObject;
michael@0 14
michael@0 15 import android.content.Context;
michael@0 16 import android.content.res.Configuration;
michael@0 17 import android.content.res.Resources;
michael@0 18 import android.os.Handler;
michael@0 19 import android.os.Looper;
michael@0 20 import android.os.SystemClock;
michael@0 21 import android.util.Log;
michael@0 22
michael@0 23 import java.io.IOException;
michael@0 24 import java.util.Locale;
michael@0 25
michael@0 26 public class GeckoThread extends Thread implements GeckoEventListener {
michael@0 27 private static final String LOGTAG = "GeckoThread";
michael@0 28
michael@0 29 @RobocopTarget
michael@0 30 public enum LaunchState {
michael@0 31 Launching,
michael@0 32 WaitForDebugger,
michael@0 33 Launched,
michael@0 34 GeckoRunning,
michael@0 35 GeckoExiting,
michael@0 36 GeckoExited
michael@0 37 };
michael@0 38
michael@0 39 private static LaunchState sLaunchState = LaunchState.Launching;
michael@0 40
michael@0 41 private static GeckoThread sGeckoThread;
michael@0 42
michael@0 43 private final String mArgs;
michael@0 44 private final String mAction;
michael@0 45 private final String mUri;
michael@0 46
michael@0 47 public static boolean ensureInit() {
michael@0 48 ThreadUtils.assertOnUiThread();
michael@0 49 if (isCreated())
michael@0 50 return false;
michael@0 51 sGeckoThread = new GeckoThread(sArgs, sAction, sUri);
michael@0 52 return true;
michael@0 53 }
michael@0 54
michael@0 55 public static String sArgs;
michael@0 56 public static String sAction;
michael@0 57 public static String sUri;
michael@0 58
michael@0 59 public static void setArgs(String args) {
michael@0 60 sArgs = args;
michael@0 61 }
michael@0 62
michael@0 63 public static void setAction(String action) {
michael@0 64 sAction = action;
michael@0 65 }
michael@0 66
michael@0 67 public static void setUri(String uri) {
michael@0 68 sUri = uri;
michael@0 69 }
michael@0 70
michael@0 71 GeckoThread(String args, String action, String uri) {
michael@0 72 mArgs = args;
michael@0 73 mAction = action;
michael@0 74 mUri = uri;
michael@0 75 setName("Gecko");
michael@0 76 GeckoAppShell.getEventDispatcher().registerEventListener("Gecko:Ready", this);
michael@0 77 }
michael@0 78
michael@0 79 public static boolean isCreated() {
michael@0 80 return sGeckoThread != null;
michael@0 81 }
michael@0 82
michael@0 83 public static void createAndStart() {
michael@0 84 if (ensureInit())
michael@0 85 sGeckoThread.start();
michael@0 86 }
michael@0 87
michael@0 88 private String initGeckoEnvironment() {
michael@0 89 // At some point while loading the gecko libs our default locale gets set
michael@0 90 // so just save it to locale here and reset it as default after the join
michael@0 91 Locale locale = Locale.getDefault();
michael@0 92
michael@0 93 if (locale.toString().equalsIgnoreCase("zh_hk")) {
michael@0 94 locale = Locale.TRADITIONAL_CHINESE;
michael@0 95 Locale.setDefault(locale);
michael@0 96 }
michael@0 97
michael@0 98 Context context = GeckoAppShell.getContext();
michael@0 99 String resourcePath = "";
michael@0 100 Resources res = null;
michael@0 101 String[] pluginDirs = null;
michael@0 102 try {
michael@0 103 pluginDirs = GeckoAppShell.getPluginDirectories();
michael@0 104 } catch (Exception e) {
michael@0 105 Log.w(LOGTAG, "Caught exception getting plugin dirs.", e);
michael@0 106 }
michael@0 107
michael@0 108 resourcePath = context.getPackageResourcePath();
michael@0 109 res = context.getResources();
michael@0 110 GeckoLoader.setupGeckoEnvironment(context, pluginDirs, context.getFilesDir().getPath());
michael@0 111
michael@0 112 GeckoLoader.loadSQLiteLibs(context, resourcePath);
michael@0 113 GeckoLoader.loadNSSLibs(context, resourcePath);
michael@0 114 GeckoLoader.loadGeckoLibs(context, resourcePath);
michael@0 115 GeckoJavaSampler.setLibsLoaded();
michael@0 116
michael@0 117 Locale.setDefault(locale);
michael@0 118
michael@0 119 Configuration config = res.getConfiguration();
michael@0 120 config.locale = locale;
michael@0 121 res.updateConfiguration(config, res.getDisplayMetrics());
michael@0 122
michael@0 123 return resourcePath;
michael@0 124 }
michael@0 125
michael@0 126 private String getTypeFromAction(String action) {
michael@0 127 if (action != null && action.startsWith(GeckoApp.ACTION_WEBAPP_PREFIX)) {
michael@0 128 return "-webapp";
michael@0 129 }
michael@0 130 if (GeckoApp.ACTION_BOOKMARK.equals(action)) {
michael@0 131 return "-bookmark";
michael@0 132 }
michael@0 133 return null;
michael@0 134 }
michael@0 135
michael@0 136 private String addCustomProfileArg(String args) {
michael@0 137 String profile = "";
michael@0 138 String guest = "";
michael@0 139 if (GeckoAppShell.getGeckoInterface() != null) {
michael@0 140 if (GeckoAppShell.getGeckoInterface().getProfile().inGuestMode()) {
michael@0 141 try {
michael@0 142 profile = " -profile " + GeckoAppShell.getGeckoInterface().getProfile().getDir().getCanonicalPath();
michael@0 143 } catch (IOException ioe) { Log.e(LOGTAG, "error getting guest profile path", ioe); }
michael@0 144
michael@0 145 if (args == null || !args.contains(BrowserApp.GUEST_BROWSING_ARG)) {
michael@0 146 guest = " " + BrowserApp.GUEST_BROWSING_ARG;
michael@0 147 }
michael@0 148 } else if (!GeckoProfile.sIsUsingCustomProfile) {
michael@0 149 // If nothing was passed in in the intent, force Gecko to use the default profile for
michael@0 150 // for this activity
michael@0 151 profile = " -P " + GeckoAppShell.getGeckoInterface().getProfile().getName();
michael@0 152 }
michael@0 153 }
michael@0 154
michael@0 155 return (args != null ? args : "") + profile + guest;
michael@0 156 }
michael@0 157
michael@0 158 @Override
michael@0 159 public void run() {
michael@0 160 Looper.prepare();
michael@0 161 ThreadUtils.sGeckoThread = this;
michael@0 162 ThreadUtils.sGeckoHandler = new Handler();
michael@0 163 ThreadUtils.sGeckoQueue = Looper.myQueue();
michael@0 164
michael@0 165 String path = initGeckoEnvironment();
michael@0 166
michael@0 167 Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - runGecko");
michael@0 168
michael@0 169 String args = addCustomProfileArg(mArgs);
michael@0 170 String type = getTypeFromAction(mAction);
michael@0 171
michael@0 172 // and then fire us up
michael@0 173 Log.i(LOGTAG, "RunGecko - args = " + args);
michael@0 174 GeckoAppShell.runGecko(path, args, mUri, type);
michael@0 175 }
michael@0 176
michael@0 177 private static Object sLock = new Object();
michael@0 178
michael@0 179 @Override
michael@0 180 public void handleMessage(String event, JSONObject message) {
michael@0 181 if ("Gecko:Ready".equals(event)) {
michael@0 182 GeckoAppShell.getEventDispatcher().unregisterEventListener(event, this);
michael@0 183 setLaunchState(LaunchState.GeckoRunning);
michael@0 184 GeckoAppShell.sendPendingEventsToGecko();
michael@0 185 }
michael@0 186 }
michael@0 187
michael@0 188 @RobocopTarget
michael@0 189 public static boolean checkLaunchState(LaunchState checkState) {
michael@0 190 synchronized (sLock) {
michael@0 191 return sLaunchState == checkState;
michael@0 192 }
michael@0 193 }
michael@0 194
michael@0 195 static void setLaunchState(LaunchState setState) {
michael@0 196 synchronized (sLock) {
michael@0 197 sLaunchState = setState;
michael@0 198 }
michael@0 199 }
michael@0 200
michael@0 201 /**
michael@0 202 * Set the launch state to <code>setState</code> and return true if the current launch
michael@0 203 * state is <code>checkState</code>; otherwise do nothing and return false.
michael@0 204 */
michael@0 205 static boolean checkAndSetLaunchState(LaunchState checkState, LaunchState setState) {
michael@0 206 synchronized (sLock) {
michael@0 207 if (sLaunchState != checkState)
michael@0 208 return false;
michael@0 209 sLaunchState = setState;
michael@0 210 return true;
michael@0 211 }
michael@0 212 }
michael@0 213 }

mercurial