no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / power / WakeLockJS.h
blobc6858fdd22ad1660c6df0747d09b681eac6d08d8
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_WAKELOCKJS_H_
8 #define DOM_WAKELOCKJS_H_
10 #include "js/TypeDecls.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/HalBatteryInformation.h"
13 #include "mozilla/dom/WakeLockBinding.h"
14 #include "nsIDOMEventListener.h"
15 #include "nsIDocumentActivity.h"
16 #include "nsIObserver.h"
17 #include "nsCycleCollectionParticipant.h"
18 #include "nsWeakReference.h"
19 #include "nsWrapperCache.h"
21 class nsPIDOMWindowInner;
23 namespace mozilla::dom {
25 class Promise;
26 class Document;
27 class WakeLockSentinel;
29 } // namespace mozilla::dom
31 namespace mozilla::dom {
33 /**
34 * Management class for wake locks held from client scripts.
35 * Instances of this class have two purposes:
36 * - Implement navigator.wakeLock.request which creates a WakeLockSentinel
37 * - Listen for state changes that require all WakeLockSentinel to be released
38 * The WakeLockSentinel objects are held in document.mActiveLocks.
40 * https://www.w3.org/TR/screen-wake-lock/#the-wakelock-interface
42 class WakeLockJS final : public nsIDOMEventListener,
43 public nsWrapperCache,
44 public hal::BatteryObserver,
45 public nsIDocumentActivity,
46 public nsIObserver,
47 public nsSupportsWeakReference {
48 public:
49 NS_DECL_NSIDOMEVENTLISTENER
50 NS_DECL_NSIDOCUMENTACTIVITY
51 NS_DECL_NSIOBSERVER
53 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
54 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(WakeLockJS,
55 nsIDOMEventListener)
57 public:
58 explicit WakeLockJS(nsPIDOMWindowInner* aWindow);
60 protected:
61 ~WakeLockJS();
63 public:
64 nsISupports* GetParentObject() const;
66 JSObject* WrapObject(JSContext* aCx,
67 JS::Handle<JSObject*> aGivenProto) override;
69 void Notify(const hal::BatteryInformation& aBatteryInfo) override;
71 already_AddRefed<Promise> Request(WakeLockType aType, ErrorResult& aRv);
73 private:
74 enum class RequestError {
75 Success,
76 DocInactive,
77 DocHidden,
78 PolicyDisallowed,
79 PrefDisabled,
80 InternalFailure,
81 PermissionDenied
84 static nsLiteralCString GetRequestErrorMessage(RequestError aRv);
86 static RequestError WakeLockAllowedForDocument(Document* aDoc);
88 void AttachListeners();
89 void DetachListeners();
91 Result<already_AddRefed<WakeLockSentinel>, RequestError> Obtain(
92 WakeLockType aType);
94 RefPtr<nsPIDOMWindowInner> mWindow;
97 MOZ_CAN_RUN_SCRIPT
98 void ReleaseWakeLock(Document* aDoc, WakeLockSentinel* aLock,
99 WakeLockType aType);
101 } // namespace mozilla::dom
103 #endif // DOM_WAKELOCKJS_H_