| 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 "content/browser/service_worker/service_worker_context_core.h" | 7 #include "content/browser/service_worker/service_worker_context_core.h" |
| 8 #include "content/browser/service_worker/service_worker_info.h" | 8 #include "content/browser/service_worker/service_worker_info.h" |
| 9 #include "content/browser/service_worker/service_worker_register_job.h" | 9 #include "content/browser/service_worker/service_worker_register_job.h" |
| 10 #include "content/browser/service_worker/service_worker_utils.h" | 10 #include "content/browser/service_worker/service_worker_utils.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 ServiceWorkerVersionInfo GetVersionInfo(ServiceWorkerVersion* version) { | 17 ServiceWorkerVersionInfo GetVersionInfo(ServiceWorkerVersion* version) { |
| 18 if (!version) | 18 if (!version) |
| 19 return ServiceWorkerVersionInfo(); | 19 return ServiceWorkerVersionInfo(); |
| 20 return version->GetInfo(); | 20 return version->GetInfo(); |
| 21 } | 21 } |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 ServiceWorkerRegistration::ServiceWorkerRegistration( | 25 ServiceWorkerRegistration::ServiceWorkerRegistration( |
| 26 const GURL& pattern, | 26 const GURL& pattern, |
| 27 const GURL& script_url, | |
| 28 int64 registration_id, | 27 int64 registration_id, |
| 29 base::WeakPtr<ServiceWorkerContextCore> context) | 28 base::WeakPtr<ServiceWorkerContextCore> context) |
| 30 : pattern_(pattern), | 29 : pattern_(pattern), |
| 31 script_url_(script_url), | |
| 32 registration_id_(registration_id), | 30 registration_id_(registration_id), |
| 33 is_deleted_(false), | 31 is_deleted_(false), |
| 34 is_uninstalling_(false), | 32 is_uninstalling_(false), |
| 35 should_activate_when_ready_(false), | 33 should_activate_when_ready_(false), |
| 36 context_(context) { | 34 context_(context) { |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 38 DCHECK(context_); | 36 DCHECK(context_); |
| 39 context_->AddLiveRegistration(this); | 37 context_->AddLiveRegistration(this); |
| 40 } | 38 } |
| 41 | 39 |
| 42 ServiceWorkerRegistration::~ServiceWorkerRegistration() { | 40 ServiceWorkerRegistration::~ServiceWorkerRegistration() { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 44 DCHECK(!listeners_.might_have_observers()); | 42 DCHECK(!listeners_.might_have_observers()); |
| 45 if (context_) | 43 if (context_) |
| 46 context_->RemoveLiveRegistration(registration_id_); | 44 context_->RemoveLiveRegistration(registration_id_); |
| 47 if (active_version()) | 45 if (active_version()) |
| 48 active_version()->RemoveListener(this); | 46 active_version()->RemoveListener(this); |
| 49 } | 47 } |
| 50 | 48 |
| 49 ServiceWorkerVersion* ServiceWorkerRegistration::GetNewestVersion() const { |
| 50 if (installing_version()) |
| 51 return installing_version(); |
| 52 if (waiting_version()) |
| 53 return waiting_version(); |
| 54 return active_version(); |
| 55 } |
| 56 |
| 51 void ServiceWorkerRegistration::AddListener(Listener* listener) { | 57 void ServiceWorkerRegistration::AddListener(Listener* listener) { |
| 52 listeners_.AddObserver(listener); | 58 listeners_.AddObserver(listener); |
| 53 } | 59 } |
| 54 | 60 |
| 55 void ServiceWorkerRegistration::RemoveListener(Listener* listener) { | 61 void ServiceWorkerRegistration::RemoveListener(Listener* listener) { |
| 56 listeners_.RemoveObserver(listener); | 62 listeners_.RemoveObserver(listener); |
| 57 } | 63 } |
| 58 | 64 |
| 59 void ServiceWorkerRegistration::NotifyRegistrationFailed() { | 65 void ServiceWorkerRegistration::NotifyRegistrationFailed() { |
| 60 FOR_EACH_OBSERVER(Listener, listeners_, OnRegistrationFailed(this)); | 66 FOR_EACH_OBSERVER(Listener, listeners_, OnRegistrationFailed(this)); |
| 61 } | 67 } |
| 62 | 68 |
| 63 ServiceWorkerRegistrationInfo ServiceWorkerRegistration::GetInfo() { | 69 ServiceWorkerRegistrationInfo ServiceWorkerRegistration::GetInfo() { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 65 return ServiceWorkerRegistrationInfo( | 71 return ServiceWorkerRegistrationInfo( |
| 66 script_url(), | |
| 67 pattern(), | 72 pattern(), |
| 68 registration_id_, | 73 registration_id_, |
| 69 GetVersionInfo(active_version_.get()), | 74 GetVersionInfo(active_version_.get()), |
| 70 GetVersionInfo(waiting_version_.get()), | 75 GetVersionInfo(waiting_version_.get()), |
| 71 GetVersionInfo(installing_version_.get())); | 76 GetVersionInfo(installing_version_.get())); |
| 72 } | 77 } |
| 73 | 78 |
| 74 void ServiceWorkerRegistration::SetActiveVersion( | 79 void ServiceWorkerRegistration::SetActiveVersion( |
| 75 ServiceWorkerVersion* version) { | 80 ServiceWorkerVersion* version) { |
| 76 should_activate_when_ready_ = false; | 81 should_activate_when_ready_ = false; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 153 |
| 149 void ServiceWorkerRegistration::ClearWhenReady() { | 154 void ServiceWorkerRegistration::ClearWhenReady() { |
| 150 DCHECK(context_); | 155 DCHECK(context_); |
| 151 if (is_uninstalling_) | 156 if (is_uninstalling_) |
| 152 return; | 157 return; |
| 153 is_uninstalling_ = true; | 158 is_uninstalling_ = true; |
| 154 | 159 |
| 155 context_->storage()->NotifyUninstallingRegistration(this); | 160 context_->storage()->NotifyUninstallingRegistration(this); |
| 156 context_->storage()->DeleteRegistration( | 161 context_->storage()->DeleteRegistration( |
| 157 id(), | 162 id(), |
| 158 script_url().GetOrigin(), | 163 pattern().GetOrigin(), |
| 159 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 164 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 160 | 165 |
| 161 if (!active_version() || !active_version()->HasControllee()) | 166 if (!active_version() || !active_version()->HasControllee()) |
| 162 Clear(); | 167 Clear(); |
| 163 } | 168 } |
| 164 | 169 |
| 165 void ServiceWorkerRegistration::AbortPendingClear() { | 170 void ServiceWorkerRegistration::AbortPendingClear() { |
| 166 DCHECK(context_); | 171 DCHECK(context_); |
| 167 if (!is_uninstalling()) | 172 if (!is_uninstalling()) |
| 168 return; | 173 return; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 return; | 245 return; |
| 241 // TODO(kinuko,falken): For some error cases (e.g. ServiceWorker is | 246 // TODO(kinuko,falken): For some error cases (e.g. ServiceWorker is |
| 242 // unexpectedly terminated) we may want to retry sending the event again. | 247 // unexpectedly terminated) we may want to retry sending the event again. |
| 243 if (status != SERVICE_WORKER_OK) { | 248 if (status != SERVICE_WORKER_OK) { |
| 244 // "11. If activateFailed is true, then:..." | 249 // "11. If activateFailed is true, then:..." |
| 245 UnsetVersion(activating_version); | 250 UnsetVersion(activating_version); |
| 246 activating_version->Doom(); | 251 activating_version->Doom(); |
| 247 if (!waiting_version()) { | 252 if (!waiting_version()) { |
| 248 // Delete the records from the db. | 253 // Delete the records from the db. |
| 249 context_->storage()->DeleteRegistration( | 254 context_->storage()->DeleteRegistration( |
| 250 id(), script_url().GetOrigin(), | 255 id(), pattern().GetOrigin(), |
| 251 base::Bind(&ServiceWorkerRegistration::OnDeleteFinished, this)); | 256 base::Bind(&ServiceWorkerRegistration::OnDeleteFinished, this)); |
| 252 // But not from memory if there is a version in the pipeline. | 257 // But not from memory if there is a version in the pipeline. |
| 253 if (installing_version()) | 258 if (installing_version()) |
| 254 is_deleted_ = false; | 259 is_deleted_ = false; |
| 255 } | 260 } |
| 256 return; | 261 return; |
| 257 } | 262 } |
| 258 | 263 |
| 259 // "12. Run the [[UpdateState]] algorithm passing registration.activeWorker | 264 // "12. Run the [[UpdateState]] algorithm passing registration.activeWorker |
| 260 // and "activated" as the arguments." | 265 // and "activated" as the arguments." |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 void ServiceWorkerRegistration::OnStoreFinished( | 307 void ServiceWorkerRegistration::OnStoreFinished( |
| 303 scoped_refptr<ServiceWorkerVersion> version, | 308 scoped_refptr<ServiceWorkerVersion> version, |
| 304 ServiceWorkerStatusCode status) { | 309 ServiceWorkerStatusCode status) { |
| 305 if (!context_) | 310 if (!context_) |
| 306 return; | 311 return; |
| 307 context_->storage()->NotifyDoneInstallingRegistration( | 312 context_->storage()->NotifyDoneInstallingRegistration( |
| 308 this, version.get(), status); | 313 this, version.get(), status); |
| 309 } | 314 } |
| 310 | 315 |
| 311 } // namespace content | 316 } // namespace content |
| OLD | NEW |