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 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
351 if (GetContext()->storage()->IsDisabled()) { | |
352 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); | |
353 return; | |
354 } | |
355 | |
356 TRACE_EVENT_ASYNC_BEGIN1( | |
357 "ServiceWorker", | |
358 "ServiceWorkerDispatcherHost::GetRegistration", | |
359 request_id, | |
360 "Document URL", document_url.spec()); | |
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 ServiceWorkerRegistration* registration, | |
461 ServiceWorkerRegistrationObjectInfo* info, | |
462 ServiceWorkerVersionAttributes* attrs) { | |
463 | |
nhiroki
2014/09/11 10:52:40
nit: there is an empty line.
Kunihiko Sakamoto
2014/09/12 00:40:10
Removed.
| |
464 ServiceWorkerRegistrationHandle* handle = | |
465 GetOrCreateRegistrationHandle(provider_id, registration); | |
466 *info = handle->GetObjectInfo(); | |
467 | |
468 attrs->installing = handle->CreateServiceWorkerHandleAndPass( | |
469 registration->installing_version()); | |
470 attrs->waiting = handle->CreateServiceWorkerHandleAndPass( | |
471 registration->waiting_version()); | |
472 attrs->active = handle->CreateServiceWorkerHandleAndPass( | |
473 registration->active_version()); | |
474 } | |
475 | |
392 void ServiceWorkerDispatcherHost::RegistrationComplete( | 476 void ServiceWorkerDispatcherHost::RegistrationComplete( |
393 int thread_id, | 477 int thread_id, |
394 int provider_id, | 478 int provider_id, |
395 int request_id, | 479 int request_id, |
396 ServiceWorkerStatusCode status, | 480 ServiceWorkerStatusCode status, |
397 int64 registration_id, | 481 int64 registration_id, |
398 int64 version_id) { | 482 int64 version_id) { |
399 if (!GetContext()) | 483 if (!GetContext()) |
400 return; | 484 return; |
401 | 485 |
402 if (status != SERVICE_WORKER_OK) { | 486 if (status != SERVICE_WORKER_OK) { |
403 SendRegistrationError(thread_id, request_id, status); | 487 SendRegistrationError(thread_id, request_id, status); |
404 return; | 488 return; |
405 } | 489 } |
406 | 490 |
407 ServiceWorkerRegistration* registration = | 491 ServiceWorkerRegistration* registration = |
408 GetContext()->GetLiveRegistration(registration_id); | 492 GetContext()->GetLiveRegistration(registration_id); |
409 DCHECK(registration); | 493 DCHECK(registration); |
410 | 494 |
411 ServiceWorkerRegistrationHandle* handle = | 495 ServiceWorkerRegistrationObjectInfo info; |
412 GetOrCreateRegistrationHandle(provider_id, registration); | |
413 | |
414 ServiceWorkerVersionAttributes attrs; | 496 ServiceWorkerVersionAttributes attrs; |
415 attrs.installing = handle->CreateServiceWorkerHandleAndPass( | 497 GetRegistrationObjectInfoAndVersionAttributes( |
416 registration->installing_version()); | 498 provider_id, registration, &info, &attrs); |
417 attrs.waiting = handle->CreateServiceWorkerHandleAndPass( | |
418 registration->waiting_version()); | |
419 attrs.active = handle->CreateServiceWorkerHandleAndPass( | |
420 registration->active_version()); | |
421 | 499 |
422 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( | 500 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( |
423 thread_id, request_id, handle->GetObjectInfo(), attrs)); | 501 thread_id, request_id, info, attrs)); |
424 TRACE_EVENT_ASYNC_END2("ServiceWorker", | 502 TRACE_EVENT_ASYNC_END2("ServiceWorker", |
425 "ServiceWorkerDispatcherHost::RegisterServiceWorker", | 503 "ServiceWorkerDispatcherHost::RegisterServiceWorker", |
426 request_id, | 504 request_id, |
427 "Registration ID", registration_id, | 505 "Registration ID", registration_id, |
428 "Version ID", version_id); | 506 "Version ID", version_id); |
429 } | 507 } |
430 | 508 |
431 void ServiceWorkerDispatcherHost::OnWorkerReadyForInspection( | 509 void ServiceWorkerDispatcherHost::OnWorkerReadyForInspection( |
432 int embedded_worker_id) { | 510 int embedded_worker_id) { |
433 TRACE_EVENT0("ServiceWorker", | 511 TRACE_EVENT0("ServiceWorker", |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, | 680 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, |
603 request_id, | 681 request_id, |
604 is_success)); | 682 is_success)); |
605 TRACE_EVENT_ASYNC_END1( | 683 TRACE_EVENT_ASYNC_END1( |
606 "ServiceWorker", | 684 "ServiceWorker", |
607 "ServiceWorkerDispatcherHost::UnregisterServiceWorker", | 685 "ServiceWorkerDispatcherHost::UnregisterServiceWorker", |
608 request_id, | 686 request_id, |
609 "Status", status); | 687 "Status", status); |
610 } | 688 } |
611 | 689 |
690 void ServiceWorkerDispatcherHost::GetRegistrationComplete( | |
691 int thread_id, | |
692 int provider_id, | |
693 int request_id, | |
694 ServiceWorkerStatusCode status, | |
695 const scoped_refptr<ServiceWorkerRegistration>& registration) { | |
696 TRACE_EVENT_ASYNC_END1("ServiceWorker", | |
697 "ServiceWorkerDispatcherHost::GetRegistration", | |
698 request_id, | |
699 "Registration ID", | |
700 registration.get() ? registration->id() | |
701 : kInvalidServiceWorkerRegistrationId); | |
702 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { | |
703 SendGetRegistrationError(thread_id, request_id, status); | |
704 return; | |
705 } | |
706 | |
707 ServiceWorkerRegistrationObjectInfo info; | |
708 ServiceWorkerVersionAttributes attrs; | |
709 if (status == SERVICE_WORKER_OK) { | |
710 DCHECK(registration.get()); | |
711 if (!registration->is_uninstalling()) { | |
712 GetRegistrationObjectInfoAndVersionAttributes( | |
713 provider_id, registration.get(), &info, &attrs); | |
714 } | |
715 } | |
716 | |
717 Send(new ServiceWorkerMsg_DidGetRegistration( | |
718 thread_id, request_id, info, attrs)); | |
719 } | |
720 | |
612 void ServiceWorkerDispatcherHost::SendRegistrationError( | 721 void ServiceWorkerDispatcherHost::SendRegistrationError( |
613 int thread_id, | 722 int thread_id, |
614 int request_id, | 723 int request_id, |
615 ServiceWorkerStatusCode status) { | 724 ServiceWorkerStatusCode status) { |
616 base::string16 error_message; | 725 base::string16 error_message; |
617 blink::WebServiceWorkerError::ErrorType error_type; | 726 blink::WebServiceWorkerError::ErrorType error_type; |
618 GetServiceWorkerRegistrationStatusResponse( | 727 GetServiceWorkerRegistrationStatusResponse( |
619 status, &error_type, &error_message); | 728 status, &error_type, &error_message); |
620 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 729 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
621 thread_id, request_id, error_type, error_message)); | 730 thread_id, request_id, error_type, error_message)); |
622 } | 731 } |
623 | 732 |
624 void ServiceWorkerDispatcherHost::SendUnregistrationError( | 733 void ServiceWorkerDispatcherHost::SendUnregistrationError( |
625 int thread_id, | 734 int thread_id, |
626 int request_id, | 735 int request_id, |
627 ServiceWorkerStatusCode status) { | 736 ServiceWorkerStatusCode status) { |
628 base::string16 error_message; | 737 base::string16 error_message; |
629 blink::WebServiceWorkerError::ErrorType error_type; | 738 blink::WebServiceWorkerError::ErrorType error_type; |
630 GetServiceWorkerRegistrationStatusResponse( | 739 GetServiceWorkerRegistrationStatusResponse( |
631 status, &error_type, &error_message); | 740 status, &error_type, &error_message); |
632 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( | 741 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( |
633 thread_id, request_id, error_type, error_message)); | 742 thread_id, request_id, error_type, error_message)); |
634 } | 743 } |
635 | 744 |
745 void ServiceWorkerDispatcherHost::SendGetRegistrationError( | |
746 int thread_id, | |
747 int request_id, | |
748 ServiceWorkerStatusCode status) { | |
749 base::string16 error_message; | |
750 blink::WebServiceWorkerError::ErrorType error_type; | |
751 GetServiceWorkerRegistrationStatusResponse( | |
752 status, &error_type, &error_message); | |
753 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( | |
754 thread_id, request_id, error_type, error_message)); | |
755 } | |
756 | |
636 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { | 757 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
637 return context_wrapper_->context(); | 758 return context_wrapper_->context(); |
638 } | 759 } |
639 | 760 |
640 } // namespace content | 761 } // namespace content |
OLD | NEW |