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 16 matching lines...) Expand all Loading... | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "ServiceWorker.h" | 32 #include "ServiceWorker.h" |
33 | 33 |
34 #include "EventTargetNames.h" | 34 #include "EventTargetNames.h" |
35 #include "bindings/v8/ExceptionState.h" | 35 #include "bindings/v8/ExceptionState.h" |
36 #include "bindings/v8/NewScriptState.h" | 36 #include "bindings/v8/NewScriptState.h" |
37 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | |
37 #include "core/dom/MessagePort.h" | 38 #include "core/dom/MessagePort.h" |
38 #include "core/events/Event.h" | 39 #include "core/events/Event.h" |
39 #include "platform/NotImplemented.h" | 40 #include "platform/NotImplemented.h" |
40 #include "public/platform/WebMessagePortChannel.h" | 41 #include "public/platform/WebMessagePortChannel.h" |
41 #include "public/platform/WebServiceWorkerState.h" | 42 #include "public/platform/WebServiceWorkerState.h" |
42 #include "public/platform/WebString.h" | 43 #include "public/platform/WebString.h" |
43 | 44 |
44 namespace WebCore { | 45 namespace WebCore { |
45 | 46 |
47 class ServiceWorker::ThenFunction FINAL : public ScriptFunction { | |
48 public: | |
49 enum ResolveType { | |
50 Fulfilled, | |
51 Rejected, | |
52 }; | |
53 | |
54 static PassOwnPtr<ScriptFunction> create(PassRefPtr<ServiceWorker> observer, ResolveType type) | |
55 { | |
56 ExecutionContext* executionContext = observer->executionContext(); | |
57 return adoptPtr(new ThenFunction(toIsolate(executionContext), observer, type)); | |
58 } | |
59 private: | |
60 ThenFunction(v8::Isolate* isolate, PassRefPtr<ServiceWorker> observer, Resol veType type) | |
61 : ScriptFunction(isolate) | |
62 , m_observer(observer) | |
63 , m_resolveType(type) | |
64 { | |
65 } | |
66 | |
67 virtual ScriptValue call(ScriptValue value) OVERRIDE | |
68 { | |
69 ASSERT(m_resolveType == Fulfilled); | |
70 m_observer->onPromiseResolved(); | |
71 return value; | |
72 } | |
73 | |
74 RefPtr<ServiceWorker> m_observer; | |
75 ResolveType m_resolveType; | |
76 }; | |
77 | |
46 const AtomicString& ServiceWorker::interfaceName() const | 78 const AtomicString& ServiceWorker::interfaceName() const |
47 { | 79 { |
48 return EventTargetNames::ServiceWorker; | 80 return EventTargetNames::ServiceWorker; |
49 } | 81 } |
50 | 82 |
51 void ServiceWorker::postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exceptionState) | 83 void ServiceWorker::postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exceptionState) |
52 { | 84 { |
53 // Disentangle the port in preparation for sending it to the remote context. | 85 // Disentangle the port in preparation for sending it to the remote context. |
54 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(por ts, exceptionState); | 86 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(por ts, exceptionState); |
55 if (exceptionState.hadException()) | 87 if (exceptionState.hadException()) |
56 return; | 88 return; |
57 | 89 |
58 blink::WebString messageString = message->toWireString(); | 90 blink::WebString messageString = message->toWireString(); |
59 OwnPtr<blink::WebMessagePortChannelArray> webChannels = MessagePort::toWebMe ssagePortChannelArray(channels.release()); | 91 OwnPtr<blink::WebMessagePortChannelArray> webChannels = MessagePort::toWebMe ssagePortChannelArray(channels.release()); |
60 m_outerWorker->postMessage(messageString, webChannels.leakPtr()); | 92 m_outerWorker->postMessage(messageString, webChannels.leakPtr()); |
61 } | 93 } |
62 | 94 |
95 void ServiceWorker::onStateChanged(blink::WebServiceWorkerState state) | |
96 { | |
97 if (m_isPromisePending) | |
98 m_queuedStates.append(state); | |
99 else | |
100 changeState(state); | |
101 } | |
102 | |
63 void ServiceWorker::dispatchStateChangeEvent() | 103 void ServiceWorker::dispatchStateChangeEvent() |
64 { | 104 { |
kinuko
2014/04/23 14:20:00
Can you also add 'FIXME: deprecate' comment here?
falken
2014/04/24 00:58:50
Done.
| |
65 this->dispatchEvent(Event::create(EventTypeNames::statechange)); | 105 changeState(m_outerWorker->state()); |
66 } | 106 } |
67 | 107 |
68 const AtomicString& ServiceWorker::state() const | 108 const AtomicString& ServiceWorker::state() const |
69 { | 109 { |
70 DEFINE_STATIC_LOCAL(AtomicString, unknown, ("unknown", AtomicString::Constru ctFromLiteral)); | 110 DEFINE_STATIC_LOCAL(AtomicString, unknown, ("unknown", AtomicString::Constru ctFromLiteral)); |
71 DEFINE_STATIC_LOCAL(AtomicString, parsed, ("parsed", AtomicString::Construct FromLiteral)); | 111 DEFINE_STATIC_LOCAL(AtomicString, parsed, ("parsed", AtomicString::Construct FromLiteral)); |
72 DEFINE_STATIC_LOCAL(AtomicString, installing, ("installing", AtomicString::C onstructFromLiteral)); | 112 DEFINE_STATIC_LOCAL(AtomicString, installing, ("installing", AtomicString::C onstructFromLiteral)); |
73 DEFINE_STATIC_LOCAL(AtomicString, installed, ("installed", AtomicString::Con structFromLiteral)); | 113 DEFINE_STATIC_LOCAL(AtomicString, installed, ("installed", AtomicString::Con structFromLiteral)); |
74 DEFINE_STATIC_LOCAL(AtomicString, activating, ("activating", AtomicString::C onstructFromLiteral)); | 114 DEFINE_STATIC_LOCAL(AtomicString, activating, ("activating", AtomicString::C onstructFromLiteral)); |
75 DEFINE_STATIC_LOCAL(AtomicString, active, ("active", AtomicString::Construct FromLiteral)); | 115 DEFINE_STATIC_LOCAL(AtomicString, active, ("active", AtomicString::Construct FromLiteral)); |
(...skipping 15 matching lines...) Expand all Loading... | |
91 case blink::WebServiceWorkerStateActive: | 131 case blink::WebServiceWorkerStateActive: |
92 return active; | 132 return active; |
93 case blink::WebServiceWorkerStateDeactivated: | 133 case blink::WebServiceWorkerStateDeactivated: |
94 return deactivated; | 134 return deactivated; |
95 default: | 135 default: |
96 ASSERT_NOT_REACHED(); | 136 ASSERT_NOT_REACHED(); |
97 return nullAtom; | 137 return nullAtom; |
98 } | 138 } |
99 } | 139 } |
100 | 140 |
101 PassRefPtr<ServiceWorker> ServiceWorker::from(NewScriptState* scriptState, WebTy pe* worker) | 141 PassRefPtr<ServiceWorker> ServiceWorker::from(ScriptPromiseResolverWithContext* resolver, WebType* worker) |
102 { | 142 { |
103 return create(scriptState->executionContext(), adoptPtr(worker)); | 143 NewScriptState::Scope scope(resolver->scriptState()); |
144 RefPtr<ServiceWorker> serviceWorker = create(resolver->scriptState()->execut ionContext(), adoptPtr(worker)); | |
145 serviceWorker->waitOnPromise(resolver->promise()); | |
146 return serviceWorker; | |
147 } | |
148 | |
149 void ServiceWorker::onPromiseResolved() | |
150 { | |
kinuko
2014/04/23 14:20:00
nit: maybe ASSERT(m_isPromisePending) here
falken
2014/04/24 00:58:50
Done.
| |
151 m_isPromisePending = false; | |
152 for (Vector<blink::WebServiceWorkerState>::iterator iterator = m_queuedState s.begin(); iterator != m_queuedStates.end(); ++iterator) | |
153 changeState(*iterator); | |
154 } | |
155 | |
156 void ServiceWorker::waitOnPromise(ScriptPromise promise) | |
157 { | |
158 m_isPromisePending = true; | |
159 promise.then( | |
160 ThenFunction::create(this, ThenFunction::Fulfilled), | |
161 ThenFunction::create(this, ThenFunction::Rejected)); | |
162 } | |
163 | |
164 void ServiceWorker::changeState(blink::WebServiceWorkerState state) | |
165 { | |
166 m_outerWorker->setState(state); | |
167 this->dispatchEvent(Event::create(EventTypeNames::statechange)); | |
104 } | 168 } |
105 | 169 |
106 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionConte xt, PassOwnPtr<blink::WebServiceWorker> outerWorker) | 170 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionConte xt, PassOwnPtr<blink::WebServiceWorker> outerWorker) |
107 { | 171 { |
108 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext, outerWorker)); | 172 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext, outerWorker)); |
109 worker->suspendIfNeeded(); | 173 worker->suspendIfNeeded(); |
110 return worker.release(); | 174 return worker.release(); |
111 } | 175 } |
112 | 176 |
113 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blin k::WebServiceWorker> worker) | 177 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blin k::WebServiceWorker> worker) |
114 : AbstractWorker(executionContext) | 178 : AbstractWorker(executionContext) |
115 , m_outerWorker(worker) | 179 , m_outerWorker(worker) |
180 , m_isPromisePending(false) | |
116 { | 181 { |
117 ScriptWrappable::init(this); | 182 ScriptWrappable::init(this); |
118 ASSERT(m_outerWorker); | 183 ASSERT(m_outerWorker); |
119 m_outerWorker->setProxy(this); | 184 m_outerWorker->setProxy(this); |
120 } | 185 } |
121 | 186 |
122 } // namespace WebCore | 187 } // namespace WebCore |
OLD | NEW |