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

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

Issue 2119143002: service worker: Wait for inflight requests before activating (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 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
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 <vector> 7 #include <vector>
8 8
9 #include "content/browser/service_worker/embedded_worker_status.h" 9 #include "content/browser/service_worker/embedded_worker_status.h"
10 #include "content/browser/service_worker/service_worker_context_core.h" 10 #include "content/browser/service_worker/service_worker_context_core.h"
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 } else if (active_version_.get() == version) { 175 } else if (active_version_.get() == version) {
176 active_version_->RemoveListener(this); 176 active_version_->RemoveListener(this);
177 active_version_ = NULL; 177 active_version_ = NULL;
178 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION); 178 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION);
179 } 179 }
180 } 180 }
181 181
182 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { 182 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() {
183 DCHECK(waiting_version()); 183 DCHECK(waiting_version());
184 should_activate_when_ready_ = true; 184 should_activate_when_ready_ = true;
185 185 if (IsReadyToActivate())
186 if (!active_version() || !active_version()->HasControllee() ||
187 waiting_version()->skip_waiting()) {
188 ActivateWaitingVersion(false); 186 ActivateWaitingVersion(false);
189 }
190 } 187 }
191 188
192 void ServiceWorkerRegistration::ClaimClients() { 189 void ServiceWorkerRegistration::ClaimClients() {
193 DCHECK(context_); 190 DCHECK(context_);
194 DCHECK(active_version()); 191 DCHECK(active_version());
195 192
196 for (std::unique_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = 193 for (std::unique_ptr<ServiceWorkerContextCore::ProviderHostIterator> it =
197 context_->GetProviderHostIterator(); 194 context_->GetProviderHostIterator();
198 !it->IsAtEnd(); it->Advance()) { 195 !it->IsAtEnd(); it->Advance()) {
199 ServiceWorkerProviderHost* host = it->GetProviderHost(); 196 ServiceWorkerProviderHost* host = it->GetProviderHost();
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 callback, 243 callback,
247 most_recent_version)); 244 most_recent_version));
248 } 245 }
249 246
250 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { 247 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) {
251 if (!context_) 248 if (!context_)
252 return; 249 return;
253 DCHECK_EQ(active_version(), version); 250 DCHECK_EQ(active_version(), version);
254 if (is_uninstalling_) 251 if (is_uninstalling_)
255 Clear(); 252 Clear();
256 else if (should_activate_when_ready_) 253 else if (IsReadyToActivate())
257 ActivateWaitingVersion(true); 254 ActivateWaitingVersion(true /* delay */);
255 }
256
257 void ServiceWorkerRegistration::OnNoInflightRequests(
258 ServiceWorkerVersion* version) {
259 if (!context_)
260 return;
261 DCHECK_EQ(active_version(), version);
262 if (IsReadyToActivate())
263 ActivateWaitingVersion(false /* delay */);
264 }
265
266 bool ServiceWorkerRegistration::IsReadyToActivate() const {
nhiroki 2016/07/08 05:04:42 DCHECK(waiting_version()) ?
falken 2016/07/08 07:06:45 Hmm good catch. The code assumes should_activate_w
267 if (!should_activate_when_ready_)
268 return false;
269
270 const ServiceWorkerVersion* active = active_version();
271 return !(active &&
272 (active->HasInflightRequests() ||
273 (active->HasControllee() && !waiting_version()->skip_waiting())));
nhiroki 2016/07/08 05:04:42 Negating the whole complex expression would make i
falken 2016/07/08 07:06:45 Done.
258 } 274 }
259 275
260 void ServiceWorkerRegistration::ActivateWaitingVersion(bool delay) { 276 void ServiceWorkerRegistration::ActivateWaitingVersion(bool delay) {
261 DCHECK_CURRENTLY_ON(BrowserThread::IO); 277 DCHECK_CURRENTLY_ON(BrowserThread::IO);
262 DCHECK(context_); 278 DCHECK(context_);
263 DCHECK(waiting_version()); 279 DCHECK(waiting_version());
264 DCHECK(should_activate_when_ready_); 280 DCHECK(should_activate_when_ready_);
nhiroki 2016/07/08 05:04:42 Can you replace this check with "DCHECK(IsReadyToA
falken 2016/07/08 07:06:45 Good. Done.
265 should_activate_when_ready_ = false; 281 should_activate_when_ready_ = false;
266 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version(); 282 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version();
267 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version(); 283 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version();
268 284
269 if (activating_version->is_redundant()) 285 if (activating_version->is_redundant())
270 return; // Activation is no longer relevant. 286 return; // Activation is no longer relevant.
271 287
272 // "5. If exitingWorker is not null, 288 // "5. If exitingWorker is not null,
273 if (exiting_version.get()) { 289 if (exiting_version.get()) {
274 // TODO(michaeln): should wait for events to be complete 290 // TODO(falken): Update the quoted spec comments once
291 // https://github.com/slightlyoff/ServiceWorker/issues/916 is codified in
292 // the spec.
275 // "1. Wait for exitingWorker to finish handling any in-progress requests." 293 // "1. Wait for exitingWorker to finish handling any in-progress requests."
294 // This is already handled by IsReadyToActivate().
276 // "2. Terminate exitingWorker." 295 // "2. Terminate exitingWorker."
277 exiting_version->StopWorker( 296 exiting_version->StopWorker(
278 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 297 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
279 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and 298 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and
280 // "redundant" as the arguments." 299 // "redundant" as the arguments."
281 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); 300 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT);
282 } 301 }
283 302
284 // "6. Set serviceWorkerRegistration.activeWorker to activatingWorker." 303 // "6. Set serviceWorkerRegistration.activeWorker to activatingWorker."
285 // "7. Set serviceWorkerRegistration.waitingWorker to null." 304 // "7. Set serviceWorkerRegistration.waitingWorker to null."
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 if (!context_) { 493 if (!context_) {
475 callback.Run(SERVICE_WORKER_ERROR_ABORT); 494 callback.Run(SERVICE_WORKER_ERROR_ABORT);
476 return; 495 return;
477 } 496 }
478 context_->storage()->NotifyDoneInstallingRegistration( 497 context_->storage()->NotifyDoneInstallingRegistration(
479 this, version.get(), status); 498 this, version.get(), status);
480 callback.Run(status); 499 callback.Run(status);
481 } 500 }
482 501
483 } // namespace content 502 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698