Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / search / search_ipc_router.cc
blob09b1a72b1fc942cd7e65df60ee8087502b430791
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 namespace {
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)
18 return false;
19 for (base::string16::const_iterator it = provider.begin();
20 it != provider.end(); ++it) {
21 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it))
22 return false;
24 return true;
27 } // namespace
29 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
30 Delegate* delegate, scoped_ptr<Policy> policy)
31 : WebContentsObserver(web_contents),
32 delegate_(delegate),
33 policy_(policy.Pass()),
34 is_active_tab_(false) {
35 DCHECK(web_contents);
36 DCHECK(delegate);
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())
50 return;
52 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity,
53 identity_match));
56 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
57 if (!policy_->ShouldSendSetPromoInformation())
58 return;
60 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
61 is_app_launcher_enabled));
64 void SearchIPCRouter::SetDisplayInstantResults() {
65 if (!policy_->ShouldSendSetDisplayInstantResults())
66 return;
68 bool is_search_results_page = !chrome::GetSearchTerms(web_contents()).empty();
69 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
70 routing_id(),
71 (is_search_results_page && chrome::ShouldPrefetchSearchResultsOnSRP()) ||
72 chrome::ShouldPrefetchSearchResults()));
75 void SearchIPCRouter::SetSuggestionToPrefetch(
76 const InstantSuggestion& suggestion) {
77 if (!policy_->ShouldSendSetSuggestionToPrefetch())
78 return;
80 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
81 suggestion));
84 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin) {
85 if (!policy_->ShouldSendSetOmniboxStartMargin())
86 return;
88 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin));
91 void SearchIPCRouter::SetInputInProgress(bool input_in_progress) {
92 if (!policy_->ShouldSendSetInputInProgress(is_active_tab_))
93 return;
95 Send(new ChromeViewMsg_SearchBoxSetInputInProgress(routing_id(),
96 input_in_progress));
99 void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state,
100 OmniboxFocusChangeReason reason) {
101 if (!policy_->ShouldSendOmniboxFocusChanged())
102 return;
104 Send(new ChromeViewMsg_SearchBoxFocusChanged(routing_id(), state, reason));
107 void SearchIPCRouter::SendMostVisitedItems(
108 const std::vector<InstantMostVisitedItem>& items) {
109 if (!policy_->ShouldSendMostVisitedItems())
110 return;
112 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
115 void SearchIPCRouter::SendThemeBackgroundInfo(
116 const ThemeBackgroundInfo& theme_info) {
117 if (!policy_->ShouldSendThemeBackgroundInfo())
118 return;
120 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
123 void SearchIPCRouter::ToggleVoiceSearch() {
124 if (!policy_->ShouldSendToggleVoiceSearch())
125 return;
127 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
130 void SearchIPCRouter::Submit(const base::string16& text) {
131 if (!policy_->ShouldSubmitQuery())
132 return;
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) {
146 bool handled = true;
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()
172 return handled;
175 void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
176 bool instant_support) const {
177 if (!web_contents()->IsActiveEntry(page_id))
178 return;
180 delegate_->OnInstantSupportDetermined(instant_support);
183 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
184 int page_id,
185 bool supports_voice_search) const {
186 if (!web_contents()->IsActiveEntry(page_id))
187 return;
189 delegate_->OnInstantSupportDetermined(true);
190 if (!policy_->ShouldProcessSetVoiceSearchSupport())
191 return;
193 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
196 void SearchIPCRouter::OnFocusOmnibox(int page_id,
197 OmniboxFocusState state) const {
198 if (!web_contents()->IsActiveEntry(page_id))
199 return;
201 delegate_->OnInstantSupportDetermined(true);
202 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
203 return;
205 delegate_->FocusOmnibox(state);
208 void SearchIPCRouter::OnSearchBoxNavigate(
209 int page_id,
210 const GURL& url,
211 WindowOpenDisposition disposition,
212 bool is_most_visited_item_url) const {
213 if (!web_contents()->IsActiveEntry(page_id))
214 return;
216 delegate_->OnInstantSupportDetermined(true);
217 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
218 return;
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))
226 return;
228 delegate_->OnInstantSupportDetermined(true);
229 if (!policy_->ShouldProcessDeleteMostVisitedItem())
230 return;
232 delegate_->OnDeleteMostVisitedItem(url);
235 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
236 const GURL& url) const {
237 if (!web_contents()->IsActiveEntry(page_id))
238 return;
240 delegate_->OnInstantSupportDetermined(true);
241 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
242 return;
244 delegate_->OnUndoMostVisitedDeletion(url);
247 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
248 if (!web_contents()->IsActiveEntry(page_id))
249 return;
251 delegate_->OnInstantSupportDetermined(true);
252 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
253 return;
255 delegate_->OnUndoAllMostVisitedDeletions();
258 void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
259 if (!web_contents()->IsActiveEntry(page_id))
260 return;
262 delegate_->OnInstantSupportDetermined(true);
263 if (!policy_->ShouldProcessLogEvent())
264 return;
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))
272 return;
274 delegate_->OnInstantSupportDetermined(true);
275 // Logging impressions is controlled by the same policy as logging events.
276 if (!policy_->ShouldProcessLogEvent())
277 return;
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))
285 return;
287 delegate_->OnInstantSupportDetermined(true);
288 // Logging navigations is controlled by the same policy as logging events.
289 if (!policy_->ShouldProcessLogEvent())
290 return;
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))
298 return;
300 delegate_->OnInstantSupportDetermined(true);
301 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
302 return;
304 delegate_->PasteIntoOmnibox(text);
307 void SearchIPCRouter::OnChromeIdentityCheck(
308 int page_id,
309 const base::string16& identity) const {
310 if (!web_contents()->IsActiveEntry(page_id))
311 return;
313 delegate_->OnInstantSupportDetermined(true);
314 if (!policy_->ShouldProcessChromeIdentityCheck())
315 return;
317 delegate_->OnChromeIdentityCheck(identity);
320 void SearchIPCRouter::set_delegate(Delegate* delegate) {
321 DCHECK(delegate);
322 delegate_ = delegate;
325 void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
326 DCHECK(policy.get());
327 policy_.reset(policy.release());