OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_unregister_job.h" | 5 #include "content/browser/service_worker/service_worker_unregister_job.h" |
6 | 6 |
| 7 #include "base/memory/weak_ptr.h" |
7 #include "content/browser/service_worker/service_worker_context_core.h" | 8 #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_job_coordinator.h" |
9 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
10 #include "content/browser/service_worker/service_worker_storage.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" |
11 | 14 |
12 namespace content { | 15 namespace content { |
13 | 16 |
14 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType; | 17 typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType; |
15 | 18 |
16 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob( | 19 ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob( |
17 base::WeakPtr<ServiceWorkerContextCore> context, | 20 base::WeakPtr<ServiceWorkerContextCore> context, |
18 const GURL& pattern) | 21 const GURL& pattern) |
19 : context_(context), | 22 : context_(context), |
20 pattern_(pattern), | 23 pattern_(pattern), |
21 weak_factory_(this) {} | 24 is_promise_resolved_(false), |
| 25 weak_factory_(this) { |
| 26 } |
22 | 27 |
23 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {} | 28 ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {} |
24 | 29 |
25 void ServiceWorkerUnregisterJob::AddCallback( | 30 void ServiceWorkerUnregisterJob::AddCallback( |
26 const UnregistrationCallback& callback) { | 31 const UnregistrationCallback& callback) { |
27 callbacks_.push_back(callback); | 32 callbacks_.push_back(callback); |
28 } | 33 } |
29 | 34 |
30 void ServiceWorkerUnregisterJob::Start() { | 35 void ServiceWorkerUnregisterJob::Start() { |
31 context_->storage()->FindRegistrationForPattern( | 36 context_->storage()->FindRegistrationForPattern( |
(...skipping 18 matching lines...) Expand all Loading... |
50 | 55 |
51 void ServiceWorkerUnregisterJob::OnRegistrationFound( | 56 void ServiceWorkerUnregisterJob::OnRegistrationFound( |
52 ServiceWorkerStatusCode status, | 57 ServiceWorkerStatusCode status, |
53 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 58 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
54 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 59 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
55 DCHECK(!registration); | 60 DCHECK(!registration); |
56 Complete(SERVICE_WORKER_OK); | 61 Complete(SERVICE_WORKER_OK); |
57 return; | 62 return; |
58 } | 63 } |
59 | 64 |
60 if (status != SERVICE_WORKER_OK) { | 65 if (status != SERVICE_WORKER_OK || registration->is_uninstalling()) { |
61 Complete(status); | 66 Complete(status); |
62 return; | 67 return; |
63 } | 68 } |
64 | 69 |
65 DCHECK(registration); | 70 // TODO: "7. If registration.updatePromise is not null..." |
66 DeleteRegistration(registration); | |
67 } | |
68 | 71 |
69 void ServiceWorkerUnregisterJob::DeleteRegistration( | 72 // "8. Resolve promise." |
70 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 73 ResolvePromise(SERVICE_WORKER_OK); |
71 // TODO: Also doom installing version. | |
72 if (registration->waiting_version()) | |
73 registration->waiting_version()->Doom(); | |
74 if (registration->active_version()) | |
75 registration->active_version()->Doom(); | |
76 | 74 |
77 context_->storage()->DeleteRegistration( | 75 registration->ClearWhenReady(); |
78 registration->id(), | 76 |
79 registration->script_url().GetOrigin(), | 77 Complete(SERVICE_WORKER_OK); |
80 base::Bind(&ServiceWorkerUnregisterJob::Complete, | |
81 weak_factory_.GetWeakPtr())); | |
82 } | 78 } |
83 | 79 |
84 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) { | 80 void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) { |
85 CompleteInternal(status); | 81 CompleteInternal(status); |
86 context_->job_coordinator()->FinishJob(pattern_, this); | 82 context_->job_coordinator()->FinishJob(pattern_, this); |
87 } | 83 } |
88 | 84 |
89 void ServiceWorkerUnregisterJob::CompleteInternal( | 85 void ServiceWorkerUnregisterJob::CompleteInternal( |
90 ServiceWorkerStatusCode status) { | 86 ServiceWorkerStatusCode status) { |
| 87 if (!is_promise_resolved_) |
| 88 ResolvePromise(status); |
| 89 } |
| 90 |
| 91 void ServiceWorkerUnregisterJob::ResolvePromise( |
| 92 ServiceWorkerStatusCode status) { |
| 93 DCHECK(!is_promise_resolved_); |
| 94 is_promise_resolved_ = true; |
91 for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin(); | 95 for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin(); |
92 it != callbacks_.end(); | 96 it != callbacks_.end(); |
93 ++it) { | 97 ++it) { |
94 it->Run(status); | 98 it->Run(status); |
95 } | 99 } |
96 } | 100 } |
97 | 101 |
98 } // namespace content | 102 } // namespace content |
OLD | NEW |