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/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "content/browser/message_port_message_filter.h" | 10 #include "content/browser/message_port_message_filter.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 document_url.GetOrigin() == script_url.GetOrigin(); | 48 document_url.GetOrigin() == script_url.GetOrigin(); |
49 } | 49 } |
50 | 50 |
51 bool CanUnregisterServiceWorker(const GURL& document_url, | 51 bool CanUnregisterServiceWorker(const GURL& document_url, |
52 const GURL& pattern) { | 52 const GURL& pattern) { |
53 // TODO: Respect Chrome's content settings, if we add a setting for | 53 // TODO: Respect Chrome's content settings, if we add a setting for |
54 // controlling whether Service Worker is allowed. | 54 // controlling whether Service Worker is allowed. |
55 return document_url.GetOrigin() == pattern.GetOrigin(); | 55 return document_url.GetOrigin() == pattern.GetOrigin(); |
56 } | 56 } |
57 | 57 |
| 58 bool CanGetRegistration(const GURL& document_url, |
| 59 const GURL& given_document_url) { |
| 60 // TODO: Respect Chrome's content settings, if we add a setting for |
| 61 // controlling whether Service Worker is allowed. |
| 62 return document_url.GetOrigin() == given_document_url.GetOrigin(); |
| 63 } |
| 64 |
58 } // namespace | 65 } // namespace |
59 | 66 |
60 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 67 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
61 int render_process_id, | 68 int render_process_id, |
62 MessagePortMessageFilter* message_port_message_filter) | 69 MessagePortMessageFilter* message_port_message_filter) |
63 : BrowserMessageFilter(kFilteredMessageClasses, | 70 : BrowserMessageFilter(kFilteredMessageClasses, |
64 arraysize(kFilteredMessageClasses)), | 71 arraysize(kFilteredMessageClasses)), |
65 render_process_id_(render_process_id), | 72 render_process_id_(render_process_id), |
66 message_port_message_filter_(message_port_message_filter), | 73 message_port_message_filter_(message_port_message_filter), |
67 channel_ready_(false) { | 74 channel_ready_(false) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 } | 113 } |
107 | 114 |
108 bool ServiceWorkerDispatcherHost::OnMessageReceived( | 115 bool ServiceWorkerDispatcherHost::OnMessageReceived( |
109 const IPC::Message& message) { | 116 const IPC::Message& message) { |
110 bool handled = true; | 117 bool handled = true; |
111 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) | 118 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) |
112 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, | 119 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, |
113 OnRegisterServiceWorker) | 120 OnRegisterServiceWorker) |
114 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 121 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
115 OnUnregisterServiceWorker) | 122 OnUnregisterServiceWorker) |
| 123 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, |
| 124 OnGetRegistration) |
116 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, | 125 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
117 OnProviderCreated) | 126 OnProviderCreated) |
118 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, | 127 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, |
119 OnProviderDestroyed) | 128 OnProviderDestroyed) |
120 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetVersionId, | 129 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetVersionId, |
121 OnSetHostedVersionId) | 130 OnSetHostedVersionId) |
122 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker, | 131 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker, |
123 OnPostMessageToWorker) | 132 OnPostMessageToWorker) |
124 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerReadyForInspection, | 133 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerReadyForInspection, |
125 OnWorkerReadyForInspection) | 134 OnWorkerReadyForInspection) |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 request_id, | 304 request_id, |
296 "Pattern", pattern.spec()); | 305 "Pattern", pattern.spec()); |
297 GetContext()->UnregisterServiceWorker( | 306 GetContext()->UnregisterServiceWorker( |
298 pattern, | 307 pattern, |
299 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, | 308 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, |
300 this, | 309 this, |
301 thread_id, | 310 thread_id, |
302 request_id)); | 311 request_id)); |
303 } | 312 } |
304 | 313 |
| 314 void ServiceWorkerDispatcherHost::OnGetRegistration( |
| 315 int thread_id, |
| 316 int request_id, |
| 317 int provider_id, |
| 318 const GURL& document_url) { |
| 319 TRACE_EVENT0("ServiceWorker", |
| 320 "ServiceWorkerDispatcherHost::OnGetRegistration"); |
| 321 if (!GetContext()) { |
| 322 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
| 323 thread_id, |
| 324 request_id, |
| 325 blink::WebServiceWorkerError::ErrorTypeAbort, |
| 326 base::ASCIIToUTF16(kShutdownErrorMessage))); |
| 327 return; |
| 328 } |
| 329 |
| 330 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost( |
| 331 render_process_id_, provider_id); |
| 332 if (!provider_host) { |
| 333 BadMessageReceived(); |
| 334 return; |
| 335 } |
| 336 if (!provider_host->IsContextAlive()) { |
| 337 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
| 338 thread_id, |
| 339 request_id, |
| 340 blink::WebServiceWorkerError::ErrorTypeAbort, |
| 341 base::ASCIIToUTF16(kShutdownErrorMessage))); |
| 342 return; |
| 343 } |
| 344 |
| 345 if (!CanGetRegistration(provider_host->document_url(), document_url)) { |
| 346 BadMessageReceived(); |
| 347 return; |
| 348 } |
| 349 |
| 350 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 351 if (GetContext()->storage()->IsDisabled()) { |
| 352 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); |
| 353 return; |
| 354 } |
| 355 |
| 356 TRACE_EVENT_ASYNC_BEGIN1( |
| 357 "ServiceWorker", |
| 358 "ServiceWorkerDispatcherHost::GetRegistration", |
| 359 request_id, |
| 360 "Document URL", document_url.spec()); |
| 361 |
| 362 GetContext()->storage()->FindRegistrationForDocument( |
| 363 document_url, |
| 364 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete, |
| 365 this, |
| 366 thread_id, |
| 367 provider_id, |
| 368 request_id)); |
| 369 } |
| 370 |
305 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( | 371 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
306 int handle_id, | 372 int handle_id, |
307 const base::string16& message, | 373 const base::string16& message, |
308 const std::vector<int>& sent_message_port_ids) { | 374 const std::vector<int>& sent_message_port_ids) { |
309 TRACE_EVENT0("ServiceWorker", | 375 TRACE_EVENT0("ServiceWorker", |
310 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); | 376 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); |
311 if (!GetContext()) | 377 if (!GetContext()) |
312 return; | 378 return; |
313 | 379 |
314 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 380 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); | 448 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); |
383 DCHECK(handle); | 449 DCHECK(handle); |
384 if (handle->provider_id() == provider_id && handle->registration() && | 450 if (handle->provider_id() == provider_id && handle->registration() && |
385 handle->registration()->id() == registration_id) { | 451 handle->registration()->id() == registration_id) { |
386 return handle; | 452 return handle; |
387 } | 453 } |
388 } | 454 } |
389 return NULL; | 455 return NULL; |
390 } | 456 } |
391 | 457 |
| 458 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( |
| 459 int provider_id, |
| 460 ServiceWorkerRegistration* registration, |
| 461 ServiceWorkerRegistrationObjectInfo* info, |
| 462 ServiceWorkerVersionAttributes* attrs) { |
| 463 ServiceWorkerRegistrationHandle* handle = |
| 464 GetOrCreateRegistrationHandle(provider_id, registration); |
| 465 *info = handle->GetObjectInfo(); |
| 466 |
| 467 attrs->installing = handle->CreateServiceWorkerHandleAndPass( |
| 468 registration->installing_version()); |
| 469 attrs->waiting = handle->CreateServiceWorkerHandleAndPass( |
| 470 registration->waiting_version()); |
| 471 attrs->active = handle->CreateServiceWorkerHandleAndPass( |
| 472 registration->active_version()); |
| 473 } |
| 474 |
392 void ServiceWorkerDispatcherHost::RegistrationComplete( | 475 void ServiceWorkerDispatcherHost::RegistrationComplete( |
393 int thread_id, | 476 int thread_id, |
394 int provider_id, | 477 int provider_id, |
395 int request_id, | 478 int request_id, |
396 ServiceWorkerStatusCode status, | 479 ServiceWorkerStatusCode status, |
397 int64 registration_id, | 480 int64 registration_id, |
398 int64 version_id) { | 481 int64 version_id) { |
399 if (!GetContext()) | 482 if (!GetContext()) |
400 return; | 483 return; |
401 | 484 |
402 if (status != SERVICE_WORKER_OK) { | 485 if (status != SERVICE_WORKER_OK) { |
403 SendRegistrationError(thread_id, request_id, status); | 486 SendRegistrationError(thread_id, request_id, status); |
404 return; | 487 return; |
405 } | 488 } |
406 | 489 |
407 ServiceWorkerRegistration* registration = | 490 ServiceWorkerRegistration* registration = |
408 GetContext()->GetLiveRegistration(registration_id); | 491 GetContext()->GetLiveRegistration(registration_id); |
409 DCHECK(registration); | 492 DCHECK(registration); |
410 | 493 |
411 ServiceWorkerRegistrationHandle* handle = | 494 ServiceWorkerRegistrationObjectInfo info; |
412 GetOrCreateRegistrationHandle(provider_id, registration); | |
413 | |
414 ServiceWorkerVersionAttributes attrs; | 495 ServiceWorkerVersionAttributes attrs; |
415 attrs.installing = handle->CreateServiceWorkerHandleAndPass( | 496 GetRegistrationObjectInfoAndVersionAttributes( |
416 registration->installing_version()); | 497 provider_id, registration, &info, &attrs); |
417 attrs.waiting = handle->CreateServiceWorkerHandleAndPass( | |
418 registration->waiting_version()); | |
419 attrs.active = handle->CreateServiceWorkerHandleAndPass( | |
420 registration->active_version()); | |
421 | 498 |
422 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( | 499 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( |
423 thread_id, request_id, handle->GetObjectInfo(), attrs)); | 500 thread_id, request_id, info, attrs)); |
424 TRACE_EVENT_ASYNC_END2("ServiceWorker", | 501 TRACE_EVENT_ASYNC_END2("ServiceWorker", |
425 "ServiceWorkerDispatcherHost::RegisterServiceWorker", | 502 "ServiceWorkerDispatcherHost::RegisterServiceWorker", |
426 request_id, | 503 request_id, |
427 "Registration ID", registration_id, | 504 "Registration ID", registration_id, |
428 "Version ID", version_id); | 505 "Version ID", version_id); |
429 } | 506 } |
430 | 507 |
431 void ServiceWorkerDispatcherHost::OnWorkerReadyForInspection( | 508 void ServiceWorkerDispatcherHost::OnWorkerReadyForInspection( |
432 int embedded_worker_id) { | 509 int embedded_worker_id) { |
433 TRACE_EVENT0("ServiceWorker", | 510 TRACE_EVENT0("ServiceWorker", |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, | 679 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, |
603 request_id, | 680 request_id, |
604 is_success)); | 681 is_success)); |
605 TRACE_EVENT_ASYNC_END1( | 682 TRACE_EVENT_ASYNC_END1( |
606 "ServiceWorker", | 683 "ServiceWorker", |
607 "ServiceWorkerDispatcherHost::UnregisterServiceWorker", | 684 "ServiceWorkerDispatcherHost::UnregisterServiceWorker", |
608 request_id, | 685 request_id, |
609 "Status", status); | 686 "Status", status); |
610 } | 687 } |
611 | 688 |
| 689 void ServiceWorkerDispatcherHost::GetRegistrationComplete( |
| 690 int thread_id, |
| 691 int provider_id, |
| 692 int request_id, |
| 693 ServiceWorkerStatusCode status, |
| 694 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 695 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
| 696 "ServiceWorkerDispatcherHost::GetRegistration", |
| 697 request_id, |
| 698 "Registration ID", |
| 699 registration.get() ? registration->id() |
| 700 : kInvalidServiceWorkerRegistrationId); |
| 701 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 702 SendGetRegistrationError(thread_id, request_id, status); |
| 703 return; |
| 704 } |
| 705 |
| 706 ServiceWorkerRegistrationObjectInfo info; |
| 707 ServiceWorkerVersionAttributes attrs; |
| 708 if (status == SERVICE_WORKER_OK) { |
| 709 DCHECK(registration.get()); |
| 710 if (!registration->is_uninstalling()) { |
| 711 GetRegistrationObjectInfoAndVersionAttributes( |
| 712 provider_id, registration.get(), &info, &attrs); |
| 713 } |
| 714 } |
| 715 |
| 716 Send(new ServiceWorkerMsg_DidGetRegistration( |
| 717 thread_id, request_id, info, attrs)); |
| 718 } |
| 719 |
612 void ServiceWorkerDispatcherHost::SendRegistrationError( | 720 void ServiceWorkerDispatcherHost::SendRegistrationError( |
613 int thread_id, | 721 int thread_id, |
614 int request_id, | 722 int request_id, |
615 ServiceWorkerStatusCode status) { | 723 ServiceWorkerStatusCode status) { |
616 base::string16 error_message; | 724 base::string16 error_message; |
617 blink::WebServiceWorkerError::ErrorType error_type; | 725 blink::WebServiceWorkerError::ErrorType error_type; |
618 GetServiceWorkerRegistrationStatusResponse( | 726 GetServiceWorkerRegistrationStatusResponse( |
619 status, &error_type, &error_message); | 727 status, &error_type, &error_message); |
620 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 728 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
621 thread_id, request_id, error_type, error_message)); | 729 thread_id, request_id, error_type, error_message)); |
622 } | 730 } |
623 | 731 |
624 void ServiceWorkerDispatcherHost::SendUnregistrationError( | 732 void ServiceWorkerDispatcherHost::SendUnregistrationError( |
625 int thread_id, | 733 int thread_id, |
626 int request_id, | 734 int request_id, |
627 ServiceWorkerStatusCode status) { | 735 ServiceWorkerStatusCode status) { |
628 base::string16 error_message; | 736 base::string16 error_message; |
629 blink::WebServiceWorkerError::ErrorType error_type; | 737 blink::WebServiceWorkerError::ErrorType error_type; |
630 GetServiceWorkerRegistrationStatusResponse( | 738 GetServiceWorkerRegistrationStatusResponse( |
631 status, &error_type, &error_message); | 739 status, &error_type, &error_message); |
632 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( | 740 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( |
633 thread_id, request_id, error_type, error_message)); | 741 thread_id, request_id, error_type, error_message)); |
634 } | 742 } |
635 | 743 |
| 744 void ServiceWorkerDispatcherHost::SendGetRegistrationError( |
| 745 int thread_id, |
| 746 int request_id, |
| 747 ServiceWorkerStatusCode status) { |
| 748 base::string16 error_message; |
| 749 blink::WebServiceWorkerError::ErrorType error_type; |
| 750 GetServiceWorkerRegistrationStatusResponse( |
| 751 status, &error_type, &error_message); |
| 752 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
| 753 thread_id, request_id, error_type, error_message)); |
| 754 } |
| 755 |
636 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { | 756 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
637 return context_wrapper_->context(); | 757 return context_wrapper_->context(); |
638 } | 758 } |
639 | 759 |
640 } // namespace content | 760 } // namespace content |
OLD | NEW |