1 // Copyright 2014 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_registration_impl.h"
7 #include "content/child/service_worker/service_worker_dispatcher.h"
8 #include "content/child/service_worker/service_worker_registration_handle_reference.h"
9 #include "content/child/service_worker/web_service_worker_impl.h"
10 #include "content/child/service_worker/web_service_worker_provider_impl.h"
11 #include "content/common/service_worker/service_worker_types.h"
12 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerRegistrationProxy.h"
18 class HandleImpl
: public blink::WebServiceWorkerRegistration::Handle
{
20 explicit HandleImpl(WebServiceWorkerRegistrationImpl
* registration
)
21 : registration_(registration
) {}
22 virtual ~HandleImpl() {}
24 virtual blink::WebServiceWorkerRegistration
* registration() {
25 return registration_
.get();
29 scoped_refptr
<WebServiceWorkerRegistrationImpl
> registration_
;
31 DISALLOW_COPY_AND_ASSIGN(HandleImpl
);
36 WebServiceWorkerRegistrationImpl::QueuedTask::QueuedTask(
38 blink::WebServiceWorker
* worker
)
43 WebServiceWorkerRegistrationImpl::WebServiceWorkerRegistrationImpl(
44 scoped_ptr
<ServiceWorkerRegistrationHandleReference
> handle_ref
)
45 : handle_ref_(handle_ref
.Pass()),
48 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId
,
49 handle_ref_
->handle_id());
50 ServiceWorkerDispatcher
* dispatcher
=
51 ServiceWorkerDispatcher::GetThreadSpecificInstance();
53 dispatcher
->AddServiceWorkerRegistration(handle_ref_
->handle_id(), this);
56 void WebServiceWorkerRegistrationImpl::SetInstalling(
57 blink::WebServiceWorker
* service_worker
) {
59 proxy_
->setInstalling(service_worker
);
61 queued_tasks_
.push_back(QueuedTask(INSTALLING
, service_worker
));
64 void WebServiceWorkerRegistrationImpl::SetWaiting(
65 blink::WebServiceWorker
* service_worker
) {
67 proxy_
->setWaiting(service_worker
);
69 queued_tasks_
.push_back(QueuedTask(WAITING
, service_worker
));
72 void WebServiceWorkerRegistrationImpl::SetActive(
73 blink::WebServiceWorker
* service_worker
) {
75 proxy_
->setActive(service_worker
);
77 queued_tasks_
.push_back(QueuedTask(ACTIVE
, service_worker
));
80 void WebServiceWorkerRegistrationImpl::OnUpdateFound() {
82 proxy_
->dispatchUpdateFoundEvent();
84 queued_tasks_
.push_back(QueuedTask(UPDATE_FOUND
, NULL
));
87 void WebServiceWorkerRegistrationImpl::setProxy(
88 blink::WebServiceWorkerRegistrationProxy
* proxy
) {
93 void WebServiceWorkerRegistrationImpl::RunQueuedTasks() {
95 for (std::vector
<QueuedTask
>::const_iterator it
= queued_tasks_
.begin();
96 it
!= queued_tasks_
.end(); ++it
) {
97 if (it
->type
== INSTALLING
)
98 proxy_
->setInstalling(it
->worker
);
99 else if (it
->type
== WAITING
)
100 proxy_
->setWaiting(it
->worker
);
101 else if (it
->type
== ACTIVE
)
102 proxy_
->setActive(it
->worker
);
103 else if (it
->type
== UPDATE_FOUND
)
104 proxy_
->dispatchUpdateFoundEvent();
106 queued_tasks_
.clear();
109 void WebServiceWorkerRegistrationImpl::ClearQueuedTasks() {
110 for (std::vector
<QueuedTask
>::const_iterator it
= queued_tasks_
.begin();
111 it
!= queued_tasks_
.end(); ++it
) {
112 // If the owner of the WebServiceWorker does not exist, delete it.
113 if (it
->worker
&& !it
->worker
->proxy())
116 queued_tasks_
.clear();
119 blink::WebServiceWorkerRegistrationProxy
*
120 WebServiceWorkerRegistrationImpl::proxy() {
124 blink::WebURL
WebServiceWorkerRegistrationImpl::scope() const {
125 return handle_ref_
->scope();
128 void WebServiceWorkerRegistrationImpl::update(
129 blink::WebServiceWorkerProvider
* provider
,
130 WebServiceWorkerUpdateCallbacks
* callbacks
) {
131 WebServiceWorkerProviderImpl
* provider_impl
=
132 static_cast<WebServiceWorkerProviderImpl
*>(provider
);
133 ServiceWorkerDispatcher
* dispatcher
=
134 ServiceWorkerDispatcher::GetThreadSpecificInstance();
136 dispatcher
->UpdateServiceWorker(provider_impl
->provider_id(),
137 registration_id(), callbacks
);
140 void WebServiceWorkerRegistrationImpl::unregister(
141 blink::WebServiceWorkerProvider
* provider
,
142 WebServiceWorkerUnregistrationCallbacks
* callbacks
) {
143 WebServiceWorkerProviderImpl
* provider_impl
=
144 static_cast<WebServiceWorkerProviderImpl
*>(provider
);
145 ServiceWorkerDispatcher
* dispatcher
=
146 ServiceWorkerDispatcher::GetThreadSpecificInstance();
148 dispatcher
->UnregisterServiceWorker(provider_impl
->provider_id(),
149 registration_id(), callbacks
);
152 int64
WebServiceWorkerRegistrationImpl::registration_id() const {
153 return handle_ref_
->registration_id();
156 blink::WebPassOwnPtr
<blink::WebServiceWorkerRegistration::Handle
>
157 WebServiceWorkerRegistrationImpl::CreateHandle() {
158 return blink::adoptWebPtr(new HandleImpl(this));
161 blink::WebServiceWorkerRegistration::Handle
*
162 WebServiceWorkerRegistrationImpl::CreateLeakyHandle() {
163 return new HandleImpl(this);
166 WebServiceWorkerRegistrationImpl::~WebServiceWorkerRegistrationImpl() {
167 ServiceWorkerDispatcher
* dispatcher
=
168 ServiceWorkerDispatcher::GetThreadSpecificInstance();
170 dispatcher
->RemoveServiceWorkerRegistration(handle_ref_
->handle_id());
174 } // namespace content