2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
32 #include "ServiceWorker.h"
34 #include "EventTargetNames.h"
35 #include "bindings/v8/ExceptionState.h"
36 #include "bindings/v8/ScriptPromiseResolverWithContext.h"
37 #include "bindings/v8/ScriptState.h"
38 #include "core/dom/MessagePort.h"
39 #include "core/events/Event.h"
40 #include "platform/NotImplemented.h"
41 #include "public/platform/WebMessagePortChannel.h"
42 #include "public/platform/WebServiceWorkerState.h"
43 #include "public/platform/WebString.h"
47 class ServiceWorker::ThenFunction FINAL : public ScriptFunction {
49 static PassOwnPtr<ScriptFunction> create(PassRefPtr<ServiceWorker> observer)
51 ExecutionContext* executionContext = observer->executionContext();
52 return adoptPtr(new ThenFunction(toIsolate(executionContext), observer));
55 ThenFunction(v8::Isolate* isolate, PassRefPtr<ServiceWorker> observer)
56 : ScriptFunction(isolate)
57 , m_observer(observer)
61 virtual ScriptValue call(ScriptValue value) OVERRIDE
63 m_observer->onPromiseResolved();
67 RefPtr<ServiceWorker> m_observer;
70 const AtomicString& ServiceWorker::interfaceName() const
72 return EventTargetNames::ServiceWorker;
75 void ServiceWorker::postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exceptionState)
77 // Disentangle the port in preparation for sending it to the remote context.
78 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(ports, exceptionState);
79 if (exceptionState.hadException())
82 blink::WebString messageString = message->toWireString();
83 OwnPtr<blink::WebMessagePortChannelArray> webChannels = MessagePort::toWebMessagePortChannelArray(channels.release());
84 m_outerWorker->postMessage(messageString, webChannels.leakPtr());
87 bool ServiceWorker::isReady()
89 return !m_isPromisePending;
92 void ServiceWorker::dispatchStateChangeEvent()
95 this->dispatchEvent(Event::create(EventTypeNames::statechange));
98 const AtomicString& ServiceWorker::state() const
100 DEFINE_STATIC_LOCAL(AtomicString, unknown, ("unknown", AtomicString::ConstructFromLiteral));
101 DEFINE_STATIC_LOCAL(AtomicString, parsed, ("parsed", AtomicString::ConstructFromLiteral));
102 DEFINE_STATIC_LOCAL(AtomicString, installing, ("installing", AtomicString::ConstructFromLiteral));
103 DEFINE_STATIC_LOCAL(AtomicString, installed, ("installed", AtomicString::ConstructFromLiteral));
104 DEFINE_STATIC_LOCAL(AtomicString, activating, ("activating", AtomicString::ConstructFromLiteral));
105 DEFINE_STATIC_LOCAL(AtomicString, active, ("active", AtomicString::ConstructFromLiteral));
106 DEFINE_STATIC_LOCAL(AtomicString, deactivated, ("deactivated", AtomicString::ConstructFromLiteral));
108 switch (m_outerWorker->state()) {
109 case blink::WebServiceWorkerStateUnknown:
110 // The web platform should never see this internal state
111 ASSERT_NOT_REACHED();
113 case blink::WebServiceWorkerStateParsed:
115 case blink::WebServiceWorkerStateInstalling:
117 case blink::WebServiceWorkerStateInstalled:
119 case blink::WebServiceWorkerStateActivating:
121 case blink::WebServiceWorkerStateActive:
123 case blink::WebServiceWorkerStateDeactivated:
126 ASSERT_NOT_REACHED();
131 PassRefPtr<ServiceWorker> ServiceWorker::from(ScriptPromiseResolverWithContext* resolver, WebType* worker)
133 ScriptState::Scope scope(resolver->scriptState());
134 RefPtr<ServiceWorker> serviceWorker = create(resolver->scriptState()->executionContext(), adoptPtr(worker));
135 serviceWorker->waitOnPromise(resolver->promise());
136 return serviceWorker;
139 void ServiceWorker::onPromiseResolved()
141 ASSERT(m_isPromisePending);
142 m_isPromisePending = false;
143 m_outerWorker->proxyReadyChanged();
146 void ServiceWorker::waitOnPromise(ScriptPromise promise)
148 ASSERT(!m_isPromisePending);
149 m_isPromisePending = true;
150 m_outerWorker->proxyReadyChanged();
151 promise.then(ThenFunction::create(this));
154 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionContext, PassOwnPtr<blink::WebServiceWorker> outerWorker)
156 RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext, outerWorker));
157 worker->suspendIfNeeded();
158 return worker.release();
161 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blink::WebServiceWorker> worker)
162 : AbstractWorker(executionContext)
163 , m_outerWorker(worker)
164 , m_isPromisePending(false)
166 ScriptWrappable::init(this);
167 ASSERT(m_outerWorker);
168 m_outerWorker->setProxy(this);
171 } // namespace WebCore