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.
4
5 #include "chrome/browser/ui/search/search_ipc_router.h"
6
7 #include "chrome/browser/search/search.h"
8 #include "chrome/common/render_messages.h"
9 #include "content/public/browser/web_contents.h"
10
SearchIPCRouter(content::WebContents * web_contents,Delegate * delegate,scoped_ptr<Policy> policy)11 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
12 Delegate* delegate, scoped_ptr<Policy> policy)
13 : WebContentsObserver(web_contents),
14 delegate_(delegate),
15 policy_(policy.Pass()),
16 is_active_tab_(false) {
17 DCHECK(web_contents);
18 DCHECK(delegate);
19 DCHECK(policy_.get());
20 }
21
~SearchIPCRouter()22 SearchIPCRouter::~SearchIPCRouter() {}
23
DetermineIfPageSupportsInstant()24 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
25 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
26 }
27
SendChromeIdentityCheckResult(const base::string16 & identity,bool identity_match)28 void SearchIPCRouter::SendChromeIdentityCheckResult(
29 const base::string16& identity,
30 bool identity_match) {
31 if (!policy_->ShouldProcessChromeIdentityCheck())
32 return;
33
34 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity,
35 identity_match));
36 }
37
SetPromoInformation(bool is_app_launcher_enabled)38 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
39 if (!policy_->ShouldSendSetPromoInformation())
40 return;
41
42 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
43 is_app_launcher_enabled));
44 }
45
SetDisplayInstantResults()46 void SearchIPCRouter::SetDisplayInstantResults() {
47 if (!policy_->ShouldSendSetDisplayInstantResults())
48 return;
49
50 bool is_search_results_page = !chrome::GetSearchTerms(web_contents()).empty();
51 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
52 routing_id(),
53 (is_search_results_page && chrome::ShouldPrefetchSearchResultsOnSRP()) ||
54 chrome::ShouldPrefetchSearchResults()));
55 }
56
SetSuggestionToPrefetch(const InstantSuggestion & suggestion)57 void SearchIPCRouter::SetSuggestionToPrefetch(
58 const InstantSuggestion& suggestion) {
59 if (!policy_->ShouldSendSetSuggestionToPrefetch())
60 return;
61
62 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
63 suggestion));
64 }
65
SendMostVisitedItems(const std::vector<InstantMostVisitedItem> & items)66 void SearchIPCRouter::SendMostVisitedItems(
67 const std::vector<InstantMostVisitedItem>& items) {
68 if (!policy_->ShouldSendMostVisitedItems())
69 return;
70
71 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
72 }
73
SendThemeBackgroundInfo(const ThemeBackgroundInfo & theme_info)74 void SearchIPCRouter::SendThemeBackgroundInfo(
75 const ThemeBackgroundInfo& theme_info) {
76 if (!policy_->ShouldSendThemeBackgroundInfo())
77 return;
78
79 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
80 }
81
ToggleVoiceSearch()82 void SearchIPCRouter::ToggleVoiceSearch() {
83 if (!policy_->ShouldSendToggleVoiceSearch())
84 return;
85
86 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
87 }
88
Submit(const base::string16 & text)89 void SearchIPCRouter::Submit(const base::string16& text) {
90 if (!policy_->ShouldSubmitQuery())
91 return;
92
93 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text));
94 }
95
OnTabActivated()96 void SearchIPCRouter::OnTabActivated() {
97 is_active_tab_ = true;
98 }
99
OnTabDeactivated()100 void SearchIPCRouter::OnTabDeactivated() {
101 is_active_tab_ = false;
102 }
103
OnMessageReceived(const IPC::Message & message)104 bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
105 bool handled = true;
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()
128 return handled;
129 }
130
OnInstantSupportDetermined(int page_id,bool instant_support) const131 void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
132 bool instant_support) const {
133 if (!web_contents()->IsActiveEntry(page_id))
134 return;
135
136 delegate_->OnInstantSupportDetermined(instant_support);
137 }
138
OnVoiceSearchSupportDetermined(int page_id,bool supports_voice_search) const139 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
140 int page_id,
141 bool supports_voice_search) const {
142 if (!web_contents()->IsActiveEntry(page_id))
143 return;
144
145 delegate_->OnInstantSupportDetermined(true);
146 if (!policy_->ShouldProcessSetVoiceSearchSupport())
147 return;
148
149 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
150 }
151
OnFocusOmnibox(int page_id,OmniboxFocusState state) const152 void SearchIPCRouter::OnFocusOmnibox(int page_id,
153 OmniboxFocusState state) const {
154 if (!web_contents()->IsActiveEntry(page_id))
155 return;
156
157 delegate_->OnInstantSupportDetermined(true);
158 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
159 return;
160
161 delegate_->FocusOmnibox(state);
162 }
163
OnSearchBoxNavigate(int page_id,const GURL & url,WindowOpenDisposition disposition,bool is_most_visited_item_url) const164 void SearchIPCRouter::OnSearchBoxNavigate(
165 int page_id,
166 const GURL& url,
167 WindowOpenDisposition disposition,
168 bool is_most_visited_item_url) const {
169 if (!web_contents()->IsActiveEntry(page_id))
170 return;
171
172 delegate_->OnInstantSupportDetermined(true);
173 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
174 return;
175
176 delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
177 }
178
OnDeleteMostVisitedItem(int page_id,const GURL & url) const179 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_id,
180 const GURL& url) const {
181 if (!web_contents()->IsActiveEntry(page_id))
182 return;
183
184 delegate_->OnInstantSupportDetermined(true);
185 if (!policy_->ShouldProcessDeleteMostVisitedItem())
186 return;
187
188 delegate_->OnDeleteMostVisitedItem(url);
189 }
190
OnUndoMostVisitedDeletion(int page_id,const GURL & url) const191 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
192 const GURL& url) const {
193 if (!web_contents()->IsActiveEntry(page_id))
194 return;
195
196 delegate_->OnInstantSupportDetermined(true);
197 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
198 return;
199
200 delegate_->OnUndoMostVisitedDeletion(url);
201 }
202
OnUndoAllMostVisitedDeletions(int page_id) const203 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
204 if (!web_contents()->IsActiveEntry(page_id))
205 return;
206
207 delegate_->OnInstantSupportDetermined(true);
208 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
209 return;
210
211 delegate_->OnUndoAllMostVisitedDeletions();
212 }
213
OnLogEvent(int page_id,NTPLoggingEventType event) const214 void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
215 if (!web_contents()->IsActiveEntry(page_id))
216 return;
217
218 delegate_->OnInstantSupportDetermined(true);
219 if (!policy_->ShouldProcessLogEvent())
220 return;
221
222 delegate_->OnLogEvent(event);
223 }
224
OnLogImpression(int page_id,int position,const base::string16 & provider) const225 void SearchIPCRouter::OnLogImpression(int page_id,
226 int position,
227 const base::string16& provider) const {
228 if (!web_contents()->IsActiveEntry(page_id))
229 return;
230
231 // Only allow string of 8 alphanumeric characters or less as providers.
232 if (provider.length() > 8)
233 return;
234 for (base::string16::const_iterator it = provider.begin();
235 it != provider.end(); ++it) {
236 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it))
237 return;
238 }
239
240 delegate_->OnInstantSupportDetermined(true);
241 // Logging impressions is controlled by the same policy as logging events.
242 if (!policy_->ShouldProcessLogEvent())
243 return;
244
245 delegate_->OnLogImpression(position, provider);
246 }
247
OnPasteAndOpenDropDown(int page_id,const base::string16 & text) const248 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_id,
249 const base::string16& text) const {
250 if (!web_contents()->IsActiveEntry(page_id))
251 return;
252
253 delegate_->OnInstantSupportDetermined(true);
254 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
255 return;
256
257 delegate_->PasteIntoOmnibox(text);
258 }
259
OnChromeIdentityCheck(int page_id,const base::string16 & identity) const260 void SearchIPCRouter::OnChromeIdentityCheck(
261 int page_id,
262 const base::string16& identity) const {
263 if (!web_contents()->IsActiveEntry(page_id))
264 return;
265
266 delegate_->OnInstantSupportDetermined(true);
267 if (!policy_->ShouldProcessChromeIdentityCheck())
268 return;
269
270 delegate_->OnChromeIdentityCheck(identity);
271 }
272
set_delegate(Delegate * delegate)273 void SearchIPCRouter::set_delegate(Delegate* delegate) {
274 DCHECK(delegate);
275 delegate_ = delegate;
276 }
277
set_policy(scoped_ptr<Policy> policy)278 void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
279 DCHECK(policy.get());
280 policy_.reset(policy.release());
281 }
282