Bug 1492736 - Part 1. Add nsIWakeLock to manage wake lock from devtools. r=baku
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Fri, 28 Sep 2018 14:05:07 +0900
changeset 490985 77ccb45257313e29d9c93c7faa045ee18d85a57e
parent 490984 f21a33a5ef890a48aa153fb2ba1404bc04ddd126
child 490986 e358e36fc048abf53d144f70a371c99fd634ba07
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersbaku
bugs1492736, 1369194
milestone65.0a1
Bug 1492736 - Part 1. Add nsIWakeLock to manage wake lock from devtools. r=baku DevTools wants to use wake lock to keep screen. But since MozWakeLock is removed by bug 1369194, there is no way to unlock wake lock from script. So this issue adds nsIWakeLock to unlock wake lock. Differential Revision: https://phabricator.services.mozilla.com/D7158
dom/power/PowerManagerService.cpp
dom/power/WakeLock.cpp
dom/power/WakeLock.h
dom/power/moz.build
dom/power/nsIPowerManagerService.idl
dom/power/nsIWakeLock.idl
dom/tests/browser/browser.ini
dom/tests/browser/browser_wakelock.js
--- a/dom/power/PowerManagerService.cpp
+++ b/dom/power/PowerManagerService.cpp
@@ -134,27 +134,26 @@ PowerManagerService::NewWakeLock(const n
   }
 
   return wakelock.forget();
 }
 
 NS_IMETHODIMP
 PowerManagerService::NewWakeLock(const nsAString &aTopic,
                                  mozIDOMWindow *aWindow,
-                                 nsISupports **aWakeLock)
+                                 nsIWakeLock **aWakeLock)
 {
   mozilla::ErrorResult rv;
   RefPtr<WakeLock> wakelock =
     NewWakeLock(aTopic, nsPIDOMWindowInner::From(aWindow), rv);
   if (rv.Failed()) {
     return rv.StealNSResult();
   }
 
-  nsCOMPtr<nsIDOMEventListener> eventListener = wakelock.get();
-  eventListener.forget(aWakeLock);
+  wakelock.forget(aWakeLock);
   return NS_OK;
 }
 
 already_AddRefed<WakeLock>
 PowerManagerService::NewWakeLockOnBehalfOfProcess(const nsAString& aTopic,
                                                   ContentParent* aContentParent)
 {
   RefPtr<WakeLock> wakelock = new WakeLock();
--- a/dom/power/WakeLock.cpp
+++ b/dom/power/WakeLock.cpp
@@ -20,16 +20,17 @@ using namespace mozilla::hal;
 namespace mozilla {
 namespace dom {
 
 NS_INTERFACE_MAP_BEGIN(WakeLock)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventListener)
   NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
   NS_INTERFACE_MAP_ENTRY(nsIObserver)
   NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
+  NS_INTERFACE_MAP_ENTRY(nsIWakeLock)
 NS_INTERFACE_MAP_END
 
 NS_IMPL_ADDREF(WakeLock)
 NS_IMPL_RELEASE(WakeLock)
 
 WakeLock::WakeLock()
   : mLocked(false)
   , mHidden(true)
@@ -253,16 +254,24 @@ WakeLock::HandleEvent(Event *aEvent)
   if (type.EqualsLiteral("pageshow")) {
     DoLock();
     return NS_OK;
   }
 
   return NS_OK;
 }
 
+NS_IMETHODIMP
+WakeLock::Unlock()
+{
+  ErrorResult error;
+  Unlock(error);
+  return error.StealNSResult();
+}
+
 nsPIDOMWindowInner*
 WakeLock::GetParentObject() const
 {
   nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mWindow);
   return window;
 }
 
 } // namespace dom
--- a/dom/power/WakeLock.h
+++ b/dom/power/WakeLock.h
@@ -5,36 +5,39 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_power_WakeLock_h
 #define mozilla_dom_power_WakeLock_h
 
 #include "nsCOMPtr.h"
 #include "nsIDOMEventListener.h"
 #include "nsIObserver.h"
+#include "nsIWakeLock.h"
 #include "nsString.h"
 #include "nsWeakReference.h"
 #include "nsWrapperCache.h"
 #include "mozilla/ErrorResult.h"
 
 class nsPIDOMWindowInner;
 
 namespace mozilla {
 namespace dom {
 
 class ContentParent;
 
 class WakeLock final
   : public nsIDOMEventListener
   , public nsIObserver
   , public nsSupportsWeakReference
+  , public nsIWakeLock
 {
 public:
   NS_DECL_NSIDOMEVENTLISTENER
   NS_DECL_NSIOBSERVER
+  NS_DECL_NSIWAKELOCK
 
   NS_DECL_ISUPPORTS
 
   // Note: WakeLock lives for the lifetime of the document in order to avoid
   // exposing GC behavior to pages. This means that
   // |var foo = navigator.requestWakeLock('cpu'); foo = null;|
   // doesn't unlock the 'cpu' resource.
 
--- a/dom/power/moz.build
+++ b/dom/power/moz.build
@@ -5,16 +5,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 with Files("**"):
     BUG_COMPONENT = ("Core", "DOM")
 
 XPIDL_SOURCES += [
     'nsIDOMWakeLockListener.idl',
     'nsIPowerManagerService.idl',
+    'nsIWakeLock.idl',
 ]
 
 XPIDL_MODULE = 'dom_power'
 
 EXPORTS.mozilla.dom += [
     'WakeLock.h',
 ]
 
--- a/dom/power/nsIPowerManagerService.idl
+++ b/dom/power/nsIPowerManagerService.idl
@@ -7,26 +7,27 @@
 
 %{C++
 #define NS_POWERMANAGERSERVICE_CID { 0x18c2e238, 0x3a0a, 0x4153, {0x89, 0xfc, 0x16, 0x6b, 0x3b, 0x14, 0x65, 0xa1 } }
 #define POWERMANAGERSERVICE_CONTRACTID "@mozilla.org/power/powermanagerservice;1"
 %}
 
 interface nsIDOMMozWakeLockListener;
 interface mozIDOMWindow;
+interface nsIWakeLock;
 
 /**
  * For use with non-content code.
  */
 [scriptable, builtinclass, uuid(ba7ca4c1-9d92-4425-a83b-85dd7fa953f7)]
 interface nsIPowerManagerService : nsISupports
 {
   void              addWakeLockListener(in nsIDOMMozWakeLockListener aListener);
   void              removeWakeLockListener(in nsIDOMMozWakeLockListener aListener);
   AString           getWakeLockState(in AString aTopic);
 
   /**
    * Return a wake lock (MozWakeLock) object of aTopic associated with aWindow.
    * A wake lock without associated window, e.g. used in chrome, is
    * always considered invisible.
    */
-  nsISupports newWakeLock(in AString aTopic, [optional] in mozIDOMWindow aWindow);
+  nsIWakeLock newWakeLock(in AString aTopic, [optional] in mozIDOMWindow aWindow);
 };
new file mode 100644
--- /dev/null
+++ b/dom/power/nsIWakeLock.idl
@@ -0,0 +1,12 @@
+/* -*- Mode: C++; tab-width: 40; 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 "nsISupports.idl"
+
+[scriptable, builtinclass, uuid(e27e57ce-fa63-4035-b9ef-27c5dc0cc3ae)]
+interface nsIWakeLock : nsISupports
+{
+  void unlock();
+};
--- a/dom/tests/browser/browser.ini
+++ b/dom/tests/browser/browser.ini
@@ -76,8 +76,9 @@ support-files =
 [browser_noopener.js]
 skip-if = (verify && debug && (os == 'linux'))
 support-files =
   test_noopener_source.html
   test_noopener_target.html
 [browser_noopener_null_uri.js]
 [browser_test_performance_metrics_off.js]
 skip-if = verify
+[browser_wakelock.js]
new file mode 100644
--- /dev/null
+++ b/dom/tests/browser/browser_wakelock.js
@@ -0,0 +1,27 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+"use strict";
+
+add_task(async () => {
+  info("creating test window");
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+
+  const pm = Cc["@mozilla.org/power/powermanagerservice;1"]
+             .getService(Ci.nsIPowerManagerService);
+
+  is(pm.getWakeLockState("screen"), "unlocked", "Wakelock should be unlocked state");
+
+  info("aquiring wakelock");
+  const wakelock = pm.newWakeLock("screen", win);
+  isnot(pm.getWakeLockState("screen"), "unlocked", "Wakelock shouldn't be unlocked state");
+
+  info("releasing wakelock");
+  wakelock.unlock();
+  is(pm.getWakeLockState("screen"), "unlocked", "Wakelock should be unlocked state");
+
+  info("closing test window");
+  await BrowserTestUtils.closeWindow(win);
+});