Commit 0d1c4968 authored by ksakamoto's avatar ksakamoto Committed by Commit bot

ServiceWorker: Implement navigator.serviceWorker.getRegistration [2/3]

This patch implements the Chromium-side change of
ServiceWorkerContainer#getRegistration().

Spec: https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator-service-worker-getRegistration

[1/3] https://codereview.chromium.org/553983010/
[3/3] https://codereview.chromium.org/540823003/

BUG=404951
TEST=content_unittests

Review URL: https://codereview.chromium.org/535753002

Cr-Commit-Position: refs/heads/master@{#294720}
parent fecaedc0
...@@ -55,6 +55,13 @@ bool CanUnregisterServiceWorker(const GURL& document_url, ...@@ -55,6 +55,13 @@ bool CanUnregisterServiceWorker(const GURL& document_url,
return document_url.GetOrigin() == pattern.GetOrigin(); 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 } // namespace
ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost(
...@@ -113,6 +120,8 @@ bool ServiceWorkerDispatcherHost::OnMessageReceived( ...@@ -113,6 +120,8 @@ bool ServiceWorkerDispatcherHost::OnMessageReceived(
OnRegisterServiceWorker) OnRegisterServiceWorker)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker,
OnUnregisterServiceWorker) OnUnregisterServiceWorker)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration,
OnGetRegistration)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated,
OnProviderCreated) OnProviderCreated)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed,
...@@ -302,6 +311,63 @@ void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( ...@@ -302,6 +311,63 @@ void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker(
request_id)); 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;
}
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (GetContext()->storage()->IsDisabled()) {
SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT);
return;
}
TRACE_EVENT_ASYNC_BEGIN1(
"ServiceWorker",
"ServiceWorkerDispatcherHost::GetRegistration",
request_id,
"Document URL", document_url.spec());
GetContext()->storage()->FindRegistrationForDocument(
document_url,
base::Bind(&ServiceWorkerDispatcherHost::GetRegistrationComplete,
this,
thread_id,
provider_id,
request_id));
}
void ServiceWorkerDispatcherHost::OnPostMessageToWorker( void ServiceWorkerDispatcherHost::OnPostMessageToWorker(
int handle_id, int handle_id,
const base::string16& message, const base::string16& message,
...@@ -389,6 +455,23 @@ ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id, ...@@ -389,6 +455,23 @@ ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id,
return NULL; return NULL;
} }
void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes(
int provider_id,
ServiceWorkerRegistration* registration,
ServiceWorkerRegistrationObjectInfo* info,
ServiceWorkerVersionAttributes* attrs) {
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( void ServiceWorkerDispatcherHost::RegistrationComplete(
int thread_id, int thread_id,
int provider_id, int provider_id,
...@@ -408,19 +491,13 @@ void ServiceWorkerDispatcherHost::RegistrationComplete( ...@@ -408,19 +491,13 @@ void ServiceWorkerDispatcherHost::RegistrationComplete(
GetContext()->GetLiveRegistration(registration_id); GetContext()->GetLiveRegistration(registration_id);
DCHECK(registration); DCHECK(registration);
ServiceWorkerRegistrationHandle* handle = ServiceWorkerRegistrationObjectInfo info;
GetOrCreateRegistrationHandle(provider_id, registration);
ServiceWorkerVersionAttributes attrs; ServiceWorkerVersionAttributes attrs;
attrs.installing = handle->CreateServiceWorkerHandleAndPass( GetRegistrationObjectInfoAndVersionAttributes(
registration->installing_version()); provider_id, registration, &info, &attrs);
attrs.waiting = handle->CreateServiceWorkerHandleAndPass(
registration->waiting_version());
attrs.active = handle->CreateServiceWorkerHandleAndPass(
registration->active_version());
Send(new ServiceWorkerMsg_ServiceWorkerRegistered( Send(new ServiceWorkerMsg_ServiceWorkerRegistered(
thread_id, request_id, handle->GetObjectInfo(), attrs)); thread_id, request_id, info, attrs));
TRACE_EVENT_ASYNC_END2("ServiceWorker", TRACE_EVENT_ASYNC_END2("ServiceWorker",
"ServiceWorkerDispatcherHost::RegisterServiceWorker", "ServiceWorkerDispatcherHost::RegisterServiceWorker",
request_id, request_id,
...@@ -609,6 +686,37 @@ void ServiceWorkerDispatcherHost::UnregistrationComplete( ...@@ -609,6 +686,37 @@ void ServiceWorkerDispatcherHost::UnregistrationComplete(
"Status", status); "Status", status);
} }
void ServiceWorkerDispatcherHost::GetRegistrationComplete(
int thread_id,
int provider_id,
int request_id,
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& registration) {
TRACE_EVENT_ASYNC_END1("ServiceWorker",
"ServiceWorkerDispatcherHost::GetRegistration",
request_id,
"Registration ID",
registration.get() ? registration->id()
: kInvalidServiceWorkerRegistrationId);
if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) {
SendGetRegistrationError(thread_id, request_id, status);
return;
}
ServiceWorkerRegistrationObjectInfo info;
ServiceWorkerVersionAttributes attrs;
if (status == SERVICE_WORKER_OK) {
DCHECK(registration.get());
if (!registration->is_uninstalling()) {
GetRegistrationObjectInfoAndVersionAttributes(
provider_id, registration.get(), &info, &attrs);
}
}
Send(new ServiceWorkerMsg_DidGetRegistration(
thread_id, request_id, info, attrs));
}
void ServiceWorkerDispatcherHost::SendRegistrationError( void ServiceWorkerDispatcherHost::SendRegistrationError(
int thread_id, int thread_id,
int request_id, int request_id,
...@@ -633,6 +741,18 @@ void ServiceWorkerDispatcherHost::SendUnregistrationError( ...@@ -633,6 +741,18 @@ void ServiceWorkerDispatcherHost::SendUnregistrationError(
thread_id, request_id, error_type, error_message)); 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() { ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() {
return context_wrapper_->context(); return context_wrapper_->context();
} }
......
...@@ -25,6 +25,8 @@ class ServiceWorkerHandle; ...@@ -25,6 +25,8 @@ class ServiceWorkerHandle;
class ServiceWorkerProviderHost; class ServiceWorkerProviderHost;
class ServiceWorkerRegistration; class ServiceWorkerRegistration;
class ServiceWorkerRegistrationHandle; class ServiceWorkerRegistrationHandle;
struct ServiceWorkerRegistrationObjectInfo;
struct ServiceWorkerVersionAttributes;
class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter { class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
public: public:
...@@ -79,6 +81,10 @@ class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter { ...@@ -79,6 +81,10 @@ class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
int request_id, int request_id,
int provider_id, int provider_id,
const GURL& pattern); const GURL& pattern);
void OnGetRegistration(int thread_id,
int request_id,
int provider_id,
const GURL& document_url);
void OnProviderCreated(int provider_id); void OnProviderCreated(int provider_id);
void OnProviderDestroyed(int provider_id); void OnProviderDestroyed(int provider_id);
void OnSetHostedVersionId(int provider_id, int64 version_id); void OnSetHostedVersionId(int provider_id, int64 version_id);
...@@ -112,6 +118,12 @@ class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter { ...@@ -112,6 +118,12 @@ class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
int provider_id, int provider_id,
int64 registration_id); int64 registration_id);
void GetRegistrationObjectInfoAndVersionAttributes(
int provider_id,
ServiceWorkerRegistration* registration,
ServiceWorkerRegistrationObjectInfo* info,
ServiceWorkerVersionAttributes* attrs);
// Callbacks from ServiceWorkerContextCore // Callbacks from ServiceWorkerContextCore
void RegistrationComplete(int thread_id, void RegistrationComplete(int thread_id,
int provider_id, int provider_id,
...@@ -124,6 +136,13 @@ class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter { ...@@ -124,6 +136,13 @@ class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
int request_id, int request_id,
ServiceWorkerStatusCode status); ServiceWorkerStatusCode status);
void GetRegistrationComplete(
int thread_id,
int provider_id,
int request_id,
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& registration);
void SendRegistrationError(int thread_id, void SendRegistrationError(int thread_id,
int request_id, int request_id,
ServiceWorkerStatusCode status); ServiceWorkerStatusCode status);
...@@ -132,6 +151,10 @@ class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter { ...@@ -132,6 +151,10 @@ class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
int request_id, int request_id,
ServiceWorkerStatusCode status); ServiceWorkerStatusCode status);
void SendGetRegistrationError(int thread_id,
int request_id,
ServiceWorkerStatusCode status);
ServiceWorkerContextCore* GetContext(); ServiceWorkerContextCore* GetContext();
int render_process_id_; int render_process_id_;
......
...@@ -103,6 +103,22 @@ class ServiceWorkerDispatcherHostTest : public testing::Test { ...@@ -103,6 +103,22 @@ class ServiceWorkerDispatcherHostTest : public testing::Test {
dispatcher_host_->ipc_sink()->ClearMessages(); dispatcher_host_->ipc_sink()->ClearMessages();
} }
void SendGetRegistration(int64 provider_id, GURL document_url) {
dispatcher_host_->OnMessageReceived(
ServiceWorkerHostMsg_GetRegistration(
-1, -1, provider_id, document_url));
base::RunLoop().RunUntilIdle();
}
void GetRegistration(int64 provider_id,
GURL document_url,
uint32 expected_message) {
SendGetRegistration(provider_id, document_url);
EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching(
expected_message));
dispatcher_host_->ipc_sink()->ClearMessages();
}
TestBrowserThreadBundle browser_thread_bundle_; TestBrowserThreadBundle browser_thread_bundle_;
scoped_ptr<EmbeddedWorkerTestHelper> helper_; scoped_ptr<EmbeddedWorkerTestHelper> helper_;
scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_;
...@@ -235,4 +251,40 @@ TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { ...@@ -235,4 +251,40 @@ TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) {
EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId));
} }
TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) {
const int64 kProviderId = 99; // Dummy value
scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL));
host->SetDocumentUrl(GURL("https://www.example.com/foo"));
base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr();
context()->AddProviderHost(host.Pass());
GetRegistration(kProviderId,
GURL("https://www.example.com/"),
ServiceWorkerMsg_DidGetRegistration::ID);
}
TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_CrossOrigin) {
const int64 kProviderId = 99; // Dummy value
scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL));
host->SetDocumentUrl(GURL("https://www.example.com/foo"));
base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr();
context()->AddProviderHost(host.Pass());
SendGetRegistration(kProviderId, GURL("https://foo.example.com/"));
EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_);
}
TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_EarlyContextDeletion) {
helper_->ShutdownContext();
// Let the shutdown reach the simulated IO thread.
base::RunLoop().RunUntilIdle();
GetRegistration(-1,
GURL(),
ServiceWorkerMsg_ServiceWorkerGetRegistrationError::ID);
}
} // namespace content } // namespace content
...@@ -61,10 +61,14 @@ void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { ...@@ -61,10 +61,14 @@ void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) {
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered,
OnUnregistered) OnUnregistered)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetRegistration,
OnDidGetRegistration)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError,
OnRegistrationError) OnRegistrationError)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistrationError, IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistrationError,
OnUnregistrationError) OnUnregistrationError)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerGetRegistrationError,
OnGetRegistrationError)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerStateChanged, IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerStateChanged,
OnServiceWorkerStateChanged) OnServiceWorkerStateChanged)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetVersionAttributes, IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetVersionAttributes,
...@@ -135,6 +139,30 @@ void ServiceWorkerDispatcher::UnregisterServiceWorker( ...@@ -135,6 +139,30 @@ void ServiceWorkerDispatcher::UnregisterServiceWorker(
CurrentWorkerId(), request_id, provider_id, pattern)); CurrentWorkerId(), request_id, provider_id, pattern));
} }
void ServiceWorkerDispatcher::GetRegistration(
int provider_id,
const GURL& document_url,
WebServiceWorkerRegistrationCallbacks* callbacks) {
DCHECK(callbacks);
if (document_url.possibly_invalid_spec().size() > GetMaxURLChars()) {
scoped_ptr<WebServiceWorkerRegistrationCallbacks>
owned_callbacks(callbacks);
scoped_ptr<WebServiceWorkerError> error(new WebServiceWorkerError(
WebServiceWorkerError::ErrorTypeSecurity, "URL too long"));
callbacks->onError(error.release());
return;
}
int request_id = pending_get_registration_callbacks_.Add(callbacks);
TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker",
"ServiceWorkerDispatcher::GetRegistration",
request_id,
"Document URL", document_url.spec());
thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetRegistration(
CurrentWorkerId(), request_id, provider_id, document_url));
}
void ServiceWorkerDispatcher::AddProviderContext( void ServiceWorkerDispatcher::AddProviderContext(
ServiceWorkerProviderContext* provider_context) { ServiceWorkerProviderContext* provider_context) {
DCHECK(provider_context); DCHECK(provider_context);
...@@ -303,25 +331,7 @@ void ServiceWorkerDispatcher::OnRegistered( ...@@ -303,25 +331,7 @@ void ServiceWorkerDispatcher::OnRegistered(
if (!callbacks) if (!callbacks)
return; return;
WebServiceWorkerRegistrationImpl* registration = callbacks->onSuccess(FindOrCreateRegistration(info, attrs));
FindServiceWorkerRegistration(info, true);
if (!registration) {
registration = CreateServiceWorkerRegistration(info, true);
registration->SetInstalling(GetServiceWorker(attrs.installing, true));
registration->SetWaiting(GetServiceWorker(attrs.waiting, true));
registration->SetActive(GetServiceWorker(attrs.active, true));
} else {
// |registration| must already have version attributes, so adopt and destroy
// handle refs for them.
ServiceWorkerHandleReference::Adopt(
attrs.installing, thread_safe_sender_.get());
ServiceWorkerHandleReference::Adopt(
attrs.waiting, thread_safe_sender_.get());
ServiceWorkerHandleReference::Adopt(
attrs.active, thread_safe_sender_.get());
}
callbacks->onSuccess(registration);
pending_registration_callbacks_.Remove(request_id); pending_registration_callbacks_.Remove(request_id);
TRACE_EVENT_ASYNC_END0("ServiceWorker", TRACE_EVENT_ASYNC_END0("ServiceWorker",
"ServiceWorkerDispatcher::RegisterServiceWorker", "ServiceWorkerDispatcher::RegisterServiceWorker",
...@@ -348,6 +358,33 @@ void ServiceWorkerDispatcher::OnUnregistered(int thread_id, ...@@ -348,6 +358,33 @@ void ServiceWorkerDispatcher::OnUnregistered(int thread_id,
request_id); request_id);
} }
void ServiceWorkerDispatcher::OnDidGetRegistration(
int thread_id,
int request_id,
const ServiceWorkerRegistrationObjectInfo& info,
const ServiceWorkerVersionAttributes& attrs) {
WebServiceWorkerRegistrationCallbacks* callbacks =
pending_get_registration_callbacks_.Lookup(request_id);
TRACE_EVENT_ASYNC_STEP_INTO0(
"ServiceWorker",
"ServiceWorkerDispatcher::GetRegistration",
request_id,
"OnDidGetRegistration");
DCHECK(callbacks);
if (!callbacks)
return;
WebServiceWorkerRegistrationImpl* registration = NULL;
if (info.handle_id != kInvalidServiceWorkerHandleId)
registration = FindOrCreateRegistration(info, attrs);
callbacks->onSuccess(registration);
pending_get_registration_callbacks_.Remove(request_id);
TRACE_EVENT_ASYNC_END0("ServiceWorker",
"ServiceWorkerDispatcher::GetRegistration",
request_id);
}
void ServiceWorkerDispatcher::OnRegistrationError( void ServiceWorkerDispatcher::OnRegistrationError(
int thread_id, int thread_id,
int request_id, int request_id,
...@@ -397,6 +434,31 @@ void ServiceWorkerDispatcher::OnUnregistrationError( ...@@ -397,6 +434,31 @@ void ServiceWorkerDispatcher::OnUnregistrationError(
request_id); request_id);
} }
void ServiceWorkerDispatcher::OnGetRegistrationError(
int thread_id,
int request_id,
WebServiceWorkerError::ErrorType error_type,
const base::string16& message) {
TRACE_EVENT_ASYNC_STEP_INTO0(
"ServiceWorker",
"ServiceWorkerDispatcher::GetRegistration",
request_id,
"OnGetRegistrationError");
WebServiceWorkerGetRegistrationCallbacks* callbacks =
pending_get_registration_callbacks_.Lookup(request_id);
DCHECK(callbacks);
if (!callbacks)
return;
scoped_ptr<WebServiceWorkerError> error(
new WebServiceWorkerError(error_type, message));
callbacks->onError(error.release());
pending_get_registration_callbacks_.Remove(request_id);
TRACE_EVENT_ASYNC_END0("ServiceWorker",
"ServiceWorkerDispatcher::GetRegistration",
request_id);
}
void ServiceWorkerDispatcher::OnServiceWorkerStateChanged( void ServiceWorkerDispatcher::OnServiceWorkerStateChanged(
int thread_id, int thread_id,
int handle_id, int handle_id,
...@@ -650,4 +712,28 @@ void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( ...@@ -650,4 +712,28 @@ void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration(
registrations_.erase(registration_handle_id); registrations_.erase(registration_handle_id);
} }
WebServiceWorkerRegistrationImpl*
ServiceWorkerDispatcher::FindOrCreateRegistration(
const ServiceWorkerRegistrationObjectInfo& info,
const ServiceWorkerVersionAttributes& attrs) {
WebServiceWorkerRegistrationImpl* registration =
FindServiceWorkerRegistration(info, true);
if (!registration) {
registration = CreateServiceWorkerRegistration(info, true);
registration->SetInstalling(GetServiceWorker(attrs.installing, true));
registration->SetWaiting(GetServiceWorker(attrs.waiting, true));
registration->SetActive(GetServiceWorker(attrs.active, true));
} else {
// |registration| must already have version attributes, so adopt and destroy
// handle refs for them.
ServiceWorkerHandleReference::Adopt(
attrs.installing, thread_safe_sender_.get());
ServiceWorkerHandleReference::Adopt(
attrs.waiting, thread_safe_sender_.get());
ServiceWorkerHandleReference::Adopt(
attrs.active, thread_safe_sender_.get());
}
return registration;
}
} // namespace content } // namespace content
...@@ -47,6 +47,9 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer { ...@@ -47,6 +47,9 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer {
typedef typedef
blink::WebServiceWorkerProvider::WebServiceWorkerUnregistrationCallbacks blink::WebServiceWorkerProvider::WebServiceWorkerUnregistrationCallbacks
WebServiceWorkerUnregistrationCallbacks; WebServiceWorkerUnregistrationCallbacks;
typedef
blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationCallbacks
WebServiceWorkerGetRegistrationCallbacks;
explicit ServiceWorkerDispatcher(ThreadSafeSender* thread_safe_sender); explicit ServiceWorkerDispatcher(ThreadSafeSender* thread_safe_sender);
virtual ~ServiceWorkerDispatcher(); virtual ~ServiceWorkerDispatcher();
...@@ -65,6 +68,11 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer { ...@@ -65,6 +68,11 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer {
int provider_id, int provider_id,
const GURL& pattern, const GURL& pattern,
WebServiceWorkerUnregistrationCallbacks* callbacks); WebServiceWorkerUnregistrationCallbacks* callbacks);
// Corresponds to navigator.serviceWorker.getRegistration()
void GetRegistration(
int provider_id,
const GURL& document_url,
WebServiceWorkerRegistrationCallbacks* callbacks);
// Called when a new provider context for a document is created. Usually // Called when a new provider context for a document is created. Usually
// this happens when a new document is being loaded, and is called much // this happens when a new document is being loaded, and is called much
...@@ -126,6 +134,8 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer { ...@@ -126,6 +134,8 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer {
IDMapOwnPointer> RegistrationCallbackMap; IDMapOwnPointer> RegistrationCallbackMap;
typedef IDMap<WebServiceWorkerUnregistrationCallbacks, typedef IDMap<WebServiceWorkerUnregistrationCallbacks,
IDMapOwnPointer> UnregistrationCallbackMap; IDMapOwnPointer> UnregistrationCallbackMap;
typedef IDMap<WebServiceWorkerGetRegistrationCallbacks,
IDMapOwnPointer> GetRegistrationCallbackMap;
typedef std::map<int, blink::WebServiceWorkerProviderClient*> ScriptClientMap; typedef std::map<int, blink::WebServiceWorkerProviderClient*> ScriptClientMap;
typedef std::map<int, ServiceWorkerProviderContext*> ProviderContextMap; typedef std::map<int, ServiceWorkerProviderContext*> ProviderContextMap;
typedef std::map<int, WebServiceWorkerImpl*> WorkerObjectMap; typedef std::map<int, WebServiceWorkerImpl*> WorkerObjectMap;
...@@ -152,6 +162,10 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer { ...@@ -152,6 +162,10 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer {
void OnUnregistered(int thread_id, void OnUnregistered(int thread_id,
int request_id, int request_id,
bool is_success); bool is_success);
void OnDidGetRegistration(int thread_id,
int request_id,
const ServiceWorkerRegistrationObjectInfo& info,
const ServiceWorkerVersionAttributes& attrs);
void OnRegistrationError(int thread_id, void OnRegistrationError(int thread_id,
int request_id, int request_id,
blink::WebServiceWorkerError::ErrorType error_type, blink::WebServiceWorkerError::ErrorType error_type,
...@@ -160,6 +174,11 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer { ...@@ -160,6 +174,11 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer {
int request_id, int request_id,
blink::WebServiceWorkerError::ErrorType error_type, blink::WebServiceWorkerError::ErrorType error_type,
const base::string16& message); const base::string16& message);
void OnGetRegistrationError(
int thread_id,
int request_id,
blink::WebServiceWorkerError::ErrorType error_type,
const base::string16& message);
void OnServiceWorkerStateChanged(int thread_id, void OnServiceWorkerStateChanged(int thread_id,
int handle_id, int handle_id,
blink::WebServiceWorkerState state); blink::WebServiceWorkerState state);
...@@ -206,8 +225,13 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer { ...@@ -206,8 +225,13 @@ class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer {
void RemoveServiceWorkerRegistration( void RemoveServiceWorkerRegistration(
int registration_handle_id); int registration_handle_id);
WebServiceWorkerRegistrationImpl* FindOrCreateRegistration(
const ServiceWorkerRegistrationObjectInfo& info,
const ServiceWorkerVersionAttributes& attrs);
RegistrationCallbackMap pending_registration_callbacks_; RegistrationCallbackMap pending_registration_callbacks_;
UnregistrationCallbackMap pending_unregistration_callbacks_; UnregistrationCallbackMap pending_unregistration_callbacks_;
GetRegistrationCallbackMap pending_get_registration_callbacks_;
ScriptClientMap script_clients_; ScriptClientMap script_clients_;
ProviderContextMap provider_contexts_; ProviderContextMap provider_contexts_;
WorkerObjectMap service_workers_; WorkerObjectMap service_workers_;
......
...@@ -94,6 +94,12 @@ void WebServiceWorkerProviderImpl::unregisterServiceWorker( ...@@ -94,6 +94,12 @@ void WebServiceWorkerProviderImpl::unregisterServiceWorker(
provider_id_, pattern, callbacks); provider_id_, pattern, callbacks);
} }
void WebServiceWorkerProviderImpl::getRegistration(
const blink::WebURL& document_url,
WebServiceWorkerRegistrationCallbacks* callbacks) {
GetDispatcher()->GetRegistration(provider_id_, document_url, callbacks);
}
void WebServiceWorkerProviderImpl::RemoveScriptClient() { void WebServiceWorkerProviderImpl::RemoveScriptClient() {
// Remove the script client, but only if the dispatcher is still there. // Remove the script client, but only if the dispatcher is still there.
// (For cleanup path we don't need to bother creating a new dispatcher) // (For cleanup path we don't need to bother creating a new dispatcher)
......
...@@ -40,6 +40,9 @@ class WebServiceWorkerProviderImpl ...@@ -40,6 +40,9 @@ class WebServiceWorkerProviderImpl
const blink::WebURL& pattern, const blink::WebURL& pattern,
WebServiceWorkerUnregistrationCallbacks*); WebServiceWorkerUnregistrationCallbacks*);
virtual void getRegistration(const blink::WebURL& document_url,
WebServiceWorkerGetRegistrationCallbacks*);
ServiceWorkerProviderContext* context() { return context_.get(); } ServiceWorkerProviderContext* context() { return context_.get(); }
int provider_id() const { return provider_id_; } int provider_id() const { return provider_id_; }
......
...@@ -90,6 +90,12 @@ IPC_MESSAGE_CONTROL4(ServiceWorkerHostMsg_UnregisterServiceWorker, ...@@ -90,6 +90,12 @@ IPC_MESSAGE_CONTROL4(ServiceWorkerHostMsg_UnregisterServiceWorker,
int /* provider_id */, int /* provider_id */,
GURL /* scope (url pattern) */) GURL /* scope (url pattern) */)
IPC_MESSAGE_CONTROL4(ServiceWorkerHostMsg_GetRegistration,
int /* thread_id */,
int /* request_id */,
int /* provider_id */,
GURL /* document_url */)
// Sends a 'message' event to a service worker (renderer->browser). // Sends a 'message' event to a service worker (renderer->browser).
IPC_MESSAGE_CONTROL3(ServiceWorkerHostMsg_PostMessageToWorker, IPC_MESSAGE_CONTROL3(ServiceWorkerHostMsg_PostMessageToWorker,
int /* handle_id */, int /* handle_id */,
...@@ -195,19 +201,26 @@ IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_DisassociateRegistration, ...@@ -195,19 +201,26 @@ IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_DisassociateRegistration,
int /* thread_id */, int /* thread_id */,
int /* provider_id */) int /* provider_id */)
// Response to ServiceWorkerMsg_RegisterServiceWorker. // Response to ServiceWorkerHostMsg_RegisterServiceWorker.
IPC_MESSAGE_CONTROL4(ServiceWorkerMsg_ServiceWorkerRegistered, IPC_MESSAGE_CONTROL4(ServiceWorkerMsg_ServiceWorkerRegistered,
int /* thread_id */, int /* thread_id */,
int /* request_id */, int /* request_id */,
content::ServiceWorkerRegistrationObjectInfo, content::ServiceWorkerRegistrationObjectInfo,
content::ServiceWorkerVersionAttributes) content::ServiceWorkerVersionAttributes)
// Response to ServiceWorkerMsg_UnregisterServiceWorker. // Response to ServiceWorkerHostMsg_UnregisterServiceWorker.
IPC_MESSAGE_CONTROL3(ServiceWorkerMsg_ServiceWorkerUnregistered, IPC_MESSAGE_CONTROL3(ServiceWorkerMsg_ServiceWorkerUnregistered,
int /* thread_id */, int /* thread_id */,
int /* request_id */, int /* request_id */,
bool /* is_success */) bool /* is_success */)
// Response to ServiceWorkerHostMsg_GetRegistration.
IPC_MESSAGE_CONTROL4(ServiceWorkerMsg_DidGetRegistration,
int /* thread_id */,
int /* request_id */,
content::ServiceWorkerRegistrationObjectInfo,
content::ServiceWorkerVersionAttributes)
// Sent when any kind of registration error occurs during a // Sent when any kind of registration error occurs during a
// RegisterServiceWorker handler above. // RegisterServiceWorker handler above.
IPC_MESSAGE_CONTROL4(ServiceWorkerMsg_ServiceWorkerRegistrationError, IPC_MESSAGE_CONTROL4(ServiceWorkerMsg_ServiceWorkerRegistrationError,
...@@ -224,6 +237,14 @@ IPC_MESSAGE_CONTROL4(ServiceWorkerMsg_ServiceWorkerUnregistrationError, ...@@ -224,6 +237,14 @@ IPC_MESSAGE_CONTROL4(ServiceWorkerMsg_ServiceWorkerUnregistrationError,
blink::WebServiceWorkerError::ErrorType /* code */, blink::WebServiceWorkerError::ErrorType /* code */,
base::string16 /* message */) base::string16 /* message */)
// Sent when any kind of registration error occurs during a
// GetRegistration handler above.
IPC_MESSAGE_CONTROL4(ServiceWorkerMsg_ServiceWorkerGetRegistrationError,
int /* thread_id */,
int /* request_id */,
blink::WebServiceWorkerError::ErrorType /* code */,
base::string16 /* message */)
// Informs the child process that the ServiceWorker's state has changed. // Informs the child process that the ServiceWorker's state has changed.
IPC_MESSAGE_CONTROL3(ServiceWorkerMsg_ServiceWorkerStateChanged, IPC_MESSAGE_CONTROL3(ServiceWorkerMsg_ServiceWorkerStateChanged,
int /* thread_id */, int /* thread_id */,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment