Update To 11.40.268.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/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"
12
13 namespace {
14
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)
20     return false;
21   for (base::string16::const_iterator it = provider.begin();
22        it != provider.end(); ++it) {
23     if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it))
24       return false;
25   }
26   return true;
27 }
28
29 }  // namespace
30
31 SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
32                                  Delegate* delegate, scoped_ptr<Policy> policy)
33     : WebContentsObserver(web_contents),
34       delegate_(delegate),
35       policy_(policy.Pass()),
36       commit_counter_(0),
37       is_active_tab_(false) {
38   DCHECK(web_contents);
39   DCHECK(delegate);
40   DCHECK(policy_.get());
41 }
42
43 SearchIPCRouter::~SearchIPCRouter() {}
44
45 void SearchIPCRouter::OnNavigationEntryCommitted() {
46   ++commit_counter_;
47   Send(new ChromeViewMsg_SetPageSequenceNumber(routing_id(), commit_counter_));
48 }
49
50 void SearchIPCRouter::DetermineIfPageSupportsInstant() {
51   Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
52 }
53
54 void SearchIPCRouter::SendChromeIdentityCheckResult(
55     const base::string16& identity,
56     bool identity_match) {
57   if (!policy_->ShouldProcessChromeIdentityCheck())
58     return;
59
60   Send(new ChromeViewMsg_ChromeIdentityCheckResult(routing_id(), identity,
61                                                    identity_match));
62 }
63
64 void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
65   if (!policy_->ShouldSendSetPromoInformation())
66     return;
67
68   Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
69                                                    is_app_launcher_enabled));
70 }
71
72 void SearchIPCRouter::SetDisplayInstantResults() {
73   if (!policy_->ShouldSendSetDisplayInstantResults())
74     return;
75
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));
82 }
83
84 void SearchIPCRouter::SetSuggestionToPrefetch(
85     const InstantSuggestion& suggestion) {
86   if (!policy_->ShouldSendSetSuggestionToPrefetch())
87     return;
88
89   Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
90                                                           suggestion));
91 }
92
93 void SearchIPCRouter::SetOmniboxStartMargin(int start_margin) {
94   if (!policy_->ShouldSendSetOmniboxStartMargin())
95     return;
96
97   Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start_margin));
98 }
99
100 void SearchIPCRouter::SetInputInProgress(bool input_in_progress) {
101   if (!policy_->ShouldSendSetInputInProgress(is_active_tab_))
102     return;
103
104   Send(new ChromeViewMsg_SearchBoxSetInputInProgress(routing_id(),
105                                                      input_in_progress));
106 }
107
108 void SearchIPCRouter::OmniboxFocusChanged(OmniboxFocusState state,
109                                           OmniboxFocusChangeReason reason) {
110   if (!policy_->ShouldSendOmniboxFocusChanged())
111     return;
112
113   Send(new ChromeViewMsg_SearchBoxFocusChanged(routing_id(), state, reason));
114 }
115
116 void SearchIPCRouter::SendMostVisitedItems(
117     const std::vector<InstantMostVisitedItem>& items) {
118   if (!policy_->ShouldSendMostVisitedItems())
119     return;
120
121   Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
122 }
123
124 void SearchIPCRouter::SendThemeBackgroundInfo(
125     const ThemeBackgroundInfo& theme_info) {
126   if (!policy_->ShouldSendThemeBackgroundInfo())
127     return;
128
129   Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
130 }
131
132 void SearchIPCRouter::ToggleVoiceSearch() {
133   if (!policy_->ShouldSendToggleVoiceSearch())
134     return;
135
136   Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
137 }
138
139 void SearchIPCRouter::Submit(const base::string16& text,
140                              const EmbeddedSearchRequestParams& params) {
141   if (!policy_->ShouldSubmitQuery())
142     return;
143
144   Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text, params));
145 }
146
147 void SearchIPCRouter::OnTabActivated() {
148   is_active_tab_ = true;
149 }
150
151 void SearchIPCRouter::OnTabDeactivated() {
152   is_active_tab_ = false;
153 }
154
155 bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
156   Profile* profile =
157       Profile::FromBrowserContext(web_contents()->GetBrowserContext());
158   if (!chrome::IsRenderedInInstantProcess(web_contents(), profile))
159     return false;
160
161   bool handled = true;
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()
187   return handled;
188 }
189
190 void SearchIPCRouter::OnInstantSupportDetermined(int page_seq_no,
191                                                  bool instant_support) const {
192   if (page_seq_no != commit_counter_)
193     return;
194
195   delegate_->OnInstantSupportDetermined(instant_support);
196 }
197
198 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
199     int page_seq_no,
200     bool supports_voice_search) const {
201   if (page_seq_no != commit_counter_)
202     return;
203
204   delegate_->OnInstantSupportDetermined(true);
205   if (!policy_->ShouldProcessSetVoiceSearchSupport())
206     return;
207
208   delegate_->OnSetVoiceSearchSupport(supports_voice_search);
209 }
210
211 void SearchIPCRouter::OnFocusOmnibox(int page_seq_no,
212                                      OmniboxFocusState state) const {
213   if (page_seq_no != commit_counter_)
214     return;
215
216   delegate_->OnInstantSupportDetermined(true);
217   if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
218     return;
219
220   delegate_->FocusOmnibox(state);
221 }
222
223 void SearchIPCRouter::OnSearchBoxNavigate(
224     int page_seq_no,
225     const GURL& url,
226     WindowOpenDisposition disposition,
227     bool is_most_visited_item_url) const {
228   if (page_seq_no != commit_counter_)
229     return;
230
231   delegate_->OnInstantSupportDetermined(true);
232   if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
233     return;
234
235   delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
236 }
237
238 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_seq_no,
239                                               const GURL& url) const {
240   if (page_seq_no != commit_counter_)
241     return;
242
243   delegate_->OnInstantSupportDetermined(true);
244   if (!policy_->ShouldProcessDeleteMostVisitedItem())
245     return;
246
247   delegate_->OnDeleteMostVisitedItem(url);
248 }
249
250 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_seq_no,
251                                                 const GURL& url) const {
252   if (page_seq_no != commit_counter_)
253     return;
254
255   delegate_->OnInstantSupportDetermined(true);
256   if (!policy_->ShouldProcessUndoMostVisitedDeletion())
257     return;
258
259   delegate_->OnUndoMostVisitedDeletion(url);
260 }
261
262 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_seq_no) const {
263   if (page_seq_no != commit_counter_)
264     return;
265
266   delegate_->OnInstantSupportDetermined(true);
267   if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
268     return;
269
270   delegate_->OnUndoAllMostVisitedDeletions();
271 }
272
273 void SearchIPCRouter::OnLogEvent(int page_seq_no,
274                                  NTPLoggingEventType event) const {
275   if (page_seq_no != commit_counter_)
276     return;
277
278   delegate_->OnInstantSupportDetermined(true);
279   if (!policy_->ShouldProcessLogEvent())
280     return;
281
282   delegate_->OnLogEvent(event);
283 }
284
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))
288     return;
289
290   delegate_->OnInstantSupportDetermined(true);
291   // Logging impressions is controlled by the same policy as logging events.
292   if (!policy_->ShouldProcessLogEvent())
293     return;
294
295   delegate_->OnLogMostVisitedImpression(position, provider);
296 }
297
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))
301     return;
302
303   delegate_->OnInstantSupportDetermined(true);
304   // Logging navigations is controlled by the same policy as logging events.
305   if (!policy_->ShouldProcessLogEvent())
306     return;
307
308   delegate_->OnLogMostVisitedNavigation(position, provider);
309 }
310
311 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_seq_no,
312                                              const base::string16& text) const {
313   if (page_seq_no != commit_counter_)
314     return;
315
316   delegate_->OnInstantSupportDetermined(true);
317   if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
318     return;
319
320   delegate_->PasteIntoOmnibox(text);
321 }
322
323 void SearchIPCRouter::OnChromeIdentityCheck(
324     int page_seq_no,
325     const base::string16& identity) const {
326   if (page_seq_no != commit_counter_)
327     return;
328
329   delegate_->OnInstantSupportDetermined(true);
330   if (!policy_->ShouldProcessChromeIdentityCheck())
331     return;
332
333   delegate_->OnChromeIdentityCheck(identity);
334 }
335
336 void SearchIPCRouter::set_delegate_for_testing(Delegate* delegate) {
337   DCHECK(delegate);
338   delegate_ = delegate;
339 }
340
341 void SearchIPCRouter::set_policy_for_testing(scoped_ptr<Policy> policy) {
342   DCHECK(policy.get());
343   policy_.reset(policy.release());
344 }