hal/HalWakeLock.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/Hal.h"
michael@0 7 #include "mozilla/HalWakeLock.h"
michael@0 8 #include "mozilla/Services.h"
michael@0 9 #include "mozilla/StaticPtr.h"
michael@0 10 #include "mozilla/dom/ContentParent.h"
michael@0 11 #include "nsClassHashtable.h"
michael@0 12 #include "nsDataHashtable.h"
michael@0 13 #include "nsHashKeys.h"
michael@0 14 #include "nsIPropertyBag2.h"
michael@0 15 #include "nsIObserverService.h"
michael@0 16
michael@0 17 using namespace mozilla;
michael@0 18 using namespace mozilla::hal;
michael@0 19
michael@0 20 namespace {
michael@0 21
michael@0 22 struct LockCount {
michael@0 23 LockCount()
michael@0 24 : numLocks(0)
michael@0 25 , numHidden(0)
michael@0 26 {}
michael@0 27 uint32_t numLocks;
michael@0 28 uint32_t numHidden;
michael@0 29 nsTArray<uint64_t> processes;
michael@0 30 };
michael@0 31
michael@0 32 typedef nsDataHashtable<nsUint64HashKey, LockCount> ProcessLockTable;
michael@0 33 typedef nsClassHashtable<nsStringHashKey, ProcessLockTable> LockTable;
michael@0 34
michael@0 35 int sActiveListeners = 0;
michael@0 36 StaticAutoPtr<LockTable> sLockTable;
michael@0 37 bool sInitialized = false;
michael@0 38 bool sIsShuttingDown = false;
michael@0 39
michael@0 40 WakeLockInformation
michael@0 41 WakeLockInfoFromLockCount(const nsAString& aTopic, const LockCount& aLockCount)
michael@0 42 {
michael@0 43 // TODO: Once we abandon b2g18, we can switch this to use the
michael@0 44 // WakeLockInformation constructor, which is better because it doesn't let us
michael@0 45 // forget to assign a param. For now we have to do it this way, because
michael@0 46 // b2g18 doesn't have the nsTArray <--> InfallibleTArray conversion (bug
michael@0 47 // 819791).
michael@0 48
michael@0 49 WakeLockInformation info;
michael@0 50 info.topic() = aTopic;
michael@0 51 info.numLocks() = aLockCount.numLocks;
michael@0 52 info.numHidden() = aLockCount.numHidden;
michael@0 53 info.lockingProcesses().AppendElements(aLockCount.processes);
michael@0 54 return info;
michael@0 55 }
michael@0 56
michael@0 57 PLDHashOperator
michael@0 58 CountWakeLocks(const uint64_t& aKey, LockCount aCount, void* aUserArg)
michael@0 59 {
michael@0 60 MOZ_ASSERT(aUserArg);
michael@0 61
michael@0 62 LockCount* totalCount = static_cast<LockCount*>(aUserArg);
michael@0 63 totalCount->numLocks += aCount.numLocks;
michael@0 64 totalCount->numHidden += aCount.numHidden;
michael@0 65
michael@0 66 // This is linear in the number of processes, but that should be small.
michael@0 67 if (!totalCount->processes.Contains(aKey)) {
michael@0 68 totalCount->processes.AppendElement(aKey);
michael@0 69 }
michael@0 70
michael@0 71 return PL_DHASH_NEXT;
michael@0 72 }
michael@0 73
michael@0 74 static PLDHashOperator
michael@0 75 RemoveChildFromList(const nsAString& aKey, nsAutoPtr<ProcessLockTable>& aTable,
michael@0 76 void* aUserArg)
michael@0 77 {
michael@0 78 MOZ_ASSERT(aUserArg);
michael@0 79
michael@0 80 PLDHashOperator op = PL_DHASH_NEXT;
michael@0 81 uint64_t childID = *static_cast<uint64_t*>(aUserArg);
michael@0 82 if (aTable->Get(childID, nullptr)) {
michael@0 83 aTable->Remove(childID);
michael@0 84
michael@0 85 LockCount totalCount;
michael@0 86 aTable->EnumerateRead(CountWakeLocks, &totalCount);
michael@0 87 if (!totalCount.numLocks) {
michael@0 88 op = PL_DHASH_REMOVE;
michael@0 89 }
michael@0 90
michael@0 91 if (sActiveListeners) {
michael@0 92 NotifyWakeLockChange(WakeLockInfoFromLockCount(aKey, totalCount));
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 return op;
michael@0 97 }
michael@0 98
michael@0 99 class ClearHashtableOnShutdown MOZ_FINAL : public nsIObserver {
michael@0 100 public:
michael@0 101 NS_DECL_ISUPPORTS
michael@0 102 NS_DECL_NSIOBSERVER
michael@0 103 };
michael@0 104
michael@0 105 NS_IMPL_ISUPPORTS(ClearHashtableOnShutdown, nsIObserver)
michael@0 106
michael@0 107 NS_IMETHODIMP
michael@0 108 ClearHashtableOnShutdown::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* data)
michael@0 109 {
michael@0 110 MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown"));
michael@0 111
michael@0 112 sIsShuttingDown = true;
michael@0 113 sLockTable = nullptr;
michael@0 114
michael@0 115 return NS_OK;
michael@0 116 }
michael@0 117
michael@0 118 class CleanupOnContentShutdown MOZ_FINAL : public nsIObserver {
michael@0 119 public:
michael@0 120 NS_DECL_ISUPPORTS
michael@0 121 NS_DECL_NSIOBSERVER
michael@0 122 };
michael@0 123
michael@0 124 NS_IMPL_ISUPPORTS(CleanupOnContentShutdown, nsIObserver)
michael@0 125
michael@0 126 NS_IMETHODIMP
michael@0 127 CleanupOnContentShutdown::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* data)
michael@0 128 {
michael@0 129 MOZ_ASSERT(!strcmp(aTopic, "ipc:content-shutdown"));
michael@0 130
michael@0 131 if (sIsShuttingDown) {
michael@0 132 return NS_OK;
michael@0 133 }
michael@0 134
michael@0 135 nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
michael@0 136 if (!props) {
michael@0 137 NS_WARNING("ipc:content-shutdown message without property bag as subject");
michael@0 138 return NS_OK;
michael@0 139 }
michael@0 140
michael@0 141 uint64_t childID = 0;
michael@0 142 nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"),
michael@0 143 &childID);
michael@0 144 if (NS_SUCCEEDED(rv)) {
michael@0 145 sLockTable->Enumerate(RemoveChildFromList, &childID);
michael@0 146 } else {
michael@0 147 NS_WARNING("ipc:content-shutdown message without childID property");
michael@0 148 }
michael@0 149 return NS_OK;
michael@0 150 }
michael@0 151
michael@0 152 void
michael@0 153 Init()
michael@0 154 {
michael@0 155 sLockTable = new LockTable();
michael@0 156 sInitialized = true;
michael@0 157
michael@0 158 nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
michael@0 159 if (obs) {
michael@0 160 obs->AddObserver(new ClearHashtableOnShutdown(), "xpcom-shutdown", false);
michael@0 161 obs->AddObserver(new CleanupOnContentShutdown(), "ipc:content-shutdown", false);
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 } // anonymous namespace
michael@0 166
michael@0 167 namespace mozilla {
michael@0 168
michael@0 169 namespace hal {
michael@0 170
michael@0 171 WakeLockState
michael@0 172 ComputeWakeLockState(int aNumLocks, int aNumHidden)
michael@0 173 {
michael@0 174 if (aNumLocks == 0) {
michael@0 175 return WAKE_LOCK_STATE_UNLOCKED;
michael@0 176 } else if (aNumLocks == aNumHidden) {
michael@0 177 return WAKE_LOCK_STATE_HIDDEN;
michael@0 178 } else {
michael@0 179 return WAKE_LOCK_STATE_VISIBLE;
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 } // namespace hal
michael@0 184
michael@0 185 namespace hal_impl {
michael@0 186
michael@0 187 void
michael@0 188 EnableWakeLockNotifications()
michael@0 189 {
michael@0 190 sActiveListeners++;
michael@0 191 }
michael@0 192
michael@0 193 void
michael@0 194 DisableWakeLockNotifications()
michael@0 195 {
michael@0 196 sActiveListeners--;
michael@0 197 }
michael@0 198
michael@0 199 void
michael@0 200 ModifyWakeLock(const nsAString& aTopic,
michael@0 201 hal::WakeLockControl aLockAdjust,
michael@0 202 hal::WakeLockControl aHiddenAdjust,
michael@0 203 uint64_t aProcessID)
michael@0 204 {
michael@0 205 MOZ_ASSERT(NS_IsMainThread());
michael@0 206 MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
michael@0 207
michael@0 208 if (sIsShuttingDown) {
michael@0 209 return;
michael@0 210 }
michael@0 211 if (!sInitialized) {
michael@0 212 Init();
michael@0 213 }
michael@0 214
michael@0 215 ProcessLockTable* table = sLockTable->Get(aTopic);
michael@0 216 LockCount processCount;
michael@0 217 LockCount totalCount;
michael@0 218 if (!table) {
michael@0 219 table = new ProcessLockTable();
michael@0 220 sLockTable->Put(aTopic, table);
michael@0 221 } else {
michael@0 222 table->Get(aProcessID, &processCount);
michael@0 223 table->EnumerateRead(CountWakeLocks, &totalCount);
michael@0 224 }
michael@0 225
michael@0 226 MOZ_ASSERT(processCount.numLocks >= processCount.numHidden);
michael@0 227 MOZ_ASSERT(aLockAdjust >= 0 || processCount.numLocks > 0);
michael@0 228 MOZ_ASSERT(aHiddenAdjust >= 0 || processCount.numHidden > 0);
michael@0 229 MOZ_ASSERT(totalCount.numLocks >= totalCount.numHidden);
michael@0 230 MOZ_ASSERT(aLockAdjust >= 0 || totalCount.numLocks > 0);
michael@0 231 MOZ_ASSERT(aHiddenAdjust >= 0 || totalCount.numHidden > 0);
michael@0 232
michael@0 233 WakeLockState oldState = ComputeWakeLockState(totalCount.numLocks, totalCount.numHidden);
michael@0 234 bool processWasLocked = processCount.numLocks > 0;
michael@0 235
michael@0 236 processCount.numLocks += aLockAdjust;
michael@0 237 processCount.numHidden += aHiddenAdjust;
michael@0 238
michael@0 239 totalCount.numLocks += aLockAdjust;
michael@0 240 totalCount.numHidden += aHiddenAdjust;
michael@0 241
michael@0 242 if (processCount.numLocks) {
michael@0 243 table->Put(aProcessID, processCount);
michael@0 244 } else {
michael@0 245 table->Remove(aProcessID);
michael@0 246 }
michael@0 247 if (!totalCount.numLocks) {
michael@0 248 sLockTable->Remove(aTopic);
michael@0 249 }
michael@0 250
michael@0 251 if (sActiveListeners &&
michael@0 252 (oldState != ComputeWakeLockState(totalCount.numLocks,
michael@0 253 totalCount.numHidden) ||
michael@0 254 processWasLocked != (processCount.numLocks > 0))) {
michael@0 255
michael@0 256 WakeLockInformation info;
michael@0 257 hal::GetWakeLockInfo(aTopic, &info);
michael@0 258 NotifyWakeLockChange(info);
michael@0 259 }
michael@0 260 }
michael@0 261
michael@0 262 void
michael@0 263 GetWakeLockInfo(const nsAString& aTopic, WakeLockInformation* aWakeLockInfo)
michael@0 264 {
michael@0 265 if (sIsShuttingDown) {
michael@0 266 NS_WARNING("You don't want to get wake lock information during xpcom-shutdown!");
michael@0 267 *aWakeLockInfo = WakeLockInformation();
michael@0 268 return;
michael@0 269 }
michael@0 270 if (!sInitialized) {
michael@0 271 Init();
michael@0 272 }
michael@0 273
michael@0 274 ProcessLockTable* table = sLockTable->Get(aTopic);
michael@0 275 if (!table) {
michael@0 276 *aWakeLockInfo = WakeLockInfoFromLockCount(aTopic, LockCount());
michael@0 277 return;
michael@0 278 }
michael@0 279 LockCount totalCount;
michael@0 280 table->EnumerateRead(CountWakeLocks, &totalCount);
michael@0 281 *aWakeLockInfo = WakeLockInfoFromLockCount(aTopic, totalCount);
michael@0 282 }
michael@0 283
michael@0 284 } // hal_impl
michael@0 285 } // mozilla

mercurial