Bug 836654 - Part 3: Modify hal to accept a ContentParent ID in ModifyWakeLock(). r=cjones

Also return the list of processes holding the lock in GetWakeLockInfo
and in the wake-lock changed notification.
This commit is contained in:
Justin Lebar 2013-02-14 15:41:30 -05:00
parent c24d48d121
commit 11d5a89e4e
6 changed files with 49 additions and 40 deletions

View File

@ -22,6 +22,7 @@
#include "WindowIdentifier.h"
#include "mozilla/dom/ScreenOrientation.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/ContentParent.h"
#ifdef XP_WIN
#include <process.h>
@ -29,6 +30,7 @@
#endif
using namespace mozilla::services;
using namespace mozilla::dom;
#define PROXY_IF_SANDBOXED(_call) \
do { \
@ -644,23 +646,18 @@ UnregisterWakeLockObserver(WakeLockObserver* aObserver)
void
ModifyWakeLock(const nsAString& aTopic,
WakeLockControl aLockAdjust,
WakeLockControl aHiddenAdjust)
WakeLockControl aHiddenAdjust,
uint64_t aProcessID /* = CONTENT_PROCESS_ID_UNKNOWN */)
{
AssertMainThread();
uint64_t processID = InSandbox() ? dom::ContentChild::GetSingleton()->GetID() : 0;
PROXY_IF_SANDBOXED(ModifyWakeLockInternal(aTopic, aLockAdjust, aHiddenAdjust, processID));
}
void
ModifyWakeLockInternal(const nsAString& aTopic,
WakeLockControl aLockAdjust,
WakeLockControl aHiddenAdjust,
uint64_t aProcessID)
{
AssertMainThread();
// TODO: Bug 812403 - support wake locks in nested content processes.
AssertMainProcess();
PROXY_IF_SANDBOXED(ModifyWakeLockInternal(aTopic, aLockAdjust, aHiddenAdjust, aProcessID));
if (aProcessID == CONTENT_PROCESS_ID_UNKNOWN) {
aProcessID = InSandbox() ? ContentChild::GetSingleton()->GetID() :
CONTENT_PROCESS_ID_MAIN;
}
PROXY_IF_SANDBOXED(ModifyWakeLock(aTopic, aLockAdjust,
aHiddenAdjust, aProcessID));
}
void

View File

@ -15,6 +15,7 @@
#include "mozilla/dom/battery/Types.h"
#include "mozilla/dom/network/Types.h"
#include "mozilla/dom/power/Types.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/hal_sandbox/PHal.h"
#include "mozilla/dom/ScreenOrientation.h"
@ -340,26 +341,25 @@ void RegisterWakeLockObserver(WakeLockObserver* aObserver);
void UnregisterWakeLockObserver(WakeLockObserver* aObserver);
/**
* Adjust the wake lock counts.
* Adjust a wake lock's counts on behalf of a given process.
*
* In most cases, you shouldn't need to pass the aProcessID argument; the
* default of CONTENT_PROCESS_ID_UNKNOWN is probably what you want.
*
* @param aTopic lock topic
* @param aLockAdjust to increase or decrease active locks
* @param aHiddenAdjust to increase or decrease hidden locks
* @param aProcessID indicates which process we're modifying the wake lock
* on behalf of. It is interpreted as
*
* CONTENT_PROCESS_ID_UNKNOWN: The current process
* CONTENT_PROCESS_ID_MAIN: The root process
* X: The process with ContentChild::GetID() == X
*/
void ModifyWakeLock(const nsAString &aTopic,
hal::WakeLockControl aLockAdjust,
hal::WakeLockControl aHiddenAdjust);
/**
* Adjust the wake lock counts. Do not call this function directly.
* @param aTopic lock topic
* @param aLockAdjust to increase or decrease active locks
* @param aHiddenAdjust to increase or decrease hidden locks
* @param aProcessID unique id per-ContentChild or 0 for chrome
*/
void ModifyWakeLockInternal(const nsAString &aTopic,
hal::WakeLockControl aLockAdjust,
hal::WakeLockControl aHiddenAdjust,
uint64_t aProcessID);
hal::WakeLockControl aHiddenAdjust,
uint64_t aProcessID = CONTENT_PROCESS_ID_UNKNOWN);
/**
* Query the wake lock numbers of aTopic.

View File

@ -7,6 +7,7 @@
#define mozilla_hal_Types_h
#include "IPCMessageUtils.h"
#include "Observer.h"
namespace mozilla {
namespace hal {

View File

@ -7,6 +7,7 @@
#include "mozilla/HalWakeLock.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/ContentParent.h"
#include "nsClassHashtable.h"
#include "nsDataHashtable.h"
#include "nsHashKeys.h"
@ -44,6 +45,7 @@ struct LockCount {
{}
uint32_t numLocks;
uint32_t numHidden;
nsTArray<uint64_t> processes;
};
typedef nsDataHashtable<nsUint64HashKey, LockCount> ProcessLockTable;
typedef nsClassHashtable<nsStringHashKey, ProcessLockTable> LockTable;
@ -62,6 +64,11 @@ CountWakeLocks(const uint64_t& aKey, LockCount aCount, void* aUserArg)
totalCount->numLocks += aCount.numLocks;
totalCount->numHidden += aCount.numHidden;
// This is linear in the number of processes, but that should be small.
if (!totalCount->processes.Contains(aKey)) {
totalCount->processes.AppendElement(aKey);
}
return PL_DHASH_NEXT;
}
@ -173,12 +180,13 @@ DisableWakeLockNotifications()
}
void
ModifyWakeLockInternal(const nsAString& aTopic,
hal::WakeLockControl aLockAdjust,
hal::WakeLockControl aHiddenAdjust,
uint64_t aProcessID)
ModifyWakeLock(const nsAString& aTopic,
hal::WakeLockControl aLockAdjust,
hal::WakeLockControl aHiddenAdjust,
uint64_t aProcessID)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
if (sIsShuttingDown) {
return;
@ -227,9 +235,7 @@ ModifyWakeLockInternal(const nsAString& aTopic,
if (sActiveListeners && oldState != newState) {
WakeLockInformation info;
info.numLocks() = totalCount.numLocks;
info.numHidden() = totalCount.numHidden;
info.topic() = aTopic;
GetWakeLockInfo(aTopic, &info);
NotifyWakeLockChange(info);
}
}
@ -256,6 +262,7 @@ GetWakeLockInfo(const nsAString& aTopic, WakeLockInformation* aWakeLockInfo)
table->EnumerateRead(CountWakeLocks, &totalCount);
aWakeLockInfo->numLocks() = totalCount.numLocks;
aWakeLockInfo->numHidden() = totalCount.numHidden;
aWakeLockInfo->lockingProcesses() = totalCount.processes;
aWakeLockInfo->topic() = aTopic;
}

View File

@ -66,6 +66,7 @@ struct SwitchEvent {
struct WakeLockInformation {
uint32_t numLocks;
uint32_t numHidden;
uint64_t[] lockingProcesses;
nsString topic;
};

View File

@ -276,11 +276,12 @@ DisableWakeLockNotifications()
}
void
ModifyWakeLockInternal(const nsAString &aTopic,
WakeLockControl aLockAdjust,
WakeLockControl aHiddenAdjust,
uint64_t aProcessID)
ModifyWakeLock(const nsAString &aTopic,
WakeLockControl aLockAdjust,
WakeLockControl aHiddenAdjust,
uint64_t aProcessID)
{
MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
Hal()->SendModifyWakeLock(nsString(aTopic), aLockAdjust, aHiddenAdjust, aProcessID);
}
@ -712,8 +713,10 @@ public:
const WakeLockControl& aHiddenAdjust,
const uint64_t& aProcessID) MOZ_OVERRIDE
{
MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
// We allow arbitrary content to use wake locks.
hal::ModifyWakeLockInternal(aTopic, aLockAdjust, aHiddenAdjust, aProcessID);
hal::ModifyWakeLock(aTopic, aLockAdjust, aHiddenAdjust, aProcessID);
return true;
}