2012-03-07 03:03:25 -08: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-05 19:09:46 -07:00
|
|
|
#include "mozilla/Services.h"
|
2012-11-17 07:05:18 -08:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-02-14 12:41:30 -08:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2012-11-17 07:05:18 -08:00
|
|
|
#include "nsClassHashtable.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2012-11-17 07:05:18 -08:00
|
|
|
#include "nsIPropertyBag2.h"
|
|
|
|
#include "nsObserverService.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2013-05-10 08:20:52 -07:00
|
|
|
using namespace mozilla;
|
2012-03-07 03:03:25 -08:00
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
|
|
|
namespace {
|
2013-05-10 08:20:52 -07:00
|
|
|
|
2012-03-07 03:03:25 -08:00
|
|
|
struct LockCount {
|
2012-11-17 07:05:18 -08:00
|
|
|
LockCount()
|
|
|
|
: numLocks(0)
|
|
|
|
, numHidden(0)
|
|
|
|
{}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numLocks;
|
|
|
|
uint32_t numHidden;
|
2013-02-14 12:41:30 -08:00
|
|
|
nsTArray<uint64_t> processes;
|
2012-03-07 03:03:25 -08:00
|
|
|
};
|
2013-05-10 08:20:52 -07:00
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
typedef nsDataHashtable<nsUint64HashKey, LockCount> ProcessLockTable;
|
|
|
|
typedef nsClassHashtable<nsStringHashKey, ProcessLockTable> LockTable;
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2013-05-10 08:20:52 -07:00
|
|
|
int sActiveListeners = 0;
|
|
|
|
StaticAutoPtr<LockTable> sLockTable;
|
|
|
|
bool sInitialized = false;
|
|
|
|
bool sIsShuttingDown = false;
|
2012-07-05 19:09:46 -07:00
|
|
|
|
2013-05-10 08:20:52 -07:00
|
|
|
WakeLockInformation
|
|
|
|
WakeLockInfoFromLockCount(const nsAString& aTopic, const LockCount& aLockCount)
|
|
|
|
{
|
|
|
|
// TODO: Once we abandon b2g18, we can switch this to use the
|
|
|
|
// WakeLockInformation constructor, which is better because it doesn't let us
|
|
|
|
// forget to assign a param. For now we have to do it this way, because
|
|
|
|
// b2g18 doesn't have the nsTArray <--> InfallibleTArray conversion (bug
|
|
|
|
// 819791).
|
|
|
|
|
|
|
|
WakeLockInformation info;
|
|
|
|
info.topic() = aTopic;
|
|
|
|
info.numLocks() = aLockCount.numLocks;
|
|
|
|
info.numHidden() = aLockCount.numHidden;
|
|
|
|
info.lockingProcesses().AppendElements(aLockCount.processes);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashOperator
|
2012-11-26 18:28:11 -08:00
|
|
|
CountWakeLocks(const uint64_t& aKey, LockCount aCount, void* aUserArg)
|
2012-11-17 07:05:18 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aUserArg);
|
|
|
|
|
2012-11-26 18:28:11 -08:00
|
|
|
LockCount* totalCount = static_cast<LockCount*>(aUserArg);
|
|
|
|
totalCount->numLocks += aCount.numLocks;
|
|
|
|
totalCount->numHidden += aCount.numHidden;
|
2012-11-17 07:05:18 -08:00
|
|
|
|
2013-02-14 12:41:30 -08: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 07:05:18 -08:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2012-12-27 02:43:55 -08:00
|
|
|
RemoveChildFromList(const nsAString& aKey, nsAutoPtr<ProcessLockTable>& aTable,
|
|
|
|
void* aUserArg)
|
2012-11-17 07:05:18 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aUserArg);
|
|
|
|
|
2012-12-27 02:43:55 -08:00
|
|
|
PLDHashOperator op = PL_DHASH_NEXT;
|
2012-11-26 18:28:11 -08:00
|
|
|
uint64_t childID = *static_cast<uint64_t*>(aUserArg);
|
2013-05-10 08:20:52 -07:00
|
|
|
if (aTable->Get(childID, nullptr)) {
|
2012-11-26 18:28:11 -08:00
|
|
|
aTable->Remove(childID);
|
2013-05-10 08:20:52 -07:00
|
|
|
|
|
|
|
LockCount totalCount;
|
|
|
|
aTable->EnumerateRead(CountWakeLocks, &totalCount);
|
|
|
|
if (!totalCount.numLocks) {
|
|
|
|
op = PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:28:11 -08:00
|
|
|
if (sActiveListeners) {
|
2013-05-10 08:20:52 -07:00
|
|
|
NotifyWakeLockChange(WakeLockInfoFromLockCount(aKey, totalCount));
|
2012-11-26 18:28:11 -08:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 07:05:18 -08:00
|
|
|
|
2012-12-27 02:43:55 -08:00
|
|
|
return op;
|
2012-11-17 07:05:18 -08:00
|
|
|
}
|
|
|
|
|
2012-07-15 08:13:15 -07:00
|
|
|
class ClearHashtableOnShutdown MOZ_FINAL : public nsIObserver {
|
2012-07-05 19:09:46 -07: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 07:20:58 -07:00
|
|
|
sLockTable = nullptr;
|
2012-07-05 19:09:46 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-17 07:05:18 -08: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 02:43:55 -08:00
|
|
|
sLockTable->Enumerate(RemoveChildFromList, &childID);
|
2012-11-17 07:05:18 -08:00
|
|
|
} else {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without childID property");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2013-05-10 08:20:52 -07:00
|
|
|
void
|
2012-03-07 03:03:25 -08:00
|
|
|
Init()
|
|
|
|
{
|
2012-11-17 07:05:18 -08:00
|
|
|
sLockTable = new LockTable();
|
2012-03-07 03:03:25 -08:00
|
|
|
sLockTable->Init();
|
|
|
|
sInitialized = true;
|
2012-07-05 19:09:46 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(new ClearHashtableOnShutdown(), "xpcom-shutdown", false);
|
2012-11-17 07:05:18 -08:00
|
|
|
obs->AddObserver(new CleanupOnContentShutdown(), "ipc:content-shutdown", false);
|
2012-07-05 19:09:46 -07:00
|
|
|
}
|
2012-03-07 03:03:25 -08:00
|
|
|
}
|
2013-05-10 08:20:52 -07:00
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
} // anonymous namespace
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2013-05-10 08:20:52 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace hal
|
|
|
|
|
|
|
|
namespace hal_impl {
|
|
|
|
|
2012-03-07 03:03:25 -08:00
|
|
|
void
|
|
|
|
EnableWakeLockNotifications()
|
|
|
|
{
|
2012-11-17 07:05:18 -08:00
|
|
|
sActiveListeners++;
|
2012-03-07 03:03:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableWakeLockNotifications()
|
|
|
|
{
|
2012-11-17 07:05:18 -08:00
|
|
|
sActiveListeners--;
|
2012-03-07 03:03:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-14 12:41:30 -08:00
|
|
|
ModifyWakeLock(const nsAString& aTopic,
|
|
|
|
hal::WakeLockControl aLockAdjust,
|
|
|
|
hal::WakeLockControl aHiddenAdjust,
|
|
|
|
uint64_t aProcessID)
|
2012-03-07 03:03:25 -08:00
|
|
|
{
|
2012-11-26 18:28:11 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-02-14 12:41:30 -08:00
|
|
|
MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
|
2012-11-26 18:28:11 -08:00
|
|
|
|
2012-07-05 19:09:46 -07:00
|
|
|
if (sIsShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-07 03:03:25 -08:00
|
|
|
if (!sInitialized) {
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2012-11-17 07:05:18 -08: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 03:03:25 -08:00
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
WakeLockState oldState = ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden);
|
2013-05-10 08:20:52 -07:00
|
|
|
bool processWasLocked = processCount.numLocks > 0;
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
processCount.numLocks += aLockAdjust;
|
|
|
|
processCount.numHidden += aHiddenAdjust;
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
totalCount.numLocks += aLockAdjust;
|
|
|
|
totalCount.numHidden += aHiddenAdjust;
|
|
|
|
|
|
|
|
if (processCount.numLocks) {
|
|
|
|
table->Put(aProcessID, processCount);
|
2012-03-07 03:03:25 -08:00
|
|
|
} else {
|
2012-11-17 07:05:18 -08:00
|
|
|
table->Remove(aProcessID);
|
|
|
|
}
|
|
|
|
if (!totalCount.numLocks) {
|
2012-03-07 03:03:25 -08:00
|
|
|
sLockTable->Remove(aTopic);
|
|
|
|
}
|
|
|
|
|
2013-05-10 08:20:52 -07:00
|
|
|
if (sActiveListeners &&
|
|
|
|
(oldState != ComputeWakeLockState(totalCount.numLocks,
|
|
|
|
totalCount.numHidden) ||
|
2013-05-10 08:58:09 -07:00
|
|
|
processWasLocked != (processCount.numLocks > 0))) {
|
2012-03-07 03:03:25 -08:00
|
|
|
|
|
|
|
WakeLockInformation info;
|
2013-02-14 12:41:30 -08:00
|
|
|
GetWakeLockInfo(aTopic, &info);
|
2012-03-07 03:03:25 -08:00
|
|
|
NotifyWakeLockChange(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-17 07:05:18 -08:00
|
|
|
GetWakeLockInfo(const nsAString& aTopic, WakeLockInformation* aWakeLockInfo)
|
2012-03-07 03:03:25 -08:00
|
|
|
{
|
2012-07-05 19:09:46 -07:00
|
|
|
if (sIsShuttingDown) {
|
|
|
|
NS_WARNING("You don't want to get wake lock information during xpcom-shutdown!");
|
2013-05-10 08:20:52 -07:00
|
|
|
*aWakeLockInfo = WakeLockInformation();
|
2012-07-05 19:09:46 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-03-07 03:03:25 -08:00
|
|
|
if (!sInitialized) {
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
ProcessLockTable* table = sLockTable->Get(aTopic);
|
|
|
|
if (!table) {
|
2013-05-10 08:20:52 -07:00
|
|
|
*aWakeLockInfo = WakeLockInfoFromLockCount(aTopic, LockCount());
|
2012-11-17 07:05:18 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LockCount totalCount;
|
|
|
|
table->EnumerateRead(CountWakeLocks, &totalCount);
|
2013-05-10 08:20:52 -07:00
|
|
|
*aWakeLockInfo = WakeLockInfoFromLockCount(aTopic, totalCount);
|
2012-03-07 03:03:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // hal_impl
|
|
|
|
} // mozilla
|