- add sources.
[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::SetPromoInformation(bool is_app_launcher_enabled) {
29   if (!policy_->ShouldSendSetPromoInformation())
30     return;
31
32   Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
33                                                    is_app_launcher_enabled));
34 }
35
36 void SearchIPCRouter::SetDisplayInstantResults() {
37   if (!policy_->ShouldSendSetDisplayInstantResults())
38     return;
39
40   bool is_search_results_page = !chrome::GetSearchTerms(web_contents()).empty();
41   Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
42        routing_id(),
43        is_search_results_page && chrome::ShouldPrefetchSearchResultsOnSRP()));
44 }
45
46 void SearchIPCRouter::SendThemeBackgroundInfo(
47     const ThemeBackgroundInfo& theme_info) {
48   if (!policy_->ShouldSendThemeBackgroundInfo())
49     return;
50
51   Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
52 }
53
54 void SearchIPCRouter::SendMostVisitedItems(
55     const std::vector<InstantMostVisitedItem>& items) {
56   if (!policy_->ShouldSendMostVisitedItems())
57     return;
58
59   Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(
60       routing_id(), items));
61 }
62
63 void SearchIPCRouter::SendChromeIdentityCheckResult(
64     const string16& identity,
65     bool identity_match) {
66   if (!policy_->ShouldProcessChromeIdentityCheck())
67     return;
68
69   Send(new ChromeViewMsg_ChromeIdentityCheckResult(
70       routing_id(),
71       identity,
72       identity_match));
73 }
74
75 void SearchIPCRouter::SetSuggestionToPrefetch(
76     const InstantSuggestion& suggestion) {
77   if (!policy_->ShouldSendSetSuggestionToPrefetch())
78     return;
79
80   Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
81                                                           suggestion));
82 }
83
84 void SearchIPCRouter::Submit(const string16& text) {
85   if (!policy_->ShouldSubmitQuery())
86     return;
87
88   Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text));
89 }
90
91 void SearchIPCRouter::OnTabActivated() {
92   is_active_tab_ = true;
93 }
94
95 void SearchIPCRouter::OnTabDeactivated() {
96   is_active_tab_ = false;
97 }
98
99 bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
100   bool handled = true;
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()
122   return handled;
123 }
124
125 void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
126                                                  bool instant_support) const {
127   if (!web_contents()->IsActiveEntry(page_id))
128     return;
129
130   delegate_->OnInstantSupportDetermined(instant_support);
131 }
132
133 void SearchIPCRouter::OnVoiceSearchSupportDetermined(
134     int page_id,
135     bool supports_voice_search) const {
136   if (!web_contents()->IsActiveEntry(page_id))
137     return;
138
139   delegate_->OnInstantSupportDetermined(true);
140   if (!policy_->ShouldProcessSetVoiceSearchSupport())
141     return;
142
143   delegate_->OnSetVoiceSearchSupport(supports_voice_search);
144 }
145
146 void SearchIPCRouter::OnFocusOmnibox(int page_id,
147                                      OmniboxFocusState state) const {
148   if (!web_contents()->IsActiveEntry(page_id))
149     return;
150
151   delegate_->OnInstantSupportDetermined(true);
152   if (!policy_->ShouldProcessFocusOmnibox(is_active_tab_))
153     return;
154
155   delegate_->FocusOmnibox(state);
156 }
157
158 void SearchIPCRouter::OnSearchBoxNavigate(
159     int page_id,
160     const GURL& url,
161     WindowOpenDisposition disposition,
162     bool is_most_visited_item_url) const {
163   if (!web_contents()->IsActiveEntry(page_id))
164     return;
165
166   delegate_->OnInstantSupportDetermined(true);
167   if (!policy_->ShouldProcessNavigateToURL(is_active_tab_))
168     return;
169
170   delegate_->NavigateToURL(url, disposition, is_most_visited_item_url);
171 }
172
173 void SearchIPCRouter::OnDeleteMostVisitedItem(int page_id,
174                                               const GURL& url) const {
175   if (!web_contents()->IsActiveEntry(page_id))
176     return;
177
178   delegate_->OnInstantSupportDetermined(true);
179   if (!policy_->ShouldProcessDeleteMostVisitedItem())
180     return;
181
182   delegate_->OnDeleteMostVisitedItem(url);
183 }
184
185 void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
186                                                 const GURL& url) const {
187   if (!web_contents()->IsActiveEntry(page_id))
188     return;
189
190   delegate_->OnInstantSupportDetermined(true);
191   if (!policy_->ShouldProcessUndoMostVisitedDeletion())
192     return;
193
194   delegate_->OnUndoMostVisitedDeletion(url);
195 }
196
197 void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
198   if (!web_contents()->IsActiveEntry(page_id))
199     return;
200
201   delegate_->OnInstantSupportDetermined(true);
202   if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
203     return;
204
205   delegate_->OnUndoAllMostVisitedDeletions();
206 }
207
208 void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
209   if (!web_contents()->IsActiveEntry(page_id))
210     return;
211
212   delegate_->OnInstantSupportDetermined(true);
213   if (!policy_->ShouldProcessLogEvent())
214     return;
215
216   delegate_->OnLogEvent(event);
217 }
218
219 void SearchIPCRouter::OnPasteAndOpenDropDown(int page_id,
220                                              const string16& text) const {
221   if (!web_contents()->IsActiveEntry(page_id))
222     return;
223
224   delegate_->OnInstantSupportDetermined(true);
225   if (!policy_->ShouldProcessPasteIntoOmnibox(is_active_tab_))
226     return;
227
228   delegate_->PasteIntoOmnibox(text);
229 }
230
231 void SearchIPCRouter::OnChromeIdentityCheck(int page_id,
232                                             const string16& identity) const {
233   if (!web_contents()->IsActiveEntry(page_id))
234     return;
235
236   delegate_->OnInstantSupportDetermined(true);
237   if (!policy_->ShouldProcessChromeIdentityCheck())
238     return;
239
240   delegate_->OnChromeIdentityCheck(identity);
241 }
242
243 void SearchIPCRouter::set_delegate(Delegate* delegate) {
244   DCHECK(delegate);
245   delegate_ = delegate;
246 }
247
248 void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
249   DCHECK(policy.get());
250   policy_.reset(policy.release());
251 }