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
3 // found in the LICENSE file.
5 #include "content/child/service_worker/web_service_worker_provider_impl.h"
7 #include "base/atomic_sequence_num.h"
8 #include "base/logging.h"
9 #include "content/child/child_thread.h"
10 #include "content/child/service_worker/service_worker_dispatcher.h"
11 #include "content/child/service_worker/service_worker_handle_reference.h"
12 #include "content/child/service_worker/service_worker_provider_context.h"
13 #include "content/child/service_worker/service_worker_registration_handle_reference.h"
14 #include "content/child/service_worker/web_service_worker_impl.h"
15 #include "content/child/service_worker/web_service_worker_registration_impl.h"
16 #include "content/child/thread_safe_sender.h"
17 #include "content/common/service_worker/service_worker_messages.h"
18 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h"
19 #include "third_party/WebKit/public/platform/WebURL.h"
25 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl(
26 ThreadSafeSender
* thread_safe_sender
,
27 ServiceWorkerProviderContext
* context
)
28 : thread_safe_sender_(thread_safe_sender
),
30 provider_id_(context
->provider_id()) {
33 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
34 // Make sure the script client is removed.
38 void WebServiceWorkerProviderImpl::setClient(
39 blink::WebServiceWorkerProviderClient
* client
) {
45 // TODO(kinuko): Here we could also register the current thread ID
46 // on the provider context so that multiple WebServiceWorkerProviderImpl
47 // (e.g. on document and on dedicated workers) can properly share
48 // the single provider context across threads. (http://crbug.com/366538
50 GetDispatcher()->AddScriptClient(provider_id_
, client
);
52 if (!context_
->registration()) {
53 // This provider is not associated with any registration.
57 // Set .ready if the associated registration has the active service worker.
58 if (context_
->active_handle_id() != kInvalidServiceWorkerHandleId
) {
59 WebServiceWorkerRegistrationImpl
* registration
=
60 GetDispatcher()->FindServiceWorkerRegistration(
61 context_
->registration()->info(), false);
63 registration
= GetDispatcher()->CreateServiceWorkerRegistration(
64 context_
->registration()->info(), false);
65 ServiceWorkerVersionAttributes attrs
= context_
->GetVersionAttributes();
66 registration
->SetInstalling(
67 GetDispatcher()->GetServiceWorker(attrs
.installing
, false));
68 registration
->SetWaiting(
69 GetDispatcher()->GetServiceWorker(attrs
.waiting
, false));
70 registration
->SetActive(
71 GetDispatcher()->GetServiceWorker(attrs
.active
, false));
73 client
->setReadyRegistration(registration
);
76 if (context_
->controller_handle_id() != kInvalidServiceWorkerHandleId
) {
77 client
->setController(GetDispatcher()->GetServiceWorker(
78 context_
->controller()->info(), false),
79 false /* shouldNotifyControllerChange */);
83 void WebServiceWorkerProviderImpl::registerServiceWorker(
84 const WebURL
& pattern
,
85 const WebURL
& script_url
,
86 WebServiceWorkerRegistrationCallbacks
* callbacks
) {
87 GetDispatcher()->RegisterServiceWorker(
88 provider_id_
, pattern
, script_url
, callbacks
);
91 void WebServiceWorkerProviderImpl::unregisterServiceWorker(
92 const WebURL
& pattern
,
93 WebServiceWorkerUnregistrationCallbacks
* callbacks
) {
94 GetDispatcher()->UnregisterServiceWorker(
95 provider_id_
, pattern
, callbacks
);
98 void WebServiceWorkerProviderImpl::getRegistration(
99 const blink::WebURL
& document_url
,
100 WebServiceWorkerRegistrationCallbacks
* callbacks
) {
101 GetDispatcher()->GetRegistration(provider_id_
, document_url
, callbacks
);
104 void WebServiceWorkerProviderImpl::RemoveScriptClient() {
105 // Remove the script client, but only if the dispatcher is still there.
106 // (For cleanup path we don't need to bother creating a new dispatcher)
107 ServiceWorkerDispatcher
* dispatcher
=
108 ServiceWorkerDispatcher::GetThreadSpecificInstance();
110 dispatcher
->RemoveScriptClient(provider_id_
);
113 ServiceWorkerDispatcher
* WebServiceWorkerProviderImpl::GetDispatcher() {
114 return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
115 thread_safe_sender_
.get());
118 } // namespace content