2015-10-20 07:35:20 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-03-07 15:03:25 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2014-09-24 17:23:18 +04:00
|
|
|
#include "Hal.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "mozilla/HalWakeLock.h"
|
2012-07-06 06:09:46 +04:00
|
|
|
#include "mozilla/Services.h"
|
2012-11-17 19:05:18 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsClassHashtable.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2012-11-17 19:05:18 +04:00
|
|
|
#include "nsIPropertyBag2.h"
|
2014-04-14 23:04:27 +04:00
|
|
|
#include "nsIObserverService.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2013-05-10 19:20:52 +04:00
|
|
|
using namespace mozilla;
|
2012-03-07 15:03:25 +04:00
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
|
|
|
namespace {
|
2013-05-10 19:20:52 +04:00
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
struct LockCount {
|
2012-11-17 19:05:18 +04:00
|
|
|
LockCount() : numLocks(0), numHidden(0) {}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numLocks;
|
|
|
|
uint32_t numHidden;
|
2013-02-15 00:41:30 +04:00
|
|
|
nsTArray<uint64_t> processes;
|
2012-03-07 15:03:25 +04:00
|
|
|
};
|
2013-05-10 19:20:52 +04:00
|
|
|
|
2012-11-17 19:05:18 +04:00
|
|
|
typedef nsDataHashtable<nsUint64HashKey, LockCount> ProcessLockTable;
|
|
|
|
typedef nsClassHashtable<nsStringHashKey, ProcessLockTable> LockTable;
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2013-05-10 19:20:52 +04:00
|
|
|
int sActiveListeners = 0;
|
|
|
|
StaticAutoPtr<LockTable> sLockTable;
|
|
|
|
bool sIsShuttingDown = false;
|
2012-07-06 06:09:46 +04:00
|
|
|
|
2013-05-10 19:20:52 +04:00
|
|
|
WakeLockInformation WakeLockInfoFromLockCount(const nsAString& aTopic,
|
|
|
|
const LockCount& aLockCount) {
|
2018-08-31 23:29:30 +03:00
|
|
|
nsString topic(aTopic);
|
|
|
|
WakeLockInformation info(topic, aLockCount.numLocks, aLockCount.numHidden,
|
|
|
|
aLockCount.processes);
|
|
|
|
|
2013-05-10 19:20:52 +04:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2015-10-20 07:35:20 +03:00
|
|
|
static void CountWakeLocks(ProcessLockTable* aTable, LockCount* aTotalCount) {
|
|
|
|
for (auto iter = aTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
const uint64_t& key = iter.Key();
|
|
|
|
LockCount count = iter.UserData();
|
2012-11-17 19:05:18 +04:00
|
|
|
|
2015-10-20 07:35:20 +03:00
|
|
|
aTotalCount->numLocks += count.numLocks;
|
|
|
|
aTotalCount->numHidden += count.numHidden;
|
2012-11-17 19:05:18 +04:00
|
|
|
|
2015-10-20 07:35:20 +03:00
|
|
|
// This is linear in the number of processes, but that should be small.
|
|
|
|
if (!aTotalCount->processes.Contains(key)) {
|
|
|
|
aTotalCount->processes.AppendElement(key);
|
|
|
|
}
|
2013-02-15 00:41:30 +04:00
|
|
|
}
|
2012-11-17 19:05:18 +04:00
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ClearHashtableOnShutdown final : public nsIObserver {
|
2014-06-24 02:40:03 +04:00
|
|
|
~ClearHashtableOnShutdown() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-07-06 06:09:46 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ClearHashtableOnShutdown, nsIObserver)
|
2012-07-06 06:09:46 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
ClearHashtableOnShutdown::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* data) {
|
2012-07-06 06:09:46 +04:00
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown"));
|
|
|
|
|
|
|
|
sIsShuttingDown = true;
|
2012-07-30 18:20:58 +04:00
|
|
|
sLockTable = nullptr;
|
2012-07-06 06:09:46 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-17 19:05:18 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CleanupOnContentShutdown final : public nsIObserver {
|
2014-06-24 02:40:03 +04:00
|
|
|
~CleanupOnContentShutdown() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-11-17 19:05:18 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CleanupOnContentShutdown, nsIObserver)
|
2012-11-17 19:05:18 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
CleanupOnContentShutdown::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* data) {
|
2012-11-17 19:05:18 +04:00
|
|
|
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)) {
|
2015-10-20 07:52:44 +03:00
|
|
|
for (auto iter = sLockTable->Iter(); !iter.Done(); iter.Next()) {
|
2020-01-13 22:18:56 +03:00
|
|
|
auto table = iter.UserData();
|
2015-10-20 07:52:44 +03:00
|
|
|
|
|
|
|
if (table->Get(childID, nullptr)) {
|
|
|
|
table->Remove(childID);
|
|
|
|
|
|
|
|
LockCount totalCount;
|
|
|
|
CountWakeLocks(table, &totalCount);
|
|
|
|
|
|
|
|
if (sActiveListeners) {
|
|
|
|
NotifyWakeLockChange(
|
|
|
|
WakeLockInfoFromLockCount(iter.Key(), totalCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (totalCount.numLocks == 0) {
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 19:05:18 +04:00
|
|
|
} else {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without childID property");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2018-08-31 23:29:30 +03:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace hal {
|
|
|
|
|
|
|
|
void WakeLockInit() {
|
2012-11-17 19:05:18 +04:00
|
|
|
sLockTable = new LockTable();
|
2012-07-06 06:09:46 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(new ClearHashtableOnShutdown(), "xpcom-shutdown", false);
|
2012-11-17 19:05:18 +04:00
|
|
|
obs->AddObserver(new CleanupOnContentShutdown(), "ipc:content-shutdown",
|
|
|
|
false);
|
2012-07-06 06:09:46 +04:00
|
|
|
}
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
2013-05-10 19:20:52 +04:00
|
|
|
|
|
|
|
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 15:03:25 +04:00
|
|
|
void EnableWakeLockNotifications() { sActiveListeners++; }
|
|
|
|
|
|
|
|
void DisableWakeLockNotifications() { sActiveListeners--; }
|
|
|
|
|
2013-02-15 00:41:30 +04:00
|
|
|
void ModifyWakeLock(const nsAString& aTopic, hal::WakeLockControl aLockAdjust,
|
|
|
|
hal::WakeLockControl aHiddenAdjust, uint64_t aProcessID) {
|
2012-11-27 06:28:11 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-02-15 00:41:30 +04:00
|
|
|
MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
|
2012-11-27 06:28:11 +04:00
|
|
|
|
2012-07-06 06:09:46 +04:00
|
|
|
if (sIsShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2012-11-17 19:05:18 +04:00
|
|
|
ProcessLockTable* table = sLockTable->Get(aTopic);
|
|
|
|
LockCount processCount;
|
|
|
|
LockCount totalCount;
|
|
|
|
if (!table) {
|
|
|
|
table = new ProcessLockTable();
|
|
|
|
sLockTable->Put(aTopic, table);
|
|
|
|
} else {
|
|
|
|
table->Get(aProcessID, &processCount);
|
2015-10-20 07:35:20 +03:00
|
|
|
CountWakeLocks(table, &totalCount);
|
2012-11-17 19:05:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 15:03:25 +04:00
|
|
|
|
2012-11-17 19:05:18 +04:00
|
|
|
WakeLockState oldState =
|
|
|
|
ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden);
|
2013-05-10 19:20:52 +04:00
|
|
|
bool processWasLocked = processCount.numLocks > 0;
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2012-11-17 19:05:18 +04:00
|
|
|
processCount.numLocks += aLockAdjust;
|
|
|
|
processCount.numHidden += aHiddenAdjust;
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2012-11-17 19:05:18 +04:00
|
|
|
totalCount.numLocks += aLockAdjust;
|
|
|
|
totalCount.numHidden += aHiddenAdjust;
|
|
|
|
|
|
|
|
if (processCount.numLocks) {
|
|
|
|
table->Put(aProcessID, processCount);
|
2012-03-07 15:03:25 +04:00
|
|
|
} else {
|
2012-11-17 19:05:18 +04:00
|
|
|
table->Remove(aProcessID);
|
|
|
|
}
|
|
|
|
if (!totalCount.numLocks) {
|
2012-03-07 15:03:25 +04:00
|
|
|
sLockTable->Remove(aTopic);
|
|
|
|
}
|
|
|
|
|
2013-05-10 19:20:52 +04:00
|
|
|
if (sActiveListeners &&
|
|
|
|
(oldState !=
|
|
|
|
ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden) ||
|
2013-05-10 19:58:09 +04:00
|
|
|
processWasLocked != (processCount.numLocks > 0))) {
|
2012-03-07 15:03:25 +04:00
|
|
|
WakeLockInformation info;
|
2013-11-25 23:57:18 +04:00
|
|
|
hal::GetWakeLockInfo(aTopic, &info);
|
2012-03-07 15:03:25 +04:00
|
|
|
NotifyWakeLockChange(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 19:05:18 +04:00
|
|
|
void GetWakeLockInfo(const nsAString& aTopic,
|
|
|
|
WakeLockInformation* aWakeLockInfo) {
|
2012-07-06 06:09:46 +04:00
|
|
|
if (sIsShuttingDown) {
|
|
|
|
NS_WARNING(
|
|
|
|
"You don't want to get wake lock information during xpcom-shutdown!");
|
2013-05-10 19:20:52 +04:00
|
|
|
*aWakeLockInfo = WakeLockInformation();
|
2012-07-06 06:09:46 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2012-11-17 19:05:18 +04:00
|
|
|
ProcessLockTable* table = sLockTable->Get(aTopic);
|
|
|
|
if (!table) {
|
2013-05-10 19:20:52 +04:00
|
|
|
*aWakeLockInfo = WakeLockInfoFromLockCount(aTopic, LockCount());
|
2012-11-17 19:05:18 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LockCount totalCount;
|
2015-10-20 07:35:20 +03:00
|
|
|
CountWakeLocks(table, &totalCount);
|
2013-05-10 19:20:52 +04:00
|
|
|
*aWakeLockInfo = WakeLockInfoFromLockCount(aTopic, totalCount);
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace hal_impl
|
|
|
|
} // namespace mozilla
|