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 | |
103 // FIXME: To be removed, this is just here as part of a three-sided patch. | |
63 void ServiceWorker::dispatchStateChangeEvent() | 104 void ServiceWorker::dispatchStateChangeEvent() |
64 { | 105 { |
65 this->dispatchEvent(Event::create(EventTypeNames::statechange)); | 106 changeState(m_outerWorker->state()); |
66 } | 107 } |
67 | 108 |
68 const AtomicString& ServiceWorker::state() const | 109 const AtomicString& ServiceWorker::state() const |
69 { | 110 { |
70 DEFINE_STATIC_LOCAL(AtomicString, unknown, ("unknown", AtomicString::Constru ctFromLiteral)); | 111 DEFINE_STATIC_LOCAL(AtomicString, unknown, ("unknown", AtomicString::Constru ctFromLiteral)); |
71 DEFINE_STATIC_LOCAL(AtomicString, parsed, ("parsed", AtomicString::Construct FromLiteral)); | 112 DEFINE_STATIC_LOCAL(AtomicString, parsed, ("parsed", AtomicString::Construct FromLiteral)); |
72 DEFINE_STATIC_LOCAL(AtomicString, installing, ("installing", AtomicString::C onstructFromLiteral)); | 113 DEFINE_STATIC_LOCAL(AtomicString, installing, ("installing", AtomicString::C onstructFromLiteral)); |
73 DEFINE_STATIC_LOCAL(AtomicString, installed, ("installed", AtomicString::Con structFromLiteral)); | 114 DEFINE_STATIC_LOCAL(AtomicString, installed, ("installed", AtomicString::Con structFromLiteral)); |
74 DEFINE_STATIC_LOCAL(AtomicString, activating, ("activating", AtomicString::C onstructFromLiteral)); | 115 DEFINE_STATIC_LOCAL(AtomicString, activating, ("activating", AtomicString::C onstructFromLiteral)); |
75 DEFINE_STATIC_LOCAL(AtomicString, active, ("active", AtomicString::Construct FromLiteral)); | 116 DEFINE_STATIC_LOCAL(AtomicString, active, ("active", AtomicString::Construct FromLiteral)); |
(...skipping 15 matching lines...) Expand all Loading... | |
91 case blink::WebServiceWorkerStateActive: | 132 case blink::WebServiceWorkerStateActive: |
92 return active; | 133 return active; |
93 case blink::WebServiceWorkerStateDeactivated: | 134 case blink::WebServiceWorkerStateDeactivated: |
94 return deactivated; | 135 return deactivated; |
95 default: | 136 default: |
96 ASSERT_NOT_REACHED(); | 137 ASSERT_NOT_REACHED(); |
97 return nullAtom; | 138 return nullAtom; |
98 } | 139 } |
99 } | 140 } |
100 | 141 |
101 PassRefPtr<ServiceWorker> ServiceWorker::from(NewScriptState* scriptState, WebTy pe* worker) | 142 PassRefPtr<ServiceWorker> ServiceWorker::from(ScriptPromiseResolverWithContext* resolver, WebType* worker) |
102 { | 143 { |
103 return create(scriptState->executionContext(), adoptPtr(worker)); | 144 NewScriptState::Scope scope(resolver->scriptState()); |
145 RefPtr<ServiceWorker> serviceWorker = create(resolver->scriptState()->execut ionContext(), adoptPtr(worker)); | |
146 serviceWorker->waitOnPromise(resolver->promise()); | |
147 return serviceWorker; | |
148 } | |
149 | |
150 void ServiceWorker::onPromiseResolved() | |
151 { | |
152 ASSERT(m_isPromisePending); | |
153 m_isPromisePending = false; | |
154 for (Vector<blink::WebServiceWorkerState>::iterator iterator = m_queuedState s.begin(); iterator != m_queuedStates.end(); ++iterator) | |
155 changeState(*iterator); | |
156 } | |
yhirano
2014/04/24 01:53:49
Shouldn't we clear the queue?
falken
2014/04/24 02:38:42
Oh course! Thanks
| |
157 | |
158 void ServiceWorker::waitOnPromise(ScriptPromise promise) | |
159 { | |
160 m_isPromisePending = true; | |
161 promise.then( | |
162 ThenFunction::create(this, ThenFunction::Fulfilled), | |
163 ThenFunction::create(this, ThenFunction::Rejected)); | |
yhirano
2014/04/24 01:53:49
You don't have to pass the second parameter. It do
falken
2014/04/24 02:38:42
I didn't realize we could do that. Thanks
| |
164 } | |
165 | |
166 void ServiceWorker::changeState(blink::WebServiceWorkerState state) | |
167 { | |
168 m_outerWorker->setState(state); | |
169 this->dispatchEvent(Event::create(EventTypeNames::statechange)); | |
104 } | 170 } |
105 | 171 |
106 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionConte xt, PassOwnPtr<blink::WebServiceWorker> outerWorker) | 172 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionConte xt, PassOwnPtr<blink::WebServiceWorker> outerWorker) |
107 { | 173 { |
108 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext, outerWorker)); | 174 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext, outerWorker)); |
109 worker->suspendIfNeeded(); | 175 worker->suspendIfNeeded(); |
110 return worker.release(); | 176 return worker.release(); |
111 } | 177 } |
112 | 178 |
113 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blin k::WebServiceWorker> worker) | 179 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blin k::WebServiceWorker> worker) |
114 : AbstractWorker(executionContext) | 180 : AbstractWorker(executionContext) |
115 , m_outerWorker(worker) | 181 , m_outerWorker(worker) |
182 , m_isPromisePending(false) | |
116 { | 183 { |
117 ScriptWrappable::init(this); | 184 ScriptWrappable::init(this); |
118 ASSERT(m_outerWorker); | 185 ASSERT(m_outerWorker); |
119 m_outerWorker->setProxy(this); | 186 m_outerWorker->setProxy(this); |
120 } | 187 } |
121 | 188 |
122 } // namespace WebCore | 189 } // namespace WebCore |
OLD | NEW |