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