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/web_service_worker_impl.h"
14 #include "content/child/thread_safe_sender.h"
15 #include "content/common/service_worker/service_worker_messages.h"
16 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h"
17 #include "third_party/WebKit/public/platform/WebURL.h"
23 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl(
24 ThreadSafeSender* thread_safe_sender,
25 ServiceWorkerProviderContext* context)
26 : thread_safe_sender_(thread_safe_sender),
28 provider_id_(context->provider_id()) {
31 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
32 // Make sure the script client is removed.
36 void WebServiceWorkerProviderImpl::setClient(
37 blink::WebServiceWorkerProviderClient* client) {
43 // TODO(kinuko): Here we could also register the current thread ID
44 // on the provider context so that multiple WebServiceWorkerProviderImpl
45 // (e.g. on document and on dedicated workers) can properly share
46 // the single provider context across threads. (http://crbug.com/366538
48 scoped_ptr<ServiceWorkerHandleReference> current =
49 context_->GetCurrentServiceWorkerHandle();
50 GetDispatcher()->AddScriptClient(provider_id_, client);
54 int handle_id = current->info().handle_id;
55 if (handle_id != kInvalidServiceWorkerHandleId) {
56 scoped_ptr<WebServiceWorkerImpl> worker(
57 new WebServiceWorkerImpl(current.Pass(), thread_safe_sender_));
58 client->setCurrentServiceWorker(worker.release());
62 void WebServiceWorkerProviderImpl::registerServiceWorker(
63 const WebURL& pattern,
64 const WebURL& script_url,
65 WebServiceWorkerCallbacks* callbacks) {
66 GetDispatcher()->RegisterServiceWorker(
67 provider_id_, pattern, script_url, callbacks);
70 void WebServiceWorkerProviderImpl::unregisterServiceWorker(
71 const WebURL& pattern,
72 WebServiceWorkerCallbacks* callbacks) {
73 GetDispatcher()->UnregisterServiceWorker(
74 provider_id_, pattern, callbacks);
77 void WebServiceWorkerProviderImpl::RemoveScriptClient() {
78 // Remove the script client, but only if the dispatcher is still there.
79 // (For cleanup path we don't need to bother creating a new dispatcher)
80 ServiceWorkerDispatcher* dispatcher =
81 ServiceWorkerDispatcher::GetThreadSpecificInstance();
83 dispatcher->RemoveScriptClient(provider_id_);
86 ServiceWorkerDispatcher* WebServiceWorkerProviderImpl::GetDispatcher() {
87 return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
91 } // namespace content