Bug 1452399 - Add GeckoRuntime.Delegate r=esawin
authorJames Willcox <snorp@snorp.net>
Tue, 10 Apr 2018 13:10:57 -0500
changeset 413235 1a959252a8189ef91ed5e92d67439e6577471b99
parent 413234 c06258f53c2c476ba35b51b7ab06b105f8d4102b
child 413236 73ba53cdcc2b96c516683b177a9d91e9b327c197
push id33840
push userapavel@mozilla.com
push dateFri, 13 Apr 2018 21:56:54 +0000
treeherdermozilla-central@6547c27303bc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersesawin
bugs1452399
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1452399 - Add GeckoRuntime.Delegate r=esawin This lets us notify apps when Gecko exits and maybe other interesting things in the future. MozReview-Commit-ID: FFHBp7OdFBi
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
@@ -1,34 +1,29 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * vim: ts=4 sw=4 expandtab:
  * 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/. */
 
 package org.mozilla.geckoview;
 
-import java.util.ArrayList;
-
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.content.Context;
-import android.support.annotation.IntDef;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.util.Log;
 
-import org.mozilla.gecko.annotation.WrapForJNI;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoThread;
 import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.GeckoBundle;
-import org.mozilla.gecko.util.ThreadUtils;
 
 public final class GeckoRuntime implements Parcelable {
     private static final String LOGTAG = "GeckoRuntime";
     private static final boolean DEBUG = false;
 
     private static GeckoRuntime sDefaultRuntime;
 
     /**
@@ -48,48 +43,63 @@ public final class GeckoRuntime implemen
             sDefaultRuntime.attachTo(context);
             sDefaultRuntime.init(new GeckoRuntimeSettings());
         }
 
         return sDefaultRuntime;
     }
 
     private GeckoRuntimeSettings mSettings;
+    private Delegate mDelegate;
 
     /**
      * Attach the runtime to the given context.
      *
      * @param context The new context to attach to.
      */
     public void attachTo(final @NonNull Context context) {
         if (DEBUG) {
             Log.d(LOGTAG, "attachTo " + context.getApplicationContext());
         }
         final Context appContext = context.getApplicationContext();
         if (!appContext.equals(GeckoAppShell.getApplicationContext())) {
             GeckoAppShell.setApplicationContext(appContext);
         }
     }
 
+    private final BundleEventListener mEventListener = new BundleEventListener() {
+        @Override
+        public void handleMessage(final String event, final GeckoBundle message,
+                                  final EventCallback callback) {
+            if ("Gecko:Exited".equals(event) && mDelegate != null) {
+                mDelegate.onShutdown();
+                EventDispatcher.getInstance().unregisterUiThreadListener(mEventListener, "Gecko:Exited");
+            }
+        }
+    };
+
     /* package */ boolean init(final @NonNull GeckoRuntimeSettings settings) {
         if (DEBUG) {
             Log.d(LOGTAG, "init");
         }
         final int flags = settings.getUseContentProcessHint()
                           ? GeckoThread.FLAG_PRELOAD_CHILD
                           : 0;
         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;
+
+            // Bug 1453062 -- the EventDispatcher should really live here (or in GeckoThread)
+            EventDispatcher.getInstance().registerUiThreadListener(mEventListener, "Gecko:Exited");
             return true;
         }
         Log.d(LOGTAG, "init failed (could not initiate GeckoThread)");
         return false;
     }
 
     /**
      * Create a new runtime with default settings and attach it to the given
@@ -141,16 +151,42 @@ public final class GeckoRuntime implemen
     public void shutdown() {
         if (DEBUG) {
             Log.d(LOGTAG, "shutdown");
         }
 
         GeckoThread.forceQuit();
     }
 
+    public interface Delegate {
+        /**
+         * This is called when the runtime shuts down. Any GeckoSession instances that were
+         * opened with this instance are now considered closed.
+         **/
+        void onShutdown();
+    }
+
+    /**
+     * Set a delegate for receiving callbacks relevant to to this GeckoRuntime.
+     *
+     * @param delegate an implementation of {@link GeckoRuntime.Delegate}.
+     */
+    public void setDelegate(final Delegate delegate) {
+        mDelegate = delegate;
+    }
+
+    /**
+     * Returns the current delegate, if any.
+     *
+     * @return an instance of {@link GeckoRuntime.Delegate} or null if no delegate has been set.
+     */
+    public @Nullable Delegate getDelegate() {
+        return mDelegate;
+    }
+
     @Override // Parcelable
     public int describeContents() {
         return 0;
     }
 
     @Override // Parcelable
     public void writeToParcel(Parcel out, int flags) {
         out.writeParcelable(mSettings, flags);