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 (!GetContentClient()->browser()->AllowServiceWorker( | |
553 provider_host->document_url(), provider_host->topmost_frame_url(), | |
554 resource_context_, render_process_id_, provider_host->frame_id())) { | |
555 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError( | |
556 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, | |
557 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) + | |
558 base::ASCIIToUTF16(kUserDeniedPermissionMessage))); | |
559 return; | |
560 } | |
561 | |
562 if (GetContext()->storage()->IsDisabled()) { | |
563 SendGetRegistrationsError(thread_id, request_id, | |
564 SERVICE_WORKER_ERROR_ABORT); | |
565 return; | |
566 } | |
567 | |
568 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", | |
569 "ServiceWorkerDispatcherHost::GetRegistrations", | |
570 request_id); | |
571 | |
572 GetContext()->storage()->GetRegistrationsForOrigin( | |
573 provider_host->document_url().GetOrigin(), | |
574 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationsComplete, this, | |
575 thread_id, provider_id, request_id)); | |
576 } | |
577 | |
512 void ServiceWorkerDispatcherHost::OnGetRegistrationForReady( | 578 void ServiceWorkerDispatcherHost::OnGetRegistrationForReady( |
513 int thread_id, | 579 int thread_id, |
514 int request_id, | 580 int request_id, |
515 int provider_id) { | 581 int provider_id) { |
516 TRACE_EVENT0("ServiceWorker", | 582 TRACE_EVENT0("ServiceWorker", |
517 "ServiceWorkerDispatcherHost::OnGetRegistrationForReady"); | 583 "ServiceWorkerDispatcherHost::OnGetRegistrationForReady"); |
518 if (!GetContext()) | 584 if (!GetContext()) |
519 return; | 585 return; |
520 ServiceWorkerProviderHost* provider_host = | 586 ServiceWorkerProviderHost* provider_host = |
521 GetContext()->GetProviderHost(render_process_id_, provider_id); | 587 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()) { | 1017 if (!registration->is_uninstalling()) { |
952 GetRegistrationObjectInfoAndVersionAttributes( | 1018 GetRegistrationObjectInfoAndVersionAttributes( |
953 provider_host->AsWeakPtr(), registration.get(), &info, &attrs); | 1019 provider_host->AsWeakPtr(), registration.get(), &info, &attrs); |
954 } | 1020 } |
955 } | 1021 } |
956 | 1022 |
957 Send(new ServiceWorkerMsg_DidGetRegistration( | 1023 Send(new ServiceWorkerMsg_DidGetRegistration( |
958 thread_id, request_id, info, attrs)); | 1024 thread_id, request_id, info, attrs)); |
959 } | 1025 } |
960 | 1026 |
1027 void ServiceWorkerDispatcherHost::GetRegistrationsComplete( | |
1028 int thread_id, | |
1029 int provider_id, | |
1030 int request_id, | |
1031 const std::vector<ServiceWorkerRegistrationInfo>& infos) { | |
1032 TRACE_EVENT_ASYNC_END0("ServiceWorker", | |
1033 "ServiceWorkerDispatcherHost::GetRegistrations", | |
1034 request_id); | |
1035 | |
1036 if (!GetContext()) | |
1037 return; | |
1038 | |
1039 ServiceWorkerProviderHost* provider_host = | |
1040 GetContext()->GetProviderHost(render_process_id_, provider_id); | |
1041 if (!provider_host) | |
1042 return; // The provider has already been destroyed. | |
1043 | |
1044 std::vector<ServiceWorkerRegistrationObjectInfo> object_infos; | |
1045 std::vector<ServiceWorkerVersionAttributes> version_attrs; | |
1046 | |
1047 for (size_t i = 0; i < infos.size(); ++i) { | |
1048 ServiceWorkerRegistration* registration = | |
1049 GetContext()->GetLiveRegistration(infos[i].registration_id); | |
michaeln
2015/05/21 20:33:30
What about registrations that are stored but not "
kinuko
2015/05/22 06:19:06
Yes, I think so. (Hadn't looked impl details yet)
jungkees
2015/05/22 13:26:44
Yes, I think I'd like to try something like:
// ca
| |
1050 | |
1051 if (registration && !registration->is_uninstalling()) { | |
1052 ServiceWorkerRegistrationObjectInfo object_info; | |
1053 ServiceWorkerVersionAttributes version_attr; | |
1054 GetRegistrationObjectInfoAndVersionAttributes(provider_host->AsWeakPtr(), | |
1055 registration, &object_info, | |
1056 &version_attr); | |
1057 object_infos.push_back(object_info); | |
1058 version_attrs.push_back(version_attr); | |
1059 } | |
1060 } | |
1061 | |
1062 Send(new ServiceWorkerMsg_DidGetRegistrations(thread_id, request_id, | |
1063 object_infos, version_attrs)); | |
1064 } | |
1065 | |
961 void ServiceWorkerDispatcherHost::GetRegistrationForReadyComplete( | 1066 void ServiceWorkerDispatcherHost::GetRegistrationForReadyComplete( |
962 int thread_id, | 1067 int thread_id, |
963 int request_id, | 1068 int request_id, |
964 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 1069 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
965 ServiceWorkerRegistration* registration) { | 1070 ServiceWorkerRegistration* registration) { |
966 DCHECK(registration); | 1071 DCHECK(registration); |
967 TRACE_EVENT_ASYNC_END1("ServiceWorker", | 1072 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
968 "ServiceWorkerDispatcherHost::GetRegistrationForReady", | 1073 "ServiceWorkerDispatcherHost::GetRegistrationForReady", |
969 request_id, | 1074 request_id, |
970 "Registration ID", | 1075 "Registration ID", |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1016 base::string16 error_message; | 1121 base::string16 error_message; |
1017 blink::WebServiceWorkerError::ErrorType error_type; | 1122 blink::WebServiceWorkerError::ErrorType error_type; |
1018 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type, | 1123 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type, |
1019 &error_message); | 1124 &error_message); |
1020 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( | 1125 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
1021 thread_id, request_id, error_type, | 1126 thread_id, request_id, error_type, |
1022 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + | 1127 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + |
1023 error_message)); | 1128 error_message)); |
1024 } | 1129 } |
1025 | 1130 |
1131 void ServiceWorkerDispatcherHost::SendGetRegistrationsError( | |
1132 int thread_id, | |
1133 int request_id, | |
1134 ServiceWorkerStatusCode status) { | |
1135 base::string16 error_message; | |
1136 blink::WebServiceWorkerError::ErrorType error_type; | |
1137 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type, | |
1138 &error_message); | |
1139 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError( | |
1140 thread_id, request_id, error_type, | |
1141 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + | |
1142 error_message)); | |
1143 } | |
1144 | |
1026 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { | 1145 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
1027 if (!context_wrapper_.get()) | 1146 if (!context_wrapper_.get()) |
1028 return nullptr; | 1147 return nullptr; |
1029 return context_wrapper_->context(); | 1148 return context_wrapper_->context(); |
1030 } | 1149 } |
1031 | 1150 |
1032 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { | 1151 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { |
1033 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 1152 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
1034 if (!handle) { | 1153 if (!handle) { |
1035 BadMessageReceived(); | 1154 BadMessageReceived(); |
1036 return; | 1155 return; |
1037 } | 1156 } |
1038 handle->version()->StopWorker( | 1157 handle->version()->StopWorker( |
1039 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1158 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
1040 } | 1159 } |
1041 | 1160 |
1042 } // namespace content | 1161 } // namespace content |
OLD | NEW |