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"
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)
21 for (base::string16::const_iterator it
= provider
.begin();
22 it
!= provider
.end(); ++it
) {
23 if (!IsAsciiAlpha(*it
) && !IsAsciiDigit(*it
))
31 SearchIPCRouter::SearchIPCRouter(content::WebContents
* web_contents
,
32 Delegate
* delegate
, scoped_ptr
<Policy
> policy
)
33 : WebContentsObserver(web_contents
),
35 policy_(policy
.Pass()),
37 is_active_tab_(false) {
40 DCHECK(policy_
.get());
43 SearchIPCRouter::~SearchIPCRouter() {}
45 void SearchIPCRouter::OnNavigationEntryCommitted() {
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())
60 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity
,
64 void SearchIPCRouter::SendHistorySyncCheckResult(bool sync_history
) {
65 if (!policy_
->ShouldProcessHistorySyncCheck())
68 Send(new ChromeViewMsg_HistorySyncCheckResult(routing_id(), sync_history
));
71 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled
) {
72 if (!policy_
->ShouldSendSetPromoInformation())
75 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
76 is_app_launcher_enabled
));
79 void SearchIPCRouter::SetDisplayInstantResults() {
80 if (!policy_
->ShouldSendSetDisplayInstantResults())
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())
96 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
100 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin
) {
101 if (!policy_
->ShouldSendSetOmniboxStartMargin())
104 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin
));
107 void SearchIPCRouter::SetInputInProgress(bool input_in_progress
) {
108 if (!policy_
->ShouldSendSetInputInProgress(is_active_tab_
))
111 Send(new ChromeViewMsg_SearchBoxSetInputInProgress(routing_id(),
115 void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state
,
116 OmniboxFocusChangeReason reason
) {
117 if (!policy_
->ShouldSendOmniboxFocusChanged())
120 Send(new ChromeViewMsg_SearchBoxFocusChanged(routing_id(), state
, reason
));
123 void SearchIPCRouter::SendMostVisitedItems(
124 const std::vector
<InstantMostVisitedItem
>& items
) {
125 if (!policy_
->ShouldSendMostVisitedItems())
128 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items
));
131 void SearchIPCRouter::SendThemeBackgroundInfo(
132 const ThemeBackgroundInfo
& theme_info
) {
133 if (!policy_
->ShouldSendThemeBackgroundInfo())
136 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info
));
139 void SearchIPCRouter::ToggleVoiceSearch() {
140 if (!policy_
->ShouldSendToggleVoiceSearch())
143 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
146 void SearchIPCRouter::Submit(const base::string16
& text
,
147 const EmbeddedSearchRequestParams
& params
) {
148 if (!policy_
->ShouldSubmitQuery())
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
)
167 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
168 if (!chrome::IsRenderedInInstantProcess(web_contents(), profile
))
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
,
195 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck
,
196 OnChromeIdentityCheck
);
197 IPC_MESSAGE_UNHANDLED(handled
= false)
198 IPC_END_MESSAGE_MAP()
202 void SearchIPCRouter::OnInstantSupportDetermined(int page_seq_no
,
203 bool instant_support
) const {
204 if (page_seq_no
!= commit_counter_
)
207 delegate_
->OnInstantSupportDetermined(instant_support
);
210 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
212 bool supports_voice_search
) const {
213 if (page_seq_no
!= commit_counter_
)
216 delegate_
->OnInstantSupportDetermined(true);
217 if (!policy_
->ShouldProcessSetVoiceSearchSupport())
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_
)
228 delegate_
->OnInstantSupportDetermined(true);
229 if (!policy_
->ShouldProcessFocusOmnibox(is_active_tab_
))
232 delegate_
->FocusOmnibox(state
);
235 void SearchIPCRouter::OnSearchBoxNavigate(
238 WindowOpenDisposition disposition
,
239 bool is_most_visited_item_url
) const {
240 if (page_seq_no
!= commit_counter_
)
243 delegate_
->OnInstantSupportDetermined(true);
244 if (!policy_
->ShouldProcessNavigateToURL(is_active_tab_
))
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_
)
255 delegate_
->OnInstantSupportDetermined(true);
256 if (!policy_
->ShouldProcessDeleteMostVisitedItem())
259 delegate_
->OnDeleteMostVisitedItem(url
);
262 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_seq_no
,
263 const GURL
& url
) const {
264 if (page_seq_no
!= commit_counter_
)
267 delegate_
->OnInstantSupportDetermined(true);
268 if (!policy_
->ShouldProcessUndoMostVisitedDeletion())
271 delegate_
->OnUndoMostVisitedDeletion(url
);
274 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_seq_no
) const {
275 if (page_seq_no
!= commit_counter_
)
278 delegate_
->OnInstantSupportDetermined(true);
279 if (!policy_
->ShouldProcessUndoAllMostVisitedDeletions())
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_
)
291 delegate_
->OnInstantSupportDetermined(true);
292 if (!policy_
->ShouldProcessLogEvent())
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
))
303 delegate_
->OnInstantSupportDetermined(true);
304 // Logging impressions is controlled by the same policy as logging events.
305 if (!policy_
->ShouldProcessLogEvent())
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
))
316 delegate_
->OnInstantSupportDetermined(true);
317 // Logging navigations is controlled by the same policy as logging events.
318 if (!policy_
->ShouldProcessLogEvent())
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_
)
329 delegate_
->OnInstantSupportDetermined(true);
330 if (!policy_
->ShouldProcessPasteIntoOmnibox(is_active_tab_
))
333 delegate_
->PasteIntoOmnibox(text
);
336 void SearchIPCRouter::OnChromeIdentityCheck(
338 const base::string16
& identity
) const {
339 if (page_seq_no
!= commit_counter_
)
342 delegate_
->OnInstantSupportDetermined(true);
343 if (!policy_
->ShouldProcessChromeIdentityCheck())
346 delegate_
->OnChromeIdentityCheck(identity
);
349 void SearchIPCRouter::OnHistorySyncCheck(int page_seq_no
) const {
350 if (page_seq_no
!= commit_counter_
)
353 delegate_
->OnInstantSupportDetermined(true);
354 if (!policy_
->ShouldProcessHistorySyncCheck())
357 delegate_
->OnHistorySyncCheck();
360 void SearchIPCRouter::set_delegate_for_testing(Delegate
* delegate
) {
362 delegate_
= delegate
;
365 void SearchIPCRouter::set_policy_for_testing(scoped_ptr
<Policy
> policy
) {
366 DCHECK(policy
.get());
367 policy_
.reset(policy
.release());