| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/child/service_worker/service_worker_dispatcher.h" | 5 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
| 10 #include "content/child/child_thread.h" |
| 10 #include "content/child/service_worker/service_worker_handle_reference.h" | 11 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 11 #include "content/child/service_worker/service_worker_provider_context.h" | 12 #include "content/child/service_worker/service_worker_provider_context.h" |
| 12 #include "content/child/service_worker/web_service_worker_impl.h" | 13 #include "content/child/service_worker/web_service_worker_impl.h" |
| 13 #include "content/child/thread_safe_sender.h" | 14 #include "content/child/thread_safe_sender.h" |
| 15 #include "content/child/webmessageportchannel_impl.h" |
| 14 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
| 15 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h" | 17 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h" |
| 16 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 18 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
| 17 | 19 |
| 18 using blink::WebServiceWorkerError; | 20 using blink::WebServiceWorkerError; |
| 19 using blink::WebServiceWorkerProvider; | 21 using blink::WebServiceWorkerProvider; |
| 20 using base::ThreadLocalPointer; | 22 using base::ThreadLocalPointer; |
| 21 | 23 |
| 22 namespace content { | 24 namespace content { |
| 23 | 25 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 50 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) | 52 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) |
| 51 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) | 53 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) |
| 52 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, | 54 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, |
| 53 OnUnregistered) | 55 OnUnregistered) |
| 54 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, | 56 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, |
| 55 OnRegistrationError) | 57 OnRegistrationError) |
| 56 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerStateChanged, | 58 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerStateChanged, |
| 57 OnServiceWorkerStateChanged) | 59 OnServiceWorkerStateChanged) |
| 58 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetCurrentServiceWorker, | 60 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetCurrentServiceWorker, |
| 59 OnSetCurrentServiceWorker) | 61 OnSetCurrentServiceWorker) |
| 62 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToDocument, |
| 63 OnPostMessage) |
| 60 IPC_MESSAGE_UNHANDLED(handled = false) | 64 IPC_MESSAGE_UNHANDLED(handled = false) |
| 61 IPC_END_MESSAGE_MAP() | 65 IPC_END_MESSAGE_MAP() |
| 62 DCHECK(handled) << "Unhandled message:" << msg.type(); | 66 DCHECK(handled) << "Unhandled message:" << msg.type(); |
| 63 } | 67 } |
| 64 | 68 |
| 65 bool ServiceWorkerDispatcher::Send(IPC::Message* msg) { | 69 bool ServiceWorkerDispatcher::Send(IPC::Message* msg) { |
| 66 return thread_safe_sender_->Send(msg); | 70 return thread_safe_sender_->Send(msg); |
| 67 } | 71 } |
| 68 | 72 |
| 69 void ServiceWorkerDispatcher::RegisterServiceWorker( | 73 void ServiceWorkerDispatcher::RegisterServiceWorker( |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 228 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
| 225 if (found != script_clients_.end()) { | 229 if (found != script_clients_.end()) { |
| 226 // Populate the .current field with the new worker object. | 230 // Populate the .current field with the new worker object. |
| 227 scoped_ptr<ServiceWorkerHandleReference> handle_ref( | 231 scoped_ptr<ServiceWorkerHandleReference> handle_ref( |
| 228 ServiceWorkerHandleReference::Create(info, thread_safe_sender_)); | 232 ServiceWorkerHandleReference::Create(info, thread_safe_sender_)); |
| 229 found->second->setCurrentServiceWorker( | 233 found->second->setCurrentServiceWorker( |
| 230 new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_)); | 234 new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_)); |
| 231 } | 235 } |
| 232 } | 236 } |
| 233 | 237 |
| 238 void ServiceWorkerDispatcher::OnPostMessage( |
| 239 int thread_id, |
| 240 int provider_id, |
| 241 const base::string16& message, |
| 242 const std::vector<int>& sent_message_port_ids, |
| 243 const std::vector<int>& new_routing_ids) { |
| 244 // Make sure we're on the main document thread. (That must be the only |
| 245 // thread we get this message) |
| 246 DCHECK(ChildThread::current()); |
| 247 |
| 248 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
| 249 if (found == script_clients_.end()) { |
| 250 // For now we do no queueing for messages sent to nonexistent / unattached |
| 251 // client. |
| 252 return; |
| 253 } |
| 254 |
| 255 std::vector<WebMessagePortChannelImpl*> ports; |
| 256 if (!sent_message_port_ids.empty()) { |
| 257 ports.resize(sent_message_port_ids.size()); |
| 258 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
| 259 ports[i] = new WebMessagePortChannelImpl( |
| 260 new_routing_ids[i], sent_message_port_ids[i], |
| 261 base::MessageLoopProxy::current()); |
| 262 } |
| 263 } |
| 264 |
| 265 found->second->dispatchMessageEvent(message, ports); |
| 266 } |
| 267 |
| 234 void ServiceWorkerDispatcher::AddServiceWorker( | 268 void ServiceWorkerDispatcher::AddServiceWorker( |
| 235 int handle_id, WebServiceWorkerImpl* worker) { | 269 int handle_id, WebServiceWorkerImpl* worker) { |
| 236 DCHECK(!ContainsKey(service_workers_, handle_id)); | 270 DCHECK(!ContainsKey(service_workers_, handle_id)); |
| 237 service_workers_[handle_id] = worker; | 271 service_workers_[handle_id] = worker; |
| 238 } | 272 } |
| 239 | 273 |
| 240 void ServiceWorkerDispatcher::RemoveServiceWorker(int handle_id) { | 274 void ServiceWorkerDispatcher::RemoveServiceWorker(int handle_id) { |
| 241 DCHECK(ContainsKey(service_workers_, handle_id)); | 275 DCHECK(ContainsKey(service_workers_, handle_id)); |
| 242 service_workers_.erase(handle_id); | 276 service_workers_.erase(handle_id); |
| 243 } | 277 } |
| 244 | 278 |
| 245 } // namespace content | 279 } // namespace content |
| OLD | NEW |