Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / child / service_worker / web_service_worker_provider_impl.cc
blobca4cfc7ba37eb461362657261e8ee6872ef7753a
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"
19 using blink::WebURL;
21 namespace content {
23 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl(
24 ThreadSafeSender* thread_safe_sender,
25 ServiceWorkerProviderContext* context)
26 : thread_safe_sender_(thread_safe_sender),
27 context_(context),
28 provider_id_(context->provider_id()) {
31 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
32 // Make sure the script client is removed.
33 RemoveScriptClient();
36 void WebServiceWorkerProviderImpl::setClient(
37 blink::WebServiceWorkerProviderClient* client) {
38 if (!client) {
39 RemoveScriptClient();
40 return;
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
47 // for more context)
48 GetDispatcher()->AddScriptClient(provider_id_, client);
50 if (context_->installing_handle_id() != kInvalidServiceWorkerHandleId) {
51 client->setInstalling(GetDispatcher()->GetServiceWorker(
52 context_->installing()->info(), false));
55 if (context_->waiting_handle_id() != kInvalidServiceWorkerHandleId) {
56 client->setWaiting(GetDispatcher()->GetServiceWorker(
57 context_->waiting()->info(), false));
60 if (context_->active_handle_id() != kInvalidServiceWorkerHandleId) {
61 client->setActive(GetDispatcher()->GetServiceWorker(
62 context_->active()->info(), false));
65 if (context_->controller_handle_id() != kInvalidServiceWorkerHandleId) {
66 client->setController(GetDispatcher()->GetServiceWorker(
67 context_->controller()->info(), false));
71 void WebServiceWorkerProviderImpl::registerServiceWorker(
72 const WebURL& pattern,
73 const WebURL& script_url,
74 WebServiceWorkerRegistrationCallbacks* callbacks) {
75 GetDispatcher()->RegisterServiceWorker(
76 provider_id_, pattern, script_url, callbacks);
79 void WebServiceWorkerProviderImpl::unregisterServiceWorker(
80 const WebURL& pattern,
81 WebServiceWorkerRegistrationCallbacks* callbacks) {
82 GetDispatcher()->UnregisterServiceWorker(
83 provider_id_, pattern, callbacks);
86 void WebServiceWorkerProviderImpl::RemoveScriptClient() {
87 // Remove the script client, but only if the dispatcher is still there.
88 // (For cleanup path we don't need to bother creating a new dispatcher)
89 ServiceWorkerDispatcher* dispatcher =
90 ServiceWorkerDispatcher::GetThreadSpecificInstance();
91 if (dispatcher)
92 dispatcher->RemoveScriptClient(provider_id_);
95 ServiceWorkerDispatcher* WebServiceWorkerProviderImpl::GetDispatcher() {
96 return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
97 thread_safe_sender_.get());
100 } // namespace content