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

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

Issue 535753002: ServiceWorker: Implement navigator.serviceWorker.getRegistration [2/3] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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/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
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
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 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 request_id, 305 request_id,
297 "Pattern", pattern.spec()); 306 "Pattern", pattern.spec());
298 GetContext()->UnregisterServiceWorker( 307 GetContext()->UnregisterServiceWorker(
299 pattern, 308 pattern,
300 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, 309 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete,
301 this, 310 this,
302 thread_id, 311 thread_id,
303 request_id)); 312 request_id));
304 } 313 }
305 314
315 void ServiceWorkerDispatcherHost::OnGetRegistration(
316 int thread_id,
317 int request_id,
318 int provider_id,
319 const GURL& document_url) {
320 TRACE_EVENT0("ServiceWorker",
321 "ServiceWorkerDispatcherHost::OnGetRegistration");
322 if (!GetContext()) {
323 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
324 thread_id,
325 request_id,
326 blink::WebServiceWorkerError::ErrorTypeAbort,
327 base::ASCIIToUTF16(kShutdownErrorMessage)));
328 return;
329 }
330
331 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost(
332 render_process_id_, provider_id);
333 if (!provider_host) {
334 BadMessageReceived();
335 return;
336 }
337 if (!provider_host->IsContextAlive()) {
338 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
339 thread_id,
340 request_id,
341 blink::WebServiceWorkerError::ErrorTypeAbort,
342 base::ASCIIToUTF16(kShutdownErrorMessage)));
343 return;
344 }
345
346 if (!CanGetRegistration(provider_host->document_url(), document_url)) {
347 BadMessageReceived();
348 return;
349 }
350
351 TRACE_EVENT_ASYNC_BEGIN1(
352 "ServiceWorker",
353 "ServiceWorkerDispatcherHost::GetRegistration",
354 request_id,
355 "Document URL", document_url.spec());
356 GetContext()->GetRegistration(
357 document_url,
358 base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete,
359 this,
360 thread_id,
361 provider_id,
362 request_id));
363 }
364
306 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( 365 void ServiceWorkerDispatcherHost::OnPostMessageToWorker(
307 int handle_id, 366 int handle_id,
308 const base::string16& message, 367 const base::string16& message,
309 const std::vector<int>& sent_message_port_ids) { 368 const std::vector<int>& sent_message_port_ids) {
310 TRACE_EVENT0("ServiceWorker", 369 TRACE_EVENT0("ServiceWorker",
311 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); 370 "ServiceWorkerDispatcherHost::OnPostMessageToWorker");
312 if (!GetContext()) 371 if (!GetContext())
313 return; 372 return;
314 373
315 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); 374 ServiceWorkerHandle* handle = handles_.Lookup(handle_id);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); 442 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue();
384 DCHECK(handle); 443 DCHECK(handle);
385 if (handle->provider_id() == provider_id && handle->registration() && 444 if (handle->provider_id() == provider_id && handle->registration() &&
386 handle->registration()->id() == registration_id) { 445 handle->registration()->id() == registration_id) {
387 return handle; 446 return handle;
388 } 447 }
389 } 448 }
390 return NULL; 449 return NULL;
391 } 450 }
392 451
452 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes(
453 int provider_id,
454 int64 registration_id,
455 ServiceWorkerRegistrationObjectInfo* info,
456 ServiceWorkerVersionAttributes* attrs) {
457 ServiceWorkerRegistration* registration =
458 GetContext()->GetLiveRegistration(registration_id);
459 DCHECK(registration);
460
461 ServiceWorkerRegistrationHandle* handle =
462 GetOrCreateRegistrationHandle(provider_id, registration);
463 *info = handle->GetObjectInfo();
464
465 attrs->installing = handle->CreateServiceWorkerHandleAndPass(
466 registration->installing_version());
467 attrs->waiting = handle->CreateServiceWorkerHandleAndPass(
468 registration->waiting_version());
469 attrs->active = handle->CreateServiceWorkerHandleAndPass(
470 registration->active_version());
471 }
472
393 void ServiceWorkerDispatcherHost::RegistrationComplete( 473 void ServiceWorkerDispatcherHost::RegistrationComplete(
394 int thread_id, 474 int thread_id,
395 int provider_id, 475 int provider_id,
396 int request_id, 476 int request_id,
397 ServiceWorkerStatusCode status, 477 ServiceWorkerStatusCode status,
398 int64 registration_id, 478 int64 registration_id,
399 int64 version_id) { 479 int64 version_id) {
400 if (!GetContext()) 480 if (!GetContext())
401 return; 481 return;
402 482
403 if (status != SERVICE_WORKER_OK) { 483 if (status != SERVICE_WORKER_OK) {
404 SendRegistrationError(thread_id, request_id, status); 484 SendRegistrationError(thread_id, request_id, status);
405 return; 485 return;
406 } 486 }
407 487
408 ServiceWorkerRegistration* registration = 488 ServiceWorkerRegistrationObjectInfo info;
409 GetContext()->GetLiveRegistration(registration_id);
410 DCHECK(registration);
411
412 ServiceWorkerRegistrationHandle* handle =
413 GetOrCreateRegistrationHandle(provider_id, registration);
414
415 ServiceWorkerVersionAttributes attrs; 489 ServiceWorkerVersionAttributes attrs;
416 attrs.installing = handle->CreateServiceWorkerHandleAndPass( 490 GetRegistrationObjectInfoAndVersionAttributes(
417 registration->installing_version()); 491 provider_id, registration_id, &info, &attrs);
418 attrs.waiting = handle->CreateServiceWorkerHandleAndPass(
419 registration->waiting_version());
420 attrs.active = handle->CreateServiceWorkerHandleAndPass(
421 registration->active_version());
422 492
423 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( 493 Send(new ServiceWorkerMsg_ServiceWorkerRegistered(
424 thread_id, request_id, handle->GetObjectInfo(), attrs)); 494 thread_id, request_id, info, attrs));
425 TRACE_EVENT_ASYNC_END2("ServiceWorker", 495 TRACE_EVENT_ASYNC_END2("ServiceWorker",
426 "ServiceWorkerDispatcherHost::RegisterServiceWorker", 496 "ServiceWorkerDispatcherHost::RegisterServiceWorker",
427 request_id, 497 request_id,
428 "Registration ID", registration_id, 498 "Registration ID", registration_id,
429 "Version ID", version_id); 499 "Version ID", version_id);
430 } 500 }
431 501
432 void ServiceWorkerDispatcherHost::OnWorkerReadyForInspection( 502 void ServiceWorkerDispatcherHost::OnWorkerReadyForInspection(
433 int embedded_worker_id) { 503 int embedded_worker_id) {
434 TRACE_EVENT0("ServiceWorker", 504 TRACE_EVENT0("ServiceWorker",
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 } 671 }
602 672
603 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id)); 673 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id));
604 TRACE_EVENT_ASYNC_END1( 674 TRACE_EVENT_ASYNC_END1(
605 "ServiceWorker", 675 "ServiceWorker",
606 "ServiceWorkerDispatcherHost::UnregisterServiceWorker", 676 "ServiceWorkerDispatcherHost::UnregisterServiceWorker",
607 request_id, 677 request_id,
608 "Status", status); 678 "Status", status);
609 } 679 }
610 680
681 void ServiceWorkerDispatcherHost::GetRegistrationComplete(
682 int thread_id,
683 int provider_id,
684 int request_id,
685 ServiceWorkerStatusCode status,
686 int64 registration_id) {
michaeln 2014/09/10 23:52:24 This method is relying on a 'live' registration ob
Kunihiko Sakamoto 2014/09/11 02:54:05 Done (as a consequence of using FindRegistrationFo
687 if (!GetContext())
688 return;
689
690 if (status != SERVICE_WORKER_OK) {
691 SendGetRegistrationError(thread_id, request_id, status);
692 return;
693 }
694
695 ServiceWorkerRegistrationObjectInfo info;
696 ServiceWorkerVersionAttributes attrs;
697 if (registration_id != kInvalidServiceWorkerRegistrationId) {
698 GetRegistrationObjectInfoAndVersionAttributes(
699 provider_id, registration_id, &info, &attrs);
700 }
701
702 Send(new ServiceWorkerMsg_DidGetRegistration(
703 thread_id, request_id, info, attrs));
704 TRACE_EVENT_ASYNC_END1("ServiceWorker",
705 "ServiceWorkerDispatcherHost::GetRegistration",
706 request_id,
707 "Registration ID", registration_id);
708 }
709
611 void ServiceWorkerDispatcherHost::SendRegistrationError( 710 void ServiceWorkerDispatcherHost::SendRegistrationError(
612 int thread_id, 711 int thread_id,
613 int request_id, 712 int request_id,
614 ServiceWorkerStatusCode status) { 713 ServiceWorkerStatusCode status) {
615 base::string16 error_message; 714 base::string16 error_message;
616 blink::WebServiceWorkerError::ErrorType error_type; 715 blink::WebServiceWorkerError::ErrorType error_type;
617 GetServiceWorkerRegistrationStatusResponse( 716 GetServiceWorkerRegistrationStatusResponse(
618 status, &error_type, &error_message); 717 status, &error_type, &error_message);
619 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( 718 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError(
620 thread_id, request_id, error_type, error_message)); 719 thread_id, request_id, error_type, error_message));
621 } 720 }
622 721
623 void ServiceWorkerDispatcherHost::SendUnregistrationError( 722 void ServiceWorkerDispatcherHost::SendUnregistrationError(
624 int thread_id, 723 int thread_id,
625 int request_id, 724 int request_id,
626 ServiceWorkerStatusCode status) { 725 ServiceWorkerStatusCode status) {
627 base::string16 error_message; 726 base::string16 error_message;
628 blink::WebServiceWorkerError::ErrorType error_type; 727 blink::WebServiceWorkerError::ErrorType error_type;
629 GetServiceWorkerRegistrationStatusResponse( 728 GetServiceWorkerRegistrationStatusResponse(
630 status, &error_type, &error_message); 729 status, &error_type, &error_message);
631 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( 730 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError(
632 thread_id, request_id, error_type, error_message)); 731 thread_id, request_id, error_type, error_message));
633 } 732 }
634 733
734 void ServiceWorkerDispatcherHost::SendGetRegistrationError(
735 int thread_id,
736 int request_id,
737 ServiceWorkerStatusCode status) {
738 base::string16 error_message;
739 blink::WebServiceWorkerError::ErrorType error_type;
740 GetServiceWorkerRegistrationStatusResponse(
741 status, &error_type, &error_message);
742 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError(
743 thread_id, request_id, error_type, error_message));
744 }
745
635 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { 746 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() {
636 return context_wrapper_->context(); 747 return context_wrapper_->context();
637 } 748 }
638 749
639 } // namespace content 750 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698