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/profiles/profile.h"
8 #include "chrome/browser/search/search.h"
9 #include "chrome/common/render_messages.h"
10 #include "content/public/browser/navigation_details.h"
11 #include "content/public/browser/web_contents.h"
15 bool IsProviderValid(const base::string16& provider) {
16 // Only allow string of 8 alphanumeric characters or less as providers.
17 // The empty string is considered valid and should be treated as if no
18 // provider were specified.
19 if (provider.length() > 8)
21 for (base::string16::const_iterator it = provider.begin();
22 it != provider.end(); ++it) {
23 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it))
31 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
32 Delegate* delegate, scoped_ptr<Policy> policy)
33 : WebContentsObserver(web_contents),
35 policy_(policy.Pass()),
37 is_active_tab_(false) {
40 DCHECK(policy_.get());
43 SearchIPCRouter::~SearchIPCRouter() {}
45 void SearchIPCRouter::OnNavigationEntryCommitted() {
47 Send(new ChromeViewMsg_SetPageSequenceNumber(routing_id(), commit_counter_));
50 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
51 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
54 void SearchIPCRouter::SendChromeIdentityCheckResult(
55 const base::string16& identity,
56 bool identity_match) {
57 if (!policy_->ShouldProcessChromeIdentityCheck())
60 Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity,
64 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
65 if (!policy_->ShouldSendSetPromoInformation())
68 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
69 is_app_launcher_enabled));
72 void SearchIPCRouter::SetDisplayInstantResults() {
73 if (!policy_->ShouldSendSetDisplayInstantResults())
76 bool is_search_results_page = !chrome::GetSearchTerms(web_contents()).empty();
77 bool display_instant_results = is_search_results_page ?
78 chrome::ShouldPrefetchSearchResultsOnSRP() :
79 chrome::ShouldPrefetchSearchResults();
80 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
81 routing_id(), display_instant_results));
84 void SearchIPCRouter::SetSuggestionToPrefetch(
85 const InstantSuggestion& suggestion) {
86 if (!policy_->ShouldSendSetSuggestionToPrefetch())
89 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
93 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin) {
94 if (!policy_->ShouldSendSetOmniboxStartMargin())
97 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin));
100 void SearchIPCRouter::SetInputInProgress(bool input_in_progress) {
101 if (!policy_->ShouldSendSetInputInProgress(is_active_tab_))
104 Send(new ChromeViewMsg_SearchBoxSetInputInProgress(routing_id(),
108 void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state,
109 OmniboxFocusChangeReason reason) {
110 if (!policy_->ShouldSendOmniboxFocusChanged())
113 Send(new ChromeViewMsg_SearchBoxFocusChanged(routing_id(), state, reason));
116 void SearchIPCRouter::SendMostVisitedItems(
117 const std::vector<InstantMostVisitedItem>& items) {
118 if (!policy_->ShouldSendMostVisitedItems())
121 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
124 void SearchIPCRouter::SendThemeBackgroundInfo(
125 const ThemeBackgroundInfo& theme_info) {
126 if (!policy_->ShouldSendThemeBackgroundInfo())
129 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
132 void SearchIPCRouter::ToggleVoiceSearch() {
133 if (!policy_->ShouldSendToggleVoiceSearch())
136 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
139 void SearchIPCRouter::Submit(const base::string16& text,
140 const EmbeddedSearchRequestParams& params) {
141 if (!policy_->ShouldSubmitQuery())
144 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text, params));
147 void SearchIPCRouter::OnTabActivated() {
148 is_active_tab_ = true;
151 void SearchIPCRouter::OnTabDeactivated() {
152 is_active_tab_ = false;
155 bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
157 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
158 if (!chrome::IsRenderedInInstantProcess(web_contents(), profile))
162 IPC_BEGIN_MESSAGE_MAP(SearchIPCRouter, message)
163 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
164 OnInstantSupportDetermined)
165 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetVoiceSearchSupported,
166 OnVoiceSearchSupportDetermined)
167 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox);
168 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
169 OnSearchBoxNavigate);
170 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
171 OnDeleteMostVisitedItem);
172 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
173 OnUndoMostVisitedDeletion);
174 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
175 OnUndoAllMostVisitedDeletions);
176 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent, OnLogEvent);
177 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogMostVisitedImpression,
178 OnLogMostVisitedImpression);
179 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogMostVisitedNavigation,
180 OnLogMostVisitedNavigation);
181 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PasteAndOpenDropdown,
182 OnPasteAndOpenDropDown);
183 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck,
184 OnChromeIdentityCheck);
185 IPC_MESSAGE_UNHANDLED(handled = false)
186 IPC_END_MESSAGE_MAP()
190 void SearchIPCRouter::OnInstantSupportDetermined(int page_seq_no,
191 bool instant_support) const {
192 if (page_seq_no != commit_counter_)
195 delegate_->OnInstantSupportDetermined(instant_support);
198 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
200 bool supports_voice_search) const {
201 if (page_seq_no != commit_counter_)
204 delegate_->OnInstantSupportDetermined(true);
205 if (!policy_->ShouldProcessSetVoiceSearchSupport())
208 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
211 void SearchIPCRouter::OnFocusOmnibox(int page_seq_no,
212 OmniboxFocusState state) const {
213 if (page_seq_no != commit_counter_)
216 delegate_->OnInstantSupportDetermined(true);
217 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
220 delegate_->FocusOmnibox(state);
223 void SearchIPCRouter::OnSearchBoxNavigate(
226 WindowOpenDisposition disposition,
227 bool is_most_visited_item_url) const {
228 if (page_seq_no != commit_counter_)
231 delegate_->OnInstantSupportDetermined(true);
232 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
235 delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
238 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_seq_no,
239 const GURL& url) const {
240 if (page_seq_no != commit_counter_)
243 delegate_->OnInstantSupportDetermined(true);
244 if (!policy_->ShouldProcessDeleteMostVisitedItem())
247 delegate_->OnDeleteMostVisitedItem(url);
250 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_seq_no,
251 const GURL& url) const {
252 if (page_seq_no != commit_counter_)
255 delegate_->OnInstantSupportDetermined(true);
256 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
259 delegate_->OnUndoMostVisitedDeletion(url);
262 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_seq_no) const {
263 if (page_seq_no != commit_counter_)
266 delegate_->OnInstantSupportDetermined(true);
267 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
270 delegate_->OnUndoAllMostVisitedDeletions();
273 void SearchIPCRouter::OnLogEvent(int page_seq_no,
274 NTPLoggingEventType event) const {
275 if (page_seq_no != commit_counter_)
278 delegate_->OnInstantSupportDetermined(true);
279 if (!policy_->ShouldProcessLogEvent())
282 delegate_->OnLogEvent(event);
285 void SearchIPCRouter::OnLogMostVisitedImpression(
286 int page_seq_no, int position, const base::string16& provider) const {
287 if (page_seq_no != commit_counter_ || !IsProviderValid(provider))
290 delegate_->OnInstantSupportDetermined(true);
291 // Logging impressions is controlled by the same policy as logging events.
292 if (!policy_->ShouldProcessLogEvent())
295 delegate_->OnLogMostVisitedImpression(position, provider);
298 void SearchIPCRouter::OnLogMostVisitedNavigation(
299 int page_seq_no, int position, const base::string16& provider) const {
300 if (page_seq_no != commit_counter_ || !IsProviderValid(provider))
303 delegate_->OnInstantSupportDetermined(true);
304 // Logging navigations is controlled by the same policy as logging events.
305 if (!policy_->ShouldProcessLogEvent())
308 delegate_->OnLogMostVisitedNavigation(position, provider);
311 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_seq_no,
312 const base::string16& text) const {
313 if (page_seq_no != commit_counter_)
316 delegate_->OnInstantSupportDetermined(true);
317 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
320 delegate_->PasteIntoOmnibox(text);
323 void SearchIPCRouter::OnChromeIdentityCheck(
325 const base::string16& identity) const {
326 if (page_seq_no != commit_counter_)
329 delegate_->OnInstantSupportDetermined(true);
330 if (!policy_->ShouldProcessChromeIdentityCheck())
333 delegate_->OnChromeIdentityCheck(identity);
336 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) {
338 delegate_ = delegate;
341 void SearchIPCRouter::set_policy_for_testing(scoped_ptr<Policy> policy) {
342 DCHECK(policy.get());
343 policy_.reset(policy.release());