| 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/command_line.h" |
| 7 #include "base/guid.h" | 8 #include "base/guid.h" |
| 8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 10 #include "content/browser/frame_host/frame_tree.h" | 11 #include "content/browser/frame_host/frame_tree.h" |
| 11 #include "content/browser/frame_host/frame_tree_node.h" | 12 #include "content/browser/frame_host/frame_tree_node.h" |
| 12 #include "content/browser/frame_host/render_frame_host_impl.h" | 13 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 13 #include "content/browser/message_port_message_filter.h" | 14 #include "content/browser/message_port_message_filter.h" |
| 14 #include "content/browser/service_worker/service_worker_context_core.h" | 15 #include "content/browser/service_worker/service_worker_context_core.h" |
| 15 #include "content/browser/service_worker/service_worker_context_request_handler.
h" | 16 #include "content/browser/service_worker/service_worker_context_request_handler.
h" |
| 16 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" | 17 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" |
| 17 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | 18 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 18 #include "content/browser/service_worker/service_worker_handle.h" | 19 #include "content/browser/service_worker/service_worker_handle.h" |
| 19 #include "content/browser/service_worker/service_worker_registration_handle.h" | 20 #include "content/browser/service_worker/service_worker_registration_handle.h" |
| 20 #include "content/browser/service_worker/service_worker_version.h" | 21 #include "content/browser/service_worker/service_worker_version.h" |
| 21 #include "content/browser/web_contents/web_contents_impl.h" | 22 #include "content/browser/web_contents/web_contents_impl.h" |
| 22 #include "content/common/resource_request_body.h" | 23 #include "content/common/resource_request_body.h" |
| 23 #include "content/common/service_worker/service_worker_messages.h" | 24 #include "content/common/service_worker/service_worker_messages.h" |
| 24 #include "content/common/service_worker/service_worker_types.h" | 25 #include "content/common/service_worker/service_worker_types.h" |
| 25 #include "content/common/service_worker/service_worker_utils.h" | 26 #include "content/common/service_worker/service_worker_utils.h" |
| 26 #include "content/public/browser/render_frame_host.h" | 27 #include "content/public/browser/render_frame_host.h" |
| 27 #include "content/public/browser/render_widget_host_view.h" | 28 #include "content/public/browser/render_widget_host_view.h" |
| 28 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/common/child_process_host.h" | 30 #include "content/public/common/child_process_host.h" |
| 31 #include "content/public/common/content_switches.h" |
| 30 | 32 |
| 31 namespace content { | 33 namespace content { |
| 32 | 34 |
| 33 namespace { | 35 namespace { |
| 34 | 36 |
| 35 ServiceWorkerClientInfo FocusOnUIThread(int render_process_id, | 37 ServiceWorkerClientInfo FocusOnUIThread(int render_process_id, |
| 36 int render_frame_id) { | 38 int render_frame_id) { |
| 37 RenderFrameHostImpl* render_frame_host = | 39 RenderFrameHostImpl* render_frame_host = |
| 38 RenderFrameHostImpl::FromID(render_process_id, render_frame_id); | 40 RenderFrameHostImpl::FromID(render_process_id, render_frame_id); |
| 39 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | 41 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 52 | 54 |
| 53 // Move the web contents to the foreground. | 55 // Move the web contents to the foreground. |
| 54 web_contents->Activate(); | 56 web_contents->Activate(); |
| 55 | 57 |
| 56 return ServiceWorkerProviderHost::GetWindowClientInfoOnUI(render_process_id, | 58 return ServiceWorkerProviderHost::GetWindowClientInfoOnUI(render_process_id, |
| 57 render_frame_id); | 59 render_frame_id); |
| 58 } | 60 } |
| 59 | 61 |
| 60 } // anonymous namespace | 62 } // anonymous namespace |
| 61 | 63 |
| 64 // PlzNavigate |
| 65 int ServiceWorkerProviderHost::kVirtualProcessIDForBrowserRequest = -2; |
| 66 |
| 62 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback( | 67 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback( |
| 63 const GetRegistrationForReadyCallback& callback) | 68 const GetRegistrationForReadyCallback& callback) |
| 64 : callback(callback), | 69 : callback(callback), |
| 65 called(false) { | 70 called(false) { |
| 66 } | 71 } |
| 67 | 72 |
| 68 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { | 73 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { |
| 69 } | 74 } |
| 70 | 75 |
| 71 ServiceWorkerProviderHost::ServiceWorkerProviderHost( | 76 ServiceWorkerProviderHost::ServiceWorkerProviderHost( |
| 72 int render_process_id, | 77 int render_process_id, |
| 73 int route_id, | 78 int route_id, |
| 74 int provider_id, | 79 int provider_id, |
| 75 ServiceWorkerProviderType provider_type, | 80 ServiceWorkerProviderType provider_type, |
| 76 base::WeakPtr<ServiceWorkerContextCore> context, | 81 base::WeakPtr<ServiceWorkerContextCore> context, |
| 77 ServiceWorkerDispatcherHost* dispatcher_host) | 82 ServiceWorkerDispatcherHost* dispatcher_host) |
| 78 : client_uuid_(base::GenerateGUID()), | 83 : client_uuid_(base::GenerateGUID()), |
| 79 render_process_id_(render_process_id), | 84 render_process_id_(render_process_id), |
| 80 route_id_(route_id), | 85 route_id_(route_id), |
| 81 render_thread_id_(kDocumentMainThreadId), | 86 render_thread_id_(kDocumentMainThreadId), |
| 82 provider_id_(provider_id), | 87 provider_id_(provider_id), |
| 83 provider_type_(provider_type), | 88 provider_type_(provider_type), |
| 84 context_(context), | 89 context_(context), |
| 85 dispatcher_host_(dispatcher_host), | 90 dispatcher_host_(dispatcher_host), |
| 86 allow_association_(true) { | 91 allow_association_(true) { |
| 87 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); | 92 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
| 88 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, provider_type_); | 93 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, provider_type_); |
| 89 DCHECK_NE(SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME, provider_type_); | 94 DCHECK_NE(SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME, provider_type_); |
| 95 |
| 96 // PlzNavigate |
| 97 CHECK_IMPLIES(render_process_id == kVirtualProcessIDForBrowserRequest, |
| 98 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 99 switches::kEnableBrowserSideNavigation)); |
| 100 |
| 90 if (provider_type_ == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { | 101 if (provider_type_ == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { |
| 91 // Actual thread id is set when the service worker context gets started. | 102 // Actual thread id is set when the service worker context gets started. |
| 92 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; | 103 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; |
| 93 } | 104 } |
| 94 context_->RegisterProviderHostByClientID(client_uuid_, this); | 105 context_->RegisterProviderHostByClientID(client_uuid_, this); |
| 95 } | 106 } |
| 96 | 107 |
| 97 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { | 108 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { |
| 98 if (context_) | 109 if (context_) |
| 99 context_->UnregisterProviderHostByClientID(client_uuid_); | 110 context_->UnregisterProviderHostByClientID(client_uuid_); |
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 provider_type_ = SERVICE_WORKER_PROVIDER_UNKNOWN; | 516 provider_type_ = SERVICE_WORKER_PROVIDER_UNKNOWN; |
| 506 dispatcher_host_ = nullptr; | 517 dispatcher_host_ = nullptr; |
| 507 } | 518 } |
| 508 | 519 |
| 509 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( | 520 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( |
| 510 int new_process_id, | 521 int new_process_id, |
| 511 int new_frame_id, | 522 int new_frame_id, |
| 512 int new_provider_id, | 523 int new_provider_id, |
| 513 ServiceWorkerProviderType new_provider_type, | 524 ServiceWorkerProviderType new_provider_type, |
| 514 ServiceWorkerDispatcherHost* new_dispatcher_host) { | 525 ServiceWorkerDispatcherHost* new_dispatcher_host) { |
| 526 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 527 switches::kEnableBrowserSideNavigation)); |
| 515 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); | 528 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
| 516 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, new_process_id); | 529 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, new_process_id); |
| 517 DCHECK_NE(MSG_ROUTING_NONE, new_frame_id); | 530 DCHECK_NE(MSG_ROUTING_NONE, new_frame_id); |
| 518 | 531 |
| 519 render_process_id_ = new_process_id; | |
| 520 route_id_ = new_frame_id; | |
| 521 render_thread_id_ = kDocumentMainThreadId; | 532 render_thread_id_ = kDocumentMainThreadId; |
| 522 provider_id_ = new_provider_id; | 533 provider_id_ = new_provider_id; |
| 523 provider_type_ = new_provider_type; | 534 provider_type_ = new_provider_type; |
| 524 dispatcher_host_ = new_dispatcher_host; | |
| 525 | 535 |
| 526 for (const GURL& pattern : associated_patterns_) | 536 FinalizeInitialization(new_process_id, new_frame_id, new_dispatcher_host); |
| 527 IncreaseProcessReference(pattern); | 537 } |
| 528 | 538 |
| 529 for (auto& key_registration : matching_registrations_) | 539 // PlzNavigate |
| 530 IncreaseProcessReference(key_registration.second->pattern()); | 540 void ServiceWorkerProviderHost::CompleteNavigationInitialized( |
| 541 int process_id, |
| 542 int frame_id, |
| 543 ServiceWorkerDispatcherHost* dispatcher_host) { |
| 544 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 545 switches::kEnableBrowserSideNavigation)); |
| 546 DCHECK_EQ(kVirtualProcessIDForBrowserRequest, render_process_id_); |
| 547 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_WINDOW, provider_type_); |
| 548 DCHECK_EQ(kDocumentMainThreadId, render_thread_id_); |
| 531 | 549 |
| 532 if (associated_registration_.get()) { | 550 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, process_id); |
| 533 SendAssociateRegistrationMessage(); | 551 DCHECK_NE(MSG_ROUTING_NONE, frame_id); |
| 534 if (dispatcher_host_ && associated_registration_->active_version()) { | 552 |
| 535 Send(new ServiceWorkerMsg_SetControllerServiceWorker( | 553 FinalizeInitialization(process_id, frame_id, dispatcher_host); |
| 536 render_thread_id_, provider_id(), | |
| 537 GetOrCreateServiceWorkerHandle( | |
| 538 associated_registration_->active_version()), | |
| 539 false /* shouldNotifyControllerChange */)); | |
| 540 } | |
| 541 } | |
| 542 } | 554 } |
| 543 | 555 |
| 544 void ServiceWorkerProviderHost::SendUpdateFoundMessage( | 556 void ServiceWorkerProviderHost::SendUpdateFoundMessage( |
| 545 int registration_handle_id) { | 557 int registration_handle_id) { |
| 546 if (!dispatcher_host_) | 558 if (!dispatcher_host_) |
| 547 return; // Could be nullptr in some tests. | 559 return; // Could be nullptr in some tests. |
| 548 | 560 |
| 549 if (!IsReadyToSendMessages()) { | 561 if (!IsReadyToSendMessages()) { |
| 550 queued_events_.push_back( | 562 queued_events_.push_back( |
| 551 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage, | 563 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 bool ServiceWorkerProviderHost::IsContextAlive() { | 689 bool ServiceWorkerProviderHost::IsContextAlive() { |
| 678 return context_ != NULL; | 690 return context_ != NULL; |
| 679 } | 691 } |
| 680 | 692 |
| 681 void ServiceWorkerProviderHost::Send(IPC::Message* message) const { | 693 void ServiceWorkerProviderHost::Send(IPC::Message* message) const { |
| 682 DCHECK(dispatcher_host_); | 694 DCHECK(dispatcher_host_); |
| 683 DCHECK(IsReadyToSendMessages()); | 695 DCHECK(IsReadyToSendMessages()); |
| 684 dispatcher_host_->Send(message); | 696 dispatcher_host_->Send(message); |
| 685 } | 697 } |
| 686 | 698 |
| 699 void ServiceWorkerProviderHost::FinalizeInitialization( |
| 700 int process_id, |
| 701 int frame_id, |
| 702 ServiceWorkerDispatcherHost* dispatcher_host) { |
| 703 render_process_id_ = process_id; |
| 704 route_id_ = frame_id; |
| 705 dispatcher_host_ = dispatcher_host; |
| 706 |
| 707 for (const GURL& pattern : associated_patterns_) |
| 708 IncreaseProcessReference(pattern); |
| 709 |
| 710 for (auto& key_registration : matching_registrations_) |
| 711 IncreaseProcessReference(key_registration.second->pattern()); |
| 712 |
| 713 if (associated_registration_.get()) { |
| 714 SendAssociateRegistrationMessage(); |
| 715 if (dispatcher_host_ && associated_registration_->active_version()) { |
| 716 Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
| 717 render_thread_id_, provider_id(), |
| 718 GetOrCreateServiceWorkerHandle( |
| 719 associated_registration_->active_version()), |
| 720 false /* shouldNotifyControllerChange */)); |
| 721 } |
| 722 } |
| 723 } |
| 724 |
| 687 } // namespace content | 725 } // namespace content |
| OLD | NEW |