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 "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_registration.h" 10 #include "content/browser/service_worker/service_worker_storage.h" 11 12 namespace content { 13 14 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType; 15 16 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob( 17 base::WeakPtr<ServiceWorkerContextCore> context, 18 const GURL& pattern) 19 : context_(context), 20 pattern_(pattern), 21 weak_factory_(this) {} 22 23 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {} 24 25 void ServiceWorkerUnregisterJob::AddCallback( 26 const UnregistrationCallback& callback) { 27 callbacks_.push_back(callback); 28 } 29 30 void ServiceWorkerUnregisterJob::Start() { 31 context_->storage()->FindRegistrationForPattern( 32 pattern_, 33 base::Bind(&ServiceWorkerUnregisterJob::DeleteExistingRegistration, 34 weak_factory_.GetWeakPtr())); 35 } 36 37 void ServiceWorkerUnregisterJob::Abort() { 38 CompleteInternal(SERVICE_WORKER_ERROR_ABORT); 39 } 40 41 bool ServiceWorkerUnregisterJob::Equals(ServiceWorkerRegisterJobBase* job) { 42 if (job->GetType() != GetType()) 43 return false; 44 return static_cast<ServiceWorkerUnregisterJob*>(job)->pattern_ == pattern_; 45 } 46 47 RegistrationJobType ServiceWorkerUnregisterJob::GetType() { 48 return ServiceWorkerRegisterJobBase::UNREGISTRATION; 49 } 50 51 void ServiceWorkerUnregisterJob::DeleteExistingRegistration( 52 ServiceWorkerStatusCode status, 53 const scoped_refptr<ServiceWorkerRegistration>& registration) { 54 if (status == SERVICE_WORKER_OK) { 55 DCHECK(registration); 56 // TODO(michaeln): Deactivate the live registration object and 57 // eventually call storage->DeleteVersionResources() 58 // when the version no longer has any controllees. 59 context_->storage()->DeleteRegistration( 60 registration->id(), 61 registration->script_url().GetOrigin(), 62 base::Bind(&ServiceWorkerUnregisterJob::Complete, 63 weak_factory_.GetWeakPtr())); 64 return; 65 } 66 67 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { 68 DCHECK(!registration); 69 Complete(SERVICE_WORKER_OK); 70 return; 71 } 72 73 Complete(status); 74 } 75 76 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) { 77 CompleteInternal(status); 78 context_->job_coordinator()->FinishJob(pattern_, this); 79 } 80 81 void ServiceWorkerUnregisterJob::CompleteInternal( 82 ServiceWorkerStatusCode status) { 83 for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin(); 84 it != callbacks_.end(); 85 ++it) { 86 it->Run(status); 87 } 88 } 89 90 } // namespace content 91