Commit f24d4094 authored by momohatt's avatar momohatt Committed by Commit Bot

Add ServiceWorkerUpdateChecker

This patch adds a new class called ServiceWorkerUpdateChecker and
makes ServiceWorkerRegisterJob use this class. The new class has a
method called Start(), which takes a callback as an argument and
triggers it only when the scripts had any changes to update. Currently
this method runs the callback unconditionally, but following patches
will implement script loading and comparison with the stored resources.

Bug: 648295
Change-Id: Iebbe85b6d5e5cdab27c23873b04811626b5abad0
Reviewed-on: https://chromium-review.googlesource.com/1215458
Commit-Queue: Momoko Hattori <momohatt@google.com>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590260}
parent 7f9ed80c
...@@ -1646,6 +1646,8 @@ jumbo_source_set("browser") { ...@@ -1646,6 +1646,8 @@ jumbo_source_set("browser") {
"service_worker/service_worker_type_converters.h", "service_worker/service_worker_type_converters.h",
"service_worker/service_worker_unregister_job.cc", "service_worker/service_worker_unregister_job.cc",
"service_worker/service_worker_unregister_job.h", "service_worker/service_worker_unregister_job.h",
"service_worker/service_worker_update_checker.cc",
"service_worker/service_worker_update_checker.h",
"service_worker/service_worker_url_job_wrapper.cc", "service_worker/service_worker_url_job_wrapper.cc",
"service_worker/service_worker_url_job_wrapper.h", "service_worker/service_worker_url_job_wrapper.h",
"service_worker/service_worker_url_request_job.cc", "service_worker/service_worker_url_request_job.cc",
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "mojo/public/cpp/bindings/associated_binding.h" #include "mojo/public/cpp/bindings/associated_binding.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "third_party/blink/public/common/service_worker/service_worker_type_converters.h" #include "third_party/blink/public/common/service_worker/service_worker_type_converters.h"
#include "third_party/blink/public/common/service_worker/service_worker_utils.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom.h" #include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom.h"
namespace content { namespace content {
...@@ -64,9 +65,15 @@ ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( ...@@ -64,9 +65,15 @@ ServiceWorkerRegisterJob::ServiceWorkerRegisterJob(
is_promise_resolved_(false), is_promise_resolved_(false),
should_uninstall_on_failure_(false), should_uninstall_on_failure_(false),
force_bypass_cache_(force_bypass_cache), force_bypass_cache_(force_bypass_cache),
skip_script_comparison_(skip_script_comparison),
promise_resolved_status_(blink::ServiceWorkerStatusCode::kOk), promise_resolved_status_(blink::ServiceWorkerStatusCode::kOk),
weak_factory_(this) { weak_factory_(this) {
// |skip_script_comparison_| should be true when
// ServiceWorkerImportedScriptUpdateCheck is enabled, because then script
// comparison happens before starting a worker and it doesn't need to happen
// during the worker startup.
skip_script_comparison_ =
blink::ServiceWorkerUtils::IsImportedScriptUpdateCheckEnabled() ||
skip_script_comparison;
internal_.registration = registration; internal_.registration = registration;
} }
...@@ -280,6 +287,36 @@ void ServiceWorkerRegisterJob::ContinueWithUpdate( ...@@ -280,6 +287,36 @@ void ServiceWorkerRegisterJob::ContinueWithUpdate(
// ago, depending on the freshness of the cached worker script we // ago, depending on the freshness of the cached worker script we
// may be able to complete the update job right here. // may be able to complete the update job right here.
if (blink::ServiceWorkerUtils::IsImportedScriptUpdateCheckEnabled()) {
update_checker_ =
std::make_unique<ServiceWorkerUpdateChecker>(registration());
update_checker_->Start(
base::BindOnce(&ServiceWorkerRegisterJob::OnUpdateCheckFinished,
weak_factory_.GetWeakPtr()));
return;
}
UpdateAndContinue();
}
void ServiceWorkerRegisterJob::OnUpdateCheckFinished(bool script_changed) {
DCHECK(blink::ServiceWorkerUtils::IsImportedScriptUpdateCheckEnabled());
if (!script_changed) {
// TODO(momohatt): Set phase correctly.
// TODO(momohatt): Update the last update check time correctly.
ServiceWorkerVersion* newest_version = registration()->GetNewestVersion();
if (newest_version->force_bypass_cache_for_scripts()) {
registration()->set_last_update_check(base::Time::Now());
}
context_->storage()->UpdateLastUpdateCheckTime(registration());
ResolvePromise(blink::ServiceWorkerStatusCode::kOk, std::string(),
registration());
// This terminates the current job (|this|).
Complete(blink::ServiceWorkerStatusCode::kErrorExists,
"The updated worker is identical to the incumbent.");
return;
}
UpdateAndContinue(); UpdateAndContinue();
} }
...@@ -608,6 +645,7 @@ void ServiceWorkerRegisterJob::AddRegistrationToMatchingProviderHosts( ...@@ -608,6 +645,7 @@ void ServiceWorkerRegisterJob::AddRegistrationToMatchingProviderHosts(
} }
void ServiceWorkerRegisterJob::OnPausedAfterDownload() { void ServiceWorkerRegisterJob::OnPausedAfterDownload() {
DCHECK(!blink::ServiceWorkerUtils::IsImportedScriptUpdateCheckEnabled());
net::URLRequestStatus status = net::URLRequestStatus status =
new_version()->script_cache_map()->main_script_status(); new_version()->script_cache_map()->main_script_status();
if (!status.is_success()) { if (!status.is_success()) {
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "content/browser/service_worker/service_worker_register_job_base.h" #include "content/browser/service_worker/service_worker_register_job_base.h"
#include "content/browser/service_worker/service_worker_registration.h" #include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_update_checker.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h" #include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom.h" #include "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h" #include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
...@@ -105,6 +106,11 @@ class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase { ...@@ -105,6 +106,11 @@ class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase {
void ContinueWithUpdate( void ContinueWithUpdate(
blink::ServiceWorkerStatusCode status, blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration); scoped_refptr<ServiceWorkerRegistration> registration);
// This method is only called when ServiceWorkerImportedScriptUpdateCheck is
// enabled.
void OnUpdateCheckFinished(bool script_chnaged);
void RegisterAndContinue(); void RegisterAndContinue();
void ContinueWithUninstallingRegistration( void ContinueWithUninstallingRegistration(
scoped_refptr<ServiceWorkerRegistration> existing_registration, scoped_refptr<ServiceWorkerRegistration> existing_registration,
...@@ -142,6 +148,8 @@ class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase { ...@@ -142,6 +148,8 @@ class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase {
// The ServiceWorkerContextCore object should always outlive this. // The ServiceWorkerContextCore object should always outlive this.
base::WeakPtr<ServiceWorkerContextCore> context_; base::WeakPtr<ServiceWorkerContextCore> context_;
std::unique_ptr<ServiceWorkerUpdateChecker> update_checker_;
RegistrationJobType job_type_; RegistrationJobType job_type_;
const GURL pattern_; const GURL pattern_;
GURL script_url_; GURL script_url_;
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/service_worker/service_worker_update_checker.h"
#include "content/browser/service_worker/service_worker_registration.h"
namespace content {
ServiceWorkerUpdateChecker::ServiceWorkerUpdateChecker(
scoped_refptr<ServiceWorkerRegistration> registration) {
NOTIMPLEMENTED();
}
ServiceWorkerUpdateChecker::~ServiceWorkerUpdateChecker() = default;
void ServiceWorkerUpdateChecker::Start(UpdateStatusCallback callback) {
std::move(callback).Run(true);
NOTIMPLEMENTED();
}
} // namespace content
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UPDATE_CHECKER_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UPDATE_CHECKER_H_
#include "base/callback.h"
namespace content {
class ServiceWorkerRegistration;
class ServiceWorkerUpdateChecker {
public:
using UpdateStatusCallback = base::OnceCallback<void(bool)>;
ServiceWorkerUpdateChecker(
scoped_refptr<ServiceWorkerRegistration> registration);
~ServiceWorkerUpdateChecker();
// |callback| is always triggered when Start() finishes. If the scripts are
// found to have any changes, the argument of |callback| is true and otherwise
// false.
void Start(UpdateStatusCallback callback);
};
} // namespace content
#endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UPDATE_CHECKER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment