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
{
27 class WakeLockSentinel
;
29 } // namespace mozilla::dom
31 namespace mozilla::dom
{
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 nsIObserver
,
43 public nsWrapperCache
,
44 public hal::BatteryObserver
,
45 public nsSupportsWeakReference
{
49 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
50 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(WakeLockJS
, nsIObserver
)
53 explicit WakeLockJS(nsPIDOMWindowInner
* aWindow
);
59 nsISupports
* GetParentObject() const;
61 JSObject
* WrapObject(JSContext
* aCx
,
62 JS::Handle
<JSObject
*> aGivenProto
) override
;
64 void Notify(const hal::BatteryInformation
& aBatteryInfo
) override
;
66 already_AddRefed
<Promise
> Request(WakeLockType aType
, ErrorResult
& aRv
);
69 enum class RequestError
{
79 static nsLiteralCString
GetRequestErrorMessage(RequestError aRv
);
81 static RequestError
WakeLockAllowedForDocument(Document
* aDoc
);
83 void AttachListeners();
84 void DetachListeners();
86 Result
<already_AddRefed
<WakeLockSentinel
>, RequestError
> Obtain(
87 WakeLockType aType
, Document
* aDoc
);
89 RefPtr
<nsPIDOMWindowInner
> mWindow
;
93 void ReleaseWakeLock(Document
* aDoc
, WakeLockSentinel
* aLock
,
96 } // namespace mozilla::dom
98 #endif // DOM_WAKELOCKJS_H_