|
1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef mozilla_dom_power_WakeLock_h |
|
7 #define mozilla_dom_power_WakeLock_h |
|
8 |
|
9 #include "nsCOMPtr.h" |
|
10 #include "nsIDOMEventListener.h" |
|
11 #include "nsIObserver.h" |
|
12 #include "nsString.h" |
|
13 #include "nsWeakReference.h" |
|
14 #include "nsWrapperCache.h" |
|
15 #include "mozilla/ErrorResult.h" |
|
16 |
|
17 class nsIDOMWindow; |
|
18 |
|
19 namespace mozilla { |
|
20 namespace dom { |
|
21 |
|
22 class ContentParent; |
|
23 |
|
24 class WakeLock MOZ_FINAL |
|
25 : public nsIDOMEventListener |
|
26 , public nsWrapperCache |
|
27 , public nsIObserver |
|
28 , public nsSupportsWeakReference |
|
29 { |
|
30 public: |
|
31 NS_DECL_NSIDOMEVENTLISTENER |
|
32 NS_DECL_NSIOBSERVER |
|
33 |
|
34 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
35 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WakeLock, nsIDOMEventListener) |
|
36 |
|
37 // Note: WakeLock lives for the lifetime of the document in order to avoid |
|
38 // exposing GC behavior to pages. This means that |
|
39 // |var foo = navigator.requestWakeLock('cpu'); foo = null;| |
|
40 // doesn't unlock the 'cpu' resource. |
|
41 |
|
42 WakeLock(); |
|
43 virtual ~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, nsIDOMWindow* 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 nsISupports* GetParentObject() const; |
|
58 |
|
59 virtual JSObject* |
|
60 WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
61 |
|
62 void GetTopic(nsAString& aTopic); |
|
63 |
|
64 void Unlock(ErrorResult& aRv); |
|
65 |
|
66 private: |
|
67 void DoUnlock(); |
|
68 void DoLock(); |
|
69 void AttachEventListener(); |
|
70 void DetachEventListener(); |
|
71 |
|
72 bool mLocked; |
|
73 bool mHidden; |
|
74 |
|
75 // The ID of the ContentParent on behalf of whom we acquired this lock, or |
|
76 // CONTENT_PROCESS_UNKNOWN_ID if this lock was acquired on behalf of the |
|
77 // current process. |
|
78 uint64_t mContentParentID; |
|
79 nsString mTopic; |
|
80 |
|
81 // window that this was created for. Weak reference. |
|
82 nsWeakPtr mWindow; |
|
83 }; |
|
84 |
|
85 } // namespace dom |
|
86 } // namespace mozilla |
|
87 |
|
88 #endif // mozilla_dom_power_WakeLock_h |