Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / child / service_worker / web_service_worker_provider_impl.cc
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.
4
5 #include "content/child/service_worker/web_service_worker_provider_impl.h"
6
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"
18
19 using blink::WebURL;
20
21 namespace content {
22
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()) {
29 }
30
31 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
32   // Make sure the script client is removed.
33   RemoveScriptClient();
34 }
35
36 void WebServiceWorkerProviderImpl::setClient(
37     blink::WebServiceWorkerProviderClient* client) {
38   if (!client) {
39     RemoveScriptClient();
40     return;
41   }
42
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   scoped_ptr<ServiceWorkerHandleReference> current =
49       context_->GetCurrentServiceWorkerHandle();
50   GetDispatcher()->AddScriptClient(provider_id_, client);
51   if (!current)
52     return;
53
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());
59   }
60 }
61
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);
68 }
69
70 void WebServiceWorkerProviderImpl::unregisterServiceWorker(
71     const WebURL& pattern,
72     WebServiceWorkerCallbacks* callbacks) {
73   GetDispatcher()->UnregisterServiceWorker(
74       provider_id_, pattern, callbacks);
75 }
76
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();
82   if (dispatcher)
83     dispatcher->RemoveScriptClient(provider_id_);
84 }
85
86 ServiceWorkerDispatcher* WebServiceWorkerProviderImpl::GetDispatcher() {
87   return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
88       thread_safe_sender_);
89 }
90
91 }  // namespace content