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/common/service_worker/service_worker_types.h"
11 #include "third_party/WebKit/public/platform/WebServiceWorkerRegistrationProxy.h"
15 WebServiceWorkerRegistrationImpl::QueuedTask::QueuedTask(
17 blink::WebServiceWorker
* worker
)
22 WebServiceWorkerRegistrationImpl::WebServiceWorkerRegistrationImpl(
23 scoped_ptr
<ServiceWorkerRegistrationHandleReference
> handle_ref
)
24 : handle_ref_(handle_ref
.Pass()),
27 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId
,
28 handle_ref_
->handle_id());
29 ServiceWorkerDispatcher
* dispatcher
=
30 ServiceWorkerDispatcher::GetThreadSpecificInstance();
32 dispatcher
->AddServiceWorkerRegistration(handle_ref_
->handle_id(), this);
35 WebServiceWorkerRegistrationImpl::~WebServiceWorkerRegistrationImpl() {
36 ServiceWorkerDispatcher
* dispatcher
=
37 ServiceWorkerDispatcher::GetThreadSpecificInstance();
39 dispatcher
->RemoveServiceWorkerRegistration(handle_ref_
->handle_id());
43 void WebServiceWorkerRegistrationImpl::SetInstalling(
44 blink::WebServiceWorker
* service_worker
) {
46 proxy_
->setInstalling(service_worker
);
48 queued_tasks_
.push_back(QueuedTask(INSTALLING
, service_worker
));
51 void WebServiceWorkerRegistrationImpl::SetWaiting(
52 blink::WebServiceWorker
* service_worker
) {
54 proxy_
->setWaiting(service_worker
);
56 queued_tasks_
.push_back(QueuedTask(WAITING
, service_worker
));
59 void WebServiceWorkerRegistrationImpl::SetActive(
60 blink::WebServiceWorker
* service_worker
) {
62 proxy_
->setActive(service_worker
);
64 queued_tasks_
.push_back(QueuedTask(ACTIVE
, service_worker
));
67 void WebServiceWorkerRegistrationImpl::OnUpdateFound() {
69 proxy_
->dispatchUpdateFoundEvent();
71 queued_tasks_
.push_back(QueuedTask(UPDATE_FOUND
, NULL
));
74 void WebServiceWorkerRegistrationImpl::setProxy(
75 blink::WebServiceWorkerRegistrationProxy
* proxy
) {
80 void WebServiceWorkerRegistrationImpl::RunQueuedTasks() {
82 for (std::vector
<QueuedTask
>::const_iterator it
= queued_tasks_
.begin();
83 it
!= queued_tasks_
.end(); ++it
) {
84 if (it
->type
== INSTALLING
)
85 proxy_
->setInstalling(it
->worker
);
86 else if (it
->type
== WAITING
)
87 proxy_
->setWaiting(it
->worker
);
88 else if (it
->type
== ACTIVE
)
89 proxy_
->setActive(it
->worker
);
90 else if (it
->type
== UPDATE_FOUND
)
91 proxy_
->dispatchUpdateFoundEvent();
93 queued_tasks_
.clear();
96 void WebServiceWorkerRegistrationImpl::ClearQueuedTasks() {
97 for (std::vector
<QueuedTask
>::const_iterator it
= queued_tasks_
.begin();
98 it
!= queued_tasks_
.end(); ++it
) {
99 // If the owner of the WebServiceWorker does not exist, delete it.
100 if (it
->worker
&& !it
->worker
->proxy())
103 queued_tasks_
.clear();
106 blink::WebServiceWorkerRegistrationProxy
*
107 WebServiceWorkerRegistrationImpl::proxy() {
111 blink::WebURL
WebServiceWorkerRegistrationImpl::scope() const {
112 return handle_ref_
->scope();
115 int64
WebServiceWorkerRegistrationImpl::registration_id() const {
116 return handle_ref_
->registration_id();
119 } // namespace content