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"
|
|
|
|
#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
|
|
|
|
|
|
|
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 07:05:18 -08:00
|
|
|
LockCount()
|
|
|
|
: numLocks(0)
|
|
|
|
, numHidden(0)
|
|
|
|
{}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numLocks;
|
|
|
|
uint32_t numHidden;
|
2012-03-07 03:03:25 -08: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
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
static int sActiveListeners = 0;
|
|
|
|
static StaticAutoPtr<LockTable> sLockTable;
|
2012-03-07 03:03:25 -08:00
|
|
|
static bool sInitialized = false;
|
2012-07-05 19:09:46 -07:00
|
|
|
static bool sIsShuttingDown = false;
|
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
static PLDHashOperator
|
|
|
|
RemoveChildFromList(const nsAString& aKey, ProcessLockTable* aTable, void* aUserArg)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aUserArg);
|
|
|
|
|
|
|
|
uint64_t childID = *static_cast<uint64_t*>(aUserArg);
|
|
|
|
aTable->Remove(childID);
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
CountWakeLocks(const uint64_t& aKey, LockCount aCount, void* aUserArg)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aUserArg);
|
|
|
|
|
|
|
|
LockCount* totalCount = static_cast<LockCount*>(aUserArg);
|
|
|
|
totalCount->numLocks += aCount.numLocks;
|
|
|
|
totalCount->numHidden += aCount.numHidden;
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
sLockTable->EnumerateRead(RemoveChildFromList, &childID);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without childID property");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-07 03:03:25 -08:00
|
|
|
|
|
|
|
static void
|
|
|
|
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
|
|
|
}
|
2012-11-17 07:05:18 -08:00
|
|
|
} // anonymous namespace
|
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
|
2012-11-17 07:05:18 -08:00
|
|
|
ModifyWakeLockInternal(const nsAString& aTopic,
|
|
|
|
hal::WakeLockControl aLockAdjust,
|
|
|
|
hal::WakeLockControl aHiddenAdjust,
|
|
|
|
uint64_t aProcessID)
|
2012-03-07 03:03:25 -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);
|
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);
|
|
|
|
}
|
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
WakeLockState newState = ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden);
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2012-11-17 07:05:18 -08:00
|
|
|
if (sActiveListeners && oldState != newState) {
|
2012-03-07 03:03:25 -08:00
|
|
|
WakeLockInformation info;
|
2012-11-17 07:05:18 -08:00
|
|
|
info.numLocks() = totalCount.numLocks;
|
|
|
|
info.numHidden() = totalCount.numHidden;
|
2012-03-07 03:03:25 -08:00
|
|
|
info.topic() = aTopic;
|
|
|
|
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!");
|
|
|
|
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) {
|
|
|
|
aWakeLockInfo->numLocks() = 0;
|
|
|
|
aWakeLockInfo->numHidden() = 0;
|
|
|
|
aWakeLockInfo->topic() = aTopic;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LockCount totalCount;
|
|
|
|
table->EnumerateRead(CountWakeLocks, &totalCount);
|
|
|
|
aWakeLockInfo->numLocks() = totalCount.numLocks;
|
|
|
|
aWakeLockInfo->numHidden() = totalCount.numHidden;
|
2012-03-07 03:03:25 -08:00
|
|
|
aWakeLockInfo->topic() = aTopic;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // hal_impl
|
|
|
|
} // mozilla
|