OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_register_job.h" | 5 #include "content/browser/service_worker/service_worker_register_job.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
10 #include "content/browser/service_worker/service_worker_job_coordinator.h" | 10 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 ServiceWorkerStatusCode status, | 64 ServiceWorkerStatusCode status, |
65 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 65 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
66 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 66 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
67 // A previous registration does not exist. | 67 // A previous registration does not exist. |
68 RegisterAndContinue(SERVICE_WORKER_OK); | 68 RegisterAndContinue(SERVICE_WORKER_OK); |
69 return; | 69 return; |
70 } | 70 } |
71 | 71 |
72 if (status != SERVICE_WORKER_OK) { | 72 if (status != SERVICE_WORKER_OK) { |
73 // Abort this registration job. | 73 // Abort this registration job. |
74 Complete(status); | 74 Complete(NULL, status); |
75 return; | 75 return; |
76 } | 76 } |
77 | 77 |
78 if (registration->script_url() != script_url_) { | 78 if (registration->script_url() != script_url_) { |
79 // Script URL mismatch: delete the existing registration and register a new | 79 // Script URL mismatch: delete the existing registration and register a new |
80 // one. | 80 // one. |
81 registration->Shutdown(); | 81 registration->Shutdown(); |
82 context_->storage()->DeleteRegistration( | 82 context_->storage()->DeleteRegistration( |
83 pattern_, | 83 pattern_, |
84 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, | 84 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, |
85 weak_factory_.GetWeakPtr())); | 85 weak_factory_.GetWeakPtr())); |
86 return; | 86 return; |
87 } | 87 } |
88 | 88 |
89 // Reuse the existing registration. | 89 // Reuse the existing registration. |
90 registration_ = registration; | 90 StartWorkerAndContinue(registration, SERVICE_WORKER_OK); |
91 StartWorkerAndContinue(SERVICE_WORKER_OK); | |
92 } | 91 } |
93 | 92 |
94 void ServiceWorkerRegisterJob::RegisterAndContinue( | 93 void ServiceWorkerRegisterJob::RegisterAndContinue( |
95 ServiceWorkerStatusCode status) { | 94 ServiceWorkerStatusCode status) { |
96 DCHECK(!registration_); | |
97 if (status != SERVICE_WORKER_OK) { | 95 if (status != SERVICE_WORKER_OK) { |
98 // Abort this registration job. | 96 // Abort this registration job. |
99 Complete(status); | 97 Complete(NULL, status); |
100 return; | 98 return; |
101 } | 99 } |
102 | 100 |
103 registration_ = new ServiceWorkerRegistration( | 101 scoped_refptr<ServiceWorkerRegistration> registration = |
104 pattern_, script_url_, context_->storage()->NewRegistrationId(), | 102 new ServiceWorkerRegistration(pattern_, |
105 context_); | 103 script_url_, |
| 104 context_->storage()->NewRegistrationId(), |
| 105 context_); |
106 context_->storage()->StoreRegistration( | 106 context_->storage()->StoreRegistration( |
107 registration_.get(), | 107 registration.get(), |
108 base::Bind(&ServiceWorkerRegisterJob::StartWorkerAndContinue, | 108 base::Bind(&ServiceWorkerRegisterJob::StartWorkerAndContinue, |
109 weak_factory_.GetWeakPtr())); | 109 weak_factory_.GetWeakPtr(), |
| 110 registration)); |
110 } | 111 } |
111 | 112 |
112 void ServiceWorkerRegisterJob::StartWorkerAndContinue( | 113 void ServiceWorkerRegisterJob::StartWorkerAndContinue( |
| 114 ServiceWorkerRegistration* registration, |
113 ServiceWorkerStatusCode status) { | 115 ServiceWorkerStatusCode status) { |
114 // TODO(falken): Handle the case where status is an error code. | 116 // TODO(falken): Handle the case where status is an error code. |
115 DCHECK(registration_); | 117 if (registration->active_version()) { |
116 if (registration_->active_version()) { | |
117 // We have an active version, so we can complete immediately, even | 118 // We have an active version, so we can complete immediately, even |
118 // if the service worker isn't running. | 119 // if the service worker isn't running. |
119 Complete(SERVICE_WORKER_OK); | 120 Complete(registration, SERVICE_WORKER_OK); |
120 return; | 121 return; |
121 } | 122 } |
122 | 123 |
123 pending_version_ = new ServiceWorkerVersion( | 124 pending_version_ = new ServiceWorkerVersion( |
124 registration_, context_->storage()->NewVersionId(), context_); | 125 registration, context_->storage()->NewVersionId(), context_); |
125 for (std::vector<int>::const_iterator it = pending_process_ids_.begin(); | 126 for (std::vector<int>::const_iterator it = pending_process_ids_.begin(); |
126 it != pending_process_ids_.end(); | 127 it != pending_process_ids_.end(); |
127 ++it) | 128 ++it) |
128 pending_version_->AddProcessToWorker(*it); | 129 pending_version_->AddProcessToWorker(*it); |
129 | 130 |
130 // The callback to watch "installation" actually fires as soon as | 131 // The callback to watch "installation" actually fires as soon as |
131 // the worker is up and running, just before the install event is | 132 // the worker is up and running, just before the install event is |
132 // dispatched. The job will continue to run even though the main | 133 // dispatched. The job will continue to run even though the main |
133 // callback has executed. | 134 // callback has executed. |
134 pending_version_->StartWorker(base::Bind(&ServiceWorkerRegisterJob::Complete, | 135 pending_version_->StartWorker(base::Bind(&ServiceWorkerRegisterJob::Complete, |
135 weak_factory_.GetWeakPtr())); | 136 weak_factory_.GetWeakPtr(), |
| 137 make_scoped_refptr(registration))); |
136 | 138 |
137 // TODO(falken): Don't set the active version until just before | 139 // TODO(falken): Don't set the active version until just before |
138 // the activate event is dispatched. | 140 // the activate event is dispatched. |
139 pending_version_->SetStatus(ServiceWorkerVersion::ACTIVE); | 141 pending_version_->SetStatus(ServiceWorkerVersion::ACTIVE); |
140 registration_->set_active_version(pending_version_); | 142 registration->set_active_version(pending_version_); |
141 } | 143 } |
142 | 144 |
143 void ServiceWorkerRegisterJob::Complete(ServiceWorkerStatusCode status) { | 145 void ServiceWorkerRegisterJob::Complete(ServiceWorkerRegistration* registration, |
| 146 ServiceWorkerStatusCode status) { |
144 for (std::vector<RegistrationCallback>::iterator it = callbacks_.begin(); | 147 for (std::vector<RegistrationCallback>::iterator it = callbacks_.begin(); |
145 it != callbacks_.end(); | 148 it != callbacks_.end(); |
146 ++it) { | 149 ++it) { |
147 it->Run(status, status == SERVICE_WORKER_OK ? registration_ : NULL); | 150 it->Run(status, status == SERVICE_WORKER_OK ? registration : NULL); |
148 } | 151 } |
149 context_->job_coordinator()->FinishJob(pattern_, this); | 152 context_->job_coordinator()->FinishJob(pattern_, this); |
150 } | 153 } |
151 | 154 |
152 } // namespace content | 155 } // namespace content |
OLD | NEW |