Service Worker: in Unregister, wait until after the active worker no longer controls...
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_unregister_job.cc
blobaabad3aaf2f49e02422d448da024da20f1418f21
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"
15 namespace content {
17 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType;
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) {
28 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {}
30 void ServiceWorkerUnregisterJob::AddCallback(
31 const UnregistrationCallback& callback) {
32 callbacks_.push_back(callback);
35 void ServiceWorkerUnregisterJob::Start() {
36 context_->storage()->FindRegistrationForPattern(
37 pattern_,
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())
48 return false;
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);
62 return;
65 if (status != SERVICE_WORKER_OK || registration->is_uninstalling()) {
66 Complete(status);
67 return;
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();
97 ++it) {
98 it->Run(status);
102 } // namespace content