Bug 710135 - Move screen.enabled/brightness to mozPower. r=jlebar,mounir sr=cjones,sicking

This commit is contained in:
Kan-Ru Chen 2012-04-03 01:51:00 -04:00
parent 7dce10b04f
commit 8bb62e5b2e
8 changed files with 86 additions and 154 deletions

View File

@ -412,11 +412,6 @@ pref("security.fileuri.strict_origin_policy", false);
// 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);

View File

@ -285,7 +285,7 @@ var shell = {
let idleHandler = function idleHandler(subject, topic, time) {
if (topic === "idle") {
if (power.getWakeLockState("screen") != "locked-foreground") {
screen.mozEnabled = false;
navigator.mozPower.screenEnabled = false;
}
}
}
@ -298,10 +298,10 @@ var shell = {
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;
}
}
}

View File

@ -42,20 +42,13 @@
#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
@ -73,26 +66,11 @@ IsChromeType(nsIDocShell *aDocShell)
} // 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;
}
@ -103,7 +81,6 @@ nsScreen::Create(nsPIDOMWindow* aWindow)
nsRefPtr<nsScreen> screen = new nsScreen();
screen->BindToOwner(aWindow);
screen->mIsChrome = IsChromeType(aWindow->GetDocShell());
hal::RegisterScreenOrientationObserver(screen);
hal::GetCurrentScreenOrientation(&(screen->mOrientation));
@ -148,28 +125,6 @@ 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)
{
@ -329,55 +284,6 @@ nsScreen::GetAvailRect(nsRect& aRect)
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)
{

View File

@ -74,8 +74,6 @@ protected:
nsresult GetRect(nsRect& aRect);
nsresult GetAvailRect(nsRect& aRect);
bool mIsChrome;
mozilla::dom::ScreenOrientation mOrientation;
private:
@ -91,14 +89,6 @@ private:
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)

View File

@ -39,7 +39,7 @@
#include "nsIDOMEventTarget.idl"
[scriptable, uuid(8a66b30c-9a32-4b17-ab4e-ca8b7b588243)]
[scriptable, uuid(9b978f58-5bfe-409d-aa3f-946ca934e51d)]
interface nsIDOMScreen : nsIDOMEventTarget
{
readonly attribute long top;
@ -53,29 +53,6 @@ interface nsIDOMScreen : nsIDOMEventTarget
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,

View File

@ -35,6 +35,7 @@
*
* ***** END LICENSE BLOCK ***** */
#include "mozilla/Hal.h"
#include "PowerManager.h"
#include "WakeLock.h"
#include "nsContentUtils.h"
@ -87,33 +88,32 @@ PowerManager::Shutdown()
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);
@ -127,8 +127,7 @@ PowerManager::Reboot()
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);
@ -142,8 +141,7 @@ PowerManager::PowerOff()
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))
@ -156,8 +154,7 @@ PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
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;
@ -166,8 +163,7 @@ PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
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);
@ -195,6 +191,51 @@ PowerManager::Callback(const nsAString &aTopic, const nsAString &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

View File

@ -64,7 +64,7 @@ public:
nsresult Shutdown();
private:
nsresult CheckPermission();
bool CheckPermission();
nsWeakPtr mWindow;
nsTArray<nsCOMPtr<nsIDOMMozWakeLockListener> > mListeners;

View File

@ -42,7 +42,7 @@ 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();
@ -73,4 +73,27 @@ interface nsIDOMMozPowerManager : nsISupports
* @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;
};