| 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
|
|
|