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 | |
33 #if ENABLE(SHARED_WORKERS) | |
34 | |
35 #include "SharedWorkerRepository.h" | |
36 | |
37 #include "Event.h" | |
38 #include "EventNames.h" | |
39 #include "MessagePortChannel.h" | |
40 #include "PlatformMessagePortChannel.h" | |
41 #include "ScriptExecutionContext.h" | |
42 #include "SharedWorker.h" | |
43 #include "WebFrameClient.h" | |
44 #include "WebFrameImpl.h" | |
45 #include "WebKit.h" | |
46 #include "WebKitClient.h" | |
47 #include "WebMessagePortChannel.h" | |
48 #include "WebSharedWorker.h" | |
49 #include "WebSharedWorkerRepository.h" | |
50 #include "WebString.h" | |
51 #include "WebURL.h" | |
52 #include "WorkerScriptLoader.h" | |
53 #include "WorkerScriptLoaderClient.h" | |
54 | |
55 namespace WebCore { | |
56 | |
57 class Document; | |
58 using WebKit::WebFrameImpl; | |
59 using WebKit::WebMessagePortChannel; | |
60 using WebKit::WebSharedWorker; | |
61 using WebKit::WebSharedWorkerRepository; | |
62 | |
63 // Callback class that keeps the SharedWorker and WebSharedWorker objects alive while loads are potentially happening, and also translates load errors into error events on the worker. | |
64 class SharedWorkerScriptLoader : private WorkerScriptLoaderClient, private WebSharedWorker::ConnectListener, private ActiveDOMObject { | |
65 public: | |
66 SharedWorkerScriptLoader(PassRefPtr<SharedWorker> worker, const KURL& url, const String& name, PassOwnPtr<MessagePortChannel> port, PassOwnPtr<WebSharedWorker> webWorker) | |
67 : ActiveDOMObject(worker->scriptExecutionContext(), this) | |
68 , m_worker(worker) | |
69 , m_url(url) | |
70 , m_name(name) | |
71 , m_webWorker(webWorker) | |
72 , m_port(port) | |
73 { | |
74 } | |
75 | |
76 void load(); | |
77 virtual void contextDestroyed(); | |
78 private: | |
79 // WorkerScriptLoaderClient callback | |
80 virtual void notifyFinished(); | |
81 | |
82 virtual void connected(); | |
83 | |
84 void sendConnect(); | |
85 | |
86 RefPtr<SharedWorker> m_worker; | |
87 KURL m_url; | |
88 String m_name; | |
89 OwnPtr<WebSharedWorker> m_webWorker; | |
90 OwnPtr<MessagePortChannel> m_port; | |
91 WorkerScriptLoader m_scriptLoader; | |
92 }; | |
93 | |
94 void SharedWorkerScriptLoader::load() | |
95 { | |
96 // If the shared worker is not yet running, load the script resource for it, otherwise just send it a connect event. | |
97 if (m_webWorker->isStarted()) | |
98 sendConnect(); | |
99 else | |
100 m_scriptLoader.loadAsynchronously(m_worker->scriptExecutionContext(), m_url, DenyCrossOriginRequests, this); | |
101 } | |
102 | |
103 // Extracts a WebMessagePortChannel from a MessagePortChannel. | |
104 static WebMessagePortChannel* getWebPort(PassOwnPtr<MessagePortChannel> port) | |
105 { | |
106 // Extract the WebMessagePortChannel to send to the worker. | |
107 PlatformMessagePortChannel* platformChannel = port->channel(); | |
108 WebMessagePortChannel* webPort = platformChannel->webChannelRelease(); | |
109 webPort->setClient(0); | |
110 return webPort; | |
111 } | |
112 | |
113 void SharedWorkerScriptLoader::notifyFinished() | |
114 { | |
115 if (m_scriptLoader.failed()) { | |
116 m_worker->dispatchEvent(Event::create(eventNames().errorEvent, false, true)); | |
117 delete this; | |
118 } else { | |
119 // Pass the script off to the worker, then send a connect event. | |
120 m_webWorker->startWorkerContext(m_url, m_name, m_worker->scriptExecutionContext()->userAgent(m_url), m_scriptLoader.script()); | |
121 sendConnect(); | |
122 } | |
123 } | |
124 | |
125 void SharedWorkerScriptLoader::sendConnect() | |
126 { | |
127 // Send the connect event off, and linger until it is done sending. | |
128 m_webWorker->connect(getWebPort(m_port.release()), this); | |
129 } | |
130 | |
131 void SharedWorkerScriptLoader::contextDestroyed() | |
132 { | |
133 ActiveDOMObject::contextDestroyed(); | |
134 delete this; | |
135 } | |
136 | |
137 void SharedWorkerScriptLoader::connected() | |
138 { | |
139 // Connect event has been sent, so free ourselves (this releases the SharedWorker so it can be freed as well if unreferenced). | |
140 delete this; | |
141 } | |
142 | |
143 bool SharedWorkerRepository::isAvailable() | |
144 { | |
145 // Allow the WebKitClient to determine if SharedWorkers are available. | |
146 return WebKit::webKitClient()->sharedWorkerRepository(); | |
147 } | |
148 | |
149 static WebSharedWorkerRepository::DocumentID getId(void* document) | |
150 { | |
151 ASSERT(document); | |
152 return reinterpret_cast<WebSharedWorkerRepository::DocumentID>(document); | |
153 } | |
154 | |
155 void SharedWorkerRepository::connect(PassRefPtr<SharedWorker> worker, PassOwnPtr<MessagePortChannel> port, const KURL& url, const String& name, ExceptionCode& ec) | |
156 { | |
157 // This should not be callable unless there's a SharedWorkerRepository for | |
158 // this context (since isAvailable() should have returned null). | |
159 ASSERT(WebKit::webKitClient()->sharedWorkerRepository()); | |
160 | |
161 // No nested workers (for now) - connect() should only be called from document context. | |
162 ASSERT(worker->scriptExecutionContext()->isDocument()); | |
163 Document* document = static_cast<Document*>(worker->scriptExecutionContext()); | |
164 WebFrameImpl* webFrame = WebFrameImpl::fromFrame(document->frame()); | |
165 OwnPtr<WebSharedWorker> webWorker; | |
166 webWorker = webFrame->client()->createSharedWorker(webFrame, url, name, getId(document)); | |
167 | |
168 if (!webWorker) { | |
169 // Existing worker does not match this url, so return an error back to the caller. | |
170 ec = URL_MISMATCH_ERR; | |
171 return; | |
172 } | |
173 | |
174 // The loader object manages its own lifecycle (and the lifecycles of the two worker objects). | |
175 // It will free itself once loading is completed. | |
176 SharedWorkerScriptLoader* loader = new SharedWorkerScriptLoader(worker, url, name, port.release(), webWorker.release()); | |
177 loader->load(); | |
178 } | |
179 | |
180 void SharedWorkerRepository::documentDetached(Document* document) | |
181 { | |
182 WebSharedWorkerRepository* repo = WebKit::webKitClient()->sharedWorkerRepository(); | |
183 if (repo) | |
184 repo->documentDetached(getId(document)); | |
185 } | |
186 | |
187 bool SharedWorkerRepository::hasSharedWorkers(Document* document) | |
188 { | |
189 WebSharedWorkerRepository* repo = WebKit::webKitClient()->sharedWorkerRepository(); | |
190 return repo && repo->hasSharedWorkers(getId(document)); | |
191 } | |
192 | |
193 | |
194 | |
195 } // namespace WebCore | |
196 | |
197 #endif // ENABLE(SHARED_WORKERS) | |
OLD | NEW |