Index: Source/core/dom/WakeLock.cpp |
diff --git a/Source/core/dom/WakeLock.cpp b/Source/core/dom/WakeLock.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c0b6ec7de7541a9ea64edcd402a34e06d981a601 |
--- /dev/null |
+++ b/Source/core/dom/WakeLock.cpp |
@@ -0,0 +1,124 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "core/dom/WakeLock.h" |
+ |
+#include "core/dom/Document.h" |
+ |
+namespace blink { |
+ |
+class WakeLock::WakeLockDocumentVisibilityObserver final |
+ : public NoBaseWillBeGarbageCollectedFinalized<WakeLockDocumentVisibilityObserver> |
+ , public DocumentVisibilityObserver { |
+ WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(WakeLockDocumentVisibilityObserver); |
+public: |
+ WakeLockDocumentVisibilityObserver(Document&, WakeLock*); |
+ |
+ // DocumentVisibilityObserver implementation |
+ virtual void didChangeVisibilityState(PageVisibilityState) override; |
+ |
+ DECLARE_VIRTUAL_TRACE(); |
+ |
+private: |
+ RawPtrWillBeMember<WakeLock> m_wakeLock; |
+}; |
+ |
+WakeLock::WakeLockDocumentVisibilityObserver::WakeLockDocumentVisibilityObserver(Document& document, WakeLock* wakeLock) |
+ : DocumentVisibilityObserver(document) |
+ , m_wakeLock(wakeLock) |
+{ |
+} |
+ |
+void WakeLock::WakeLockDocumentVisibilityObserver::didChangeVisibilityState(PageVisibilityState state) |
+{ |
+ if (m_wakeLock) { |
+ m_wakeLock->didChangeVisibilityState(state); |
+ } |
+} |
+ |
+DEFINE_TRACE(WakeLock::WakeLockDocumentVisibilityObserver) |
+{ |
+ DocumentVisibilityObserver::trace(visitor); |
+ visitor->trace(m_wakeLock); |
+} |
+ |
+WakeLock::WakeLock(Document& document) |
+ : DocumentLifecycleObserver(&document) |
+ , m_controller(nullptr) |
+ , m_pageVisibilityState(document.pageVisibilityState()) |
+ , m_keepScreenAwake(false) |
+{ |
+ if (LocalFrame* frame = document.frame()) { |
mlamouri (slow - plz ping)
2015/04/28 08:00:25
if (!document.frame())
return;
[...]
|
+ m_controller = WakeLockController::from(frame); |
+ m_observer = adoptPtrWillBeNoop(new WakeLockDocumentVisibilityObserver(document, this)); |
+ } |
+} |
+ |
+// static |
+const char* WakeLock::supplementName() |
+{ |
+ return "WakeLock"; |
+} |
+ |
+// static |
+WakeLock& WakeLock::from(Document& document) |
+{ |
+ WakeLock* wakeLock = static_cast<WakeLock*>(DocumentSupplement::from(document, supplementName())); |
+ |
+ if (!wakeLock) { |
+ wakeLock = new WakeLock(document); |
+ DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(wakeLock)); |
+ } |
+ |
+ return *wakeLock; |
+} |
+ |
+// static |
+bool WakeLock::keepScreenAwake(Document& document) |
+{ |
+ return WakeLock::from(document).m_keepScreenAwake; |
+} |
+ |
+// static |
+void WakeLock::setKeepScreenAwake(Document& document, bool keepScreenAwake) |
+{ |
+ WakeLock::from(document).setKeepScreenAwake(keepScreenAwake); |
+} |
+ |
+void WakeLock::didChangeVisibilityState(PageVisibilityState pageVisibilityState) |
+{ |
+ m_pageVisibilityState = pageVisibilityState; |
+ notifyController(); |
+} |
+ |
+void WakeLock::documentWasDetached() |
+{ |
+ m_observer.clear(); |
+ setKeepScreenAwake(false); |
+} |
+ |
+void WakeLock::setKeepScreenAwake(bool keepScreenAwake) |
+{ |
+ m_keepScreenAwake = keepScreenAwake; |
+ notifyController(); |
+} |
+ |
+void WakeLock::notifyController() const |
+{ |
+ if (m_controller) { |
+ m_controller->requestKeepScreenAwake(m_keepScreenAwake |
+ && m_pageVisibilityState == PageVisibilityStateVisible); |
+ } |
+} |
+ |
+DEFINE_TRACE(WakeLock) |
+{ |
+ DocumentSupplement::trace(visitor); |
+ DocumentLifecycleObserver::trace(visitor); |
+ visitor->trace(m_controller); |
+ visitor->trace(m_observer); |
+} |
+ |
+} |