Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: content/browser/service_worker/service_worker_registration.cc

Issue 380093002: Update installed ServiceWorkers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/service_worker_registration.h" 5 #include "content/browser/service_worker/service_worker_registration.h"
6 6
7 #include "content/browser/service_worker/service_worker_context_core.h" 7 #include "content/browser/service_worker/service_worker_context_core.h"
8 #include "content/browser/service_worker/service_worker_info.h" 8 #include "content/browser/service_worker/service_worker_info.h"
9 #include "content/browser/service_worker/service_worker_register_job.h"
10 #include "content/browser/service_worker/service_worker_utils.h"
9 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
10 12
11 namespace content { 13 namespace content {
12 14
13 namespace { 15 namespace {
14 16
15 ServiceWorkerVersionInfo GetVersionInfo(ServiceWorkerVersion* version) { 17 ServiceWorkerVersionInfo GetVersionInfo(ServiceWorkerVersion* version) {
16 if (!version) 18 if (!version)
17 return ServiceWorkerVersionInfo(); 19 return ServiceWorkerVersionInfo();
18 return version->GetInfo(); 20 return version->GetInfo();
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); 117 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION);
116 } else if (waiting_version_ == version) { 118 } else if (waiting_version_ == version) {
117 waiting_version_ = NULL; 119 waiting_version_ = NULL;
118 mask->add(ChangedVersionAttributesMask::WAITING_VERSION); 120 mask->add(ChangedVersionAttributesMask::WAITING_VERSION);
119 } else if (active_version_ == version) { 121 } else if (active_version_ == version) {
120 active_version_ = NULL; 122 active_version_ = NULL;
121 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION); 123 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION);
122 } 124 }
123 } 125 }
124 126
127 void ServiceWorkerRegistration::ActivateWaitingVersion(
128 const StatusCallback& completion_callback) {
129 DCHECK(context_);
130 DCHECK(waiting_version());
131 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version();
132 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version();
133
134 // "4. If exitingWorker is not null,
135 if (exiting_version) {
136 DCHECK(!exiting_version->HasControllee());
137 // TODO(michaeln): should wait for events to be complete
138 // "1. Wait for exitingWorker to finish handling any in-progress requests."
139 // "2. Terminate exitingWorker."
140 exiting_version->StopWorker(
141 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
142 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and
143 // "redundant" as the arguments."
144 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT);
145 }
146
147 // "5. Set serviceWorkerRegistration.activeWorker to activatingWorker."
148 // "6. Set serviceWorkerRegistration.waitingWorker to null."
149 ServiceWorkerRegisterJob::DisassociateVersionFromDocuments(
150 context_, activating_version);
151 SetActiveVersion(activating_version);
152 ServiceWorkerRegisterJob::AssociateActiveVersionToDocuments(
153 context_, activating_version);
154
155 // "7. Run the [[UpdateState]] algorithm passing registration.activeWorker and
156 // "activating" as arguments."
157 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATING);
158
159 // TODO(nhiroki): "8. Fire a simple event named controllerchange..."
160
161 // "9. Queue a task to fire an event named activate..."
162 activating_version->DispatchActivateEvent(
163 base::Bind(&ServiceWorkerRegistration::OnActivateEventFinished,
164 this, activating_version, completion_callback));
165 }
166
167 void ServiceWorkerRegistration::OnActivateEventFinished(
168 ServiceWorkerVersion* activating_version,
169 const StatusCallback& completion_callback,
170 ServiceWorkerStatusCode status) {
171 if (activating_version != active_version())
172 return;
173 // TODO(kinuko,falken): For some error cases (e.g. ServiceWorker is
174 // unexpectedly terminated) we may want to retry sending the event again.
175 if (status != SERVICE_WORKER_OK) {
176 // "11. If activateFailed is true, then:..."
177 ServiceWorkerRegisterJob::DisassociateVersionFromDocuments(
178 context_, activating_version);
179 UnsetVersion(activating_version);
180 activating_version->Doom();
181 if (!active_version()) {
182 context_->storage()->DeleteRegistration(
183 id(), script_url().GetOrigin(),
184 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
185 }
186 completion_callback.Run(status);
187 return;
188 }
189
190 // "12. Run the [[UpdateState]] algorithm passing registration.activeWorker
191 // and "activated" as the arguments."
192 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATED);
193 if (context_) {
194 context_->storage()->UpdateToActiveState(
195 this,
196 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
197 }
198 completion_callback.Run(SERVICE_WORKER_OK);
199 }
125 200
126 } // namespace content 201 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698