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 10 matching lines...) Expand all Loading... |
50 // Focus the frame's view to make sure the frame is now considered as focused. | 52 // Focus the frame's view to make sure the frame is now considered as focused. |
51 render_frame_host->GetView()->Focus(); | 53 render_frame_host->GetView()->Focus(); |
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 |
| 62 // PlzNavigate |
| 63 // Next ServiceWorkerProviderHost ID for navigations, starts at -2 and keeps |
| 64 // going down. |
| 65 int g_next_navigation_provider_id = -2; |
| 66 |
60 } // anonymous namespace | 67 } // anonymous namespace |
61 | 68 |
62 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback( | 69 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback( |
63 const GetRegistrationForReadyCallback& callback) | 70 const GetRegistrationForReadyCallback& callback) |
64 : callback(callback), | 71 : callback(callback), |
65 called(false) { | 72 called(false) { |
66 } | 73 } |
67 | 74 |
68 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { | 75 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { |
69 } | 76 } |
70 | 77 |
| 78 // static |
| 79 scoped_ptr<ServiceWorkerProviderHost> |
| 80 ServiceWorkerProviderHost::PreCreateNavigationHost( |
| 81 base::WeakPtr<ServiceWorkerContextCore> context) { |
| 82 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 83 switches::kEnableBrowserSideNavigation)); |
| 84 // Generate a new browser-assigned id for the host. |
| 85 int provider_id = g_next_navigation_provider_id--; |
| 86 return scoped_ptr<ServiceWorkerProviderHost>(new ServiceWorkerProviderHost( |
| 87 ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE, provider_id, |
| 88 SERVICE_WORKER_PROVIDER_FOR_WINDOW, context, nullptr)); |
| 89 } |
| 90 |
71 ServiceWorkerProviderHost::ServiceWorkerProviderHost( | 91 ServiceWorkerProviderHost::ServiceWorkerProviderHost( |
72 int render_process_id, | 92 int render_process_id, |
73 int route_id, | 93 int route_id, |
74 int provider_id, | 94 int provider_id, |
75 ServiceWorkerProviderType provider_type, | 95 ServiceWorkerProviderType provider_type, |
76 base::WeakPtr<ServiceWorkerContextCore> context, | 96 base::WeakPtr<ServiceWorkerContextCore> context, |
77 ServiceWorkerDispatcherHost* dispatcher_host) | 97 ServiceWorkerDispatcherHost* dispatcher_host) |
78 : client_uuid_(base::GenerateGUID()), | 98 : client_uuid_(base::GenerateGUID()), |
79 render_process_id_(render_process_id), | 99 render_process_id_(render_process_id), |
80 route_id_(route_id), | 100 route_id_(route_id), |
81 render_thread_id_(kDocumentMainThreadId), | 101 render_thread_id_(kDocumentMainThreadId), |
82 provider_id_(provider_id), | 102 provider_id_(provider_id), |
83 provider_type_(provider_type), | 103 provider_type_(provider_type), |
84 context_(context), | 104 context_(context), |
85 dispatcher_host_(dispatcher_host), | 105 dispatcher_host_(dispatcher_host), |
86 allow_association_(true) { | 106 allow_association_(true) { |
87 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); | |
88 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, provider_type_); | 107 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, provider_type_); |
89 DCHECK_NE(SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME, provider_type_); | 108 DCHECK_NE(SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME, provider_type_); |
| 109 |
| 110 // PlzNavigate |
| 111 CHECK_IMPLIES(render_process_id == ChildProcessHost::kInvalidUniqueID, |
| 112 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 113 switches::kEnableBrowserSideNavigation)); |
| 114 |
90 if (provider_type_ == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { | 115 if (provider_type_ == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { |
91 // Actual thread id is set when the service worker context gets started. | 116 // Actual thread id is set when the service worker context gets started. |
92 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; | 117 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; |
93 } | 118 } |
94 context_->RegisterProviderHostByClientID(client_uuid_, this); | 119 context_->RegisterProviderHostByClientID(client_uuid_, this); |
95 } | 120 } |
96 | 121 |
97 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { | 122 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { |
98 if (context_) | 123 if (context_) |
99 context_->UnregisterProviderHostByClientID(client_uuid_); | 124 context_->UnregisterProviderHostByClientID(client_uuid_); |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( | 534 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( |
510 int new_process_id, | 535 int new_process_id, |
511 int new_frame_id, | 536 int new_frame_id, |
512 int new_provider_id, | 537 int new_provider_id, |
513 ServiceWorkerProviderType new_provider_type, | 538 ServiceWorkerProviderType new_provider_type, |
514 ServiceWorkerDispatcherHost* new_dispatcher_host) { | 539 ServiceWorkerDispatcherHost* new_dispatcher_host) { |
515 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); | 540 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
516 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, new_process_id); | 541 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, new_process_id); |
517 DCHECK_NE(MSG_ROUTING_NONE, new_frame_id); | 542 DCHECK_NE(MSG_ROUTING_NONE, new_frame_id); |
518 | 543 |
519 render_process_id_ = new_process_id; | |
520 route_id_ = new_frame_id; | |
521 render_thread_id_ = kDocumentMainThreadId; | 544 render_thread_id_ = kDocumentMainThreadId; |
522 provider_id_ = new_provider_id; | 545 provider_id_ = new_provider_id; |
523 provider_type_ = new_provider_type; | 546 provider_type_ = new_provider_type; |
524 dispatcher_host_ = new_dispatcher_host; | |
525 | 547 |
526 for (const GURL& pattern : associated_patterns_) | 548 FinalizeInitialization(new_process_id, new_frame_id, new_dispatcher_host); |
527 IncreaseProcessReference(pattern); | 549 } |
528 | 550 |
529 for (auto& key_registration : matching_registrations_) | 551 // PlzNavigate |
530 IncreaseProcessReference(key_registration.second->pattern()); | 552 void ServiceWorkerProviderHost::CompleteNavigationInitialized( |
| 553 int process_id, |
| 554 int frame_routing_id, |
| 555 ServiceWorkerDispatcherHost* dispatcher_host) { |
| 556 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 557 switches::kEnableBrowserSideNavigation)); |
| 558 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
| 559 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_WINDOW, provider_type_); |
| 560 DCHECK_EQ(kDocumentMainThreadId, render_thread_id_); |
531 | 561 |
532 if (associated_registration_.get()) { | 562 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, process_id); |
533 SendAssociateRegistrationMessage(); | 563 DCHECK_NE(MSG_ROUTING_NONE, frame_routing_id); |
534 if (dispatcher_host_ && associated_registration_->active_version()) { | 564 |
535 Send(new ServiceWorkerMsg_SetControllerServiceWorker( | 565 FinalizeInitialization(process_id, frame_routing_id, dispatcher_host); |
536 render_thread_id_, provider_id(), | |
537 GetOrCreateServiceWorkerHandle( | |
538 associated_registration_->active_version()), | |
539 false /* shouldNotifyControllerChange */)); | |
540 } | |
541 } | |
542 } | 566 } |
543 | 567 |
544 void ServiceWorkerProviderHost::SendUpdateFoundMessage( | 568 void ServiceWorkerProviderHost::SendUpdateFoundMessage( |
545 int registration_handle_id) { | 569 int registration_handle_id) { |
546 if (!dispatcher_host_) | 570 if (!dispatcher_host_) |
547 return; // Could be nullptr in some tests. | 571 return; // Could be nullptr in some tests. |
548 | 572 |
549 if (!IsReadyToSendMessages()) { | 573 if (!IsReadyToSendMessages()) { |
550 queued_events_.push_back( | 574 queued_events_.push_back( |
551 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage, | 575 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage, |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 bool ServiceWorkerProviderHost::IsContextAlive() { | 701 bool ServiceWorkerProviderHost::IsContextAlive() { |
678 return context_ != NULL; | 702 return context_ != NULL; |
679 } | 703 } |
680 | 704 |
681 void ServiceWorkerProviderHost::Send(IPC::Message* message) const { | 705 void ServiceWorkerProviderHost::Send(IPC::Message* message) const { |
682 DCHECK(dispatcher_host_); | 706 DCHECK(dispatcher_host_); |
683 DCHECK(IsReadyToSendMessages()); | 707 DCHECK(IsReadyToSendMessages()); |
684 dispatcher_host_->Send(message); | 708 dispatcher_host_->Send(message); |
685 } | 709 } |
686 | 710 |
| 711 void ServiceWorkerProviderHost::FinalizeInitialization( |
| 712 int process_id, |
| 713 int frame_routing_id, |
| 714 ServiceWorkerDispatcherHost* dispatcher_host) { |
| 715 render_process_id_ = process_id; |
| 716 route_id_ = frame_routing_id; |
| 717 dispatcher_host_ = dispatcher_host; |
| 718 |
| 719 for (const GURL& pattern : associated_patterns_) |
| 720 IncreaseProcessReference(pattern); |
| 721 |
| 722 for (auto& key_registration : matching_registrations_) |
| 723 IncreaseProcessReference(key_registration.second->pattern()); |
| 724 |
| 725 if (associated_registration_.get()) { |
| 726 SendAssociateRegistrationMessage(); |
| 727 if (dispatcher_host_ && associated_registration_->active_version()) { |
| 728 Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
| 729 render_thread_id_, provider_id(), |
| 730 GetOrCreateServiceWorkerHandle( |
| 731 associated_registration_->active_version()), |
| 732 false /* shouldNotifyControllerChange */)); |
| 733 } |
| 734 } |
| 735 } |
| 736 |
687 } // namespace content | 737 } // namespace content |
OLD | NEW |