Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / search / search_ipc_router.cc
blob4c3e467f6ed2562d3cdd7056dec29ff8560f1c39
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::SendMostVisitedItems(
67 const std::vector<InstantMostVisitedItem>& items) {
68 if (!policy_->ShouldSendMostVisitedItems())
69 return;
71 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
74 void SearchIPCRouter::SendThemeBackgroundInfo(
75 const ThemeBackgroundInfo& theme_info) {
76 if (!policy_->ShouldSendThemeBackgroundInfo())
77 return;
79 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
82 void SearchIPCRouter::ToggleVoiceSearch() {
83 if (!policy_->ShouldSendToggleVoiceSearch())
84 return;
86 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
89 void SearchIPCRouter::Submit(const base::string16& text) {
90 if (!policy_->ShouldSubmitQuery())
91 return;
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) {
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;
131 void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
132 bool instant_support) const {
133 if (!web_contents()->IsActiveEntry(page_id))
134 return;
136 delegate_->OnInstantSupportDetermined(instant_support);
139 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
140 int page_id,
141 bool supports_voice_search) const {
142 if (!web_contents()->IsActiveEntry(page_id))
143 return;
145 delegate_->OnInstantSupportDetermined(true);
146 if (!policy_->ShouldProcessSetVoiceSearchSupport())
147 return;
149 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
152 void SearchIPCRouter::OnFocusOmnibox(int page_id,
153 OmniboxFocusState state) const {
154 if (!web_contents()->IsActiveEntry(page_id))
155 return;
157 delegate_->OnInstantSupportDetermined(true);
158 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
159 return;
161 delegate_->FocusOmnibox(state);
164 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;
172 delegate_->OnInstantSupportDetermined(true);
173 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
174 return;
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))
182 return;
184 delegate_->OnInstantSupportDetermined(true);
185 if (!policy_->ShouldProcessDeleteMostVisitedItem())
186 return;
188 delegate_->OnDeleteMostVisitedItem(url);
191 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
192 const GURL& url) const {
193 if (!web_contents()->IsActiveEntry(page_id))
194 return;
196 delegate_->OnInstantSupportDetermined(true);
197 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
198 return;
200 delegate_->OnUndoMostVisitedDeletion(url);
203 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
204 if (!web_contents()->IsActiveEntry(page_id))
205 return;
207 delegate_->OnInstantSupportDetermined(true);
208 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
209 return;
211 delegate_->OnUndoAllMostVisitedDeletions();
214 void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
215 if (!web_contents()->IsActiveEntry(page_id))
216 return;
218 delegate_->OnInstantSupportDetermined(true);
219 if (!policy_->ShouldProcessLogEvent())
220 return;
222 delegate_->OnLogEvent(event);
225 void SearchIPCRouter::OnLogImpression(int page_id,
226 int position,
227 const base::string16& provider) const {
228 if (!web_contents()->IsActiveEntry(page_id))
229 return;
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;
240 delegate_->OnInstantSupportDetermined(true);
241 // Logging impressions is controlled by the same policy as logging events.
242 if (!policy_->ShouldProcessLogEvent())
243 return;
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))
251 return;
253 delegate_->OnInstantSupportDetermined(true);
254 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
255 return;
257 delegate_->PasteIntoOmnibox(text);
260 void SearchIPCRouter::OnChromeIdentityCheck(
261 int page_id,
262 const base::string16& identity) const {
263 if (!web_contents()->IsActiveEntry(page_id))
264 return;
266 delegate_->OnInstantSupportDetermined(true);
267 if (!policy_->ShouldProcessChromeIdentityCheck())
268 return;
270 delegate_->OnChromeIdentityCheck(identity);
273 void SearchIPCRouter::set_delegate(Delegate* delegate) {
274 DCHECK(delegate);
275 delegate_ = delegate;
278 void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
279 DCHECK(policy.get());
280 policy_.reset(policy.release());