OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2009 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 "core/workers/SharedWorkerRepository.h" | |
33 | |
34 #include "WebContentSecurityPolicy.h" | |
35 #include "WebFrameClient.h" | |
36 #include "WebFrameImpl.h" | |
37 #include "WebKit.h" | |
38 #include "WebSharedWorker.h" | |
39 #include "WebSharedWorkerRepository.h" | |
40 #include "bindings/v8/ExceptionMessages.h" | |
41 #include "bindings/v8/ExceptionState.h" | |
42 #include "core/dom/ExceptionCode.h" | |
43 #include "core/dom/ExecutionContext.h" | |
44 #include "core/dom/MessagePortChannel.h" | |
45 #include "core/events/Event.h" | |
46 #include "core/events/ThreadLocalEventNames.h" | |
47 #include "core/frame/ContentSecurityPolicy.h" | |
48 #include "core/inspector/InspectorInstrumentation.h" | |
49 #include "core/workers/SharedWorker.h" | |
50 #include "core/workers/WorkerScriptLoader.h" | |
51 #include "core/workers/WorkerScriptLoaderClient.h" | |
52 #include "platform/network/ResourceResponse.h" | |
53 #include "public/platform/Platform.h" | |
54 #include "public/platform/WebMessagePortChannel.h" | |
55 #include "public/platform/WebString.h" | |
56 #include "public/platform/WebURL.h" | |
57 | |
58 namespace WebKit { | |
59 | |
60 WebSharedWorkerRepository* s_sharedWorkerRepository = 0; | |
61 | |
62 void setSharedWorkerRepository(WebSharedWorkerRepository* repository) | |
63 { | |
64 s_sharedWorkerRepository = repository; | |
65 } | |
66 | |
67 static WebSharedWorkerRepository* sharedWorkerRepository() | |
68 { | |
69 // Will only be non-zero if the embedder has set the shared worker repositor
y upon initialization. Nothing in WebKit sets this. | |
70 return s_sharedWorkerRepository; | |
71 } | |
72 | |
73 } | |
74 | |
75 namespace WebCore { | |
76 | |
77 class Document; | |
78 using WebKit::WebFrameImpl; | |
79 using WebKit::WebMessagePortChannel; | |
80 using WebKit::WebSharedWorker; | |
81 using WebKit::WebSharedWorkerRepository; | |
82 | |
83 // Callback class that keeps the SharedWorker and WebSharedWorker objects alive
while loads are potentially happening, and also translates load errors into erro
r events on the worker. | |
84 class SharedWorkerScriptLoader : private WorkerScriptLoaderClient, private WebSh
aredWorker::ConnectListener { | |
85 public: | |
86 SharedWorkerScriptLoader(PassRefPtr<SharedWorker> worker, const KURL& url, c
onst String& name, PassRefPtr<MessagePortChannel> channel, PassOwnPtr<WebSharedW
orker> webWorker) | |
87 : m_worker(worker) | |
88 , m_url(url) | |
89 , m_name(name) | |
90 , m_webWorker(webWorker) | |
91 , m_channel(channel) | |
92 , m_scriptLoader(WorkerScriptLoader::create()) | |
93 , m_loading(false) | |
94 , m_responseAppCacheID(0) | |
95 { | |
96 m_scriptLoader->setTargetType(ResourceRequest::TargetIsSharedWorker); | |
97 } | |
98 | |
99 ~SharedWorkerScriptLoader(); | |
100 void load(); | |
101 static void stopAllLoadersForContext(ExecutionContext*); | |
102 | |
103 private: | |
104 // WorkerScriptLoaderClient callbacks | |
105 virtual void didReceiveResponse(unsigned long identifier, const ResourceResp
onse&); | |
106 virtual void notifyFinished(); | |
107 | |
108 virtual void connected(); | |
109 | |
110 const ExecutionContext* loadingContext() { return m_worker->executionContext
(); } | |
111 | |
112 void sendConnect(); | |
113 | |
114 RefPtr<SharedWorker> m_worker; | |
115 KURL m_url; | |
116 String m_name; | |
117 OwnPtr<WebSharedWorker> m_webWorker; | |
118 RefPtr<MessagePortChannel> m_channel; | |
119 RefPtr<WorkerScriptLoader> m_scriptLoader; | |
120 bool m_loading; | |
121 long long m_responseAppCacheID; | |
122 }; | |
123 | |
124 static Vector<SharedWorkerScriptLoader*>& pendingLoaders() | |
125 { | |
126 AtomicallyInitializedStatic(Vector<SharedWorkerScriptLoader*>&, loaders = *n
ew Vector<SharedWorkerScriptLoader*>); | |
127 return loaders; | |
128 } | |
129 | |
130 void SharedWorkerScriptLoader::stopAllLoadersForContext(ExecutionContext* contex
t) | |
131 { | |
132 // Walk our list of pending loaders and shutdown any that belong to this con
text. | |
133 Vector<SharedWorkerScriptLoader*>& loaders = pendingLoaders(); | |
134 for (unsigned i = 0; i < loaders.size(); ) { | |
135 SharedWorkerScriptLoader* loader = loaders[i]; | |
136 if (context == loader->loadingContext()) { | |
137 loaders.remove(i); | |
138 delete loader; | |
139 } else | |
140 i++; | |
141 } | |
142 } | |
143 | |
144 SharedWorkerScriptLoader::~SharedWorkerScriptLoader() | |
145 { | |
146 if (m_loading) | |
147 m_worker->unsetPendingActivity(m_worker.get()); | |
148 } | |
149 | |
150 void SharedWorkerScriptLoader::load() | |
151 { | |
152 ASSERT(!m_loading); | |
153 // If the shared worker is not yet running, load the script resource for it,
otherwise just send it a connect event. | |
154 if (m_webWorker->isStarted()) | |
155 sendConnect(); | |
156 else { | |
157 // Keep the worker + JS wrapper alive until the resource load is complet
e in case we need to dispatch an error event. | |
158 m_worker->setPendingActivity(m_worker.get()); | |
159 m_loading = true; | |
160 | |
161 m_scriptLoader->loadAsynchronously(m_worker->executionContext(), m_url,
DenyCrossOriginRequests, this); | |
162 } | |
163 } | |
164 | |
165 void SharedWorkerScriptLoader::didReceiveResponse(unsigned long identifier, cons
t ResourceResponse& response) | |
166 { | |
167 m_responseAppCacheID = response.appCacheID(); | |
168 InspectorInstrumentation::didReceiveScriptResponse(m_worker->executionContex
t(), identifier); | |
169 } | |
170 | |
171 void SharedWorkerScriptLoader::notifyFinished() | |
172 { | |
173 if (m_scriptLoader->failed()) { | |
174 m_worker->dispatchEvent(Event::createCancelable(EventTypeNames::error)); | |
175 delete this; | |
176 } else { | |
177 InspectorInstrumentation::scriptImported(m_worker->executionContext(), m
_scriptLoader->identifier(), m_scriptLoader->script()); | |
178 // Pass the script off to the worker, then send a connect event. | |
179 m_webWorker->startWorkerContext(m_url, m_name, m_worker->executionContex
t()->userAgent(m_url), m_scriptLoader->script(), m_worker->executionContext()->c
ontentSecurityPolicy()->deprecatedHeader(), static_cast<WebKit::WebContentSecuri
tyPolicyType>(m_worker->executionContext()->contentSecurityPolicy()->deprecatedH
eaderType()), m_responseAppCacheID); | |
180 sendConnect(); | |
181 } | |
182 } | |
183 | |
184 void SharedWorkerScriptLoader::sendConnect() | |
185 { | |
186 WebMessagePortChannel* webChannel = m_channel->webChannelRelease(); | |
187 m_channel.clear(); | |
188 // Send the connect event off, and linger until it is done sending. | |
189 m_webWorker->connect(webChannel, this); | |
190 } | |
191 | |
192 void SharedWorkerScriptLoader::connected() | |
193 { | |
194 // Connect event has been sent, so free ourselves (this releases the SharedW
orker so it can be freed as well if unreferenced). | |
195 delete this; | |
196 } | |
197 | |
198 bool SharedWorkerRepository::isAvailable() | |
199 { | |
200 return WebKit::sharedWorkerRepository(); | |
201 } | |
202 | |
203 static WebSharedWorkerRepository::DocumentID getId(void* document) | |
204 { | |
205 ASSERT(document); | |
206 return reinterpret_cast<WebSharedWorkerRepository::DocumentID>(document); | |
207 } | |
208 | |
209 void SharedWorkerRepository::connect(PassRefPtr<SharedWorker> worker, PassRefPtr
<MessagePortChannel> port, const KURL& url, const String& name, ExceptionState&
es) | |
210 { | |
211 WebKit::WebSharedWorkerRepository* repository = WebKit::sharedWorkerReposito
ry(); | |
212 | |
213 // This should not be callable unless there's a SharedWorkerRepository for | |
214 // this context (since isAvailable() should have returned null). | |
215 ASSERT(repository); | |
216 | |
217 // No nested workers (for now) - connect() should only be called from docume
nt context. | |
218 ASSERT(worker->executionContext()->isDocument()); | |
219 Document* document = toDocument(worker->executionContext()); | |
220 WebFrameImpl* webFrame = WebFrameImpl::fromFrame(document->frame()); | |
221 OwnPtr<WebSharedWorker> webWorker; | |
222 webWorker = adoptPtr(webFrame->client()->createSharedWorker(webFrame, url, n
ame, getId(document))); | |
223 | |
224 if (!webWorker) { | |
225 // Existing worker does not match this url, so return an error back to t
he caller. | |
226 es.throwDOMException(URLMismatchError, ExceptionMessages::failedToConstr
uct("SharedWorker", "The location of the SharedWorker named '" + name + "' does
not exactly match the provided URL ('" + url.elidedString() + "').")); | |
227 return; | |
228 } | |
229 | |
230 repository->addSharedWorker(webWorker.get(), getId(document)); | |
231 | |
232 // The loader object manages its own lifecycle (and the lifecycles of the tw
o worker objects). | |
233 // It will free itself once loading is completed. | |
234 SharedWorkerScriptLoader* loader = new SharedWorkerScriptLoader(worker, url,
name, port, webWorker.release()); | |
235 loader->load(); | |
236 } | |
237 | |
238 void SharedWorkerRepository::documentDetached(Document* document) | |
239 { | |
240 WebKit::WebSharedWorkerRepository* repository = WebKit::sharedWorkerReposito
ry(); | |
241 | |
242 if (repository) | |
243 repository->documentDetached(getId(document)); | |
244 | |
245 // Stop the creation of any pending SharedWorkers for this context. | |
246 // FIXME: Need a way to invoke this for WorkerGlobalScopes as well when we s
upport for nested workers. | |
247 SharedWorkerScriptLoader::stopAllLoadersForContext(document); | |
248 } | |
249 | |
250 bool SharedWorkerRepository::hasSharedWorkers(Document* document) | |
251 { | |
252 WebKit::WebSharedWorkerRepository* repository = WebKit::sharedWorkerReposito
ry(); | |
253 | |
254 return repository && repository->hasSharedWorkers(getId(document)); | |
255 } | |
256 | |
257 } // namespace WebCore | |
OLD | NEW |