Bug 739913 - Add cpuSleepAllowed attribute to mozPower. r=cjones

This commit is contained in:
Kan-Ru Chen 2012-04-16 18:35:33 -04:00
Родитель e903ef6073
Коммит e9a3b24dc9
10 изменённых файлов: 140 добавлений и 2 удалений

Просмотреть файл

@ -323,6 +323,10 @@ var shell = {
navigator.mozPower.screenEnabled = true;
}
}
if (topic == "cpu") {
navigator.mozPower.cpuSleepAllowed = (state != "locked-foreground" &&
state != "locked-background");
}
}
let idleTimeout = Services.prefs.getIntPref("power.screen.timeout");

Просмотреть файл

@ -236,6 +236,27 @@ PowerManager::SetScreenBrightness(double aBrightness)
return NS_OK;
}
NS_IMETHODIMP
PowerManager::GetCpuSleepAllowed(bool *aAllowed)
{
if (!CheckPermission()) {
*aAllowed = true;
return NS_OK;
}
*aAllowed = hal::GetCpuSleepAllowed();
return NS_OK;
}
NS_IMETHODIMP
PowerManager::SetCpuSleepAllowed(bool aAllowed)
{
NS_ENSURE_TRUE(CheckPermission(), NS_ERROR_DOM_SECURITY_ERR);
hal::SetCpuSleepAllowed(aAllowed);
return NS_OK;
}
} // power
} // dom
} // mozilla

Просмотреть файл

@ -42,7 +42,7 @@ interface nsIDOMMozWakeLockListener;
/**
* This interface implements navigator.mozPower
*/
[scriptable, uuid(4586bed1-cf78-4436-b503-88277d645b68)]
[scriptable, uuid(256a3287-f528-45b5-9ba8-2b3650c056e6)]
interface nsIDOMMozPowerManager : nsISupports
{
void powerOff();
@ -96,4 +96,11 @@ interface nsIDOMMozPowerManager : nsISupports
* @throw NS_ERROR_INVALID_ARG if brightness is not in the range [0, 1].
*/
attribute double screenBrightness;
/**
* Is it possible that the device's CPU will sleep after the screen is
* disabled? Setting this attribute to false will prevent the device
* entering suspend state.
*/
attribute boolean cpuSleepAllowed;
};

Просмотреть файл

@ -363,6 +363,22 @@ void SetScreenEnabled(bool enabled)
PROXY_IF_SANDBOXED(SetScreenEnabled(enabled));
}
bool GetCpuSleepAllowed()
{
// Generally for interfaces that are accessible by normal web content
// we should cache the result and be notified on state changes, like
// what the battery API does. But since this is only used by
// privileged interface, the synchronous getter is OK here.
AssertMainThread();
RETURN_PROXY_IF_SANDBOXED(GetCpuSleepAllowed());
}
void SetCpuSleepAllowed(bool allowed)
{
AssertMainThread();
PROXY_IF_SANDBOXED(SetCpuSleepAllowed(allowed));
}
double GetScreenBrightness()
{
AssertMainThread();

Просмотреть файл

@ -148,6 +148,17 @@ double GetScreenBrightness();
*/
void SetScreenBrightness(double brightness);
/**
* Determine whether the device is allowed to sleep.
*/
bool GetCpuSleepAllowed();
/**
* Set whether the device is allowed to suspend automatically after
* the screen is disabled.
*/
void SetCpuSleepAllowed(bool allowed);
/**
* Set the value of a light to a particular color, with a specific flash pattern.
* light specifices which light. See Hal.idl for the list of constants

Просмотреть файл

@ -117,7 +117,11 @@ CPPSRCS += \
endif
ifneq (gonk,$(MOZ_WIDGET_TOOLKIT)) #{
CPPSRCS += FallbackLights.cpp FallbackTime.cpp
CPPSRCS += \
FallbackLights.cpp \
FallbackTime.cpp \
FallbackWakeLocks.cpp \
$(NULL)
endif #}
# Screen Orientation backend

Просмотреть файл

@ -0,0 +1,23 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et ft=cpp : */
/* 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 "Hal.h"
namespace mozilla {
namespace hal_impl {
bool
GetCpuSleepAllowed()
{
return true;
}
void
SetCpuSleepAllowed(bool allowed)
{}
} // namespace hal_impl
} // namespace mozilla

Просмотреть файл

@ -329,6 +329,8 @@ namespace {
* RAII class to help us remember to close file descriptors.
*/
const char *screenEnabledFilename = "/sys/power/state";
const char *wakeLockFilename = "/sys/power/wake_lock";
const char *wakeUnlockFilename = "/sys/power/wake_unlock";
template<ssize_t n>
bool ReadFromFile(const char *filename, char (&buf)[n])
@ -370,6 +372,12 @@ void WriteToFile(const char *filename, const char *toWrite)
// the screen is on or not.
bool sScreenEnabled = true;
// We can read wakeLockFilename to find out whether the cpu wake lock
// is already acquired, but reading and parsing it is a lot more work
// than tracking it ourselves, and it won't be accurate anyway (kernel
// internal wake locks aren't counted here.)
bool sCpuSleepAllowed = true;
} // anonymous namespace
bool
@ -422,6 +430,19 @@ SetScreenBrightness(double brightness)
hal::SetLight(hal::eHalLightID_Buttons, aConfig);
}
bool
GetCpuSleepAllowed()
{
return sCpuSleepAllowed;
}
void
SetCpuSleepAllowed(bool aAllowed)
{
WriteToFile(aAllowed ? wakeUnlockFilename : wakeLockFilename, "gecko");
sCpuSleepAllowed = aAllowed;
}
static light_device_t* sLights[hal::eHalLightID_Count]; // will be initialized to NULL
light_device_t* GetDevice(hw_module_t* module, char const* name)

Просмотреть файл

@ -120,6 +120,9 @@ parent:
sync GetScreenEnabled() returns (bool enabled);
SetScreenEnabled(bool enabled);
sync GetCpuSleepAllowed() returns (bool allowed);
SetCpuSleepAllowed(bool allowed);
sync GetScreenBrightness() returns (double brightness);
SetScreenBrightness(double brightness);

Просмотреть файл

@ -138,6 +138,20 @@ SetScreenEnabled(bool enabled)
Hal()->SendSetScreenEnabled(enabled);
}
bool
GetCpuSleepAllowed()
{
bool allowed = true;
Hal()->SendGetCpuSleepAllowed(&allowed);
return allowed;
}
void
SetCpuSleepAllowed(bool allowed)
{
Hal()->SendSetCpuSleepAllowed(allowed);
}
double
GetScreenBrightness()
{
@ -367,6 +381,20 @@ public:
return true;
}
NS_OVERRIDE virtual bool
RecvGetCpuSleepAllowed(bool *allowed)
{
*allowed = hal::GetCpuSleepAllowed();
return true;
}
NS_OVERRIDE virtual bool
RecvSetCpuSleepAllowed(const bool &allowed)
{
hal::SetCpuSleepAllowed(allowed);
return true;
}
NS_OVERRIDE virtual bool
RecvGetScreenBrightness(double *brightness)
{