NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / search / search_ipc_router.cc
blob1d4a73eea0c06728fd437ee50c88f51b033b5f77
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),
14 delegate_(delegate),
15 policy_(policy.Pass()),
16 is_active_tab_(false) {
17 DCHECK(web_contents);
18 DCHECK(delegate);
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())
32 return;
34 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity,
35 identity_match));
38 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
39 if (!policy_->ShouldSendSetPromoInformation())
40 return;
42 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
43 is_app_launcher_enabled));
46 void SearchIPCRouter::SetDisplayInstantResults() {
47 if (!policy_->ShouldSendSetDisplayInstantResults())
48 return;
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()));
57 void SearchIPCRouter::SetSuggestionToPrefetch(
58 const InstantSuggestion& suggestion) {
59 if (!policy_->ShouldSendSetSuggestionToPrefetch())
60 return;
62 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
63 suggestion));
66 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin) {
67 if (!policy_->ShouldSendSetOmniboxStartMargin())
68 return;
70 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin));
73 void SearchIPCRouter::SendMostVisitedItems(
74 const std::vector<InstantMostVisitedItem>& items) {
75 if (!policy_->ShouldSendMostVisitedItems())
76 return;
78 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
81 void SearchIPCRouter::SendThemeBackgroundInfo(
82 const ThemeBackgroundInfo& theme_info) {
83 if (!policy_->ShouldSendThemeBackgroundInfo())
84 return;
86 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
89 void SearchIPCRouter::ToggleVoiceSearch() {
90 if (!policy_->ShouldSendToggleVoiceSearch())
91 return;
93 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
96 void SearchIPCRouter::Submit(const base::string16& text) {
97 if (!policy_->ShouldSubmitQuery())
98 return;
100 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text));
103 void SearchIPCRouter::OnTabActivated() {
104 is_active_tab_ = true;
107 void SearchIPCRouter::OnTabDeactivated() {
108 is_active_tab_ = false;
111 bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
112 bool handled = true;
113 IPC_BEGIN_MESSAGE_MAP(SearchIPCRouter, message)
114 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
115 OnInstantSupportDetermined)
116 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetVoiceSearchSupported,
117 OnVoiceSearchSupportDetermined)
118 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox);
119 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
120 OnSearchBoxNavigate);
121 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
122 OnDeleteMostVisitedItem);
123 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
124 OnUndoMostVisitedDeletion);
125 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
126 OnUndoAllMostVisitedDeletions);
127 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent, OnLogEvent);
128 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogImpression, OnLogImpression);
129 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PasteAndOpenDropdown,
130 OnPasteAndOpenDropDown);
131 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck,
132 OnChromeIdentityCheck);
133 IPC_MESSAGE_UNHANDLED(handled = false)
134 IPC_END_MESSAGE_MAP()
135 return handled;
138 void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
139 bool instant_support) const {
140 if (!web_contents()->IsActiveEntry(page_id))
141 return;
143 delegate_->OnInstantSupportDetermined(instant_support);
146 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
147 int page_id,
148 bool supports_voice_search) const {
149 if (!web_contents()->IsActiveEntry(page_id))
150 return;
152 delegate_->OnInstantSupportDetermined(true);
153 if (!policy_->ShouldProcessSetVoiceSearchSupport())
154 return;
156 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
159 void SearchIPCRouter::OnFocusOmnibox(int page_id,
160 OmniboxFocusState state) const {
161 if (!web_contents()->IsActiveEntry(page_id))
162 return;
164 delegate_->OnInstantSupportDetermined(true);
165 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
166 return;
168 delegate_->FocusOmnibox(state);
171 void SearchIPCRouter::OnSearchBoxNavigate(
172 int page_id,
173 const GURL& url,
174 WindowOpenDisposition disposition,
175 bool is_most_visited_item_url) const {
176 if (!web_contents()->IsActiveEntry(page_id))
177 return;
179 delegate_->OnInstantSupportDetermined(true);
180 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
181 return;
183 delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
186 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_id,
187 const GURL& url) const {
188 if (!web_contents()->IsActiveEntry(page_id))
189 return;
191 delegate_->OnInstantSupportDetermined(true);
192 if (!policy_->ShouldProcessDeleteMostVisitedItem())
193 return;
195 delegate_->OnDeleteMostVisitedItem(url);
198 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
199 const GURL& url) const {
200 if (!web_contents()->IsActiveEntry(page_id))
201 return;
203 delegate_->OnInstantSupportDetermined(true);
204 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
205 return;
207 delegate_->OnUndoMostVisitedDeletion(url);
210 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
211 if (!web_contents()->IsActiveEntry(page_id))
212 return;
214 delegate_->OnInstantSupportDetermined(true);
215 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
216 return;
218 delegate_->OnUndoAllMostVisitedDeletions();
221 void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
222 if (!web_contents()->IsActiveEntry(page_id))
223 return;
225 delegate_->OnInstantSupportDetermined(true);
226 if (!policy_->ShouldProcessLogEvent())
227 return;
229 delegate_->OnLogEvent(event);
232 void SearchIPCRouter::OnLogImpression(int page_id,
233 int position,
234 const base::string16& provider) const {
235 if (!web_contents()->IsActiveEntry(page_id))
236 return;
238 // Only allow string of 8 alphanumeric characters or less as providers.
239 if (provider.length() > 8)
240 return;
241 for (base::string16::const_iterator it = provider.begin();
242 it != provider.end(); ++it) {
243 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it))
244 return;
247 delegate_->OnInstantSupportDetermined(true);
248 // Logging impressions is controlled by the same policy as logging events.
249 if (!policy_->ShouldProcessLogEvent())
250 return;
252 delegate_->OnLogImpression(position, provider);
255 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_id,
256 const base::string16& text) const {
257 if (!web_contents()->IsActiveEntry(page_id))
258 return;
260 delegate_->OnInstantSupportDetermined(true);
261 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
262 return;
264 delegate_->PasteIntoOmnibox(text);
267 void SearchIPCRouter::OnChromeIdentityCheck(
268 int page_id,
269 const base::string16& identity) const {
270 if (!web_contents()->IsActiveEntry(page_id))
271 return;
273 delegate_->OnInstantSupportDetermined(true);
274 if (!policy_->ShouldProcessChromeIdentityCheck())
275 return;
277 delegate_->OnChromeIdentityCheck(identity);
280 void SearchIPCRouter::set_delegate(Delegate* delegate) {
281 DCHECK(delegate);
282 delegate_ = delegate;
285 void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
286 DCHECK(policy.get());
287 policy_.reset(policy.release());