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

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

Issue 399313003: Initial implementation of API WakeLock. Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Implementation of WakeLock API on JavaScript side Created 6 years, 4 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/WakeLockController.cpp
diff --git a/Source/modules/wake_lock/WakeLockController.cpp b/Source/modules/wake_lock/WakeLockController.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..1f21a4bc9d328d555802a6f7c4efda3c100ceb60
--- /dev/null
+++ b/Source/modules/wake_lock/WakeLockController.cpp
@@ -0,0 +1,138 @@
+// Copyright 2014 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/WakeLockController.h"
+
+#include "modules/wake_lock/WakeLockPromiseResolver.h"
+#include "public/platform/WebWakeLockClient.h"
+
+namespace blink {
+
+WakeLockController::WakeLockController(Page& page, WebWakeLockClient* client)
+ : PageLifecycleObserver(&page)
+ , m_client(client)
+{
+ m_lockCounter.resize(WebWakeLockNone + 1);
mlamouri (slow - plz ping) 2014/08/18 12:15:53 I'm not sure you need the +1, assuming you will no
redchenko 2014/08/19 16:42:20 Done.
+}
+
+WakeLockController::~WakeLockController()
+{
+}
+
+void WakeLockController::willBeDestroyed()
+{
+ if (m_client)
+ m_client->wakeLockDestroyed();
+}
+
+PassOwnPtrWillBeRawPtr<WakeLockController> WakeLockController::create(Page& page, WebWakeLockClient* client)
+{
+ return adoptPtrWillBeNoop(new WakeLockController(page, client));
+}
+
+void WakeLockController::resetClient()
+{
+ m_client = 0;
+}
+
+const char* WakeLockController::supplementName()
+{
+ return "WakeLockController";
+}
+
+WakeLockController* WakeLockController::from(Page* page)
+{
+ return static_cast<WakeLockController*>(WillBeHeapSupplement<Page>::from(page, supplementName()));
+}
+
+void WakeLockController::provideWakeLockTo(Page& page, WebWakeLockClient* client)
+{
+ WillBeHeapSupplement<Page>::provideTo(page, WakeLockController::supplementName(), WakeLockController::create(page, client));
+}
+
+void WakeLockController::trace(Visitor* visitor)
+{
+ WillBeHeapSupplement<Page>::trace(visitor);
+}
+
+ScriptPromise WakeLockController::requestWakeLock(ScriptState* state, WebWakeLockType type)
+{
+ return requestLockOrUnlock(state, type, true);
+}
+
+ScriptPromise WakeLockController::requestWakeUnlock(ScriptState* state, WebWakeLockType type)
+{
+ return requestLockOrUnlock(state, type, false);
+}
+
+bool WakeLockController::isHeld(WebWakeLockType type)
+{
+ return (m_lockCounter[type] > 0);
+}
+
+ScriptPromise WakeLockController::requestLockOrUnlock(ScriptState* state, WebWakeLockType type, bool lock)
+{
+ WakeLockPromiseResolverOwnPtr resolver = WakeLockPromiseResolver::create(state, type);
+ ScriptPromise promise = resolver->promise();
+ const int resolverId = resolver->id();
+
+ if (m_client) {
+ m_resolvers.append(resolver.release());
+ if (lock) {
+ m_client->requestWakeLock(type, resolverId,
+ blink::WebSecurityOrigin(context->securityOrigin()));
+ } else {
+ m_client->requestWakeUnlock(type, resolverId);
+ }
+ } else {
+ resolver->resolve();
+ }
+ return promise;
+}
+
+void WakeLockController::onCreatedWakeLockSuccessful(int resolverId)
+{
+ WebWakeLockType type = resolveOrRejectById(resolverId, true);
+ ++m_lockCounter[type];
mlamouri (slow - plz ping) 2014/08/18 12:15:53 Does that mean that we expect to handle multiple l
redchenko 2014/08/19 16:42:20 Done.
+}
+
+void WakeLockController::onCreatedWakeLockFailed(int resolverId)
+{
+ resolveOrRejectById(resolverId, false);
+}
+
+void WakeLockController::onUnlockedWakeLockSuccessful(int resolverId)
+{
+ WebWakeLockType type = resolveOrRejectById(resolverId, true);
+ if (m_lockCounter[type] > 0)
+ --m_lockCounter[type];
+}
+
+void WakeLockController::onUnlockedWakeLockFailed(int resolverId)
+{
+ resolveOrRejectById(resolverId, false);
+}
+
+WebWakeLockType WakeLockController::resolveOrRejectById(int resolverId, bool successful)
+{
+ for (Vector<WakeLockPromiseResolverOwnPtr>::iterator it = m_resolvers.begin();
+ it != m_resolvers.end();
+ ++it) {
+ WakeLockPromiseResolverOwnPtr& resolver = (*it);
mlamouri (slow - plz ping) 2014/08/18 12:15:53 nit: no need for the parenthesis.
redchenko 2014/08/19 16:42:20 Done.
+ const WebWakeLockType type = resolver->type();
+ if (resolver->id() == resolverId) {
+ if (successful)
+ resolver->resolve();
+ else
+ resolver->reject();
+ m_resolvers.remove(it - m_resolvers.begin());
+ return type;
+ }
+ }
+ ASSERT_NOT_REACHED();
+ return WebWakeLockNone;
+}
+
+} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698