Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / serviceworkers / ServiceWorker.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "ServiceWorker.h"
33
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"
44
45 namespace WebCore {
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
70 const AtomicString& ServiceWorker::interfaceName() const
71 {
72     return EventTargetNames::ServiceWorker;
73 }
74
75 void ServiceWorker::postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exceptionState)
76 {
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())
80         return;
81
82     blink::WebString messageString = message->toWireString();
83     OwnPtr<blink::WebMessagePortChannelArray> webChannels = MessagePort::toWebMessagePortChannelArray(channels.release());
84     m_outerWorker->postMessage(messageString, webChannels.leakPtr());
85 }
86
87 bool ServiceWorker::isReady()
88 {
89     return !m_isPromisePending;
90 }
91
92 void ServiceWorker::dispatchStateChangeEvent()
93 {
94     ASSERT(isReady());
95     this->dispatchEvent(Event::create(EventTypeNames::statechange));
96 }
97
98 const AtomicString& ServiceWorker::state() const
99 {
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));
107
108     switch (m_outerWorker->state()) {
109     case blink::WebServiceWorkerStateUnknown:
110         // The web platform should never see this internal state
111         ASSERT_NOT_REACHED();
112         return unknown;
113     case blink::WebServiceWorkerStateParsed:
114         return parsed;
115     case blink::WebServiceWorkerStateInstalling:
116         return installing;
117     case blink::WebServiceWorkerStateInstalled:
118         return installed;
119     case blink::WebServiceWorkerStateActivating:
120         return activating;
121     case blink::WebServiceWorkerStateActive:
122         return active;
123     case blink::WebServiceWorkerStateDeactivated:
124         return deactivated;
125     default:
126         ASSERT_NOT_REACHED();
127         return nullAtom;
128     }
129 }
130
131 PassRefPtr<ServiceWorker> ServiceWorker::from(ScriptPromiseResolverWithContext* resolver, WebType* worker)
132 {
133     ScriptState::Scope scope(resolver->scriptState());
134     RefPtr<ServiceWorker> serviceWorker = create(resolver->scriptState()->executionContext(), adoptPtr(worker));
135     serviceWorker->waitOnPromise(resolver->promise());
136     return serviceWorker;
137 }
138
139 void ServiceWorker::onPromiseResolved()
140 {
141     ASSERT(m_isPromisePending);
142     m_isPromisePending = false;
143     m_outerWorker->proxyReadyChanged();
144 }
145
146 void ServiceWorker::waitOnPromise(ScriptPromise promise)
147 {
148     ASSERT(!m_isPromisePending);
149     m_isPromisePending = true;
150     m_outerWorker->proxyReadyChanged();
151     promise.then(ThenFunction::create(this));
152 }
153
154 PassRefPtr<ServiceWorker> ServiceWorker::create(ExecutionContext* executionContext, PassOwnPtr<blink::WebServiceWorker> outerWorker)
155 {
156     RefPtr<ServiceWorker> worker = adoptRef(new ServiceWorker(executionContext, outerWorker));
157     worker->suspendIfNeeded();
158     return worker.release();
159 }
160
161 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<blink::WebServiceWorker> worker)
162     : AbstractWorker(executionContext)
163     , m_outerWorker(worker)
164     , m_isPromisePending(false)
165 {
166     ScriptWrappable::init(this);
167     ASSERT(m_outerWorker);
168     m_outerWorker->setProxy(this);
169 }
170
171 } // namespace WebCore