| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 ServiceWorkerStorage::FindRegistrationCallback next_step; | 76 ServiceWorkerStorage::FindRegistrationCallback next_step; |
| 77 if (job_type_ == REGISTRATION_JOB) { | 77 if (job_type_ == REGISTRATION_JOB) { |
| 78 next_step = base::Bind( | 78 next_step = base::Bind( |
| 79 &ServiceWorkerRegisterJob::ContinueWithRegistration, | 79 &ServiceWorkerRegisterJob::ContinueWithRegistration, |
| 80 weak_factory_.GetWeakPtr()); | 80 weak_factory_.GetWeakPtr()); |
| 81 } else { | 81 } else { |
| 82 next_step = base::Bind( | 82 next_step = base::Bind( |
| 83 &ServiceWorkerRegisterJob::ContinueWithUpdate, | 83 &ServiceWorkerRegisterJob::ContinueWithUpdate, |
| 84 weak_factory_.GetWeakPtr()); | 84 weak_factory_.GetWeakPtr()); |
| 85 } | 85 } |
| 86 context_->storage()->FindRegistrationForPattern(pattern_, next_step); | 86 |
| 87 scoped_refptr<ServiceWorkerRegistration> registration = |
| 88 context_->storage()->GetUninstallingRegistration(pattern_); |
| 89 if (registration) |
| 90 RunSoon(base::Bind(next_step, SERVICE_WORKER_OK, registration)); |
| 91 else |
| 92 context_->storage()->FindRegistrationForPattern(pattern_, next_step); |
| 87 } | 93 } |
| 88 | 94 |
| 89 void ServiceWorkerRegisterJob::Abort() { | 95 void ServiceWorkerRegisterJob::Abort() { |
| 90 SetPhase(ABORT); | 96 SetPhase(ABORT); |
| 91 CompleteInternal(SERVICE_WORKER_ERROR_ABORT); | 97 CompleteInternal(SERVICE_WORKER_ERROR_ABORT); |
| 92 // Don't have to call FinishJob() because the caller takes care of removing | 98 // Don't have to call FinishJob() because the caller takes care of removing |
| 93 // the jobs from the queue. | 99 // the jobs from the queue. |
| 94 } | 100 } |
| 95 | 101 |
| 96 bool ServiceWorkerRegisterJob::Equals(ServiceWorkerRegisterJobBase* job) { | 102 bool ServiceWorkerRegisterJob::Equals(ServiceWorkerRegisterJobBase* job) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 break; | 162 break; |
| 157 case COMPLETE: | 163 case COMPLETE: |
| 158 DCHECK(phase_ != INITIAL && phase_ != COMPLETE) << phase_; | 164 DCHECK(phase_ != INITIAL && phase_ != COMPLETE) << phase_; |
| 159 break; | 165 break; |
| 160 case ABORT: | 166 case ABORT: |
| 161 break; | 167 break; |
| 162 } | 168 } |
| 163 phase_ = phase; | 169 phase_ = phase; |
| 164 } | 170 } |
| 165 | 171 |
| 166 // This function corresponds to the steps in Register following | 172 // This function corresponds to the steps in [[Register]] following |
| 167 // "Let serviceWorkerRegistration be _GetRegistration(scope)" | 173 // "Let registration be the result of running the [[GetRegistration]] algorithm. |
| 168 // |existing_registration| corresponds to serviceWorkerRegistration. | |
| 169 // Throughout this file, comments in quotes are excerpts from the spec. | 174 // Throughout this file, comments in quotes are excerpts from the spec. |
| 170 void ServiceWorkerRegisterJob::ContinueWithRegistration( | 175 void ServiceWorkerRegisterJob::ContinueWithRegistration( |
| 171 ServiceWorkerStatusCode status, | 176 ServiceWorkerStatusCode status, |
| 172 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { | 177 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { |
| 173 DCHECK_EQ(REGISTRATION_JOB, job_type_); | 178 DCHECK_EQ(REGISTRATION_JOB, job_type_); |
| 174 // On unexpected error, abort this registration job. | |
| 175 if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) { | 179 if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) { |
| 176 Complete(status); | 180 Complete(status); |
| 177 return; | 181 return; |
| 178 } | 182 } |
| 179 | 183 |
| 180 // "If serviceWorkerRegistration is not null and script is equal to | 184 if (!existing_registration) { |
| 181 // serviceWorkerRegistration.scriptUrl..." resolve with the existing | 185 RegisterAndContinue(SERVICE_WORKER_OK); |
| 182 // registration and abort. | 186 return; |
| 183 if (existing_registration.get() && | 187 } |
| 184 existing_registration->script_url() == script_url_) { | 188 |
| 189 // "1. Set registration.[[Uninstalling]] to false." |
| 190 existing_registration->AbortPendingClear(); |
| 191 |
| 192 // "2. If scriptURL is equal to registration.[[ScriptURL]], then:" |
| 193 if (existing_registration->script_url() == script_url_) { |
| 194 // Spec says to resolve with registration.[[GetNewestWorker]]. We come close |
| 195 // by resolving with the active version. |
| 185 set_registration(existing_registration); | 196 set_registration(existing_registration); |
| 186 // If there's no active version, go ahead to Update (this isn't in the spec | 197 |
| 187 // but seems reasonable, and without SoftUpdate implemented we can never | |
| 188 // Update otherwise). | |
| 189 if (!existing_registration->active_version()) { | 198 if (!existing_registration->active_version()) { |
| 190 UpdateAndContinue(); | 199 UpdateAndContinue(); |
| 191 return; | 200 return; |
| 192 } | 201 } |
| 202 |
| 193 ResolvePromise( | 203 ResolvePromise( |
| 194 status, existing_registration, existing_registration->active_version()); | 204 status, existing_registration, existing_registration->active_version()); |
| 195 Complete(SERVICE_WORKER_OK); | 205 Complete(SERVICE_WORKER_OK); |
| 196 return; | 206 return; |
| 197 } | 207 } |
| 198 | 208 |
| 199 // "If serviceWorkerRegistration is null..." create a new registration. | 209 // "Set registration.[[ScriptURL]] to scriptURL." We accomplish this by |
| 200 if (!existing_registration.get()) { | 210 // deleting the existing registration and registering a new one. |
| 201 RegisterAndContinue(SERVICE_WORKER_OK); | |
| 202 return; | |
| 203 } | |
| 204 | |
| 205 // On script URL mismatch, "set serviceWorkerRegistration.scriptUrl to | |
| 206 // script." We accomplish this by deleting the existing registration and | |
| 207 // registering a new one. | |
| 208 // TODO(falken): Match the spec. We now throw away the active_version_ and | |
| 209 // waiting_version_ of the existing registration, which isn't in the spec. | |
| 210 // TODO(michaeln): Deactivate the live existing_registration object and | 211 // TODO(michaeln): Deactivate the live existing_registration object and |
| 211 // eventually call storage->DeleteVersionResources() | 212 // eventually call storage->DeleteVersionResources() when it no longer has any |
| 212 // when it no longer has any controllees. | 213 // controllees. |
| 213 context_->storage()->DeleteRegistration( | 214 context_->storage()->DeleteRegistration( |
| 214 existing_registration->id(), | 215 existing_registration->id(), |
| 215 existing_registration->script_url().GetOrigin(), | 216 existing_registration->script_url().GetOrigin(), |
| 216 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, | 217 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, |
| 217 weak_factory_.GetWeakPtr())); | 218 weak_factory_.GetWeakPtr())); |
| 218 } | 219 } |
| 219 | 220 |
| 220 void ServiceWorkerRegisterJob::ContinueWithUpdate( | 221 void ServiceWorkerRegisterJob::ContinueWithUpdate( |
| 221 ServiceWorkerStatusCode status, | 222 ServiceWorkerStatusCode status, |
| 222 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { | 223 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 DCHECK(context); | 521 DCHECK(context); |
| 521 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = | 522 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = |
| 522 context->GetProviderHostIterator(); | 523 context->GetProviderHostIterator(); |
| 523 !it->IsAtEnd(); it->Advance()) { | 524 !it->IsAtEnd(); it->Advance()) { |
| 524 ServiceWorkerProviderHost* host = it->GetProviderHost(); | 525 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
| 525 host->UnsetVersion(version); | 526 host->UnsetVersion(version); |
| 526 } | 527 } |
| 527 } | 528 } |
| 528 | 529 |
| 529 } // namespace content | 530 } // namespace content |
| OLD | NEW |