ServiceWorker: Route unregister() through WebServiceWorkerRegistration for refactorin...
[chromium-blink-merge.git] / content / child / service_worker / web_service_worker_provider_impl.cc
blob8fb9d344e1682db163662af32082f900992760dc
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"
15 using blink::WebURL;
17 namespace content {
19 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl(
20 ThreadSafeSender* thread_safe_sender,
21 ServiceWorkerProviderContext* context)
22 : thread_safe_sender_(thread_safe_sender),
23 context_(context) {
26 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
27 // Make sure the provider client is removed.
28 RemoveProviderClient();
31 void WebServiceWorkerProviderImpl::setClient(
32 blink::WebServiceWorkerProviderClient* client) {
33 if (!client) {
34 RemoveProviderClient();
35 return;
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
42 // for more context)
43 GetDispatcher()->AddProviderClient(context_->provider_id(), client);
45 if (!context_->controller())
46 return;
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 blink::WebCallbacks<bool, blink::WebServiceWorkerError>* callbacks) {
63 GetDispatcher()->DeprecatedUnregisterServiceWorker(context_->provider_id(),
64 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::getRegistrations(
75 WebServiceWorkerGetRegistrationsCallbacks* callbacks) {
76 GetDispatcher()->GetRegistrations(
77 context_->provider_id(), callbacks);
80 void WebServiceWorkerProviderImpl::getRegistrationForReady(
81 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks) {
82 GetDispatcher()->GetRegistrationForReady(context_->provider_id(), callbacks);
85 int WebServiceWorkerProviderImpl::provider_id() const {
86 return context_->provider_id();
89 void WebServiceWorkerProviderImpl::RemoveProviderClient() {
90 // Remove the provider client, but only if the dispatcher is still there.
91 // (For cleanup path we don't need to bother creating a new dispatcher)
92 ServiceWorkerDispatcher* dispatcher =
93 ServiceWorkerDispatcher::GetThreadSpecificInstance();
94 if (dispatcher)
95 dispatcher->RemoveProviderClient(context_->provider_id());
98 ServiceWorkerDispatcher* WebServiceWorkerProviderImpl::GetDispatcher() {
99 return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
100 thread_safe_sender_.get());
103 } // namespace content