Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "core/dom/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); | |
| 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 (LocalFrame* frame = document.frame()) { | |
|
mlamouri (slow - plz ping)
2015/04/28 08:00:25
if (!document.frame())
return;
[...]
| |
| 54 m_controller = WakeLockController::from(frame); | |
| 55 m_observer = adoptPtrWillBeNoop(new WakeLockDocumentVisibilityObserver(d ocument, this)); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 // static | |
| 60 const char* WakeLock::supplementName() | |
| 61 { | |
| 62 return "WakeLock"; | |
| 63 } | |
| 64 | |
| 65 // static | |
| 66 WakeLock& WakeLock::from(Document& document) | |
| 67 { | |
| 68 WakeLock* wakeLock = static_cast<WakeLock*>(DocumentSupplement::from(documen t, supplementName())); | |
| 69 | |
| 70 if (!wakeLock) { | |
| 71 wakeLock = new WakeLock(document); | |
| 72 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe Noop(wakeLock)); | |
| 73 } | |
| 74 | |
| 75 return *wakeLock; | |
| 76 } | |
| 77 | |
| 78 // static | |
| 79 bool WakeLock::keepScreenAwake(Document& document) | |
| 80 { | |
| 81 return WakeLock::from(document).m_keepScreenAwake; | |
| 82 } | |
| 83 | |
| 84 // static | |
| 85 void WakeLock::setKeepScreenAwake(Document& document, bool keepScreenAwake) | |
| 86 { | |
| 87 WakeLock::from(document).setKeepScreenAwake(keepScreenAwake); | |
| 88 } | |
| 89 | |
| 90 void WakeLock::didChangeVisibilityState(PageVisibilityState pageVisibilityState) | |
| 91 { | |
| 92 m_pageVisibilityState = pageVisibilityState; | |
| 93 notifyController(); | |
| 94 } | |
| 95 | |
| 96 void WakeLock::documentWasDetached() | |
| 97 { | |
| 98 m_observer.clear(); | |
| 99 setKeepScreenAwake(false); | |
| 100 } | |
| 101 | |
| 102 void WakeLock::setKeepScreenAwake(bool keepScreenAwake) | |
| 103 { | |
| 104 m_keepScreenAwake = keepScreenAwake; | |
| 105 notifyController(); | |
| 106 } | |
| 107 | |
| 108 void WakeLock::notifyController() const | |
| 109 { | |
| 110 if (m_controller) { | |
| 111 m_controller->requestKeepScreenAwake(m_keepScreenAwake | |
| 112 && m_pageVisibilityState == PageVisibilityStateVisible); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 DEFINE_TRACE(WakeLock) | |
| 117 { | |
| 118 DocumentSupplement::trace(visitor); | |
| 119 DocumentLifecycleObserver::trace(visitor); | |
| 120 visitor->trace(m_controller); | |
| 121 visitor->trace(m_observer); | |
| 122 } | |
| 123 | |
| 124 } | |
| OLD | NEW |