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_provider_host.h" | 5 #include "content/browser/service_worker/service_worker_provider_host.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "content/browser/message_port_message_filter.h" | 8 #include "content/browser/message_port_message_filter.h" |
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_context_request_handler. h" | 10 #include "content/browser/service_worker/service_worker_context_request_handler. h" |
(...skipping 30 matching lines...) Expand all Loading... | |
41 waiting_version_->RemovePotentialControllee(this); | 41 waiting_version_->RemovePotentialControllee(this); |
42 if (installing_version_) | 42 if (installing_version_) |
43 installing_version_->RemovePotentialControllee(this); | 43 installing_version_->RemovePotentialControllee(this); |
44 if (associated_registration_) | 44 if (associated_registration_) |
45 associated_registration_->RemoveListener(this); | 45 associated_registration_->RemoveListener(this); |
46 } | 46 } |
47 | 47 |
48 void ServiceWorkerProviderHost::OnVersionAttributesChanged( | 48 void ServiceWorkerProviderHost::OnVersionAttributesChanged( |
49 ServiceWorkerRegistration* registration, | 49 ServiceWorkerRegistration* registration, |
50 ChangedVersionAttributesMask changed_mask, | 50 ChangedVersionAttributesMask changed_mask, |
51 const ServiceWorkerRegistrationInfo& info) { | 51 const ServiceWorkerRegistrationInfo& info) { |
michaeln
2014/08/11 22:15:41
maybe DCHECK(registration == accociated_registrati
nhiroki
2014/08/12 01:45:56
Done.
| |
52 if (changed_mask.installing_changed()) | 52 SetVersionAttributes(registration->installing_version(), |
53 SetInstallingVersion(registration->installing_version()); | 53 registration->waiting_version(), |
54 if (changed_mask.waiting_changed()) | 54 registration->active_version()); |
55 SetWaitingVersion(registration->waiting_version()); | |
56 if (changed_mask.active_changed()) | |
57 SetActiveVersion(registration->active_version()); | |
58 } | 55 } |
59 | 56 |
60 void ServiceWorkerProviderHost::OnRegistrationFailed( | 57 void ServiceWorkerProviderHost::OnRegistrationFailed( |
61 ServiceWorkerRegistration* registration) { | 58 ServiceWorkerRegistration* registration) { |
62 DCHECK(associated_registration_); | 59 DCHECK(associated_registration_); |
63 DCHECK_EQ(registration->id(), associated_registration_->id()); | 60 DCHECK_EQ(registration->id(), associated_registration_->id()); |
64 UnassociateRegistration(); | 61 UnassociateRegistration(); |
65 } | 62 } |
66 | 63 |
67 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { | 64 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { |
68 DCHECK(!url.has_ref()); | 65 DCHECK(!url.has_ref()); |
69 document_url_ = url; | 66 document_url_ = url; |
70 } | 67 } |
71 | 68 |
72 void ServiceWorkerProviderHost::SetControllerVersion( | 69 void ServiceWorkerProviderHost::SetVersionAttributes( |
70 ServiceWorkerVersion* installing_version, | |
71 ServiceWorkerVersion* waiting_version, | |
72 ServiceWorkerVersion* active_version) { | |
73 ChangedVersionAttributesMask mask; | |
74 | |
75 ChangedVersionAttributesMask::AttributeType type = | |
76 ChangedVersionAttributesMask::INSTALLING_VERSION; | |
77 if (CanSetVersionAttribute(type, installing_version)) { | |
michaeln
2014/08/11 22:15:41
could this be replace with if (installing_version
nhiroki
2014/08/12 01:45:56
Done.
| |
78 SetVersionAttributesInternal(type, installing_version); | |
79 mask.add(type); | |
80 } | |
81 | |
82 type = ChangedVersionAttributesMask::WAITING_VERSION; | |
83 if (CanSetVersionAttribute(type, waiting_version)) { | |
84 SetVersionAttributesInternal(type, waiting_version); | |
85 mask.add(type); | |
86 } | |
87 | |
88 type = ChangedVersionAttributesMask::ACTIVE_VERSION; | |
89 if (CanSetVersionAttribute(type, active_version)) { | |
90 SetVersionAttributesInternal(type, active_version); | |
91 mask.add(type); | |
92 } | |
93 | |
94 if (!dispatcher_host_) | |
95 return; // Could be NULL in some tests. | |
96 if (!mask.changed()) | |
97 return; | |
98 | |
99 ServiceWorkerVersionAttributes attributes; | |
100 if (mask.installing_changed()) | |
101 attributes.installing = CreateHandleAndPass(installing_version); | |
102 if (mask.waiting_changed()) | |
103 attributes.waiting = CreateHandleAndPass(waiting_version); | |
104 if (mask.active_changed()) | |
105 attributes.active = CreateHandleAndPass(active_version); | |
106 | |
107 dispatcher_host_->Send(new ServiceWorkerMsg_SetVersionAttributes( | |
108 kDocumentMainThreadId, provider_id(), mask.changed(), attributes)); | |
109 } | |
110 | |
111 void ServiceWorkerProviderHost::SetVersionAttributesInternal( | |
112 ChangedVersionAttributesMask::AttributeType type, | |
73 ServiceWorkerVersion* version) { | 113 ServiceWorkerVersion* version) { |
michaeln
2014/08/11 22:15:41
if this method took a ptr to the version and a ptr
nhiroki
2014/08/12 01:45:56
Done.
| |
74 DCHECK(CanAssociateVersion(version)); | 114 DCHECK(CanSetVersionAttribute(type, version)); |
75 if (version == controlling_version_) | 115 scoped_refptr<ServiceWorkerVersion> previous_version; |
116 | |
117 switch (type) { | |
118 case ChangedVersionAttributesMask::INSTALLING_VERSION: | |
119 previous_version = installing_version_; | |
120 installing_version_ = version; | |
121 break; | |
122 case ChangedVersionAttributesMask::WAITING_VERSION: | |
123 previous_version = waiting_version_; | |
124 waiting_version_ = version; | |
125 break; | |
126 case ChangedVersionAttributesMask::ACTIVE_VERSION: | |
127 previous_version = active_version_; | |
128 active_version_ = version; | |
129 break; | |
130 default: | |
131 NOTREACHED() << "Unsupported attribute type: " << type; | |
132 break; | |
133 } | |
134 | |
135 if (version) | |
136 version->AddPotentialControllee(this); | |
137 if (previous_version) | |
138 previous_version->RemovePotentialControllee(this); | |
139 } | |
140 | |
141 void ServiceWorkerProviderHost::SetControllerVersionAttribute( | |
142 ServiceWorkerVersion* version) { | |
143 if (!CanSetVersionAttribute(ChangedVersionAttributesMask::CONTROLLING_VERSION, | |
michaeln
2014/08/11 22:15:41
can this be simplified to if (version == controlli
nhiroki
2014/08/12 01:45:56
Done.
| |
144 version)) { | |
76 return; | 145 return; |
146 } | |
147 | |
77 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_; | 148 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_; |
78 controlling_version_ = version; | 149 controlling_version_ = version; |
79 if (version) | 150 if (version) |
80 version->AddControllee(this); | 151 version->AddControllee(this); |
81 if (previous_version) | 152 if (previous_version) |
82 previous_version->RemoveControllee(this); | 153 previous_version->RemoveControllee(this); |
83 | 154 |
84 if (!dispatcher_host_) | 155 if (!dispatcher_host_) |
85 return; // Could be NULL in some tests. | 156 return; // Could be NULL in some tests. |
86 | 157 |
87 dispatcher_host_->Send(new ServiceWorkerMsg_SetControllerServiceWorker( | 158 dispatcher_host_->Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
88 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); | 159 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); |
89 } | 160 } |
90 | 161 |
91 void ServiceWorkerProviderHost::SetActiveVersion( | 162 void ServiceWorkerProviderHost::ClearVersionAttributes() { |
92 ServiceWorkerVersion* version) { | 163 SetVersionAttributes(NULL, NULL, NULL); |
93 DCHECK(CanAssociateVersion(version)); | 164 SetControllerVersionAttribute(NULL); |
94 if (version == active_version_) | |
95 return; | |
96 scoped_refptr<ServiceWorkerVersion> previous_version = active_version_; | |
97 active_version_ = version; | |
98 if (version) | |
99 version->AddPotentialControllee(this); | |
100 if (previous_version) | |
101 previous_version->RemovePotentialControllee(this); | |
102 | |
103 if (!dispatcher_host_) | |
104 return; // Could be NULL in some tests. | |
105 | |
106 dispatcher_host_->Send(new ServiceWorkerMsg_SetActiveServiceWorker( | |
107 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); | |
108 } | |
109 | |
110 void ServiceWorkerProviderHost::SetWaitingVersion( | |
111 ServiceWorkerVersion* version) { | |
112 DCHECK(CanAssociateVersion(version)); | |
113 if (version == waiting_version_) | |
114 return; | |
115 scoped_refptr<ServiceWorkerVersion> previous_version = waiting_version_; | |
116 waiting_version_ = version; | |
117 if (version) | |
118 version->AddPotentialControllee(this); | |
119 if (previous_version) | |
120 previous_version->RemovePotentialControllee(this); | |
121 | |
122 if (!dispatcher_host_) | |
123 return; // Could be NULL in some tests. | |
124 | |
125 dispatcher_host_->Send(new ServiceWorkerMsg_SetWaitingServiceWorker( | |
126 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); | |
127 } | |
128 | |
129 void ServiceWorkerProviderHost::SetInstallingVersion( | |
130 ServiceWorkerVersion* version) { | |
131 DCHECK(CanAssociateVersion(version)); | |
132 if (version == installing_version_) | |
133 return; | |
134 scoped_refptr<ServiceWorkerVersion> previous_version = installing_version_; | |
135 installing_version_ = version; | |
136 if (version) | |
137 version->AddPotentialControllee(this); | |
138 if (previous_version) | |
139 previous_version->RemovePotentialControllee(this); | |
140 | |
141 if (!dispatcher_host_) | |
142 return; // Could be NULL in some tests. | |
143 | |
144 dispatcher_host_->Send(new ServiceWorkerMsg_SetInstallingServiceWorker( | |
145 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); | |
146 } | |
147 | |
148 void ServiceWorkerProviderHost::UnsetVersion(ServiceWorkerVersion* version) { | |
149 if (!version) | |
150 return; | |
151 if (installing_version_ == version) | |
152 SetInstallingVersion(NULL); | |
153 else if (waiting_version_ == version) | |
154 SetWaitingVersion(NULL); | |
155 else if (active_version_ == version) | |
156 SetActiveVersion(NULL); | |
157 else if (controlling_version_ == version) | |
158 SetControllerVersion(NULL); | |
159 } | 165 } |
160 | 166 |
161 bool ServiceWorkerProviderHost::SetHostedVersionId(int64 version_id) { | 167 bool ServiceWorkerProviderHost::SetHostedVersionId(int64 version_id) { |
162 if (!context_) | 168 if (!context_) |
163 return true; // System is shutting down. | 169 return true; // System is shutting down. |
164 if (active_version_) | 170 if (active_version_) |
165 return false; // Unexpected bad message. | 171 return false; // Unexpected bad message. |
166 | 172 |
167 ServiceWorkerVersion* live_version = context_->GetLiveVersion(version_id); | 173 ServiceWorkerVersion* live_version = context_->GetLiveVersion(version_id); |
168 if (!live_version) | 174 if (!live_version) |
169 return true; // Was deleted before it got started. | 175 return true; // Was deleted before it got started. |
170 | 176 |
171 ServiceWorkerVersionInfo info = live_version->GetInfo(); | 177 ServiceWorkerVersionInfo info = live_version->GetInfo(); |
172 if (info.running_status != ServiceWorkerVersion::STARTING || | 178 if (info.running_status != ServiceWorkerVersion::STARTING || |
173 info.process_id != process_id_) { | 179 info.process_id != process_id_) { |
174 // If we aren't trying to start this version in our process | 180 // If we aren't trying to start this version in our process |
175 // something is amiss. | 181 // something is amiss. |
176 return false; | 182 return false; |
177 } | 183 } |
178 | 184 |
179 running_hosted_version_ = live_version; | 185 running_hosted_version_ = live_version; |
180 return true; | 186 return true; |
181 } | 187 } |
182 | 188 |
183 void ServiceWorkerProviderHost::AssociateRegistration( | 189 void ServiceWorkerProviderHost::AssociateRegistration( |
184 ServiceWorkerRegistration* registration) { | 190 ServiceWorkerRegistration* registration) { |
185 DCHECK(CanAssociateRegistration(registration)); | 191 DCHECK(CanAssociateRegistration(registration)); |
186 associated_registration_ = registration; | 192 associated_registration_ = registration; |
187 registration->AddListener(this); | 193 registration->AddListener(this); |
188 | 194 SetVersionAttributes(registration->installing_version(), |
189 SetActiveVersion(registration->active_version()); | 195 registration->waiting_version(), |
190 SetInstallingVersion(registration->installing_version()); | 196 registration->active_version()); |
191 SetWaitingVersion(registration->waiting_version()); | 197 SetControllerVersionAttribute(registration->active_version()); |
192 } | 198 } |
193 | 199 |
194 void ServiceWorkerProviderHost::UnassociateRegistration() { | 200 void ServiceWorkerProviderHost::UnassociateRegistration() { |
195 if (!associated_registration_) | 201 if (!associated_registration_) |
196 return; | 202 return; |
197 associated_registration_->RemoveListener(this); | 203 associated_registration_->RemoveListener(this); |
198 associated_registration_ = NULL; | 204 associated_registration_ = NULL; |
199 | 205 ClearVersionAttributes(); |
200 SetActiveVersion(NULL); | |
201 SetInstallingVersion(NULL); | |
202 SetWaitingVersion(NULL); | |
203 SetControllerVersion(NULL); | |
204 } | 206 } |
205 | 207 |
206 scoped_ptr<ServiceWorkerRequestHandler> | 208 scoped_ptr<ServiceWorkerRequestHandler> |
207 ServiceWorkerProviderHost::CreateRequestHandler( | 209 ServiceWorkerProviderHost::CreateRequestHandler( |
208 ResourceType resource_type, | 210 ResourceType resource_type, |
209 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context) { | 211 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context) { |
210 if (IsHostToRunningServiceWorker()) { | 212 if (IsHostToRunningServiceWorker()) { |
211 return scoped_ptr<ServiceWorkerRequestHandler>( | 213 return scoped_ptr<ServiceWorkerRequestHandler>( |
212 new ServiceWorkerContextRequestHandler( | 214 new ServiceWorkerContextRequestHandler( |
213 context_, AsWeakPtr(), blob_storage_context, resource_type)); | 215 context_, AsWeakPtr(), blob_storage_context, resource_type)); |
214 } | 216 } |
215 if (ServiceWorkerUtils::IsMainResourceType(resource_type) || | 217 if (ServiceWorkerUtils::IsMainResourceType(resource_type) || |
216 active_version()) { | 218 active_version()) { |
217 return scoped_ptr<ServiceWorkerRequestHandler>( | 219 return scoped_ptr<ServiceWorkerRequestHandler>( |
218 new ServiceWorkerControlleeRequestHandler( | 220 new ServiceWorkerControlleeRequestHandler( |
219 context_, AsWeakPtr(), blob_storage_context, resource_type)); | 221 context_, AsWeakPtr(), blob_storage_context, resource_type)); |
220 } | 222 } |
221 return scoped_ptr<ServiceWorkerRequestHandler>(); | 223 return scoped_ptr<ServiceWorkerRequestHandler>(); |
222 } | 224 } |
223 | 225 |
224 bool ServiceWorkerProviderHost::CanAssociateVersion( | 226 bool ServiceWorkerProviderHost::CanSetVersionAttribute( |
michaeln
2014/08/11 22:15:41
I think this method could maybe be simplified or e
nhiroki
2014/08/12 01:45:56
Ah, good point! I think we can completely remove t
| |
227 ChangedVersionAttributesMask::AttributeType type, | |
225 ServiceWorkerVersion* version) { | 228 ServiceWorkerVersion* version) { |
226 if (!context_) | 229 if (!context_) |
227 return false; | 230 return false; |
228 if (running_hosted_version_) | 231 if (running_hosted_version_) |
229 return false; | 232 return false; |
230 if (!version) | 233 if (!associated_registration_) |
231 return true; | 234 return false; |
235 if (version && version->registration_id() != associated_registration_->id()) | |
236 return false; | |
232 | 237 |
233 ServiceWorkerVersion* already_associated_version = NULL; | 238 switch (type) { |
234 if (controlling_version_) | 239 case ChangedVersionAttributesMask::INSTALLING_VERSION: |
235 already_associated_version = controlling_version_; | 240 return installing_version_ != version; |
236 if (active_version_) | 241 case ChangedVersionAttributesMask::WAITING_VERSION: |
237 already_associated_version = active_version_; | 242 return waiting_version_ != version; |
238 else if (waiting_version_) | 243 case ChangedVersionAttributesMask::ACTIVE_VERSION: |
239 already_associated_version = waiting_version_; | 244 return active_version_ != version; |
240 else if (installing_version_) | 245 case ChangedVersionAttributesMask::CONTROLLING_VERSION: |
241 already_associated_version = installing_version_; | 246 return controlling_version_ != version; |
242 | 247 } |
243 return !already_associated_version || | 248 NOTREACHED(); |
244 already_associated_version->registration_id() == | 249 return false; |
245 version->registration_id(); | |
246 } | 250 } |
247 | 251 |
248 bool ServiceWorkerProviderHost::CanAssociateRegistration( | 252 bool ServiceWorkerProviderHost::CanAssociateRegistration( |
249 ServiceWorkerRegistration* registration) { | 253 ServiceWorkerRegistration* registration) { |
250 if (!context_) | 254 if (!context_) |
251 return false; | 255 return false; |
252 if (running_hosted_version_) | 256 if (running_hosted_version_) |
253 return false; | 257 return false; |
254 if (!registration || associated_registration_) | 258 if (!registration || associated_registration_) |
255 return false; | 259 return false; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); | 293 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); |
290 } | 294 } |
291 return info; | 295 return info; |
292 } | 296 } |
293 | 297 |
294 bool ServiceWorkerProviderHost::IsContextAlive() { | 298 bool ServiceWorkerProviderHost::IsContextAlive() { |
295 return context_ != NULL; | 299 return context_ != NULL; |
296 } | 300 } |
297 | 301 |
298 } // namespace content | 302 } // namespace content |
OLD | NEW |