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 3 // found in the LICENSE file. 4 5 #include "content/browser/service_worker/service_worker_unregister_job.h" 6 7 #include "base/memory/weak_ptr.h" 8 #include "content/browser/service_worker/service_worker_context_core.h" 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" 10 #include "content/browser/service_worker/service_worker_registration.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" 14 15 namespace content { 16 17 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType; 18 19 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob( 20 base::WeakPtr<ServiceWorkerContextCore> context, 21 const GURL& pattern) 22 : context_(context), 23 pattern_(pattern), 24 is_promise_resolved_(false), 25 weak_factory_(this) { 26 } 27 28 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {} 29 30 void ServiceWorkerUnregisterJob::AddCallback( 31 const UnregistrationCallback& callback) { 32 callbacks_.push_back(callback); 33 } 34 35 void ServiceWorkerUnregisterJob::Start() { 36 context_->storage()->FindRegistrationForPattern( 37 pattern_, 38 base::Bind(&ServiceWorkerUnregisterJob::OnRegistrationFound, 39 weak_factory_.GetWeakPtr())); 40 } 41 42 void ServiceWorkerUnregisterJob::Abort() { 43 CompleteInternal(SERVICE_WORKER_ERROR_ABORT); 44 } 45 46 bool ServiceWorkerUnregisterJob::Equals(ServiceWorkerRegisterJobBase* job) { 47 if (job->GetType() != GetType()) 48 return false; 49 return static_cast<ServiceWorkerUnregisterJob*>(job)->pattern_ == pattern_; 50 } 51 52 RegistrationJobType ServiceWorkerUnregisterJob::GetType() { 53 return UNREGISTRATION_JOB; 54 } 55 56 void ServiceWorkerUnregisterJob::OnRegistrationFound( 57 ServiceWorkerStatusCode status, 58 const scoped_refptr<ServiceWorkerRegistration>& registration) { 59 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { 60 DCHECK(!registration.get()); 61 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); 62 return; 63 } 64 65 if (status != SERVICE_WORKER_OK || registration->is_uninstalling()) { 66 Complete(status); 67 return; 68 } 69 70 // TODO: "7. If registration.updatePromise is not null..." 71 72 // "8. Resolve promise." 73 ResolvePromise(SERVICE_WORKER_OK); 74 75 registration->ClearWhenReady(); 76 77 Complete(SERVICE_WORKER_OK); 78 } 79 80 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) { 81 CompleteInternal(status); 82 context_->job_coordinator()->FinishJob(pattern_, this); 83 } 84 85 void ServiceWorkerUnregisterJob::CompleteInternal( 86 ServiceWorkerStatusCode status) { 87 if (!is_promise_resolved_) 88 ResolvePromise(status); 89 } 90 91 void ServiceWorkerUnregisterJob::ResolvePromise( 92 ServiceWorkerStatusCode status) { 93 DCHECK(!is_promise_resolved_); 94 is_promise_resolved_ = true; 95 for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin(); 96 it != callbacks_.end(); 97 ++it) { 98 it->Run(status); 99 } 100 } 101 102 } // namespace content 103