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/frame_host/frame_tree.h" | 8 #include "content/browser/frame_host/frame_tree.h" |
9 #include "content/browser/frame_host/frame_tree_node.h" | 9 #include "content/browser/frame_host/frame_tree_node.h" |
10 #include "content/browser/frame_host/render_frame_host_impl.h" | 10 #include "content/browser/frame_host/render_frame_host_impl.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 render_frame_host->GetView()->Focus(); | 69 render_frame_host->GetView()->Focus(); |
70 | 70 |
71 // Move the web contents to the foreground. | 71 // Move the web contents to the foreground. |
72 web_contents->Activate(); | 72 web_contents->Activate(); |
73 | 73 |
74 return GetClientInfoOnUIThread(render_process_id, render_frame_id); | 74 return GetClientInfoOnUIThread(render_process_id, render_frame_id); |
75 } | 75 } |
76 | 76 |
77 } // anonymous namespace | 77 } // anonymous namespace |
78 | 78 |
79 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback( | |
80 const GetRegistrationForReadyCallback& callback) | |
81 : callback(callback), | |
82 called(false) { | |
83 } | |
84 | |
85 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { | |
86 callback.Reset(); | |
michaeln
2015/02/26 21:41:06
not sure this line is needed?
xiang
2015/02/27 09:33:46
Done. This's not needed.
| |
87 } | |
88 | |
79 ServiceWorkerProviderHost::ServiceWorkerProviderHost( | 89 ServiceWorkerProviderHost::ServiceWorkerProviderHost( |
80 int render_process_id, | 90 int render_process_id, |
81 int render_frame_id, | 91 int render_frame_id, |
82 int provider_id, | 92 int provider_id, |
83 ServiceWorkerProviderType provider_type, | 93 ServiceWorkerProviderType provider_type, |
84 base::WeakPtr<ServiceWorkerContextCore> context, | 94 base::WeakPtr<ServiceWorkerContextCore> context, |
85 ServiceWorkerDispatcherHost* dispatcher_host) | 95 ServiceWorkerDispatcherHost* dispatcher_host) |
86 : render_process_id_(render_process_id), | 96 : render_process_id_(render_process_id), |
87 render_frame_id_(render_frame_id), | 97 render_frame_id_(render_frame_id), |
88 render_thread_id_(kDocumentMainThreadId), | 98 render_thread_id_(kDocumentMainThreadId), |
(...skipping 10 matching lines...) Expand all Loading... | |
99 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; | 109 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; |
100 } | 110 } |
101 } | 111 } |
102 | 112 |
103 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { | 113 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { |
104 // Clear docurl so the deferred activation of a waiting worker | 114 // Clear docurl so the deferred activation of a waiting worker |
105 // won't associate the new version with a provider being destroyed. | 115 // won't associate the new version with a provider being destroyed. |
106 document_url_ = GURL(); | 116 document_url_ = GURL(); |
107 if (controlling_version_.get()) | 117 if (controlling_version_.get()) |
108 controlling_version_->RemoveControllee(this); | 118 controlling_version_->RemoveControllee(this); |
109 if (associated_registration_.get()) { | 119 |
110 DecreaseProcessReference(associated_registration_->pattern()); | 120 for (auto& key_registration : matching_registrations_) { |
111 associated_registration_->RemoveListener(this); | 121 DecreaseProcessReference(key_registration.second->pattern()); |
122 key_registration.second->RemoveListener(this); | |
112 } | 123 } |
113 | 124 |
114 for (const GURL& pattern : associated_patterns_) | 125 for (const GURL& pattern : associated_patterns_) |
115 DecreaseProcessReference(pattern); | 126 DecreaseProcessReference(pattern); |
116 } | 127 } |
117 | 128 |
129 void ServiceWorkerProviderHost::OnVersionAttributesChanged( | |
130 ServiceWorkerRegistration* registration, | |
131 ChangedVersionAttributesMask changed_mask, | |
132 const ServiceWorkerRegistrationInfo& info) { | |
133 ReturnRegistrationForReadyIfNeeded(); | |
134 } | |
135 | |
118 void ServiceWorkerProviderHost::OnRegistrationFailed( | 136 void ServiceWorkerProviderHost::OnRegistrationFailed( |
119 ServiceWorkerRegistration* registration) { | 137 ServiceWorkerRegistration* registration) { |
120 DCHECK_EQ(associated_registration_.get(), registration); | 138 if (associated_registration_ == registration) |
121 DisassociateRegistration(); | 139 DisassociateRegistration(); |
140 RemoveMatchingRegistration(registration); | |
141 } | |
142 | |
143 void ServiceWorkerProviderHost::OnRegistrationFinishedUninstalling( | |
144 ServiceWorkerRegistration* registration) { | |
145 RemoveMatchingRegistration(registration); | |
122 } | 146 } |
123 | 147 |
124 void ServiceWorkerProviderHost::OnSkippedWaiting( | 148 void ServiceWorkerProviderHost::OnSkippedWaiting( |
125 ServiceWorkerRegistration* registration) { | 149 ServiceWorkerRegistration* registration) { |
126 DCHECK_EQ(associated_registration_.get(), registration); | 150 if (associated_registration_ != registration) |
151 return; | |
127 // A client is "using" a registration if it is controlled by the active | 152 // A client is "using" a registration if it is controlled by the active |
128 // worker of the registration. skipWaiting doesn't cause a client to start | 153 // worker of the registration. skipWaiting doesn't cause a client to start |
129 // using the registration. | 154 // using the registration. |
130 if (!controlling_version_) | 155 if (!controlling_version_) |
131 return; | 156 return; |
132 ServiceWorkerVersion* active_version = registration->active_version(); | 157 ServiceWorkerVersion* active_version = registration->active_version(); |
133 DCHECK_EQ(active_version->status(), ServiceWorkerVersion::ACTIVATING); | 158 DCHECK_EQ(active_version->status(), ServiceWorkerVersion::ACTIVATING); |
134 SetControllerVersionAttribute(active_version); | 159 SetControllerVersionAttribute(active_version); |
135 } | 160 } |
136 | 161 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 return false; | 212 return false; |
188 } | 213 } |
189 | 214 |
190 running_hosted_version_ = live_version; | 215 running_hosted_version_ = live_version; |
191 return true; | 216 return true; |
192 } | 217 } |
193 | 218 |
194 void ServiceWorkerProviderHost::AssociateRegistration( | 219 void ServiceWorkerProviderHost::AssociateRegistration( |
195 ServiceWorkerRegistration* registration) { | 220 ServiceWorkerRegistration* registration) { |
196 DCHECK(CanAssociateRegistration(registration)); | 221 DCHECK(CanAssociateRegistration(registration)); |
197 IncreaseProcessReference(registration->pattern()); | |
198 associated_registration_ = registration; | 222 associated_registration_ = registration; |
199 associated_registration_->AddListener(this); | 223 AddMatchingRegistration(registration); |
200 SendAssociateRegistrationMessage(); | 224 SendAssociateRegistrationMessage(); |
201 SetControllerVersionAttribute(registration->active_version()); | 225 SetControllerVersionAttribute(registration->active_version()); |
202 } | 226 } |
203 | 227 |
204 void ServiceWorkerProviderHost::DisassociateRegistration() { | 228 void ServiceWorkerProviderHost::DisassociateRegistration() { |
205 queued_events_.clear(); | 229 queued_events_.clear(); |
206 if (!associated_registration_.get()) | 230 if (!associated_registration_.get()) |
207 return; | 231 return; |
208 DecreaseProcessReference(associated_registration_->pattern()); | |
209 associated_registration_->RemoveListener(this); | |
210 associated_registration_ = NULL; | 232 associated_registration_ = NULL; |
211 SetControllerVersionAttribute(NULL); | 233 SetControllerVersionAttribute(NULL); |
212 | 234 |
213 if (!dispatcher_host_) | 235 if (!dispatcher_host_) |
214 return; | 236 return; |
215 | 237 |
216 // Disassociation message should be sent only for controllees. | 238 // Disassociation message should be sent only for controllees. |
217 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, provider_type_); | 239 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, provider_type_); |
218 Send(new ServiceWorkerMsg_DisassociateRegistration( | 240 Send(new ServiceWorkerMsg_DisassociateRegistration( |
219 render_thread_id_, provider_id())); | 241 render_thread_id_, provider_id())); |
220 } | 242 } |
221 | 243 |
244 void ServiceWorkerProviderHost::AddMatchingRegistration( | |
245 ServiceWorkerRegistration* registration) { | |
246 DCHECK(ServiceWorkerUtils::ScopeMatches( | |
247 registration->pattern(), document_url_)); | |
248 size_t key = registration->pattern().spec().size(); | |
249 if (ContainsKey(matching_registrations_, key)) | |
250 return; | |
251 IncreaseProcessReference(registration->pattern()); | |
252 registration->AddListener(this); | |
253 matching_registrations_[key] = registration; | |
254 ReturnRegistrationForReadyIfNeeded(); | |
255 } | |
256 | |
257 void ServiceWorkerProviderHost::RemoveMatchingRegistration( | |
258 ServiceWorkerRegistration* registration) { | |
259 size_t key = registration->pattern().spec().size(); | |
260 DCHECK(ContainsKey(matching_registrations_, key)); | |
261 DecreaseProcessReference(registration->pattern()); | |
262 registration->RemoveListener(this); | |
263 matching_registrations_.erase(key); | |
264 } | |
265 | |
266 ServiceWorkerRegistration* | |
267 ServiceWorkerProviderHost::MatchRegistration() const { | |
268 ServiceWorkerRegistrationMap::const_reverse_iterator it = | |
269 matching_registrations_.rbegin(); | |
270 for (; it != matching_registrations_.rend(); ++it) { | |
271 if (it->second->is_uninstalled()) | |
272 continue; | |
273 if (it->second->is_uninstalling()) | |
274 return nullptr; | |
275 return it->second.get(); | |
276 } | |
277 return nullptr; | |
278 } | |
279 | |
222 scoped_ptr<ServiceWorkerRequestHandler> | 280 scoped_ptr<ServiceWorkerRequestHandler> |
223 ServiceWorkerProviderHost::CreateRequestHandler( | 281 ServiceWorkerProviderHost::CreateRequestHandler( |
224 FetchRequestMode request_mode, | 282 FetchRequestMode request_mode, |
225 FetchCredentialsMode credentials_mode, | 283 FetchCredentialsMode credentials_mode, |
226 ResourceType resource_type, | 284 ResourceType resource_type, |
227 RequestContextType request_context_type, | 285 RequestContextType request_context_type, |
228 RequestContextFrameType frame_type, | 286 RequestContextFrameType frame_type, |
229 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 287 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
230 scoped_refptr<ResourceRequestBody> body) { | 288 scoped_refptr<ResourceRequestBody> body) { |
231 if (IsHostToRunningServiceWorker()) { | 289 if (IsHostToRunningServiceWorker()) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 is_claiming_ = true; | 381 is_claiming_ = true; |
324 if (registration == associated_registration_) { | 382 if (registration == associated_registration_) { |
325 SetControllerVersionAttribute(registration->active_version()); | 383 SetControllerVersionAttribute(registration->active_version()); |
326 } else if (allow_association_) { | 384 } else if (allow_association_) { |
327 DisassociateRegistration(); | 385 DisassociateRegistration(); |
328 AssociateRegistration(registration); | 386 AssociateRegistration(registration); |
329 } | 387 } |
330 is_claiming_ = false; | 388 is_claiming_ = false; |
331 } | 389 } |
332 | 390 |
391 bool ServiceWorkerProviderHost::GetRegistrationForReady( | |
392 const GetRegistrationForReadyCallback& callback) { | |
393 if (get_ready_callback_) | |
394 return false; | |
395 get_ready_callback_.reset(new OneShotGetReadyCallback(callback)); | |
396 ReturnRegistrationForReadyIfNeeded(); | |
397 return true; | |
398 } | |
399 | |
333 void ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() { | 400 void ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() { |
334 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); | 401 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
335 DCHECK_NE(MSG_ROUTING_NONE, render_frame_id_); | 402 DCHECK_NE(MSG_ROUTING_NONE, render_frame_id_); |
336 DCHECK_EQ(kDocumentMainThreadId, render_thread_id_); | 403 DCHECK_EQ(kDocumentMainThreadId, render_thread_id_); |
337 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, provider_type_); | 404 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, provider_type_); |
338 | 405 |
339 for (const GURL& pattern : associated_patterns_) | 406 for (const GURL& pattern : associated_patterns_) |
340 DecreaseProcessReference(pattern); | 407 DecreaseProcessReference(pattern); |
341 | 408 |
409 for (auto& key_registration : matching_registrations_) | |
410 DecreaseProcessReference(key_registration.second->pattern()); | |
411 | |
342 if (associated_registration_.get()) { | 412 if (associated_registration_.get()) { |
343 DecreaseProcessReference(associated_registration_->pattern()); | |
344 if (dispatcher_host_) { | 413 if (dispatcher_host_) { |
345 Send(new ServiceWorkerMsg_DisassociateRegistration( | 414 Send(new ServiceWorkerMsg_DisassociateRegistration( |
346 render_thread_id_, provider_id())); | 415 render_thread_id_, provider_id())); |
347 } | 416 } |
348 } | 417 } |
349 | 418 |
350 render_process_id_ = ChildProcessHost::kInvalidUniqueID; | 419 render_process_id_ = ChildProcessHost::kInvalidUniqueID; |
351 render_frame_id_ = MSG_ROUTING_NONE; | 420 render_frame_id_ = MSG_ROUTING_NONE; |
352 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; | 421 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; |
353 provider_id_ = kInvalidServiceWorkerProviderId; | 422 provider_id_ = kInvalidServiceWorkerProviderId; |
(...skipping 14 matching lines...) Expand all Loading... | |
368 render_process_id_ = new_process_id; | 437 render_process_id_ = new_process_id; |
369 render_frame_id_ = new_frame_id; | 438 render_frame_id_ = new_frame_id; |
370 render_thread_id_ = kDocumentMainThreadId; | 439 render_thread_id_ = kDocumentMainThreadId; |
371 provider_id_ = new_provider_id; | 440 provider_id_ = new_provider_id; |
372 provider_type_ = new_provider_type; | 441 provider_type_ = new_provider_type; |
373 dispatcher_host_ = new_dispatcher_host; | 442 dispatcher_host_ = new_dispatcher_host; |
374 | 443 |
375 for (const GURL& pattern : associated_patterns_) | 444 for (const GURL& pattern : associated_patterns_) |
376 IncreaseProcessReference(pattern); | 445 IncreaseProcessReference(pattern); |
377 | 446 |
447 for (auto& key_registration : matching_registrations_) | |
448 IncreaseProcessReference(key_registration.second->pattern()); | |
449 | |
378 if (associated_registration_.get()) { | 450 if (associated_registration_.get()) { |
379 IncreaseProcessReference(associated_registration_->pattern()); | |
380 SendAssociateRegistrationMessage(); | 451 SendAssociateRegistrationMessage(); |
381 if (dispatcher_host_ && associated_registration_->active_version()) { | 452 if (dispatcher_host_ && associated_registration_->active_version()) { |
382 Send(new ServiceWorkerMsg_SetControllerServiceWorker( | 453 Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
383 render_thread_id_, provider_id(), | 454 render_thread_id_, provider_id(), |
384 CreateAndRegisterServiceWorkerHandle( | 455 CreateAndRegisterServiceWorkerHandle( |
385 associated_registration_->active_version()), | 456 associated_registration_->active_version()), |
386 false /* shouldNotifyControllerChange */)); | 457 false /* shouldNotifyControllerChange */)); |
387 } | 458 } |
388 } | 459 } |
389 } | 460 } |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
495 } | 566 } |
496 | 567 |
497 void ServiceWorkerProviderHost::DecreaseProcessReference( | 568 void ServiceWorkerProviderHost::DecreaseProcessReference( |
498 const GURL& pattern) { | 569 const GURL& pattern) { |
499 if (context_ && context_->process_manager()) { | 570 if (context_ && context_->process_manager()) { |
500 context_->process_manager()->RemoveProcessReferenceFromPattern( | 571 context_->process_manager()->RemoveProcessReferenceFromPattern( |
501 pattern, render_process_id_); | 572 pattern, render_process_id_); |
502 } | 573 } |
503 } | 574 } |
504 | 575 |
576 void ServiceWorkerProviderHost::ReturnRegistrationForReadyIfNeeded() { | |
577 if (!get_ready_callback_ || get_ready_callback_->called) | |
578 return; | |
579 ServiceWorkerRegistration* registration = MatchRegistration(); | |
580 if (!registration) | |
581 return; | |
582 if (registration->active_version()) { | |
583 get_ready_callback_->callback.Run(registration); | |
584 get_ready_callback_->callback.Reset(); | |
585 get_ready_callback_->called = true; | |
586 return; | |
587 } | |
588 } | |
589 | |
505 bool ServiceWorkerProviderHost::IsReadyToSendMessages() const { | 590 bool ServiceWorkerProviderHost::IsReadyToSendMessages() const { |
506 return render_thread_id_ != kInvalidEmbeddedWorkerThreadId; | 591 return render_thread_id_ != kInvalidEmbeddedWorkerThreadId; |
507 } | 592 } |
508 | 593 |
509 bool ServiceWorkerProviderHost::IsContextAlive() { | 594 bool ServiceWorkerProviderHost::IsContextAlive() { |
510 return context_ != NULL; | 595 return context_ != NULL; |
511 } | 596 } |
512 | 597 |
513 void ServiceWorkerProviderHost::Send(IPC::Message* message) const { | 598 void ServiceWorkerProviderHost::Send(IPC::Message* message) const { |
514 DCHECK(dispatcher_host_); | 599 DCHECK(dispatcher_host_); |
515 DCHECK(IsReadyToSendMessages()); | 600 DCHECK(IsReadyToSendMessages()); |
516 dispatcher_host_->Send(message); | 601 dispatcher_host_->Send(message); |
517 } | 602 } |
518 | 603 |
519 } // namespace content | 604 } // namespace content |
OLD | NEW |