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

Unified Diff: Source/modules/wake_lock/ScreenWakeLock.cpp

Issue 1084923002: Wake Lock API implementation (Blink part) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: Source/modules/wake_lock/ScreenWakeLock.cpp
diff --git a/Source/modules/wake_lock/ScreenWakeLock.cpp b/Source/modules/wake_lock/ScreenWakeLock.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..84604be0b26e69fa0a9fa556ea4a1923fa86450f
--- /dev/null
+++ b/Source/modules/wake_lock/ScreenWakeLock.cpp
@@ -0,0 +1,112 @@
+// 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/ScreenWakeLock.h"
+
+#include "core/frame/LocalFrame.h"
+#include "core/frame/Screen.h"
+#include "core/page/PageVisibilityState.h"
+#include "platform/RuntimeEnabledFeatures.h"
+#include "public/platform/modules/wake_lock/WebWakeLockClient.h"
+
+namespace blink {
+
+// static
+bool ScreenWakeLock::keepAwake(Screen& screen)
+{
+ ScreenWakeLock* screenWakeLock = fromScreen(screen);
+ if (!screenWakeLock)
+ return false;
+
+ return screenWakeLock->keepAwake();
+}
+
+// static
+void ScreenWakeLock::setKeepAwake(Screen& screen, bool keepAwake)
+{
+ ScreenWakeLock* screenWakeLock = fromScreen(screen);
+ if (!screenWakeLock)
+ return;
+
+ screenWakeLock->setKeepAwake(keepAwake);
+}
+
+void ScreenWakeLock::setKeepAwake(bool keepAwake)
+{
+ m_keepAwake = keepAwake;
+ notifyClient();
+}
+
+// static
+const char* ScreenWakeLock::supplementName()
+{
+ return "ScreenWakeLock";
+}
+
+// static
+ScreenWakeLock* ScreenWakeLock::from(LocalFrame* frame)
+{
+ return static_cast<ScreenWakeLock*>(WillBeHeapSupplement<LocalFrame>::from(frame, supplementName()));
+}
+
+// static
+void ScreenWakeLock::provideTo(LocalFrame& frame, WebWakeLockClient* client)
+{
+ ASSERT(RuntimeEnabledFeatures::wakeLockEnabled());
+ WillBeHeapSupplement<LocalFrame>::provideTo(
+ frame,
+ ScreenWakeLock::supplementName(),
+ adoptPtrWillBeNoop(new ScreenWakeLock(frame, client)));
+}
+
+void ScreenWakeLock::pageVisibilityChanged()
+{
+ notifyClient();
+}
+
+void ScreenWakeLock::didCommitLoad(LocalFrame* committedFrame)
+{
+ // Reset wake lock flag for this frame if it is the one being navigated
mlamouri (slow - plz ping) 2015/07/24 10:47:57 nit: period at the end of the sentence.
alogvinov 2015/07/24 13:36:01 Acknowledged.
+ if (committedFrame == frame()) {
+ setKeepAwake(false);
+ }
+}
+
+void ScreenWakeLock::willDetachFrameHost()
+{
+ m_client = nullptr;
+}
+
+DEFINE_TRACE(ScreenWakeLock)
+{
+ WillBeHeapSupplement<LocalFrame>::trace(visitor);
+ PageLifecycleObserver::trace(visitor);
+ LocalFrameLifecycleObserver::trace(visitor);
+}
+
+ScreenWakeLock::ScreenWakeLock(LocalFrame& frame, WebWakeLockClient* client)
+ : PageLifecycleObserver(frame.page())
+ , LocalFrameLifecycleObserver(&frame)
+ , m_keepAwake(false)
+ , m_client(client)
+{
+}
+
+// static
+ScreenWakeLock* ScreenWakeLock::fromScreen(Screen& screen)
+{
+ return screen.frame() ? ScreenWakeLock::from(screen.frame()) : nullptr;
+}
+
+void ScreenWakeLock::notifyClient()
+{
+ if (!m_client)
+ return;
+
+ bool visible = page() && page()->visibilityState() == PageVisibilityStateVisible;
+ m_client->requestKeepScreenAwake(m_keepAwake && visible);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698