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

Side by Side Diff: webkit/api/src/SharedWorkerRepository.cpp

Issue 340036: Initial pass of shared workers renderer-side code (Closed)
Patch Set: Changes reflecting review feedback Created 11 years, 1 month 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
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 16 matching lines...) Expand all
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 32
33 #if ENABLE(SHARED_WORKERS) 33 #if ENABLE(SHARED_WORKERS)
34 34
35 #include "SharedWorkerRepository.h" 35 #include "SharedWorkerRepository.h"
36 36
37 #include "Event.h"
38 #include "EventNames.h"
37 #include "MessagePortChannel.h" 39 #include "MessagePortChannel.h"
38 #include "SharedWorker.h" 40 #include "SharedWorker.h"
41 #include "PlatformMessagePortChannel.h"
42 #include "WebKit.h"
43 #include "WebKitClient.h"
44 #include "WebMessagePortChannel.h"
45 #include "WebSharedWorker.h"
46 #include "WebSharedWorkerRepository.h"
47 #include "WebString.h"
48 #include "WebURL.h"
49 #include "WorkerScriptLoader.h"
50 #include "WorkerScriptLoaderClient.h"
39 51
40 namespace WebCore { 52 namespace WebCore {
41 53
42 class Document; 54 class Document;
55 using WebKit::WebMessagePortChannel;
56 using WebKit::WebSharedWorker;
57 using WebKit::WebSharedWorkerRepository;
58
59 // Callback class that keeps the Worker object alive while loads are potentially happening, and also translates load errors into error events on the worker.
60 class SharedWorkerScriptLoader : public RefCounted<SharedWorkerScriptLoader>, private WorkerScriptLoaderClient {
61 public:
62 SharedWorkerScriptLoader(PassRefPtr<SharedWorker> worker, PassOwnPtr<MessagePortChannel> port, PassOwnPtr<WebSharedWorker> webWorker)
63 : m_worker(worker),
John Gregg 2009/10/30 19:09:23 nit: webkit style is to line up ',' with ':', and
64 m_webWorker(webWorker),
65 m_port(port)
66 {
67 }
68
69 void load(const KURL&);
70
71 private:
72 // WorkerScriptLoaderClient callback
73 virtual void notifyFinished();
74
75 RefPtr<SharedWorker> m_worker;
76 OwnPtr<WebSharedWorker> m_webWorker;
77 OwnPtr<MessagePortChannel> m_port;
78 WorkerScriptLoader m_scriptLoader;
79 };
80
81 void SharedWorkerScriptLoader::load(const KURL& url)
82 {
83 m_scriptLoader.loadAsynchronously(m_worker->scriptExecutionContext(), url, DenyCrossOriginRequests, this);
84 }
85
86 // Extracts a WebMessagePortChannel from a MessagePortChannel.
87 static WebMessagePortChannel* getWebPort(PassOwnPtr<MessagePortChannel> port) {
John Gregg 2009/10/30 19:09:23 nit: webkit style is '{' on own line for functions
88 // Extract the WebMessagePortChannel to send to the worker.
89 PlatformMessagePortChannel* platformChannel = port->channel();
90 WebMessagePortChannel* webPort = platformChannel->webChannelRelease();
91 webPort->setClient(0);
92 return webPort;
93 }
94
95 void SharedWorkerScriptLoader::notifyFinished()
96 {
97 if (m_scriptLoader.failed())
98 m_worker->dispatchEvent(Event::create(eventNames().errorEvent, false, true));
99 else {
100 m_webWorker->startWorkerContext(m_scriptLoader.url(), m_worker->scriptExecutionContext()->userAgent(m_scriptLoader.url()), m_scriptLoader.script());
101 m_webWorker->connect(getWebPort(m_port.release()));
102 }
103
104 // Connect event has been sent, so free ourselves (this releases the SharedWorker so it can be freed as well if unreferenced).
105 delete this;
106 }
43 107
44 bool SharedWorkerRepository::isAvailable() 108 bool SharedWorkerRepository::isAvailable()
45 { 109 {
46 // SharedWorkers are disabled for now until the implementation is further along. 110 // SharedWorkers are disabled for now until the implementation is further along.
111 // TODO(atwilson): Add code to check for a runtime flag like so:
darin (slow to review) 2009/10/30 18:56:00 this needs to be FIXME per webkit style
112 // return commandLineFlag && WebKit::webKitClient()->sharedWorkerRepository();
47 return false; 113 return false;
48 } 114 }
49 115
50 void SharedWorkerRepository::connect(PassRefPtr<SharedWorker>, PassOwnPtr<MessagePortChannel>, const KURL&, const String&, ExceptionCode&) 116 static WebSharedWorkerRepository::DocumentID getId(void* document)
51 { 117 {
52 // Should not be called because SharedWorkers are disabled. 118 ASSERT(document);
53 ASSERT_NOT_REACHED(); 119 return reinterpret_cast<WebSharedWorkerRepository::DocumentID>(document);
54 } 120 }
55 121
56 void SharedWorkerRepository::documentDetached(Document*) 122 void SharedWorkerRepository::connect(PassRefPtr<SharedWorker> worker, PassOwnPtr<MessagePortChannel> port, const KURL& url, const String& name, ExceptionCode& ec)
57 { 123 {
58 // Does nothing for now (will shutdown shared workers in the future). 124 ScriptExecutionContext* context = worker->scriptExecutionContext();
125 // No nested workers (for now) - connect() should only be called from document context.
126 ASSERT(context->isDocument());
127 OwnPtr<WebSharedWorker> webWorker;
128 ASSERT(WebKit::webKitClient()->sharedWorkerRepository());
129 webWorker = WebKit::webKitClient()->sharedWorkerRepository()->lookup(url, name, getId(context));
130
131 if (!webWorker) {
132 // Existing worker does not match this url, so return an error back to the caller.
133 ec = URL_MISMATCH_ERR;
134 return;
135 }
136
137 if (!webWorker->isStarted()) {
138 // Need to kick off a load for the worker. The loader will connect to the worker once the script has been loaded, then free itself.
139 SharedWorkerScriptLoader* loader = new SharedWorkerScriptLoader(worker, port.release(), webWorker.release());
140 loader->load(url);
141 } else
142 webWorker->connect(getWebPort(port.release()));
59 } 143 }
60 144
61 bool SharedWorkerRepository::hasSharedWorkers(Document*) 145 void SharedWorkerRepository::documentDetached(Document* document)
62 { 146 {
63 // There can be no shared workers until the API is enabled. 147 WebKit::WebSharedWorkerRepository* repo = WebKit::webKitClient()->sharedWorkerRepository();
John Gregg 2009/10/30 19:09:23 you have a using WebKit::WebSharedWorkerRepository
64 return false; 148 if (repo)
149 repo->documentDetached(getId(document));
65 } 150 }
66 151
152 bool SharedWorkerRepository::hasSharedWorkers(Document* document)
153 {
154 WebKit::WebSharedWorkerRepository* repo = WebKit::webKitClient()->sharedWorkerRepository();
John Gregg 2009/10/30 19:09:23 ditto
155 return repo && repo->hasSharedWorkers(getId(document));
156 }
157
158
159
67 } // namespace WebCore 160 } // namespace WebCore
68 161
69 #endif // ENABLE(SHARED_WORKERS) 162 #endif // ENABLE(SHARED_WORKERS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698