Bug 1274597 - Update the pause counter in nsAppShell to track multiple resumes as well multiple pauses. r?snorp draft
authorKartikaya Gupta <kgupta@mozilla.com>
Mon, 12 Sep 2016 13:43:08 -0400
changeset 412688 a95fe646ef835c7aa08db6e3e93b99a0ab767124
parent 412625 1851b78b5a9673ee422f189b92e5f1e86b82a01c
child 531050 fa517db75e3dbe888e43cad6fce8cea0636ed2d6
push id29239
push userkgupta@mozilla.com
push dateMon, 12 Sep 2016 17:44:08 +0000
reviewerssnorp
bugs1274597
milestone51.0a1
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,