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_version.h"
13 #include "content/common/service_worker/service_worker_utils.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(kInvalidServiceWorkerRegistrationId
,
44 SERVICE_WORKER_ERROR_ABORT
);
47 bool ServiceWorkerUnregisterJob::Equals(
48 ServiceWorkerRegisterJobBase
* job
) const {
49 if (job
->GetType() != GetType())
51 return static_cast<ServiceWorkerUnregisterJob
*>(job
)->pattern_
== pattern_
;
54 RegistrationJobType
ServiceWorkerUnregisterJob::GetType() const {
55 return UNREGISTRATION_JOB
;
58 void ServiceWorkerUnregisterJob::OnRegistrationFound(
59 ServiceWorkerStatusCode status
,
60 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
61 if (status
== SERVICE_WORKER_ERROR_NOT_FOUND
) {
62 DCHECK(!registration
.get());
63 Complete(kInvalidServiceWorkerRegistrationId
,
64 SERVICE_WORKER_ERROR_NOT_FOUND
);
68 if (status
!= SERVICE_WORKER_OK
|| registration
->is_uninstalling()) {
69 Complete(kInvalidServiceWorkerRegistrationId
, status
);
73 // TODO: "7. If registration.updatePromise is not null..."
75 // "8. Resolve promise."
76 ResolvePromise(registration
->id(), SERVICE_WORKER_OK
);
78 registration
->ClearWhenReady();
80 Complete(registration
->id(), SERVICE_WORKER_OK
);
83 void ServiceWorkerUnregisterJob::Complete(int64 registration_id
,
84 ServiceWorkerStatusCode status
) {
85 CompleteInternal(registration_id
, status
);
86 context_
->job_coordinator()->FinishJob(pattern_
, this);
89 void ServiceWorkerUnregisterJob::CompleteInternal(
90 int64 registration_id
,
91 ServiceWorkerStatusCode status
) {
92 if (!is_promise_resolved_
)
93 ResolvePromise(registration_id
, status
);
96 void ServiceWorkerUnregisterJob::ResolvePromise(
97 int64 registration_id
,
98 ServiceWorkerStatusCode status
) {
99 DCHECK(!is_promise_resolved_
);
100 is_promise_resolved_
= true;
101 for (std::vector
<UnregistrationCallback
>::iterator it
= callbacks_
.begin();
102 it
!= callbacks_
.end();
104 it
->Run(registration_id
, status
);
108 } // namespace content