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)), |
42 } | 44 render_process_id_(render_process_id), |
| 45 message_port_message_filter_(message_port_message_filter) {} |
43 | 46 |
44 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 47 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
45 if (context_) { | 48 if (context_) { |
46 context_->RemoveAllProviderHostsForProcess(render_process_id_); | 49 context_->RemoveAllProviderHostsForProcess(render_process_id_); |
47 context_->embedded_worker_registry()->RemoveChildProcessSender( | 50 context_->embedded_worker_registry()->RemoveChildProcessSender( |
48 render_process_id_); | 51 render_process_id_); |
49 } | 52 } |
50 } | 53 } |
51 | 54 |
52 void ServiceWorkerDispatcherHost::Init( | 55 void ServiceWorkerDispatcherHost::Init( |
(...skipping 25 matching lines...) Expand all Loading... |
78 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 81 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
79 OnUnregisterServiceWorker) | 82 OnUnregisterServiceWorker) |
80 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, | 83 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
81 OnProviderCreated) | 84 OnProviderCreated) |
82 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, | 85 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, |
83 OnProviderDestroyed) | 86 OnProviderDestroyed) |
84 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_AddScriptClient, | 87 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_AddScriptClient, |
85 OnAddScriptClient) | 88 OnAddScriptClient) |
86 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RemoveScriptClient, | 89 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RemoveScriptClient, |
87 OnRemoveScriptClient) | 90 OnRemoveScriptClient) |
| 91 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessage, OnPostMessage) |
88 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted, | 92 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStarted, |
89 OnWorkerStarted) | 93 OnWorkerStarted) |
90 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped, | 94 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerStopped, |
91 OnWorkerStopped) | 95 OnWorkerStopped) |
92 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_SendMessageToBrowser, | 96 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_SendMessageToBrowser, |
93 OnSendMessageToBrowser) | 97 OnSendMessageToBrowser) |
94 IPC_MESSAGE_UNHANDLED(handled = false) | 98 IPC_MESSAGE_UNHANDLED(handled = false) |
95 IPC_END_MESSAGE_MAP() | 99 IPC_END_MESSAGE_MAP() |
96 | 100 |
97 return handled; | 101 return handled; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 155 |
152 context_->UnregisterServiceWorker( | 156 context_->UnregisterServiceWorker( |
153 pattern, | 157 pattern, |
154 render_process_id_, | 158 render_process_id_, |
155 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, | 159 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, |
156 this, | 160 this, |
157 thread_id, | 161 thread_id, |
158 request_id)); | 162 request_id)); |
159 } | 163 } |
160 | 164 |
| 165 void ServiceWorkerDispatcherHost::OnPostMessage( |
| 166 int64 registration_id, |
| 167 const base::string16& message, |
| 168 const std::vector<int>& sent_message_port_ids) { |
| 169 if (!context_ || !ServiceWorkerUtils::IsFeatureEnabled()) |
| 170 return; |
| 171 |
| 172 std::vector<int> new_routing_ids(sent_message_port_ids.size()); |
| 173 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
| 174 new_routing_ids[i] = message_port_message_filter_->GetNextRoutingID(); |
| 175 MessagePortService::GetInstance()->UpdateMessagePort( |
| 176 sent_message_port_ids[i], |
| 177 message_port_message_filter_, |
| 178 new_routing_ids[i]); |
| 179 } |
| 180 |
| 181 context_->storage()->FindRegistrationForId( |
| 182 registration_id, |
| 183 base::Bind(&ServiceWorkerDispatcherHost::PostMessageFoundRegistration, |
| 184 message, |
| 185 sent_message_port_ids, |
| 186 new_routing_ids)); |
| 187 } |
| 188 |
| 189 namespace { |
| 190 void NoOpStatusCallback(ServiceWorkerStatusCode status) {} |
| 191 } // namespace |
| 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 |