Chromium Code Reviews| 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 "chrome/browser/ui/search/search_ipc_router.h" | 5 #include "chrome/browser/ui/search/search_ipc_router.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search/search.h" | 10 #include "chrome/browser/search/search.h" |
| 11 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
| 12 #include "components/search/search.h" | 12 #include "components/search/search.h" |
| 13 #include "content/public/browser/navigation_details.h" | 13 #include "content/public/browser/navigation_details.h" |
| 14 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
| 15 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/associated_interface_provider.h" | 17 #include "content/public/common/associated_interface_provider.h" |
| 18 #include "content/public/common/child_process_host.h" | 18 #include "content/public/common/child_process_host.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 bool IsRenderedInInstantProcess(content::WebContents* web_contents) { | 22 bool IsRenderedInInstantProcess(content::WebContents* web_contents) { |
| 23 // TODO(tibell): Instead of rejecting messages check at connection time if we | 23 // TODO(tibell): Instead of rejecting messages, check at connection time if we |
| 24 // want to talk to the render frame or not. Later, in an out-of-process iframe | 24 // want to talk to the render frame or not. To do that we need a version of |
| 25 // world, the IsRenderedInInstantProcess check will have to be done, as it's | 25 // search::IsRenderedInInstantProcess that works on RenderFrameHost*. That |
| 26 // based on the RenderView. | 26 // should also work in an out-of-process iframe world. Also tests of message |
| 27 // rejection need to be updated/deleted. | |
| 27 Profile* profile = | 28 Profile* profile = |
| 28 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 29 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 29 return search::IsRenderedInInstantProcess(web_contents, profile); | 30 return search::IsRenderedInInstantProcess(web_contents, profile); |
| 30 } | 31 } |
| 31 | 32 |
| 32 class SearchBoxClientFactoryImpl | 33 class SearchBoxClientFactoryImpl |
| 33 : public SearchIPCRouter::SearchBoxClientFactory { | 34 : public SearchIPCRouter::SearchBoxClientFactory, |
| 35 public chrome::mojom::EmbeddedSearchConnector { | |
| 34 public: | 36 public: |
| 35 // The |web_contents| must outlive this object. | 37 // |web_contents| and |binding| must outlive this object. |
| 36 explicit SearchBoxClientFactoryImpl(content::WebContents* web_contents) | 38 SearchBoxClientFactoryImpl( |
| 39 content::WebContents* web_contents, | |
| 40 mojo::AssociatedBinding<chrome::mojom::Instant>* binding) | |
| 37 : web_contents_(web_contents), | 41 : web_contents_(web_contents), |
| 38 last_connected_rfh_( | 42 binding_(binding), |
| 39 std::make_pair(content::ChildProcessHost::kInvalidUniqueID, | 43 bindings_(web_contents, this) { |
| 40 MSG_ROUTING_NONE)) {} | 44 DCHECK(web_contents); |
| 41 chrome::mojom::SearchBox* GetSearchBox() override; | 45 DCHECK(binding); |
| 46 // Before we are connected to a frame we throw away all messages. | |
| 47 mojo::GetIsolatedProxy(&search_box_); | |
| 48 } | |
| 49 | |
| 50 chrome::mojom::SearchBox* GetSearchBox() override { | |
| 51 return search_box_.get(); | |
| 52 } | |
| 42 | 53 |
| 43 private: | 54 private: |
| 44 void OnConnectionError(); | 55 void Connect(chrome::mojom::InstantAssociatedRequest request, |
| 56 chrome::mojom::SearchBoxAssociatedPtrInfo client) override; | |
| 45 | 57 |
| 46 content::WebContents* web_contents_; | 58 content::WebContents* web_contents_; |
| 59 | |
| 60 // An interface used to push updates to the frame that connected to us. Before | |
| 61 // we've been connected to a frame, messages sent on this interface go into | |
| 62 // the void. | |
| 47 chrome::mojom::SearchBoxAssociatedPtr search_box_; | 63 chrome::mojom::SearchBoxAssociatedPtr search_box_; |
| 48 | 64 |
| 49 // The proccess ID and routing ID of the last connected main frame. May be | 65 // Used to bind incoming interface requests to the implementation, which lives |
| 50 // (ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE) if we haven't | 66 // in SearchIPCRouter. |
| 51 // connected yet. | 67 mojo::AssociatedBinding<chrome::mojom::Instant>* binding_; |
| 52 std::pair<int, int> last_connected_rfh_; | 68 |
| 69 // Binding used to listen to connection requests. | |
| 70 content::WebContentsFrameBindingSet<chrome::mojom::EmbeddedSearchConnector> | |
| 71 bindings_; | |
| 53 | 72 |
| 54 DISALLOW_COPY_AND_ASSIGN(SearchBoxClientFactoryImpl); | 73 DISALLOW_COPY_AND_ASSIGN(SearchBoxClientFactoryImpl); |
| 55 }; | 74 }; |
| 56 | 75 |
| 57 chrome::mojom::SearchBox* SearchBoxClientFactoryImpl::GetSearchBox() { | 76 void SearchBoxClientFactoryImpl::Connect( |
| 58 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); | 77 chrome::mojom::InstantAssociatedRequest request, |
| 59 auto id = std::make_pair(frame->GetProcess()->GetID(), frame->GetRoutingID()); | 78 chrome::mojom::SearchBoxAssociatedPtrInfo client) { |
| 60 // Avoid reconnecting repeatedly to the same RenderFrameHost for performance | 79 if (!IsRenderedInInstantProcess(web_contents_)) { |
|
dcheng
2017/02/10 10:24:14
One thing I was wondering... once a renderer proce
tibell
2017/02/12 23:15:26
A renderer should stay the same process type durin
| |
| 61 // reasons. | 80 return; |
| 62 if (id != last_connected_rfh_) { | |
| 63 if (IsRenderedInInstantProcess(web_contents_)) { | |
| 64 frame->GetRemoteAssociatedInterfaces()->GetInterface(&search_box_); | |
| 65 search_box_.set_connection_error_handler( | |
| 66 base::Bind(&SearchBoxClientFactoryImpl::OnConnectionError, | |
| 67 base::Unretained(this))); | |
| 68 } else { | |
| 69 // Renderer is not an instant process. We'll create a connection that | |
| 70 // drops all messages. | |
| 71 mojo::GetIsolatedProxy(&search_box_); | |
| 72 } | |
| 73 last_connected_rfh_ = id; | |
| 74 } | 81 } |
| 75 return search_box_.get(); | 82 binding_->Bind(std::move(request)); |
| 76 } | 83 search_box_.Bind(std::move(client)); |
| 77 | |
| 78 void SearchBoxClientFactoryImpl::OnConnectionError() { | |
| 79 search_box_.reset(); | |
| 80 last_connected_rfh_ = std::make_pair( | |
| 81 content::ChildProcessHost::kInvalidUniqueID, | |
| 82 MSG_ROUTING_NONE); | |
| 83 } | 84 } |
| 84 | 85 |
| 85 } // namespace | 86 } // namespace |
| 86 | 87 |
| 87 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents, | 88 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents, |
| 88 Delegate* delegate, | 89 Delegate* delegate, |
| 89 std::unique_ptr<Policy> policy) | 90 std::unique_ptr<Policy> policy) |
| 90 : WebContentsObserver(web_contents), | 91 : WebContentsObserver(web_contents), |
| 91 delegate_(delegate), | 92 delegate_(delegate), |
| 92 policy_(std::move(policy)), | 93 policy_(std::move(policy)), |
| 93 commit_counter_(0), | 94 commit_counter_(0), |
| 94 is_active_tab_(false), | 95 is_active_tab_(false), |
| 95 bindings_(web_contents, this), | 96 binding_(this), |
| 96 search_box_client_factory_(new SearchBoxClientFactoryImpl(web_contents)) { | 97 search_box_client_factory_( |
| 98 new SearchBoxClientFactoryImpl(web_contents, &binding_)) { | |
| 97 DCHECK(web_contents); | 99 DCHECK(web_contents); |
| 98 DCHECK(delegate); | 100 DCHECK(delegate); |
| 99 DCHECK(policy_.get()); | 101 DCHECK(policy_.get()); |
| 100 } | 102 } |
| 101 | 103 |
| 102 SearchIPCRouter::~SearchIPCRouter() {} | 104 SearchIPCRouter::~SearchIPCRouter() = default; |
| 103 | 105 |
| 104 void SearchIPCRouter::OnNavigationEntryCommitted() { | 106 void SearchIPCRouter::OnNavigationEntryCommitted() { |
| 105 ++commit_counter_; | 107 ++commit_counter_; |
| 106 search_box()->SetPageSequenceNumber(commit_counter_); | 108 search_box()->SetPageSequenceNumber(commit_counter_); |
| 107 } | 109 } |
| 108 | 110 |
| 109 void SearchIPCRouter::DetermineIfPageSupportsInstant() { | 111 void SearchIPCRouter::DetermineIfPageSupportsInstant() { |
| 110 search_box()->DetermineIfPageSupportsInstant(); | 112 search_box()->DetermineIfPageSupportsInstant(); |
| 111 } | 113 } |
| 112 | 114 |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 | 339 |
| 338 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) { | 340 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) { |
| 339 DCHECK(delegate); | 341 DCHECK(delegate); |
| 340 delegate_ = delegate; | 342 delegate_ = delegate; |
| 341 } | 343 } |
| 342 | 344 |
| 343 void SearchIPCRouter::set_policy_for_testing(std::unique_ptr<Policy> policy) { | 345 void SearchIPCRouter::set_policy_for_testing(std::unique_ptr<Policy> policy) { |
| 344 DCHECK(policy); | 346 DCHECK(policy); |
| 345 policy_ = std::move(policy); | 347 policy_ = std::move(policy); |
| 346 } | 348 } |
| OLD | NEW |