Bug 1274597 - Update the pause counter in nsAppShell to track multiple resumes as well multiple pauses. r=snorp
authorKartikaya Gupta <kgupta@mozilla.com>
Mon, 12 Sep 2016 13:43:08 -0400
changeset 355181 109281cadd425161c50bce8ab4064faa68c4113c
parent 355163 820356f508df8c83436f8ae3de1a3cbbd28b19bf
child 355182 4b1f411b1ea6e183a32c43bdd1ff672cca7fee6d
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1274597
milestone51.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 1274597 - Update the pause counter in nsAppShell to track multiple resumes as well multiple pauses. r=snorp MozReview-Commit-ID: CSGFU9dygVI
widget/android/nsAppShell.cpp
--- a/widget/android/nsAppShell.cpp
+++ b/widget/android/nsAppShell.cpp
@@ -99,17 +99,19 @@ public:
 NS_IMPL_ISUPPORTS(WakeLockListener, nsIDOMMozWakeLockListener)
 nsCOMPtr<nsIPowerManagerService> sPowerManagerService = nullptr;
 StaticRefPtr<WakeLockListener> sWakeLockListener;
 
 
 class GeckoThreadSupport final
     : public java::GeckoThread::Natives<GeckoThreadSupport>
 {
-    static uint32_t sPauseCount;
+    // When this number goes above 0, the app is paused. When less than or
+    // equal to zero, the app is resumed.
+    static int32_t sPauseCount;
 
 public:
     static void SpeculativeConnect(jni::String::Param aUriStr)
     {
         if (!NS_IsMainThread()) {
             // We will be on the main thread if the call was queued on the Java
             // side during startup. Otherwise, the call was not queued, which
             // means Gecko is already sufficiently loaded, and we don't really
@@ -137,18 +139,20 @@ public:
         };
         nsAppShell::SyncRunEvent(NoOpEvent());
     }
 
     static void OnPause()
     {
         MOZ_ASSERT(NS_IsMainThread());
 
-        if ((++sPauseCount) > 1) {
-            // Already paused.
+        sPauseCount++;
+        // If sPauseCount is now 1, we just crossed the threshold from "resumed"
+        // "paused". so we should notify observers and so on.
+        if (sPauseCount != 1) {
             return;
         }
 
         nsCOMPtr<nsIObserverService> obsServ =
             mozilla::services::GetObserverService();
         obsServ->NotifyObservers(nullptr, "application-background", nullptr);
 
         NS_NAMED_LITERAL_STRING(minimize, "heap-minimize");
@@ -169,18 +173,20 @@ public:
             prefs->SavePrefFile(nullptr);
         }
     }
 
     static void OnResume()
     {
         MOZ_ASSERT(NS_IsMainThread());
 
-        if (!sPauseCount || (--sPauseCount) > 0) {
-            // Still paused.
+        sPauseCount--;
+        // If sPauseCount is now 0, we just crossed the threshold from "paused"
+        // to "resumed", so we should notify observers and so on.
+        if (sPauseCount != 0) {
             return;
         }
 
         // If we are OOM killed with the disk cache enabled, the entire
         // cache will be cleared (bug 105843), so shut down cache on backgrounding
         // and re-init here
         if (nsCacheService::GlobalInstance()) {
             nsCacheService::GlobalInstance()->Init();
@@ -211,17 +217,17 @@ public:
         if (!AndroidBridge::Bridge()) {
             return -1;
         }
 
         return AndroidBridge::Bridge()->RunDelayedUiThreadTasks();
     }
 };
 
-uint32_t GeckoThreadSupport::sPauseCount;
+int32_t GeckoThreadSupport::sPauseCount;
 
 
 class GeckoAppShellSupport final
     : public java::GeckoAppShell::Natives<GeckoAppShellSupport>
 {
 public:
     static void ReportJavaCrash(const jni::Class::LocalRef& aCls,
                                 jni::Throwable::Param aException,