Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1029)

Side by Side Diff: Source/modules/wake_lock/WakeLock.cpp

Issue 1084923002: Wake Lock API implementation (Blink part) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Applied review comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "modules/wake_lock/WakeLock.h"
7
8 #include "core/dom/Document.h"
9
10 namespace blink {
11
12 class WakeLock::WakeLockDocumentVisibilityObserver final
13 : public NoBaseWillBeGarbageCollectedFinalized<WakeLockDocumentVisibilityObs erver>
14 , public DocumentVisibilityObserver {
15 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(WakeLockDocumentVisibilityObserver);
mlamouri (slow - plz ping) 2015/05/05 13:45:44 WTF_MAKE_NONCOPYABLE?
16 public:
17 WakeLockDocumentVisibilityObserver(Document&, WakeLock*);
18
19 // DocumentVisibilityObserver implementation
20 virtual void didChangeVisibilityState(PageVisibilityState) override;
21
22 DECLARE_VIRTUAL_TRACE();
23
24 private:
25 RawPtrWillBeMember<WakeLock> m_wakeLock;
26 };
27
28 WakeLock::WakeLockDocumentVisibilityObserver::WakeLockDocumentVisibilityObserver (Document& document, WakeLock* wakeLock)
29 : DocumentVisibilityObserver(document)
30 , m_wakeLock(wakeLock)
31 {
32 }
33
34 void WakeLock::WakeLockDocumentVisibilityObserver::didChangeVisibilityState(Page VisibilityState state)
35 {
36 if (m_wakeLock) {
37 m_wakeLock->didChangeVisibilityState(state);
38 }
39 }
40
41 DEFINE_TRACE(WakeLock::WakeLockDocumentVisibilityObserver)
42 {
43 DocumentVisibilityObserver::trace(visitor);
44 visitor->trace(m_wakeLock);
45 }
46
47 WakeLock::WakeLock(Document& document)
48 : DocumentLifecycleObserver(&document)
49 , m_controller(nullptr)
50 , m_pageVisibilityState(document.pageVisibilityState())
51 , m_keepScreenAwake(false)
52 {
53 if (!document.frame())
54 return;
55
56 m_controller = WakeLockController::from(document.frame());
57 m_observer = adoptPtrWillBeNoop(new WakeLockDocumentVisibilityObserver(docum ent, this));
58 }
59
60 // static
61 const char* WakeLock::supplementName()
62 {
63 return "WakeLock";
64 }
65
66 // static
67 WakeLock& WakeLock::from(Document& document)
68 {
69 WakeLock* wakeLock = static_cast<WakeLock*>(DocumentSupplement::from(documen t, supplementName()));
70
71 if (!wakeLock) {
72 wakeLock = new WakeLock(document);
73 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe Noop(wakeLock));
74 }
75
76 return *wakeLock;
77 }
78
79 // static
80 bool WakeLock::keepScreenAwake(Document& document)
81 {
82 return WakeLock::from(document).m_keepScreenAwake;
83 }
84
85 // static
86 void WakeLock::setKeepScreenAwake(Document& document, bool keepScreenAwake)
87 {
88 WakeLock::from(document).setKeepScreenAwake(keepScreenAwake);
89 }
90
91 void WakeLock::didChangeVisibilityState(PageVisibilityState pageVisibilityState)
92 {
93 m_pageVisibilityState = pageVisibilityState;
94 notifyController();
95 }
96
97 void WakeLock::documentWasDetached()
98 {
99 m_observer.clear();
100 setKeepScreenAwake(false);
mlamouri (slow - plz ping) 2015/05/05 13:45:43 You might want to set m_controller to false at tha
101 }
102
103 void WakeLock::setKeepScreenAwake(bool keepScreenAwake)
104 {
105 m_keepScreenAwake = keepScreenAwake;
106 notifyController();
107 }
108
109 void WakeLock::notifyController() const
110 {
111 if (m_controller) {
112 m_controller->requestKeepScreenAwake(m_keepScreenAwake
113 && m_pageVisibilityState == PageVisibilityStateVisible);
114 }
115 }
116
117 DEFINE_TRACE(WakeLock)
118 {
119 DocumentSupplement::trace(visitor);
120 DocumentLifecycleObserver::trace(visitor);
121 visitor->trace(m_controller);
122 visitor->trace(m_observer);
123 }
124
125 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698