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::SetPromoInformation(bool is_app_launcher_enabled) {
29 if (!policy_->ShouldSendSetPromoInformation())
32 Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
33 is_app_launcher_enabled));
36 void SearchIPCRouter::SetDisplayInstantResults() {
37 if (!policy_->ShouldSendSetDisplayInstantResults())
40 bool is_search_results_page = !chrome::GetSearchTerms(web_contents()).empty();
41 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
43 is_search_results_page && chrome::ShouldPrefetchSearchResultsOnSRP()));
46 void SearchIPCRouter::SendThemeBackgroundInfo(
47 const ThemeBackgroundInfo& theme_info) {
48 if (!policy_->ShouldSendThemeBackgroundInfo())
51 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
54 void SearchIPCRouter::SendMostVisitedItems(
55 const std::vector<InstantMostVisitedItem>& items) {
56 if (!policy_->ShouldSendMostVisitedItems())
59 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(
60 routing_id(), items));
63 void SearchIPCRouter::SendChromeIdentityCheckResult(
64 const string16& identity,
65 bool identity_match) {
66 if (!policy_->ShouldProcessChromeIdentityCheck())
69 Send(new ChromeViewMsg_ChromeIdentityCheckResult(
75 void SearchIPCRouter::SetSuggestionToPrefetch(
76 const InstantSuggestion& suggestion) {
77 if (!policy_->ShouldSendSetSuggestionToPrefetch())
80 Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
84 void SearchIPCRouter::Submit(const string16& text) {
85 if (!policy_->ShouldSubmitQuery())
88 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text));
91 void SearchIPCRouter::OnTabActivated() {
92 is_active_tab_ = true;
95 void SearchIPCRouter::OnTabDeactivated() {
96 is_active_tab_ = false;
99 bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
101 IPC_BEGIN_MESSAGE_MAP(SearchIPCRouter, message)
102 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
103 OnInstantSupportDetermined)
104 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetVoiceSearchSupported,
105 OnVoiceSearchSupportDetermined)
106 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox);
107 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
108 OnSearchBoxNavigate);
109 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
110 OnDeleteMostVisitedItem);
111 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
112 OnUndoMostVisitedDeletion);
113 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
114 OnUndoAllMostVisitedDeletions);
115 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent, OnLogEvent);
116 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PasteAndOpenDropdown,
117 OnPasteAndOpenDropDown);
118 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck,
119 OnChromeIdentityCheck);
120 IPC_MESSAGE_UNHANDLED(handled = false)
121 IPC_END_MESSAGE_MAP()
125 void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
126 bool instant_support) const {
127 if (!web_contents()->IsActiveEntry(page_id))
130 delegate_->OnInstantSupportDetermined(instant_support);
133 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
135 bool supports_voice_search) const {
136 if (!web_contents()->IsActiveEntry(page_id))
139 delegate_->OnInstantSupportDetermined(true);
140 if (!policy_->ShouldProcessSetVoiceSearchSupport())
143 delegate_->OnSetVoiceSearchSupport(supports_voice_search);
146 void SearchIPCRouter::OnFocusOmnibox(int page_id,
147 OmniboxFocusState state) const {
148 if (!web_contents()->IsActiveEntry(page_id))
151 delegate_->OnInstantSupportDetermined(true);
152 if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
155 delegate_->FocusOmnibox(state);
158 void SearchIPCRouter::OnSearchBoxNavigate(
161 WindowOpenDisposition disposition,
162 bool is_most_visited_item_url) const {
163 if (!web_contents()->IsActiveEntry(page_id))
166 delegate_->OnInstantSupportDetermined(true);
167 if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
170 delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
173 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_id,
174 const GURL& url) const {
175 if (!web_contents()->IsActiveEntry(page_id))
178 delegate_->OnInstantSupportDetermined(true);
179 if (!policy_->ShouldProcessDeleteMostVisitedItem())
182 delegate_->OnDeleteMostVisitedItem(url);
185 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
186 const GURL& url) const {
187 if (!web_contents()->IsActiveEntry(page_id))
190 delegate_->OnInstantSupportDetermined(true);
191 if (!policy_->ShouldProcessUndoMostVisitedDeletion())
194 delegate_->OnUndoMostVisitedDeletion(url);
197 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
198 if (!web_contents()->IsActiveEntry(page_id))
201 delegate_->OnInstantSupportDetermined(true);
202 if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
205 delegate_->OnUndoAllMostVisitedDeletions();
208 void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
209 if (!web_contents()->IsActiveEntry(page_id))
212 delegate_->OnInstantSupportDetermined(true);
213 if (!policy_->ShouldProcessLogEvent())
216 delegate_->OnLogEvent(event);
219 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_id,
220 const string16& text) const {
221 if (!web_contents()->IsActiveEntry(page_id))
224 delegate_->OnInstantSupportDetermined(true);
225 if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
228 delegate_->PasteIntoOmnibox(text);
231 void SearchIPCRouter::OnChromeIdentityCheck(int page_id,
232 const string16& identity) const {
233 if (!web_contents()->IsActiveEntry(page_id))
236 delegate_->OnInstantSupportDetermined(true);
237 if (!policy_->ShouldProcessChromeIdentityCheck())
240 delegate_->OnChromeIdentityCheck(identity);
243 void SearchIPCRouter::set_delegate(Delegate* delegate) {
245 delegate_ = delegate;
248 void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
249 DCHECK(policy.get());
250 policy_.reset(policy.release());