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_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.
| |
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_lockCounter[type] > 0); | |
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 const int resolverId = resolver->id(); | |
80 | |
81 if (m_client) { | |
82 m_resolvers.append(resolver.release()); | |
83 if (lock) { | |
84 m_client->requestWakeLock(type, resolverId, | |
85 blink::WebSecurityOrigin(context->securityOrigin())); | |
86 } else { | |
87 m_client->requestWakeUnlock(type, resolverId); | |
88 } | |
89 } else { | |
90 resolver->resolve(); | |
91 } | |
92 return promise; | |
93 } | |
94 | |
95 void WakeLockController::onCreatedWakeLockSuccessful(int resolverId) | |
96 { | |
97 WebWakeLockType type = resolveOrRejectById(resolverId, true); | |
98 ++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.
| |
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 if (m_lockCounter[type] > 0) | |
110 --m_lockCounter[type]; | |
111 } | |
112 | |
113 void WakeLockController::onUnlockedWakeLockFailed(int resolverId) | |
114 { | |
115 resolveOrRejectById(resolverId, false); | |
116 } | |
117 | |
118 WebWakeLockType WakeLockController::resolveOrRejectById(int resolverId, bool suc cessful) | |
119 { | |
120 for (Vector<WakeLockPromiseResolverOwnPtr>::iterator it = m_resolvers.begin( ); | |
121 it != m_resolvers.end(); | |
122 ++it) { | |
123 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.
| |
124 const WebWakeLockType type = resolver->type(); | |
125 if (resolver->id() == resolverId) { | |
126 if (successful) | |
127 resolver->resolve(); | |
128 else | |
129 resolver->reject(); | |
130 m_resolvers.remove(it - m_resolvers.begin()); | |
131 return type; | |
132 } | |
133 } | |
134 ASSERT_NOT_REACHED(); | |
135 return WebWakeLockNone; | |
136 } | |
137 | |
138 } // namespace WebCore | |
OLD | NEW |