Bug 1885489 - Part 5: Add SnapshotIterator::readInt32(). r=iain
[gecko.git] / dom / power / WakeLockSentinel.h
blobf9e79e2c656eec52af17de941c9265f9b02d90a7
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/. */
7 #ifndef DOM_WAKELOCKSENTINEL_H_
8 #define DOM_WAKELOCKSENTINEL_H_
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"
16 namespace mozilla::dom {
18 class Promise;
20 } // namespace mozilla::dom
22 namespace mozilla::dom {
24 class WakeLockSentinel final : public DOMEventTargetHelper {
25 public:
26 WakeLockSentinel(nsIGlobalObject* aOwnerWindow, WakeLockType aType)
27 : DOMEventTargetHelper(aOwnerWindow),
28 mType(aType),
29 mCreationTime(TimeStamp::Now()) {}
31 protected:
32 ~WakeLockSentinel() {
33 MOZ_DIAGNOSTIC_ASSERT(mReleased);
34 MOZ_DIAGNOSTIC_ASSERT(!mHoldsActualLock);
37 public:
38 JSObject* WrapObject(JSContext* aCx,
39 JS::Handle<JSObject*> aGivenProto) override;
41 bool Released() const;
43 WakeLockType Type() const { return mType; }
45 MOZ_CAN_RUN_SCRIPT
46 already_AddRefed<Promise> ReleaseLock(ErrorResult& aRv);
48 MOZ_CAN_RUN_SCRIPT
49 void NotifyLockReleased();
51 IMPL_EVENT_HANDLER(release);
53 // Acquire underlying system wake lock by modifying the HAL wake lock counter
54 void AcquireActualLock();
56 private:
57 WakeLockType mType;
59 bool mReleased = false;
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.
68 * https://w3c.github.io/screen-wake-lock/#dfn-applicable-wake-lock
69 * https://w3c.github.io/screen-wake-lock/#the-request-method
71 bool mHoldsActualLock = false;
73 // Time when this object was created
74 TimeStamp mCreationTime;
77 } // namespace mozilla::dom
79 #endif // DOM_WAKELOCKSENTINEL_H_