Chromium Code Reviews| Index: Source/modules/wake_lock/WakeLock.cpp |
| diff --git a/Source/modules/wake_lock/WakeLock.cpp b/Source/modules/wake_lock/WakeLock.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a1a3fd9ff4aac3d587887a780d0e4e24e68044b6 |
| --- /dev/null |
| +++ b/Source/modules/wake_lock/WakeLock.cpp |
| @@ -0,0 +1,125 @@ |
| +// 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 "modules/wake_lock/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); |
|
mlamouri (slow - plz ping)
2015/05/05 13:45:44
WTF_MAKE_NONCOPYABLE?
|
| +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 (!document.frame()) |
| + return; |
| + |
| + m_controller = WakeLockController::from(document.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); |
|
mlamouri (slow - plz ping)
2015/05/05 13:45:43
You might want to set m_controller to false at tha
|
| +} |
| + |
| +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); |
| +} |
| + |
| +} |