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

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
« no previous file with comments | « Source/modules/wake_lock/WakeLockController.h ('k') | Source/modules/wake_lock/WakeLockPromiseResolver.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..3d3f4f0f62e691421999b631f8cc6befc3337384
--- /dev/null
+++ b/Source/modules/wake_lock/WakeLockController.cpp
@@ -0,0 +1,137 @@
+// 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_isLocked.resize(WebWakeLockGuard);
+}
+
+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_isLocked[type];
+}
+
+ScriptPromise WakeLockController::requestLockOrUnlock(ScriptState* state, WebWakeLockType type, bool lock)
+{
+ WakeLockPromiseResolverOwnPtr resolver = WakeLockPromiseResolver::create(state, type);
+ ScriptPromise promise = resolver->promise();
+
+ if (m_client) {
+ const int resolverId = resolver->id();
+ m_resolvers.append(resolver.release());
+ if (lock) {
+ m_client->requestWakeLock(type, resolverId,
+ WebSecurityOrigin(state->executionContext()->securityOrigin()));
+ } else {
+ m_client->requestWakeUnlock(type, resolverId);
+ }
+ } else {
+ resolver->reject();
+ }
+ return promise;
+}
+
+void WakeLockController::onCreatedWakeLockSuccessful(int resolverId)
+{
+ WebWakeLockType type = resolveOrRejectById(resolverId, true);
+ m_isLocked[type] = true;
+}
+
+void WakeLockController::onCreatedWakeLockFailed(int resolverId)
+{
+ resolveOrRejectById(resolverId, false);
+}
+
+void WakeLockController::onUnlockedWakeLockSuccessful(int resolverId)
+{
+ WebWakeLockType type = resolveOrRejectById(resolverId, true);
+ m_isLocked[type] = false;
+}
+
+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;
+ 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 WebWakeLockGuard;
+}
+
+} // namespace blink
« no previous file with comments | « Source/modules/wake_lock/WakeLockController.h ('k') | Source/modules/wake_lock/WakeLockPromiseResolver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698