Chromium Code Reviews| Index: content/browser/service_worker/service_worker_dispatcher_host.cc |
| diff --git a/content/browser/service_worker/service_worker_dispatcher_host.cc b/content/browser/service_worker/service_worker_dispatcher_host.cc |
| index ffdd78d081ee2d4529cb717b16e1e42e49601894..8a9b430cc2a3a78ef2c72d7934e5002b73e59a84 100644 |
| --- a/content/browser/service_worker/service_worker_dispatcher_host.cc |
| +++ b/content/browser/service_worker/service_worker_dispatcher_host.cc |
| @@ -55,6 +55,13 @@ bool CanUnregisterServiceWorker(const GURL& document_url, |
| return document_url.GetOrigin() == pattern.GetOrigin(); |
| } |
| +bool CanGetRegistration(const GURL& document_url, |
| + const GURL& given_document_url) { |
| + // TODO: Respect Chrome's content settings, if we add a setting for |
| + // controlling whether Service Worker is allowed. |
| + return document_url.GetOrigin() == given_document_url.GetOrigin(); |
| +} |
| + |
| } // namespace |
| ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
| @@ -113,6 +120,8 @@ bool ServiceWorkerDispatcherHost::OnMessageReceived( |
| OnRegisterServiceWorker) |
| IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
| OnUnregisterServiceWorker) |
| + IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, |
| + OnGetRegistration) |
| IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
| OnProviderCreated) |
| IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, |
| @@ -302,6 +311,63 @@ void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( |
| request_id)); |
| } |
| +void ServiceWorkerDispatcherHost::OnGetRegistration( |
| + int thread_id, |
| + int request_id, |
| + int provider_id, |
| + const GURL& document_url) { |
| + TRACE_EVENT0("ServiceWorker", |
| + "ServiceWorkerDispatcherHost::OnGetRegistration"); |
| + if (!GetContext()) { |
| + Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
| + thread_id, |
| + request_id, |
| + blink::WebServiceWorkerError::ErrorTypeAbort, |
| + base::ASCIIToUTF16(kShutdownErrorMessage))); |
| + return; |
| + } |
| + |
| + ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost( |
| + render_process_id_, provider_id); |
| + if (!provider_host) { |
| + BadMessageReceived(); |
| + return; |
| + } |
| + if (!provider_host->IsContextAlive()) { |
| + Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
| + thread_id, |
| + request_id, |
| + blink::WebServiceWorkerError::ErrorTypeAbort, |
| + base::ASCIIToUTF16(kShutdownErrorMessage))); |
| + return; |
| + } |
| + |
| + if (!CanGetRegistration(provider_host->document_url(), document_url)) { |
| + BadMessageReceived(); |
| + return; |
| + } |
| + |
| + TRACE_EVENT_ASYNC_BEGIN1( |
| + "ServiceWorker", |
| + "ServiceWorkerDispatcherHost::GetRegistration", |
| + request_id, |
| + "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.
|
| + |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (GetContext()->storage()->IsDisabled()) { |
| + SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); |
| + return; |
| + } |
| + |
| + GetContext()->storage()->FindRegistrationForDocument( |
| + document_url, |
| + base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete, |
| + this, |
| + thread_id, |
| + provider_id, |
| + request_id)); |
| +} |
| + |
| void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
| int handle_id, |
| const base::string16& message, |
| @@ -389,6 +455,27 @@ ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id, |
| return NULL; |
| } |
| +void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( |
| + int provider_id, |
| + int64 registration_id, |
| + ServiceWorkerRegistrationObjectInfo* info, |
| + ServiceWorkerVersionAttributes* attrs) { |
| + ServiceWorkerRegistration* registration = |
| + GetContext()->GetLiveRegistration(registration_id); |
| + 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.
|
| + |
| + ServiceWorkerRegistrationHandle* handle = |
| + GetOrCreateRegistrationHandle(provider_id, registration); |
| + *info = handle->GetObjectInfo(); |
| + |
| + attrs->installing = handle->CreateServiceWorkerHandleAndPass( |
| + registration->installing_version()); |
| + attrs->waiting = handle->CreateServiceWorkerHandleAndPass( |
| + registration->waiting_version()); |
| + attrs->active = handle->CreateServiceWorkerHandleAndPass( |
| + registration->active_version()); |
| +} |
| + |
| void ServiceWorkerDispatcherHost::RegistrationComplete( |
| int thread_id, |
| int provider_id, |
| @@ -404,23 +491,13 @@ void ServiceWorkerDispatcherHost::RegistrationComplete( |
| return; |
| } |
| - ServiceWorkerRegistration* registration = |
| - GetContext()->GetLiveRegistration(registration_id); |
| - DCHECK(registration); |
| - |
| - ServiceWorkerRegistrationHandle* handle = |
| - GetOrCreateRegistrationHandle(provider_id, registration); |
| - |
| + ServiceWorkerRegistrationObjectInfo info; |
| ServiceWorkerVersionAttributes attrs; |
| - attrs.installing = handle->CreateServiceWorkerHandleAndPass( |
| - registration->installing_version()); |
| - attrs.waiting = handle->CreateServiceWorkerHandleAndPass( |
| - registration->waiting_version()); |
| - attrs.active = handle->CreateServiceWorkerHandleAndPass( |
| - registration->active_version()); |
| + GetRegistrationObjectInfoAndVersionAttributes( |
| + provider_id, registration_id, &info, &attrs); |
| Send(new ServiceWorkerMsg_ServiceWorkerRegistered( |
| - thread_id, request_id, handle->GetObjectInfo(), attrs)); |
| + thread_id, request_id, info, attrs)); |
| TRACE_EVENT_ASYNC_END2("ServiceWorker", |
| "ServiceWorkerDispatcherHost::RegisterServiceWorker", |
| request_id, |
| @@ -609,6 +686,34 @@ void ServiceWorkerDispatcherHost::UnregistrationComplete( |
| "Status", status); |
| } |
| +void ServiceWorkerDispatcherHost::GetRegistrationComplete( |
| + int thread_id, |
| + int provider_id, |
| + int request_id, |
| + ServiceWorkerStatusCode status, |
| + const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| + if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { |
| + SendGetRegistrationError(thread_id, request_id, status); |
| + return; |
| + } |
| + |
| + ServiceWorkerRegistrationObjectInfo info; |
| + ServiceWorkerVersionAttributes attrs; |
| + int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| + if (status == SERVICE_WORKER_OK && !registration->is_uninstalling()) { |
| + registration_id = registration->id(); |
| + GetRegistrationObjectInfoAndVersionAttributes( |
| + provider_id, registration_id, &info, &attrs); |
| + } |
| + |
| + Send(new ServiceWorkerMsg_DidGetRegistration( |
| + thread_id, request_id, info, attrs)); |
| + TRACE_EVENT_ASYNC_END1("ServiceWorker", |
| + "ServiceWorkerDispatcherHost::GetRegistration", |
| + request_id, |
| + "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.
|
| +} |
| + |
| void ServiceWorkerDispatcherHost::SendRegistrationError( |
| int thread_id, |
| int request_id, |
| @@ -633,6 +738,18 @@ void ServiceWorkerDispatcherHost::SendUnregistrationError( |
| thread_id, request_id, error_type, error_message)); |
| } |
| +void ServiceWorkerDispatcherHost::SendGetRegistrationError( |
| + int thread_id, |
| + int request_id, |
| + ServiceWorkerStatusCode status) { |
| + base::string16 error_message; |
| + blink::WebServiceWorkerError::ErrorType error_type; |
| + GetServiceWorkerRegistrationStatusResponse( |
| + status, &error_type, &error_message); |
| + Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
| + thread_id, request_id, error_type, error_message)); |
| +} |
| + |
| ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
| return context_wrapper_->context(); |
| } |