| 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 |
| 601 if (status != SERVICE_WORKER_OK) { | 609 if (status != SERVICE_WORKER_OK) { |
| 602 SendRegistrationError(thread_id, request_id, status, status_message); | 610 SendRegistrationError(thread_id, request_id, status, status_message); |
| 603 return; | 611 return; |
| 604 } | 612 } |
| 605 | 613 |
| 606 ServiceWorkerRegistration* registration = | 614 ServiceWorkerRegistration* registration = |
| 607 GetContext()->GetLiveRegistration(registration_id); | 615 GetContext()->GetLiveRegistration(registration_id); |
| 608 DCHECK(registration); | 616 DCHECK(registration); |
| 609 | 617 |
| 610 ServiceWorkerRegistrationObjectInfo info; | 618 ServiceWorkerRegistrationObjectInfo info; |
| 611 ServiceWorkerVersionAttributes attrs; | 619 ServiceWorkerVersionAttributes attrs; |
| 612 GetRegistrationObjectInfoAndVersionAttributes( | 620 GetRegistrationObjectInfoAndVersionAttributes( |
| 613 provider_id, registration, &info, &attrs); | 621 provider_host->AsWeakPtr(), registration, &info, &attrs); |
| 614 | 622 |
| 615 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( | 623 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( |
| 616 thread_id, request_id, info, attrs)); | 624 thread_id, request_id, info, attrs)); |
| 617 TRACE_EVENT_ASYNC_END1("ServiceWorker", | 625 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
| 618 "ServiceWorkerDispatcherHost::RegisterServiceWorker", | 626 "ServiceWorkerDispatcherHost::RegisterServiceWorker", |
| 619 request_id, | 627 request_id, |
| 620 "Registration ID", | 628 "Registration ID", |
| 621 registration_id); | 629 registration_id); |
| 622 } | 630 } |
| 623 | 631 |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 TRACE_EVENT_ASYNC_END1("ServiceWorker", | 833 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
| 826 "ServiceWorkerDispatcherHost::GetRegistration", | 834 "ServiceWorkerDispatcherHost::GetRegistration", |
| 827 request_id, | 835 request_id, |
| 828 "Registration ID", | 836 "Registration ID", |
| 829 registration.get() ? registration->id() | 837 registration.get() ? registration->id() |
| 830 : kInvalidServiceWorkerRegistrationId); | 838 : kInvalidServiceWorkerRegistrationId); |
| 831 | 839 |
| 832 if (!GetContext()) | 840 if (!GetContext()) |
| 833 return; | 841 return; |
| 834 | 842 |
| 843 ServiceWorkerProviderHost* provider_host = |
| 844 GetContext()->GetProviderHost(render_process_id_, provider_id); |
| 845 if (!provider_host) |
| 846 return; // The provider has already been destroyed. |
| 847 |
| 835 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { | 848 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 836 SendGetRegistrationError(thread_id, request_id, status); | 849 SendGetRegistrationError(thread_id, request_id, status); |
| 837 return; | 850 return; |
| 838 } | 851 } |
| 839 | 852 |
| 840 ServiceWorkerRegistrationObjectInfo info; | 853 ServiceWorkerRegistrationObjectInfo info; |
| 841 ServiceWorkerVersionAttributes attrs; | 854 ServiceWorkerVersionAttributes attrs; |
| 842 if (status == SERVICE_WORKER_OK) { | 855 if (status == SERVICE_WORKER_OK) { |
| 843 DCHECK(registration.get()); | 856 DCHECK(registration.get()); |
| 844 if (!registration->is_uninstalling()) { | 857 if (!registration->is_uninstalling()) { |
| 845 GetRegistrationObjectInfoAndVersionAttributes( | 858 GetRegistrationObjectInfoAndVersionAttributes( |
| 846 provider_id, registration.get(), &info, &attrs); | 859 provider_host->AsWeakPtr(), registration.get(), &info, &attrs); |
| 847 } | 860 } |
| 848 } | 861 } |
| 849 | 862 |
| 850 Send(new ServiceWorkerMsg_DidGetRegistration( | 863 Send(new ServiceWorkerMsg_DidGetRegistration( |
| 851 thread_id, request_id, info, attrs)); | 864 thread_id, request_id, info, attrs)); |
| 852 } | 865 } |
| 853 | 866 |
| 854 void ServiceWorkerDispatcherHost::SendRegistrationError( | 867 void ServiceWorkerDispatcherHost::SendRegistrationError( |
| 855 int thread_id, | 868 int thread_id, |
| 856 int request_id, | 869 int request_id, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 901 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { | 914 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { |
| 902 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 915 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
| 903 if (!handle) { | 916 if (!handle) { |
| 904 BadMessageReceived(); | 917 BadMessageReceived(); |
| 905 return; | 918 return; |
| 906 } | 919 } |
| 907 handle->version()->StopWorker( | 920 handle->version()->StopWorker( |
| 908 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 921 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 909 } | 922 } |
| 910 | 923 |
| 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 | 924 } // namespace content |
| OLD | NEW |