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/WebServiceWorkerRegistrationProxy.h"
16 WebServiceWorkerRegistrationImpl::QueuedTask::QueuedTask(
18 blink::WebServiceWorker
* worker
)
23 WebServiceWorkerRegistrationImpl::WebServiceWorkerRegistrationImpl(
24 scoped_ptr
<ServiceWorkerRegistrationHandleReference
> handle_ref
)
25 : handle_ref_(handle_ref
.Pass()),
28 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId
,
29 handle_ref_
->handle_id());
30 ServiceWorkerDispatcher
* dispatcher
=
31 ServiceWorkerDispatcher::GetThreadSpecificInstance();
33 dispatcher
->AddServiceWorkerRegistration(handle_ref_
->handle_id(), this);
36 WebServiceWorkerRegistrationImpl::~WebServiceWorkerRegistrationImpl() {
37 ServiceWorkerDispatcher
* dispatcher
=
38 ServiceWorkerDispatcher::GetThreadSpecificInstance();
40 dispatcher
->RemoveServiceWorkerRegistration(handle_ref_
->handle_id());
44 void WebServiceWorkerRegistrationImpl::SetInstalling(
45 blink::WebServiceWorker
* service_worker
) {
47 proxy_
->setInstalling(service_worker
);
49 queued_tasks_
.push_back(QueuedTask(INSTALLING
, service_worker
));
52 void WebServiceWorkerRegistrationImpl::SetWaiting(
53 blink::WebServiceWorker
* service_worker
) {
55 proxy_
->setWaiting(service_worker
);
57 queued_tasks_
.push_back(QueuedTask(WAITING
, service_worker
));
60 void WebServiceWorkerRegistrationImpl::SetActive(
61 blink::WebServiceWorker
* service_worker
) {
63 proxy_
->setActive(service_worker
);
65 queued_tasks_
.push_back(QueuedTask(ACTIVE
, service_worker
));
68 void WebServiceWorkerRegistrationImpl::OnUpdateFound() {
70 proxy_
->dispatchUpdateFoundEvent();
72 queued_tasks_
.push_back(QueuedTask(UPDATE_FOUND
, NULL
));
75 void WebServiceWorkerRegistrationImpl::setProxy(
76 blink::WebServiceWorkerRegistrationProxy
* proxy
) {
81 void WebServiceWorkerRegistrationImpl::RunQueuedTasks() {
83 for (std::vector
<QueuedTask
>::const_iterator it
= queued_tasks_
.begin();
84 it
!= queued_tasks_
.end(); ++it
) {
85 if (it
->type
== INSTALLING
)
86 proxy_
->setInstalling(it
->worker
);
87 else if (it
->type
== WAITING
)
88 proxy_
->setWaiting(it
->worker
);
89 else if (it
->type
== ACTIVE
)
90 proxy_
->setActive(it
->worker
);
91 else if (it
->type
== UPDATE_FOUND
)
92 proxy_
->dispatchUpdateFoundEvent();
94 queued_tasks_
.clear();
97 void WebServiceWorkerRegistrationImpl::ClearQueuedTasks() {
98 for (std::vector
<QueuedTask
>::const_iterator it
= queued_tasks_
.begin();
99 it
!= queued_tasks_
.end(); ++it
) {
100 // If the owner of the WebServiceWorker does not exist, delete it.
101 if (it
->worker
&& !it
->worker
->proxy())
104 queued_tasks_
.clear();
107 blink::WebServiceWorkerRegistrationProxy
*
108 WebServiceWorkerRegistrationImpl::proxy() {
112 blink::WebURL
WebServiceWorkerRegistrationImpl::scope() const {
113 return handle_ref_
->scope();
116 void WebServiceWorkerRegistrationImpl::update(
117 blink::WebServiceWorkerProvider
* provider
,
118 WebServiceWorkerUpdateCallbacks
* callbacks
) {
119 WebServiceWorkerProviderImpl
* provider_impl
=
120 static_cast<WebServiceWorkerProviderImpl
*>(provider
);
121 ServiceWorkerDispatcher
* dispatcher
=
122 ServiceWorkerDispatcher::GetThreadSpecificInstance();
124 dispatcher
->UpdateServiceWorker(provider_impl
->provider_id(),
125 registration_id(), callbacks
);
128 void WebServiceWorkerRegistrationImpl::unregister(
129 blink::WebServiceWorkerProvider
* provider
,
130 WebServiceWorkerUnregistrationCallbacks
* callbacks
) {
131 WebServiceWorkerProviderImpl
* provider_impl
=
132 static_cast<WebServiceWorkerProviderImpl
*>(provider
);
133 ServiceWorkerDispatcher
* dispatcher
=
134 ServiceWorkerDispatcher::GetThreadSpecificInstance();
136 dispatcher
->UnregisterServiceWorker(provider_impl
->provider_id(),
137 registration_id(), callbacks
);
140 int64
WebServiceWorkerRegistrationImpl::registration_id() const {
141 return handle_ref_
->registration_id();
144 } // namespace content