Reland debugging for ipc related crash.
[chromium-blink-merge.git] / content / child / service_worker / web_service_worker_registration_impl.cc
blobc3fb98b2d52889657ede9b34c2f92dbf692776da
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"
14 namespace content {
16 namespace {
18 class HandleImpl : public blink::WebServiceWorkerRegistration::Handle {
19 public:
20 explicit HandleImpl(WebServiceWorkerRegistrationImpl* registration)
21 : registration_(registration) {}
22 virtual ~HandleImpl() {}
24 virtual blink::WebServiceWorkerRegistration* registration() {
25 return registration_.get();
28 private:
29 scoped_refptr<WebServiceWorkerRegistrationImpl> registration_;
31 DISALLOW_COPY_AND_ASSIGN(HandleImpl);
34 } // namespace
36 WebServiceWorkerRegistrationImpl::QueuedTask::QueuedTask(
37 QueuedTaskType type,
38 blink::WebServiceWorker* worker)
39 : type(type),
40 worker(worker) {
43 WebServiceWorkerRegistrationImpl::WebServiceWorkerRegistrationImpl(
44 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref)
45 : handle_ref_(handle_ref.Pass()),
46 proxy_(NULL) {
47 DCHECK(handle_ref_);
48 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId,
49 handle_ref_->handle_id());
50 ServiceWorkerDispatcher* dispatcher =
51 ServiceWorkerDispatcher::GetThreadSpecificInstance();
52 DCHECK(dispatcher);
53 dispatcher->AddServiceWorkerRegistration(handle_ref_->handle_id(), this);
56 void WebServiceWorkerRegistrationImpl::SetInstalling(
57 blink::WebServiceWorker* service_worker) {
58 if (proxy_)
59 proxy_->setInstalling(service_worker);
60 else
61 queued_tasks_.push_back(QueuedTask(INSTALLING, service_worker));
64 void WebServiceWorkerRegistrationImpl::SetWaiting(
65 blink::WebServiceWorker* service_worker) {
66 if (proxy_)
67 proxy_->setWaiting(service_worker);
68 else
69 queued_tasks_.push_back(QueuedTask(WAITING, service_worker));
72 void WebServiceWorkerRegistrationImpl::SetActive(
73 blink::WebServiceWorker* service_worker) {
74 if (proxy_)
75 proxy_->setActive(service_worker);
76 else
77 queued_tasks_.push_back(QueuedTask(ACTIVE, service_worker));
80 void WebServiceWorkerRegistrationImpl::OnUpdateFound() {
81 if (proxy_)
82 proxy_->dispatchUpdateFoundEvent();
83 else
84 queued_tasks_.push_back(QueuedTask(UPDATE_FOUND, NULL));
87 void WebServiceWorkerRegistrationImpl::setProxy(
88 blink::WebServiceWorkerRegistrationProxy* proxy) {
89 proxy_ = proxy;
90 RunQueuedTasks();
93 void WebServiceWorkerRegistrationImpl::RunQueuedTasks() {
94 DCHECK(proxy_);
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())
114 delete it->worker;
116 queued_tasks_.clear();
119 blink::WebServiceWorkerRegistrationProxy*
120 WebServiceWorkerRegistrationImpl::proxy() {
121 return 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();
135 DCHECK(dispatcher);
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();
147 DCHECK(dispatcher);
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();
169 if (dispatcher)
170 dispatcher->RemoveServiceWorkerRegistration(handle_ref_->handle_id());
171 ClearQueuedTasks();
174 } // namespace content