| OLD | NEW |
| 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> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 version_id, context_)); | 345 version_id, context_)); |
| 346 new_version()->set_force_bypass_cache_for_scripts(force_bypass_cache_); | 346 new_version()->set_force_bypass_cache_for_scripts(force_bypass_cache_); |
| 347 new_version()->set_skip_script_comparison(skip_script_comparison_); | 347 new_version()->set_skip_script_comparison(skip_script_comparison_); |
| 348 new_version()->StartWorker( | 348 new_version()->StartWorker( |
| 349 base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished, | 349 base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished, |
| 350 weak_factory_.GetWeakPtr())); | 350 weak_factory_.GetWeakPtr())); |
| 351 } | 351 } |
| 352 | 352 |
| 353 void ServiceWorkerRegisterJob::OnStartWorkerFinished( | 353 void ServiceWorkerRegisterJob::OnStartWorkerFinished( |
| 354 ServiceWorkerStatusCode status) { | 354 ServiceWorkerStatusCode status) { |
| 355 // Bump the last update check time only when the register/update job fetched |
| 356 // the version having bypassed the network cache. We assume that the |
| 357 // BYPASS_CACHE flag evicts an existing cache entry, so even if the install |
| 358 // ultimately failed for whatever reason, we know the version in the HTTP |
| 359 // cache is not stale, so it's OK to bump the update check time. |
| 360 if (new_version()->embedded_worker()->network_accessed_for_script() || |
| 361 new_version()->force_bypass_cache_for_scripts() || |
| 362 registration()->last_update_check().is_null()) { |
| 363 registration()->set_last_update_check(base::Time::Now()); |
| 364 |
| 365 if (registration()->waiting_version() || registration()->active_version()) |
| 366 context_->storage()->UpdateLastUpdateCheckTime(registration()); |
| 367 } |
| 368 |
| 355 if (status == SERVICE_WORKER_OK) { | 369 if (status == SERVICE_WORKER_OK) { |
| 356 InstallAndContinue(); | 370 InstallAndContinue(); |
| 357 return; | 371 return; |
| 358 } | 372 } |
| 359 | 373 |
| 360 // The updated worker is identical to the incumbent. | 374 // The updated worker is identical to the incumbent. |
| 361 if (status == SERVICE_WORKER_ERROR_EXISTS) { | 375 if (status == SERVICE_WORKER_ERROR_EXISTS) { |
| 362 // Only bump the last check time when we've bypassed the browser cache. | |
| 363 base::TimeDelta time_since_last_check = | |
| 364 base::Time::Now() - registration()->last_update_check(); | |
| 365 if (time_since_last_check > base::TimeDelta::FromHours( | |
| 366 kServiceWorkerScriptMaxCacheAgeInHours) || | |
| 367 new_version()->force_bypass_cache_for_scripts()) { | |
| 368 registration()->set_last_update_check(base::Time::Now()); | |
| 369 context_->storage()->UpdateLastUpdateCheckTime(registration()); | |
| 370 } | |
| 371 | |
| 372 ResolvePromise(SERVICE_WORKER_OK, std::string(), registration()); | 376 ResolvePromise(SERVICE_WORKER_OK, std::string(), registration()); |
| 373 Complete(status, "The updated worker is identical to the incumbent."); | 377 Complete(status, "The updated worker is identical to the incumbent."); |
| 374 return; | 378 return; |
| 375 } | 379 } |
| 376 | 380 |
| 377 // "If serviceWorker fails to start up..." then reject the promise with an | 381 // "If serviceWorker fails to start up..." then reject the promise with an |
| 378 // error and abort. | 382 // error and abort. |
| 379 if (status == SERVICE_WORKER_ERROR_TIMEOUT) { | 383 if (status == SERVICE_WORKER_ERROR_TIMEOUT) { |
| 380 Complete(status, "Timed out while trying to start the Service Worker."); | 384 Complete(status, "Timed out while trying to start the Service Worker."); |
| 381 return; | 385 return; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 ServiceWorkerStatusCode status) { | 430 ServiceWorkerStatusCode status) { |
| 427 ServiceWorkerMetrics::RecordInstallEventStatus(status); | 431 ServiceWorkerMetrics::RecordInstallEventStatus(status); |
| 428 | 432 |
| 429 if (status != SERVICE_WORKER_OK) { | 433 if (status != SERVICE_WORKER_OK) { |
| 430 // "8. If installFailed is true, then:..." | 434 // "8. If installFailed is true, then:..." |
| 431 Complete(status); | 435 Complete(status); |
| 432 return; | 436 return; |
| 433 } | 437 } |
| 434 | 438 |
| 435 SetPhase(STORE); | 439 SetPhase(STORE); |
| 436 registration()->set_last_update_check(base::Time::Now()); | 440 DCHECK(!registration()->last_update_check().is_null()); |
| 437 context_->storage()->StoreRegistration( | 441 context_->storage()->StoreRegistration( |
| 438 registration(), | 442 registration(), |
| 439 new_version(), | 443 new_version(), |
| 440 base::Bind(&ServiceWorkerRegisterJob::OnStoreRegistrationComplete, | 444 base::Bind(&ServiceWorkerRegisterJob::OnStoreRegistrationComplete, |
| 441 weak_factory_.GetWeakPtr())); | 445 weak_factory_.GetWeakPtr())); |
| 442 } | 446 } |
| 443 | 447 |
| 444 void ServiceWorkerRegisterJob::OnStoreRegistrationComplete( | 448 void ServiceWorkerRegisterJob::OnStoreRegistrationComplete( |
| 445 ServiceWorkerStatusCode status) { | 449 ServiceWorkerStatusCode status) { |
| 446 if (status != SERVICE_WORKER_OK) { | 450 if (status != SERVICE_WORKER_OK) { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 if (host->IsHostToRunningServiceWorker()) | 551 if (host->IsHostToRunningServiceWorker()) |
| 548 continue; | 552 continue; |
| 549 if (!ServiceWorkerUtils::ScopeMatches(registration->pattern(), | 553 if (!ServiceWorkerUtils::ScopeMatches(registration->pattern(), |
| 550 host->document_url())) | 554 host->document_url())) |
| 551 continue; | 555 continue; |
| 552 host->AddMatchingRegistration(registration); | 556 host->AddMatchingRegistration(registration); |
| 553 } | 557 } |
| 554 } | 558 } |
| 555 | 559 |
| 556 } // namespace content | 560 } // namespace content |
| OLD | NEW |