WakeLockSentinel.h (2140B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef DOM_WAKELOCKSENTINEL_H_ 8 #define DOM_WAKELOCKSENTINEL_H_ 9 10 #include "js/TypeDecls.h" 11 #include "mozilla/Attributes.h" 12 #include "mozilla/DOMEventTargetHelper.h" 13 #include "mozilla/TimeStamp.h" 14 #include "mozilla/dom/WakeLockBinding.h" 15 16 namespace mozilla::dom { 17 18 class Promise; 19 20 } // namespace mozilla::dom 21 22 namespace mozilla::dom { 23 24 class WakeLockSentinel final : public DOMEventTargetHelper { 25 public: 26 WakeLockSentinel(nsIGlobalObject* aOwnerWindow, WakeLockType aType) 27 : DOMEventTargetHelper(aOwnerWindow), 28 mType(aType), 29 mCreationTime(TimeStamp::Now()) {} 30 31 protected: 32 ~WakeLockSentinel() { 33 MOZ_DIAGNOSTIC_ASSERT(mReleased); 34 MOZ_DIAGNOSTIC_ASSERT(!mHoldsActualLock); 35 } 36 37 public: 38 JSObject* WrapObject(JSContext* aCx, 39 JS::Handle<JSObject*> aGivenProto) override; 40 41 bool Released() const; 42 43 WakeLockType Type() const { return mType; } 44 45 MOZ_CAN_RUN_SCRIPT 46 already_AddRefed<Promise> ReleaseLock(ErrorResult& aRv); 47 48 MOZ_CAN_RUN_SCRIPT 49 void NotifyLockReleased(); 50 51 IMPL_EVENT_HANDLER(release); 52 53 // Acquire underlying system wake lock by modifying the HAL wake lock counter 54 void AcquireActualLock(); 55 56 private: 57 WakeLockType mType; 58 59 bool mReleased = false; 60 61 /** 62 * To avoid user fingerprinting, WakeLockJS::Request will provide a 63 * WakeLockSentinel even if the lock type is not applicable or cannot be 64 * obtained. 65 * But when releasing this sentinel, we have to know whether 66 * AcquireActualLock was called. 67 * 68 * https://w3c.github.io/screen-wake-lock/#dfn-applicable-wake-lock 69 * https://w3c.github.io/screen-wake-lock/#the-request-method 70 */ 71 bool mHoldsActualLock = false; 72 73 // Time when this object was created 74 TimeStamp mCreationTime; 75 }; 76 77 } // namespace mozilla::dom 78 79 #endif // DOM_WAKELOCKSENTINEL_H_