Bug 1463576 - 6. Refactor GeckoRuntime; r=me draft
authorJim Chen <nchen@mozilla.com>
Fri, 01 Jun 2018 13:39:21 -0400
changeset 802943 477ceef33a03c8788e7423bd8217cb715efe98b6
parent 802942 82753a2b34abd231a6096981cce1c119cf1dde9d
child 802944 51f0c9a68dd7e7cc2fd29a8ac8ccdef4604a747b
push id112001
push userbmo:nchen@mozilla.com
push dateFri, 01 Jun 2018 17:40:20 +0000
reviewersme
bugs1463576
milestone62.0a1
Bug 1463576 - 6. Refactor GeckoRuntime; r=me Add UI thread checks and reformat some code in GeckoRuntime. MozReview-Commit-ID: 2AE1IZGFnQd
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
--- 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)) {