| 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 "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_job_coordinator.h" | 8 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
| 9 #include "content/browser/service_worker/service_worker_registration.h" | 9 #include "content/browser/service_worker/service_worker_registration.h" |
| 10 #include "content/browser/service_worker/service_worker_storage.h" | 10 #include "content/browser/service_worker/service_worker_storage.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType; | 14 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType; |
| 15 | 15 |
| 16 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob( | 16 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob( |
| 17 base::WeakPtr<ServiceWorkerContextCore> context, | 17 base::WeakPtr<ServiceWorkerContextCore> context, |
| 18 const GURL& pattern) | 18 const GURL& pattern) |
| 19 : context_(context), | 19 : context_(context), |
| 20 pattern_(pattern), | 20 pattern_(pattern), |
| 21 weak_factory_(this) {} | 21 is_promise_resolved_(false), |
| 22 weak_factory_(this) { |
| 23 } |
| 22 | 24 |
| 23 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {} | 25 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {} |
| 24 | 26 |
| 25 void ServiceWorkerUnregisterJob::AddCallback( | 27 void ServiceWorkerUnregisterJob::AddCallback( |
| 26 const UnregistrationCallback& callback) { | 28 const UnregistrationCallback& callback) { |
| 27 callbacks_.push_back(callback); | 29 callbacks_.push_back(callback); |
| 28 } | 30 } |
| 29 | 31 |
| 30 void ServiceWorkerUnregisterJob::Start() { | 32 void ServiceWorkerUnregisterJob::Start() { |
| 31 context_->storage()->FindRegistrationForPattern( | 33 context_->storage()->FindRegistrationForPattern( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 56 Complete(SERVICE_WORKER_OK); | 58 Complete(SERVICE_WORKER_OK); |
| 57 return; | 59 return; |
| 58 } | 60 } |
| 59 | 61 |
| 60 if (status != SERVICE_WORKER_OK) { | 62 if (status != SERVICE_WORKER_OK) { |
| 61 Complete(status); | 63 Complete(status); |
| 62 return; | 64 return; |
| 63 } | 65 } |
| 64 | 66 |
| 65 DCHECK(registration); | 67 DCHECK(registration); |
| 66 DeleteRegistration(registration); | 68 registration_ = registration; |
| 69 ResolvePromise(status); |
| 70 UninstallRegistration(); |
| 67 } | 71 } |
| 68 | 72 |
| 69 void ServiceWorkerUnregisterJob::DeleteRegistration( | 73 void ServiceWorkerUnregisterJob::UninstallRegistration() { |
| 70 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 74 registration_->mark_as_uninstalling(); |
| 75 |
| 71 // TODO(nhiroki): When we've implemented the installing version, terminate and | 76 // TODO(nhiroki): When we've implemented the installing version, terminate and |
| 72 // set it to redundant here as per spec. | 77 // set it to redundant here as per spec. |
| 78 context_->storage()->DeleteRegistration( |
| 79 registration_->id(), |
| 80 registration_->script_url().GetOrigin(), |
| 81 base::Bind(&ServiceWorkerUnregisterJob::DeleteWaitingVersion, |
| 82 weak_factory_.GetWeakPtr())); |
| 83 } |
| 73 | 84 |
| 74 if (registration->waiting_version()) { | 85 void ServiceWorkerUnregisterJob::DeleteWaitingVersion( |
| 75 registration->waiting_version()->SetStatus( | 86 ServiceWorkerStatusCode status) { |
| 76 ServiceWorkerVersion::REDUNDANT); | 87 // Nothing we can do about a bad status, keep trying to delete stuff. |
| 88 if (!registration_->waiting_version()) { |
| 89 DeleteActiveVersion(status); |
| 90 return; |
| 77 } | 91 } |
| 92 registration_->waiting_version()->SetStatus(ServiceWorkerVersion::REDUNDANT); |
| 93 context_->storage()->DeleteVersionResources( |
| 94 registration_->waiting_version()->version_id(), |
| 95 base::Bind(&ServiceWorkerUnregisterJob::DeleteActiveVersion, |
| 96 weak_factory_.GetWeakPtr())); |
| 97 } |
| 78 | 98 |
| 79 // TODO(michaeln): Eventually call storage->DeleteVersionResources() when the | 99 void ServiceWorkerUnregisterJob::DeleteActiveVersion( |
| 80 // version no longer has any controllees. | 100 ServiceWorkerStatusCode status) { |
| 81 context_->storage()->DeleteRegistration( | 101 ServiceWorkerVersion* active_version = registration_->active_version(); |
| 82 registration->id(), | 102 if (!active_version || active_version->HasControllee()) { |
| 83 registration->script_url().GetOrigin(), | 103 Complete(status); |
| 104 return; |
| 105 } |
| 106 context_->storage()->DeleteVersionResources( |
| 107 active_version->version_id(), |
| 84 base::Bind(&ServiceWorkerUnregisterJob::Complete, | 108 base::Bind(&ServiceWorkerUnregisterJob::Complete, |
| 85 weak_factory_.GetWeakPtr())); | 109 weak_factory_.GetWeakPtr())); |
| 86 } | 110 } |
| 87 | 111 |
| 88 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) { | 112 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) { |
| 89 CompleteInternal(status); | 113 CompleteInternal(status); |
| 90 context_->job_coordinator()->FinishJob(pattern_, this); | 114 context_->job_coordinator()->FinishJob(pattern_, this); |
| 91 } | 115 } |
| 92 | 116 |
| 93 void ServiceWorkerUnregisterJob::CompleteInternal( | 117 void ServiceWorkerUnregisterJob::CompleteInternal( |
| 94 ServiceWorkerStatusCode status) { | 118 ServiceWorkerStatusCode status) { |
| 119 if (!is_promise_resolved_) |
| 120 ResolvePromise(status); |
| 121 } |
| 122 |
| 123 void ServiceWorkerUnregisterJob::ResolvePromise( |
| 124 ServiceWorkerStatusCode status) { |
| 125 DCHECK(!is_promise_resolved_); |
| 95 for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin(); | 126 for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin(); |
| 96 it != callbacks_.end(); | 127 it != callbacks_.end(); |
| 97 ++it) { | 128 ++it) { |
| 98 it->Run(status); | 129 it->Run(status); |
| 99 } | 130 } |
| 131 callbacks_.clear(); |
| 100 } | 132 } |
| 101 | 133 |
| 102 } // namespace content | 134 } // namespace content |
| OLD | NEW |