Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: Source/web/SharedWorkerRepositoryClientImpl.cpp

Issue 40143003: Simplify SharedWorkerRepository code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/web/SharedWorkerRepositoryClientImpl.h ('k') | Source/web/WebRuntimeFeatures.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 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 11 matching lines...) Expand all
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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 "core/workers/SharedWorkerRepository.h" 32 #include "SharedWorkerRepositoryClientImpl.h"
33 33
34 #include "WebContentSecurityPolicy.h" 34 #include "WebContentSecurityPolicy.h"
35 #include "WebFrameClient.h" 35 #include "WebFrameClient.h"
36 #include "WebFrameImpl.h" 36 #include "WebFrameImpl.h"
37 #include "WebKit.h" 37 #include "WebKit.h"
38 #include "WebSharedWorker.h" 38 #include "WebSharedWorker.h"
39 #include "WebSharedWorkerRepository.h" 39 #include "WebSharedWorkerRepository.h"
40 #include "WebSharedWorkerRepositoryClient.h"
40 #include "bindings/v8/ExceptionMessages.h" 41 #include "bindings/v8/ExceptionMessages.h"
41 #include "bindings/v8/ExceptionState.h" 42 #include "bindings/v8/ExceptionState.h"
42 #include "core/dom/ExceptionCode.h" 43 #include "core/dom/ExceptionCode.h"
43 #include "core/dom/ExecutionContext.h" 44 #include "core/dom/ExecutionContext.h"
44 #include "core/dom/MessagePortChannel.h" 45 #include "core/dom/MessagePortChannel.h"
45 #include "core/events/Event.h" 46 #include "core/events/Event.h"
46 #include "core/events/ThreadLocalEventNames.h" 47 #include "core/events/ThreadLocalEventNames.h"
47 #include "core/frame/ContentSecurityPolicy.h" 48 #include "core/frame/ContentSecurityPolicy.h"
48 #include "core/inspector/InspectorInstrumentation.h" 49 #include "core/inspector/InspectorInstrumentation.h"
49 #include "core/workers/SharedWorker.h" 50 #include "core/workers/SharedWorker.h"
50 #include "core/workers/WorkerScriptLoader.h" 51 #include "core/workers/WorkerScriptLoader.h"
51 #include "core/workers/WorkerScriptLoaderClient.h" 52 #include "core/workers/WorkerScriptLoaderClient.h"
52 #include "platform/network/ResourceResponse.h" 53 #include "platform/network/ResourceResponse.h"
53 #include "public/platform/Platform.h"
54 #include "public/platform/WebMessagePortChannel.h" 54 #include "public/platform/WebMessagePortChannel.h"
55 #include "public/platform/WebString.h" 55 #include "public/platform/WebString.h"
56 #include "public/platform/WebURL.h" 56 #include "public/platform/WebURL.h"
57 57
58 using namespace WebCore;
59
58 namespace WebKit { 60 namespace WebKit {
59 61
62 // FIXME: Deprecate these static methods.
60 WebSharedWorkerRepository* s_sharedWorkerRepository = 0; 63 WebSharedWorkerRepository* s_sharedWorkerRepository = 0;
61 64
62 void setSharedWorkerRepository(WebSharedWorkerRepository* repository) 65 void setSharedWorkerRepository(WebSharedWorkerRepository* repository)
63 { 66 {
64 s_sharedWorkerRepository = repository; 67 s_sharedWorkerRepository = repository;
65 } 68 }
66 69
67 static WebSharedWorkerRepository* sharedWorkerRepository() 70 static WebSharedWorkerRepository* sharedWorkerRepository()
68 { 71 {
69 // Will only be non-zero if the embedder has set the shared worker repositor y upon initialization. Nothing in WebKit sets this. 72 // 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; 73 return s_sharedWorkerRepository;
71 } 74 }
72 75
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. 76 // 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 { 77 class SharedWorkerScriptLoader : private WorkerScriptLoaderClient, private WebSh aredWorker::ConnectListener {
85 public: 78 public:
86 SharedWorkerScriptLoader(PassRefPtr<SharedWorker> worker, const KURL& url, c onst String& name, PassRefPtr<MessagePortChannel> channel, PassOwnPtr<WebSharedW orker> webWorker) 79 SharedWorkerScriptLoader(PassRefPtr<SharedWorker> worker, const KURL& url, c onst String& name, PassRefPtr<MessagePortChannel> channel, PassOwnPtr<WebSharedW orker> webWorker)
87 : m_worker(worker) 80 : m_worker(worker)
88 , m_url(url) 81 , m_url(url)
89 , m_name(name) 82 , m_name(name)
90 , m_webWorker(webWorker) 83 , m_webWorker(webWorker)
91 , m_channel(channel) 84 , m_channel(channel)
92 , m_scriptLoader(WorkerScriptLoader::create()) 85 , m_scriptLoader(WorkerScriptLoader::create())
93 , m_loading(false) 86 , m_loading(false)
94 , m_responseAppCacheID(0) 87 , m_responseAppCacheID(0)
95 { 88 {
96 m_scriptLoader->setTargetType(ResourceRequest::TargetIsSharedWorker); 89 m_scriptLoader->setTargetType(ResourceRequest::TargetIsSharedWorker);
97 } 90 }
98 91
99 ~SharedWorkerScriptLoader(); 92 ~SharedWorkerScriptLoader();
100 void load(); 93 void load();
101 static void stopAllLoadersForContext(ExecutionContext*);
102 94
103 private: 95 private:
104 // WorkerScriptLoaderClient callbacks 96 // WorkerScriptLoaderClient callbacks
105 virtual void didReceiveResponse(unsigned long identifier, const ResourceResp onse&); 97 virtual void didReceiveResponse(unsigned long identifier, const ResourceResp onse&);
106 virtual void notifyFinished(); 98 virtual void notifyFinished();
107
108 virtual void connected(); 99 virtual void connected();
109 100
110 const ExecutionContext* loadingContext() { return m_worker->executionContext (); }
111
112 void sendConnect(); 101 void sendConnect();
113 102
114 RefPtr<SharedWorker> m_worker; 103 RefPtr<SharedWorker> m_worker;
115 KURL m_url; 104 KURL m_url;
116 String m_name; 105 String m_name;
117 OwnPtr<WebSharedWorker> m_webWorker; 106 OwnPtr<WebSharedWorker> m_webWorker;
118 RefPtr<MessagePortChannel> m_channel; 107 RefPtr<MessagePortChannel> m_channel;
119 RefPtr<WorkerScriptLoader> m_scriptLoader; 108 RefPtr<WorkerScriptLoader> m_scriptLoader;
120 bool m_loading; 109 bool m_loading;
121 long long m_responseAppCacheID; 110 long long m_responseAppCacheID;
122 }; 111 };
123 112
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() 113 SharedWorkerScriptLoader::~SharedWorkerScriptLoader()
145 { 114 {
146 if (m_loading) 115 if (m_loading)
147 m_worker->unsetPendingActivity(m_worker.get()); 116 m_worker->unsetPendingActivity(m_worker.get());
148 } 117 }
149 118
150 void SharedWorkerScriptLoader::load() 119 void SharedWorkerScriptLoader::load()
151 { 120 {
152 ASSERT(!m_loading); 121 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. 122 // 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()) 123 if (m_webWorker->isStarted()) {
155 sendConnect(); 124 sendConnect();
156 else { 125 } else {
157 // Keep the worker + JS wrapper alive until the resource load is complet e in case we need to dispatch an error event. 126 // 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()); 127 m_worker->setPendingActivity(m_worker.get());
159 m_loading = true; 128 m_loading = true;
160 129
161 m_scriptLoader->loadAsynchronously(m_worker->executionContext(), m_url, DenyCrossOriginRequests, this); 130 m_scriptLoader->loadAsynchronously(m_worker->executionContext(), m_url, DenyCrossOriginRequests, this);
162 } 131 }
163 } 132 }
164 133
165 void SharedWorkerScriptLoader::didReceiveResponse(unsigned long identifier, cons t ResourceResponse& response) 134 void SharedWorkerScriptLoader::didReceiveResponse(unsigned long identifier, cons t ResourceResponse& response)
166 { 135 {
(...skipping 21 matching lines...) Expand all
188 // Send the connect event off, and linger until it is done sending. 157 // Send the connect event off, and linger until it is done sending.
189 m_webWorker->connect(webChannel, this); 158 m_webWorker->connect(webChannel, this);
190 } 159 }
191 160
192 void SharedWorkerScriptLoader::connected() 161 void SharedWorkerScriptLoader::connected()
193 { 162 {
194 // Connect event has been sent, so free ourselves (this releases the SharedW orker so it can be freed as well if unreferenced). 163 // 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; 164 delete this;
196 } 165 }
197 166
198 bool SharedWorkerRepository::isAvailable()
199 {
200 return WebKit::sharedWorkerRepository();
201 }
202
203 static WebSharedWorkerRepository::DocumentID getId(void* document) 167 static WebSharedWorkerRepository::DocumentID getId(void* document)
204 { 168 {
205 ASSERT(document); 169 ASSERT(document);
206 return reinterpret_cast<WebSharedWorkerRepository::DocumentID>(document); 170 return reinterpret_cast<WebSharedWorkerRepository::DocumentID>(document);
207 } 171 }
208 172
209 void SharedWorkerRepository::connect(PassRefPtr<SharedWorker> worker, PassRefPtr <MessagePortChannel> port, const KURL& url, const String& name, ExceptionState& es) 173 void SharedWorkerRepositoryClientImpl::connect(PassRefPtr<SharedWorker> worker, PassRefPtr<MessagePortChannel> port, const KURL& url, const String& name, Except ionState& es)
210 { 174 {
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. 175 // No nested workers (for now) - connect() should only be called from docume nt context.
218 ASSERT(worker->executionContext()->isDocument()); 176 ASSERT(worker->executionContext()->isDocument());
219 Document* document = toDocument(worker->executionContext()); 177 Document* document = toDocument(worker->executionContext());
220 WebFrameImpl* webFrame = WebFrameImpl::fromFrame(document->frame()); 178 WebFrameImpl* webFrame = WebFrameImpl::fromFrame(document->frame());
221 OwnPtr<WebSharedWorker> webWorker; 179 OwnPtr<WebSharedWorker> webWorker;
222 webWorker = adoptPtr(webFrame->client()->createSharedWorker(webFrame, url, n ame, getId(document))); 180 if (m_client) {
181 webWorker = adoptPtr(m_client->createSharedWorker(url, name, getId(docum ent)));
182 } else {
183 // FIXME: Deprecate this once client is implemented.
184 webWorker = adoptPtr(webFrame->client()->createSharedWorker(webFrame, ur l, name, getId(document)));
185 }
223 186
224 if (!webWorker) { 187 if (!webWorker) {
225 // Existing worker does not match this url, so return an error back to t he caller. 188 // 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() + "').")); 189 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; 190 return;
228 } 191 }
229 192
230 repository->addSharedWorker(webWorker.get(), getId(document)); 193 WebKit::WebSharedWorkerRepository* repository = WebKit::sharedWorkerReposito ry();
194 if (repository)
195 repository->addSharedWorker(webWorker.get(), getId(document));
231 196
232 // The loader object manages its own lifecycle (and the lifecycles of the tw o worker objects). 197 // 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. 198 // It will free itself once loading is completed.
234 SharedWorkerScriptLoader* loader = new SharedWorkerScriptLoader(worker, url, name, port, webWorker.release()); 199 SharedWorkerScriptLoader* loader = new SharedWorkerScriptLoader(worker, url, name, port, webWorker.release());
235 loader->load(); 200 loader->load();
236 } 201 }
237 202
238 void SharedWorkerRepository::documentDetached(Document* document) 203 void SharedWorkerRepositoryClientImpl::documentDetached(Document* document)
239 { 204 {
240 WebKit::WebSharedWorkerRepository* repository = WebKit::sharedWorkerReposito ry(); 205 if (m_client) {
206 m_client->documentDetached(getId(document));
207 return;
208 }
241 209
210 // FIXME: Deprecate this once client is implemented.
211 WebSharedWorkerRepository* repository = sharedWorkerRepository();
242 if (repository) 212 if (repository)
243 repository->documentDetached(getId(document)); 213 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 } 214 }
249 215
250 bool SharedWorkerRepository::hasSharedWorkers(Document* document) 216 SharedWorkerRepositoryClientImpl::SharedWorkerRepositoryClientImpl(WebSharedWork erRepositoryClient* client)
217 : m_client(client)
251 { 218 {
252 WebKit::WebSharedWorkerRepository* repository = WebKit::sharedWorkerReposito ry();
253
254 return repository && repository->hasSharedWorkers(getId(document));
255 } 219 }
256 220
257 } // namespace WebCore 221 } // namespace WebKit
OLDNEW
« no previous file with comments | « Source/web/SharedWorkerRepositoryClientImpl.h ('k') | Source/web/WebRuntimeFeatures.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698