Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / search / search_ipc_router.cc
blob72598b24e363ebc75b62b4870f63050fb80fafbb
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/search/search_ipc_router.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/search/search.h"
9 #include "chrome/common/render_messages.h"
10 #include "components/search/search.h"
11 #include "content/public/browser/navigation_details.h"
12 #include "content/public/browser/web_contents.h"
14 namespace {
16 bool IsProviderValid(const base::string16& provider) {
17 // Only allow string of 8 alphanumeric characters or less as providers.
18 // The empty string is considered valid and should be treated as if no
19 // provider were specified.
20 if (provider.length() > 8)
21 return false;
22 for (base::string16::const_iterator it = provider.begin();
23 it != provider.end(); ++it) {
24 if (!base::IsAsciiAlpha(*it) && !base::IsAsciiDigit(*it))
25 return false;
27 return true;
30 } // namespace
32 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
33 Delegate* delegate, scoped_ptr<Policy> policy)
34 : WebContentsObserver(web_contents),
35 delegate_(delegate),
36 policy_(policy.Pass()),
37 commit_counter_(0),
38 is_active_tab_(false) {
39 DCHECK(web_contents);
40 DCHECK(delegate);
41 DCHECK(policy_.get());
44 SearchIPCRouter::~SearchIPCRouter() {}
46 void SearchIPCRouter::OnNavigationEntryCommitted() {
47 ++commit_counter_;
48 Send(new ChromeViewMsg_SetPageSequenceNumber(routing_id(), commit_counter_));
51 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
52 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
55 void SearchIPCRouter::SendChromeIdentityCheckResult(
56 const base::string16& identity,
57 bool identity_match) {
58 if (!policy_->ShouldProcessChromeIdentityCheck())
59 return;
61 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity,
62 identity_match));
65 void SearchIPCRouter::SendHistorySyncCheckResult(bool sync_history) {
66 if (!policy_->ShouldProcessHistorySyncCheck())
67 return;
69 Send(new ChromeViewMsg_HistorySyncCheckResult(routing_id(), sync_history));
72 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
73 if (!policy_->ShouldSendSetPromoInformation())
74 return;
76 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
77 is_app_launcher_enabled));
80 void SearchIPCRouter::SetDisplayInstantResults() {
81 if (!policy_->ShouldSendSetDisplayInstantResults())
82 return;
84 bool is_search_results_page = !search::GetSearchTerms(web_contents()).empty();
85 bool display_instant_results =
86 is_search_results_page ? search::ShouldPrefetchSearchResultsOnSRP()
87 : search::ShouldPrefetchSearchResults();
88 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
89 routing_id(), display_instant_results));
92 void SearchIPCRouter::SetSuggestionToPrefetch(
93 const InstantSuggestion& suggestion) {
94 if (!policy_->ShouldSendSetSuggestionToPrefetch())
95 return;
97 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
98 suggestion));
101 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin) {
102 if (!policy_->ShouldSendSetOmniboxStartMargin())
103 return;
105 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin));
108 void SearchIPCRouter::SetInputInProgress(bool input_in_progress) {
109 if (!policy_->ShouldSendSetInputInProgress(is_active_tab_))
110 return;
112 Send(new ChromeViewMsg_SearchBoxSetInputInProgress(routing_id(),
113 input_in_progress));
116 void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state,
117 OmniboxFocusChangeReason reason) {
118 if (!policy_->ShouldSendOmniboxFocusChanged())
119 return;
121 Send(new ChromeViewMsg_SearchBoxFocusChanged(routing_id(), state, reason));
124 void SearchIPCRouter::SendMostVisitedItems(
125 const std::vector<InstantMostVisitedItem>& items) {
126 if (!policy_->ShouldSendMostVisitedItems())
127 return;
129 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
132 void SearchIPCRouter::SendThemeBackgroundInfo(
133 const ThemeBackgroundInfo& theme_info) {
134 if (!policy_->ShouldSendThemeBackgroundInfo())
135 return;
137 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
140 void SearchIPCRouter::ToggleVoiceSearch() {
141 if (!policy_->ShouldSendToggleVoiceSearch())
142 return;
144 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
147 void SearchIPCRouter::Submit(const base::string16& text,
148 const EmbeddedSearchRequestParams& params) {
149 if (!policy_->ShouldSubmitQuery())
150 return;
152 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text, params));
155 void SearchIPCRouter::OnTabActivated() {
156 is_active_tab_ = true;
159 void SearchIPCRouter::OnTabDeactivated() {
160 is_active_tab_ = false;
163 bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
164 if (IPC_MESSAGE_CLASS(message) != ChromeMsgStart)
165 return false;
167 Profile* profile =
168 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
169 if (!search::IsRenderedInInstantProcess(web_contents(), profile))
170 return false;
172 bool handled = true;
173 IPC_BEGIN_MESSAGE_MAP(SearchIPCRouter, message)
174 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
175 OnInstantSupportDetermined)
176 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetVoiceSearchSupported,
177 OnVoiceSearchSupportDetermined)
178 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox);
179 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
180 OnSearchBoxNavigate);
181 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
182 OnDeleteMostVisitedItem);
183 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
184 OnUndoMostVisitedDeletion);
185 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
186 OnUndoAllMostVisitedDeletions);
187 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent, OnLogEvent);
188 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogMostVisitedImpression,
189 OnLogMostVisitedImpression);
190 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogMostVisitedNavigation,
191 OnLogMostVisitedNavigation);
192 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PasteAndOpenDropdown,
193 OnPasteAndOpenDropDown);
194 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_HistorySyncCheck,
195 OnHistorySyncCheck);
196 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck,
197 OnChromeIdentityCheck);
198 IPC_MESSAGE_UNHANDLED(handled = false)
199 IPC_END_MESSAGE_MAP()
200 return handled;
203 void SearchIPCRouter::OnInstantSupportDetermined(int page_seq_no,
204 bool instant_support) const {
205 if (page_seq_no != commit_counter_)
206 return;
208 delegate_->OnInstantSupportDetermined(instant_support);
211 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
212 int page_seq_no,
213 bool supports_voice_search) const {
214 if (page_seq_no != commit_counter_)
215 return;
217 delegate_->OnInstantSupportDetermined(true);
218 if (!policy_->ShouldProcessSetVoiceSearchSupport())
219 return;
221 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
224 void SearchIPCRouter::OnFocusOmnibox(int page_seq_no,
225 OmniboxFocusState state) const {
226 if (page_seq_no != commit_counter_)
227 return;
229 delegate_->OnInstantSupportDetermined(true);
230 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
231 return;
233 delegate_->FocusOmnibox(state);
236 void SearchIPCRouter::OnSearchBoxNavigate(
237 int page_seq_no,
238 const GURL& url,
239 WindowOpenDisposition disposition,
240 bool is_most_visited_item_url) const {
241 if (page_seq_no != commit_counter_)
242 return;
244 delegate_->OnInstantSupportDetermined(true);
245 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
246 return;
248 delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
251 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_seq_no,
252 const GURL& url) const {
253 if (page_seq_no != commit_counter_)
254 return;
256 delegate_->OnInstantSupportDetermined(true);
257 if (!policy_->ShouldProcessDeleteMostVisitedItem())
258 return;
260 delegate_->OnDeleteMostVisitedItem(url);
263 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_seq_no,
264 const GURL& url) const {
265 if (page_seq_no != commit_counter_)
266 return;
268 delegate_->OnInstantSupportDetermined(true);
269 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
270 return;
272 delegate_->OnUndoMostVisitedDeletion(url);
275 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_seq_no) const {
276 if (page_seq_no != commit_counter_)
277 return;
279 delegate_->OnInstantSupportDetermined(true);
280 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
281 return;
283 delegate_->OnUndoAllMostVisitedDeletions();
286 void SearchIPCRouter::OnLogEvent(int page_seq_no,
287 NTPLoggingEventType event,
288 base::TimeDelta time) const {
289 if (page_seq_no != commit_counter_)
290 return;
292 delegate_->OnInstantSupportDetermined(true);
293 if (!policy_->ShouldProcessLogEvent())
294 return;
296 delegate_->OnLogEvent(event, time);
299 void SearchIPCRouter::OnLogMostVisitedImpression(
300 int page_seq_no, int position, const base::string16& provider) const {
301 if (page_seq_no != commit_counter_ || !IsProviderValid(provider))
302 return;
304 delegate_->OnInstantSupportDetermined(true);
305 // Logging impressions is controlled by the same policy as logging events.
306 if (!policy_->ShouldProcessLogEvent())
307 return;
309 delegate_->OnLogMostVisitedImpression(position, provider);
312 void SearchIPCRouter::OnLogMostVisitedNavigation(
313 int page_seq_no, int position, const base::string16& provider) const {
314 if (page_seq_no != commit_counter_ || !IsProviderValid(provider))
315 return;
317 delegate_->OnInstantSupportDetermined(true);
318 // Logging navigations is controlled by the same policy as logging events.
319 if (!policy_->ShouldProcessLogEvent())
320 return;
322 delegate_->OnLogMostVisitedNavigation(position, provider);
325 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_seq_no,
326 const base::string16& text) const {
327 if (page_seq_no != commit_counter_)
328 return;
330 delegate_->OnInstantSupportDetermined(true);
331 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
332 return;
334 delegate_->PasteIntoOmnibox(text);
337 void SearchIPCRouter::OnChromeIdentityCheck(
338 int page_seq_no,
339 const base::string16& identity) const {
340 if (page_seq_no != commit_counter_)
341 return;
343 delegate_->OnInstantSupportDetermined(true);
344 if (!policy_->ShouldProcessChromeIdentityCheck())
345 return;
347 delegate_->OnChromeIdentityCheck(identity);
350 void SearchIPCRouter::OnHistorySyncCheck(int page_seq_no) const {
351 if (page_seq_no != commit_counter_)
352 return;
354 delegate_->OnInstantSupportDetermined(true);
355 if (!policy_->ShouldProcessHistorySyncCheck())
356 return;
358 delegate_->OnHistorySyncCheck();
361 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) {
362 DCHECK(delegate);
363 delegate_ = delegate;
366 void SearchIPCRouter::set_policy_for_testing(scoped_ptr<Policy> policy) {
367 DCHECK(policy.get());
368 policy_.reset(policy.release());