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.
5 #include "content/browser/service_worker/service_worker_unregister_job.h"
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"
14 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType
;
16 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob(
17 base::WeakPtr
<ServiceWorkerContextCore
> context
,
21 weak_factory_(this) {}
23 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {}
25 void ServiceWorkerUnregisterJob::AddCallback(
26 const UnregistrationCallback
& callback
) {
27 callbacks_
.push_back(callback
);
30 void ServiceWorkerUnregisterJob::Start() {
31 context_
->storage()->FindRegistrationForPattern(
33 base::Bind(&ServiceWorkerUnregisterJob::DeleteExistingRegistration
,
34 weak_factory_
.GetWeakPtr()));
37 bool ServiceWorkerUnregisterJob::Equals(ServiceWorkerRegisterJobBase
* job
) {
38 if (job
->GetType() != GetType())
40 return static_cast<ServiceWorkerUnregisterJob
*>(job
)->pattern_
== pattern_
;
43 RegistrationJobType
ServiceWorkerUnregisterJob::GetType() {
44 return ServiceWorkerRegisterJobBase::UNREGISTRATION
;
47 void ServiceWorkerUnregisterJob::DeleteExistingRegistration(
48 ServiceWorkerStatusCode status
,
49 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
50 if (status
== SERVICE_WORKER_OK
) {
52 // TODO(michaeln): Deactivate the live registration object and
53 // eventually call storage->DeleteVersionResources()
54 // when the version no longer has any controllees.
55 context_
->storage()->DeleteRegistration(
57 base::Bind(&ServiceWorkerUnregisterJob::Complete
,
58 weak_factory_
.GetWeakPtr()));
62 if (status
== SERVICE_WORKER_ERROR_NOT_FOUND
) {
63 DCHECK(!registration
);
64 Complete(SERVICE_WORKER_OK
);
71 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status
) {
72 for (std::vector
<UnregistrationCallback
>::iterator it
= callbacks_
.begin();
73 it
!= callbacks_
.end();
77 context_
->job_coordinator()->FinishJob(pattern_
, this);
80 } // namespace content