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 "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"
17 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType
;
19 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob(
20 base::WeakPtr
<ServiceWorkerContextCore
> context
,
24 is_promise_resolved_(false),
28 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {}
30 void ServiceWorkerUnregisterJob::AddCallback(
31 const UnregistrationCallback
& callback
) {
32 callbacks_
.push_back(callback
);
35 void ServiceWorkerUnregisterJob::Start() {
36 context_
->storage()->FindRegistrationForPattern(
38 base::Bind(&ServiceWorkerUnregisterJob::OnRegistrationFound
,
39 weak_factory_
.GetWeakPtr()));
42 void ServiceWorkerUnregisterJob::Abort() {
43 CompleteInternal(SERVICE_WORKER_ERROR_ABORT
);
46 bool ServiceWorkerUnregisterJob::Equals(ServiceWorkerRegisterJobBase
* job
) {
47 if (job
->GetType() != GetType())
49 return static_cast<ServiceWorkerUnregisterJob
*>(job
)->pattern_
== pattern_
;
52 RegistrationJobType
ServiceWorkerUnregisterJob::GetType() {
53 return UNREGISTRATION_JOB
;
56 void ServiceWorkerUnregisterJob::OnRegistrationFound(
57 ServiceWorkerStatusCode status
,
58 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
59 if (status
== SERVICE_WORKER_ERROR_NOT_FOUND
) {
60 DCHECK(!registration
);
61 Complete(SERVICE_WORKER_OK
);
65 if (status
!= SERVICE_WORKER_OK
|| registration
->is_uninstalling()) {
70 // TODO: "7. If registration.updatePromise is not null..."
72 // "8. Resolve promise."
73 ResolvePromise(SERVICE_WORKER_OK
);
75 registration
->ClearWhenReady();
77 Complete(SERVICE_WORKER_OK
);
80 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status
) {
81 CompleteInternal(status
);
82 context_
->job_coordinator()->FinishJob(pattern_
, this);
85 void ServiceWorkerUnregisterJob::CompleteInternal(
86 ServiceWorkerStatusCode status
) {
87 if (!is_promise_resolved_
)
88 ResolvePromise(status
);
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();
102 } // namespace content