Chromium Code Reviews| 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/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "content/browser/message_port_message_filter.h" | |
| 9 #include "content/browser/message_port_service.h" | |
| 8 #include "content/browser/service_worker/embedded_worker_registry.h" | 10 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 11 #include "content/browser/service_worker/service_worker_provider_host.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
| 12 #include "content/browser/service_worker/service_worker_utils.h" | 14 #include "content/browser/service_worker/service_worker_utils.h" |
| 13 #include "content/common/service_worker/embedded_worker_messages.h" | 15 #include "content/common/service_worker/embedded_worker_messages.h" |
| 14 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
| 15 #include "ipc/ipc_message_macros.h" | 17 #include "ipc/ipc_message_macros.h" |
| 16 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" | 18 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" |
| 17 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 18 | 20 |
| 19 using blink::WebServiceWorkerError; | 21 using blink::WebServiceWorkerError; |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 const char kDisabledErrorMessage[] = | 25 const char kDisabledErrorMessage[] = |
| 24 "ServiceWorker is disabled"; | 26 "ServiceWorker is disabled"; |
| 25 const char kDomainMismatchErrorMessage[] = | 27 const char kDomainMismatchErrorMessage[] = |
| 26 "Scope and scripts do not have the same origin"; | 28 "Scope and scripts do not have the same origin"; |
| 27 | 29 |
| 28 const uint32 kFilteredMessageClasses[] = { | 30 const uint32 kFilteredMessageClasses[] = { |
| 29 ServiceWorkerMsgStart, | 31 ServiceWorkerMsgStart, |
| 30 EmbeddedWorkerMsgStart, | 32 EmbeddedWorkerMsgStart, |
| 31 }; | 33 }; |
| 32 | 34 |
| 33 } // namespace | 35 } // namespace |
| 34 | 36 |
| 35 namespace content { | 37 namespace content { |
| 36 | 38 |
| 37 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 39 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
| 38 int render_process_id) | 40 int render_process_id, |
| 39 : BrowserMessageFilter( | 41 MessagePortMessageFilter* message_port_message_filter) |
| 40 kFilteredMessageClasses, arraysize(kFilteredMessageClasses)), | 42 : BrowserMessageFilter(kFilteredMessageClasses, |
| 41 render_process_id_(render_process_id) { | 43 arraysize(kFilteredMessageClasses)), |
| 44 render_process_id_(render_process_id), | |
| 45 message_port_message_filter_(message_port_message_filter) { | |
| 42 } | 46 } |
| 43 | 47 |
| 44 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 48 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| 45 if (context_) { | 49 if (context_) { |
| 46 context_->RemoveAllProviderHostsForProcess(render_process_id_); | 50 context_->RemoveAllProviderHostsForProcess(render_process_id_); |
| 47 context_->embedded_worker_registry()->RemoveChildProcessSender( | 51 context_->embedded_worker_registry()->RemoveChildProcessSender( |
| 48 render_process_id_); | 52 render_process_id_); |
| 49 } | 53 } |
| 50 } | 54 } |
| 51 | 55 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 78 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 82 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
| 79 OnUnregisterServiceWorker) | 83 OnUnregisterServiceWorker) |
| 80 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, | 84 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
| 81 OnProviderCreated) | 85 OnProviderCreated) |
| 82 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, | 86 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, |
| 83 OnProviderDestroyed) | 87 OnProviderDestroyed) |
| 84 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_AddScriptClient, | 88 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_AddScriptClient, |
| 85 OnAddScriptClient) | 89 OnAddScriptClient) |
| 86 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RemoveScriptClient, | 90 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RemoveScriptClient, |
| 87 OnRemoveScriptClient) | 91 OnRemoveScriptClient) |
| 92 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessage, | |
| 93 OnPostMessage) | |
| 88 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted, | 94 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted, |
| 89 OnWorkerStarted) | 95 OnWorkerStarted) |
| 90 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped, | 96 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped, |
| 91 OnWorkerStopped) | 97 OnWorkerStopped) |
| 92 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_SendMessageToBrowser, | 98 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_SendMessageToBrowser, |
| 93 OnSendMessageToBrowser) | 99 OnSendMessageToBrowser) |
| 94 IPC_MESSAGE_UNHANDLED(handled = false) | 100 IPC_MESSAGE_UNHANDLED(handled = false) |
| 95 IPC_END_MESSAGE_MAP() | 101 IPC_END_MESSAGE_MAP() |
| 96 | 102 |
| 97 return handled; | 103 return handled; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 | 157 |
| 152 context_->UnregisterServiceWorker( | 158 context_->UnregisterServiceWorker( |
| 153 pattern, | 159 pattern, |
| 154 render_process_id_, | 160 render_process_id_, |
| 155 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, | 161 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, |
| 156 this, | 162 this, |
| 157 thread_id, | 163 thread_id, |
| 158 request_id)); | 164 request_id)); |
| 159 } | 165 } |
| 160 | 166 |
| 167 void ServiceWorkerDispatcherHost::OnPostMessage( | |
| 168 int64 registration_id, | |
| 169 const base::string16& message, | |
| 170 const std::vector<int>& sent_message_port_ids) { | |
| 171 if (!context_ || !ServiceWorkerUtils::IsFeatureEnabled()) | |
| 172 return; | |
| 173 | |
| 174 std::vector<int> new_routing_ids(sent_message_port_ids.size()); | |
| 175 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { | |
| 176 new_routing_ids[i] = message_port_message_filter_->GetNextRoutingID(); | |
| 177 MessagePortService::GetInstance()->UpdateMessagePort( | |
| 178 sent_message_port_ids[i], | |
| 179 message_port_message_filter_, | |
| 180 new_routing_ids[i]); | |
| 181 } | |
| 182 | |
| 183 context_->storage()->FindRegistrationForId( | |
| 184 registration_id, | |
| 185 base::Bind(&ServiceWorkerDispatcherHost::PostMessageFoundRegistration, | |
| 186 message, | |
| 187 sent_message_port_ids, | |
| 188 new_routing_ids)); | |
| 189 } | |
| 190 | |
| 191 static void NoOpStatusCallback(ServiceWorkerStatusCode status) {} | |
|
marja
2014/03/17 09:15:02
Unnamed namespace instead of static?
(It seems th
jsbell
2014/03/17 16:49:06
Done.
| |
| 192 | |
| 193 // static | |
| 194 void ServiceWorkerDispatcherHost::PostMessageFoundRegistration( | |
| 195 const base::string16& message, | |
| 196 const std::vector<int>& sent_message_port_ids, | |
| 197 const std::vector<int>& new_routing_ids, | |
| 198 ServiceWorkerStatusCode status, | |
| 199 const scoped_refptr<ServiceWorkerRegistration>& result) { | |
| 200 if (status != SERVICE_WORKER_OK) | |
| 201 return; | |
| 202 DCHECK(result); | |
| 203 | |
| 204 // TODO(jsbell): Route message to appropriate version. crbug.com/351797 | |
| 205 ServiceWorkerVersion* version = result->active_version(); | |
| 206 if (!version) | |
| 207 return; | |
| 208 version->SendMessage( | |
| 209 ServiceWorkerMsg_Message(message, sent_message_port_ids, new_routing_ids), | |
| 210 base::Bind(&NoOpStatusCallback)); | |
| 211 } | |
| 212 | |
| 161 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) { | 213 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) { |
| 162 if (!context_) | 214 if (!context_) |
| 163 return; | 215 return; |
| 164 if (context_->GetProviderHost(render_process_id_, provider_id)) { | 216 if (context_->GetProviderHost(render_process_id_, provider_id)) { |
| 165 BadMessageReceived(); | 217 BadMessageReceived(); |
| 166 return; | 218 return; |
| 167 } | 219 } |
| 168 scoped_ptr<ServiceWorkerProviderHost> provider_host( | 220 scoped_ptr<ServiceWorkerProviderHost> provider_host( |
| 169 new ServiceWorkerProviderHost(render_process_id_, provider_id)); | 221 new ServiceWorkerProviderHost(render_process_id_, provider_id)); |
| 170 context_->AddProviderHost(provider_host.Pass()); | 222 context_->AddProviderHost(provider_host.Pass()); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 ServiceWorkerStatusCode status) { | 311 ServiceWorkerStatusCode status) { |
| 260 base::string16 error_message; | 312 base::string16 error_message; |
| 261 blink::WebServiceWorkerError::ErrorType error_type; | 313 blink::WebServiceWorkerError::ErrorType error_type; |
| 262 GetServiceWorkerRegistrationStatusResponse( | 314 GetServiceWorkerRegistrationStatusResponse( |
| 263 status, &error_type, &error_message); | 315 status, &error_type, &error_message); |
| 264 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 316 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 265 thread_id, request_id, error_type, error_message)); | 317 thread_id, request_id, error_type, error_message)); |
| 266 } | 318 } |
| 267 | 319 |
| 268 } // namespace content | 320 } // namespace content |
| OLD | NEW |