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

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, 6 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..83105c2aa1fdebbdab278f7f86445f7222737213
--- /dev/null
+++ b/Source/modules/wake_lock/ScreenWakeLock.cpp
@@ -0,0 +1,113 @@
+// 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 {
+
+ScreenWakeLock::ScreenWakeLock(LocalFrame& frame, WebWakeLockClient* client)
mlamouri (slow - plz ping) 2015/07/05 14:50:14 nit: could you move around the definitions to matc
+ : PageLifecycleObserver(frame.page())
+ , LocalFrameLifecycleObserver(&frame)
+ , m_keepAwake(false)
+ , m_client(client)
+{
+}
+
+void ScreenWakeLock::setKeepAwake(bool keepAwake)
+{
+ m_keepAwake = keepAwake;
+ updateClient();
+}
+
+// static
+bool ScreenWakeLock::keepAwake(Screen& screen)
+{
+ ScreenWakeLock* screenWakeLock = fromScreen(screen);
+
mlamouri (slow - plz ping) 2015/07/05 14:50:14 nit: remove empty line
+ if (!screenWakeLock)
+ return false;
+
+ return screenWakeLock->keepAwake();
+}
+
+// static
+void ScreenWakeLock::setKeepAwake(Screen& screen, bool keepAwake)
+{
+ ScreenWakeLock* screenWakeLock = fromScreen(screen);
+
mlamouri (slow - plz ping) 2015/07/05 14:50:14 ditto
+ if (!screenWakeLock)
+ return;
+
+ screenWakeLock->setKeepAwake(keepAwake);
+}
+
+// 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()
+{
+ updateClient();
+}
+
+void ScreenWakeLock::didCommitLoad(LocalFrame* committedFrame)
+{
+ // Reset wake lock flag for this frame if it is the one being navigated
+ if (committedFrame == frame()) {
+ setKeepAwake(false);
+ }
mlamouri (slow - plz ping) 2015/07/05 14:50:14 Couldn't that be handled by the content layer?
alogvinov 2015/07/06 10:18:35 Probably it could, but we're going to need to rese
+}
+
+void ScreenWakeLock::willDetachFrameHost()
+{
+ m_client = nullptr;
+}
+
+DEFINE_TRACE(ScreenWakeLock)
+{
+ WillBeHeapSupplement<LocalFrame>::trace(visitor);
+ PageLifecycleObserver::trace(visitor);
+ LocalFrameLifecycleObserver::trace(visitor);
+}
+
+// static
+ScreenWakeLock* ScreenWakeLock::fromScreen(Screen& screen)
+{
+ return screen.frame() ? ScreenWakeLock::from(screen.frame()) : nullptr;
+}
+
+void ScreenWakeLock::updateClient()
mlamouri (slow - plz ping) 2015/07/05 14:50:14 nit: could you rename updateClient() to notificyCl
+{
+ if (m_client) {
mlamouri (slow - plz ping) 2015/07/05 14:50:14 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