Chromium Code Reviews| 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 0bcec8983900ca78b5beb9bd25191707c4c7138b..27ff20619ed487f665e37208a043ac0bd7cfd68d 100644 |
| --- a/content/browser/service_worker/service_worker_version.cc |
| +++ b/content/browser/service_worker/service_worker_version.cc |
| @@ -11,6 +11,7 @@ |
| #include "content/browser/service_worker/embedded_worker_registry.h" |
| #include "content/browser/service_worker/service_worker_context_core.h" |
| #include "content/browser/service_worker/service_worker_registration.h" |
| +#include "content/browser/service_worker/service_worker_utils.h" |
| #include "content/common/service_worker/service_worker_messages.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/common/content_switches.h" |
| @@ -22,6 +23,12 @@ typedef ServiceWorkerVersion::MessageCallback MessageCallback; |
| namespace { |
| +// Default delay to stop the worker context after all documents that |
| +// are associated to the worker are closed. |
| +// (Note that if all references to the version is dropped the worker |
| +// is also stopped without delay) |
| +const int64 kStopWorkerDelay = 5 * 60; // 5 mins. |
|
Jeffrey Yasskin
2014/05/15 19:16:44
We should probably make this faster. Extension/App
kinuko
2014/05/16 04:13:51
I don't have a strong opinion on this delay for no
|
| + |
| void RunSoon(const base::Closure& callback) { |
| if (!callback.is_null()) |
| base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
| @@ -103,10 +110,13 @@ ServiceWorkerVersion::ServiceWorkerVersion( |
| } |
| ServiceWorkerVersion::~ServiceWorkerVersion() { |
| - if (embedded_worker_) { |
| - embedded_worker_->RemoveListener(this); |
| - embedded_worker_.reset(); |
| + if (running_status() == RUNNING) { |
| + if (stop_worker_timer_.IsRunning()) |
| + stop_worker_timer_.Stop(); |
|
michaeln
2014/05/15 19:02:20
base::Timer's destructor takes care of this
kinuko
2014/05/16 04:13:51
Oh yeah that's handy...
|
| } |
| + embedded_worker_->RemoveListener(this); |
| + // EmbeddedWorker's dtor sends StopWorker if it's still running. |
| + embedded_worker_.reset(); |
|
michaeln
2014/05/15 19:02:20
is the call to .reset() needed for ordering reason
kinuko
2014/05/16 04:13:51
Done.
|
| if (context_) |
| context_->RemoveLiveVersion(version_id_); |
| } |
| @@ -148,7 +158,6 @@ void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) { |
| void ServiceWorkerVersion::StartWorkerWithCandidateProcesses( |
| const std::vector<int>& possible_process_ids, |
| const StatusCallback& callback) { |
| - DCHECK(embedded_worker_); |
| switch (running_status()) { |
| case RUNNING: |
| RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
| @@ -173,7 +182,6 @@ void ServiceWorkerVersion::StartWorkerWithCandidateProcesses( |
| } |
| void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { |
| - DCHECK(embedded_worker_); |
| if (running_status() == STOPPED) { |
| RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
| return; |
| @@ -190,7 +198,6 @@ void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { |
| void ServiceWorkerVersion::SendMessage( |
| const IPC::Message& message, const StatusCallback& callback) { |
| - DCHECK(embedded_worker_); |
| if (running_status() != RUNNING) { |
| // Schedule calling this method after starting the worker. |
| StartWorker(base::Bind(&RunTaskAfterStartWorker, |
| @@ -318,6 +325,8 @@ void ServiceWorkerVersion::AddControllee( |
| int controllee_id = controllee_by_id_.Add(provider_host); |
| controllee_map_[provider_host] = controllee_id; |
| AddProcessToWorker(provider_host->process_id()); |
| + if (stop_worker_timer_.IsRunning()) |
| + stop_worker_timer_.Stop(); |
| } |
| void ServiceWorkerVersion::RemoveControllee( |
| @@ -327,6 +336,8 @@ void ServiceWorkerVersion::RemoveControllee( |
| controllee_by_id_.Remove(found->second); |
| controllee_map_.erase(found); |
| RemoveProcessFromWorker(provider_host->process_id()); |
| + if (!HasControllee()) |
| + ScheduleStopWorker(); |
|
Jeffrey Yasskin
2014/05/15 19:16:44
No need to handle this in this CL, but we're going
kinuko
2014/05/16 04:13:51
Yeah we could add similar code in other events / u
|
| // TODO(kinuko): Fire NoControllees notification when the # of controllees |
| // reaches 0, so that a new pending version can be activated (which will |
| // deactivate this version). |
| @@ -565,4 +576,18 @@ void ServiceWorkerVersion::OnPostMessageToDocument( |
| provider_host->PostMessage(message, sent_message_port_ids); |
| } |
| +void ServiceWorkerVersion::ScheduleStopWorker() { |
| + if (running_status() != RUNNING) |
| + return; |
| + if (stop_worker_timer_.IsRunning()) { |
| + stop_worker_timer_.Reset(); |
|
michaeln
2014/05/15 19:02:20
nit: maybe prefer early return here
kinuko
2014/05/16 04:13:51
Done.
|
| + } else { |
| + stop_worker_timer_.Start( |
| + FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), |
| + base::Bind(&ServiceWorkerVersion::StopWorker, |
| + weak_factory_.GetWeakPtr(), |
| + base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); |
| + } |
| +} |
| + |
| } // namespace content |