File: search_ipc_router.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (346 lines) | stat: -rw-r--r-- 10,548 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/search/search_ipc_router.h"

#include <utility>

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/common/render_messages.h"
#include "components/search/search.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/associated_interface_provider.h"
#include "content/public/common/child_process_host.h"

namespace {

bool IsRenderedInInstantProcess(content::WebContents* web_contents) {
  // TODO(tibell): Instead of rejecting messages check at connection time if we
  // want to talk to the render frame or not. Later, in an out-of-process iframe
  // world, the IsRenderedInInstantProcess check will have to be done, as it's
  // based on the RenderView.
  Profile* profile =
      Profile::FromBrowserContext(web_contents->GetBrowserContext());
  return search::IsRenderedInInstantProcess(web_contents, profile);
}

class SearchBoxClientFactoryImpl
    : public SearchIPCRouter::SearchBoxClientFactory {
 public:
  // The |web_contents| must outlive this object.
  explicit SearchBoxClientFactoryImpl(content::WebContents* web_contents)
      : web_contents_(web_contents),
        last_connected_rfh_(
            std::make_pair(content::ChildProcessHost::kInvalidUniqueID,
                           MSG_ROUTING_NONE)) {}
  chrome::mojom::SearchBox* GetSearchBox() override;

 private:
  void OnConnectionError();

  content::WebContents* web_contents_;
  chrome::mojom::SearchBoxAssociatedPtr search_box_;

  // The proccess ID and routing ID of the last connected main frame. May be
  // (ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE) if we haven't
  // connected yet.
  std::pair<int, int> last_connected_rfh_;

  DISALLOW_COPY_AND_ASSIGN(SearchBoxClientFactoryImpl);
};

chrome::mojom::SearchBox* SearchBoxClientFactoryImpl::GetSearchBox() {
  content::RenderFrameHost* frame = web_contents_->GetMainFrame();
  auto id = std::make_pair(frame->GetProcess()->GetID(), frame->GetRoutingID());
  // Avoid reconnecting repeatedly to the same RenderFrameHost for performance
  // reasons.
  if (id != last_connected_rfh_) {
    if (IsRenderedInInstantProcess(web_contents_)) {
      frame->GetRemoteAssociatedInterfaces()->GetInterface(&search_box_);
      search_box_.set_connection_error_handler(
          base::Bind(&SearchBoxClientFactoryImpl::OnConnectionError,
                     base::Unretained(this)));
    } else {
      // Renderer is not an instant process. We'll create a connection that
      // drops all messages.
      mojo::GetIsolatedProxy(&search_box_);
    }
    last_connected_rfh_ = id;
  }
  return search_box_.get();
}

void SearchBoxClientFactoryImpl::OnConnectionError() {
  search_box_.reset();
  last_connected_rfh_ = std::make_pair(
      content::ChildProcessHost::kInvalidUniqueID,
      MSG_ROUTING_NONE);
}

}  // namespace

SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
                                 Delegate* delegate,
                                 std::unique_ptr<Policy> policy)
    : WebContentsObserver(web_contents),
      delegate_(delegate),
      policy_(std::move(policy)),
      commit_counter_(0),
      is_active_tab_(false),
      bindings_(web_contents, this),
      search_box_client_factory_(new SearchBoxClientFactoryImpl(web_contents)) {
  DCHECK(web_contents);
  DCHECK(delegate);
  DCHECK(policy_.get());
}

SearchIPCRouter::~SearchIPCRouter() {}

void SearchIPCRouter::OnNavigationEntryCommitted() {
  ++commit_counter_;
  search_box()->SetPageSequenceNumber(commit_counter_);
}

void SearchIPCRouter::DetermineIfPageSupportsInstant() {
  search_box()->DetermineIfPageSupportsInstant();
}

void SearchIPCRouter::SendChromeIdentityCheckResult(
    const base::string16& identity,
    bool identity_match) {
  if (!policy_->ShouldProcessChromeIdentityCheck())
    return;

  search_box()->ChromeIdentityCheckResult(identity, identity_match);
}

void SearchIPCRouter::SendHistorySyncCheckResult(bool sync_history) {
  if (!policy_->ShouldProcessHistorySyncCheck())
    return;

  search_box()->HistorySyncCheckResult(sync_history);
}

void SearchIPCRouter::SetSuggestionToPrefetch(
    const InstantSuggestion& suggestion) {
  if (!policy_->ShouldSendSetSuggestionToPrefetch())
    return;

  search_box()->SetSuggestionToPrefetch(suggestion);
}

void SearchIPCRouter::SetInputInProgress(bool input_in_progress) {
  if (!policy_->ShouldSendSetInputInProgress(is_active_tab_))
    return;

  search_box()->SetInputInProgress(input_in_progress);
}

void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state,
                                          OmniboxFocusChangeReason reason) {
  if (!policy_->ShouldSendOmniboxFocusChanged())
    return;

  search_box()->FocusChanged(state, reason);
}

void SearchIPCRouter::SendMostVisitedItems(
    const std::vector<InstantMostVisitedItem>& items) {
  if (!policy_->ShouldSendMostVisitedItems())
    return;

  search_box()->MostVisitedChanged(items);
}

void SearchIPCRouter::SendThemeBackgroundInfo(
    const ThemeBackgroundInfo& theme_info) {
  if (!policy_->ShouldSendThemeBackgroundInfo())
    return;

  search_box()->ThemeChanged(theme_info);
}

void SearchIPCRouter::Submit(const base::string16& text,
                             const EmbeddedSearchRequestParams& params) {
  if (!policy_->ShouldSubmitQuery())
    return;

  search_box()->Submit(text, params);
}

void SearchIPCRouter::OnTabActivated() {
  is_active_tab_ = true;
}

void SearchIPCRouter::OnTabDeactivated() {
  is_active_tab_ = false;
}

void SearchIPCRouter::InstantSupportDetermined(int page_seq_no,
                                               bool instant_support) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(instant_support);
}

void SearchIPCRouter::FocusOmnibox(int page_seq_no, OmniboxFocusState state) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
    return;

  delegate_->FocusOmnibox(state);
}

void SearchIPCRouter::SearchBoxDeleteMostVisitedItem(int page_seq_no,
                                                     const GURL& url) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  if (!policy_->ShouldProcessDeleteMostVisitedItem())
    return;

  delegate_->OnDeleteMostVisitedItem(url);
}

void SearchIPCRouter::SearchBoxUndoMostVisitedDeletion(int page_seq_no,
                                                       const GURL& url) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  if (!policy_->ShouldProcessUndoMostVisitedDeletion())
    return;

  delegate_->OnUndoMostVisitedDeletion(url);
}

void SearchIPCRouter::SearchBoxUndoAllMostVisitedDeletions(int page_seq_no) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
    return;

  delegate_->OnUndoAllMostVisitedDeletions();
}

void SearchIPCRouter::LogEvent(int page_seq_no,
                               NTPLoggingEventType event,
                               base::TimeDelta time) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  if (!policy_->ShouldProcessLogEvent())
    return;

  delegate_->OnLogEvent(event, time);
}

void SearchIPCRouter::LogMostVisitedImpression(
    int page_seq_no,
    int position,
    ntp_tiles::NTPTileSource tile_source) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  // Logging impressions is controlled by the same policy as logging events.
  if (!policy_->ShouldProcessLogEvent())
    return;

  delegate_->OnLogMostVisitedImpression(position, tile_source);
}

void SearchIPCRouter::LogMostVisitedNavigation(
    int page_seq_no,
    int position,
    ntp_tiles::NTPTileSource tile_source) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  // Logging navigations is controlled by the same policy as logging events.
  if (!policy_->ShouldProcessLogEvent())
    return;

  delegate_->OnLogMostVisitedNavigation(position, tile_source);
}

void SearchIPCRouter::PasteAndOpenDropdown(int page_seq_no,
                                           const base::string16& text) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
    return;

  delegate_->PasteIntoOmnibox(text);
}

void SearchIPCRouter::ChromeIdentityCheck(int page_seq_no,
                                          const base::string16& identity) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  if (!policy_->ShouldProcessChromeIdentityCheck())
    return;

  delegate_->OnChromeIdentityCheck(identity);
}

void SearchIPCRouter::HistorySyncCheck(int page_seq_no) {
  if (!IsRenderedInInstantProcess(web_contents()))
    return;
  if (page_seq_no != commit_counter_)
    return;

  delegate_->OnInstantSupportDetermined(true);
  if (!policy_->ShouldProcessHistorySyncCheck())
    return;

  delegate_->OnHistorySyncCheck();
}

void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) {
  DCHECK(delegate);
  delegate_ = delegate;
}

void SearchIPCRouter::set_policy_for_testing(std::unique_ptr<Policy> policy) {
  DCHECK(policy);
  policy_ = std::move(policy);
}