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

Unified Diff: content/browser/service_worker/service_worker_version.cc

Issue 739383002: Don't drop messages/events send to a service worker that is stopping. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@termnate_sw
Patch Set: address nit Created 6 years 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_version.cc
diff --git a/content/browser/service_worker/service_worker_version.cc b/content/browser/service_worker/service_worker_version.cc
index 1e00b8227637cf7c82500244f7145911610324ca..693b10d3171c13b5fdf1edbc8fc3ad20a828c4d5 100644
--- a/content/browser/service_worker/service_worker_version.cc
+++ b/content/browser/service_worker/service_worker_version.cc
@@ -177,13 +177,15 @@ void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) {
void ServiceWorkerVersion::StartWorker(
bool pause_after_download,
const StatusCallback& callback) {
+ if (is_doomed()) {
+ RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED));
+ return;
+ }
switch (running_status()) {
case RUNNING:
RunSoon(base::Bind(callback, SERVICE_WORKER_OK));
return;
case STOPPING:
- RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED));
- return;
case STOPPED:
case STARTING:
start_callbacks_.push_back(callback);
@@ -543,16 +545,22 @@ void ServiceWorkerVersion::OnStarted() {
FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStarted(this));
}
-void ServiceWorkerVersion::OnStopped() {
+void ServiceWorkerVersion::OnStopped(
+ EmbeddedWorkerInstance::Status old_status) {
DCHECK_EQ(STOPPED, running_status());
scoped_refptr<ServiceWorkerVersion> protect(this);
+ bool should_restart = !is_doomed() && !start_callbacks_.empty() &&
+ (old_status != EmbeddedWorkerInstance::STARTING);
+
// Fire all stop callbacks.
RunCallbacks(this, &stop_callbacks_, SERVICE_WORKER_OK);
- // Let all start callbacks fail.
- RunCallbacks(
- this, &start_callbacks_, SERVICE_WORKER_ERROR_START_WORKER_FAILED);
+ if (!should_restart) {
+ // Let all start callbacks fail.
+ RunCallbacks(this, &start_callbacks_,
+ SERVICE_WORKER_ERROR_START_WORKER_FAILED);
+ }
// Let all message callbacks fail (this will also fire and clear all
// callbacks for events).
@@ -584,6 +592,15 @@ void ServiceWorkerVersion::OnStopped() {
// the listener prevents any pending completion callbacks from causing
// messages to be sent to the stopped worker.
cache_listener_.reset();
+
+ // Restart worker if we have any start callbacks and the worker isn't doomed.
+ if (should_restart) {
+ cache_listener_.reset(new ServiceWorkerCacheListener(this, context_));
+ embedded_worker_->Start(
+ version_id_, scope_, script_url_, false /* pause_after_download */,
+ base::Bind(&ServiceWorkerVersion::OnStartMessageSent,
+ weak_factory_.GetWeakPtr()));
+ }
}
void ServiceWorkerVersion::OnReportException(

Powered by Google App Engine
This is Rietveld 408576698