Bug 710135 - Move screen.enabled/brightness to mozPower. r=jlebar,mounir sr=cjones,sicking
authorKan-Ru Chen <kanru@kanru.info>
Tue, 03 Apr 2012 01:51:00 -0400
changeset 90916 9f5925b56c85d43abe2b6a9e1dc4f9cf67d38733
parent 90915 99a1c7a14296a9976ba86bb8761bd814d27d5c8c
child 90917 c62d71bac750b9e6956029a744e0359d45683327
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjlebar, mounir, cjones, sicking
bugs710135
milestone14.0a1
Bug 710135 - Move screen.enabled/brightness to mozPower. r=jlebar,mounir sr=cjones,sicking
b2g/app/b2g.js
b2g/chrome/content/shell.js
dom/base/nsScreen.cpp
dom/base/nsScreen.h
dom/interfaces/base/nsIDOMScreen.idl
dom/power/PowerManager.cpp
dom/power/PowerManager.h
dom/power/nsIDOMPowerManager.idl
--- a/b2g/app/b2g.js
+++ b/b2g/app/b2g.js
@@ -407,21 +407,16 @@ pref("browser.dom.window.dump.enabled", 
 // installable apps or wifi support.
 pref("security.fileuri.strict_origin_policy", false);
 
 // Temporarily force-enable GL compositing.  This is default-disabled
 // deep within the bowels of the widgetry system.  Remove me when GL
 // compositing isn't default disabled in widget/android.
 pref("layers.acceleration.force-enabled", true);
 
-// screen.enabled and screen.brightness properties.
-pref("dom.screenEnabledProperty.enabled", true);
-pref("dom.screenBrightnessProperty.enabled", true);
-pref("dom.mozScreenWhitelist", "http://homescreen.gaiamobile.org,http://settings.gaiamobile.org");
-
 // handle links targeting new windows
 // 1=current window/tab, 2=new window, 3=new tab in most recent window
 pref("browser.link.open_newwindow", 3);
 
 // 0: no restrictions - divert everything
 // 1: don't divert window.open at all
 // 2: don't divert window.open with features
 pref("browser.link.open_newwindow.restriction", 0);
--- a/b2g/chrome/content/shell.js
+++ b/b2g/chrome/content/shell.js
@@ -280,33 +280,33 @@ var shell = {
 
 (function PowerManager() {
   // This will eventually be moved to content, so use content API as
   // much as possible here. TODO: Bug 738530
   let power = navigator.mozPower;
   let idleHandler = function idleHandler(subject, topic, time) {
     if (topic === "idle") {
       if (power.getWakeLockState("screen") != "locked-foreground") {
-        screen.mozEnabled = false;
+        navigator.mozPower.screenEnabled = false;
       }
     }
   }
   let wakeLockHandler = function wakeLockHandler(topic, state) {
     // Turn off the screen when no one needs the it or all of them are
     // invisible, otherwise turn the screen on. Note that the CPU
     // might go to sleep as soon as the screen is turned off and
     // acquiring wake lock will not bring it back (actually the code
     // is not executed at all).
     if (topic == "screen") {
       if (state != "locked-foreground") {
         if (Services.idle.idleTime > idleTimeout*1000) {
-          screen.mozEnabled = false;
+          navigator.mozPower.screenEnabled = false;
         }
       } else {
-        screen.mozEnabled = true;
+        navigator.mozPower.screenEnabled = true;
       }
     }
   }
   let idleTimeout = Services.prefs.getIntPref("power.screen.timeout");
   let request = navigator.mozSettings.getLock().get("power.screen.timeout");
   request.onsuccess = function onSuccess() {
     idleTimeout = request.result["power.screen.timeout"] || idleTimeout;
     if (idleTimeout) {
--- a/dom/base/nsScreen.cpp
+++ b/dom/base/nsScreen.cpp
@@ -37,30 +37,23 @@
  * ***** END LICENSE BLOCK ***** */
 
 #include "mozilla/Hal.h"
 #include "nsScreen.h"
 #include "nsIDocShell.h"
 #include "nsPresContext.h"
 #include "nsCOMPtr.h"
 #include "nsDOMClassInfoID.h"
-#include "nsIInterfaceRequestorUtils.h"
 #include "nsIDocShellTreeItem.h"
 #include "nsLayoutUtils.h"
-#include "nsContentUtils.h"
-#include "mozilla/Preferences.h"
 #include "nsDOMEvent.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
-/* static */ bool nsScreen::sInitialized = false;
-/* static */ bool nsScreen::sAllowScreenEnabledProperty = false;
-/* static */ bool nsScreen::sAllowScreenBrightnessProperty = false;
-
 namespace {
 
 bool
 IsChromeType(nsIDocShell *aDocShell)
 {
   nsCOMPtr<nsIDocShellTreeItem> ds = do_QueryInterface(aDocShell);
   if (!ds) {
     return false;
@@ -68,47 +61,31 @@ IsChromeType(nsIDocShell *aDocShell)
 
   PRInt32 itemType;
   ds->GetItemType(&itemType);
   return itemType == nsIDocShellTreeItem::typeChrome;
 }
 
 } // anonymous namespace
 
-/* static */ void
-nsScreen::Initialize()
-{
-  MOZ_ASSERT(!sInitialized);
-  sInitialized = true;
-  Preferences::AddBoolVarCache(&sAllowScreenEnabledProperty,
-                               "dom.screenEnabledProperty.enabled");
-  Preferences::AddBoolVarCache(&sAllowScreenBrightnessProperty,
-                               "dom.screenBrightnessProperty.enabled");
-}
-
 /* static */ already_AddRefed<nsScreen>
 nsScreen::Create(nsPIDOMWindow* aWindow)
 {
   MOZ_ASSERT(aWindow);
 
-  if (!sInitialized) {
-    Initialize();
-  }
-
   if (!aWindow->GetDocShell()) {
     return nsnull;
   }
 
   nsCOMPtr<nsIScriptGlobalObject> sgo =
     do_QueryInterface(static_cast<nsPIDOMWindow*>(aWindow));
   NS_ENSURE_TRUE(sgo, nsnull);
 
   nsRefPtr<nsScreen> screen = new nsScreen();
   screen->BindToOwner(aWindow);
-  screen->mIsChrome = IsChromeType(aWindow->GetDocShell());
 
   hal::RegisterScreenOrientationObserver(screen);
   hal::GetCurrentScreenOrientation(&(screen->mOrientation));
 
   return screen.forget();
 }
 
 nsScreen::nsScreen()
@@ -143,38 +120,16 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_
   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Screen)
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
 NS_IMPL_ADDREF_INHERITED(nsScreen, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(nsScreen, nsDOMEventTargetHelper)
 
 NS_IMPL_EVENT_HANDLER(nsScreen, mozorientationchange)
 
-bool
-nsScreen::IsWhiteListed() {
-  if (mIsChrome) {
-    return true;
-  }
-
-  if (!GetOwner()) {
-    return false;
-  }
-
-  nsCOMPtr<nsIDocument> doc = do_GetInterface(GetOwner()->GetDocShell());
-  if (!doc) {
-    return false;
-  }
-
-  nsIPrincipal *principal = doc->NodePrincipal();
-  nsCOMPtr<nsIURI> principalURI;
-  principal->GetURI(getter_AddRefs(principalURI));
-  return nsContentUtils::URIIsChromeOrInPref(principalURI,
-                                             "dom.mozScreenWhitelist");
-}
-
 NS_IMETHODIMP
 nsScreen::GetTop(PRInt32* aTop)
 {
   nsRect rect;
   nsresult rv = GetRect(rect);
 
   *aTop = rect.y;
 
@@ -324,65 +279,16 @@ nsScreen::GetAvailRect(nsRect& aRect)
   aRect.x = nsPresContext::AppUnitsToIntCSSPixels(aRect.x);
   aRect.y = nsPresContext::AppUnitsToIntCSSPixels(aRect.y);
   aRect.height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
   aRect.width = nsPresContext::AppUnitsToIntCSSPixels(aRect.width);
 
   return NS_OK;
 }
 
-nsresult
-nsScreen::GetMozEnabled(bool *aEnabled)
-{
-  if (!sAllowScreenEnabledProperty || !IsWhiteListed()) {
-    *aEnabled = true;
-    return NS_OK;
-  }
-
-  *aEnabled = hal::GetScreenEnabled();
-  return NS_OK;
-}
-
-nsresult
-nsScreen::SetMozEnabled(bool aEnabled)
-{
-  if (!sAllowScreenEnabledProperty || !IsWhiteListed()) {
-    return NS_OK;
-  }
-
-  // TODO bug 707589: When the screen's state changes, all visible windows
-  // should fire a visibility change event.
-  hal::SetScreenEnabled(aEnabled);
-  return NS_OK;
-}
-
-nsresult
-nsScreen::GetMozBrightness(double *aBrightness)
-{
-  if (!sAllowScreenEnabledProperty || !IsWhiteListed()) {
-    *aBrightness = 1;
-    return NS_OK;
-  }
-
-  *aBrightness = hal::GetScreenBrightness();
-  return NS_OK;
-}
-
-nsresult
-nsScreen::SetMozBrightness(double aBrightness)
-{
-  if (!sAllowScreenEnabledProperty || !IsWhiteListed()) {
-    return NS_OK;
-  }
-
-  NS_ENSURE_TRUE(0 <= aBrightness && aBrightness <= 1, NS_ERROR_INVALID_ARG);
-  hal::SetScreenBrightness(aBrightness);
-  return NS_OK;
-}
-
 void
 nsScreen::Notify(const ScreenOrientationWrapper& aOrientation)
 {
   ScreenOrientation previousOrientation = mOrientation;
   mOrientation = aOrientation.orientation;
 
   NS_ASSERTION(mOrientation != eScreenOrientation_None &&
                mOrientation != eScreenOrientation_EndGuard &&
--- a/dom/base/nsScreen.h
+++ b/dom/base/nsScreen.h
@@ -69,39 +69,29 @@ public:
 
   void Notify(const mozilla::dom::ScreenOrientationWrapper& aOrientation);
 
 protected:
   nsDeviceContext* GetDeviceContext();
   nsresult GetRect(nsRect& aRect);
   nsresult GetAvailRect(nsRect& aRect);
 
-  bool mIsChrome;
-
   mozilla::dom::ScreenOrientation mOrientation;
 
 private:
   class FullScreenEventListener : public nsIDOMEventListener
   {
   public:
     FullScreenEventListener() {};
 
     NS_DECL_ISUPPORTS
     NS_DECL_NSIDOMEVENTLISTENER
   };
 
   nsScreen();
   virtual ~nsScreen();
 
-  static bool sInitialized;
-  static bool sAllowScreenEnabledProperty;
-  static bool sAllowScreenBrightnessProperty;
-
-  static void Initialize();
-
-  bool IsWhiteListed();
-
   nsRefPtr<FullScreenEventListener> mEventListener;
 
   NS_DECL_EVENT_HANDLER(mozorientationchange)
 };
 
 #endif /* nsScreen_h___ */
--- a/dom/interfaces/base/nsIDOMScreen.idl
+++ b/dom/interfaces/base/nsIDOMScreen.idl
@@ -34,54 +34,31 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsIDOMEventTarget.idl"
 
-[scriptable, uuid(8a66b30c-9a32-4b17-ab4e-ca8b7b588243)]
+[scriptable, uuid(9b978f58-5bfe-409d-aa3f-946ca934e51d)]
 interface nsIDOMScreen : nsIDOMEventTarget
 {
   readonly attribute long             top;
   readonly attribute long             left;
   readonly attribute long             width;
   readonly attribute long             height;
   readonly attribute long             pixelDepth;
   readonly attribute long             colorDepth;
   readonly attribute long             availWidth;
   readonly attribute long             availHeight;
   readonly attribute long             availLeft;
   readonly attribute long             availTop;
 
   /**
-   * Is the device's screen currently enabled?  This attribute controls the
-   * device's screen, so setting it to false will turn off the screen.
-   */
-  attribute boolean mozEnabled;
-
-  /**
-   * How bright is the screen's backlight, on a scale from 0 (very dim) to 1
-   * (full brightness)?  Setting this attribute modifies the screen's
-   * brightness.
-   *
-   * You can read and write this attribute even when the screen is disabled,
-   * but the backlight is off while the screen is disabled.
-   *
-   * If you write a value of X into this attribute, the attribute may not have
-   * the same value X when you later read it.  Most screens don't support as
-   * many different brightness levels as there are doubles between 0 and 1, so
-   * we may reduce the value's precision before storing it.
-   *
-   * @throw NS_ERROR_INVALID_ARG if brightness is not in the range [0, 1].
-   */
-  attribute double mozBrightness;
-
-  /**
    * Returns the current screen orientation.
    * Can be: landscape-primary, landscape-secondary,
    *         portrait-primary or portrait-secondary.
    */
   readonly attribute DOMString       mozOrientation;
 
   attribute nsIDOMEventListener      onmozorientationchange;
 
--- a/dom/power/PowerManager.cpp
+++ b/dom/power/PowerManager.cpp
@@ -30,16 +30,17 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+#include "mozilla/Hal.h"
 #include "PowerManager.h"
 #include "WakeLock.h"
 #include "nsContentUtils.h"
 #include "nsDOMClassInfoID.h"
 #include "nsIDOMWakeLockListener.h"
 #include "nsIPowerManagerService.h"
 #include "nsIPrincipal.h"
 #include "nsPIDOMWindow.h"
@@ -82,97 +83,92 @@ PowerManager::Shutdown()
     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
   NS_ENSURE_STATE(pmService);
 
   // Remove ourself from the global notification list.
   pmService->RemoveWakeLockListener(this);
   return NS_OK;
 }
 
-nsresult
+bool
 PowerManager::CheckPermission()
 {
   if (nsContentUtils::IsCallerChrome()) {
-    return NS_OK;
+    return true;
   }
 
   nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mWindow);
-  NS_ENSURE_STATE(win);
+  NS_ENSURE_TRUE(win, false);
   nsCOMPtr<nsIDocument> doc = do_QueryInterface(win->GetExtantDocument());
-  NS_ENSURE_STATE(doc);
+  NS_ENSURE_TRUE(doc, false);
 
   nsCOMPtr<nsIURI> uri;
   doc->NodePrincipal()->GetURI(getter_AddRefs(uri));
 
   if (!nsContentUtils::URIIsChromeOrInPref(uri, "dom.power.whitelist")) {
-    return NS_ERROR_DOM_SECURITY_ERR;
+    return false;
   }
 
-  return NS_OK;
+  return true;
 }
 
 NS_IMETHODIMP
 PowerManager::Reboot()
 {
-  nsresult rv = CheckPermission();
-  NS_ENSURE_SUCCESS(rv, rv);
+  NS_ENSURE_TRUE(CheckPermission(), NS_ERROR_DOM_SECURITY_ERR);
 
   nsCOMPtr<nsIPowerManagerService> pmService =
     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
   NS_ENSURE_STATE(pmService);
 
   pmService->Reboot();
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PowerManager::PowerOff()
 {
-  nsresult rv = CheckPermission();
-  NS_ENSURE_SUCCESS(rv, rv);
+  NS_ENSURE_TRUE(CheckPermission(), NS_ERROR_DOM_SECURITY_ERR);
 
   nsCOMPtr<nsIPowerManagerService> pmService =
     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
   NS_ENSURE_STATE(pmService);
 
   pmService->PowerOff();
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
 {
-  nsresult rv = CheckPermission();
-  NS_ENSURE_SUCCESS(rv, rv);
+  NS_ENSURE_TRUE(CheckPermission(), NS_ERROR_DOM_SECURITY_ERR);
 
   // already added? bail out.
   if (mListeners.Contains(aListener))
     return NS_OK;
 
   mListeners.AppendElement(aListener);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
 {
-  nsresult rv = CheckPermission();
-  NS_ENSURE_SUCCESS(rv, rv);
+  NS_ENSURE_TRUE(CheckPermission(), NS_ERROR_DOM_SECURITY_ERR);
 
   mListeners.RemoveElement(aListener);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PowerManager::GetWakeLockState(const nsAString &aTopic, nsAString &aState)
 {
-  nsresult rv = CheckPermission();
-  NS_ENSURE_SUCCESS(rv, rv);
+  NS_ENSURE_TRUE(CheckPermission(), NS_ERROR_DOM_SECURITY_ERR);
 
   nsCOMPtr<nsIPowerManagerService> pmService =
     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
   NS_ENSURE_STATE(pmService);
 
   return pmService->GetWakeLockState(aTopic, aState);
 }
 
@@ -190,11 +186,56 @@ PowerManager::Callback(const nsAString &
   nsAutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(mListeners);
   for (PRUint32 i = 0; i < listeners.Length(); ++i) {
     listeners[i]->Callback(aTopic, aState);
   }
 
   return NS_OK;
 }
 
+NS_IMETHODIMP
+PowerManager::GetScreenEnabled(bool *aEnabled)
+{
+  if (!CheckPermission()) {
+    *aEnabled = true;
+    return NS_OK;
+  }
+
+  *aEnabled = hal::GetScreenEnabled();
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+PowerManager::SetScreenEnabled(bool aEnabled)
+{
+  NS_ENSURE_TRUE(CheckPermission(), NS_ERROR_DOM_SECURITY_ERR);
+
+  // TODO bug 707589: When the screen's state changes, all visible windows
+  // should fire a visibility change event.
+  hal::SetScreenEnabled(aEnabled);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+PowerManager::GetScreenBrightness(double *aBrightness)
+{
+  if (!CheckPermission()) {
+    *aBrightness = 1;
+    return NS_OK;
+  }
+
+  *aBrightness = hal::GetScreenBrightness();
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+PowerManager::SetScreenBrightness(double aBrightness)
+{
+  NS_ENSURE_TRUE(CheckPermission(), NS_ERROR_DOM_SECURITY_ERR);
+
+  NS_ENSURE_TRUE(0 <= aBrightness && aBrightness <= 1, NS_ERROR_INVALID_ARG);
+  hal::SetScreenBrightness(aBrightness);
+  return NS_OK;
+}
+
 } // power
 } // dom
 } // mozilla
--- a/dom/power/PowerManager.h
+++ b/dom/power/PowerManager.h
@@ -59,17 +59,17 @@ public:
 
   PowerManager() {};
   virtual ~PowerManager() {};
 
   nsresult Init(nsIDOMWindow *aWindow);
   nsresult Shutdown();
 
 private:
-  nsresult CheckPermission();
+  bool CheckPermission();
 
   nsWeakPtr mWindow;
   nsTArray<nsCOMPtr<nsIDOMMozWakeLockListener> > mListeners;
 };
 
 } // namespace power
 } // namespace dom
 } // namespace mozilla
--- a/dom/power/nsIDOMPowerManager.idl
+++ b/dom/power/nsIDOMPowerManager.idl
@@ -37,17 +37,17 @@
 
 #include "nsISupports.idl"
 
 interface nsIDOMMozWakeLockListener;
 
 /**
  * This interface implements navigator.mozPower
  */
-[scriptable, uuid(abf4b2b1-139d-4eff-998d-8f24616910ae)]
+[scriptable, uuid(4586bed1-cf78-4436-b503-88277d645b68)]
 interface nsIDOMMozPowerManager : nsISupports
 {
     void    powerOff();
     void    reboot();
 
     /**
      * The listeners are notified when a resource changes its lock state to:
      *  - unlocked
@@ -68,9 +68,32 @@ interface nsIDOMMozPowerManager : nsISup
      *    and it is visible.
      *
      *  - "locked-background" - at least one window holds the wake lock,
      *    but all of them are hidden.
      *
      * @param aTopic The resource name related to the wake lock.
      */
     DOMString getWakeLockState(in DOMString aTopic);
+
+    /**
+     * Is the device's screen currently enabled?  This attribute controls the
+     * device's screen, so setting it to false will turn off the screen.
+     */
+    attribute boolean screenEnabled;
+
+    /**
+     * How bright is the screen's backlight, on a scale from 0 (very dim) to 1
+     * (full brightness)?  Setting this attribute modifies the screen's
+     * brightness.
+     *
+     * You can read and write this attribute even when the screen is disabled,
+     * but the backlight is off while the screen is disabled.
+     *
+     * If you write a value of X into this attribute, the attribute may not have
+     * the same value X when you later read it.  Most screens don't support as
+     * many different brightness levels as there are doubles between 0 and 1, so
+     * we may reduce the value's precision before storing it.
+     *
+     * @throw NS_ERROR_INVALID_ARG if brightness is not in the range [0, 1].
+     */
+    attribute double screenBrightness;
 };