Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_dom_power_WakeLock_h
8 : #define mozilla_dom_power_WakeLock_h
9 :
10 : #include "nsCOMPtr.h"
11 : #include "nsIDOMEventListener.h"
12 : #include "nsIObserver.h"
13 : #include "nsString.h"
14 : #include "nsWeakReference.h"
15 : #include "nsWrapperCache.h"
16 : #include "mozilla/ErrorResult.h"
17 :
18 : class nsPIDOMWindowInner;
19 :
20 : namespace mozilla {
21 : namespace dom {
22 :
23 : class ContentParent;
24 :
25 : class WakeLock final
26 : : public nsIDOMEventListener
27 : , public nsWrapperCache
28 : , public nsIObserver
29 : , public nsSupportsWeakReference
30 : {
31 : public:
32 : NS_DECL_NSIDOMEVENTLISTENER
33 : NS_DECL_NSIOBSERVER
34 :
35 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
36 1 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WakeLock, nsIDOMEventListener)
37 :
38 : // Note: WakeLock lives for the lifetime of the document in order to avoid
39 : // exposing GC behavior to pages. This means that
40 : // |var foo = navigator.requestWakeLock('cpu'); foo = null;|
41 : // doesn't unlock the 'cpu' resource.
42 :
43 : WakeLock();
44 :
45 : // Initialize this wake lock on behalf of the given window. Null windows are
46 : // allowed; a lock without an associated window is always considered
47 : // invisible.
48 : nsresult Init(const nsAString &aTopic, nsPIDOMWindowInner* aWindow);
49 :
50 : // Initialize this wake lock on behalf of the given process. If the process
51 : // dies, the lock is released. A wake lock initialized via this method is
52 : // always considered visible.
53 : nsresult Init(const nsAString &aTopic, ContentParent* aContentParent);
54 :
55 : // WebIDL methods
56 :
57 : nsPIDOMWindowInner* GetParentObject() const;
58 :
59 : virtual JSObject*
60 : WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
61 :
62 : void GetTopic(nsAString& aTopic);
63 :
64 : void Unlock(ErrorResult& aRv);
65 :
66 : private:
67 : virtual ~WakeLock();
68 :
69 : void DoUnlock();
70 : void DoLock();
71 : void AttachEventListener();
72 : void DetachEventListener();
73 :
74 : bool mLocked;
75 : bool mHidden;
76 :
77 : // The ID of the ContentParent on behalf of whom we acquired this lock, or
78 : // CONTENT_PROCESS_UNKNOWN_ID if this lock was acquired on behalf of the
79 : // current process.
80 : uint64_t mContentParentID;
81 : nsString mTopic;
82 :
83 : // window that this was created for. Weak reference.
84 : nsWeakPtr mWindow;
85 : };
86 :
87 : } // namespace dom
88 : } // namespace mozilla
89 :
90 : #endif // mozilla_dom_power_WakeLock_h
|