Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chrome/browser/ui/search/search_ipc_router.cc

Issue 2688453002: NTP: simplify mojo connection setup (Closed)
Patch Set: Restore IsRenderedInInstantProcess check Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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. Later, in an out-of-process iframe
25 // world, the IsRenderedInInstantProcess check will have to be done, as it's 25 // world, the IsRenderedInInstantProcess check will have to be done, as it's
26 // based on the RenderView. 26 // based on the RenderView.
Marc Treib 2017/02/08 10:00:36 Does this describe a possible optimization that wo
tibell 2017/02/09 03:35:28 I've updated the comment to what I think we should
27 Profile* profile = 27 Profile* profile =
28 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 28 Profile::FromBrowserContext(web_contents->GetBrowserContext());
29 return search::IsRenderedInInstantProcess(web_contents, profile); 29 return search::IsRenderedInInstantProcess(web_contents, profile);
30 } 30 }
31 31
32 class SearchBoxClientFactoryImpl 32 class SearchBoxClientFactoryImpl
33 : public SearchIPCRouter::SearchBoxClientFactory { 33 : public SearchIPCRouter::SearchBoxClientFactory,
34 public chrome::mojom::NTPConnector {
34 public: 35 public:
35 // The |web_contents| must outlive this object. 36 // |web_contents| and |binding| must outlive this object.
36 explicit SearchBoxClientFactoryImpl(content::WebContents* web_contents) 37 SearchBoxClientFactoryImpl(
38 content::WebContents* web_contents,
39 mojo::AssociatedBinding<chrome::mojom::Instant>* binding)
37 : web_contents_(web_contents), 40 : web_contents_(web_contents),
38 last_connected_rfh_( 41 binding_(binding),
39 std::make_pair(content::ChildProcessHost::kInvalidUniqueID, 42 bindings_(web_contents, this) {
40 MSG_ROUTING_NONE)) {} 43 DCHECK(web_contents);
41 chrome::mojom::SearchBox* GetSearchBox() override; 44 DCHECK(binding);
45 // Before we are connected to a frame we throw away all messages.
46 mojo::GetIsolatedProxy(&search_box_);
47 }
48
49 chrome::mojom::SearchBox* GetSearchBox() override {
50 return search_box_.get();
51 }
42 52
43 private: 53 private:
44 void OnConnectionError(); 54 void Connect(chrome::mojom::InstantAssociatedRequest request,
55 chrome::mojom::SearchBoxAssociatedPtrInfo search_box) override;
45 56
46 content::WebContents* web_contents_; 57 content::WebContents* web_contents_;
58
59 // An interface used to push updates to the frame that connected to us. Before
60 // we've been connected to a frame, messages sent on this interface goes into
Marc Treib 2017/02/08 10:00:36 nit: s/goes/go/
tibell 2017/02/09 03:35:28 Done.
61 // the void.
47 chrome::mojom::SearchBoxAssociatedPtr search_box_; 62 chrome::mojom::SearchBoxAssociatedPtr search_box_;
48 63
49 // The proccess ID and routing ID of the last connected main frame. May be 64 // Used to bind incomming interface requests to the implementation, which
dcheng 2017/02/08 09:01:39 Nit: incomming -> incoming
tibell 2017/02/09 03:35:28 Done.
50 // (ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE) if we haven't 65 // lives in SearchIPCRouter.
51 // connected yet. 66 mojo::AssociatedBinding<chrome::mojom::Instant>* binding_;
52 std::pair<int, int> last_connected_rfh_; 67
68 // Binding used to listen to connection requests.
69 content::WebContentsFrameBindingSet<chrome::mojom::NTPConnector> bindings_;
53 70
54 DISALLOW_COPY_AND_ASSIGN(SearchBoxClientFactoryImpl); 71 DISALLOW_COPY_AND_ASSIGN(SearchBoxClientFactoryImpl);
55 }; 72 };
56 73
57 chrome::mojom::SearchBox* SearchBoxClientFactoryImpl::GetSearchBox() { 74 void SearchBoxClientFactoryImpl::Connect(
58 content::RenderFrameHost* frame = web_contents_->GetMainFrame(); 75 chrome::mojom::InstantAssociatedRequest request,
59 auto id = std::make_pair(frame->GetProcess()->GetID(), frame->GetRoutingID()); 76 chrome::mojom::SearchBoxAssociatedPtrInfo search_box) {
60 // Avoid reconnecting repeatedly to the same RenderFrameHost for performance 77 if (!IsRenderedInInstantProcess(web_contents_)) {
61 // reasons. 78 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 } 79 }
75 return search_box_.get(); 80 binding_->Bind(std::move(request));
76 } 81 search_box_.Bind(std::move(search_box));
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 } 82 }
84 83
85 } // namespace 84 } // namespace
86 85
87 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents, 86 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
88 Delegate* delegate, 87 Delegate* delegate,
89 std::unique_ptr<Policy> policy) 88 std::unique_ptr<Policy> policy)
90 : WebContentsObserver(web_contents), 89 : WebContentsObserver(web_contents),
91 delegate_(delegate), 90 delegate_(delegate),
92 policy_(std::move(policy)), 91 policy_(std::move(policy)),
93 commit_counter_(0), 92 commit_counter_(0),
94 is_active_tab_(false), 93 is_active_tab_(false),
95 bindings_(web_contents, this), 94 binding_(this),
96 search_box_client_factory_(new SearchBoxClientFactoryImpl(web_contents)) { 95 search_box_client_factory_(
96 new SearchBoxClientFactoryImpl(web_contents, &binding_)) {
97 DCHECK(web_contents); 97 DCHECK(web_contents);
98 DCHECK(delegate); 98 DCHECK(delegate);
99 DCHECK(policy_.get()); 99 DCHECK(policy_.get());
100 } 100 }
101 101
102 SearchIPCRouter::~SearchIPCRouter() {} 102 SearchIPCRouter::~SearchIPCRouter() = default;
103 103
104 void SearchIPCRouter::OnNavigationEntryCommitted() { 104 void SearchIPCRouter::OnNavigationEntryCommitted() {
105 ++commit_counter_; 105 ++commit_counter_;
106 search_box()->SetPageSequenceNumber(commit_counter_); 106 search_box()->SetPageSequenceNumber(commit_counter_);
107 } 107 }
108 108
109 void SearchIPCRouter::DetermineIfPageSupportsInstant() { 109 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
110 search_box()->DetermineIfPageSupportsInstant(); 110 search_box()->DetermineIfPageSupportsInstant();
111 } 111 }
112 112
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 337
338 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) { 338 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) {
339 DCHECK(delegate); 339 DCHECK(delegate);
340 delegate_ = delegate; 340 delegate_ = delegate;
341 } 341 }
342 342
343 void SearchIPCRouter::set_policy_for_testing(std::unique_ptr<Policy> policy) { 343 void SearchIPCRouter::set_policy_for_testing(std::unique_ptr<Policy> policy) {
344 DCHECK(policy); 344 DCHECK(policy);
345 policy_ = std::move(policy); 345 policy_ = std::move(policy);
346 } 346 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698