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 "content/child/service_worker/service_worker_dispatcher.h"
8 #include "content/child/service_worker/service_worker_handle_reference.h"
9 #include "content/child/service_worker/service_worker_provider_context.h"
10 #include "content/child/service_worker/web_service_worker_impl.h"
11 #include "content/child/thread_safe_sender.h"
12 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h"
13 #include "third_party/WebKit/public/platform/WebURL.h"
19 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl(
20 ThreadSafeSender
* thread_safe_sender
,
21 ServiceWorkerProviderContext
* context
)
22 : thread_safe_sender_(thread_safe_sender
),
26 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
27 // Make sure the provider client is removed.
28 RemoveProviderClient();
31 void WebServiceWorkerProviderImpl::setClient(
32 blink::WebServiceWorkerProviderClient
* client
) {
34 RemoveProviderClient();
38 // TODO(kinuko): Here we could also register the current thread ID
39 // on the provider context so that multiple WebServiceWorkerProviderImpl
40 // (e.g. on document and on dedicated workers) can properly share
41 // the single provider context across threads. (http://crbug.com/366538
43 GetDispatcher()->AddProviderClient(context_
->provider_id(), client
);
45 if (!context_
->controller())
47 client
->setController(
48 GetDispatcher()->GetServiceWorker(context_
->controller()->info(), false),
49 false /* shouldNotifyControllerChange */);
52 void WebServiceWorkerProviderImpl::registerServiceWorker(
53 const WebURL
& pattern
,
54 const WebURL
& script_url
,
55 WebServiceWorkerRegistrationCallbacks
* callbacks
) {
56 GetDispatcher()->RegisterServiceWorker(
57 context_
->provider_id(), pattern
, script_url
, callbacks
);
60 void WebServiceWorkerProviderImpl::unregisterServiceWorker(
61 const WebURL
& pattern
,
62 WebServiceWorkerUnregistrationCallbacks
* callbacks
) {
63 GetDispatcher()->UnregisterServiceWorker(
64 context_
->provider_id(), pattern
, callbacks
);
67 void WebServiceWorkerProviderImpl::getRegistration(
68 const blink::WebURL
& document_url
,
69 WebServiceWorkerRegistrationCallbacks
* callbacks
) {
70 GetDispatcher()->GetRegistration(
71 context_
->provider_id(), document_url
, callbacks
);
74 void WebServiceWorkerProviderImpl::getRegistrationForReady(
75 WebServiceWorkerGetRegistrationForReadyCallbacks
* callbacks
) {
76 GetDispatcher()->GetRegistrationForReady(context_
->provider_id(), callbacks
);
79 void WebServiceWorkerProviderImpl::RemoveProviderClient() {
80 // Remove the provider client, but only if the dispatcher is still there.
81 // (For cleanup path we don't need to bother creating a new dispatcher)
82 ServiceWorkerDispatcher
* dispatcher
=
83 ServiceWorkerDispatcher::GetThreadSpecificInstance();
85 dispatcher
->RemoveProviderClient(context_
->provider_id());
88 ServiceWorkerDispatcher
* WebServiceWorkerProviderImpl::GetDispatcher() {
89 return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
90 thread_safe_sender_
.get());
93 } // namespace content