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 static PassOwnPtr<ScriptFunction> create(PassRefPtr<ServiceWorker> observer) |
| 50 { |
| 51 ExecutionContext* executionContext = observer->executionContext(); |
| 52 return adoptPtr(new ThenFunction(toIsolate(executionContext), observer))
; |
| 53 } |
| 54 private: |
| 55 ThenFunction(v8::Isolate* isolate, PassRefPtr<ServiceWorker> observer) |
| 56 : ScriptFunction(isolate) |
| 57 , m_observer(observer) |
| 58 { |
| 59 } |
| 60 |
| 61 virtual ScriptValue call(ScriptValue value) OVERRIDE |
| 62 { |
| 63 m_observer->onPromiseResolved(); |
| 64 return value; |
| 65 } |
| 66 |
| 67 RefPtr<ServiceWorker> m_observer; |
| 68 }; |
| 69 |
46 const AtomicString& ServiceWorker::interfaceName() const | 70 const AtomicString& ServiceWorker::interfaceName() const |
47 { | 71 { |
48 return EventTargetNames::ServiceWorker; | 72 return EventTargetNames::ServiceWorker; |
49 } | 73 } |
50 | 74 |
51 void ServiceWorker::postMessage(PassRefPtr<SerializedScriptValue> message, const
MessagePortArray* ports, ExceptionState& exceptionState) | 75 void ServiceWorker::postMessage(PassRefPtr<SerializedScriptValue> message, const
MessagePortArray* ports, ExceptionState& exceptionState) |
52 { | 76 { |
53 // Disentangle the port in preparation for sending it to the remote context. | 77 // Disentangle the port in preparation for sending it to the remote context. |
54 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(por
ts, exceptionState); | 78 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(por
ts, exceptionState); |
55 if (exceptionState.hadException()) | 79 if (exceptionState.hadException()) |
56 return; | 80 return; |
57 | 81 |
58 blink::WebString messageString = message->toWireString(); | 82 blink::WebString messageString = message->toWireString(); |
59 OwnPtr<blink::WebMessagePortChannelArray> webChannels = MessagePort::toWebMe
ssagePortChannelArray(channels.release()); | 83 OwnPtr<blink::WebMessagePortChannelArray> webChannels = MessagePort::toWebMe
ssagePortChannelArray(channels.release()); |
60 m_outerWorker->postMessage(messageString, webChannels.leakPtr()); | 84 m_outerWorker->postMessage(messageString, webChannels.leakPtr()); |
61 } | 85 } |
62 | 86 |
| 87 void ServiceWorker::onStateChanged(blink::WebServiceWorkerState state) |
| 88 { |
| 89 if (m_isPromisePending) |
| 90 m_queuedStates.append(state); |
| 91 else |
| 92 changeState(state); |
| 93 } |
| 94 |
| 95 // FIXME: To be removed, this is just here as part of a three-sided patch. |
63 void ServiceWorker::dispatchStateChangeEvent() | 96 void ServiceWorker::dispatchStateChangeEvent() |
64 { | 97 { |
65 this->dispatchEvent(Event::create(EventTypeNames::statechange)); | 98 changeState(m_outerWorker->state()); |
66 } | 99 } |
67 | 100 |
68 const AtomicString& ServiceWorker::state() const | 101 const AtomicString& ServiceWorker::state() const |
69 { | 102 { |
70 DEFINE_STATIC_LOCAL(AtomicString, unknown, ("unknown", AtomicString::Constru
ctFromLiteral)); | 103 DEFINE_STATIC_LOCAL(AtomicString, unknown, ("unknown", AtomicString::Constru
ctFromLiteral)); |
71 DEFINE_STATIC_LOCAL(AtomicString, parsed, ("parsed", AtomicString::Construct
FromLiteral)); | 104 DEFINE_STATIC_LOCAL(AtomicString, parsed, ("parsed", AtomicString::Construct
FromLiteral)); |
72 DEFINE_STATIC_LOCAL(AtomicString, installing, ("installing", AtomicString::C
onstructFromLiteral)); | 105 DEFINE_STATIC_LOCAL(AtomicString, installing, ("installing", AtomicString::C
onstructFromLiteral)); |
73 DEFINE_STATIC_LOCAL(AtomicString, installed, ("installed", AtomicString::Con
structFromLiteral)); | 106 DEFINE_STATIC_LOCAL(AtomicString, installed, ("installed", AtomicString::Con
structFromLiteral)); |
74 DEFINE_STATIC_LOCAL(AtomicString, activating, ("activating", AtomicString::C
onstructFromLiteral)); | 107 DEFINE_STATIC_LOCAL(AtomicString, activating, ("activating", AtomicString::C
onstructFromLiteral)); |
75 DEFINE_STATIC_LOCAL(AtomicString, active, ("active", AtomicString::Construct
FromLiteral)); | 108 DEFINE_STATIC_LOCAL(AtomicString, active, ("active", AtomicString::Construct
FromLiteral)); |
(...skipping 15 matching lines...) Expand all Loading... |
91 case blink::WebServiceWorkerStateActive: | 124 case blink::WebServiceWorkerStateActive: |
92 return active; | 125 return active; |
93 case blink::WebServiceWorkerStateDeactivated: | 126 case blink::WebServiceWorkerStateDeactivated: |
94 return deactivated; | 127 return deactivated; |
95 default: | 128 default: |
96 ASSERT_NOT_REACHED(); | 129 ASSERT_NOT_REACHED(); |
97 return nullAtom; | 130 return nullAtom; |
98 } | 131 } |
99 } | 132 } |
100 | 133 |
101 PassRefPtr<ServiceWorker> ServiceWorker::from(NewScriptState* scriptState, WebTy
pe* worker) | 134 PassRefPtr<ServiceWorker> ServiceWorker::from(ScriptPromiseResolverWithContext*
resolver, WebType* worker) |
102 { | 135 { |
103 return create(scriptState->executionContext(), adoptPtr(worker)); | 136 NewScriptState::Scope scope(resolver->scriptState()); |
| 137 RefPtr<ServiceWorker> serviceWorker = create(resolver->scriptState()->execut
ionContext(), adoptPtr(worker)); |
| 138 serviceWorker->waitOnPromise(resolver->promise()); |
| 139 return serviceWorker; |
| 140 } |
| 141 |
| 142 void ServiceWorker::onPromiseResolved() |
| 143 { |
| 144 ASSERT(m_isPromisePending); |
| 145 m_isPromisePending = false; |
| 146 for (Vector<blink::WebServiceWorkerState>::iterator iterator = m_queuedState
s.begin(); iterator != m_queuedStates.end(); ++iterator) |
| 147 changeState(*iterator); |
| 148 m_queuedStates.clear(); |
| 149 } |
| 150 |
| 151 void ServiceWorker::waitOnPromise(ScriptPromise promise) |
| 152 { |
| 153 ASSERT(!m_isPromisePending); |
| 154 m_isPromisePending = true; |
| 155 promise.then(ThenFunction::create(this)); |
| 156 } |
| 157 |
| 158 void ServiceWorker::changeState(blink::WebServiceWorkerState state) |
| 159 { |
| 160 m_outerWorker->setState(state); |
| 161 this->dispatchEvent(Event::create(EventTypeNames::statechange)); |
104 } | 162 } |
105 | 163 |
106 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionConte
xt, PassOwnPtr<blink::WebServiceWorker> outerWorker) | 164 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionConte
xt, PassOwnPtr<blink::WebServiceWorker> outerWorker) |
107 { | 165 { |
108 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext,
outerWorker)); | 166 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext,
outerWorker)); |
109 worker->suspendIfNeeded(); | 167 worker->suspendIfNeeded(); |
110 return worker.release(); | 168 return worker.release(); |
111 } | 169 } |
112 | 170 |
113 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blin
k::WebServiceWorker> worker) | 171 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blin
k::WebServiceWorker> worker) |
114 : AbstractWorker(executionContext) | 172 : AbstractWorker(executionContext) |
115 , m_outerWorker(worker) | 173 , m_outerWorker(worker) |
| 174 , m_isPromisePending(false) |
116 { | 175 { |
117 ScriptWrappable::init(this); | 176 ScriptWrappable::init(this); |
118 ASSERT(m_outerWorker); | 177 ASSERT(m_outerWorker); |
119 m_outerWorker->setProxy(this); | 178 m_outerWorker->setProxy(this); |
120 } | 179 } |
121 | 180 |
122 } // namespace WebCore | 181 } // namespace WebCore |
OLD | NEW |