Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / service_worker / web_service_worker_registration_impl.cc
blob12db5310dad9ac71ac55eb2383b601ff0ce5d071
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"
13 namespace content {
15 WebServiceWorkerRegistrationImpl::QueuedTask::QueuedTask(
16 QueuedTaskType type,
17 blink::WebServiceWorker* worker)
18 : type(type),
19 worker(worker) {
22 WebServiceWorkerRegistrationImpl::WebServiceWorkerRegistrationImpl(
23 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref)
24 : handle_ref_(handle_ref.Pass()),
25 proxy_(NULL) {
26 DCHECK(handle_ref_);
27 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId,
28 handle_ref_->handle_id());
29 ServiceWorkerDispatcher* dispatcher =
30 ServiceWorkerDispatcher::GetThreadSpecificInstance();
31 DCHECK(dispatcher);
32 dispatcher->AddServiceWorkerRegistration(handle_ref_->handle_id(), this);
35 WebServiceWorkerRegistrationImpl::~WebServiceWorkerRegistrationImpl() {
36 ServiceWorkerDispatcher* dispatcher =
37 ServiceWorkerDispatcher::GetThreadSpecificInstance();
38 if (dispatcher)
39 dispatcher->RemoveServiceWorkerRegistration(handle_ref_->handle_id());
40 ClearQueuedTasks();
43 void WebServiceWorkerRegistrationImpl::SetInstalling(
44 blink::WebServiceWorker* service_worker) {
45 if (proxy_)
46 proxy_->setInstalling(service_worker);
47 else
48 queued_tasks_.push_back(QueuedTask(INSTALLING, service_worker));
51 void WebServiceWorkerRegistrationImpl::SetWaiting(
52 blink::WebServiceWorker* service_worker) {
53 if (proxy_)
54 proxy_->setWaiting(service_worker);
55 else
56 queued_tasks_.push_back(QueuedTask(WAITING, service_worker));
59 void WebServiceWorkerRegistrationImpl::SetActive(
60 blink::WebServiceWorker* service_worker) {
61 if (proxy_)
62 proxy_->setActive(service_worker);
63 else
64 queued_tasks_.push_back(QueuedTask(ACTIVE, service_worker));
67 void WebServiceWorkerRegistrationImpl::OnUpdateFound() {
68 if (proxy_)
69 proxy_->dispatchUpdateFoundEvent();
70 else
71 queued_tasks_.push_back(QueuedTask(UPDATE_FOUND, NULL));
74 void WebServiceWorkerRegistrationImpl::setProxy(
75 blink::WebServiceWorkerRegistrationProxy* proxy) {
76 proxy_ = proxy;
77 RunQueuedTasks();
80 void WebServiceWorkerRegistrationImpl::RunQueuedTasks() {
81 DCHECK(proxy_);
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())
101 delete it->worker;
103 queued_tasks_.clear();
106 blink::WebServiceWorkerRegistrationProxy*
107 WebServiceWorkerRegistrationImpl::proxy() {
108 return 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