HalWakeLock.cpp 7.2 KB

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