Bug 1540573 - P1. Observe ProcessLifecycle events in GeckoRuntime. r=snorp
authorEugen Sawin <esawin@mozilla.com>, Chris Pearce <cpearce@mozilla.com>
Mon, 06 May 2019 22:40:56 +0000
changeset 472826 6a17951bc4b981f455e3b8f54db29b5ed28fa122
parent 472825 bb4e78986eed98bb51eaa06e20e9ea032d62f3fe
child 472827 d029091d7fd831b30cbc4a660c03f962b20fa44e
push id84853
push usercpearce@mozilla.com
push dateTue, 07 May 2019 01:30:16 +0000
treeherderautoland@90ebd256a92b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1540573, 1544076
milestone68.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 1540573 - P1. Observe ProcessLifecycle events in GeckoRuntime. r=snorp This is esawin's patch from Bug 1544076; D28083. Differential Revision: https://phabricator.services.mozilla.com/D28941
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,16 +1,21 @@
 /* -*- 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 android.arch.lifecycle.ProcessLifecycleOwner;
+import android.arch.lifecycle.Lifecycle;
+import android.arch.lifecycle.LifecycleObserver;
+import android.arch.lifecycle.OnLifecycleEvent;
+
 import android.app.ActivityManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ServiceInfo;
 import android.content.res.Configuration;
@@ -88,16 +93,41 @@ public final class GeckoRuntime implemen
      * This is a key for extra data sent with {@link #ACTION_CRASHED}. The value is
      * a boolean indicating whether or not the crash was fatal or not. If true, the
      * main application process was affected by the crash. If false, only an internal
      * process used by Gecko has crashed and the application may be able to recover.
      * @see GeckoSession.ContentDelegate#onCrash(GeckoSession)
      */
     public static final String EXTRA_CRASH_FATAL = "fatal";
 
+    private final class LifecycleListener implements LifecycleObserver {
+        public LifecycleListener() {
+        }
+
+        @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
+        void onCreate() {
+            Log.d(LOGTAG, "Lifecycle: onCreate");
+        }
+
+        @OnLifecycleEvent(Lifecycle.Event.ON_START)
+        void onStart() {
+            Log.d(LOGTAG, "Lifecycle: onStart");
+        }
+
+        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
+        void onResume() {
+            Log.d(LOGTAG, "Lifecycle: onResume");
+        }
+
+        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
+        void onPause() {
+            Log.d(LOGTAG, "Lifecycle: onPause");
+        }
+    }
+
     private static GeckoRuntime sDefaultRuntime;
 
     /**
      * Get the default runtime for the given context.
      * 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.
@@ -261,16 +291,19 @@ public final class GeckoRuntime implemen
         // Bug 1453062 -- the EventDispatcher should really live here (or in GeckoThread)
         EventDispatcher.getInstance().registerUiThreadListener(mEventListener, "Gecko:Exited");
 
         // Attach and commit settings.
         mSettings.attachTo(this);
 
         // Initialize the system ClipboardManager by accessing it on the main thread.
         GeckoAppShell.getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
+
+        // Add process lifecycle listener to react to backgrounding events.
+        ProcessLifecycleOwner.get().getLifecycle().addObserver(new LifecycleListener());
         return true;
     }
 
     /* package */ void setDefaultPrefs(final GeckoBundle prefs) {
         EventDispatcher.getInstance().dispatch("GeckoView:SetDefaultPrefs", prefs);
     }
 
     /**