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

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
« no previous file with comments | « Source/modules/wake_lock/ScreenWakeLock.h ('k') | Source/modules/wake_lock/ScreenWakeLock.idl » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..01e5d2650f2e3234a75daa9b616550668d0e32c5
--- /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.
+ 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_client(client)
+ , m_keepAwake(false)
+{
+}
+
+// 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
« no previous file with comments | « Source/modules/wake_lock/ScreenWakeLock.h ('k') | Source/modules/wake_lock/ScreenWakeLock.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698