Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / child / service_worker / web_service_worker_registration_impl.cc
blob1b4b610cb6894f376dd92c0135306550d64719fe
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"
14 namespace content {
16 WebServiceWorkerRegistrationImpl::QueuedTask::QueuedTask(
17 QueuedTaskType type,
18 blink::WebServiceWorker* worker)
19 : type(type),
20 worker(worker) {
23 WebServiceWorkerRegistrationImpl::WebServiceWorkerRegistrationImpl(
24 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref)
25 : handle_ref_(handle_ref.Pass()),
26 proxy_(NULL) {
27 DCHECK(handle_ref_);
28 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId,
29 handle_ref_->handle_id());
30 ServiceWorkerDispatcher* dispatcher =
31 ServiceWorkerDispatcher::GetThreadSpecificInstance();
32 DCHECK(dispatcher);
33 dispatcher->AddServiceWorkerRegistration(handle_ref_->handle_id(), this);
36 WebServiceWorkerRegistrationImpl::~WebServiceWorkerRegistrationImpl() {
37 ServiceWorkerDispatcher* dispatcher =
38 ServiceWorkerDispatcher::GetThreadSpecificInstance();
39 if (dispatcher)
40 dispatcher->RemoveServiceWorkerRegistration(handle_ref_->handle_id());
41 ClearQueuedTasks();
44 void WebServiceWorkerRegistrationImpl::SetInstalling(
45 blink::WebServiceWorker* service_worker) {
46 if (proxy_)
47 proxy_->setInstalling(service_worker);
48 else
49 queued_tasks_.push_back(QueuedTask(INSTALLING, service_worker));
52 void WebServiceWorkerRegistrationImpl::SetWaiting(
53 blink::WebServiceWorker* service_worker) {
54 if (proxy_)
55 proxy_->setWaiting(service_worker);
56 else
57 queued_tasks_.push_back(QueuedTask(WAITING, service_worker));
60 void WebServiceWorkerRegistrationImpl::SetActive(
61 blink::WebServiceWorker* service_worker) {
62 if (proxy_)
63 proxy_->setActive(service_worker);
64 else
65 queued_tasks_.push_back(QueuedTask(ACTIVE, service_worker));
68 void WebServiceWorkerRegistrationImpl::OnUpdateFound() {
69 if (proxy_)
70 proxy_->dispatchUpdateFoundEvent();
71 else
72 queued_tasks_.push_back(QueuedTask(UPDATE_FOUND, NULL));
75 void WebServiceWorkerRegistrationImpl::setProxy(
76 blink::WebServiceWorkerRegistrationProxy* proxy) {
77 proxy_ = proxy;
78 RunQueuedTasks();
81 void WebServiceWorkerRegistrationImpl::RunQueuedTasks() {
82 DCHECK(proxy_);
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())
102 delete it->worker;
104 queued_tasks_.clear();
107 blink::WebServiceWorkerRegistrationProxy*
108 WebServiceWorkerRegistrationImpl::proxy() {
109 return 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();
123 DCHECK(dispatcher);
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();
135 DCHECK(dispatcher);
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