Bug 814822 - Notify wake lock change when content crashes. r=jlebar a=blocking-basecamp
authorKan-Ru Chen (陳侃如) <kanru@kanru.info>
Wed, 28 Nov 2012 11:23:43 +0800
changeset 117163 9fd912a263a6a72fdc9ccada677a892dc87acc42
parent 117162 e8b696e40f368ebebb5caacbfc4120c7b62f3148
child 117164 66daf0a9bc0491c05f917062ab6041be81e64c48
push idunknown
push userunknown
push dateunknown
reviewersjlebar, blocking-basecamp
bugs814822
milestone18.0
Bug 814822 - Notify wake lock change when content crashes. r=jlebar a=blocking-basecamp
hal/HalWakeLock.cpp
--- a/hal/HalWakeLock.cpp
+++ b/hal/HalWakeLock.cpp
@@ -49,38 +49,52 @@ typedef nsDataHashtable<nsUint64HashKey,
 typedef nsClassHashtable<nsStringHashKey, ProcessLockTable> LockTable;
 
 static int sActiveListeners = 0;
 static StaticAutoPtr<LockTable> sLockTable;
 static bool sInitialized = false;
 static bool sIsShuttingDown = false;
 
 static PLDHashOperator
-RemoveChildFromList(const nsAString& aKey, ProcessLockTable* aTable, void* aUserArg)
-{
-  MOZ_ASSERT(aUserArg);
-
-  uint64_t childID = *static_cast<uint64_t*>(aUserArg);
-  aTable->Remove(childID);
-
-  return PL_DHASH_NEXT;
-}
-
-static PLDHashOperator
 CountWakeLocks(const uint64_t& aKey, LockCount aCount, void* aUserArg)
 {
   MOZ_ASSERT(aUserArg);
 
   LockCount* totalCount = static_cast<LockCount*>(aUserArg);
   totalCount->numLocks += aCount.numLocks;
   totalCount->numHidden += aCount.numHidden;
 
   return PL_DHASH_NEXT;
 }
 
+static PLDHashOperator
+RemoveChildFromList(const nsAString& aKey, ProcessLockTable* aTable, void* aUserArg)
+{
+  MOZ_ASSERT(aUserArg);
+
+  uint64_t childID = *static_cast<uint64_t*>(aUserArg);
+  if (aTable->Get(childID, NULL)) {
+    aTable->Remove(childID);
+    if (sActiveListeners) {
+      LockCount totalCount;
+      WakeLockInformation info;
+      aTable->EnumerateRead(CountWakeLocks, &totalCount);
+      if (!totalCount.numLocks) {
+        sLockTable->Remove(aKey);
+      }
+      info.numLocks() = totalCount.numLocks;
+      info.numHidden() = totalCount.numHidden;
+      info.topic() = aKey;
+      NotifyWakeLockChange(info);
+    }
+  }
+
+  return PL_DHASH_NEXT;
+}
+
 class ClearHashtableOnShutdown MOZ_FINAL : public nsIObserver {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 };
 
 NS_IMPL_ISUPPORTS1(ClearHashtableOnShutdown, nsIObserver)
 
@@ -157,16 +171,18 @@ DisableWakeLockNotifications()
 }
 
 void
 ModifyWakeLockInternal(const nsAString& aTopic,
                        hal::WakeLockControl aLockAdjust,
                        hal::WakeLockControl aHiddenAdjust,
                        uint64_t aProcessID)
 {
+  MOZ_ASSERT(NS_IsMainThread());
+
   if (sIsShuttingDown) {
     return;
   }
   if (!sInitialized) {
     Init();
   }
 
   ProcessLockTable* table = sLockTable->Get(aTopic);