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/profiler/scoped_tracker.h" | 8 #include "base/profiler/scoped_tracker.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 bool ServiceWorkerDispatcherHost::OnMessageReceived( | 154 bool ServiceWorkerDispatcherHost::OnMessageReceived( |
155 const IPC::Message& message) { | 155 const IPC::Message& message) { |
156 bool handled = true; | 156 bool handled = true; |
157 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) | 157 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) |
158 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, | 158 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, |
159 OnRegisterServiceWorker) | 159 OnRegisterServiceWorker) |
160 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 160 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
161 OnUnregisterServiceWorker) | 161 OnUnregisterServiceWorker) |
162 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, | 162 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, |
163 OnGetRegistration) | 163 OnGetRegistration) |
| 164 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrations, |
| 165 OnGetRegistrations) |
164 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, | 166 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, |
165 OnGetRegistrationForReady) | 167 OnGetRegistrationForReady) |
166 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, | 168 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
167 OnProviderCreated) | 169 OnProviderCreated) |
168 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, | 170 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, |
169 OnProviderDestroyed) | 171 OnProviderDestroyed) |
170 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetVersionId, | 172 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetVersionId, |
171 OnSetHostedVersionId) | 173 OnSetHostedVersionId) |
172 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker, | 174 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker, |
173 OnPostMessageToWorker) | 175 OnPostMessageToWorker) |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 request_id)); | 433 request_id)); |
432 } | 434 } |
433 | 435 |
434 void ServiceWorkerDispatcherHost::OnGetRegistration( | 436 void ServiceWorkerDispatcherHost::OnGetRegistration( |
435 int thread_id, | 437 int thread_id, |
436 int request_id, | 438 int request_id, |
437 int provider_id, | 439 int provider_id, |
438 const GURL& document_url) { | 440 const GURL& document_url) { |
439 TRACE_EVENT0("ServiceWorker", | 441 TRACE_EVENT0("ServiceWorker", |
440 "ServiceWorkerDispatcherHost::OnGetRegistration"); | 442 "ServiceWorkerDispatcherHost::OnGetRegistration"); |
| 443 |
| 444 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 445 |
441 if (!GetContext()) { | 446 if (!GetContext()) { |
442 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( | 447 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
443 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort, | 448 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort, |
444 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + | 449 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + |
445 base::ASCIIToUTF16(kShutdownErrorMessage))); | 450 base::ASCIIToUTF16(kShutdownErrorMessage))); |
446 return; | 451 return; |
447 } | 452 } |
448 if (!document_url.is_valid()) { | 453 if (!document_url.is_valid()) { |
449 BadMessageReceived(); | 454 BadMessageReceived(); |
450 return; | 455 return; |
(...skipping 30 matching lines...) Expand all Loading... |
481 if (!GetContentClient()->browser()->AllowServiceWorker( | 486 if (!GetContentClient()->browser()->AllowServiceWorker( |
482 provider_host->document_url(), provider_host->topmost_frame_url(), | 487 provider_host->document_url(), provider_host->topmost_frame_url(), |
483 resource_context_, render_process_id_, provider_host->frame_id())) { | 488 resource_context_, render_process_id_, provider_host->frame_id())) { |
484 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( | 489 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
485 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, | 490 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, |
486 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + | 491 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + |
487 base::ASCIIToUTF16(kUserDeniedPermissionMessage))); | 492 base::ASCIIToUTF16(kUserDeniedPermissionMessage))); |
488 return; | 493 return; |
489 } | 494 } |
490 | 495 |
491 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
492 if (GetContext()->storage()->IsDisabled()) { | 496 if (GetContext()->storage()->IsDisabled()) { |
493 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); | 497 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); |
494 return; | 498 return; |
495 } | 499 } |
496 | 500 |
497 TRACE_EVENT_ASYNC_BEGIN1( | 501 TRACE_EVENT_ASYNC_BEGIN1( |
498 "ServiceWorker", | 502 "ServiceWorker", |
499 "ServiceWorkerDispatcherHost::GetRegistration", | 503 "ServiceWorkerDispatcherHost::GetRegistration", |
500 request_id, | 504 request_id, |
501 "Document URL", document_url.spec()); | 505 "Document URL", document_url.spec()); |
502 | 506 |
503 GetContext()->storage()->FindRegistrationForDocument( | 507 GetContext()->storage()->FindRegistrationForDocument( |
504 document_url, | 508 document_url, |
505 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete, | 509 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete, |
506 this, | 510 this, |
507 thread_id, | 511 thread_id, |
508 provider_id, | 512 provider_id, |
509 request_id)); | 513 request_id)); |
510 } | 514 } |
511 | 515 |
| 516 void ServiceWorkerDispatcherHost::OnGetRegistrations(int thread_id, |
| 517 int request_id, |
| 518 int provider_id) { |
| 519 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 520 |
| 521 if (!GetContext()) { |
| 522 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError( |
| 523 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort, |
| 524 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) + |
| 525 base::ASCIIToUTF16(kShutdownErrorMessage))); |
| 526 return; |
| 527 } |
| 528 |
| 529 ServiceWorkerProviderHost* provider_host = |
| 530 GetContext()->GetProviderHost(render_process_id_, provider_id); |
| 531 if (!provider_host) { |
| 532 BadMessageReceived(); |
| 533 return; |
| 534 } |
| 535 if (!provider_host->IsContextAlive()) { |
| 536 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError( |
| 537 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort, |
| 538 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) + |
| 539 base::ASCIIToUTF16(kShutdownErrorMessage))); |
| 540 return; |
| 541 } |
| 542 |
| 543 // TODO(jungkees): This check can be removed once crbug.com/439697 is fixed. |
| 544 if (provider_host->document_url().is_empty()) { |
| 545 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError( |
| 546 thread_id, request_id, WebServiceWorkerError::ErrorTypeSecurity, |
| 547 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) + |
| 548 base::ASCIIToUTF16(kNoDocumentURLErrorMessage))); |
| 549 return; |
| 550 } |
| 551 |
| 552 if (!OriginCanAccessServiceWorkers(provider_host->document_url())) { |
| 553 BadMessageReceived(); |
| 554 return; |
| 555 } |
| 556 |
| 557 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 558 provider_host->document_url(), provider_host->topmost_frame_url(), |
| 559 resource_context_, render_process_id_, provider_host->frame_id())) { |
| 560 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError( |
| 561 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, |
| 562 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) + |
| 563 base::ASCIIToUTF16(kUserDeniedPermissionMessage))); |
| 564 return; |
| 565 } |
| 566 |
| 567 if (GetContext()->storage()->IsDisabled()) { |
| 568 SendGetRegistrationsError(thread_id, request_id, |
| 569 SERVICE_WORKER_ERROR_ABORT); |
| 570 return; |
| 571 } |
| 572 |
| 573 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", |
| 574 "ServiceWorkerDispatcherHost::GetRegistrations", |
| 575 request_id); |
| 576 |
| 577 GetContext()->storage()->GetRegistrationsForOrigin( |
| 578 provider_host->document_url().GetOrigin(), |
| 579 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationsComplete, this, |
| 580 thread_id, provider_id, request_id)); |
| 581 } |
| 582 |
512 void ServiceWorkerDispatcherHost::OnGetRegistrationForReady( | 583 void ServiceWorkerDispatcherHost::OnGetRegistrationForReady( |
513 int thread_id, | 584 int thread_id, |
514 int request_id, | 585 int request_id, |
515 int provider_id) { | 586 int provider_id) { |
516 TRACE_EVENT0("ServiceWorker", | 587 TRACE_EVENT0("ServiceWorker", |
517 "ServiceWorkerDispatcherHost::OnGetRegistrationForReady"); | 588 "ServiceWorkerDispatcherHost::OnGetRegistrationForReady"); |
518 if (!GetContext()) | 589 if (!GetContext()) |
519 return; | 590 return; |
520 ServiceWorkerProviderHost* provider_host = | 591 ServiceWorkerProviderHost* provider_host = |
521 GetContext()->GetProviderHost(render_process_id_, provider_id); | 592 GetContext()->GetProviderHost(render_process_id_, provider_id); |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 if (!registration->is_uninstalling()) { | 1022 if (!registration->is_uninstalling()) { |
952 GetRegistrationObjectInfoAndVersionAttributes( | 1023 GetRegistrationObjectInfoAndVersionAttributes( |
953 provider_host->AsWeakPtr(), registration.get(), &info, &attrs); | 1024 provider_host->AsWeakPtr(), registration.get(), &info, &attrs); |
954 } | 1025 } |
955 } | 1026 } |
956 | 1027 |
957 Send(new ServiceWorkerMsg_DidGetRegistration( | 1028 Send(new ServiceWorkerMsg_DidGetRegistration( |
958 thread_id, request_id, info, attrs)); | 1029 thread_id, request_id, info, attrs)); |
959 } | 1030 } |
960 | 1031 |
| 1032 void ServiceWorkerDispatcherHost::GetRegistrationsComplete( |
| 1033 int thread_id, |
| 1034 int provider_id, |
| 1035 int request_id, |
| 1036 const std::vector<ServiceWorkerRegistration*>& registrations) { |
| 1037 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 1038 "ServiceWorkerDispatcherHost::GetRegistrations", |
| 1039 request_id); |
| 1040 if (!GetContext()) |
| 1041 return; |
| 1042 |
| 1043 ServiceWorkerProviderHost* provider_host = |
| 1044 GetContext()->GetProviderHost(render_process_id_, provider_id); |
| 1045 if (!provider_host) |
| 1046 return; // The provider has already been destroyed. |
| 1047 |
| 1048 std::vector<ServiceWorkerRegistrationObjectInfo> object_infos; |
| 1049 std::vector<ServiceWorkerVersionAttributes> version_attrs; |
| 1050 |
| 1051 for (const auto& registration : registrations) { |
| 1052 DCHECK(registration); |
| 1053 if (!registration->is_uninstalling()) { |
| 1054 ServiceWorkerRegistrationObjectInfo object_info; |
| 1055 ServiceWorkerVersionAttributes version_attr; |
| 1056 GetRegistrationObjectInfoAndVersionAttributes(provider_host->AsWeakPtr(), |
| 1057 registration, &object_info, |
| 1058 &version_attr); |
| 1059 object_infos.push_back(object_info); |
| 1060 version_attrs.push_back(version_attr); |
| 1061 } |
| 1062 } |
| 1063 |
| 1064 Send(new ServiceWorkerMsg_DidGetRegistrations(thread_id, request_id, |
| 1065 object_infos, version_attrs)); |
| 1066 } |
| 1067 |
961 void ServiceWorkerDispatcherHost::GetRegistrationForReadyComplete( | 1068 void ServiceWorkerDispatcherHost::GetRegistrationForReadyComplete( |
962 int thread_id, | 1069 int thread_id, |
963 int request_id, | 1070 int request_id, |
964 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 1071 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
965 ServiceWorkerRegistration* registration) { | 1072 ServiceWorkerRegistration* registration) { |
966 DCHECK(registration); | 1073 DCHECK(registration); |
967 TRACE_EVENT_ASYNC_END1("ServiceWorker", | 1074 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
968 "ServiceWorkerDispatcherHost::GetRegistrationForReady", | 1075 "ServiceWorkerDispatcherHost::GetRegistrationForReady", |
969 request_id, | 1076 request_id, |
970 "Registration ID", | 1077 "Registration ID", |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1016 base::string16 error_message; | 1123 base::string16 error_message; |
1017 blink::WebServiceWorkerError::ErrorType error_type; | 1124 blink::WebServiceWorkerError::ErrorType error_type; |
1018 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type, | 1125 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type, |
1019 &error_message); | 1126 &error_message); |
1020 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( | 1127 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
1021 thread_id, request_id, error_type, | 1128 thread_id, request_id, error_type, |
1022 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + | 1129 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + |
1023 error_message)); | 1130 error_message)); |
1024 } | 1131 } |
1025 | 1132 |
| 1133 void ServiceWorkerDispatcherHost::SendGetRegistrationsError( |
| 1134 int thread_id, |
| 1135 int request_id, |
| 1136 ServiceWorkerStatusCode status) { |
| 1137 base::string16 error_message; |
| 1138 blink::WebServiceWorkerError::ErrorType error_type; |
| 1139 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type, |
| 1140 &error_message); |
| 1141 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError( |
| 1142 thread_id, request_id, error_type, |
| 1143 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + |
| 1144 error_message)); |
| 1145 } |
| 1146 |
1026 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { | 1147 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
1027 if (!context_wrapper_.get()) | 1148 if (!context_wrapper_.get()) |
1028 return nullptr; | 1149 return nullptr; |
1029 return context_wrapper_->context(); | 1150 return context_wrapper_->context(); |
1030 } | 1151 } |
1031 | 1152 |
1032 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { | 1153 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { |
1033 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 1154 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
1034 if (!handle) { | 1155 if (!handle) { |
1035 BadMessageReceived(); | 1156 BadMessageReceived(); |
1036 return; | 1157 return; |
1037 } | 1158 } |
1038 handle->version()->StopWorker( | 1159 handle->version()->StopWorker( |
1039 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1160 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
1040 } | 1161 } |
1041 | 1162 |
1042 } // namespace content | 1163 } // namespace content |
OLD | NEW |