OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/wake_lock/WakeLockController.h" |
| 7 |
| 8 #include "modules/wake_lock/WakeLockPromiseResolver.h" |
| 9 #include "public/platform/WebWakeLockClient.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 WakeLockController::WakeLockController(Page& page, WebWakeLockClient* client) |
| 14 : PageLifecycleObserver(&page) |
| 15 , m_client(client) |
| 16 { |
| 17 m_isLocked.resize(WebWakeLockGuard); |
| 18 } |
| 19 |
| 20 WakeLockController::~WakeLockController() |
| 21 { |
| 22 } |
| 23 |
| 24 void WakeLockController::willBeDestroyed() |
| 25 { |
| 26 if (m_client) |
| 27 m_client->wakeLockDestroyed(); |
| 28 } |
| 29 |
| 30 PassOwnPtrWillBeRawPtr<WakeLockController> WakeLockController::create(Page& page
, WebWakeLockClient* client) |
| 31 { |
| 32 return adoptPtrWillBeNoop(new WakeLockController(page, client)); |
| 33 } |
| 34 |
| 35 void WakeLockController::resetClient() |
| 36 { |
| 37 m_client = 0; |
| 38 } |
| 39 |
| 40 const char* WakeLockController::supplementName() |
| 41 { |
| 42 return "WakeLockController"; |
| 43 } |
| 44 |
| 45 WakeLockController* WakeLockController::from(Page* page) |
| 46 { |
| 47 return static_cast<WakeLockController*>(WillBeHeapSupplement<Page>::from(pag
e, supplementName())); |
| 48 } |
| 49 |
| 50 void WakeLockController::provideWakeLockTo(Page& page, WebWakeLockClient* client
) |
| 51 { |
| 52 WillBeHeapSupplement<Page>::provideTo(page, WakeLockController::supplementNa
me(), WakeLockController::create(page, client)); |
| 53 } |
| 54 |
| 55 void WakeLockController::trace(Visitor* visitor) |
| 56 { |
| 57 WillBeHeapSupplement<Page>::trace(visitor); |
| 58 } |
| 59 |
| 60 ScriptPromise WakeLockController::requestWakeLock(ScriptState* state, WebWakeLoc
kType type) |
| 61 { |
| 62 return requestLockOrUnlock(state, type, true); |
| 63 } |
| 64 |
| 65 ScriptPromise WakeLockController::requestWakeUnlock(ScriptState* state, WebWakeL
ockType type) |
| 66 { |
| 67 return requestLockOrUnlock(state, type, false); |
| 68 } |
| 69 |
| 70 bool WakeLockController::isHeld(WebWakeLockType type) |
| 71 { |
| 72 return m_isLocked[type]; |
| 73 } |
| 74 |
| 75 ScriptPromise WakeLockController::requestLockOrUnlock(ScriptState* state, WebWak
eLockType type, bool lock) |
| 76 { |
| 77 WakeLockPromiseResolverOwnPtr resolver = WakeLockPromiseResolver::create(sta
te, type); |
| 78 ScriptPromise promise = resolver->promise(); |
| 79 |
| 80 if (m_client) { |
| 81 const int resolverId = resolver->id(); |
| 82 m_resolvers.append(resolver.release()); |
| 83 if (lock) { |
| 84 m_client->requestWakeLock(type, resolverId, |
| 85 WebSecurityOrigin(state->executionContext()->securityOrigin())); |
| 86 } else { |
| 87 m_client->requestWakeUnlock(type, resolverId); |
| 88 } |
| 89 } else { |
| 90 resolver->reject(); |
| 91 } |
| 92 return promise; |
| 93 } |
| 94 |
| 95 void WakeLockController::onCreatedWakeLockSuccessful(int resolverId) |
| 96 { |
| 97 WebWakeLockType type = resolveOrRejectById(resolverId, true); |
| 98 m_isLocked[type] = true; |
| 99 } |
| 100 |
| 101 void WakeLockController::onCreatedWakeLockFailed(int resolverId) |
| 102 { |
| 103 resolveOrRejectById(resolverId, false); |
| 104 } |
| 105 |
| 106 void WakeLockController::onUnlockedWakeLockSuccessful(int resolverId) |
| 107 { |
| 108 WebWakeLockType type = resolveOrRejectById(resolverId, true); |
| 109 m_isLocked[type] = false; |
| 110 } |
| 111 |
| 112 void WakeLockController::onUnlockedWakeLockFailed(int resolverId) |
| 113 { |
| 114 resolveOrRejectById(resolverId, false); |
| 115 } |
| 116 |
| 117 WebWakeLockType WakeLockController::resolveOrRejectById(int resolverId, bool suc
cessful) |
| 118 { |
| 119 for (Vector<WakeLockPromiseResolverOwnPtr>::iterator it = m_resolvers.begin(
); |
| 120 it != m_resolvers.end(); |
| 121 ++it) { |
| 122 WakeLockPromiseResolverOwnPtr& resolver = *it; |
| 123 const WebWakeLockType type = resolver->type(); |
| 124 if (resolver->id() == resolverId) { |
| 125 if (successful) |
| 126 resolver->resolve(); |
| 127 else |
| 128 resolver->reject(); |
| 129 m_resolvers.remove(it - m_resolvers.begin()); |
| 130 return type; |
| 131 } |
| 132 } |
| 133 ASSERT_NOT_REACHED(); |
| 134 return WebWakeLockGuard; |
| 135 } |
| 136 |
| 137 } // namespace blink |
OLD | NEW |