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