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_registration.h" | 5 #include "content/browser/service_worker/service_worker_registration.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "content/browser/service_worker/embedded_worker_status.h" | 9 #include "content/browser/service_worker/embedded_worker_status.h" |
10 #include "content/browser/service_worker/service_worker_context_core.h" | 10 #include "content/browser/service_worker/service_worker_context_core.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 pattern(), registration_id_, | 100 pattern(), registration_id_, |
101 is_deleted_ ? ServiceWorkerRegistrationInfo::IS_DELETED | 101 is_deleted_ ? ServiceWorkerRegistrationInfo::IS_DELETED |
102 : ServiceWorkerRegistrationInfo::IS_NOT_DELETED, | 102 : ServiceWorkerRegistrationInfo::IS_NOT_DELETED, |
103 GetVersionInfo(active_version_.get()), | 103 GetVersionInfo(active_version_.get()), |
104 GetVersionInfo(waiting_version_.get()), | 104 GetVersionInfo(waiting_version_.get()), |
105 GetVersionInfo(installing_version_.get()), resources_total_size_bytes_); | 105 GetVersionInfo(installing_version_.get()), resources_total_size_bytes_); |
106 } | 106 } |
107 | 107 |
108 void ServiceWorkerRegistration::SetActiveVersion( | 108 void ServiceWorkerRegistration::SetActiveVersion( |
109 const scoped_refptr<ServiceWorkerVersion>& version) { | 109 const scoped_refptr<ServiceWorkerVersion>& version) { |
110 should_activate_when_ready_ = false; | |
111 if (active_version_ == version) | 110 if (active_version_ == version) |
nhiroki
2016/07/13 04:29:30
(This comment is not related to this CL. You don't
falken
2016/07/14 13:52:07
Acknowledged. (OTOH, it may be reasonable to write
| |
112 return; | 111 return; |
113 | 112 |
113 should_activate_when_ready_ = false; | |
114 | |
114 ChangedVersionAttributesMask mask; | 115 ChangedVersionAttributesMask mask; |
115 if (version) | 116 if (version) |
116 UnsetVersionInternal(version.get(), &mask); | 117 UnsetVersionInternal(version.get(), &mask); |
117 if (active_version_) | 118 if (active_version_) |
118 active_version_->RemoveListener(this); | 119 active_version_->RemoveListener(this); |
119 active_version_ = version; | 120 active_version_ = version; |
120 if (active_version_) | 121 if (active_version_) |
121 active_version_->AddListener(this); | 122 active_version_->AddListener(this); |
122 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); | 123 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); |
123 | 124 |
124 NotifyVersionAttributesChanged(mask); | 125 NotifyVersionAttributesChanged(mask); |
125 } | 126 } |
126 | 127 |
127 void ServiceWorkerRegistration::SetWaitingVersion( | 128 void ServiceWorkerRegistration::SetWaitingVersion( |
128 const scoped_refptr<ServiceWorkerVersion>& version) { | 129 const scoped_refptr<ServiceWorkerVersion>& version) { |
129 should_activate_when_ready_ = false; | |
130 if (waiting_version_ == version) | 130 if (waiting_version_ == version) |
nhiroki
2016/07/13 04:29:30
ditto.
| |
131 return; | 131 return; |
132 | 132 |
133 should_activate_when_ready_ = false; | |
134 | |
133 ChangedVersionAttributesMask mask; | 135 ChangedVersionAttributesMask mask; |
134 if (version) | 136 if (version) |
135 UnsetVersionInternal(version.get(), &mask); | 137 UnsetVersionInternal(version.get(), &mask); |
136 waiting_version_ = version; | 138 waiting_version_ = version; |
137 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); | 139 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); |
138 | 140 |
139 NotifyVersionAttributesChanged(mask); | 141 NotifyVersionAttributesChanged(mask); |
140 } | 142 } |
141 | 143 |
142 void ServiceWorkerRegistration::SetInstallingVersion( | 144 void ServiceWorkerRegistration::SetInstallingVersion( |
(...skipping 21 matching lines...) Expand all Loading... | |
164 | 166 |
165 void ServiceWorkerRegistration::UnsetVersionInternal( | 167 void ServiceWorkerRegistration::UnsetVersionInternal( |
166 ServiceWorkerVersion* version, | 168 ServiceWorkerVersion* version, |
167 ChangedVersionAttributesMask* mask) { | 169 ChangedVersionAttributesMask* mask) { |
168 DCHECK(version); | 170 DCHECK(version); |
169 if (installing_version_.get() == version) { | 171 if (installing_version_.get() == version) { |
170 installing_version_ = NULL; | 172 installing_version_ = NULL; |
171 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); | 173 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); |
172 } else if (waiting_version_.get() == version) { | 174 } else if (waiting_version_.get() == version) { |
173 waiting_version_ = NULL; | 175 waiting_version_ = NULL; |
176 should_activate_when_ready_ = false; | |
174 mask->add(ChangedVersionAttributesMask::WAITING_VERSION); | 177 mask->add(ChangedVersionAttributesMask::WAITING_VERSION); |
175 } else if (active_version_.get() == version) { | 178 } else if (active_version_.get() == version) { |
176 active_version_->RemoveListener(this); | 179 active_version_->RemoveListener(this); |
177 active_version_ = NULL; | 180 active_version_ = NULL; |
178 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION); | 181 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION); |
179 } | 182 } |
180 } | 183 } |
181 | 184 |
182 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { | 185 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { |
183 DCHECK(waiting_version()); | 186 DCHECK(waiting_version()); |
184 should_activate_when_ready_ = true; | 187 should_activate_when_ready_ = true; |
185 | 188 if (IsReadyToActivate()) |
186 if (!active_version() || !active_version()->HasControllee() || | |
187 waiting_version()->skip_waiting()) { | |
188 ActivateWaitingVersion(false); | 189 ActivateWaitingVersion(false); |
kinuko
2016/07/13 02:28:34
nit: add '/* delay */' comment
falken
2016/07/14 13:52:07
Done.
| |
189 } | |
190 } | 190 } |
191 | 191 |
192 void ServiceWorkerRegistration::ClaimClients() { | 192 void ServiceWorkerRegistration::ClaimClients() { |
193 DCHECK(context_); | 193 DCHECK(context_); |
194 DCHECK(active_version()); | 194 DCHECK(active_version()); |
195 | 195 |
196 for (std::unique_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = | 196 for (std::unique_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = |
197 context_->GetProviderHostIterator(); | 197 context_->GetProviderHostIterator(); |
198 !it->IsAtEnd(); it->Advance()) { | 198 !it->IsAtEnd(); it->Advance()) { |
199 ServiceWorkerProviderHost* host = it->GetProviderHost(); | 199 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 callback, | 246 callback, |
247 most_recent_version)); | 247 most_recent_version)); |
248 } | 248 } |
249 | 249 |
250 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { | 250 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { |
251 if (!context_) | 251 if (!context_) |
252 return; | 252 return; |
253 DCHECK_EQ(active_version(), version); | 253 DCHECK_EQ(active_version(), version); |
254 if (is_uninstalling_) | 254 if (is_uninstalling_) |
255 Clear(); | 255 Clear(); |
256 else if (should_activate_when_ready_) | 256 else if (IsReadyToActivate()) |
257 ActivateWaitingVersion(true); | 257 ActivateWaitingVersion(true /* delay */); |
258 } | |
259 | |
260 void ServiceWorkerRegistration::OnNoInflightRequests( | |
261 ServiceWorkerVersion* version) { | |
262 if (!context_) | |
263 return; | |
264 DCHECK_EQ(active_version(), version); | |
265 if (IsReadyToActivate()) | |
266 ActivateWaitingVersion(false /* delay */); | |
267 } | |
268 | |
269 bool ServiceWorkerRegistration::IsReadyToActivate() const { | |
270 if (!should_activate_when_ready_) | |
271 return false; | |
272 | |
273 DCHECK(waiting_version()); | |
274 const ServiceWorkerVersion* active = active_version(); | |
275 if (!active) | |
276 return true; | |
277 if (!active->HasInflightRequests() && | |
278 (!active->HasControllee() || waiting_version()->skip_waiting())) { | |
279 return true; | |
280 } | |
281 return false; | |
258 } | 282 } |
259 | 283 |
260 void ServiceWorkerRegistration::ActivateWaitingVersion(bool delay) { | 284 void ServiceWorkerRegistration::ActivateWaitingVersion(bool delay) { |
261 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 285 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
262 DCHECK(context_); | 286 DCHECK(context_); |
263 DCHECK(waiting_version()); | 287 DCHECK(IsReadyToActivate()); |
264 DCHECK(should_activate_when_ready_); | |
265 should_activate_when_ready_ = false; | 288 should_activate_when_ready_ = false; |
266 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version(); | 289 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version(); |
267 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version(); | 290 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version(); |
268 | 291 |
269 if (activating_version->is_redundant()) | 292 if (activating_version->is_redundant()) |
270 return; // Activation is no longer relevant. | 293 return; // Activation is no longer relevant. |
271 | 294 |
272 // "5. If exitingWorker is not null, | 295 // "5. If exitingWorker is not null, |
273 if (exiting_version.get()) { | 296 if (exiting_version.get()) { |
274 // TODO(michaeln): should wait for events to be complete | 297 // TODO(falken): Update the quoted spec comments once |
298 // https://github.com/slightlyoff/ServiceWorker/issues/916 is codified in | |
299 // the spec. | |
275 // "1. Wait for exitingWorker to finish handling any in-progress requests." | 300 // "1. Wait for exitingWorker to finish handling any in-progress requests." |
301 // This is already handled by IsReadyToActivate(). | |
276 // "2. Terminate exitingWorker." | 302 // "2. Terminate exitingWorker." |
277 exiting_version->StopWorker( | 303 exiting_version->StopWorker( |
278 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 304 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
279 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and | 305 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and |
280 // "redundant" as the arguments." | 306 // "redundant" as the arguments." |
281 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); | 307 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); |
282 } | 308 } |
283 | 309 |
284 // "6. Set serviceWorkerRegistration.activeWorker to activatingWorker." | 310 // "6. Set serviceWorkerRegistration.activeWorker to activatingWorker." |
285 // "7. Set serviceWorkerRegistration.waitingWorker to null." | 311 // "7. Set serviceWorkerRegistration.waitingWorker to null." |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
474 if (!context_) { | 500 if (!context_) { |
475 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 501 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
476 return; | 502 return; |
477 } | 503 } |
478 context_->storage()->NotifyDoneInstallingRegistration( | 504 context_->storage()->NotifyDoneInstallingRegistration( |
479 this, version.get(), status); | 505 this, version.get(), status); |
480 callback.Run(status); | 506 callback.Run(status); |
481 } | 507 } |
482 | 508 |
483 } // namespace content | 509 } // namespace content |
OLD | NEW |