Bug 1523974 - Make ProcessPriorityManager pay attention to video and audio playing WakeLocks. r=gsvelto
authorMike Conley <mconley@mozilla.com>
Mon, 04 Feb 2019 22:57:03 +0000
changeset 517645 4a12df5a18f03cfe88d9a778669a93f451075d4f
parent 517644 f81e32841918cf355e84b9d7f676da0fe231d58a
child 517646 f77433e6354cbc2ebe693e0c939b89d2eb5edc1a
push id2032
push userffxbld-merge
push dateMon, 13 May 2019 09:36:57 +0000
treeherdermozilla-release@455c1065dcbe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgsvelto
bugs1523974
milestone67.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 1523974 - Make ProcessPriorityManager pay attention to video and audio playing WakeLocks. r=gsvelto Differential Revision: https://phabricator.services.mozilla.com/D18393
dom/ipc/ProcessPriorityManager.cpp
--- a/dom/ipc/ProcessPriorityManager.cpp
+++ b/dom/ipc/ProcessPriorityManager.cpp
@@ -262,21 +262,25 @@ class ParticularProcessPriorityManager f
   static uint32_t sBackgroundGracePeriodMS;
 
   void FireTestOnlyObserverNotification(
       const char* aTopic, const nsACString& aData = EmptyCString());
 
   void FireTestOnlyObserverNotification(const char* aTopic,
                                         const char* aData = nullptr);
 
+  bool IsHoldingWakeLock(const nsAString& aTopic);
+
   ContentParent* mContentParent;
   uint64_t mChildID;
   ProcessPriority mPriority;
   bool mHoldsCPUWakeLock;
   bool mHoldsHighPriorityWakeLock;
+  bool mHoldsPlayingAudioWakeLock;
+  bool mHoldsPlayingVideoWakeLock;
 
   /**
    * Used to implement NameWithComma().
    */
   nsAutoCString mNameWithComma;
 
   nsCOMPtr<nsITimer> mResetPriorityTimer;
 
@@ -487,17 +491,19 @@ NS_IMPL_ISUPPORTS(ParticularProcessPrior
                   nsITimerCallback, nsISupportsWeakReference, nsINamed);
 
 ParticularProcessPriorityManager::ParticularProcessPriorityManager(
     ContentParent* aContentParent)
     : mContentParent(aContentParent),
       mChildID(aContentParent->ChildID()),
       mPriority(PROCESS_PRIORITY_UNKNOWN),
       mHoldsCPUWakeLock(false),
-      mHoldsHighPriorityWakeLock(false) {
+      mHoldsHighPriorityWakeLock(false),
+      mHoldsPlayingAudioWakeLock(false),
+      mHoldsPlayingVideoWakeLock(false) {
   MOZ_ASSERT(XRE_IsParentProcess());
   LOGP("Creating ParticularProcessPriorityManager.");
 }
 
 void ParticularProcessPriorityManager::StaticInit() {
   Preferences::AddUintVarCache(
       &sBackgroundPerceivableGracePeriodMS,
       "dom.ipc.processPriorityManager.backgroundPerceivableGracePeriodMS");
@@ -512,24 +518,37 @@ void ParticularProcessPriorityManager::I
   nsCOMPtr<nsIObserverService> os = services::GetObserverService();
   if (os) {
     os->AddObserver(this, "remote-browser-shown", /* ownsWeak */ true);
     os->AddObserver(this, "ipc:browser-destroyed", /* ownsWeak */ true);
   }
 
   // This process may already hold the CPU lock; for example, our parent may
   // have acquired it on our behalf.
-  WakeLockInformation info1, info2;
-  GetWakeLockInfo(NS_LITERAL_STRING("cpu"), &info1);
-  mHoldsCPUWakeLock = info1.lockingProcesses().Contains(ChildID());
+  mHoldsCPUWakeLock = IsHoldingWakeLock(NS_LITERAL_STRING("cpu"));
+  mHoldsHighPriorityWakeLock =
+      IsHoldingWakeLock(NS_LITERAL_STRING("high-priority"));
+  mHoldsPlayingAudioWakeLock =
+      IsHoldingWakeLock(NS_LITERAL_STRING("audio-playing"));
+  mHoldsPlayingVideoWakeLock =
+      IsHoldingWakeLock(NS_LITERAL_STRING("video-playing"));
 
-  GetWakeLockInfo(NS_LITERAL_STRING("high-priority"), &info2);
-  mHoldsHighPriorityWakeLock = info2.lockingProcesses().Contains(ChildID());
-  LOGP("Done starting up.  mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d",
-       mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock);
+  LOGP(
+      "Done starting up.  mHoldsCPUWakeLock=%d, "
+      "mHoldsHighPriorityWakeLock=%d, mHoldsPlayingAudioWakeLock=%d, "
+      "mHoldsPlayingVideoWakeLock=%d",
+      mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock, mHoldsPlayingAudioWakeLock,
+      mHoldsPlayingVideoWakeLock);
+}
+
+bool ParticularProcessPriorityManager::IsHoldingWakeLock(
+    const nsAString& aTopic) {
+  WakeLockInformation info;
+  GetWakeLockInfo(aTopic, &info);
+  return info.lockingProcesses().Contains(ChildID());
 }
 
 ParticularProcessPriorityManager::~ParticularProcessPriorityManager() {
   LOGP("Destroying ParticularProcessPriorityManager.");
 
   // Unregister our wake lock observer if ShutDown hasn't been called.  (The
   // wake lock observer takes raw refs, so we don't want to take chances here!)
   // We don't call UnregisterWakeLockObserver unconditionally because the code
@@ -547,26 +566,32 @@ ParticularProcessPriorityManager::~Parti
     return;
   }
 
   bool* dest = nullptr;
   if (aInfo.topic().EqualsLiteral("cpu")) {
     dest = &mHoldsCPUWakeLock;
   } else if (aInfo.topic().EqualsLiteral("high-priority")) {
     dest = &mHoldsHighPriorityWakeLock;
+  } else if (aInfo.topic().EqualsLiteral("audio-playing")) {
+    dest = &mHoldsPlayingAudioWakeLock;
+  } else if (aInfo.topic().EqualsLiteral("video-playing")) {
+    dest = &mHoldsPlayingVideoWakeLock;
   }
 
   if (dest) {
     bool thisProcessLocks = aInfo.lockingProcesses().Contains(ChildID());
     if (thisProcessLocks != *dest) {
       *dest = thisProcessLocks;
       LOGP(
           "Got wake lock changed event. "
-          "Now mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d",
-          mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock);
+          "Now mHoldsCPUWakeLock=%d, mHoldsHighPriorityWakeLock=%d, "
+          "mHoldsPlayingAudioWakeLock=%d, mHoldsPlayingVideoWakeLock=%d",
+          mHoldsCPUWakeLock, mHoldsHighPriorityWakeLock,
+          mHoldsPlayingAudioWakeLock, mHoldsPlayingVideoWakeLock);
       ResetPriority();
     }
   }
 }
 
 NS_IMETHODIMP
 ParticularProcessPriorityManager::Observe(nsISupports* aSubject,
                                           const char* aTopic,
@@ -723,17 +748,18 @@ ProcessPriority ParticularProcessPriorit
 }
 
 ProcessPriority ParticularProcessPriorityManager::ComputePriority() {
   if (!mActiveTabParents.IsEmpty() ||
       mContentParent->GetRemoteType().EqualsLiteral(EXTENSION_REMOTE_TYPE)) {
     return PROCESS_PRIORITY_FOREGROUND;
   }
 
-  if (mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock) {
+  if (mHoldsCPUWakeLock || mHoldsHighPriorityWakeLock ||
+      mHoldsPlayingAudioWakeLock || mHoldsPlayingVideoWakeLock) {
     return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
   }
 
   return PROCESS_PRIORITY_BACKGROUND;
 }
 
 void ParticularProcessPriorityManager::SetPriorityNow(
     ProcessPriority aPriority) {