Bug 977546 - Cleanup wakelock code in metro app shell. r=tabraldes
authorJim Mathies <jmathies@mozilla.com>
Mon, 03 Mar 2014 17:23:52 -0600
changeset 171737 f61c4ec14f0abdec3cdea8e56beb447249c93641
parent 171736 3802bdd23b74f95179e95ab3f9e40deea5335709
child 171738 5dd4f9dba66ee9cf2a2a7f48403dae3fe6d6592f
push id40545
push userryanvm@gmail.com
push dateTue, 04 Mar 2014 14:46:19 +0000
treeherdermozilla-inbound@69cfcd4c49eb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstabraldes
bugs977546
milestone30.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 977546 - Cleanup wakelock code in metro app shell. r=tabraldes
widget/windows/winrt/MetroAppShell.cpp
widget/windows/winrt/WakeLockListener.cpp
widget/windows/winrt/WakeLockListener.h
widget/windows/winrt/moz.build
--- a/widget/windows/winrt/MetroAppShell.cpp
+++ b/widget/windows/winrt/MetroAppShell.cpp
@@ -1,32 +1,34 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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/. */
 
 #include "MetroAppShell.h"
+
+#include "mozilla/AutoRestore.h"
+#include "mozilla/TimeStamp.h"
+#include "mozilla/widget/AudioSession.h"
+
+#include "nsIObserverService.h"
+#include "nsIAppStartup.h"
+#include "nsToolkitCompsCID.h"
+#include "nsIPowerManagerService.h"
+
 #include "nsXULAppAPI.h"
-#include "mozilla/widget/AudioSession.h"
+#include "nsServiceManagerUtils.h"
+#include "WinUtils.h"
+#include "nsWinMetroUtils.h"
 #include "MetroUtils.h"
 #include "MetroApp.h"
 #include "FrameworkView.h"
-#include "nsIObserverService.h"
-#include "nsServiceManagerUtils.h"
-#include "mozilla/AutoRestore.h"
-#include "mozilla/TimeStamp.h"
-#include "WinUtils.h"
-#include "nsIAppStartup.h"
-#include "nsToolkitCompsCID.h"
+#include "WakeLockListener.h"
+
 #include <shellapi.h>
-#include "nsIDOMWakeLockListener.h"
-#include "nsIPowerManagerService.h"
-#include "mozilla/StaticPtr.h"
-#include <windows.system.display.h>
-#include "nsWinMetroUtils.h"
 
 using namespace mozilla;
 using namespace mozilla::widget;
 using namespace mozilla::widget::winrt;
 using namespace Microsoft::WRL;
 using namespace Microsoft::WRL::Wrappers;
 using namespace ABI::Windows::UI::Core;
 using namespace ABI::Windows::Foundation;
@@ -45,43 +47,16 @@ extern ComPtr<MetroApp> sMetroApp;
 } } }
 
 namespace mozilla {
 namespace widget {
 // pulled from win32 app shell
 extern UINT sAppShellGeckoMsgId;
 } }
 
-class WakeLockListener : public nsIDOMMozWakeLockListener {
-public:
-  NS_DECL_ISUPPORTS;
-
-private:
-  ComPtr<ABI::Windows::System::Display::IDisplayRequest> mDisplayRequest;
-
-  NS_IMETHOD Callback(const nsAString& aTopic, const nsAString& aState) {
-    if (!mDisplayRequest) {
-      if (FAILED(ActivateGenericInstance(RuntimeClass_Windows_System_Display_DisplayRequest, mDisplayRequest))) {
-        NS_WARNING("Failed to instantiate IDisplayRequest, wakelocks will be broken!");
-        return NS_OK;
-      }
-    }
-
-    if (aState.Equals(NS_LITERAL_STRING("locked-foreground"))) {
-      mDisplayRequest->RequestActive();
-    } else {
-      mDisplayRequest->RequestRelease();
-    }
-
-    return NS_OK;
-  }
-};
-
-NS_IMPL_ISUPPORTS1(WakeLockListener, nsIDOMMozWakeLockListener)
-StaticRefPtr<WakeLockListener> sWakeLockListener;
 static ComPtr<ICoreWindowStatic> sCoreStatic;
 static bool sIsDispatching = false;
 static bool sShouldPurgeThreadQueue = false;
 static bool sBlockNativeEvents = false;
 static TimeStamp sPurgeThreadQueueStart;
 
 MetroAppShell::~MetroAppShell()
 {
@@ -125,17 +100,18 @@ MetroAppShell::Init()
     observerService->AddObserver(this, "dl-done", false);
     observerService->AddObserver(this, "dl-cancel", false);
     observerService->AddObserver(this, "dl-failed", false);
   }
 
   return nsBaseAppShell::Init();
 }
 
-HRESULT SHCreateShellItemArrayFromShellItemDynamic(IShellItem *psi, REFIID riid, void **ppv)
+HRESULT
+SHCreateShellItemArrayFromShellItemDynamic(IShellItem *psi, REFIID riid, void **ppv)
 {
   HMODULE shell32DLL = LoadLibraryW(L"shell32.dll");
   if (!shell32DLL) {
     return E_FAIL;
   }
 
   typedef BOOL (WINAPI* SHFn)(IShellItem *psi, REFIID riid, void **ppv);
 
@@ -205,16 +181,45 @@ WinLaunchDeferredMetroFirefox()
   }
   if (FAILED(hr))
     return hr;
 
   // Run the default browser through the CEH
   return executeCommand->Execute();
 }
 
+static WakeLockListener*
+InitWakeLock()
+{
+  nsCOMPtr<nsIPowerManagerService> powerManagerService =
+    do_GetService(POWERMANAGERSERVICE_CONTRACTID);
+  if (powerManagerService) {
+    WakeLockListener* pLock = new WakeLockListener();
+    powerManagerService->AddWakeLockListener(pLock);
+    return pLock;
+  }
+  else {
+    NS_WARNING("Failed to retrieve PowerManagerService, wakelocks will be broken!");
+  }
+  return nullptr;
+}
+
+static void
+ShutdownWakeLock(WakeLockListener* aLock)
+{
+  if (!aLock) {
+    return;
+  }
+  nsCOMPtr<nsIPowerManagerService> powerManagerService =
+    do_GetService(POWERMANAGERSERVICE_CONTRACTID);
+  if (powerManagerService) {
+    powerManagerService->RemoveWakeLockListener(aLock);
+  }
+}
+
 // Called by appstartup->run in xre, which is initiated by a call to
 // XRE_metroStartup in MetroApp. This call is on the metro main thread.
 NS_IMETHODIMP
 MetroAppShell::Run(void)
 {
   LogFunction();
   nsresult rv = NS_OK;
 
@@ -226,36 +231,22 @@ MetroAppShell::Run(void)
       mozilla::widget::StopAudioSession();
     break;
     case  GeckoProcessType_Plugin:
       NS_WARNING("We don't support plugins currently.");
       // Just exit
       rv = NS_ERROR_NOT_IMPLEMENTED;
     break;
     case GeckoProcessType_Default: {
-      nsCOMPtr<nsIPowerManagerService> sPowerManagerService = do_GetService(POWERMANAGERSERVICE_CONTRACTID);
-      if (sPowerManagerService) {
-        sWakeLockListener = new WakeLockListener();
-        sPowerManagerService->AddWakeLockListener(sWakeLockListener);
-      }
-      else {
-        NS_WARNING("Failed to retrieve PowerManagerService, wakelocks will be broken!");
-      }
-
+      nsAutoPtr<WakeLockListener> wakeLock = InitWakeLock();
       mozilla::widget::StartAudioSession();
       sMetroApp->ActivateBaseView();
       rv = nsBaseAppShell::Run();
       mozilla::widget::StopAudioSession();
-
-      if (sPowerManagerService) {
-        sPowerManagerService->RemoveWakeLockListener(sWakeLockListener);
-
-        sPowerManagerService = nullptr;
-        sWakeLockListener = nullptr;
-      }
+      ShutdownWakeLock(wakeLock);
 
       nsCOMPtr<nsIAppStartup> appStartup (do_GetService(NS_APPSTARTUP_CONTRACTID));
       bool restartingInMetro = false, restartingInDesktop = false;
 
       if (!appStartup || NS_FAILED(appStartup->GetRestarting(&restartingInDesktop))) {
         WinUtils::Log("appStartup->GetRestarting() unsuccessful");
       }
 
new file mode 100644
--- /dev/null
+++ b/widget/windows/winrt/WakeLockListener.cpp
@@ -0,0 +1,29 @@
+/* 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/. */
+
+#include "WakeLockListener.h"
+#include "MetroUtils.h"
+
+using namespace mozilla::widget::winrt;
+
+NS_IMPL_ISUPPORTS1(WakeLockListener, nsIDOMMozWakeLockListener)
+
+NS_IMETHODIMP
+WakeLockListener::Callback(const nsAString& aTopic, const nsAString& aState)
+{
+  if (!mDisplayRequest) {
+    if (FAILED(ActivateGenericInstance(RuntimeClass_Windows_System_Display_DisplayRequest, mDisplayRequest))) {
+      NS_WARNING("Failed to instantiate IDisplayRequest, wakelocks will be broken!");
+      return NS_OK;
+    }
+  }
+
+  if (aState.Equals(NS_LITERAL_STRING("locked-foreground"))) {
+    mDisplayRequest->RequestActive();
+  } else {
+    mDisplayRequest->RequestRelease();
+  }
+
+  return NS_OK;
+}
new file mode 100644
--- /dev/null
+++ b/widget/windows/winrt/WakeLockListener.h
@@ -0,0 +1,25 @@
+/* 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/. */
+
+#include "mozwrlbase.h"
+
+#include "nscore.h"
+#include "nsString.h"
+#include "nsIDOMWakeLockListener.h"
+
+#include <windows.system.display.h>
+
+/*
+ * A wake lock is used by dom to prevent the device from turning off the
+ * screen when the user is viewing certain types of content, like video.
+ */
+class WakeLockListener :
+  public nsIDOMMozWakeLockListener {
+public:
+  NS_DECL_ISUPPORTS;
+  NS_DECL_NSIDOMMOZWAKELOCKLISTENER;
+
+private:
+  Microsoft::WRL::ComPtr<ABI::Windows::System::Display::IDisplayRequest> mDisplayRequest;
+};
--- a/widget/windows/winrt/moz.build
+++ b/widget/windows/winrt/moz.build
@@ -13,16 +13,17 @@ SOURCES += [
     'MetroInput.cpp',
     'MetroUtils.cpp',
     'MetroWidget.cpp',
     'nsMetroFilePicker.cpp',
     'nsWinMetroUtils.cpp',
     'ToastNotificationHandler.cpp',
     'UIAAccessibilityBridge.cpp',
     'UIABridge.cpp',
+    'WakeLockListener.cpp',
 ]
 
 EXTRA_COMPONENTS += [
     'MetroUIUtils.js',
     'MetroUIUtils.manifest',
 ]
 
 EXPORTS.mozilla.widget += [