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 <[email protected]>
Reviewed-by: Makoto Shimazu <[email protected]>
Cr-Commit-Position: refs/heads/master@{#590260}
diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn
index a641d99..4ffd67d 100644
--- a/content/browser/BUILD.gn
+++ b/content/browser/BUILD.gn
@@ -1646,6 +1646,8 @@
"service_worker/service_worker_type_converters.h",
"service_worker/service_worker_unregister_job.cc",
"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.h",
"service_worker/service_worker_url_request_job.cc",
diff --git a/content/browser/service_worker/service_worker_register_job.cc b/content/browser/service_worker/service_worker_register_job.cc
index f832ed1..78c265e 100644
--- a/content/browser/service_worker/service_worker_register_job.cc
+++ b/content/browser/service_worker/service_worker_register_job.cc
@@ -26,6 +26,7 @@
#include "mojo/public/cpp/bindings/associated_binding.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_utils.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom.h"
namespace content {
@@ -64,9 +65,15 @@
is_promise_resolved_(false),
should_uninstall_on_failure_(false),
force_bypass_cache_(force_bypass_cache),
- skip_script_comparison_(skip_script_comparison),
promise_resolved_status_(blink::ServiceWorkerStatusCode::kOk),
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;
}
@@ -280,6 +287,36 @@
// ago, depending on the freshness of the cached worker script we
// 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();
}
@@ -608,6 +645,7 @@
}
void ServiceWorkerRegisterJob::OnPausedAfterDownload() {
+ DCHECK(!blink::ServiceWorkerUtils::IsImportedScriptUpdateCheckEnabled());
net::URLRequestStatus status =
new_version()->script_cache_map()->main_script_status();
if (!status.is_success()) {
diff --git a/content/browser/service_worker/service_worker_register_job.h b/content/browser/service_worker/service_worker_register_job.h
index 5381491..e396a85 100644
--- a/content/browser/service_worker/service_worker_register_job.h
+++ b/content/browser/service_worker/service_worker_register_job.h
@@ -13,6 +13,7 @@
#include "base/time/time.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_update_checker.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_registration.mojom.h"
@@ -105,6 +106,11 @@
void ContinueWithUpdate(
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration);
+
+ // This method is only called when ServiceWorkerImportedScriptUpdateCheck is
+ // enabled.
+ void OnUpdateCheckFinished(bool script_chnaged);
+
void RegisterAndContinue();
void ContinueWithUninstallingRegistration(
scoped_refptr<ServiceWorkerRegistration> existing_registration,
@@ -142,6 +148,8 @@
// The ServiceWorkerContextCore object should always outlive this.
base::WeakPtr<ServiceWorkerContextCore> context_;
+ std::unique_ptr<ServiceWorkerUpdateChecker> update_checker_;
+
RegistrationJobType job_type_;
const GURL pattern_;
GURL script_url_;
diff --git a/content/browser/service_worker/service_worker_update_checker.cc b/content/browser/service_worker/service_worker_update_checker.cc
new file mode 100644
index 0000000..e456dae9
--- /dev/null
+++ b/content/browser/service_worker/service_worker_update_checker.cc
@@ -0,0 +1,23 @@
+// 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
diff --git a/content/browser/service_worker/service_worker_update_checker.h b/content/browser/service_worker/service_worker_update_checker.h
new file mode 100644
index 0000000..78e5a8a
--- /dev/null
+++ b/content/browser/service_worker/service_worker_update_checker.h
@@ -0,0 +1,30 @@
+// 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_