Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: content/browser/service_worker/service_worker_dispatcher_host.cc

Issue 1146913004: Service Worker: Add ServiceWorkerContainer.getRegistrations() method. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update tools/metrics/histograms/histograms.xml. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 bool ServiceWorkerDispatcherHost::OnMessageReceived( 155 bool ServiceWorkerDispatcherHost::OnMessageReceived(
156 const IPC::Message& message) { 156 const IPC::Message& message) {
157 bool handled = true; 157 bool handled = true;
158 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) 158 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message)
159 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, 159 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker,
160 OnRegisterServiceWorker) 160 OnRegisterServiceWorker)
161 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, 161 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker,
162 OnUnregisterServiceWorker) 162 OnUnregisterServiceWorker)
163 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, 163 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration,
164 OnGetRegistration) 164 OnGetRegistration)
165 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrations,
166 OnGetRegistrations)
165 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, 167 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady,
166 OnGetRegistrationForReady) 168 OnGetRegistrationForReady)
167 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, 169 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated,
168 OnProviderCreated) 170 OnProviderCreated)
169 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, 171 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed,
170 OnProviderDestroyed) 172 OnProviderDestroyed)
171 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetVersionId, 173 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetVersionId,
172 OnSetHostedVersionId) 174 OnSetHostedVersionId)
173 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker, 175 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker,
174 OnPostMessageToWorker) 176 OnPostMessageToWorker)
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 request_id)); 434 request_id));
433 } 435 }
434 436
435 void ServiceWorkerDispatcherHost::OnGetRegistration( 437 void ServiceWorkerDispatcherHost::OnGetRegistration(
436 int thread_id, 438 int thread_id,
437 int request_id, 439 int request_id,
438 int provider_id, 440 int provider_id,
439 const GURL& document_url) { 441 const GURL& document_url) {
440 TRACE_EVENT0("ServiceWorker", 442 TRACE_EVENT0("ServiceWorker",
441 "ServiceWorkerDispatcherHost::OnGetRegistration"); 443 "ServiceWorkerDispatcherHost::OnGetRegistration");
444
445 DCHECK_CURRENTLY_ON(BrowserThread::IO);
446
442 if (!GetContext()) { 447 if (!GetContext()) {
443 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( 448 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
444 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort, 449 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort,
445 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + 450 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) +
446 base::ASCIIToUTF16(kShutdownErrorMessage))); 451 base::ASCIIToUTF16(kShutdownErrorMessage)));
447 return; 452 return;
448 } 453 }
449 if (!document_url.is_valid()) { 454 if (!document_url.is_valid()) {
450 bad_message::ReceivedBadMessage(this, 455 bad_message::ReceivedBadMessage(this,
451 bad_message::SWDH_GET_REGISTRATION_BAD_URL); 456 bad_message::SWDH_GET_REGISTRATION_BAD_URL);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 if (!GetContentClient()->browser()->AllowServiceWorker( 490 if (!GetContentClient()->browser()->AllowServiceWorker(
486 provider_host->document_url(), provider_host->topmost_frame_url(), 491 provider_host->document_url(), provider_host->topmost_frame_url(),
487 resource_context_, render_process_id_, provider_host->frame_id())) { 492 resource_context_, render_process_id_, provider_host->frame_id())) {
488 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( 493 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
489 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, 494 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown,
490 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + 495 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) +
491 base::ASCIIToUTF16(kUserDeniedPermissionMessage))); 496 base::ASCIIToUTF16(kUserDeniedPermissionMessage)));
492 return; 497 return;
493 } 498 }
494 499
495 DCHECK_CURRENTLY_ON(BrowserThread::IO);
496 if (GetContext()->storage()->IsDisabled()) { 500 if (GetContext()->storage()->IsDisabled()) {
497 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); 501 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT);
498 return; 502 return;
499 } 503 }
500 504
501 TRACE_EVENT_ASYNC_BEGIN1( 505 TRACE_EVENT_ASYNC_BEGIN1(
502 "ServiceWorker", 506 "ServiceWorker",
503 "ServiceWorkerDispatcherHost::GetRegistration", 507 "ServiceWorkerDispatcherHost::GetRegistration",
504 request_id, 508 request_id,
505 "Document URL", document_url.spec()); 509 "Document URL", document_url.spec());
506 510
507 GetContext()->storage()->FindRegistrationForDocument( 511 GetContext()->storage()->FindRegistrationForDocument(
508 document_url, 512 document_url,
509 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete, 513 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete,
510 this, 514 this,
511 thread_id, 515 thread_id,
512 provider_id, 516 provider_id,
513 request_id)); 517 request_id));
514 } 518 }
515 519
520 void ServiceWorkerDispatcherHost::OnGetRegistrations(int thread_id,
521 int request_id,
522 int provider_id) {
523 DCHECK_CURRENTLY_ON(BrowserThread::IO);
524
525 if (!GetContext()) {
526 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError(
527 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort,
528 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) +
529 base::ASCIIToUTF16(kShutdownErrorMessage)));
530 return;
531 }
532
533 ServiceWorkerProviderHost* provider_host =
534 GetContext()->GetProviderHost(render_process_id_, provider_id);
535 if (!provider_host) {
536 bad_message::ReceivedBadMessage(
537 this, bad_message::SWDH_GET_REGISTRATIONS_NO_HOST);
538 return;
539 }
540 if (!provider_host->IsContextAlive()) {
541 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError(
542 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort,
543 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) +
544 base::ASCIIToUTF16(kShutdownErrorMessage)));
545 return;
546 }
547
548 // TODO(jungkees): This check can be removed once crbug.com/439697 is fixed.
549 if (provider_host->document_url().is_empty()) {
550 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError(
551 thread_id, request_id, WebServiceWorkerError::ErrorTypeSecurity,
552 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) +
553 base::ASCIIToUTF16(kNoDocumentURLErrorMessage)));
554 return;
555 }
556
557 if (!OriginCanAccessServiceWorkers(provider_host->document_url())) {
558 bad_message::ReceivedBadMessage(
559 this, bad_message::SWDH_GET_REGISTRATIONS_INVALID_ORIGIN);
560 return;
561 }
562
563 if (!GetContentClient()->browser()->AllowServiceWorker(
564 provider_host->document_url(), provider_host->topmost_frame_url(),
565 resource_context_, render_process_id_, provider_host->frame_id())) {
566 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError(
567 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown,
568 base::ASCIIToUTF16(kServiceWorkerGetRegistrationsErrorPrefix) +
569 base::ASCIIToUTF16(kUserDeniedPermissionMessage)));
570 return;
571 }
572
573 if (GetContext()->storage()->IsDisabled()) {
574 SendGetRegistrationsError(thread_id, request_id,
575 SERVICE_WORKER_ERROR_ABORT);
576 return;
577 }
578
579 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker",
580 "ServiceWorkerDispatcherHost::GetRegistrations",
581 request_id);
582
583 GetContext()->storage()->GetRegistrationsForOrigin(
584 provider_host->document_url().GetOrigin(),
585 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationsComplete, this,
586 thread_id, provider_id, request_id));
587 }
588
516 void ServiceWorkerDispatcherHost::OnGetRegistrationForReady( 589 void ServiceWorkerDispatcherHost::OnGetRegistrationForReady(
517 int thread_id, 590 int thread_id,
518 int request_id, 591 int request_id,
519 int provider_id) { 592 int provider_id) {
520 TRACE_EVENT0("ServiceWorker", 593 TRACE_EVENT0("ServiceWorker",
521 "ServiceWorkerDispatcherHost::OnGetRegistrationForReady"); 594 "ServiceWorkerDispatcherHost::OnGetRegistrationForReady");
522 if (!GetContext()) 595 if (!GetContext())
523 return; 596 return;
524 ServiceWorkerProviderHost* provider_host = 597 ServiceWorkerProviderHost* provider_host =
525 GetContext()->GetProviderHost(render_process_id_, provider_id); 598 GetContext()->GetProviderHost(render_process_id_, provider_id);
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 if (!registration->is_uninstalling()) { 1038 if (!registration->is_uninstalling()) {
966 GetRegistrationObjectInfoAndVersionAttributes( 1039 GetRegistrationObjectInfoAndVersionAttributes(
967 provider_host->AsWeakPtr(), registration.get(), &info, &attrs); 1040 provider_host->AsWeakPtr(), registration.get(), &info, &attrs);
968 } 1041 }
969 } 1042 }
970 1043
971 Send(new ServiceWorkerMsg_DidGetRegistration( 1044 Send(new ServiceWorkerMsg_DidGetRegistration(
972 thread_id, request_id, info, attrs)); 1045 thread_id, request_id, info, attrs));
973 } 1046 }
974 1047
1048 void ServiceWorkerDispatcherHost::GetRegistrationsComplete(
1049 int thread_id,
1050 int provider_id,
1051 int request_id,
1052 const std::vector<scoped_refptr<ServiceWorkerRegistration>>&
1053 registrations) {
1054 TRACE_EVENT_ASYNC_END0("ServiceWorker",
1055 "ServiceWorkerDispatcherHost::GetRegistrations",
1056 request_id);
1057 if (!GetContext())
1058 return;
1059
1060 ServiceWorkerProviderHost* provider_host =
1061 GetContext()->GetProviderHost(render_process_id_, provider_id);
1062 if (!provider_host)
1063 return; // The provider has already been destroyed.
1064
1065 std::vector<ServiceWorkerRegistrationObjectInfo> object_infos;
1066 std::vector<ServiceWorkerVersionAttributes> version_attrs;
1067
1068 for (const auto& registration : registrations) {
1069 DCHECK(registration.get());
1070 if (!registration->is_uninstalling()) {
1071 ServiceWorkerRegistrationObjectInfo object_info;
1072 ServiceWorkerVersionAttributes version_attr;
1073 GetRegistrationObjectInfoAndVersionAttributes(
1074 provider_host->AsWeakPtr(), registration.get(), &object_info,
1075 &version_attr);
1076 object_infos.push_back(object_info);
1077 version_attrs.push_back(version_attr);
1078 }
1079 }
1080
1081 Send(new ServiceWorkerMsg_DidGetRegistrations(thread_id, request_id,
1082 object_infos, version_attrs));
1083 }
1084
975 void ServiceWorkerDispatcherHost::GetRegistrationForReadyComplete( 1085 void ServiceWorkerDispatcherHost::GetRegistrationForReadyComplete(
976 int thread_id, 1086 int thread_id,
977 int request_id, 1087 int request_id,
978 base::WeakPtr<ServiceWorkerProviderHost> provider_host, 1088 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
979 ServiceWorkerRegistration* registration) { 1089 ServiceWorkerRegistration* registration) {
980 DCHECK(registration); 1090 DCHECK(registration);
981 TRACE_EVENT_ASYNC_END1("ServiceWorker", 1091 TRACE_EVENT_ASYNC_END1("ServiceWorker",
982 "ServiceWorkerDispatcherHost::GetRegistrationForReady", 1092 "ServiceWorkerDispatcherHost::GetRegistrationForReady",
983 request_id, 1093 request_id,
984 "Registration ID", 1094 "Registration ID",
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1030 base::string16 error_message; 1140 base::string16 error_message;
1031 blink::WebServiceWorkerError::ErrorType error_type; 1141 blink::WebServiceWorkerError::ErrorType error_type;
1032 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type, 1142 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type,
1033 &error_message); 1143 &error_message);
1034 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( 1144 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
1035 thread_id, request_id, error_type, 1145 thread_id, request_id, error_type,
1036 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + 1146 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) +
1037 error_message)); 1147 error_message));
1038 } 1148 }
1039 1149
1150 void ServiceWorkerDispatcherHost::SendGetRegistrationsError(
1151 int thread_id,
1152 int request_id,
1153 ServiceWorkerStatusCode status) {
1154 base::string16 error_message;
1155 blink::WebServiceWorkerError::ErrorType error_type;
1156 GetServiceWorkerRegistrationStatusResponse(status, std::string(), &error_type,
1157 &error_message);
1158 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationsError(
1159 thread_id, request_id, error_type,
1160 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) +
1161 error_message));
1162 }
1163
1040 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { 1164 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() {
1041 if (!context_wrapper_.get()) 1165 if (!context_wrapper_.get())
1042 return nullptr; 1166 return nullptr;
1043 return context_wrapper_->context(); 1167 return context_wrapper_->context();
1044 } 1168 }
1045 1169
1046 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { 1170 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) {
1047 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); 1171 ServiceWorkerHandle* handle = handles_.Lookup(handle_id);
1048 if (!handle) { 1172 if (!handle) {
1049 bad_message::ReceivedBadMessage(this, 1173 bad_message::ReceivedBadMessage(this,
1050 bad_message::SWDH_TERMINATE_BAD_HANDLE); 1174 bad_message::SWDH_TERMINATE_BAD_HANDLE);
1051 return; 1175 return;
1052 } 1176 }
1053 handle->version()->StopWorker( 1177 handle->version()->StopWorker(
1054 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 1178 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
1055 } 1179 }
1056 1180
1057 } // namespace content 1181 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698