Bug 739913 - Use kernel wake lock when the "cpu" topic is locked. r=cjones

This commit is contained in:
Kan-Ru Chen 2012-04-12 19:24:20 -04:00
parent 6f228de18f
commit 14c301c057
11 changed files with 142 additions and 1 deletions

View File

@ -318,6 +318,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");

View File

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

View File

@ -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;
};

View File

@ -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 enabled)
{
AssertMainThread();
PROXY_IF_SANDBOXED(SetCpuSleepAllowed(enabled));
}
double GetScreenBrightness()
{
AssertMainThread();

View File

@ -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 enabled);
/**
* 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

View File

@ -141,6 +141,16 @@ void
SetScreenBrightness(double brightness)
{}
bool
GetCpuSleepAllowed()
{
return true;
}
void
SetCpuSleepAllowed(bool enabled)
{}
void
EnableNetworkNotifications()
{

View File

@ -322,6 +322,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])
@ -363,6 +365,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
@ -415,6 +423,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)

View File

@ -91,6 +91,16 @@ void
SetScreenBrightness(double brightness)
{}
bool
GetCpuSleepAllowed()
{
return true;
}
void
SetCpuSleepAllowed(bool enabled)
{}
void
EnableNetworkNotifications()
{}

View File

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

View File

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

View File

@ -69,6 +69,16 @@ void
SetScreenBrightness(double brightness)
{}
bool
GetCpuSleepAllowed()
{
return true;
}
void
SetCpuSleepAllowed(bool enabled)
{}
void
EnableNetworkNotifications()
{}