Chromium Code Reviews| 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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 DCHECK(phase_ == UPDATE) << phase_; | 133 DCHECK(phase_ == UPDATE) << phase_; |
| 134 DCHECK(!internal_.new_version); | 134 DCHECK(!internal_.new_version); |
| 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; |
| 141 } | 141 } |
| 142 | 142 |
| 143 void ServiceWorkerRegisterJob::set_uninstalling_registration( | |
| 144 ServiceWorkerRegistration* registration) { | |
| 145 DCHECK_EQ(phase_, WAIT_FOR_UNINSTALL); | |
| 146 internal_.uninstalling_registration = registration; | |
| 147 } | |
| 148 | |
| 149 ServiceWorkerRegistration* | |
| 150 ServiceWorkerRegisterJob::uninstalling_registration() { | |
| 151 DCHECK_EQ(phase_, WAIT_FOR_UNINSTALL); | |
| 152 return internal_.uninstalling_registration; | |
| 153 } | |
| 154 | |
| 143 void ServiceWorkerRegisterJob::SetPhase(Phase phase) { | 155 void ServiceWorkerRegisterJob::SetPhase(Phase phase) { |
| 144 switch (phase) { | 156 switch (phase) { |
| 145 case INITIAL: | 157 case INITIAL: |
| 146 NOTREACHED(); | 158 NOTREACHED(); |
| 147 break; | 159 break; |
| 148 case START: | 160 case START: |
| 149 DCHECK(phase_ == INITIAL) << phase_; | 161 DCHECK(phase_ == INITIAL) << phase_; |
| 150 break; | 162 break; |
| 163 case WAIT_FOR_UNINSTALL: | |
| 164 DCHECK(phase_ == START) << phase_; | |
| 165 break; | |
| 151 case REGISTER: | 166 case REGISTER: |
| 152 DCHECK(phase_ == START) << phase_; | 167 DCHECK(phase_ == START || phase_ == WAIT_FOR_UNINSTALL) << phase_; |
| 153 break; | 168 break; |
| 154 case UPDATE: | 169 case UPDATE: |
| 155 DCHECK(phase_ == START || phase_ == REGISTER) << phase_; | 170 DCHECK(phase_ == START || phase_ == REGISTER) << phase_; |
| 156 break; | 171 break; |
| 157 case INSTALL: | 172 case INSTALL: |
| 158 DCHECK(phase_ == UPDATE) << phase_; | 173 DCHECK(phase_ == UPDATE) << phase_; |
| 159 break; | 174 break; |
| 160 case STORE: | 175 case STORE: |
| 161 DCHECK(phase_ == INSTALL) << phase_; | 176 DCHECK(phase_ == INSTALL) << phase_; |
| 162 break; | 177 break; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 179 if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) { | 194 if (status != SERVICE_WORKER_ERROR_NOT_FOUND && status != SERVICE_WORKER_OK) { |
| 180 Complete(status); | 195 Complete(status); |
| 181 return; | 196 return; |
| 182 } | 197 } |
| 183 | 198 |
| 184 if (!existing_registration) { | 199 if (!existing_registration) { |
| 185 RegisterAndContinue(SERVICE_WORKER_OK); | 200 RegisterAndContinue(SERVICE_WORKER_OK); |
| 186 return; | 201 return; |
| 187 } | 202 } |
| 188 | 203 |
| 189 // "Set registration.[[Uninstalling]] to false." | |
| 190 existing_registration->AbortPendingClear(); | |
| 191 | |
| 192 // "If scriptURL is equal to registration.[[ScriptURL]], then:" | 204 // "If scriptURL is equal to registration.[[ScriptURL]], then:" |
| 193 if (existing_registration->script_url() == script_url_) { | 205 if (existing_registration->script_url() == script_url_) { |
| 194 // Spec says to resolve with registration.[[GetNewestWorker]]. We come close | 206 // "Set registration.[[Uninstalling]] to false." |
| 195 // by resolving with the active version. | 207 existing_registration->AbortPendingClear(base::Bind( |
| 196 set_registration(existing_registration); | 208 &ServiceWorkerRegisterJob::ContinueWithRegistrationForSameScriptUrl, |
| 197 | 209 weak_factory_.GetWeakPtr(), |
| 198 if (!existing_registration->active_version()) { | 210 existing_registration)); |
| 199 UpdateAndContinue(); | |
| 200 return; | |
| 201 } | |
| 202 | |
| 203 ResolvePromise( | |
| 204 status, existing_registration, existing_registration->active_version()); | |
| 205 Complete(SERVICE_WORKER_OK); | |
| 206 return; | 211 return; |
| 207 } | 212 } |
| 208 | 213 |
| 209 // "Set registration.[[ScriptURL]] to scriptURL." We accomplish this by | 214 if (existing_registration->is_uninstalling()) { |
| 210 // deleting the existing registration and registering a new one. | 215 // "Wait until the Record {[[key]], [[value]]} entry of its |
| 211 // TODO(michaeln): Deactivate the live existing_registration object and | 216 // [[ScopeToRegistrationMap]] where registation.scope matches entry.[[key]] |
| 212 // eventually call storage->DeleteVersionResources() when it no longer has any | 217 // is deleted." |
| 213 // controllees. | 218 WaitForUninstall(existing_registration); |
| 214 context_->storage()->DeleteRegistration( | 219 return; |
| 215 existing_registration->id(), | 220 } |
| 216 existing_registration->script_url().GetOrigin(), | 221 |
| 217 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, | 222 // "Set registration.[[ScriptURL]] to scriptURL." |
| 218 weak_factory_.GetWeakPtr())); | 223 existing_registration->set_script_url(script_url_); |
|
michaeln
2014/08/21 23:52:44
This can put the object in a strange state where .
falken
2014/08/22 11:08:00
Thanks for taking a look!
On 2014/08/21 23:52:44,
| |
| 224 | |
| 225 // "Set registration.[[Uninstalling]] to false." | |
| 226 DCHECK(!existing_registration->is_uninstalling()); | |
| 227 | |
| 228 // "Return the result of running the [[Update]] algorithm, or its equivalent, | |
| 229 // passing registration as the argument." | |
| 230 set_registration(existing_registration); | |
| 231 UpdateAndContinue(); | |
| 219 } | 232 } |
| 220 | 233 |
| 221 void ServiceWorkerRegisterJob::ContinueWithUpdate( | 234 void ServiceWorkerRegisterJob::ContinueWithUpdate( |
| 222 ServiceWorkerStatusCode status, | 235 ServiceWorkerStatusCode status, |
| 223 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { | 236 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { |
| 224 DCHECK_EQ(UPDATE_JOB, job_type_); | 237 DCHECK_EQ(UPDATE_JOB, job_type_); |
| 225 if (status != SERVICE_WORKER_OK) { | 238 if (status != SERVICE_WORKER_OK) { |
| 226 Complete(status); | 239 Complete(status); |
| 227 return; | 240 return; |
| 228 } | 241 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 249 return; | 262 return; |
| 250 } | 263 } |
| 251 | 264 |
| 252 set_registration(new ServiceWorkerRegistration( | 265 set_registration(new ServiceWorkerRegistration( |
| 253 pattern_, script_url_, context_->storage()->NewRegistrationId(), | 266 pattern_, script_url_, context_->storage()->NewRegistrationId(), |
| 254 context_)); | 267 context_)); |
| 255 AssociateProviderHostsToRegistration(registration()); | 268 AssociateProviderHostsToRegistration(registration()); |
| 256 UpdateAndContinue(); | 269 UpdateAndContinue(); |
| 257 } | 270 } |
| 258 | 271 |
| 272 void ServiceWorkerRegisterJob::WaitForUninstall( | |
| 273 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { | |
| 274 SetPhase(WAIT_FOR_UNINSTALL); | |
| 275 set_uninstalling_registration(existing_registration); | |
| 276 uninstalling_registration()->AddListener(this); | |
| 277 } | |
| 278 | |
| 279 void ServiceWorkerRegisterJob::ContinueWithRegistrationForSameScriptUrl( | |
| 280 const scoped_refptr<ServiceWorkerRegistration>& existing_registration, | |
| 281 ServiceWorkerStatusCode status) { | |
| 282 if (status != SERVICE_WORKER_OK) { | |
| 283 Complete(status); | |
| 284 return; | |
| 285 } | |
| 286 set_registration(existing_registration); | |
| 287 | |
| 288 // TODO(falken): Follow the spec steps. Resolve if the newest version | |
| 289 // shares the script URL. | |
| 290 if (!existing_registration->active_version()) { | |
| 291 UpdateAndContinue(); | |
| 292 return; | |
| 293 } | |
| 294 | |
| 295 ResolvePromise( | |
| 296 status, existing_registration, existing_registration->active_version()); | |
| 297 Complete(SERVICE_WORKER_OK); | |
| 298 } | |
| 299 | |
| 259 // This function corresponds to the spec's [[Update]] algorithm. | 300 // This function corresponds to the spec's [[Update]] algorithm. |
| 260 void ServiceWorkerRegisterJob::UpdateAndContinue() { | 301 void ServiceWorkerRegisterJob::UpdateAndContinue() { |
| 261 SetPhase(UPDATE); | 302 SetPhase(UPDATE); |
| 262 context_->storage()->NotifyInstallingRegistration(registration()); | 303 context_->storage()->NotifyInstallingRegistration(registration()); |
| 263 | 304 |
| 264 // TODO(falken): "If serviceWorkerRegistration.installingWorker is not null.." | 305 // TODO(falken): "If serviceWorkerRegistration.installingWorker is not null.." |
| 265 // then terminate the installing worker. It doesn't make sense to implement | 306 // then terminate the installing worker. It doesn't make sense to implement |
| 266 // yet since we always activate the worker if install completed, so there can | 307 // yet since we always activate the worker if install completed, so there can |
| 267 // be no installing worker at this point. | 308 // be no installing worker at this point. |
| 268 | 309 |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 438 most_recent_script_id, new_script_id, | 479 most_recent_script_id, new_script_id, |
| 439 base::Bind(&ServiceWorkerRegisterJob::OnCompareScriptResourcesComplete, | 480 base::Bind(&ServiceWorkerRegisterJob::OnCompareScriptResourcesComplete, |
| 440 weak_factory_.GetWeakPtr(), | 481 weak_factory_.GetWeakPtr(), |
| 441 most_recent_version)); | 482 most_recent_version)); |
| 442 } | 483 } |
| 443 | 484 |
| 444 bool ServiceWorkerRegisterJob::OnMessageReceived(const IPC::Message& message) { | 485 bool ServiceWorkerRegisterJob::OnMessageReceived(const IPC::Message& message) { |
| 445 return false; | 486 return false; |
| 446 } | 487 } |
| 447 | 488 |
| 489 void ServiceWorkerRegisterJob::OnRegistrationFinishedUninstalling( | |
| 490 ServiceWorkerRegistration* existing_registration) { | |
| 491 DCHECK_EQ(phase_, WAIT_FOR_UNINSTALL); | |
| 492 DCHECK_EQ(existing_registration, uninstalling_registration()); | |
| 493 existing_registration->RemoveListener(this); | |
| 494 set_uninstalling_registration(NULL); | |
| 495 RegisterAndContinue(SERVICE_WORKER_OK); | |
| 496 } | |
| 497 | |
| 448 void ServiceWorkerRegisterJob::OnCompareScriptResourcesComplete( | 498 void ServiceWorkerRegisterJob::OnCompareScriptResourcesComplete( |
| 449 ServiceWorkerVersion* most_recent_version, | 499 ServiceWorkerVersion* most_recent_version, |
| 450 ServiceWorkerStatusCode status, | 500 ServiceWorkerStatusCode status, |
| 451 bool are_equal) { | 501 bool are_equal) { |
| 452 if (are_equal) { | 502 if (are_equal) { |
| 453 // Only bump the last check time when we've bypassed the browser cache. | 503 // Only bump the last check time when we've bypassed the browser cache. |
| 454 base::TimeDelta time_since_last_check = | 504 base::TimeDelta time_since_last_check = |
| 455 base::Time::Now() - registration()->last_update_check(); | 505 base::Time::Now() - registration()->last_update_check(); |
| 456 if (time_since_last_check > base::TimeDelta::FromHours(24)) { | 506 if (time_since_last_check > base::TimeDelta::FromHours(24)) { |
| 457 registration()->set_last_update_check(base::Time::Now()); | 507 registration()->set_last_update_check(base::Time::Now()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 477 ServiceWorkerProviderHost* host = it->GetProviderHost(); | 527 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
| 478 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), | 528 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), |
| 479 host->document_url())) { | 529 host->document_url())) { |
| 480 if (host->CanAssociateRegistration(registration)) | 530 if (host->CanAssociateRegistration(registration)) |
| 481 host->AssociateRegistration(registration); | 531 host->AssociateRegistration(registration); |
| 482 } | 532 } |
| 483 } | 533 } |
| 484 } | 534 } |
| 485 | 535 |
| 486 } // namespace content | 536 } // namespace content |
| OLD | NEW |