Skip to content

Commit

Permalink
Bug 836654 - Part 3: Modify hal to accept a ContentParent ID in Modif…
Browse files Browse the repository at this point in the history
…yWakeLock(). r=cjones

Also return the list of processes holding the lock in GetWakeLockInfo
and in the wake-lock changed notification.
  • Loading branch information
jlebar committed Feb 14, 2013
1 parent 2810c72 commit 0c5ba48
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 40 deletions.
25 changes: 11 additions & 14 deletions hal/Hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
#include "WindowIdentifier.h"
#include "mozilla/dom/ScreenOrientation.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/ContentParent.h"

#ifdef XP_WIN
#include <process.h>
#define getpid _getpid
#endif

using namespace mozilla::services;
using namespace mozilla::dom;

#define PROXY_IF_SANDBOXED(_call) \
do { \
Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions hal/Hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions hal/HalTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define mozilla_hal_Types_h

#include "IPCMessageUtils.h"
#include "Observer.h"

namespace mozilla {
namespace hal {
Expand Down
21 changes: 14 additions & 7 deletions hal/HalWakeLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions hal/sandbox/PHal.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct SwitchEvent {
struct WakeLockInformation {
uint32_t numLocks;
uint32_t numHidden;
uint64_t[] lockingProcesses;
nsString topic;
};

Expand Down
13 changes: 8 additions & 5 deletions hal/sandbox/SandboxHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -712,8 +713,10 @@ class HalParent : public PHalParent
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;
}

Expand Down

0 comments on commit 0c5ba48

Please sign in to comment.