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"
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)
22 for (base::string16::const_iterator it
= provider
.begin();
23 it
!= provider
.end(); ++it
) {
24 if (!base::IsAsciiAlpha(*it
) && !base::IsAsciiDigit(*it
))
32 SearchIPCRouter::SearchIPCRouter(content::WebContents
* web_contents
,
33 Delegate
* delegate
, scoped_ptr
<Policy
> policy
)
34 : WebContentsObserver(web_contents
),
36 policy_(policy
.Pass()),
38 is_active_tab_(false) {
41 DCHECK(policy_
.get());
44 SearchIPCRouter::~SearchIPCRouter() {}
46 void SearchIPCRouter::OnNavigationEntryCommitted() {
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())
61 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity
,
65 void SearchIPCRouter::SendHistorySyncCheckResult(bool sync_history
) {
66 if (!policy_
->ShouldProcessHistorySyncCheck())
69 Send(new ChromeViewMsg_HistorySyncCheckResult(routing_id(), sync_history
));
72 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled
) {
73 if (!policy_
->ShouldSendSetPromoInformation())
76 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
77 is_app_launcher_enabled
));
80 void SearchIPCRouter::SetDisplayInstantResults() {
81 if (!policy_
->ShouldSendSetDisplayInstantResults())
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())
97 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
101 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin
) {
102 if (!policy_
->ShouldSendSetOmniboxStartMargin())
105 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin
));
108 void SearchIPCRouter::SetInputInProgress(bool input_in_progress
) {
109 if (!policy_
->ShouldSendSetInputInProgress(is_active_tab_
))
112 Send(new ChromeViewMsg_SearchBoxSetInputInProgress(routing_id(),
116 void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state
,
117 OmniboxFocusChangeReason reason
) {
118 if (!policy_
->ShouldSendOmniboxFocusChanged())
121 Send(new ChromeViewMsg_SearchBoxFocusChanged(routing_id(), state
, reason
));
124 void SearchIPCRouter::SendMostVisitedItems(
125 const std::vector
<InstantMostVisitedItem
>& items
) {
126 if (!policy_
->ShouldSendMostVisitedItems())
129 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items
));
132 void SearchIPCRouter::SendThemeBackgroundInfo(
133 const ThemeBackgroundInfo
& theme_info
) {
134 if (!policy_
->ShouldSendThemeBackgroundInfo())
137 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info
));
140 void SearchIPCRouter::ToggleVoiceSearch() {
141 if (!policy_
->ShouldSendToggleVoiceSearch())
144 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
147 void SearchIPCRouter::Submit(const base::string16
& text
,
148 const EmbeddedSearchRequestParams
& params
) {
149 if (!policy_
->ShouldSubmitQuery())
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
)
168 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
169 if (!search::IsRenderedInInstantProcess(web_contents(), profile
))
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
,
196 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck
,
197 OnChromeIdentityCheck
);
198 IPC_MESSAGE_UNHANDLED(handled
= false)
199 IPC_END_MESSAGE_MAP()
203 void SearchIPCRouter::OnInstantSupportDetermined(int page_seq_no
,
204 bool instant_support
) const {
205 if (page_seq_no
!= commit_counter_
)
208 delegate_
->OnInstantSupportDetermined(instant_support
);
211 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
213 bool supports_voice_search
) const {
214 if (page_seq_no
!= commit_counter_
)
217 delegate_
->OnInstantSupportDetermined(true);
218 if (!policy_
->ShouldProcessSetVoiceSearchSupport())
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_
)
229 delegate_
->OnInstantSupportDetermined(true);
230 if (!policy_
->ShouldProcessFocusOmnibox(is_active_tab_
))
233 delegate_
->FocusOmnibox(state
);
236 void SearchIPCRouter::OnSearchBoxNavigate(
239 WindowOpenDisposition disposition
,
240 bool is_most_visited_item_url
) const {
241 if (page_seq_no
!= commit_counter_
)
244 delegate_
->OnInstantSupportDetermined(true);
245 if (!policy_
->ShouldProcessNavigateToURL(is_active_tab_
))
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_
)
256 delegate_
->OnInstantSupportDetermined(true);
257 if (!policy_
->ShouldProcessDeleteMostVisitedItem())
260 delegate_
->OnDeleteMostVisitedItem(url
);
263 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_seq_no
,
264 const GURL
& url
) const {
265 if (page_seq_no
!= commit_counter_
)
268 delegate_
->OnInstantSupportDetermined(true);
269 if (!policy_
->ShouldProcessUndoMostVisitedDeletion())
272 delegate_
->OnUndoMostVisitedDeletion(url
);
275 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_seq_no
) const {
276 if (page_seq_no
!= commit_counter_
)
279 delegate_
->OnInstantSupportDetermined(true);
280 if (!policy_
->ShouldProcessUndoAllMostVisitedDeletions())
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_
)
292 delegate_
->OnInstantSupportDetermined(true);
293 if (!policy_
->ShouldProcessLogEvent())
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
))
304 delegate_
->OnInstantSupportDetermined(true);
305 // Logging impressions is controlled by the same policy as logging events.
306 if (!policy_
->ShouldProcessLogEvent())
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
))
317 delegate_
->OnInstantSupportDetermined(true);
318 // Logging navigations is controlled by the same policy as logging events.
319 if (!policy_
->ShouldProcessLogEvent())
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_
)
330 delegate_
->OnInstantSupportDetermined(true);
331 if (!policy_
->ShouldProcessPasteIntoOmnibox(is_active_tab_
))
334 delegate_
->PasteIntoOmnibox(text
);
337 void SearchIPCRouter::OnChromeIdentityCheck(
339 const base::string16
& identity
) const {
340 if (page_seq_no
!= commit_counter_
)
343 delegate_
->OnInstantSupportDetermined(true);
344 if (!policy_
->ShouldProcessChromeIdentityCheck())
347 delegate_
->OnChromeIdentityCheck(identity
);
350 void SearchIPCRouter::OnHistorySyncCheck(int page_seq_no
) const {
351 if (page_seq_no
!= commit_counter_
)
354 delegate_
->OnInstantSupportDetermined(true);
355 if (!policy_
->ShouldProcessHistorySyncCheck())
358 delegate_
->OnHistorySyncCheck();
361 void SearchIPCRouter::set_delegate_for_testing(Delegate
* delegate
) {
363 delegate_
= delegate
;
366 void SearchIPCRouter::set_policy_for_testing(scoped_ptr
<Policy
> policy
) {
367 DCHECK(policy
.get());
368 policy_
.reset(policy
.release());