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/search/search.h"
8 #include "chrome/common/render_messages.h"
9 #include "content/public/browser/web_contents.h"
13 bool IsProviderValid(const base::string16
& provider
) {
14 // Only allow string of 8 alphanumeric characters or less as providers.
15 // The empty string is considered valid and should be treated as if no
16 // provider were specified.
17 if (provider
.length() > 8)
19 for (base::string16::const_iterator it
= provider
.begin();
20 it
!= provider
.end(); ++it
) {
21 if (!IsAsciiAlpha(*it
) && !IsAsciiDigit(*it
))
29 SearchIPCRouter::SearchIPCRouter(content::WebContents
* web_contents
,
30 Delegate
* delegate
, scoped_ptr
<Policy
> policy
)
31 : WebContentsObserver(web_contents
),
33 policy_(policy
.Pass()),
34 is_active_tab_(false) {
37 DCHECK(policy_
.get());
40 SearchIPCRouter::~SearchIPCRouter() {}
42 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
43 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
46 void SearchIPCRouter::SendChromeIdentityCheckResult(
47 const base::string16
& identity
,
48 bool identity_match
) {
49 if (!policy_
->ShouldProcessChromeIdentityCheck())
52 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity
,
56 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled
) {
57 if (!policy_
->ShouldSendSetPromoInformation())
60 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
61 is_app_launcher_enabled
));
64 void SearchIPCRouter::SetDisplayInstantResults() {
65 if (!policy_
->ShouldSendSetDisplayInstantResults())
68 bool is_search_results_page
= !chrome::GetSearchTerms(web_contents()).empty();
69 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
71 (is_search_results_page
&& chrome::ShouldPrefetchSearchResultsOnSRP()) ||
72 chrome::ShouldPrefetchSearchResults()));
75 void SearchIPCRouter::SetSuggestionToPrefetch(
76 const InstantSuggestion
& suggestion
) {
77 if (!policy_
->ShouldSendSetSuggestionToPrefetch())
80 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
84 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin
) {
85 if (!policy_
->ShouldSendSetOmniboxStartMargin())
88 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin
));
91 void SearchIPCRouter::SetInputInProgress(bool input_in_progress
) {
92 if (!policy_
->ShouldSendSetInputInProgress(is_active_tab_
))
95 Send(new ChromeViewMsg_SearchBoxSetInputInProgress(routing_id(),
99 void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state
,
100 OmniboxFocusChangeReason reason
) {
101 if (!policy_
->ShouldSendOmniboxFocusChanged())
104 Send(new ChromeViewMsg_SearchBoxFocusChanged(routing_id(), state
, reason
));
107 void SearchIPCRouter::SendMostVisitedItems(
108 const std::vector
<InstantMostVisitedItem
>& items
) {
109 if (!policy_
->ShouldSendMostVisitedItems())
112 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items
));
115 void SearchIPCRouter::SendThemeBackgroundInfo(
116 const ThemeBackgroundInfo
& theme_info
) {
117 if (!policy_
->ShouldSendThemeBackgroundInfo())
120 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info
));
123 void SearchIPCRouter::ToggleVoiceSearch() {
124 if (!policy_
->ShouldSendToggleVoiceSearch())
127 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
130 void SearchIPCRouter::Submit(const base::string16
& text
) {
131 if (!policy_
->ShouldSubmitQuery())
134 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text
));
137 void SearchIPCRouter::OnTabActivated() {
138 is_active_tab_
= true;
141 void SearchIPCRouter::OnTabDeactivated() {
142 is_active_tab_
= false;
145 bool SearchIPCRouter::OnMessageReceived(const IPC::Message
& message
) {
147 IPC_BEGIN_MESSAGE_MAP(SearchIPCRouter
, message
)
148 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined
,
149 OnInstantSupportDetermined
)
150 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetVoiceSearchSupported
,
151 OnVoiceSearchSupportDetermined
)
152 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox
, OnFocusOmnibox
);
153 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate
,
154 OnSearchBoxNavigate
);
155 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem
,
156 OnDeleteMostVisitedItem
);
157 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion
,
158 OnUndoMostVisitedDeletion
);
159 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions
,
160 OnUndoAllMostVisitedDeletions
);
161 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent
, OnLogEvent
);
162 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogMostVisitedImpression
,
163 OnLogMostVisitedImpression
);
164 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogMostVisitedNavigation
,
165 OnLogMostVisitedNavigation
);
166 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PasteAndOpenDropdown
,
167 OnPasteAndOpenDropDown
);
168 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck
,
169 OnChromeIdentityCheck
);
170 IPC_MESSAGE_UNHANDLED(handled
= false)
171 IPC_END_MESSAGE_MAP()
175 void SearchIPCRouter::OnInstantSupportDetermined(int page_id
,
176 bool instant_support
) const {
177 if (!web_contents()->IsActiveEntry(page_id
))
180 delegate_
->OnInstantSupportDetermined(instant_support
);
183 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
185 bool supports_voice_search
) const {
186 if (!web_contents()->IsActiveEntry(page_id
))
189 delegate_
->OnInstantSupportDetermined(true);
190 if (!policy_
->ShouldProcessSetVoiceSearchSupport())
193 delegate_
->OnSetVoiceSearchSupport(supports_voice_search
);
196 void SearchIPCRouter::OnFocusOmnibox(int page_id
,
197 OmniboxFocusState state
) const {
198 if (!web_contents()->IsActiveEntry(page_id
))
201 delegate_
->OnInstantSupportDetermined(true);
202 if (!policy_
->ShouldProcessFocusOmnibox(is_active_tab_
))
205 delegate_
->FocusOmnibox(state
);
208 void SearchIPCRouter::OnSearchBoxNavigate(
211 WindowOpenDisposition disposition
,
212 bool is_most_visited_item_url
) const {
213 if (!web_contents()->IsActiveEntry(page_id
))
216 delegate_
->OnInstantSupportDetermined(true);
217 if (!policy_
->ShouldProcessNavigateToURL(is_active_tab_
))
220 delegate_
->NavigateToURL(url
, disposition
, is_most_visited_item_url
);
223 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_id
,
224 const GURL
& url
) const {
225 if (!web_contents()->IsActiveEntry(page_id
))
228 delegate_
->OnInstantSupportDetermined(true);
229 if (!policy_
->ShouldProcessDeleteMostVisitedItem())
232 delegate_
->OnDeleteMostVisitedItem(url
);
235 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id
,
236 const GURL
& url
) const {
237 if (!web_contents()->IsActiveEntry(page_id
))
240 delegate_
->OnInstantSupportDetermined(true);
241 if (!policy_
->ShouldProcessUndoMostVisitedDeletion())
244 delegate_
->OnUndoMostVisitedDeletion(url
);
247 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id
) const {
248 if (!web_contents()->IsActiveEntry(page_id
))
251 delegate_
->OnInstantSupportDetermined(true);
252 if (!policy_
->ShouldProcessUndoAllMostVisitedDeletions())
255 delegate_
->OnUndoAllMostVisitedDeletions();
258 void SearchIPCRouter::OnLogEvent(int page_id
, NTPLoggingEventType event
) const {
259 if (!web_contents()->IsActiveEntry(page_id
))
262 delegate_
->OnInstantSupportDetermined(true);
263 if (!policy_
->ShouldProcessLogEvent())
266 delegate_
->OnLogEvent(event
);
269 void SearchIPCRouter::OnLogMostVisitedImpression(
270 int page_id
, int position
, const base::string16
& provider
) const {
271 if (!web_contents()->IsActiveEntry(page_id
) || !IsProviderValid(provider
))
274 delegate_
->OnInstantSupportDetermined(true);
275 // Logging impressions is controlled by the same policy as logging events.
276 if (!policy_
->ShouldProcessLogEvent())
279 delegate_
->OnLogMostVisitedImpression(position
, provider
);
282 void SearchIPCRouter::OnLogMostVisitedNavigation(
283 int page_id
, int position
, const base::string16
& provider
) const {
284 if (!web_contents()->IsActiveEntry(page_id
) || !IsProviderValid(provider
))
287 delegate_
->OnInstantSupportDetermined(true);
288 // Logging navigations is controlled by the same policy as logging events.
289 if (!policy_
->ShouldProcessLogEvent())
292 delegate_
->OnLogMostVisitedNavigation(position
, provider
);
295 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_id
,
296 const base::string16
& text
) const {
297 if (!web_contents()->IsActiveEntry(page_id
))
300 delegate_
->OnInstantSupportDetermined(true);
301 if (!policy_
->ShouldProcessPasteIntoOmnibox(is_active_tab_
))
304 delegate_
->PasteIntoOmnibox(text
);
307 void SearchIPCRouter::OnChromeIdentityCheck(
309 const base::string16
& identity
) const {
310 if (!web_contents()->IsActiveEntry(page_id
))
313 delegate_
->OnInstantSupportDetermined(true);
314 if (!policy_
->ShouldProcessChromeIdentityCheck())
317 delegate_
->OnChromeIdentityCheck(identity
);
320 void SearchIPCRouter::set_delegate(Delegate
* delegate
) {
322 delegate_
= delegate
;
325 void SearchIPCRouter::set_policy(scoped_ptr
<Policy
> policy
) {
326 DCHECK(policy
.get());
327 policy_
.reset(policy
.release());