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 TRACE_EVENT_ASYNC_BEGIN1( | |
351 "ServiceWorker", | |
352 "ServiceWorkerDispatcherHost::GetRegistration", | |
353 request_id, | |
354 "Document URL", document_url.spec()); | |
nhiroki
2014/09/11 07:23:49
The corresponding ASYNC_END couldn't be called due
Kunihiko Sakamoto
2014/09/11 08:48:10
Good catch, thanks! Fixed.
| |
355 | |
356 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
357 if (GetContext()->storage()->IsDisabled()) { | |
358 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); | |
359 return; | |
360 } | |
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 int64 registration_id, | |
461 ServiceWorkerRegistrationObjectInfo* info, | |
462 ServiceWorkerVersionAttributes* attrs) { | |
463 ServiceWorkerRegistration* registration = | |
464 GetContext()->GetLiveRegistration(registration_id); | |
465 DCHECK(registration); | |
nhiroki
2014/09/11 07:23:49
This assumption that GetLiveRegistration() always
Kunihiko Sakamoto
2014/09/11 08:48:10
Done. It simplified GetRegistrationComplete a bit.
| |
466 | |
467 ServiceWorkerRegistrationHandle* handle = | |
468 GetOrCreateRegistrationHandle(provider_id, registration); | |
469 *info = handle->GetObjectInfo(); | |
470 | |
471 attrs->installing = handle->CreateServiceWorkerHandleAndPass( | |
472 registration->installing_version()); | |
473 attrs->waiting = handle->CreateServiceWorkerHandleAndPass( | |
474 registration->waiting_version()); | |
475 attrs->active = handle->CreateServiceWorkerHandleAndPass( | |
476 registration->active_version()); | |
477 } | |
478 | |
392 void ServiceWorkerDispatcherHost::RegistrationComplete( | 479 void ServiceWorkerDispatcherHost::RegistrationComplete( |
393 int thread_id, | 480 int thread_id, |
394 int provider_id, | 481 int provider_id, |
395 int request_id, | 482 int request_id, |
396 ServiceWorkerStatusCode status, | 483 ServiceWorkerStatusCode status, |
397 int64 registration_id, | 484 int64 registration_id, |
398 int64 version_id) { | 485 int64 version_id) { |
399 if (!GetContext()) | 486 if (!GetContext()) |
400 return; | 487 return; |
401 | 488 |
402 if (status != SERVICE_WORKER_OK) { | 489 if (status != SERVICE_WORKER_OK) { |
403 SendRegistrationError(thread_id, request_id, status); | 490 SendRegistrationError(thread_id, request_id, status); |
404 return; | 491 return; |
405 } | 492 } |
406 | 493 |
407 ServiceWorkerRegistration* registration = | 494 ServiceWorkerRegistrationObjectInfo info; |
408 GetContext()->GetLiveRegistration(registration_id); | |
409 DCHECK(registration); | |
410 | |
411 ServiceWorkerRegistrationHandle* handle = | |
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_id, &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 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { | |
696 SendGetRegistrationError(thread_id, request_id, status); | |
697 return; | |
698 } | |
699 | |
700 ServiceWorkerRegistrationObjectInfo info; | |
701 ServiceWorkerVersionAttributes attrs; | |
702 int64 registration_id = kInvalidServiceWorkerRegistrationId; | |
703 if (status == SERVICE_WORKER_OK && !registration->is_uninstalling()) { | |
704 registration_id = registration->id(); | |
705 GetRegistrationObjectInfoAndVersionAttributes( | |
706 provider_id, registration_id, &info, &attrs); | |
707 } | |
708 | |
709 Send(new ServiceWorkerMsg_DidGetRegistration( | |
710 thread_id, request_id, info, attrs)); | |
711 TRACE_EVENT_ASYNC_END1("ServiceWorker", | |
712 "ServiceWorkerDispatcherHost::GetRegistration", | |
713 request_id, | |
714 "Registration ID", registration_id); | |
nhiroki
2014/09/11 07:23:49
ditto: moving this before line 695.
Kunihiko Sakamoto
2014/09/11 08:48:10
Done.
| |
715 } | |
716 | |
612 void ServiceWorkerDispatcherHost::SendRegistrationError( | 717 void ServiceWorkerDispatcherHost::SendRegistrationError( |
613 int thread_id, | 718 int thread_id, |
614 int request_id, | 719 int request_id, |
615 ServiceWorkerStatusCode status) { | 720 ServiceWorkerStatusCode status) { |
616 base::string16 error_message; | 721 base::string16 error_message; |
617 blink::WebServiceWorkerError::ErrorType error_type; | 722 blink::WebServiceWorkerError::ErrorType error_type; |
618 GetServiceWorkerRegistrationStatusResponse( | 723 GetServiceWorkerRegistrationStatusResponse( |
619 status, &error_type, &error_message); | 724 status, &error_type, &error_message); |
620 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 725 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
621 thread_id, request_id, error_type, error_message)); | 726 thread_id, request_id, error_type, error_message)); |
622 } | 727 } |
623 | 728 |
624 void ServiceWorkerDispatcherHost::SendUnregistrationError( | 729 void ServiceWorkerDispatcherHost::SendUnregistrationError( |
625 int thread_id, | 730 int thread_id, |
626 int request_id, | 731 int request_id, |
627 ServiceWorkerStatusCode status) { | 732 ServiceWorkerStatusCode status) { |
628 base::string16 error_message; | 733 base::string16 error_message; |
629 blink::WebServiceWorkerError::ErrorType error_type; | 734 blink::WebServiceWorkerError::ErrorType error_type; |
630 GetServiceWorkerRegistrationStatusResponse( | 735 GetServiceWorkerRegistrationStatusResponse( |
631 status, &error_type, &error_message); | 736 status, &error_type, &error_message); |
632 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( | 737 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( |
633 thread_id, request_id, error_type, error_message)); | 738 thread_id, request_id, error_type, error_message)); |
634 } | 739 } |
635 | 740 |
741 void ServiceWorkerDispatcherHost::SendGetRegistrationError( | |
742 int thread_id, | |
743 int request_id, | |
744 ServiceWorkerStatusCode status) { | |
745 base::string16 error_message; | |
746 blink::WebServiceWorkerError::ErrorType error_type; | |
747 GetServiceWorkerRegistrationStatusResponse( | |
748 status, &error_type, &error_message); | |
749 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( | |
750 thread_id, request_id, error_type, error_message)); | |
751 } | |
752 | |
636 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { | 753 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
637 return context_wrapper_->context(); | 754 return context_wrapper_->context(); |
638 } | 755 } |
639 | 756 |
640 } // namespace content | 757 } // namespace content |
OLD | NEW |