| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/service_worker/service_worker_register_job.h" | 5 #include "content/browser/service_worker/service_worker_register_job.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/browser/service_worker/service_worker_context_core.h" | 10 #include "content/browser/service_worker/service_worker_context_core.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 const GURL& script_url) | 31 const GURL& script_url) |
| 32 : context_(context), | 32 : context_(context), |
| 33 pattern_(pattern), | 33 pattern_(pattern), |
| 34 script_url_(script_url), | 34 script_url_(script_url), |
| 35 phase_(INITIAL), | 35 phase_(INITIAL), |
| 36 is_promise_resolved_(false), | 36 is_promise_resolved_(false), |
| 37 promise_resolved_status_(SERVICE_WORKER_OK), | 37 promise_resolved_status_(SERVICE_WORKER_OK), |
| 38 weak_factory_(this) {} | 38 weak_factory_(this) {} |
| 39 | 39 |
| 40 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() { | 40 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() { |
| 41 DCHECK(!context_ || phase_ == INITIAL || phase_ == COMPLETE) | 41 DCHECK(!context_ || |
| 42 phase_ == INITIAL || phase_ == COMPLETE || phase_ == ABORT) |
| 42 << "Jobs should only be interrupted during shutdown."; | 43 << "Jobs should only be interrupted during shutdown."; |
| 43 } | 44 } |
| 44 | 45 |
| 45 void ServiceWorkerRegisterJob::AddCallback(const RegistrationCallback& callback, | 46 void ServiceWorkerRegisterJob::AddCallback(const RegistrationCallback& callback, |
| 46 int process_id) { | 47 int process_id) { |
| 47 if (!is_promise_resolved_) { | 48 if (!is_promise_resolved_) { |
| 48 callbacks_.push_back(callback); | 49 callbacks_.push_back(callback); |
| 49 if (process_id != -1 && (phase_ < UPDATE || !pending_version())) | 50 if (process_id != -1 && (phase_ < UPDATE || !pending_version())) |
| 50 pending_process_ids_.push_back(process_id); | 51 pending_process_ids_.push_back(process_id); |
| 51 return; | 52 return; |
| 52 } | 53 } |
| 53 RunSoon(base::Bind( | 54 RunSoon(base::Bind( |
| 54 callback, promise_resolved_status_, | 55 callback, promise_resolved_status_, |
| 55 promise_resolved_registration_, promise_resolved_version_)); | 56 promise_resolved_registration_, promise_resolved_version_)); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void ServiceWorkerRegisterJob::Start() { | 59 void ServiceWorkerRegisterJob::Start() { |
| 59 SetPhase(START); | 60 SetPhase(START); |
| 60 context_->storage()->FindRegistrationForPattern( | 61 context_->storage()->FindRegistrationForPattern( |
| 61 pattern_, | 62 pattern_, |
| 62 base::Bind( | 63 base::Bind( |
| 63 &ServiceWorkerRegisterJob::HandleExistingRegistrationAndContinue, | 64 &ServiceWorkerRegisterJob::HandleExistingRegistrationAndContinue, |
| 64 weak_factory_.GetWeakPtr())); | 65 weak_factory_.GetWeakPtr())); |
| 65 } | 66 } |
| 66 | 67 |
| 68 void ServiceWorkerRegisterJob::Abort() { |
| 69 SetPhase(ABORT); |
| 70 Complete(SERVICE_WORKER_ERROR_ABORT); |
| 71 } |
| 72 |
| 67 bool ServiceWorkerRegisterJob::Equals(ServiceWorkerRegisterJobBase* job) { | 73 bool ServiceWorkerRegisterJob::Equals(ServiceWorkerRegisterJobBase* job) { |
| 68 if (job->GetType() != GetType()) | 74 if (job->GetType() != GetType()) |
| 69 return false; | 75 return false; |
| 70 ServiceWorkerRegisterJob* register_job = | 76 ServiceWorkerRegisterJob* register_job = |
| 71 static_cast<ServiceWorkerRegisterJob*>(job); | 77 static_cast<ServiceWorkerRegisterJob*>(job); |
| 72 return register_job->pattern_ == pattern_ && | 78 return register_job->pattern_ == pattern_ && |
| 73 register_job->script_url_ == script_url_; | 79 register_job->script_url_ == script_url_; |
| 74 } | 80 } |
| 75 | 81 |
| 76 RegistrationJobType ServiceWorkerRegisterJob::GetType() { | 82 RegistrationJobType ServiceWorkerRegisterJob::GetType() { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 break; | 130 break; |
| 125 case STORE: | 131 case STORE: |
| 126 DCHECK(phase_ == INSTALL) << phase_; | 132 DCHECK(phase_ == INSTALL) << phase_; |
| 127 break; | 133 break; |
| 128 case ACTIVATE: | 134 case ACTIVATE: |
| 129 DCHECK(phase_ == STORE) << phase_; | 135 DCHECK(phase_ == STORE) << phase_; |
| 130 break; | 136 break; |
| 131 case COMPLETE: | 137 case COMPLETE: |
| 132 DCHECK(phase_ != INITIAL && phase_ != COMPLETE) << phase_; | 138 DCHECK(phase_ != INITIAL && phase_ != COMPLETE) << phase_; |
| 133 break; | 139 break; |
| 140 case ABORT: |
| 141 break; |
| 134 } | 142 } |
| 135 phase_ = phase; | 143 phase_ = phase; |
| 136 } | 144 } |
| 137 | 145 |
| 138 // This function corresponds to the steps in Register following | 146 // This function corresponds to the steps in Register following |
| 139 // "Let serviceWorkerRegistration be _GetRegistration(scope)" | 147 // "Let serviceWorkerRegistration be _GetRegistration(scope)" |
| 140 // |existing_registration| corresponds to serviceWorkerRegistration. | 148 // |existing_registration| corresponds to serviceWorkerRegistration. |
| 141 // Throughout this file, comments in quotes are excerpts from the spec. | 149 // Throughout this file, comments in quotes are excerpts from the spec. |
| 142 void ServiceWorkerRegisterJob::HandleExistingRegistrationAndContinue( | 150 void ServiceWorkerRegisterJob::HandleExistingRegistrationAndContinue( |
| 143 ServiceWorkerStatusCode status, | 151 ServiceWorkerStatusCode status, |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 Complete(status); | 360 Complete(status); |
| 353 return; | 361 return; |
| 354 } | 362 } |
| 355 context_->storage()->UpdateToActiveState( | 363 context_->storage()->UpdateToActiveState( |
| 356 registration(), | 364 registration(), |
| 357 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 365 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 358 Complete(SERVICE_WORKER_OK); | 366 Complete(SERVICE_WORKER_OK); |
| 359 } | 367 } |
| 360 | 368 |
| 361 void ServiceWorkerRegisterJob::Complete(ServiceWorkerStatusCode status) { | 369 void ServiceWorkerRegisterJob::Complete(ServiceWorkerStatusCode status) { |
| 362 SetPhase(COMPLETE); | 370 if (phase_ != ABORT) |
| 371 SetPhase(COMPLETE); |
| 363 if (status != SERVICE_WORKER_OK) { | 372 if (status != SERVICE_WORKER_OK) { |
| 364 if (registration() && registration()->waiting_version()) { | 373 if (registration() && registration()->waiting_version()) { |
| 365 DisassociateWaitingVersionFromDocuments( | 374 DisassociateWaitingVersionFromDocuments( |
| 366 context_, registration()->waiting_version()->version_id()); | 375 context_, registration()->waiting_version()->version_id()); |
| 367 registration()->set_waiting_version(NULL); | 376 registration()->set_waiting_version(NULL); |
| 368 } | 377 } |
| 369 if (registration() && !registration()->active_version()) { | 378 if (registration() && !registration()->active_version()) { |
| 370 context_->storage()->DeleteRegistration( | 379 context_->storage()->DeleteRegistration( |
| 371 registration()->id(), | 380 registration()->id(), |
| 372 registration()->script_url().GetOrigin(), | 381 registration()->script_url().GetOrigin(), |
| 373 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 382 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 374 } | 383 } |
| 375 if (!is_promise_resolved_) | 384 if (!is_promise_resolved_) |
| 376 ResolvePromise(status, NULL, NULL); | 385 ResolvePromise(status, NULL, NULL); |
| 377 } | 386 } |
| 378 DCHECK(callbacks_.empty()); | 387 DCHECK(callbacks_.empty()); |
| 379 if (registration()) { | 388 if (registration()) { |
| 380 context_->storage()->NotifyDoneInstallingRegistration( | 389 context_->storage()->NotifyDoneInstallingRegistration( |
| 381 registration(), pending_version(), status); | 390 registration(), pending_version(), status); |
| 382 } | 391 } |
| 383 context_->job_coordinator()->FinishJob(pattern_, this); | 392 // The caller of Abort() takes care of removing the jobs from the queue. |
| 393 if (phase_ != ABORT) |
| 394 context_->job_coordinator()->FinishJob(pattern_, this); |
| 384 } | 395 } |
| 385 | 396 |
| 386 void ServiceWorkerRegisterJob::ResolvePromise( | 397 void ServiceWorkerRegisterJob::ResolvePromise( |
| 387 ServiceWorkerStatusCode status, | 398 ServiceWorkerStatusCode status, |
| 388 ServiceWorkerRegistration* registration, | 399 ServiceWorkerRegistration* registration, |
| 389 ServiceWorkerVersion* version) { | 400 ServiceWorkerVersion* version) { |
| 390 DCHECK(!is_promise_resolved_); | 401 DCHECK(!is_promise_resolved_); |
| 391 is_promise_resolved_ = true; | 402 is_promise_resolved_ = true; |
| 392 promise_resolved_status_ = status; | 403 promise_resolved_status_ = status; |
| 393 promise_resolved_registration_ = registration; | 404 promise_resolved_registration_ = registration; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 if (!host->IsContextAlive()) | 456 if (!host->IsContextAlive()) |
| 446 continue; | 457 continue; |
| 447 if (host->waiting_version() && | 458 if (host->waiting_version() && |
| 448 host->waiting_version()->version_id() == version_id) { | 459 host->waiting_version()->version_id() == version_id) { |
| 449 host->SetWaitingVersion(NULL); | 460 host->SetWaitingVersion(NULL); |
| 450 } | 461 } |
| 451 } | 462 } |
| 452 } | 463 } |
| 453 | 464 |
| 454 } // namespace content | 465 } // namespace content |
| OLD | NEW |