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::SetOmniboxStartMargin(int start_margin) {
67 if (!policy_->ShouldSendSetOmniboxStartMargin())
70 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin));
73 void SearchIPCRouter::SendMostVisitedItems(
74 const std::vector<InstantMostVisitedItem>& items) {
75 if (!policy_->ShouldSendMostVisitedItems())
78 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
81 void SearchIPCRouter::SendThemeBackgroundInfo(
82 const ThemeBackgroundInfo& theme_info) {
83 if (!policy_->ShouldSendThemeBackgroundInfo())
86 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
89 void SearchIPCRouter::ToggleVoiceSearch() {
90 if (!policy_->ShouldSendToggleVoiceSearch())
93 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
96 void SearchIPCRouter::Submit(const base::string16& text) {
97 if (!policy_->ShouldSubmitQuery())
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) {
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()
138 void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
139 bool instant_support) const {
140 if (!web_contents()->IsActiveEntry(page_id))
143 delegate_->OnInstantSupportDetermined(instant_support);
146 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
148 bool supports_voice_search) const {
149 if (!web_contents()->IsActiveEntry(page_id))
152 delegate_->OnInstantSupportDetermined(true);
153 if (!policy_->ShouldProcessSetVoiceSearchSupport())
156 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
159 void SearchIPCRouter::OnFocusOmnibox(int page_id,
160 OmniboxFocusState state) const {
161 if (!web_contents()->IsActiveEntry(page_id))
164 delegate_->OnInstantSupportDetermined(true);
165 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
168 delegate_->FocusOmnibox(state);
171 void SearchIPCRouter::OnSearchBoxNavigate(
174 WindowOpenDisposition disposition,
175 bool is_most_visited_item_url) const {
176 if (!web_contents()->IsActiveEntry(page_id))
179 delegate_->OnInstantSupportDetermined(true);
180 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
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))
191 delegate_->OnInstantSupportDetermined(true);
192 if (!policy_->ShouldProcessDeleteMostVisitedItem())
195 delegate_->OnDeleteMostVisitedItem(url);
198 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
199 const GURL& url) const {
200 if (!web_contents()->IsActiveEntry(page_id))
203 delegate_->OnInstantSupportDetermined(true);
204 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
207 delegate_->OnUndoMostVisitedDeletion(url);
210 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
211 if (!web_contents()->IsActiveEntry(page_id))
214 delegate_->OnInstantSupportDetermined(true);
215 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
218 delegate_->OnUndoAllMostVisitedDeletions();
221 void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
222 if (!web_contents()->IsActiveEntry(page_id))
225 delegate_->OnInstantSupportDetermined(true);
226 if (!policy_->ShouldProcessLogEvent())
229 delegate_->OnLogEvent(event);
232 void SearchIPCRouter::OnLogImpression(int page_id,
234 const base::string16& provider) const {
235 if (!web_contents()->IsActiveEntry(page_id))
238 // Only allow string of 8 alphanumeric characters or less as providers.
239 if (provider.length() > 8)
241 for (base::string16::const_iterator it = provider.begin();
242 it != provider.end(); ++it) {
243 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it))
247 delegate_->OnInstantSupportDetermined(true);
248 // Logging impressions is controlled by the same policy as logging events.
249 if (!policy_->ShouldProcessLogEvent())
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))
260 delegate_->OnInstantSupportDetermined(true);
261 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
264 delegate_->PasteIntoOmnibox(text);
267 void SearchIPCRouter::OnChromeIdentityCheck(
269 const base::string16& identity) const {
270 if (!web_contents()->IsActiveEntry(page_id))
273 delegate_->OnInstantSupportDetermined(true);
274 if (!policy_->ShouldProcessChromeIdentityCheck())
277 delegate_->OnChromeIdentityCheck(identity);
280 void SearchIPCRouter::set_delegate(Delegate* delegate) {
282 delegate_ = delegate;
285 void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
286 DCHECK(policy.get());
287 policy_.reset(policy.release());