OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 if (exceptionState.hadException()) | 79 if (exceptionState.hadException()) |
80 return; | 80 return; |
81 | 81 |
82 blink::WebString messageString = message->toWireString(); | 82 blink::WebString messageString = message->toWireString(); |
83 OwnPtr<blink::WebMessagePortChannelArray> webChannels = MessagePort::toWebMe
ssagePortChannelArray(channels.release()); | 83 OwnPtr<blink::WebMessagePortChannelArray> webChannels = MessagePort::toWebMe
ssagePortChannelArray(channels.release()); |
84 m_outerWorker->postMessage(messageString, webChannels.leakPtr()); | 84 m_outerWorker->postMessage(messageString, webChannels.leakPtr()); |
85 } | 85 } |
86 | 86 |
87 bool ServiceWorker::isReady() | 87 bool ServiceWorker::isReady() |
88 { | 88 { |
89 return !m_isPromisePending; | 89 return m_proxyState == Ready; |
90 } | 90 } |
91 | 91 |
92 void ServiceWorker::dispatchStateChangeEvent() | 92 void ServiceWorker::dispatchStateChangeEvent() |
93 { | 93 { |
94 ASSERT(isReady()); | 94 ASSERT(isReady()); |
95 this->dispatchEvent(Event::create(EventTypeNames::statechange)); | 95 this->dispatchEvent(Event::create(EventTypeNames::statechange)); |
96 } | 96 } |
97 | 97 |
98 String ServiceWorker::scope() const | 98 String ServiceWorker::scope() const |
99 { | 99 { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 } | 154 } |
155 | 155 |
156 PassRefPtr<ServiceWorker> ServiceWorker::from(ScriptPromiseResolverWithContext*
resolver, WebType* worker) | 156 PassRefPtr<ServiceWorker> ServiceWorker::from(ScriptPromiseResolverWithContext*
resolver, WebType* worker) |
157 { | 157 { |
158 RefPtr<ServiceWorker> serviceWorker = ServiceWorker::from(resolver->scriptSt
ate()->executionContext(), worker); | 158 RefPtr<ServiceWorker> serviceWorker = ServiceWorker::from(resolver->scriptSt
ate()->executionContext(), worker); |
159 ScriptState::Scope scope(resolver->scriptState()); | 159 ScriptState::Scope scope(resolver->scriptState()); |
160 serviceWorker->waitOnPromise(resolver->promise()); | 160 serviceWorker->waitOnPromise(resolver->promise()); |
161 return serviceWorker; | 161 return serviceWorker; |
162 } | 162 } |
163 | 163 |
| 164 void ServiceWorker::setProxyState(ProxyState state) |
| 165 { |
| 166 if (m_proxyState == state) |
| 167 return; |
| 168 switch (m_proxyState) { |
| 169 case Initial: |
| 170 ASSERT(state == RegisterPromisePending || state == ContextStopped); |
| 171 break; |
| 172 case RegisterPromisePending: |
| 173 ASSERT(state == Ready || state == ContextStopped); |
| 174 break; |
| 175 case Ready: |
| 176 ASSERT(state == ContextStopped); |
| 177 break; |
| 178 case ContextStopped: |
| 179 ASSERT_NOT_REACHED(); |
| 180 break; |
| 181 } |
| 182 |
| 183 ProxyState oldState = m_proxyState; |
| 184 m_proxyState = state; |
| 185 if (oldState == Ready || state == Ready) |
| 186 m_outerWorker->proxyReadyChanged(); |
| 187 } |
| 188 |
164 void ServiceWorker::onPromiseResolved() | 189 void ServiceWorker::onPromiseResolved() |
165 { | 190 { |
166 ASSERT(m_isPromisePending); | 191 if (m_proxyState == ContextStopped) |
167 m_isPromisePending = false; | 192 return; |
168 m_outerWorker->proxyReadyChanged(); | 193 setProxyState(Ready); |
169 } | 194 } |
170 | 195 |
171 void ServiceWorker::waitOnPromise(ScriptPromise promise) | 196 void ServiceWorker::waitOnPromise(ScriptPromise promise) |
172 { | 197 { |
173 ASSERT(!m_isPromisePending); | 198 setProxyState(RegisterPromisePending); |
174 m_isPromisePending = true; | |
175 m_outerWorker->proxyReadyChanged(); | |
176 promise.then(ThenFunction::create(this)); | 199 promise.then(ThenFunction::create(this)); |
177 } | 200 } |
178 | 201 |
| 202 bool ServiceWorker::hasPendingActivity() const |
| 203 { |
| 204 if (AbstractWorker::hasPendingActivity()) |
| 205 return true; |
| 206 if (m_proxyState == ContextStopped) |
| 207 return false; |
| 208 return m_outerWorker->state() != blink::WebServiceWorkerStateDeactivated; |
| 209 } |
| 210 |
| 211 void ServiceWorker::stop() |
| 212 { |
| 213 setProxyState(ContextStopped); |
| 214 } |
| 215 |
179 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionConte
xt, PassOwnPtr<blink::WebServiceWorker> outerWorker) | 216 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionConte
xt, PassOwnPtr<blink::WebServiceWorker> outerWorker) |
180 { | 217 { |
181 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext,
outerWorker)); | 218 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext,
outerWorker)); |
182 worker->suspendIfNeeded(); | 219 worker->suspendIfNeeded(); |
183 return worker.release(); | 220 return worker.release(); |
184 } | 221 } |
185 | 222 |
186 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blin
k::WebServiceWorker> worker) | 223 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blin
k::WebServiceWorker> worker) |
187 : AbstractWorker(executionContext) | 224 : AbstractWorker(executionContext) |
188 , WebServiceWorkerProxy(this) | 225 , WebServiceWorkerProxy(this) |
189 , m_outerWorker(worker) | 226 , m_outerWorker(worker) |
190 , m_isPromisePending(false) | 227 , m_proxyState(Initial) |
191 { | 228 { |
192 ScriptWrappable::init(this); | 229 ScriptWrappable::init(this); |
193 ASSERT(m_outerWorker); | 230 ASSERT(m_outerWorker); |
194 m_outerWorker->setProxy(this); | 231 m_outerWorker->setProxy(this); |
195 } | 232 } |
196 | 233 |
197 } // namespace WebCore | 234 } // namespace WebCore |
OLD | NEW |