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"
11 SearchIPCRouter::SearchIPCRouter(content::WebContents
* web_contents
,
12 Delegate
* delegate
, scoped_ptr
<Policy
> policy
)
13 : WebContentsObserver(web_contents
),
15 policy_(policy
.Pass()),
16 is_active_tab_(false) {
19 DCHECK(policy_
.get());
22 SearchIPCRouter::~SearchIPCRouter() {}
24 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
25 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
28 void SearchIPCRouter::SendChromeIdentityCheckResult(
29 const base::string16
& identity
,
30 bool identity_match
) {
31 if (!policy_
->ShouldProcessChromeIdentityCheck())
34 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity
,
38 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled
) {
39 if (!policy_
->ShouldSendSetPromoInformation())
42 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
43 is_app_launcher_enabled
));
46 void SearchIPCRouter::SetDisplayInstantResults() {
47 if (!policy_
->ShouldSendSetDisplayInstantResults())
50 bool is_search_results_page
= !chrome::GetSearchTerms(web_contents()).empty();
51 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
53 (is_search_results_page
&& chrome::ShouldPrefetchSearchResultsOnSRP()) ||
54 chrome::ShouldPrefetchSearchResults()));
57 void SearchIPCRouter::SetSuggestionToPrefetch(
58 const InstantSuggestion
& suggestion
) {
59 if (!policy_
->ShouldSendSetSuggestionToPrefetch())
62 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
66 void SearchIPCRouter::SendMostVisitedItems(
67 const std::vector
<InstantMostVisitedItem
>& items
) {
68 if (!policy_
->ShouldSendMostVisitedItems())
71 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items
));
74 void SearchIPCRouter::SendThemeBackgroundInfo(
75 const ThemeBackgroundInfo
& theme_info
) {
76 if (!policy_
->ShouldSendThemeBackgroundInfo())
79 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info
));
82 void SearchIPCRouter::ToggleVoiceSearch() {
83 if (!policy_
->ShouldSendToggleVoiceSearch())
86 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
89 void SearchIPCRouter::Submit(const base::string16
& text
) {
90 if (!policy_
->ShouldSubmitQuery())
93 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text
));
96 void SearchIPCRouter::OnTabActivated() {
97 is_active_tab_
= true;
100 void SearchIPCRouter::OnTabDeactivated() {
101 is_active_tab_
= false;
104 bool SearchIPCRouter::OnMessageReceived(const IPC::Message
& message
) {
106 IPC_BEGIN_MESSAGE_MAP(SearchIPCRouter
, message
)
107 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined
,
108 OnInstantSupportDetermined
)
109 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetVoiceSearchSupported
,
110 OnVoiceSearchSupportDetermined
)
111 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox
, OnFocusOmnibox
);
112 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate
,
113 OnSearchBoxNavigate
);
114 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem
,
115 OnDeleteMostVisitedItem
);
116 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion
,
117 OnUndoMostVisitedDeletion
);
118 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions
,
119 OnUndoAllMostVisitedDeletions
);
120 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent
, OnLogEvent
);
121 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogImpression
, OnLogImpression
);
122 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PasteAndOpenDropdown
,
123 OnPasteAndOpenDropDown
);
124 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck
,
125 OnChromeIdentityCheck
);
126 IPC_MESSAGE_UNHANDLED(handled
= false)
127 IPC_END_MESSAGE_MAP()
131 void SearchIPCRouter::OnInstantSupportDetermined(int page_id
,
132 bool instant_support
) const {
133 if (!web_contents()->IsActiveEntry(page_id
))
136 delegate_
->OnInstantSupportDetermined(instant_support
);
139 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
141 bool supports_voice_search
) const {
142 if (!web_contents()->IsActiveEntry(page_id
))
145 delegate_
->OnInstantSupportDetermined(true);
146 if (!policy_
->ShouldProcessSetVoiceSearchSupport())
149 delegate_
->OnSetVoiceSearchSupport(supports_voice_search
);
152 void SearchIPCRouter::OnFocusOmnibox(int page_id
,
153 OmniboxFocusState state
) const {
154 if (!web_contents()->IsActiveEntry(page_id
))
157 delegate_
->OnInstantSupportDetermined(true);
158 if (!policy_
->ShouldProcessFocusOmnibox(is_active_tab_
))
161 delegate_
->FocusOmnibox(state
);
164 void SearchIPCRouter::OnSearchBoxNavigate(
167 WindowOpenDisposition disposition
,
168 bool is_most_visited_item_url
) const {
169 if (!web_contents()->IsActiveEntry(page_id
))
172 delegate_
->OnInstantSupportDetermined(true);
173 if (!policy_
->ShouldProcessNavigateToURL(is_active_tab_
))
176 delegate_
->NavigateToURL(url
, disposition
, is_most_visited_item_url
);
179 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_id
,
180 const GURL
& url
) const {
181 if (!web_contents()->IsActiveEntry(page_id
))
184 delegate_
->OnInstantSupportDetermined(true);
185 if (!policy_
->ShouldProcessDeleteMostVisitedItem())
188 delegate_
->OnDeleteMostVisitedItem(url
);
191 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id
,
192 const GURL
& url
) const {
193 if (!web_contents()->IsActiveEntry(page_id
))
196 delegate_
->OnInstantSupportDetermined(true);
197 if (!policy_
->ShouldProcessUndoMostVisitedDeletion())
200 delegate_
->OnUndoMostVisitedDeletion(url
);
203 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id
) const {
204 if (!web_contents()->IsActiveEntry(page_id
))
207 delegate_
->OnInstantSupportDetermined(true);
208 if (!policy_
->ShouldProcessUndoAllMostVisitedDeletions())
211 delegate_
->OnUndoAllMostVisitedDeletions();
214 void SearchIPCRouter::OnLogEvent(int page_id
, NTPLoggingEventType event
) const {
215 if (!web_contents()->IsActiveEntry(page_id
))
218 delegate_
->OnInstantSupportDetermined(true);
219 if (!policy_
->ShouldProcessLogEvent())
222 delegate_
->OnLogEvent(event
);
225 void SearchIPCRouter::OnLogImpression(int page_id
,
227 const base::string16
& provider
) const {
228 if (!web_contents()->IsActiveEntry(page_id
))
231 // Only allow string of 8 alphanumeric characters or less as providers.
232 if (provider
.length() > 8)
234 for (base::string16::const_iterator it
= provider
.begin();
235 it
!= provider
.end(); ++it
) {
236 if (!IsAsciiAlpha(*it
) && !IsAsciiDigit(*it
))
240 delegate_
->OnInstantSupportDetermined(true);
241 // Logging impressions is controlled by the same policy as logging events.
242 if (!policy_
->ShouldProcessLogEvent())
245 delegate_
->OnLogImpression(position
, provider
);
248 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_id
,
249 const base::string16
& text
) const {
250 if (!web_contents()->IsActiveEntry(page_id
))
253 delegate_
->OnInstantSupportDetermined(true);
254 if (!policy_
->ShouldProcessPasteIntoOmnibox(is_active_tab_
))
257 delegate_
->PasteIntoOmnibox(text
);
260 void SearchIPCRouter::OnChromeIdentityCheck(
262 const base::string16
& identity
) const {
263 if (!web_contents()->IsActiveEntry(page_id
))
266 delegate_
->OnInstantSupportDetermined(true);
267 if (!policy_
->ShouldProcessChromeIdentityCheck())
270 delegate_
->OnChromeIdentityCheck(identity
);
273 void SearchIPCRouter::set_delegate(Delegate
* delegate
) {
275 delegate_
= delegate
;
278 void SearchIPCRouter::set_policy(scoped_ptr
<Policy
> policy
) {
279 DCHECK(policy
.get());
280 policy_
.reset(policy
.release());