This source file includes following definitions.
- weak_factory_
- AddCallback
- Start
- Equals
- GetType
- HandleExistingRegistrationAndContinue
- RegisterAndContinue
- UpdateAndContinue
- OnStartWorkerFinished
- InstallAndContinue
- OnInstallFinished
- ActivateAndContinue
- Complete
- RunCallbacks
#include "content/browser/service_worker/service_worker_register_job.h"
#include <vector>
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_job_coordinator.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_storage.h"
namespace content {
typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType;
ServiceWorkerRegisterJob::ServiceWorkerRegisterJob(
base::WeakPtr<ServiceWorkerContextCore> context,
const GURL& pattern,
const GURL& script_url)
: context_(context),
pattern_(pattern),
script_url_(script_url),
weak_factory_(this) {}
ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {}
void ServiceWorkerRegisterJob::AddCallback(const RegistrationCallback& callback,
int process_id) {
callbacks_.push_back(callback);
DCHECK_NE(-1, process_id);
if (pending_version_) {
pending_version_->AddProcessToWorker(process_id);
} else {
pending_process_ids_.push_back(process_id);
}
}
void ServiceWorkerRegisterJob::Start() {
context_->storage()->FindRegistrationForPattern(
pattern_,
base::Bind(
&ServiceWorkerRegisterJob::HandleExistingRegistrationAndContinue,
weak_factory_.GetWeakPtr()));
}
bool ServiceWorkerRegisterJob::Equals(ServiceWorkerRegisterJobBase* job) {
if (job->GetType() != GetType())
return false;
ServiceWorkerRegisterJob* register_job =
static_cast<ServiceWorkerRegisterJob*>(job);
return register_job->pattern_ == pattern_ &&
register_job->script_url_ == script_url_;
}
RegistrationJobType ServiceWorkerRegisterJob::GetType() {
return REGISTER;
}
void ServiceWorkerRegisterJob::HandleExistingRegistrationAndContinue(
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& registration) {
if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) {
Complete(status);
return;
}
if (registration.get() && registration->script_url() == script_url_) {
registration_ = registration;
if (!registration_->active_version()) {
UpdateAndContinue(status);
return;
}
Complete(SERVICE_WORKER_OK);
return;
}
if (!registration.get()) {
RegisterAndContinue(SERVICE_WORKER_OK);
return;
}
registration->Shutdown();
context_->storage()->DeleteRegistration(
pattern_,
base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue,
weak_factory_.GetWeakPtr()));
}
void ServiceWorkerRegisterJob::RegisterAndContinue(
ServiceWorkerStatusCode status) {
DCHECK(!registration_);
if (status != SERVICE_WORKER_OK) {
Complete(status);
return;
}
registration_ = new ServiceWorkerRegistration(
pattern_, script_url_, context_->storage()->NewRegistrationId(),
context_);
context_->storage()->StoreRegistration(
registration_.get(),
base::Bind(&ServiceWorkerRegisterJob::UpdateAndContinue,
weak_factory_.GetWeakPtr()));
}
void ServiceWorkerRegisterJob::UpdateAndContinue(
ServiceWorkerStatusCode status) {
DCHECK(registration_);
if (status != SERVICE_WORKER_OK) {
Complete(status);
return;
}
DCHECK(!registration_->pending_version());
pending_version_ = new ServiceWorkerVersion(
registration_, context_->storage()->NewVersionId(), context_);
for (std::vector<int>::const_iterator it = pending_process_ids_.begin();
it != pending_process_ids_.end();
++it)
pending_version_->AddProcessToWorker(*it);
pending_version_->StartWorker(
base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished,
weak_factory_.GetWeakPtr()));
}
void ServiceWorkerRegisterJob::OnStartWorkerFinished(
ServiceWorkerStatusCode status) {
if (status != SERVICE_WORKER_OK) {
Complete(status);
return;
}
DCHECK(!registration_->pending_version());
registration_->set_pending_version(pending_version_);
RunCallbacks(status);
InstallAndContinue();
}
void ServiceWorkerRegisterJob::InstallAndContinue() {
pending_version_->DispatchInstallEvent(
-1,
base::Bind(&ServiceWorkerRegisterJob::OnInstallFinished,
weak_factory_.GetWeakPtr()));
}
void ServiceWorkerRegisterJob::OnInstallFinished(
ServiceWorkerStatusCode status) {
if (status != SERVICE_WORKER_OK) {
registration_->set_pending_version(NULL);
Complete(status);
return;
}
ActivateAndContinue();
}
void ServiceWorkerRegisterJob::ActivateAndContinue() {
registration_->set_pending_version(NULL);
pending_version_->SetStatus(ServiceWorkerVersion::ACTIVE);
DCHECK(!registration_->active_version());
registration_->set_active_version(pending_version_);
pending_version_ = NULL;
Complete(SERVICE_WORKER_OK);
}
void ServiceWorkerRegisterJob::Complete(ServiceWorkerStatusCode status) {
RunCallbacks(status);
if (pending_version_) {
DCHECK(!registration_->pending_version());
DCHECK(!registration_->active_version());
pending_version_->Shutdown();
}
context_->job_coordinator()->FinishJob(pattern_, this);
}
void ServiceWorkerRegisterJob::RunCallbacks(ServiceWorkerStatusCode status) {
for (std::vector<RegistrationCallback>::iterator it = callbacks_.begin();
it != callbacks_.end();
++it) {
it->Run(status, status == SERVICE_WORKER_OK ? registration_ : NULL);
}
callbacks_.clear();
}
}