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