| 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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "content/browser/message_port_message_filter.h" | 9 #include "content/browser/message_port_message_filter.h" |
| 10 #include "content/browser/message_port_service.h" | 10 #include "content/browser/message_port_service.h" |
| 11 #include "content/browser/service_worker/embedded_worker_registry.h" | 11 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 12 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
| 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 14 #include "content/browser/service_worker/service_worker_handle.h" | 14 #include "content/browser/service_worker/service_worker_handle.h" |
| 15 #include "content/browser/service_worker/service_worker_provider_host_registry.h
" |
| 15 #include "content/browser/service_worker/service_worker_registration.h" | 16 #include "content/browser/service_worker/service_worker_registration.h" |
| 16 #include "content/browser/service_worker/service_worker_utils.h" | 17 #include "content/browser/service_worker/service_worker_utils.h" |
| 17 #include "content/common/service_worker/embedded_worker_messages.h" | 18 #include "content/common/service_worker/embedded_worker_messages.h" |
| 18 #include "content/common/service_worker/service_worker_messages.h" | 19 #include "content/common/service_worker/service_worker_messages.h" |
| 19 #include "ipc/ipc_message_macros.h" | 20 #include "ipc/ipc_message_macros.h" |
| 20 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" | 21 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 23 using blink::WebServiceWorkerError; | 24 using blink::WebServiceWorkerError; |
| 24 | 25 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 42 int render_process_id, | 43 int render_process_id, |
| 43 MessagePortMessageFilter* message_port_message_filter) | 44 MessagePortMessageFilter* message_port_message_filter) |
| 44 : BrowserMessageFilter(kFilteredMessageClasses, | 45 : BrowserMessageFilter(kFilteredMessageClasses, |
| 45 arraysize(kFilteredMessageClasses)), | 46 arraysize(kFilteredMessageClasses)), |
| 46 render_process_id_(render_process_id), | 47 render_process_id_(render_process_id), |
| 47 message_port_message_filter_(message_port_message_filter), | 48 message_port_message_filter_(message_port_message_filter), |
| 48 channel_ready_(false) { | 49 channel_ready_(false) { |
| 49 } | 50 } |
| 50 | 51 |
| 51 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 52 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| 53 GetProviderRegistry()->RemoveAllProviderHostsForProcess(render_process_id_); |
| 52 if (GetContext()) { | 54 if (GetContext()) { |
| 53 GetContext()->RemoveAllProviderHostsForProcess(render_process_id_); | |
| 54 GetContext()->embedded_worker_registry()->RemoveChildProcessSender( | 55 GetContext()->embedded_worker_registry()->RemoveChildProcessSender( |
| 55 render_process_id_); | 56 render_process_id_); |
| 56 } | 57 } |
| 57 } | 58 } |
| 58 | 59 |
| 59 void ServiceWorkerDispatcherHost::Init( | 60 void ServiceWorkerDispatcherHost::Init( |
| 60 ServiceWorkerContextWrapper* context_wrapper) { | 61 ServiceWorkerContextWrapper* context_wrapper) { |
| 61 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 62 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 62 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
| 63 BrowserThread::IO, FROM_HERE, | 64 BrowserThread::IO, FROM_HERE, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // ChildProcessSecurityImpl. See http://crbug.com/311631. | 167 // ChildProcessSecurityImpl. See http://crbug.com/311631. |
| 167 if (pattern.GetOrigin() != script_url.GetOrigin()) { | 168 if (pattern.GetOrigin() != script_url.GetOrigin()) { |
| 168 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 169 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 169 thread_id, | 170 thread_id, |
| 170 request_id, | 171 request_id, |
| 171 WebServiceWorkerError::ErrorTypeSecurity, | 172 WebServiceWorkerError::ErrorTypeSecurity, |
| 172 base::ASCIIToUTF16(kDomainMismatchErrorMessage))); | 173 base::ASCIIToUTF16(kDomainMismatchErrorMessage))); |
| 173 return; | 174 return; |
| 174 } | 175 } |
| 175 | 176 |
| 176 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost( | 177 ServiceWorkerProviderHost* provider_host = |
| 177 render_process_id_, provider_id); | 178 GetProviderRegistry()->GetProviderHost(render_process_id_, provider_id); |
| 178 if (!provider_host) { | 179 if (!provider_host) { |
| 179 BadMessageReceived(); | 180 BadMessageReceived(); |
| 180 return; | 181 return; |
| 181 } | 182 } |
| 183 if (!provider_host->IsAlive()) { |
| 184 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 185 thread_id, |
| 186 request_id, |
| 187 WebServiceWorkerError::ErrorTypeDisabled, |
| 188 base::ASCIIToUTF16(kDisabledErrorMessage))); |
| 189 return; |
| 190 } |
| 182 | 191 |
| 183 GetContext()->RegisterServiceWorker( | 192 GetContext()->RegisterServiceWorker( |
| 184 pattern, | 193 pattern, |
| 185 script_url, | 194 script_url, |
| 186 render_process_id_, | 195 render_process_id_, |
| 187 provider_host, | 196 provider_host, |
| 188 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, | 197 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, |
| 189 this, | 198 this, |
| 190 thread_id, | 199 thread_id, |
| 191 request_id)); | 200 request_id)); |
| 192 } | 201 } |
| 193 | 202 |
| 194 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( | 203 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( |
| 195 int thread_id, | 204 int thread_id, |
| 196 int request_id, | 205 int request_id, |
| 197 int provider_id, | 206 int provider_id, |
| 198 const GURL& pattern) { | 207 const GURL& pattern) { |
| 199 // TODO(alecflett): This check is insufficient for release. Add a | 208 // TODO(alecflett): This check is insufficient for release. Add a |
| 200 // ServiceWorker-specific policy query in | 209 // ServiceWorker-specific policy query in |
| 201 // ChildProcessSecurityImpl. See http://crbug.com/311631. | 210 // ChildProcessSecurityImpl. See http://crbug.com/311631. |
| 202 if (!GetContext() || !ServiceWorkerUtils::IsFeatureEnabled()) { | 211 if (!GetContext() || !ServiceWorkerUtils::IsFeatureEnabled()) { |
| 203 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 212 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 204 thread_id, | 213 thread_id, |
| 205 request_id, | 214 request_id, |
| 206 blink::WebServiceWorkerError::ErrorTypeDisabled, | 215 blink::WebServiceWorkerError::ErrorTypeDisabled, |
| 207 base::ASCIIToUTF16(kDisabledErrorMessage))); | 216 base::ASCIIToUTF16(kDisabledErrorMessage))); |
| 208 return; | 217 return; |
| 209 } | 218 } |
| 210 | 219 |
| 211 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost( | 220 ServiceWorkerProviderHost* provider_host = |
| 212 render_process_id_, provider_id); | 221 GetProviderRegistry()->GetProviderHost(render_process_id_, provider_id); |
| 213 if (!provider_host) { | 222 if (!provider_host) { |
| 214 BadMessageReceived(); | 223 BadMessageReceived(); |
| 215 return; | 224 return; |
| 216 } | 225 } |
| 226 if (!provider_host->IsAlive()) { |
| 227 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 228 thread_id, |
| 229 request_id, |
| 230 WebServiceWorkerError::ErrorTypeDisabled, |
| 231 base::ASCIIToUTF16(kDisabledErrorMessage))); |
| 232 return; |
| 233 } |
| 217 | 234 |
| 218 GetContext()->UnregisterServiceWorker( | 235 GetContext()->UnregisterServiceWorker( |
| 219 pattern, | 236 pattern, |
| 220 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, | 237 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, |
| 221 this, | 238 this, |
| 222 thread_id, | 239 thread_id, |
| 223 request_id)); | 240 request_id)); |
| 224 } | 241 } |
| 225 | 242 |
| 226 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( | 243 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 242 handle->version()->SendMessage( | 259 handle->version()->SendMessage( |
| 243 ServiceWorkerMsg_MessageToWorker(message, | 260 ServiceWorkerMsg_MessageToWorker(message, |
| 244 sent_message_port_ids, | 261 sent_message_port_ids, |
| 245 new_routing_ids), | 262 new_routing_ids), |
| 246 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 263 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 247 } | 264 } |
| 248 | 265 |
| 249 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) { | 266 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) { |
| 250 if (!GetContext()) | 267 if (!GetContext()) |
| 251 return; | 268 return; |
| 252 if (GetContext()->GetProviderHost(render_process_id_, provider_id)) { | 269 if (GetProviderRegistry()->GetProviderHost(render_process_id_, provider_id)) { |
| 253 BadMessageReceived(); | 270 BadMessageReceived(); |
| 254 return; | 271 return; |
| 255 } | 272 } |
| 256 scoped_ptr<ServiceWorkerProviderHost> provider_host( | 273 scoped_ptr<ServiceWorkerProviderHost> provider_host( |
| 257 new ServiceWorkerProviderHost( | 274 new ServiceWorkerProviderHost( |
| 258 render_process_id_, provider_id, GetContext()->AsWeakPtr(), this)); | 275 render_process_id_, provider_id, GetContext()->AsWeakPtr(), this)); |
| 259 GetContext()->AddProviderHost(provider_host.Pass()); | 276 GetProviderRegistry()->AddProviderHost(provider_host.Pass()); |
| 260 } | 277 } |
| 261 | 278 |
| 262 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) { | 279 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) { |
| 263 if (!GetContext()) | 280 if (!GetContext()) |
| 264 return; | 281 return; |
| 265 if (!GetContext()->GetProviderHost(render_process_id_, provider_id)) { | 282 if (!GetProviderRegistry()->GetProviderHost(render_process_id_, |
| 283 provider_id)) { |
| 266 BadMessageReceived(); | 284 BadMessageReceived(); |
| 267 return; | 285 return; |
| 268 } | 286 } |
| 269 GetContext()->RemoveProviderHost(render_process_id_, provider_id); | 287 GetProviderRegistry()->RemoveProviderHost(render_process_id_, provider_id); |
| 270 } | 288 } |
| 271 | 289 |
| 272 void ServiceWorkerDispatcherHost::OnSetHostedVersionId( | 290 void ServiceWorkerDispatcherHost::OnSetHostedVersionId( |
| 273 int provider_id, int64 version_id) { | 291 int provider_id, int64 version_id) { |
| 274 if (!GetContext()) | 292 if (!GetContext()) |
| 275 return; | 293 return; |
| 276 ServiceWorkerProviderHost* provider_host = | 294 ServiceWorkerProviderHost* provider_host = |
| 277 GetContext()->GetProviderHost(render_process_id_, provider_id); | 295 GetProviderRegistry()->GetProviderHost(render_process_id_, provider_id); |
| 278 if (!provider_host || !provider_host->SetHostedVersionId(version_id)) { | 296 if (!provider_host) { |
| 279 BadMessageReceived(); | 297 BadMessageReceived(); |
| 280 return; | 298 return; |
| 281 } | 299 } |
| 300 if (!provider_host->IsAlive()) |
| 301 return; |
| 302 if (!provider_host->SetHostedVersionId(version_id)) |
| 303 BadMessageReceived(); |
| 282 } | 304 } |
| 283 | 305 |
| 284 void ServiceWorkerDispatcherHost::RegistrationComplete( | 306 void ServiceWorkerDispatcherHost::RegistrationComplete( |
| 285 int thread_id, | 307 int thread_id, |
| 286 int request_id, | 308 int request_id, |
| 287 ServiceWorkerStatusCode status, | 309 ServiceWorkerStatusCode status, |
| 288 int64 registration_id, | 310 int64 registration_id, |
| 289 int64 version_id) { | 311 int64 version_id) { |
| 290 if (!GetContext()) | 312 if (!GetContext()) |
| 291 return; | 313 return; |
| 292 | 314 |
| 293 if (status != SERVICE_WORKER_OK) { | 315 if (status != SERVICE_WORKER_OK) { |
| 294 SendRegistrationError(thread_id, request_id, status); | 316 SendRegistrationError(thread_id, request_id, status); |
| 295 return; | 317 return; |
| 296 } | 318 } |
| 297 | 319 |
| 298 ServiceWorkerVersion* version = GetContext()->GetLiveVersion(version_id); | 320 ServiceWorkerVersion* version = GetContext()->GetLiveVersion(version_id); |
| 299 DCHECK(version); | 321 DCHECK(version); |
| 300 DCHECK_EQ(registration_id, version->registration_id()); | 322 DCHECK_EQ(registration_id, version->registration_id()); |
| 301 scoped_ptr<ServiceWorkerHandle> handle = | 323 scoped_ptr<ServiceWorkerHandle> handle = |
| 302 ServiceWorkerHandle::Create(GetContext()->AsWeakPtr(), | 324 ServiceWorkerHandle::Create(GetContext()->AsWeakPtr(), |
| 303 this, thread_id, version); | 325 this, thread_id, version); |
| 304 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( | 326 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( |
| 305 thread_id, request_id, handle->GetObjectInfo())); | 327 thread_id, request_id, handle->GetObjectInfo())); |
| 306 RegisterServiceWorkerHandle(handle.Pass()); | 328 RegisterServiceWorkerHandle(handle.Pass()); |
| 307 } | 329 } |
| 308 | 330 |
| 331 // TODO(nhiroki): These message handlers that take |embedded_worker_id| as an |
| 332 // input should check if the worker refers to the live context. If the context |
| 333 // was deleted, handle the messege gracefully (http://crbug.com/371675). |
| 309 void ServiceWorkerDispatcherHost::OnWorkerScriptLoaded(int embedded_worker_id) { | 334 void ServiceWorkerDispatcherHost::OnWorkerScriptLoaded(int embedded_worker_id) { |
| 310 if (!GetContext()) | 335 if (!GetContext()) |
| 311 return; | 336 return; |
| 312 GetContext()->embedded_worker_registry()->OnWorkerScriptLoaded( | 337 GetContext()->embedded_worker_registry()->OnWorkerScriptLoaded( |
| 313 render_process_id_, embedded_worker_id); | 338 render_process_id_, embedded_worker_id); |
| 314 } | 339 } |
| 315 | 340 |
| 316 void ServiceWorkerDispatcherHost::OnWorkerScriptLoadFailed( | 341 void ServiceWorkerDispatcherHost::OnWorkerScriptLoadFailed( |
| 317 int embedded_worker_id) { | 342 int embedded_worker_id) { |
| 318 if (!GetContext()) | 343 if (!GetContext()) |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 GetServiceWorkerRegistrationStatusResponse( | 434 GetServiceWorkerRegistrationStatusResponse( |
| 410 status, &error_type, &error_message); | 435 status, &error_type, &error_message); |
| 411 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 436 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 412 thread_id, request_id, error_type, error_message)); | 437 thread_id, request_id, error_type, error_message)); |
| 413 } | 438 } |
| 414 | 439 |
| 415 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { | 440 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
| 416 return context_wrapper_->context(); | 441 return context_wrapper_->context(); |
| 417 } | 442 } |
| 418 | 443 |
| 444 ServiceWorkerProviderHostRegistry* |
| 445 ServiceWorkerDispatcherHost::GetProviderRegistry() { |
| 446 return context_wrapper_->provider_registry(); |
| 447 } |
| 448 |
| 419 } // namespace content | 449 } // namespace content |
| OLD | NEW |