OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/worker/websharedworker_stub.h" | 5 #include "content/worker/websharedworker_stub.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
8 #include "content/child/child_process.h" | 9 #include "content/child/child_process.h" |
9 #include "content/child/child_thread.h" | 10 #include "content/child/child_thread.h" |
10 #include "content/child/fileapi/file_system_dispatcher.h" | 11 #include "content/child/fileapi/file_system_dispatcher.h" |
11 #include "content/child/webmessageportchannel_impl.h" | 12 #include "content/child/webmessageportchannel_impl.h" |
12 #include "content/common/worker_messages.h" | 13 #include "content/common/worker_messages.h" |
13 #include "content/worker/shared_worker_devtools_agent.h" | 14 #include "content/worker/shared_worker_devtools_agent.h" |
14 #include "content/worker/worker_thread.h" | 15 #include "content/worker/worker_thread.h" |
15 #include "third_party/WebKit/public/web/WebSharedWorker.h" | 16 #include "third_party/WebKit/public/web/WebSharedWorker.h" |
| 17 #include "third_party/WebKit/public/platform/WebCString.h" |
16 #include "third_party/WebKit/public/platform/WebString.h" | 18 #include "third_party/WebKit/public/platform/WebString.h" |
17 #include "third_party/WebKit/public/platform/WebURL.h" | 19 #include "third_party/WebKit/public/platform/WebURL.h" |
| 20 #include "content/common/appcache_messages.h" |
| 21 |
| 22 using WebKit::WebController; |
18 | 23 |
19 namespace content { | 24 namespace content { |
20 | 25 |
21 WebSharedWorkerStub::WebSharedWorkerStub( | 26 WebSharedWorkerStub::WebSharedWorkerStub( |
22 const string16& name, | 27 const string16& name, |
23 int route_id, | 28 int route_id, |
24 const WorkerAppCacheInitInfo& appcache_init_info) | 29 const WorkerAppCacheInitInfo& appcache_init_info) |
25 : route_id_(route_id), | 30 : route_id_(route_id), |
26 appcache_init_info_(appcache_init_info), | 31 appcache_init_info_(appcache_init_info), |
27 client_(route_id, this), | 32 client_(route_id, this), |
28 name_(name), | 33 name_(name), |
29 started_(false) { | 34 started_(false), |
| 35 is_embedded_worker_(false) { |
30 | 36 |
31 WorkerThread* worker_thread = WorkerThread::current(); | 37 WorkerThread* worker_thread = WorkerThread::current(); |
32 DCHECK(worker_thread); | 38 DCHECK(worker_thread); |
33 worker_thread->AddWorkerStub(this); | 39 worker_thread->AddWorkerStub(this); |
34 // Start processing incoming IPCs for this worker. | 40 // Start processing incoming IPCs for this worker. |
35 worker_thread->AddRoute(route_id_, this); | 41 worker_thread->AddRoute(route_id_, this); |
36 ChildProcess::current()->AddRefProcess(); | 42 ChildProcess::current()->AddRefProcess(); |
37 | 43 |
38 // TODO(atwilson): Add support for NaCl when they support MessagePorts. | 44 // TODO(atwilson): Add support for NaCl when they support MessagePorts. |
39 impl_ = WebKit::WebSharedWorker::create(client()); | 45 impl_ = WebKit::WebSharedWorker::create(client()); |
(...skipping 19 matching lines...) Expand all Loading... |
59 client_.EnsureWorkerContextTerminates(); | 65 client_.EnsureWorkerContextTerminates(); |
60 } | 66 } |
61 | 67 |
62 bool WebSharedWorkerStub::OnMessageReceived(const IPC::Message& message) { | 68 bool WebSharedWorkerStub::OnMessageReceived(const IPC::Message& message) { |
63 if (worker_devtools_agent_->OnMessageReceived(message)) | 69 if (worker_devtools_agent_->OnMessageReceived(message)) |
64 return true; | 70 return true; |
65 | 71 |
66 bool handled = true; | 72 bool handled = true; |
67 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerStub, message) | 73 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerStub, message) |
68 IPC_MESSAGE_HANDLER(WorkerMsg_StartWorkerContext, OnStartWorkerContext) | 74 IPC_MESSAGE_HANDLER(WorkerMsg_StartWorkerContext, OnStartWorkerContext) |
| 75 IPC_MESSAGE_HANDLER(WorkerMsg_StartEmbeddedWorkerContext, |
| 76 OnStartEmbeddedWorkerContext); |
69 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, | 77 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, |
70 OnTerminateWorkerContext) | 78 OnTerminateWorkerContext) |
71 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect) | 79 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect) |
| 80 IPC_MESSAGE_HANDLER(AppCacheMsg_FetchEvent, OnFetchEvent) |
72 IPC_MESSAGE_UNHANDLED(handled = false) | 81 IPC_MESSAGE_UNHANDLED(handled = false) |
73 IPC_END_MESSAGE_MAP() | 82 IPC_END_MESSAGE_MAP() |
74 return handled; | 83 return handled; |
75 } | 84 } |
76 | 85 |
77 void WebSharedWorkerStub::OnChannelError() { | 86 void WebSharedWorkerStub::OnChannelError() { |
78 OnTerminateWorkerContext(); | 87 OnTerminateWorkerContext(); |
79 } | 88 } |
80 | 89 |
81 const GURL& WebSharedWorkerStub::url() { | 90 const GURL& WebSharedWorkerStub::url() { |
82 return url_; | 91 return url_; |
83 } | 92 } |
84 | 93 |
85 void WebSharedWorkerStub::OnStartWorkerContext( | 94 void WebSharedWorkerStub::OnStartWorkerContext( |
86 const GURL& url, const string16& user_agent, const string16& source_code, | 95 const GURL& url, const string16& user_agent, const string16& source_code, |
87 const string16& content_security_policy, | 96 const string16& content_security_policy, |
88 WebKit::WebContentSecurityPolicyType policy_type) { | 97 WebKit::WebContentSecurityPolicyType policy_type) { |
89 // Ignore multiple attempts to start this worker (can happen if two pages | 98 // Ignore multiple attempts to start this worker (can happen if two pages |
90 // try to start it simultaneously). | 99 // try to start it simultaneously). |
| 100 DCHECK(!is_embedded_worker_); |
91 if (started_) | 101 if (started_) |
92 return; | 102 return; |
93 | 103 |
94 impl_->startWorkerContext(url, name_, user_agent, source_code, | 104 impl_->startWorkerContext(url, name_, user_agent, source_code, |
95 content_security_policy, policy_type, 0); | 105 content_security_policy, policy_type, 0, false); |
96 started_ = true; | 106 started_ = true; |
97 url_ = url; | 107 url_ = url; |
98 | 108 |
99 // Process any pending connections. | 109 // Process any pending connections. |
100 for (PendingConnectInfoList::const_iterator iter = pending_connects_.begin(); | 110 for (PendingConnectInfoList::const_iterator iter = pending_connects_.begin(); |
101 iter != pending_connects_.end(); | 111 iter != pending_connects_.end(); |
102 ++iter) { | 112 ++iter) { |
103 OnConnect(iter->first, iter->second); | 113 OnConnect(iter->first, iter->second); |
104 } | 114 } |
105 pending_connects_.clear(); | 115 pending_connects_.clear(); |
106 } | 116 } |
107 | 117 |
| 118 void WebSharedWorkerStub::OnStartEmbeddedWorkerContext( |
| 119 const GURL& url, const std::string& raw_source_code) { |
| 120 DCHECK(!started_); |
| 121 |
| 122 is_embedded_worker_ = true; |
| 123 |
| 124 // TODO: plug some values in here. |
| 125 string16 user_agent; |
| 126 string16 content_security_policy; |
| 127 WebKit::WebContentSecurityPolicyType policy_type = WebKit::WebContentSecurityP
olicyType(0); |
| 128 WebKit::WebString source_code = |
| 129 WebKit::WebString::fromUTF8(raw_source_code.data(), |
| 130 raw_source_code.length()); |
| 131 impl_->startWorkerContext(url, name_, user_agent, source_code, |
| 132 content_security_policy, policy_type, 0, true); |
| 133 started_ = true; |
| 134 url_ = url; |
| 135 } |
| 136 |
108 void WebSharedWorkerStub::OnConnect(int sent_message_port_id, int routing_id) { | 137 void WebSharedWorkerStub::OnConnect(int sent_message_port_id, int routing_id) { |
109 if (started_) { | 138 if (started_) { |
110 WebKit::WebMessagePortChannel* channel = | 139 WebKit::WebMessagePortChannel* channel = |
111 new WebMessagePortChannelImpl(routing_id, | 140 new WebMessagePortChannelImpl(routing_id, |
112 sent_message_port_id, | 141 sent_message_port_id, |
113 base::MessageLoopProxy::current().get()); | 142 base::MessageLoopProxy::current().get()); |
114 impl_->connect(channel, NULL); | 143 impl_->connect(channel, NULL); |
115 } else { | 144 } else { |
116 // If two documents try to load a SharedWorker at the same time, the | 145 // If two documents try to load a SharedWorker at the same time, the |
117 // WorkerMsg_Connect for one of the documents can come in before the | 146 // WorkerMsg_Connect for one of the documents can come in before the |
118 // worker is started. Just queue up the connect and deliver it once the | 147 // worker is started. Just queue up the connect and deliver it once the |
119 // worker starts. | 148 // worker starts. |
120 PendingConnectInfo pending_connect(sent_message_port_id, routing_id); | 149 PendingConnectInfo pending_connect(sent_message_port_id, routing_id); |
121 pending_connects_.push_back(pending_connect); | 150 pending_connects_.push_back(pending_connect); |
122 } | 151 } |
123 } | 152 } |
124 | 153 |
| 154 void WebSharedWorkerStub::OnFetchEvent( |
| 155 int request_id, |
| 156 const appcache::AppCacheExecutableHandler::Request& request) { |
| 157 CHECK(started_); |
| 158 // TODO: there are more data members here then we really support atm |
| 159 // and some of them look questionable as to whether they're really needed. |
| 160 // We should prune this to be more minimalistic and reflective of what is |
| 161 // actuall supported atm. |
| 162 // |
| 163 // ANOTHER TODO: this struct is not composed of thread safe string types |
| 164 // yet it's being posted across threads :( We need to avoid using |
| 165 // WebKit defined classes and interfaces for marshalling data around. |
| 166 // fyi: WebString and WebCore::String are landmines. |
| 167 // The response plumbing has the same problem with WebControllerResponse |
| 168 // structs. |
| 169 WebController::Request* webRequest = new WebController::Request(); |
| 170 webRequest->url = request.url; |
| 171 webRequest->method = ASCIIToUTF16(request.method); |
| 172 webRequest->referrer = ASCIIToUTF16(request.referrer); // ??? this is a url |
| 173 for (std::map<std::string, std::string>::const_iterator iter = request.headers
.begin(); |
| 174 iter != request.headers.end(); |
| 175 ++iter) { |
| 176 webRequest->headers.push_back(std::make_pair( |
| 177 WebKit::WebString(UTF8ToUTF16(iter->first)), |
| 178 WebKit::WebString(UTF8ToUTF16(iter->second)))); |
| 179 } |
| 180 |
| 181 impl_->dispatchFetchEvent(request_id, webRequest); |
| 182 } |
| 183 |
| 184 void WebSharedWorkerStub::OnInstallEvent(int host_id) { |
| 185 CHECK(false); |
| 186 } |
| 187 |
125 void WebSharedWorkerStub::OnTerminateWorkerContext() { | 188 void WebSharedWorkerStub::OnTerminateWorkerContext() { |
126 impl_->terminateWorkerContext(); | 189 impl_->terminateWorkerContext(); |
127 | 190 |
128 // Call the client to make sure context exits. | 191 // Call the client to make sure context exits. |
129 EnsureWorkerContextTerminates(); | 192 EnsureWorkerContextTerminates(); |
130 started_ = false; | 193 started_ = false; |
131 } | 194 } |
132 | 195 |
133 } // namespace content | 196 } // namespace content |
OLD | NEW |