| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 | 86 |
| 87 scoped_refptr<ServiceWorkerRegistration> registration = | 87 scoped_refptr<ServiceWorkerRegistration> registration = |
| 88 context_->storage()->GetUninstallingRegistration(pattern_); | 88 context_->storage()->GetUninstallingRegistration(pattern_); |
| 89 if (registration) | 89 if (registration.get()) |
| 90 RunSoon(base::Bind(next_step, SERVICE_WORKER_OK, registration)); | 90 RunSoon(base::Bind(next_step, SERVICE_WORKER_OK, registration)); |
| 91 else | 91 else |
| 92 context_->storage()->FindRegistrationForPattern(pattern_, next_step); | 92 context_->storage()->FindRegistrationForPattern(pattern_, next_step); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void ServiceWorkerRegisterJob::Abort() { | 95 void ServiceWorkerRegisterJob::Abort() { |
| 96 SetPhase(ABORT); | 96 SetPhase(ABORT); |
| 97 CompleteInternal(SERVICE_WORKER_ERROR_ABORT); | 97 CompleteInternal(SERVICE_WORKER_ERROR_ABORT); |
| 98 // 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 |
| 99 // the jobs from the queue. | 99 // the jobs from the queue. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 112 return job_type_; | 112 return job_type_; |
| 113 } | 113 } |
| 114 | 114 |
| 115 ServiceWorkerRegisterJob::Internal::Internal() {} | 115 ServiceWorkerRegisterJob::Internal::Internal() {} |
| 116 | 116 |
| 117 ServiceWorkerRegisterJob::Internal::~Internal() {} | 117 ServiceWorkerRegisterJob::Internal::~Internal() {} |
| 118 | 118 |
| 119 void ServiceWorkerRegisterJob::set_registration( | 119 void ServiceWorkerRegisterJob::set_registration( |
| 120 ServiceWorkerRegistration* registration) { | 120 ServiceWorkerRegistration* registration) { |
| 121 DCHECK(phase_ == START || phase_ == REGISTER) << phase_; | 121 DCHECK(phase_ == START || phase_ == REGISTER) << phase_; |
| 122 DCHECK(!internal_.registration); | 122 DCHECK(!internal_.registration.get()); |
| 123 internal_.registration = registration; | 123 internal_.registration = registration; |
| 124 } | 124 } |
| 125 | 125 |
| 126 ServiceWorkerRegistration* ServiceWorkerRegisterJob::registration() { | 126 ServiceWorkerRegistration* ServiceWorkerRegisterJob::registration() { |
| 127 DCHECK(phase_ >= REGISTER || job_type_ == UPDATE_JOB) << phase_; | 127 DCHECK(phase_ >= REGISTER || job_type_ == UPDATE_JOB) << phase_; |
| 128 return internal_.registration; | 128 return internal_.registration.get(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void ServiceWorkerRegisterJob::set_new_version( | 131 void ServiceWorkerRegisterJob::set_new_version( |
| 132 ServiceWorkerVersion* version) { | 132 ServiceWorkerVersion* version) { |
| 133 DCHECK(phase_ == UPDATE) << phase_; | 133 DCHECK(phase_ == UPDATE) << phase_; |
| 134 DCHECK(!internal_.new_version); | 134 DCHECK(!internal_.new_version.get()); |
| 135 internal_.new_version = version; | 135 internal_.new_version = version; |
| 136 } | 136 } |
| 137 | 137 |
| 138 ServiceWorkerVersion* ServiceWorkerRegisterJob::new_version() { | 138 ServiceWorkerVersion* ServiceWorkerRegisterJob::new_version() { |
| 139 DCHECK(phase_ >= UPDATE) << phase_; | 139 DCHECK(phase_ >= UPDATE) << phase_; |
| 140 return internal_.new_version; | 140 return internal_.new_version.get(); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void ServiceWorkerRegisterJob::SetPhase(Phase phase) { | 143 void ServiceWorkerRegisterJob::SetPhase(Phase phase) { |
| 144 switch (phase) { | 144 switch (phase) { |
| 145 case INITIAL: | 145 case INITIAL: |
| 146 NOTREACHED(); | 146 NOTREACHED(); |
| 147 break; | 147 break; |
| 148 case START: | 148 case START: |
| 149 DCHECK(phase_ == INITIAL) << phase_; | 149 DCHECK(phase_ == INITIAL) << phase_; |
| 150 break; | 150 break; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 174 // Throughout this file, comments in quotes are excerpts from the spec. | 174 // Throughout this file, comments in quotes are excerpts from the spec. |
| 175 void ServiceWorkerRegisterJob::ContinueWithRegistration( | 175 void ServiceWorkerRegisterJob::ContinueWithRegistration( |
| 176 ServiceWorkerStatusCode status, | 176 ServiceWorkerStatusCode status, |
| 177 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { | 177 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { |
| 178 DCHECK_EQ(REGISTRATION_JOB, job_type_); | 178 DCHECK_EQ(REGISTRATION_JOB, job_type_); |
| 179 if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) { | 179 if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) { |
| 180 Complete(status); | 180 Complete(status); |
| 181 return; | 181 return; |
| 182 } | 182 } |
| 183 | 183 |
| 184 if (!existing_registration) { | 184 if (!existing_registration.get()) { |
| 185 RegisterAndContinue(SERVICE_WORKER_OK); | 185 RegisterAndContinue(SERVICE_WORKER_OK); |
| 186 return; | 186 return; |
| 187 } | 187 } |
| 188 | 188 |
| 189 // "Set registration.[[Uninstalling]] to false." | 189 // "Set registration.[[Uninstalling]] to false." |
| 190 existing_registration->AbortPendingClear(); | 190 existing_registration->AbortPendingClear(); |
| 191 | 191 |
| 192 // "If scriptURL is equal to registration.[[ScriptURL]], then:" | 192 // "If scriptURL is equal to registration.[[ScriptURL]], then:" |
| 193 if (existing_registration->script_url() == script_url_) { | 193 if (existing_registration->script_url() == script_url_) { |
| 194 // Spec says to resolve with registration.[[GetNewestWorker]]. We come close | 194 // Spec says to resolve with registration.[[GetNewestWorker]]. We come close |
| 195 // by resolving with the active version. | 195 // by resolving with the active version. |
| 196 set_registration(existing_registration); | 196 set_registration(existing_registration.get()); |
| 197 | 197 |
| 198 if (!existing_registration->active_version()) { | 198 if (!existing_registration->active_version()) { |
| 199 UpdateAndContinue(); | 199 UpdateAndContinue(); |
| 200 return; | 200 return; |
| 201 } | 201 } |
| 202 | 202 |
| 203 ResolvePromise( | 203 ResolvePromise(status, |
| 204 status, existing_registration, existing_registration->active_version()); | 204 existing_registration.get(), |
| 205 existing_registration->active_version()); |
| 205 Complete(SERVICE_WORKER_OK); | 206 Complete(SERVICE_WORKER_OK); |
| 206 return; | 207 return; |
| 207 } | 208 } |
| 208 | 209 |
| 209 // "Set registration.[[ScriptURL]] to scriptURL." We accomplish this by | 210 // "Set registration.[[ScriptURL]] to scriptURL." We accomplish this by |
| 210 // deleting the existing registration and registering a new one. | 211 // deleting the existing registration and registering a new one. |
| 211 // TODO(michaeln): Deactivate the live existing_registration object and | 212 // TODO(michaeln): Deactivate the live existing_registration object and |
| 212 // eventually call storage->DeleteVersionResources() when it no longer has any | 213 // eventually call storage->DeleteVersionResources() when it no longer has any |
| 213 // controllees. | 214 // controllees. |
| 214 context_->storage()->DeleteRegistration( | 215 context_->storage()->DeleteRegistration( |
| 215 existing_registration->id(), | 216 existing_registration->id(), |
| 216 existing_registration->script_url().GetOrigin(), | 217 existing_registration->script_url().GetOrigin(), |
| 217 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, | 218 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, |
| 218 weak_factory_.GetWeakPtr())); | 219 weak_factory_.GetWeakPtr())); |
| 219 } | 220 } |
| 220 | 221 |
| 221 void ServiceWorkerRegisterJob::ContinueWithUpdate( | 222 void ServiceWorkerRegisterJob::ContinueWithUpdate( |
| 222 ServiceWorkerStatusCode status, | 223 ServiceWorkerStatusCode status, |
| 223 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { | 224 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { |
| 224 DCHECK_EQ(UPDATE_JOB, job_type_); | 225 DCHECK_EQ(UPDATE_JOB, job_type_); |
| 225 if (status != SERVICE_WORKER_OK) { | 226 if (status != SERVICE_WORKER_OK) { |
| 226 Complete(status); | 227 Complete(status); |
| 227 return; | 228 return; |
| 228 } | 229 } |
| 229 | 230 |
| 230 if (existing_registration != registration()) { | 231 if (existing_registration.get() != registration()) { |
| 231 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); | 232 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); |
| 232 return; | 233 return; |
| 233 } | 234 } |
| 234 | 235 |
| 235 // TODO(michaeln): If the last update check was less than 24 hours | 236 // TODO(michaeln): If the last update check was less than 24 hours |
| 236 // ago, depending on the freshness of the cached worker script we | 237 // ago, depending on the freshness of the cached worker script we |
| 237 // may be able to complete the update job right here. | 238 // may be able to complete the update job right here. |
| 238 | 239 |
| 239 UpdateAndContinue(); | 240 UpdateAndContinue(); |
| 240 } | 241 } |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 } | 419 } |
| 419 callbacks_.clear(); | 420 callbacks_.clear(); |
| 420 } | 421 } |
| 421 | 422 |
| 422 void ServiceWorkerRegisterJob::OnPausedAfterDownload() { | 423 void ServiceWorkerRegisterJob::OnPausedAfterDownload() { |
| 423 // This happens prior to OnStartWorkerFinished time. | 424 // This happens prior to OnStartWorkerFinished time. |
| 424 scoped_refptr<ServiceWorkerVersion> most_recent_version = | 425 scoped_refptr<ServiceWorkerVersion> most_recent_version = |
| 425 registration()->waiting_version() ? | 426 registration()->waiting_version() ? |
| 426 registration()->waiting_version() : | 427 registration()->waiting_version() : |
| 427 registration()->active_version(); | 428 registration()->active_version(); |
| 428 DCHECK(most_recent_version); | 429 DCHECK(most_recent_version.get()); |
| 429 int64 most_recent_script_id = | 430 int64 most_recent_script_id = |
| 430 most_recent_version->script_cache_map()->Lookup(script_url_); | 431 most_recent_version->script_cache_map()->Lookup(script_url_); |
| 431 int64 new_script_id = | 432 int64 new_script_id = |
| 432 new_version()->script_cache_map()->Lookup(script_url_); | 433 new_version()->script_cache_map()->Lookup(script_url_); |
| 433 | 434 |
| 434 // TODO(michaeln): It would be better to compare as the new resource | 435 // TODO(michaeln): It would be better to compare as the new resource |
| 435 // is being downloaded and to avoid writing it to disk until we know | 436 // is being downloaded and to avoid writing it to disk until we know |
| 436 // its needed. | 437 // its needed. |
| 437 context_->storage()->CompareScriptResources( | 438 context_->storage()->CompareScriptResources( |
| 438 most_recent_script_id, new_script_id, | 439 most_recent_script_id, new_script_id, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 ServiceWorkerProviderHost* host = it->GetProviderHost(); | 478 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
| 478 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), | 479 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), |
| 479 host->document_url())) { | 480 host->document_url())) { |
| 480 if (host->CanAssociateRegistration(registration)) | 481 if (host->CanAssociateRegistration(registration)) |
| 481 host->AssociateRegistration(registration); | 482 host->AssociateRegistration(registration); |
| 482 } | 483 } |
| 483 } | 484 } |
| 484 } | 485 } |
| 485 | 486 |
| 486 } // namespace content | 487 } // namespace content |
| OLD | NEW |