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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 if (channel_ready_) { | 208 if (channel_ready_) { |
209 BrowserMessageFilter::Send(message); | 209 BrowserMessageFilter::Send(message); |
210 // Don't bother passing through Send()'s result: it's not reliable. | 210 // Don't bother passing through Send()'s result: it's not reliable. |
211 return true; | 211 return true; |
212 } | 212 } |
213 | 213 |
214 pending_messages_.push_back(message); | 214 pending_messages_.push_back(message); |
215 return true; | 215 return true; |
216 } | 216 } |
217 | 217 |
218 void ServiceWorkerDispatcherHost::RegisterServiceWorkerHandle( | |
219 scoped_ptr<ServiceWorkerHandle> handle) { | |
220 int handle_id = handle->handle_id(); | |
221 handles_.AddWithID(handle.release(), handle_id); | |
222 } | |
223 | |
224 void ServiceWorkerDispatcherHost::RegisterServiceWorkerRegistrationHandle( | |
225 scoped_ptr<ServiceWorkerRegistrationHandle> handle) { | |
226 int handle_id = handle->handle_id(); | |
227 registration_handles_.AddWithID(handle.release(), handle_id); | |
228 } | |
229 | |
218 ServiceWorkerRegistrationHandle* | 230 ServiceWorkerRegistrationHandle* |
219 ServiceWorkerDispatcherHost::GetOrCreateRegistrationHandle( | 231 ServiceWorkerDispatcherHost::GetOrCreateRegistrationHandle( |
220 int provider_id, | 232 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
221 ServiceWorkerRegistration* registration) { | 233 ServiceWorkerRegistration* registration) { |
234 DCHECK(provider_host); | |
222 ServiceWorkerRegistrationHandle* handle = | 235 ServiceWorkerRegistrationHandle* handle = |
223 FindRegistrationHandle(provider_id, registration->id()); | 236 FindRegistrationHandle(provider_host->provider_id(), registration->id()); |
224 if (handle) { | 237 if (handle) { |
225 handle->IncrementRefCount(); | 238 handle->IncrementRefCount(); |
226 return handle; | 239 return handle; |
227 } | 240 } |
228 | 241 |
229 scoped_ptr<ServiceWorkerRegistrationHandle> new_handle( | 242 scoped_ptr<ServiceWorkerRegistrationHandle> new_handle( |
230 new ServiceWorkerRegistrationHandle( | 243 new ServiceWorkerRegistrationHandle( |
231 GetContext()->AsWeakPtr(), this, provider_id, registration)); | 244 GetContext()->AsWeakPtr(), provider_host, registration)); |
232 handle = new_handle.get(); | 245 handle = new_handle.get(); |
233 RegisterServiceWorkerRegistrationHandle(new_handle.Pass()); | 246 RegisterServiceWorkerRegistrationHandle(new_handle.Pass()); |
234 return handle; | 247 return handle; |
235 } | 248 } |
236 | 249 |
237 ServiceWorkerObjectInfo | |
238 ServiceWorkerDispatcherHost::CreateAndRegisterServiceWorkerHandle( | |
239 ServiceWorkerVersion* version) { | |
240 ServiceWorkerObjectInfo info; | |
241 if (GetContext() && version) { | |
242 scoped_ptr<ServiceWorkerHandle> handle = | |
243 ServiceWorkerHandle::Create(GetContext()->AsWeakPtr(), this, version); | |
244 info = handle->GetObjectInfo(); | |
245 RegisterServiceWorkerHandle(handle.Pass()); | |
246 } | |
247 return info; | |
248 } | |
249 | |
250 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( | 250 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( |
251 int thread_id, | 251 int thread_id, |
252 int request_id, | 252 int request_id, |
253 int provider_id, | 253 int provider_id, |
254 const GURL& pattern, | 254 const GURL& pattern, |
255 const GURL& script_url) { | 255 const GURL& script_url) { |
256 TRACE_EVENT0("ServiceWorker", | 256 TRACE_EVENT0("ServiceWorker", |
257 "ServiceWorkerDispatcherHost::OnRegisterServiceWorker"); | 257 "ServiceWorkerDispatcherHost::OnRegisterServiceWorker"); |
258 if (!GetContext()) { | 258 if (!GetContext()) { |
259 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 259 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
565 DCHECK(handle); | 565 DCHECK(handle); |
566 if (handle->provider_id() == provider_id && handle->registration() && | 566 if (handle->provider_id() == provider_id && handle->registration() && |
567 handle->registration()->id() == registration_id) { | 567 handle->registration()->id() == registration_id) { |
568 return handle; | 568 return handle; |
569 } | 569 } |
570 } | 570 } |
571 return NULL; | 571 return NULL; |
572 } | 572 } |
573 | 573 |
574 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( | 574 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( |
575 int provider_id, | 575 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
576 ServiceWorkerRegistration* registration, | 576 ServiceWorkerRegistration* registration, |
577 ServiceWorkerRegistrationObjectInfo* info, | 577 ServiceWorkerRegistrationObjectInfo* info, |
578 ServiceWorkerVersionAttributes* attrs) { | 578 ServiceWorkerVersionAttributes* attrs) { |
579 ServiceWorkerRegistrationHandle* handle = | 579 ServiceWorkerRegistrationHandle* handle = |
580 GetOrCreateRegistrationHandle(provider_id, registration); | 580 GetOrCreateRegistrationHandle(provider_host, registration); |
581 *info = handle->GetObjectInfo(); | 581 *info = handle->GetObjectInfo(); |
582 | 582 |
583 attrs->installing = CreateAndRegisterServiceWorkerHandle( | 583 attrs->installing = |
584 registration->installing_version()); | 584 provider_host->CreateAndRegisterServiceWorkerHandle( |
585 attrs->waiting = CreateAndRegisterServiceWorkerHandle( | 585 registration->installing_version()); |
586 registration->waiting_version()); | 586 attrs->waiting = |
587 attrs->active = CreateAndRegisterServiceWorkerHandle( | 587 provider_host->CreateAndRegisterServiceWorkerHandle( |
588 registration->active_version()); | 588 registration->waiting_version()); |
589 attrs->active = | |
590 provider_host->CreateAndRegisterServiceWorkerHandle( | |
591 registration->active_version()); | |
589 } | 592 } |
590 | 593 |
591 void ServiceWorkerDispatcherHost::RegistrationComplete( | 594 void ServiceWorkerDispatcherHost::RegistrationComplete( |
592 int thread_id, | 595 int thread_id, |
593 int provider_id, | 596 int provider_id, |
594 int request_id, | 597 int request_id, |
595 ServiceWorkerStatusCode status, | 598 ServiceWorkerStatusCode status, |
596 const std::string& status_message, | 599 const std::string& status_message, |
597 int64 registration_id) { | 600 int64 registration_id) { |
598 if (!GetContext()) | 601 if (!GetContext()) |
599 return; | 602 return; |
600 | 603 |
604 ServiceWorkerProviderHost* provider_host = | |
605 GetContext()->GetProviderHost(render_process_id_, provider_id); | |
606 if (!provider_host) | |
607 return; // The provider has already been destroyed. | |
608 if (!provider_host->IsContextAlive()) { | |
609 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | |
610 thread_id, request_id, WebServiceWorkerError::ErrorTypeAbort, | |
611 base::ASCIIToUTF16(kServiceWorkerRegisterErrorPrefix) + | |
612 base::ASCIIToUTF16(kShutdownErrorMessage))); | |
kinuko
2015/01/21 10:11:00
Why do you add this error message? (It doesn't loo
nhiroki
2015/01/22 03:34:14
|provider_host->IsContextAlive()| may return false
nhiroki
2015/01/22 04:12:50
If |DeleteAndStartOver()| happens between OnRegist
nhiroki
2015/01/22 04:33:42
Done.
| |
613 return; | |
614 } | |
615 | |
601 if (status != SERVICE_WORKER_OK) { | 616 if (status != SERVICE_WORKER_OK) { |
602 SendRegistrationError(thread_id, request_id, status, status_message); | 617 SendRegistrationError(thread_id, request_id, status, status_message); |
603 return; | 618 return; |
604 } | 619 } |
605 | 620 |
606 ServiceWorkerRegistration* registration = | 621 ServiceWorkerRegistration* registration = |
607 GetContext()->GetLiveRegistration(registration_id); | 622 GetContext()->GetLiveRegistration(registration_id); |
608 DCHECK(registration); | 623 DCHECK(registration); |
609 | 624 |
610 ServiceWorkerRegistrationObjectInfo info; | 625 ServiceWorkerRegistrationObjectInfo info; |
611 ServiceWorkerVersionAttributes attrs; | 626 ServiceWorkerVersionAttributes attrs; |
612 GetRegistrationObjectInfoAndVersionAttributes( | 627 GetRegistrationObjectInfoAndVersionAttributes( |
613 provider_id, registration, &info, &attrs); | 628 provider_host->AsWeakPtr(), registration, &info, &attrs); |
614 | 629 |
615 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( | 630 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( |
616 thread_id, request_id, info, attrs)); | 631 thread_id, request_id, info, attrs)); |
617 TRACE_EVENT_ASYNC_END1("ServiceWorker", | 632 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
618 "ServiceWorkerDispatcherHost::RegisterServiceWorker", | 633 "ServiceWorkerDispatcherHost::RegisterServiceWorker", |
619 request_id, | 634 request_id, |
620 "Registration ID", | 635 "Registration ID", |
621 registration_id); | 636 registration_id); |
622 } | 637 } |
623 | 638 |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
825 TRACE_EVENT_ASYNC_END1("ServiceWorker", | 840 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
826 "ServiceWorkerDispatcherHost::GetRegistration", | 841 "ServiceWorkerDispatcherHost::GetRegistration", |
827 request_id, | 842 request_id, |
828 "Registration ID", | 843 "Registration ID", |
829 registration.get() ? registration->id() | 844 registration.get() ? registration->id() |
830 : kInvalidServiceWorkerRegistrationId); | 845 : kInvalidServiceWorkerRegistrationId); |
831 | 846 |
832 if (!GetContext()) | 847 if (!GetContext()) |
833 return; | 848 return; |
834 | 849 |
850 ServiceWorkerProviderHost* provider_host = | |
851 GetContext()->GetProviderHost(render_process_id_, provider_id); | |
852 if (!provider_host) | |
853 return; // The provider has already been destroyed. | |
854 if (!provider_host->IsContextAlive()) { | |
855 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( | |
856 thread_id, request_id, blink::WebServiceWorkerError::ErrorTypeAbort, | |
857 base::ASCIIToUTF16(kServiceWorkerGetRegistrationErrorPrefix) + | |
858 base::ASCIIToUTF16(kShutdownErrorMessage))); | |
859 return; | |
860 } | |
861 | |
835 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { | 862 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { |
836 SendGetRegistrationError(thread_id, request_id, status); | 863 SendGetRegistrationError(thread_id, request_id, status); |
837 return; | 864 return; |
838 } | 865 } |
839 | 866 |
840 ServiceWorkerRegistrationObjectInfo info; | 867 ServiceWorkerRegistrationObjectInfo info; |
841 ServiceWorkerVersionAttributes attrs; | 868 ServiceWorkerVersionAttributes attrs; |
842 if (status == SERVICE_WORKER_OK) { | 869 if (status == SERVICE_WORKER_OK) { |
843 DCHECK(registration.get()); | 870 DCHECK(registration.get()); |
844 if (!registration->is_uninstalling()) { | 871 if (!registration->is_uninstalling()) { |
845 GetRegistrationObjectInfoAndVersionAttributes( | 872 GetRegistrationObjectInfoAndVersionAttributes( |
846 provider_id, registration.get(), &info, &attrs); | 873 provider_host->AsWeakPtr(), registration.get(), &info, &attrs); |
847 } | 874 } |
848 } | 875 } |
849 | 876 |
850 Send(new ServiceWorkerMsg_DidGetRegistration( | 877 Send(new ServiceWorkerMsg_DidGetRegistration( |
851 thread_id, request_id, info, attrs)); | 878 thread_id, request_id, info, attrs)); |
852 } | 879 } |
853 | 880 |
854 void ServiceWorkerDispatcherHost::SendRegistrationError( | 881 void ServiceWorkerDispatcherHost::SendRegistrationError( |
855 int thread_id, | 882 int thread_id, |
856 int request_id, | 883 int request_id, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
901 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { | 928 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { |
902 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 929 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
903 if (!handle) { | 930 if (!handle) { |
904 BadMessageReceived(); | 931 BadMessageReceived(); |
905 return; | 932 return; |
906 } | 933 } |
907 handle->version()->StopWorker( | 934 handle->version()->StopWorker( |
908 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 935 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
909 } | 936 } |
910 | 937 |
911 void ServiceWorkerDispatcherHost::RegisterServiceWorkerHandle( | |
912 scoped_ptr<ServiceWorkerHandle> handle) { | |
913 int handle_id = handle->handle_id(); | |
914 handles_.AddWithID(handle.release(), handle_id); | |
915 } | |
916 | |
917 void ServiceWorkerDispatcherHost::RegisterServiceWorkerRegistrationHandle( | |
918 scoped_ptr<ServiceWorkerRegistrationHandle> handle) { | |
919 int handle_id = handle->handle_id(); | |
920 registration_handles_.AddWithID(handle.release(), handle_id); | |
921 } | |
922 | |
923 } // namespace content | 938 } // namespace content |
OLD | NEW |