OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_unregister_job.h" | 5 #include "content/browser/service_worker/service_worker_unregister_job.h" |
6 | 6 |
| 7 #include "base/memory/weak_ptr.h" |
7 #include "content/browser/service_worker/service_worker_context_core.h" | 8 #include "content/browser/service_worker/service_worker_context_core.h" |
8 #include "content/browser/service_worker/service_worker_job_coordinator.h" | 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
9 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
10 #include "content/browser/service_worker/service_worker_storage.h" | 11 #include "content/browser/service_worker/service_worker_storage.h" |
| 12 #include "content/browser/service_worker/service_worker_utils.h" |
| 13 #include "content/browser/service_worker/service_worker_version.h" |
11 | 14 |
12 namespace content { | 15 namespace content { |
13 | 16 |
14 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType; | 17 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType; |
15 | 18 |
16 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob( | 19 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob( |
17 base::WeakPtr<ServiceWorkerContextCore> context, | 20 base::WeakPtr<ServiceWorkerContextCore> context, |
18 const GURL& pattern) | 21 const GURL& pattern) |
19 : context_(context), | 22 : context_(context), |
20 pattern_(pattern), | 23 pattern_(pattern), |
21 weak_factory_(this) {} | 24 is_promise_resolved_(false), |
| 25 weak_factory_(this) { |
| 26 } |
22 | 27 |
23 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {} | 28 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {} |
24 | 29 |
25 void ServiceWorkerUnregisterJob::AddCallback( | 30 void ServiceWorkerUnregisterJob::AddCallback( |
26 const UnregistrationCallback& callback) { | 31 const UnregistrationCallback& callback) { |
27 callbacks_.push_back(callback); | 32 callbacks_.push_back(callback); |
28 } | 33 } |
29 | 34 |
30 void ServiceWorkerUnregisterJob::Start() { | 35 void ServiceWorkerUnregisterJob::Start() { |
31 context_->storage()->FindRegistrationForPattern( | 36 context_->storage()->FindRegistrationForPattern( |
(...skipping 23 matching lines...) Expand all Loading... |
55 DCHECK(!registration); | 60 DCHECK(!registration); |
56 Complete(SERVICE_WORKER_OK); | 61 Complete(SERVICE_WORKER_OK); |
57 return; | 62 return; |
58 } | 63 } |
59 | 64 |
60 if (status != SERVICE_WORKER_OK) { | 65 if (status != SERVICE_WORKER_OK) { |
61 Complete(status); | 66 Complete(status); |
62 return; | 67 return; |
63 } | 68 } |
64 | 69 |
65 DCHECK(registration); | 70 if (registration->is_uninstalling()) { |
66 DeleteRegistration(registration); | 71 Complete(SERVICE_WORKER_OK); |
67 } | 72 return; |
| 73 } |
68 | 74 |
69 void ServiceWorkerUnregisterJob::DeleteRegistration( | 75 // "6. Set registration.uninstalling to true." |
70 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 76 registration->set_uninstalling(true); |
71 // TODO: Also doom installing version. | 77 |
72 if (registration->waiting_version()) | 78 // TODO: "7. If registration.updatePromise is not null..." |
| 79 |
| 80 // "8. If registration.installingWorker is not null, then: |
| 81 if (registration->installing_version()) { |
| 82 // 1. Terminate registration.installingWorker. |
| 83 // 2. Run the [[UpdateState]] algorithm passing |
| 84 // registration.installingWorker and 'redundant' as the arguments. |
| 85 registration->installing_version()->Doom(); |
| 86 // 3. Set registration.installingWorker to null. |
| 87 registration->UnsetVersion(registration->installing_version()); |
| 88 } |
| 89 |
| 90 // "9. If registration.waitingWorker is not null, then: |
| 91 if (registration->waiting_version()) { |
| 92 // "1. Run the [[UpdateState]] algorithm passing registration.waitingWorker |
| 93 // and 'redundant' as the arguments." |
73 registration->waiting_version()->Doom(); | 94 registration->waiting_version()->Doom(); |
74 if (registration->active_version()) | 95 // "2. Set registration.waitingWorker to null." |
75 registration->active_version()->Doom(); | 96 registration->UnsetVersion(registration->waiting_version()); |
| 97 } |
76 | 98 |
77 context_->storage()->DeleteRegistration( | 99 // "10. Resolve promise." |
78 registration->id(), | 100 ResolvePromise(SERVICE_WORKER_OK); |
79 registration->script_url().GetOrigin(), | 101 |
80 base::Bind(&ServiceWorkerUnregisterJob::Complete, | 102 registration->StartUninstall(); |
81 weak_factory_.GetWeakPtr())); | 103 Complete(SERVICE_WORKER_OK); |
82 } | 104 } |
83 | 105 |
84 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) { | 106 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) { |
85 CompleteInternal(status); | 107 CompleteInternal(status); |
86 context_->job_coordinator()->FinishJob(pattern_, this); | 108 context_->job_coordinator()->FinishJob(pattern_, this); |
87 } | 109 } |
88 | 110 |
89 void ServiceWorkerUnregisterJob::CompleteInternal( | 111 void ServiceWorkerUnregisterJob::CompleteInternal( |
90 ServiceWorkerStatusCode status) { | 112 ServiceWorkerStatusCode status) { |
| 113 if (!is_promise_resolved_) |
| 114 ResolvePromise(status); |
| 115 } |
| 116 |
| 117 void ServiceWorkerUnregisterJob::ResolvePromise( |
| 118 ServiceWorkerStatusCode status) { |
| 119 DCHECK(!is_promise_resolved_); |
| 120 is_promise_resolved_ = true; |
91 for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin(); | 121 for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin(); |
92 it != callbacks_.end(); | 122 it != callbacks_.end(); |
93 ++it) { | 123 ++it) { |
94 it->Run(status); | 124 it->Run(status); |
95 } | 125 } |
96 } | 126 } |
97 | 127 |
98 } // namespace content | 128 } // namespace content |
OLD | NEW |