Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / search / search_ipc_router.cc
blob1c06b1c3704a8e89f4877d9c494bd6a3133e36a8
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 "content/public/browser/navigation_details.h"
11 #include "content/public/browser/web_contents.h"
13 namespace {
15 bool IsProviderValid(const base::string16& provider) {
16 // Only allow string of 8 alphanumeric characters or less as providers.
17 // The empty string is considered valid and should be treated as if no
18 // provider were specified.
19 if (provider.length() > 8)
20 return false;
21 for (base::string16::const_iterator it = provider.begin();
22 it != provider.end(); ++it) {
23 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it))
24 return false;
26 return true;
29 } // namespace
31 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
32 Delegate* delegate, scoped_ptr<Policy> policy)
33 : WebContentsObserver(web_contents),
34 delegate_(delegate),
35 policy_(policy.Pass()),
36 commit_counter_(0),
37 is_active_tab_(false) {
38 DCHECK(web_contents);
39 DCHECK(delegate);
40 DCHECK(policy_.get());
43 SearchIPCRouter::~SearchIPCRouter() {}
45 void SearchIPCRouter::OnNavigationEntryCommitted() {
46 ++commit_counter_;
47 Send(new ChromeViewMsg_SetPageSequenceNumber(routing_id(), commit_counter_));
50 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
51 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
54 void SearchIPCRouter::SendChromeIdentityCheckResult(
55 const base::string16& identity,
56 bool identity_match) {
57 if (!policy_->ShouldProcessChromeIdentityCheck())
58 return;
60 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity,
61 identity_match));
64 void SearchIPCRouter::SendHistorySyncCheckResult(bool sync_history) {
65 if (!policy_->ShouldProcessHistorySyncCheck())
66 return;
68 Send(new ChromeViewMsg_HistorySyncCheckResult(routing_id(), sync_history));
71 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
72 if (!policy_->ShouldSendSetPromoInformation())
73 return;
75 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
76 is_app_launcher_enabled));
79 void SearchIPCRouter::SetDisplayInstantResults() {
80 if (!policy_->ShouldSendSetDisplayInstantResults())
81 return;
83 bool is_search_results_page = !chrome::GetSearchTerms(web_contents()).empty();
84 bool display_instant_results = is_search_results_page ?
85 chrome::ShouldPrefetchSearchResultsOnSRP() :
86 chrome::ShouldPrefetchSearchResults();
87 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
88 routing_id(), display_instant_results));
91 void SearchIPCRouter::SetSuggestionToPrefetch(
92 const InstantSuggestion& suggestion) {
93 if (!policy_->ShouldSendSetSuggestionToPrefetch())
94 return;
96 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
97 suggestion));
100 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin) {
101 if (!policy_->ShouldSendSetOmniboxStartMargin())
102 return;
104 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin));
107 void SearchIPCRouter::SetInputInProgress(bool input_in_progress) {
108 if (!policy_->ShouldSendSetInputInProgress(is_active_tab_))
109 return;
111 Send(new ChromeViewMsg_SearchBoxSetInputInProgress(routing_id(),
112 input_in_progress));
115 void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state,
116 OmniboxFocusChangeReason reason) {
117 if (!policy_->ShouldSendOmniboxFocusChanged())
118 return;
120 Send(new ChromeViewMsg_SearchBoxFocusChanged(routing_id(), state, reason));
123 void SearchIPCRouter::SendMostVisitedItems(
124 const std::vector<InstantMostVisitedItem>& items) {
125 if (!policy_->ShouldSendMostVisitedItems())
126 return;
128 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
131 void SearchIPCRouter::SendThemeBackgroundInfo(
132 const ThemeBackgroundInfo& theme_info) {
133 if (!policy_->ShouldSendThemeBackgroundInfo())
134 return;
136 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
139 void SearchIPCRouter::ToggleVoiceSearch() {
140 if (!policy_->ShouldSendToggleVoiceSearch())
141 return;
143 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
146 void SearchIPCRouter::Submit(const base::string16& text,
147 const EmbeddedSearchRequestParams& params) {
148 if (!policy_->ShouldSubmitQuery())
149 return;
151 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text, params));
154 void SearchIPCRouter::OnTabActivated() {
155 is_active_tab_ = true;
158 void SearchIPCRouter::OnTabDeactivated() {
159 is_active_tab_ = false;
162 bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
163 if (IPC_MESSAGE_CLASS(message) != ChromeMsgStart)
164 return false;
166 Profile* profile =
167 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
168 if (!chrome::IsRenderedInInstantProcess(web_contents(), profile))
169 return false;
171 bool handled = true;
172 IPC_BEGIN_MESSAGE_MAP(SearchIPCRouter, message)
173 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
174 OnInstantSupportDetermined)
175 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetVoiceSearchSupported,
176 OnVoiceSearchSupportDetermined)
177 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox);
178 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
179 OnSearchBoxNavigate);
180 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
181 OnDeleteMostVisitedItem);
182 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
183 OnUndoMostVisitedDeletion);
184 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
185 OnUndoAllMostVisitedDeletions);
186 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent, OnLogEvent);
187 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogMostVisitedImpression,
188 OnLogMostVisitedImpression);
189 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogMostVisitedNavigation,
190 OnLogMostVisitedNavigation);
191 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PasteAndOpenDropdown,
192 OnPasteAndOpenDropDown);
193 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_HistorySyncCheck,
194 OnHistorySyncCheck);
195 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck,
196 OnChromeIdentityCheck);
197 IPC_MESSAGE_UNHANDLED(handled = false)
198 IPC_END_MESSAGE_MAP()
199 return handled;
202 void SearchIPCRouter::OnInstantSupportDetermined(int page_seq_no,
203 bool instant_support) const {
204 if (page_seq_no != commit_counter_)
205 return;
207 delegate_->OnInstantSupportDetermined(instant_support);
210 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
211 int page_seq_no,
212 bool supports_voice_search) const {
213 if (page_seq_no != commit_counter_)
214 return;
216 delegate_->OnInstantSupportDetermined(true);
217 if (!policy_->ShouldProcessSetVoiceSearchSupport())
218 return;
220 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
223 void SearchIPCRouter::OnFocusOmnibox(int page_seq_no,
224 OmniboxFocusState state) const {
225 if (page_seq_no != commit_counter_)
226 return;
228 delegate_->OnInstantSupportDetermined(true);
229 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
230 return;
232 delegate_->FocusOmnibox(state);
235 void SearchIPCRouter::OnSearchBoxNavigate(
236 int page_seq_no,
237 const GURL& url,
238 WindowOpenDisposition disposition,
239 bool is_most_visited_item_url) const {
240 if (page_seq_no != commit_counter_)
241 return;
243 delegate_->OnInstantSupportDetermined(true);
244 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
245 return;
247 delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
250 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_seq_no,
251 const GURL& url) const {
252 if (page_seq_no != commit_counter_)
253 return;
255 delegate_->OnInstantSupportDetermined(true);
256 if (!policy_->ShouldProcessDeleteMostVisitedItem())
257 return;
259 delegate_->OnDeleteMostVisitedItem(url);
262 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_seq_no,
263 const GURL& url) const {
264 if (page_seq_no != commit_counter_)
265 return;
267 delegate_->OnInstantSupportDetermined(true);
268 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
269 return;
271 delegate_->OnUndoMostVisitedDeletion(url);
274 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_seq_no) const {
275 if (page_seq_no != commit_counter_)
276 return;
278 delegate_->OnInstantSupportDetermined(true);
279 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
280 return;
282 delegate_->OnUndoAllMostVisitedDeletions();
285 void SearchIPCRouter::OnLogEvent(int page_seq_no,
286 NTPLoggingEventType event,
287 base::TimeDelta time) const {
288 if (page_seq_no != commit_counter_)
289 return;
291 delegate_->OnInstantSupportDetermined(true);
292 if (!policy_->ShouldProcessLogEvent())
293 return;
295 delegate_->OnLogEvent(event, time);
298 void SearchIPCRouter::OnLogMostVisitedImpression(
299 int page_seq_no, int position, const base::string16& provider) const {
300 if (page_seq_no != commit_counter_ || !IsProviderValid(provider))
301 return;
303 delegate_->OnInstantSupportDetermined(true);
304 // Logging impressions is controlled by the same policy as logging events.
305 if (!policy_->ShouldProcessLogEvent())
306 return;
308 delegate_->OnLogMostVisitedImpression(position, provider);
311 void SearchIPCRouter::OnLogMostVisitedNavigation(
312 int page_seq_no, int position, const base::string16& provider) const {
313 if (page_seq_no != commit_counter_ || !IsProviderValid(provider))
314 return;
316 delegate_->OnInstantSupportDetermined(true);
317 // Logging navigations is controlled by the same policy as logging events.
318 if (!policy_->ShouldProcessLogEvent())
319 return;
321 delegate_->OnLogMostVisitedNavigation(position, provider);
324 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_seq_no,
325 const base::string16& text) const {
326 if (page_seq_no != commit_counter_)
327 return;
329 delegate_->OnInstantSupportDetermined(true);
330 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
331 return;
333 delegate_->PasteIntoOmnibox(text);
336 void SearchIPCRouter::OnChromeIdentityCheck(
337 int page_seq_no,
338 const base::string16& identity) const {
339 if (page_seq_no != commit_counter_)
340 return;
342 delegate_->OnInstantSupportDetermined(true);
343 if (!policy_->ShouldProcessChromeIdentityCheck())
344 return;
346 delegate_->OnChromeIdentityCheck(identity);
349 void SearchIPCRouter::OnHistorySyncCheck(int page_seq_no) const {
350 if (page_seq_no != commit_counter_)
351 return;
353 delegate_->OnInstantSupportDetermined(true);
354 if (!policy_->ShouldProcessHistorySyncCheck())
355 return;
357 delegate_->OnHistorySyncCheck();
360 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) {
361 DCHECK(delegate);
362 delegate_ = delegate;
365 void SearchIPCRouter::set_policy_for_testing(scoped_ptr<Policy> policy) {
366 DCHECK(policy.get());
367 policy_.reset(policy.release());