2012-03-07 11:03:25 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "mozilla/Hal.h"
|
|
|
|
#include "mozilla/HalWakeLock.h"
|
2012-07-06 02:09:46 +00:00
|
|
|
#include "mozilla/Services.h"
|
2012-11-17 15:05:18 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-02-14 20:41:30 +00:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2012-11-17 15:05:18 +00:00
|
|
|
#include "nsClassHashtable.h"
|
2012-03-07 11:03:25 +00:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2012-11-17 15:05:18 +00:00
|
|
|
#include "nsIPropertyBag2.h"
|
|
|
|
#include "nsObserverService.h"
|
2012-03-07 11:03:25 +00:00
|
|
|
|
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace hal {
|
|
|
|
|
|
|
|
WakeLockState
|
|
|
|
ComputeWakeLockState(int aNumLocks, int aNumHidden)
|
|
|
|
{
|
|
|
|
if (aNumLocks == 0) {
|
|
|
|
return WAKE_LOCK_STATE_UNLOCKED;
|
|
|
|
} else if (aNumLocks == aNumHidden) {
|
|
|
|
return WAKE_LOCK_STATE_HIDDEN;
|
|
|
|
} else {
|
|
|
|
return WAKE_LOCK_STATE_VISIBLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // hal
|
|
|
|
} // mozilla
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace hal_impl {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct LockCount {
|
2012-11-17 15:05:18 +00:00
|
|
|
LockCount()
|
|
|
|
: numLocks(0)
|
|
|
|
, numHidden(0)
|
|
|
|
{}
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t numLocks;
|
|
|
|
uint32_t numHidden;
|
2013-02-14 20:41:30 +00:00
|
|
|
nsTArray<uint64_t> processes;
|
2012-03-07 11:03:25 +00:00
|
|
|
};
|
2012-11-17 15:05:18 +00:00
|
|
|
typedef nsDataHashtable<nsUint64HashKey, LockCount> ProcessLockTable;
|
|
|
|
typedef nsClassHashtable<nsStringHashKey, ProcessLockTable> LockTable;
|
2012-03-07 11:03:25 +00:00
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
static int sActiveListeners = 0;
|
|
|
|
static StaticAutoPtr<LockTable> sLockTable;
|
2012-03-07 11:03:25 +00:00
|
|
|
static bool sInitialized = false;
|
2012-07-06 02:09:46 +00:00
|
|
|
static bool sIsShuttingDown = false;
|
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
static PLDHashOperator
|
2012-11-27 02:28:11 +00:00
|
|
|
CountWakeLocks(const uint64_t& aKey, LockCount aCount, void* aUserArg)
|
2012-11-17 15:05:18 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aUserArg);
|
|
|
|
|
2012-11-27 02:28:11 +00:00
|
|
|
LockCount* totalCount = static_cast<LockCount*>(aUserArg);
|
|
|
|
totalCount->numLocks += aCount.numLocks;
|
|
|
|
totalCount->numHidden += aCount.numHidden;
|
2012-11-17 15:05:18 +00:00
|
|
|
|
2013-02-14 20:41:30 +00:00
|
|
|
// This is linear in the number of processes, but that should be small.
|
|
|
|
if (!totalCount->processes.Contains(aKey)) {
|
|
|
|
totalCount->processes.AppendElement(aKey);
|
|
|
|
}
|
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2012-12-27 10:43:55 +00:00
|
|
|
RemoveChildFromList(const nsAString& aKey, nsAutoPtr<ProcessLockTable>& aTable,
|
|
|
|
void* aUserArg)
|
2012-11-17 15:05:18 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aUserArg);
|
|
|
|
|
2012-12-27 10:43:55 +00:00
|
|
|
PLDHashOperator op = PL_DHASH_NEXT;
|
2012-11-27 02:28:11 +00:00
|
|
|
uint64_t childID = *static_cast<uint64_t*>(aUserArg);
|
|
|
|
if (aTable->Get(childID, NULL)) {
|
|
|
|
aTable->Remove(childID);
|
|
|
|
if (sActiveListeners) {
|
|
|
|
LockCount totalCount;
|
|
|
|
WakeLockInformation info;
|
|
|
|
aTable->EnumerateRead(CountWakeLocks, &totalCount);
|
|
|
|
if (!totalCount.numLocks) {
|
2012-12-27 10:43:55 +00:00
|
|
|
op = PL_DHASH_REMOVE;
|
2012-11-27 02:28:11 +00:00
|
|
|
}
|
|
|
|
info.numLocks() = totalCount.numLocks;
|
|
|
|
info.numHidden() = totalCount.numHidden;
|
|
|
|
info.topic() = aKey;
|
|
|
|
NotifyWakeLockChange(info);
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 15:05:18 +00:00
|
|
|
|
2012-12-27 10:43:55 +00:00
|
|
|
return op;
|
2012-11-17 15:05:18 +00:00
|
|
|
}
|
|
|
|
|
2012-07-15 15:13:15 +00:00
|
|
|
class ClearHashtableOnShutdown MOZ_FINAL : public nsIObserver {
|
2012-07-06 02:09:46 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(ClearHashtableOnShutdown, nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ClearHashtableOnShutdown::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* data)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown"));
|
|
|
|
|
|
|
|
sIsShuttingDown = true;
|
2012-07-30 14:20:58 +00:00
|
|
|
sLockTable = nullptr;
|
2012-07-06 02:09:46 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-17 15:05:18 +00:00
|
|
|
|
|
|
|
class CleanupOnContentShutdown MOZ_FINAL : public nsIObserver {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(CleanupOnContentShutdown, nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CleanupOnContentShutdown::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* data)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "ipc:content-shutdown"));
|
|
|
|
|
|
|
|
if (sIsShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
|
|
|
|
if (!props) {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without property bag as subject");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t childID = 0;
|
|
|
|
nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"),
|
|
|
|
&childID);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-12-27 10:43:55 +00:00
|
|
|
sLockTable->Enumerate(RemoveChildFromList, &childID);
|
2012-11-17 15:05:18 +00:00
|
|
|
} else {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without childID property");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-07 11:03:25 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
Init()
|
|
|
|
{
|
2012-11-17 15:05:18 +00:00
|
|
|
sLockTable = new LockTable();
|
2012-03-07 11:03:25 +00:00
|
|
|
sLockTable->Init();
|
|
|
|
sInitialized = true;
|
2012-07-06 02:09:46 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(new ClearHashtableOnShutdown(), "xpcom-shutdown", false);
|
2012-11-17 15:05:18 +00:00
|
|
|
obs->AddObserver(new CleanupOnContentShutdown(), "ipc:content-shutdown", false);
|
2012-07-06 02:09:46 +00:00
|
|
|
}
|
2012-03-07 11:03:25 +00:00
|
|
|
}
|
2012-11-17 15:05:18 +00:00
|
|
|
} // anonymous namespace
|
2012-03-07 11:03:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
EnableWakeLockNotifications()
|
|
|
|
{
|
2012-11-17 15:05:18 +00:00
|
|
|
sActiveListeners++;
|
2012-03-07 11:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableWakeLockNotifications()
|
|
|
|
{
|
2012-11-17 15:05:18 +00:00
|
|
|
sActiveListeners--;
|
2012-03-07 11:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-14 20:41:30 +00:00
|
|
|
ModifyWakeLock(const nsAString& aTopic,
|
|
|
|
hal::WakeLockControl aLockAdjust,
|
|
|
|
hal::WakeLockControl aHiddenAdjust,
|
|
|
|
uint64_t aProcessID)
|
2012-03-07 11:03:25 +00:00
|
|
|
{
|
2012-11-27 02:28:11 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-02-14 20:41:30 +00:00
|
|
|
MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
|
2012-11-27 02:28:11 +00:00
|
|
|
|
2012-07-06 02:09:46 +00:00
|
|
|
if (sIsShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-07 11:03:25 +00:00
|
|
|
if (!sInitialized) {
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
ProcessLockTable* table = sLockTable->Get(aTopic);
|
|
|
|
LockCount processCount;
|
|
|
|
LockCount totalCount;
|
|
|
|
if (!table) {
|
|
|
|
table = new ProcessLockTable();
|
|
|
|
table->Init();
|
|
|
|
sLockTable->Put(aTopic, table);
|
|
|
|
} else {
|
|
|
|
table->Get(aProcessID, &processCount);
|
|
|
|
table->EnumerateRead(CountWakeLocks, &totalCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(processCount.numLocks >= processCount.numHidden);
|
|
|
|
MOZ_ASSERT(aLockAdjust >= 0 || processCount.numLocks > 0);
|
|
|
|
MOZ_ASSERT(aHiddenAdjust >= 0 || processCount.numHidden > 0);
|
|
|
|
MOZ_ASSERT(totalCount.numLocks >= totalCount.numHidden);
|
|
|
|
MOZ_ASSERT(aLockAdjust >= 0 || totalCount.numLocks > 0);
|
|
|
|
MOZ_ASSERT(aHiddenAdjust >= 0 || totalCount.numHidden > 0);
|
2012-03-07 11:03:25 +00:00
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
WakeLockState oldState = ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden);
|
2012-03-07 11:03:25 +00:00
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
processCount.numLocks += aLockAdjust;
|
|
|
|
processCount.numHidden += aHiddenAdjust;
|
2012-03-07 11:03:25 +00:00
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
totalCount.numLocks += aLockAdjust;
|
|
|
|
totalCount.numHidden += aHiddenAdjust;
|
|
|
|
|
|
|
|
if (processCount.numLocks) {
|
|
|
|
table->Put(aProcessID, processCount);
|
2012-03-07 11:03:25 +00:00
|
|
|
} else {
|
2012-11-17 15:05:18 +00:00
|
|
|
table->Remove(aProcessID);
|
|
|
|
}
|
|
|
|
if (!totalCount.numLocks) {
|
2012-03-07 11:03:25 +00:00
|
|
|
sLockTable->Remove(aTopic);
|
|
|
|
}
|
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
WakeLockState newState = ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden);
|
2012-03-07 11:03:25 +00:00
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
if (sActiveListeners && oldState != newState) {
|
2012-03-07 11:03:25 +00:00
|
|
|
WakeLockInformation info;
|
2013-02-14 20:41:30 +00:00
|
|
|
GetWakeLockInfo(aTopic, &info);
|
2012-03-07 11:03:25 +00:00
|
|
|
NotifyWakeLockChange(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-17 15:05:18 +00:00
|
|
|
GetWakeLockInfo(const nsAString& aTopic, WakeLockInformation* aWakeLockInfo)
|
2012-03-07 11:03:25 +00:00
|
|
|
{
|
2012-07-06 02:09:46 +00:00
|
|
|
if (sIsShuttingDown) {
|
|
|
|
NS_WARNING("You don't want to get wake lock information during xpcom-shutdown!");
|
|
|
|
return;
|
|
|
|
}
|
2012-03-07 11:03:25 +00:00
|
|
|
if (!sInitialized) {
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
ProcessLockTable* table = sLockTable->Get(aTopic);
|
|
|
|
if (!table) {
|
|
|
|
aWakeLockInfo->numLocks() = 0;
|
|
|
|
aWakeLockInfo->numHidden() = 0;
|
|
|
|
aWakeLockInfo->topic() = aTopic;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LockCount totalCount;
|
|
|
|
table->EnumerateRead(CountWakeLocks, &totalCount);
|
|
|
|
aWakeLockInfo->numLocks() = totalCount.numLocks;
|
|
|
|
aWakeLockInfo->numHidden() = totalCount.numHidden;
|
2013-02-14 20:41:30 +00:00
|
|
|
aWakeLockInfo->lockingProcesses() = totalCount.processes;
|
2012-03-07 11:03:25 +00:00
|
|
|
aWakeLockInfo->topic() = aTopic;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // hal_impl
|
|
|
|
} // mozilla
|