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 "content/common/service_worker/service_worker_utils.h"
13 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h"
14 #include "third_party/WebKit/public/platform/WebURL.h"
20 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl(
21 ThreadSafeSender
* thread_safe_sender
,
22 ServiceWorkerProviderContext
* context
)
23 : thread_safe_sender_(thread_safe_sender
),
28 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
29 // Make sure the provider client is removed.
30 RemoveProviderClient();
33 void WebServiceWorkerProviderImpl::setClient(
34 blink::WebServiceWorkerProviderClient
* client
) {
36 RemoveProviderClient();
40 // TODO(kinuko): Here we could also register the current thread ID
41 // on the provider context so that multiple WebServiceWorkerProviderImpl
42 // (e.g. on document and on dedicated workers) can properly share
43 // the single provider context across threads. (http://crbug.com/366538
45 GetDispatcher()->AddProviderClient(context_
->provider_id(), client
);
47 if (!context_
->controller())
49 client
->setController(
50 GetDispatcher()->GetServiceWorker(context_
->controller()->info(), false),
51 false /* shouldNotifyControllerChange */);
54 void WebServiceWorkerProviderImpl::registerServiceWorker(
55 const WebURL
& pattern
,
56 const WebURL
& script_url
,
57 WebServiceWorkerRegistrationCallbacks
* callbacks
) {
58 GetDispatcher()->RegisterServiceWorker(
59 context_
->provider_id(), pattern
, script_url
, callbacks
);
62 void WebServiceWorkerProviderImpl::getRegistration(
63 const blink::WebURL
& document_url
,
64 WebServiceWorkerRegistrationCallbacks
* callbacks
) {
65 GetDispatcher()->GetRegistration(
66 context_
->provider_id(), document_url
, callbacks
);
69 void WebServiceWorkerProviderImpl::getRegistrations(
70 WebServiceWorkerGetRegistrationsCallbacks
* callbacks
) {
71 GetDispatcher()->GetRegistrations(
72 context_
->provider_id(), callbacks
);
75 void WebServiceWorkerProviderImpl::getRegistrationForReady(
76 WebServiceWorkerGetRegistrationForReadyCallbacks
* callbacks
) {
77 GetDispatcher()->GetRegistrationForReady(context_
->provider_id(), callbacks
);
80 bool WebServiceWorkerProviderImpl::validateScopeAndScriptURL(
81 const blink::WebURL
& scope
,
82 const blink::WebURL
& script_url
,
83 blink::WebString
* error_message
) {
85 bool has_error
= ServiceWorkerUtils::ContainsDisallowedCharacter(
86 scope
, script_url
, &error
);
88 *error_message
= blink::WebString::fromUTF8(error
);
92 int WebServiceWorkerProviderImpl::provider_id() const {
93 return context_
->provider_id();
96 void WebServiceWorkerProviderImpl::RemoveProviderClient() {
97 // Remove the provider client, but only if the dispatcher is still there.
98 // (For cleanup path we don't need to bother creating a new dispatcher)
99 ServiceWorkerDispatcher
* dispatcher
=
100 ServiceWorkerDispatcher::GetThreadSpecificInstance();
102 dispatcher
->RemoveProviderClient(context_
->provider_id());
105 ServiceWorkerDispatcher
* WebServiceWorkerProviderImpl::GetDispatcher() {
106 return ServiceWorkerDispatcher::GetThreadSpecificInstance();
109 } // namespace content