Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search / search_ipc_router.cc
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.
4
5 #include "chrome/browser/ui/search/search_ipc_router.h"
6
7 #include "chrome/browser/search/search.h"
8 #include "chrome/common/render_messages.h"
9 #include "content/public/browser/web_contents.h"
10
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());
20 }
21
22 SearchIPCRouter::~SearchIPCRouter() {}
23
24 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
25   Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
26 }
27
28 void SearchIPCRouter::SendChromeIdentityCheckResult(
29     const base::string16& identity,
30     bool identity_match) {
31   if (!policy_->ShouldProcessChromeIdentityCheck())
32     return;
33
34   Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity,
35                                                    identity_match));
36 }
37
38 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
39   if (!policy_->ShouldSendSetPromoInformation())
40     return;
41
42   Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
43                                                    is_app_launcher_enabled));
44 }
45
46 void SearchIPCRouter::SetDisplayInstantResults() {
47   if (!policy_->ShouldSendSetDisplayInstantResults())
48     return;
49
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()));
55 }
56
57 void SearchIPCRouter::SetSuggestionToPrefetch(
58     const InstantSuggestion& suggestion) {
59   if (!policy_->ShouldSendSetSuggestionToPrefetch())
60     return;
61
62   Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
63                                                           suggestion));
64 }
65
66 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin) {
67   if (!policy_->ShouldSendSetOmniboxStartMargin())
68     return;
69
70   Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin));
71 }
72
73 void SearchIPCRouter::SendMostVisitedItems(
74     const std::vector<InstantMostVisitedItem>& items) {
75   if (!policy_->ShouldSendMostVisitedItems())
76     return;
77
78   Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
79 }
80
81 void SearchIPCRouter::SendThemeBackgroundInfo(
82     const ThemeBackgroundInfo& theme_info) {
83   if (!policy_->ShouldSendThemeBackgroundInfo())
84     return;
85
86   Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
87 }
88
89 void SearchIPCRouter::ToggleVoiceSearch() {
90   if (!policy_->ShouldSendToggleVoiceSearch())
91     return;
92
93   Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
94 }
95
96 void SearchIPCRouter::Submit(const base::string16& text) {
97   if (!policy_->ShouldSubmitQuery())
98     return;
99
100   Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text));
101 }
102
103 void SearchIPCRouter::OnTabActivated() {
104   is_active_tab_ = true;
105 }
106
107 void SearchIPCRouter::OnTabDeactivated() {
108   is_active_tab_ = false;
109 }
110
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;
136 }
137
138 void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
139                                                  bool instant_support) const {
140   if (!web_contents()->IsActiveEntry(page_id))
141     return;
142
143   delegate_->OnInstantSupportDetermined(instant_support);
144 }
145
146 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
147     int page_id,
148     bool supports_voice_search) const {
149   if (!web_contents()->IsActiveEntry(page_id))
150     return;
151
152   delegate_->OnInstantSupportDetermined(true);
153   if (!policy_->ShouldProcessSetVoiceSearchSupport())
154     return;
155
156   delegate_->OnSetVoiceSearchSupport(supports_voice_search);
157 }
158
159 void SearchIPCRouter::OnFocusOmnibox(int page_id,
160                                      OmniboxFocusState state) const {
161   if (!web_contents()->IsActiveEntry(page_id))
162     return;
163
164   delegate_->OnInstantSupportDetermined(true);
165   if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
166     return;
167
168   delegate_->FocusOmnibox(state);
169 }
170
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;
178
179   delegate_->OnInstantSupportDetermined(true);
180   if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
181     return;
182
183   delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
184 }
185
186 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_id,
187                                               const GURL& url) const {
188   if (!web_contents()->IsActiveEntry(page_id))
189     return;
190
191   delegate_->OnInstantSupportDetermined(true);
192   if (!policy_->ShouldProcessDeleteMostVisitedItem())
193     return;
194
195   delegate_->OnDeleteMostVisitedItem(url);
196 }
197
198 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
199                                                 const GURL& url) const {
200   if (!web_contents()->IsActiveEntry(page_id))
201     return;
202
203   delegate_->OnInstantSupportDetermined(true);
204   if (!policy_->ShouldProcessUndoMostVisitedDeletion())
205     return;
206
207   delegate_->OnUndoMostVisitedDeletion(url);
208 }
209
210 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
211   if (!web_contents()->IsActiveEntry(page_id))
212     return;
213
214   delegate_->OnInstantSupportDetermined(true);
215   if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
216     return;
217
218   delegate_->OnUndoAllMostVisitedDeletions();
219 }
220
221 void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
222   if (!web_contents()->IsActiveEntry(page_id))
223     return;
224
225   delegate_->OnInstantSupportDetermined(true);
226   if (!policy_->ShouldProcessLogEvent())
227     return;
228
229   delegate_->OnLogEvent(event);
230 }
231
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;
237
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;
245   }
246
247   delegate_->OnInstantSupportDetermined(true);
248   // Logging impressions is controlled by the same policy as logging events.
249   if (!policy_->ShouldProcessLogEvent())
250     return;
251
252   delegate_->OnLogImpression(position, provider);
253 }
254
255 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_id,
256                                              const base::string16& text) const {
257   if (!web_contents()->IsActiveEntry(page_id))
258     return;
259
260   delegate_->OnInstantSupportDetermined(true);
261   if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
262     return;
263
264   delegate_->PasteIntoOmnibox(text);
265 }
266
267 void SearchIPCRouter::OnChromeIdentityCheck(
268     int page_id,
269     const base::string16& identity) const {
270   if (!web_contents()->IsActiveEntry(page_id))
271     return;
272
273   delegate_->OnInstantSupportDetermined(true);
274   if (!policy_->ShouldProcessChromeIdentityCheck())
275     return;
276
277   delegate_->OnChromeIdentityCheck(identity);
278 }
279
280 void SearchIPCRouter::set_delegate(Delegate* delegate) {
281   DCHECK(delegate);
282   delegate_ = delegate;
283 }
284
285 void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
286   DCHECK(policy.get());
287   policy_.reset(policy.release());
288 }