Bug 1463576 - 6. Refactor GeckoRuntime; r=me
Add UI thread checks and reformat some code in GeckoRuntime.
MozReview-Commit-ID: 2AE1IZGFnQd
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
@@ -33,19 +33,21 @@ public final class GeckoRuntime implemen
* This will create and initialize the runtime with the default settings.
*
* Note: Only use this for session-less apps.
* For regular apps, use create() instead.
*
* @param context An application context for the default runtime.
* @return The (static) default runtime for the context.
*/
- public static synchronized @NonNull GeckoRuntime getDefault(
- final @NonNull Context context) {
- Log.d(LOGTAG, "getDefault");
+ public static synchronized @NonNull GeckoRuntime getDefault(final @NonNull Context context) {
+ ThreadUtils.assertOnUiThread();
+ if (DEBUG) {
+ Log.d(LOGTAG, "getDefault");
+ }
if (sDefaultRuntime == null) {
sDefaultRuntime = new GeckoRuntime();
sDefaultRuntime.attachTo(context);
sDefaultRuntime.init(new GeckoRuntimeSettings());
}
return sDefaultRuntime;
}
@@ -96,35 +98,35 @@ public final class GeckoRuntime implemen
if (settings.getJavaCrashReportingEnabled()) {
flags |= GeckoThread.FLAG_ENABLE_JAVA_CRASHREPORTER;
}
if (settings.getPauseForDebuggerEnabled()) {
flags |= GeckoThread.FLAG_DEBUGGING;
}
- if (GeckoThread.initMainProcess(/* profile */ null,
- settings.getArguments(),
- settings.getExtras(),
- flags)) {
- if (!GeckoThread.launch()) {
- Log.d(LOGTAG, "init failed (GeckoThread already launched)");
- return false;
- }
- mSettings = settings;
+ if (!GeckoThread.initMainProcess(/* profile */ null, settings.getArguments(),
+ settings.getExtras(), flags)) {
+ Log.w(LOGTAG, "init failed (could not initiate GeckoThread)");
+ return false;
+ }
- // Bug 1453062 -- the EventDispatcher should really live here (or in GeckoThread)
- EventDispatcher.getInstance().registerUiThreadListener(mEventListener, "Gecko:Exited");
+ if (!GeckoThread.launch()) {
+ Log.w(LOGTAG, "init failed (GeckoThread already launched)");
+ return false;
+ }
+
+ mSettings = settings;
- mSettings.runtime = this;
- mSettings.flush();
- return true;
- }
- Log.d(LOGTAG, "init failed (could not initiate GeckoThread)");
- return false;
+ // Bug 1453062 -- the EventDispatcher should really live here (or in GeckoThread)
+ EventDispatcher.getInstance().registerUiThreadListener(mEventListener, "Gecko:Exited");
+
+ mSettings.runtime = this;
+ mSettings.flush();
+ return true;
}
/**
* Create a new runtime with default settings and attach it to the given
* context.
*
* Create will throw if there is already an active Gecko instance running,
* to prevent that, bind the runtime to the process lifetime instead of the
@@ -144,19 +146,19 @@ public final class GeckoRuntime implemen
* Create will throw if there is already an active Gecko instance running,
* to prevent that, bind the runtime to the process lifetime instead of the
* activity lifetime.
*
* @param context The context of the runtime.
* @param settings The settings for the runtime.
* @return An initialized runtime.
*/
- public static @NonNull GeckoRuntime create(
- final @NonNull Context context,
- final @NonNull GeckoRuntimeSettings settings) {
+ public static @NonNull GeckoRuntime create(final @NonNull Context context,
+ final @NonNull GeckoRuntimeSettings settings) {
+ ThreadUtils.assertOnUiThread();
if (DEBUG) {
Log.d(LOGTAG, "create " + context);
}
final GeckoRuntime runtime = new GeckoRuntime();
runtime.attachTo(context);
if (!runtime.init(settings)) {