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 26 matching lines...) Expand all Loading... |
37 is_promise_resolved_(false), | 37 is_promise_resolved_(false), |
38 promise_resolved_status_(SERVICE_WORKER_OK), | 38 promise_resolved_status_(SERVICE_WORKER_OK), |
39 weak_factory_(this) {} | 39 weak_factory_(this) {} |
40 | 40 |
41 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( | 41 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( |
42 base::WeakPtr<ServiceWorkerContextCore> context, | 42 base::WeakPtr<ServiceWorkerContextCore> context, |
43 ServiceWorkerRegistration* registration) | 43 ServiceWorkerRegistration* registration) |
44 : context_(context), | 44 : context_(context), |
45 job_type_(UPDATE_JOB), | 45 job_type_(UPDATE_JOB), |
46 pattern_(registration->pattern()), | 46 pattern_(registration->pattern()), |
47 script_url_(registration->script_url()), | 47 script_url_(registration->GetNewestVersion()->script_url()), |
48 phase_(INITIAL), | 48 phase_(INITIAL), |
49 is_promise_resolved_(false), | 49 is_promise_resolved_(false), |
50 promise_resolved_status_(SERVICE_WORKER_OK), | 50 promise_resolved_status_(SERVICE_WORKER_OK), |
51 weak_factory_(this) { | 51 weak_factory_(this) { |
52 internal_.registration = registration; | 52 internal_.registration = registration; |
53 } | 53 } |
54 | 54 |
55 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() { | 55 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() { |
56 DCHECK(!context_ || | 56 DCHECK(!context_ || |
57 phase_ == INITIAL || phase_ == COMPLETE || phase_ == ABORT) | 57 phase_ == INITIAL || phase_ == COMPLETE || phase_ == ABORT) |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 183 |
184 if (!existing_registration.get()) { | 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->GetNewestVersion()->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.get()); | 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(status, | 203 ResolvePromise(status, |
204 existing_registration.get(), | 204 existing_registration.get(), |
205 existing_registration->active_version()); | 205 existing_registration->active_version()); |
206 Complete(SERVICE_WORKER_OK); | 206 Complete(SERVICE_WORKER_OK); |
207 return; | 207 return; |
208 } | 208 } |
209 | 209 |
210 // "Set registration.[[ScriptURL]] to scriptURL." We accomplish this by | 210 // "Set registration.[[ScriptURL]] to scriptURL." We accomplish this by |
211 // deleting the existing registration and registering a new one. | 211 // deleting the existing registration and registering a new one. |
212 // TODO(michaeln): Deactivate the live existing_registration object and | 212 // TODO(michaeln): Deactivate the live existing_registration object and |
213 // eventually call storage->DeleteVersionResources() when it no longer has any | 213 // eventually call storage->DeleteVersionResources() when it no longer has any |
214 // controllees. | 214 // controllees. |
215 context_->storage()->DeleteRegistration( | 215 context_->storage()->DeleteRegistration( |
216 existing_registration->id(), | 216 existing_registration->id(), |
217 existing_registration->script_url().GetOrigin(), | 217 existing_registration->pattern().GetOrigin(), |
218 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, | 218 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, |
219 weak_factory_.GetWeakPtr())); | 219 weak_factory_.GetWeakPtr())); |
220 } | 220 } |
221 | 221 |
222 void ServiceWorkerRegisterJob::ContinueWithUpdate( | 222 void ServiceWorkerRegisterJob::ContinueWithUpdate( |
223 ServiceWorkerStatusCode status, | 223 ServiceWorkerStatusCode status, |
224 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { | 224 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { |
225 DCHECK_EQ(UPDATE_JOB, job_type_); | 225 DCHECK_EQ(UPDATE_JOB, job_type_); |
226 if (status != SERVICE_WORKER_OK) { | 226 if (status != SERVICE_WORKER_OK) { |
227 Complete(status); | 227 Complete(status); |
228 return; | 228 return; |
229 } | 229 } |
230 | 230 |
231 if (existing_registration.get() != registration()) { | 231 if (existing_registration.get() != registration()) { |
232 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); | 232 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); |
233 return; | 233 return; |
234 } | 234 } |
235 | 235 |
| 236 // A previous job may have unregistered or installed a new version to this |
| 237 // registration. |
| 238 if (registration()->is_uninstalling() || |
| 239 registration()->GetNewestVersion()->script_url() != script_url_) { |
| 240 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); |
| 241 return; |
| 242 } |
| 243 |
236 // TODO(michaeln): If the last update check was less than 24 hours | 244 // TODO(michaeln): If the last update check was less than 24 hours |
237 // ago, depending on the freshness of the cached worker script we | 245 // ago, depending on the freshness of the cached worker script we |
238 // may be able to complete the update job right here. | 246 // may be able to complete the update job right here. |
239 | 247 |
240 UpdateAndContinue(); | 248 UpdateAndContinue(); |
241 } | 249 } |
242 | 250 |
243 // Creates a new ServiceWorkerRegistration. | 251 // Creates a new ServiceWorkerRegistration. |
244 void ServiceWorkerRegisterJob::RegisterAndContinue( | 252 void ServiceWorkerRegisterJob::RegisterAndContinue( |
245 ServiceWorkerStatusCode status) { | 253 ServiceWorkerStatusCode status) { |
246 SetPhase(REGISTER); | 254 SetPhase(REGISTER); |
247 if (status != SERVICE_WORKER_OK) { | 255 if (status != SERVICE_WORKER_OK) { |
248 // Abort this registration job. | 256 // Abort this registration job. |
249 Complete(status); | 257 Complete(status); |
250 return; | 258 return; |
251 } | 259 } |
252 | 260 |
253 set_registration(new ServiceWorkerRegistration( | 261 set_registration(new ServiceWorkerRegistration( |
254 pattern_, script_url_, context_->storage()->NewRegistrationId(), | 262 pattern_, context_->storage()->NewRegistrationId(), context_)); |
255 context_)); | |
256 AssociateProviderHostsToRegistration(registration()); | 263 AssociateProviderHostsToRegistration(registration()); |
257 UpdateAndContinue(); | 264 UpdateAndContinue(); |
258 } | 265 } |
259 | 266 |
260 // This function corresponds to the spec's [[Update]] algorithm. | 267 // This function corresponds to the spec's [[Update]] algorithm. |
261 void ServiceWorkerRegisterJob::UpdateAndContinue() { | 268 void ServiceWorkerRegisterJob::UpdateAndContinue() { |
262 SetPhase(UPDATE); | 269 SetPhase(UPDATE); |
263 context_->storage()->NotifyInstallingRegistration(registration()); | 270 context_->storage()->NotifyInstallingRegistration(registration()); |
264 | 271 |
265 // TODO(falken): "If serviceWorkerRegistration.installingWorker is not null.." | 272 // TODO(falken): "If serviceWorkerRegistration.installingWorker is not null.." |
266 // then terminate the installing worker. It doesn't make sense to implement | 273 // then terminate the installing worker. It doesn't make sense to implement |
267 // yet since we always activate the worker if install completed, so there can | 274 // yet since we always activate the worker if install completed, so there can |
268 // be no installing worker at this point. | 275 // be no installing worker at this point. |
269 | 276 |
270 // "Let serviceWorker be a newly-created ServiceWorker object..." and start | 277 // "Let serviceWorker be a newly-created ServiceWorker object..." and start |
271 // the worker. | 278 // the worker. |
272 set_new_version(new ServiceWorkerVersion( | 279 set_new_version(new ServiceWorkerVersion(registration(), |
273 registration(), context_->storage()->NewVersionId(), context_)); | 280 script_url_, |
| 281 context_->storage()->NewVersionId(), |
| 282 context_)); |
274 | 283 |
275 bool pause_after_download = job_type_ == UPDATE_JOB; | 284 bool pause_after_download = job_type_ == UPDATE_JOB; |
276 if (pause_after_download) | 285 if (pause_after_download) |
277 new_version()->embedded_worker()->AddListener(this); | 286 new_version()->embedded_worker()->AddListener(this); |
278 new_version()->StartWorkerWithCandidateProcesses( | 287 new_version()->StartWorkerWithCandidateProcesses( |
279 pending_process_ids_, | 288 pending_process_ids_, |
280 pause_after_download, | 289 pause_after_download, |
281 base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished, | 290 base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished, |
282 weak_factory_.GetWeakPtr())); | 291 weak_factory_.GetWeakPtr())); |
283 } | 292 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 if (registration()) { | 389 if (registration()) { |
381 if (new_version()) { | 390 if (new_version()) { |
382 registration()->UnsetVersion(new_version()); | 391 registration()->UnsetVersion(new_version()); |
383 new_version()->Doom(); | 392 new_version()->Doom(); |
384 } | 393 } |
385 if (!registration()->waiting_version() && | 394 if (!registration()->waiting_version() && |
386 !registration()->active_version()) { | 395 !registration()->active_version()) { |
387 registration()->NotifyRegistrationFailed(); | 396 registration()->NotifyRegistrationFailed(); |
388 context_->storage()->DeleteRegistration( | 397 context_->storage()->DeleteRegistration( |
389 registration()->id(), | 398 registration()->id(), |
390 registration()->script_url().GetOrigin(), | 399 registration()->pattern().GetOrigin(), |
391 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 400 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
392 } | 401 } |
393 } | 402 } |
394 if (!is_promise_resolved_) | 403 if (!is_promise_resolved_) |
395 ResolvePromise(status, NULL, NULL); | 404 ResolvePromise(status, NULL, NULL); |
396 } | 405 } |
397 DCHECK(callbacks_.empty()); | 406 DCHECK(callbacks_.empty()); |
398 if (registration()) { | 407 if (registration()) { |
399 context_->storage()->NotifyDoneInstallingRegistration( | 408 context_->storage()->NotifyDoneInstallingRegistration( |
400 registration(), new_version(), status); | 409 registration(), new_version(), status); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 ServiceWorkerProviderHost* host = it->GetProviderHost(); | 487 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
479 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), | 488 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), |
480 host->document_url())) { | 489 host->document_url())) { |
481 if (host->CanAssociateRegistration(registration)) | 490 if (host->CanAssociateRegistration(registration)) |
482 host->AssociateRegistration(registration); | 491 host->AssociateRegistration(registration); |
483 } | 492 } |
484 } | 493 } |
485 } | 494 } |
486 | 495 |
487 } // namespace content | 496 } // namespace content |
OLD | NEW |