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

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

Issue 1675613002: service worker: use 200 OK for update requests even in the no update case (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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_register_job.h" 5 #include "content/browser/service_worker/service_worker_register_job.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <vector>
10
11 #include "base/location.h" 9 #include "base/location.h"
12 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h" 11 #include "base/thread_task_runner_handle.h"
14 #include "content/browser/service_worker/service_worker_context_core.h" 12 #include "content/browser/service_worker/service_worker_context_core.h"
15 #include "content/browser/service_worker/service_worker_job_coordinator.h" 13 #include "content/browser/service_worker/service_worker_job_coordinator.h"
16 #include "content/browser/service_worker/service_worker_metrics.h" 14 #include "content/browser/service_worker/service_worker_metrics.h"
17 #include "content/browser/service_worker/service_worker_registration.h" 15 #include "content/browser/service_worker/service_worker_registration.h"
18 #include "content/browser/service_worker/service_worker_storage.h" 16 #include "content/browser/service_worker/service_worker_storage.h"
19 #include "content/common/service_worker/service_worker_messages.h" 17 #include "content/common/service_worker/service_worker_messages.h"
20 #include "content/common/service_worker/service_worker_types.h" 18 #include "content/common/service_worker/service_worker_types.h"
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 if (version_id == kInvalidServiceWorkerVersionId) { 336 if (version_id == kInvalidServiceWorkerVersionId) {
339 Complete(SERVICE_WORKER_ERROR_ABORT); 337 Complete(SERVICE_WORKER_ERROR_ABORT);
340 return; 338 return;
341 } 339 }
342 340
343 // "Let worker be a new ServiceWorker object..." and start 341 // "Let worker be a new ServiceWorker object..." and start
344 // the worker. 342 // the worker.
345 set_new_version(new ServiceWorkerVersion(registration(), script_url_, 343 set_new_version(new ServiceWorkerVersion(registration(), script_url_,
346 version_id, context_)); 344 version_id, context_));
347 new_version()->set_force_bypass_cache_for_scripts(force_bypass_cache_); 345 new_version()->set_force_bypass_cache_for_scripts(force_bypass_cache_);
348 new_version()->set_skip_script_comparison(skip_script_comparison_); 346 if (registration()->has_installed_version() && !skip_script_comparison_) {
347 new_version()->set_pause_after_download(true);
348 new_version()->embedded_worker()->AddListener(this);
349 } else {
350 new_version()->set_pause_after_download(false);
351 }
349 new_version()->StartWorker( 352 new_version()->StartWorker(
350 base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished, 353 base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished,
351 weak_factory_.GetWeakPtr())); 354 weak_factory_.GetWeakPtr()));
352 } 355 }
353 356
354 void ServiceWorkerRegisterJob::OnStartWorkerFinished( 357 void ServiceWorkerRegisterJob::OnStartWorkerFinished(
355 ServiceWorkerStatusCode status) { 358 ServiceWorkerStatusCode status) {
356 // Bump the last update check time only when the register/update job fetched 359 // Bump the last update check time only when the register/update job fetched
357 // the version having bypassed the network cache. We assume that the 360 // the version having bypassed the network cache. We assume that the
358 // BYPASS_CACHE flag evicts an existing cache entry, so even if the install 361 // BYPASS_CACHE flag evicts an existing cache entry, so even if the install
359 // ultimately failed for whatever reason, we know the version in the HTTP 362 // ultimately failed for whatever reason, we know the version in the HTTP
360 // cache is not stale, so it's OK to bump the update check time. 363 // cache is not stale, so it's OK to bump the update check time.
361 if (new_version()->embedded_worker()->network_accessed_for_script() || 364 if (new_version()->embedded_worker()->network_accessed_for_script() ||
362 new_version()->force_bypass_cache_for_scripts() || 365 new_version()->force_bypass_cache_for_scripts() ||
363 registration()->last_update_check().is_null()) { 366 registration()->last_update_check().is_null()) {
364 registration()->set_last_update_check(base::Time::Now()); 367 registration()->set_last_update_check(base::Time::Now());
365 368
366 if (registration()->waiting_version() || registration()->active_version()) 369 if (registration()->has_installed_version())
367 context_->storage()->UpdateLastUpdateCheckTime(registration()); 370 context_->storage()->UpdateLastUpdateCheckTime(registration());
368 } 371 }
369 372
370 if (status == SERVICE_WORKER_OK) { 373 if (status == SERVICE_WORKER_OK) {
371 InstallAndContinue(); 374 InstallAndContinue();
372 return; 375 return;
373 } 376 }
374 377
375 // The updated worker is identical to the incumbent.
376 if (status == SERVICE_WORKER_ERROR_EXISTS) {
377 ResolvePromise(SERVICE_WORKER_OK, std::string(), registration());
378 Complete(status, "The updated worker is identical to the incumbent.");
379 return;
380 }
381
382 // "If serviceWorker fails to start up..." then reject the promise with an 378 // "If serviceWorker fails to start up..." then reject the promise with an
383 // error and abort. 379 // error and abort.
384 if (status == SERVICE_WORKER_ERROR_TIMEOUT) { 380 if (status == SERVICE_WORKER_ERROR_TIMEOUT) {
385 Complete(status, "Timed out while trying to start the Service Worker."); 381 Complete(status, "Timed out while trying to start the Service Worker.");
386 return; 382 return;
387 } 383 }
388 384
389 const net::URLRequestStatus& main_script_status = 385 const net::URLRequestStatus& main_script_status =
390 new_version()->script_cache_map()->main_script_status(); 386 new_version()->script_cache_map()->main_script_status();
391 std::string message; 387 std::string message;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 524 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
529 } 525 }
530 } 526 }
531 if (!is_promise_resolved_) 527 if (!is_promise_resolved_)
532 ResolvePromise(status, status_message, NULL); 528 ResolvePromise(status, status_message, NULL);
533 } 529 }
534 DCHECK(callbacks_.empty()); 530 DCHECK(callbacks_.empty());
535 if (registration()) { 531 if (registration()) {
536 context_->storage()->NotifyDoneInstallingRegistration( 532 context_->storage()->NotifyDoneInstallingRegistration(
537 registration(), new_version(), status); 533 registration(), new_version(), status);
538 if (registration()->waiting_version() || registration()->active_version()) 534 if (registration()->has_installed_version())
539 registration()->set_is_uninstalled(false); 535 registration()->set_is_uninstalled(false);
540 } 536 }
537
538 if (new_version()) {
michaeln 2016/02/06 01:13:58 maybe put this block up top to defend against the
falken 2016/02/10 05:40:41 Done.
539 new_version()->set_pause_after_download(false);
540 new_version()->embedded_worker()->RemoveListener(this);
541 }
541 } 542 }
542 543
543 void ServiceWorkerRegisterJob::ResolvePromise( 544 void ServiceWorkerRegisterJob::ResolvePromise(
544 ServiceWorkerStatusCode status, 545 ServiceWorkerStatusCode status,
545 const std::string& status_message, 546 const std::string& status_message,
546 ServiceWorkerRegistration* registration) { 547 ServiceWorkerRegistration* registration) {
547 DCHECK(!is_promise_resolved_); 548 DCHECK(!is_promise_resolved_);
548 549
549 is_promise_resolved_ = true; 550 is_promise_resolved_ = true;
550 promise_resolved_status_ = status; 551 promise_resolved_status_ = status;
(...skipping 16 matching lines...) Expand all
567 ServiceWorkerProviderHost* host = it->GetProviderHost(); 568 ServiceWorkerProviderHost* host = it->GetProviderHost();
568 if (host->IsHostToRunningServiceWorker()) 569 if (host->IsHostToRunningServiceWorker())
569 continue; 570 continue;
570 if (!ServiceWorkerUtils::ScopeMatches(registration->pattern(), 571 if (!ServiceWorkerUtils::ScopeMatches(registration->pattern(),
571 host->document_url())) 572 host->document_url()))
572 continue; 573 continue;
573 host->AddMatchingRegistration(registration); 574 host->AddMatchingRegistration(registration);
574 } 575 }
575 } 576 }
576 577
578 void ServiceWorkerRegisterJob::OnScriptLoaded() {
nhiroki 2016/02/10 04:56:14 Can you add "DCHECK(pause_after_download())" here?
falken 2016/02/10 05:40:40 It's not true though because pause_after_download
nhiroki 2016/02/10 06:38:29 Hmmm... I'm still thinking this event handler can
falken 2016/02/10 06:53:39 Ah, of course. EmbeddedWorkerInstance::OnScriptLoa
579 new_version()->set_pause_after_download(false);
580 net::URLRequestStatus status =
581 new_version()->script_cache_map()->main_script_status();
582 if (!status.is_success()) {
583 // OnScriptLoaded signifies a successful network load, which translates into
584 // a script cache error only if the new script was not written because it
585 // was identical to the existing script.
586 DCHECK_EQ(status.error(), net::ERR_FILE_EXISTS);
587 ResolvePromise(SERVICE_WORKER_OK, std::string(), registration());
588 Complete(SERVICE_WORKER_ERROR_EXISTS,
589 "The updated worker is identical to the incumbent.");
590 return;
591 }
592
593 new_version()->embedded_worker()->ResumeAfterDownload();
594 }
595
577 } // namespace content 596 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698