2015-10-20 04:35:20 +00: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 11:03:25 +00: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 13:23:18 +00:00
|
|
|
#include "Hal.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "base/process_util.h"
|
2022-02-03 20:37:21 +00:00
|
|
|
#include "mozilla/FOGIPC.h"
|
2012-03-07 11:03:25 +00:00
|
|
|
#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"
|
|
|
|
#include "nsClassHashtable.h"
|
2021-03-10 10:47:47 +00:00
|
|
|
#include "nsTHashMap.h"
|
2012-03-07 11:03:25 +00:00
|
|
|
#include "nsHashKeys.h"
|
2012-11-17 15:05:18 +00:00
|
|
|
#include "nsIPropertyBag2.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "nsIObserver.h"
|
2014-04-14 19:04:27 +00:00
|
|
|
#include "nsIObserverService.h"
|
2012-03-07 11:03:25 +00:00
|
|
|
|
2013-05-10 15:20:52 +00:00
|
|
|
using namespace mozilla;
|
2012-03-07 11:03:25 +00:00
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
|
|
|
namespace {
|
2013-05-10 15:20:52 +00:00
|
|
|
|
2012-03-07 11:03:25 +00:00
|
|
|
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;
|
2020-05-05 10:14:24 +00:00
|
|
|
CopyableTArray<uint64_t> processes;
|
2012-03-07 11:03:25 +00:00
|
|
|
};
|
2013-05-10 15:20:52 +00:00
|
|
|
|
2021-03-10 10:47:47 +00:00
|
|
|
typedef nsTHashMap<nsUint64HashKey, LockCount> ProcessLockTable;
|
2012-11-17 15:05:18 +00:00
|
|
|
typedef nsClassHashtable<nsStringHashKey, ProcessLockTable> LockTable;
|
2012-03-07 11:03:25 +00:00
|
|
|
|
2013-05-10 15:20:52 +00:00
|
|
|
int sActiveListeners = 0;
|
|
|
|
StaticAutoPtr<LockTable> sLockTable;
|
|
|
|
bool sIsShuttingDown = false;
|
2012-07-06 02:09:46 +00:00
|
|
|
|
2013-05-10 15:20:52 +00:00
|
|
|
WakeLockInformation WakeLockInfoFromLockCount(const nsAString& aTopic,
|
|
|
|
const LockCount& aLockCount) {
|
2018-08-31 20:29:30 +00:00
|
|
|
nsString topic(aTopic);
|
|
|
|
WakeLockInformation info(topic, aLockCount.numLocks, aLockCount.numHidden,
|
|
|
|
aLockCount.processes);
|
|
|
|
|
2013-05-10 15:20:52 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2015-10-20 04:35:20 +00: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 15:05:18 +00:00
|
|
|
|
2015-10-20 04:35:20 +00:00
|
|
|
aTotalCount->numLocks += count.numLocks;
|
|
|
|
aTotalCount->numHidden += count.numHidden;
|
2012-11-17 15:05:18 +00:00
|
|
|
|
2015-10-20 04:35:20 +00: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-14 20:41:30 +00:00
|
|
|
}
|
2012-11-17 15:05:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class ClearHashtableOnShutdown final : public nsIObserver {
|
2014-06-23 22:40:03 +00:00
|
|
|
~ClearHashtableOnShutdown() {}
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2012-07-06 02:09:46 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(ClearHashtableOnShutdown, nsIObserver)
|
2012-07-06 02:09:46 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 15:02:17 +00:00
|
|
|
ClearHashtableOnShutdown::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* data) {
|
2012-07-06 02:09:46 +00:00
|
|
|
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
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class CleanupOnContentShutdown final : public nsIObserver {
|
2014-06-23 22:40:03 +00:00
|
|
|
~CleanupOnContentShutdown() {}
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(CleanupOnContentShutdown, nsIObserver)
|
2012-11-17 15:05:18 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 15:02:17 +00:00
|
|
|
CleanupOnContentShutdown::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* data) {
|
2012-11-17 15:05:18 +00: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(u"childID"_ns, &childID);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2015-10-20 04:52:44 +00:00
|
|
|
for (auto iter = sLockTable->Iter(); !iter.Done(); iter.Next()) {
|
2020-01-13 19:18:56 +00:00
|
|
|
auto table = iter.UserData();
|
2015-10-20 04:52:44 +00: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 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
|
|
|
|
2018-08-31 20:29:30 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace hal {
|
|
|
|
|
|
|
|
void WakeLockInit() {
|
2012-11-17 15:05:18 +00:00
|
|
|
sLockTable = new LockTable();
|
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
|
|
|
}
|
2013-05-10 15:20:52 +00: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 11:03:25 +00:00
|
|
|
void EnableWakeLockNotifications() { sActiveListeners++; }
|
|
|
|
|
|
|
|
void DisableWakeLockNotifications() { sActiveListeners--; }
|
|
|
|
|
2023-11-16 00:14:10 +00:00
|
|
|
void ModifyWakeLockWithChildID(const nsAString& aTopic,
|
|
|
|
hal::WakeLockControl aLockAdjust,
|
|
|
|
hal::WakeLockControl aHiddenAdjust,
|
|
|
|
uint64_t aChildID) {
|
2012-11-27 02:28:11 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2023-11-16 00:14:10 +00:00
|
|
|
MOZ_ASSERT(aChildID != 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
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
LockCount processCount;
|
|
|
|
LockCount totalCount;
|
2021-02-15 15:12:17 +00:00
|
|
|
ProcessLockTable* const table =
|
|
|
|
sLockTable->WithEntryHandle(aTopic, [&](auto&& entry) {
|
|
|
|
if (!entry) {
|
|
|
|
entry.Insert(MakeUnique<ProcessLockTable>());
|
|
|
|
} else {
|
2023-11-16 00:14:10 +00:00
|
|
|
Unused << entry.Data()->Get(aChildID, &processCount);
|
2021-03-01 09:59:29 +00:00
|
|
|
CountWakeLocks(entry->get(), &totalCount);
|
2021-02-15 15:12:17 +00:00
|
|
|
}
|
2021-03-01 09:59:29 +00:00
|
|
|
return entry->get();
|
2021-02-15 15:12:17 +00:00
|
|
|
});
|
2012-11-17 15:05:18 +00: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 11:03:25 +00:00
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
WakeLockState oldState =
|
|
|
|
ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden);
|
2022-02-03 20:37:21 +00:00
|
|
|
|
|
|
|
if (ComputeWakeLockState(totalCount.numLocks + aLockAdjust,
|
|
|
|
totalCount.numHidden + aHiddenAdjust) != oldState &&
|
|
|
|
(aTopic.Equals(u"video-playing"_ns) ||
|
|
|
|
aTopic.Equals(u"audio-playing"_ns))) {
|
|
|
|
glean::RecordPowerMetrics();
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:20:52 +00:00
|
|
|
bool processWasLocked = processCount.numLocks > 0;
|
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) {
|
2023-11-16 00:14:10 +00:00
|
|
|
table->InsertOrUpdate(aChildID, processCount);
|
2012-03-07 11:03:25 +00:00
|
|
|
} else {
|
2023-11-16 00:14:10 +00:00
|
|
|
table->Remove(aChildID);
|
2012-11-17 15:05:18 +00:00
|
|
|
}
|
|
|
|
if (!totalCount.numLocks) {
|
2012-03-07 11:03:25 +00:00
|
|
|
sLockTable->Remove(aTopic);
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:20:52 +00:00
|
|
|
if (sActiveListeners &&
|
|
|
|
(oldState !=
|
|
|
|
ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden) ||
|
2013-05-10 15:58:09 +00:00
|
|
|
processWasLocked != (processCount.numLocks > 0))) {
|
2012-03-07 11:03:25 +00:00
|
|
|
WakeLockInformation info;
|
2013-11-25 19:57:18 +00:00
|
|
|
hal::GetWakeLockInfo(aTopic, &info);
|
2012-03-07 11:03:25 +00:00
|
|
|
NotifyWakeLockChange(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 00:14:10 +00:00
|
|
|
void ModifyWakeLock(const nsAString& aTopic, hal::WakeLockControl aLockAdjust,
|
|
|
|
hal::WakeLockControl aHiddenAdjust) {
|
|
|
|
ModifyWakeLockWithChildID(aTopic, aLockAdjust, aHiddenAdjust,
|
|
|
|
CONTENT_PROCESS_ID_MAIN);
|
|
|
|
}
|
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
void GetWakeLockInfo(const nsAString& aTopic,
|
|
|
|
WakeLockInformation* aWakeLockInfo) {
|
2012-07-06 02:09:46 +00:00
|
|
|
if (sIsShuttingDown) {
|
|
|
|
NS_WARNING(
|
|
|
|
"You don't want to get wake lock information during xpcom-shutdown!");
|
2013-05-10 15:20:52 +00:00
|
|
|
*aWakeLockInfo = WakeLockInformation();
|
2012-07-06 02:09:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-03-07 11:03:25 +00:00
|
|
|
|
2022-02-03 20:37:21 +00:00
|
|
|
if (!sLockTable) {
|
|
|
|
// This can happen during some gtests.
|
|
|
|
NS_WARNING("Attempting to get wake lock information before initialization");
|
|
|
|
*aWakeLockInfo = WakeLockInformation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-17 15:05:18 +00:00
|
|
|
ProcessLockTable* table = sLockTable->Get(aTopic);
|
|
|
|
if (!table) {
|
2013-05-10 15:20:52 +00:00
|
|
|
*aWakeLockInfo = WakeLockInfoFromLockCount(aTopic, LockCount());
|
2012-11-17 15:05:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LockCount totalCount;
|
2015-10-20 04:35:20 +00:00
|
|
|
CountWakeLocks(table, &totalCount);
|
2013-05-10 15:20:52 +00:00
|
|
|
*aWakeLockInfo = WakeLockInfoFromLockCount(aTopic, totalCount);
|
2012-03-07 11:03:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace hal_impl
|
|
|
|
} // namespace mozilla
|