search_ipc_router.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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
11SearchIPCRouter::SearchIPCRouter(content::WebContents* web_contents,
12                                 Delegate* delegate, scoped_ptr<Policy> policy)
13    : WebContentsObserver(web_contents),
14      delegate_(delegate),
15      policy_(policy.Pass()) {
16  DCHECK(web_contents);
17  DCHECK(delegate);
18  DCHECK(policy_.get());
19}
20
21SearchIPCRouter::~SearchIPCRouter() {}
22
23void SearchIPCRouter::DetermineIfPageSupportsInstant() {
24  Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
25}
26
27void SearchIPCRouter::SetPromoInformation(bool is_app_launcher_enabled) {
28  if (!policy_->ShouldSendSetPromoInformation())
29    return;
30
31  Send(new ChromeViewMsg_SearchBoxPromoInformation(routing_id(),
32                                                   is_app_launcher_enabled));
33}
34
35void SearchIPCRouter::SetDisplayInstantResults() {
36  if (!policy_->ShouldSendSetDisplayInstantResults())
37    return;
38
39  Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults(
40       routing_id(),
41       chrome::ShouldPrefetchSearchResultsOnSRP()));
42}
43
44void SearchIPCRouter::SendThemeBackgroundInfo(
45    const ThemeBackgroundInfo& theme_info) {
46  if (!policy_->ShouldSendThemeBackgroundInfo())
47    return;
48
49  Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info));
50}
51
52void SearchIPCRouter::SendMostVisitedItems(
53    const std::vector<InstantMostVisitedItem>& items) {
54  if (!policy_->ShouldSendMostVisitedItems())
55    return;
56
57  Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(
58      routing_id(), items));
59}
60
61void SearchIPCRouter::SetSuggestionToPrefetch(
62    const InstantSuggestion& suggestion) {
63  if (!policy_->ShouldSendSetSuggestionToPrefetch())
64    return;
65
66  Send(new ChromeViewMsg_SearchBoxSetSuggestionToPrefetch(routing_id(),
67                                                          suggestion));
68}
69
70void SearchIPCRouter::Submit(const string16& text) {
71  if (!policy_->ShouldSubmitQuery())
72    return;
73
74  Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text));
75}
76
77bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) {
78  bool handled = true;
79  IPC_BEGIN_MESSAGE_MAP(SearchIPCRouter, message)
80    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
81                        OnInstantSupportDetermined)
82    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetVoiceSearchSupported,
83                        OnVoiceSearchSupportDetermined)
84    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox);
85    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
86                        OnDeleteMostVisitedItem);
87    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
88                        OnUndoMostVisitedDeletion);
89    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
90                        OnUndoAllMostVisitedDeletions);
91    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent, OnLogEvent);
92    IPC_MESSAGE_UNHANDLED(handled = false)
93  IPC_END_MESSAGE_MAP()
94  return handled;
95}
96
97void SearchIPCRouter::OnInstantSupportDetermined(int page_id,
98                                                 bool instant_support) const {
99  if (!web_contents()->IsActiveEntry(page_id))
100    return;
101
102  delegate_->OnInstantSupportDetermined(instant_support);
103}
104
105void SearchIPCRouter::OnVoiceSearchSupportDetermined(
106    int page_id,
107    bool supports_voice_search) const {
108  if (!web_contents()->IsActiveEntry(page_id))
109    return;
110
111  delegate_->OnInstantSupportDetermined(true);
112  if (!policy_->ShouldProcessSetVoiceSearchSupport())
113    return;
114
115  delegate_->OnSetVoiceSearchSupport(supports_voice_search);
116}
117
118void SearchIPCRouter::OnFocusOmnibox(int page_id,
119                                     OmniboxFocusState state) const {
120  if (!web_contents()->IsActiveEntry(page_id))
121    return;
122
123  delegate_->OnInstantSupportDetermined(true);
124  if (!policy_->ShouldProcessFocusOmnibox())
125    return;
126
127  delegate_->FocusOmnibox(state);
128}
129
130void SearchIPCRouter::OnDeleteMostVisitedItem(int page_id,
131                                              const GURL& url) const {
132  if (!web_contents()->IsActiveEntry(page_id))
133    return;
134
135  delegate_->OnInstantSupportDetermined(true);
136  if (!policy_->ShouldProcessDeleteMostVisitedItem())
137    return;
138
139  delegate_->OnDeleteMostVisitedItem(url);
140}
141
142void SearchIPCRouter::OnUndoMostVisitedDeletion(int page_id,
143                                                const GURL& url) const {
144  if (!web_contents()->IsActiveEntry(page_id))
145    return;
146
147  delegate_->OnInstantSupportDetermined(true);
148  if (!policy_->ShouldProcessUndoMostVisitedDeletion())
149    return;
150
151  delegate_->OnUndoMostVisitedDeletion(url);
152}
153
154void SearchIPCRouter::OnUndoAllMostVisitedDeletions(int page_id) const {
155  if (!web_contents()->IsActiveEntry(page_id))
156    return;
157
158  delegate_->OnInstantSupportDetermined(true);
159  if (!policy_->ShouldProcessUndoAllMostVisitedDeletions())
160    return;
161
162  delegate_->OnUndoAllMostVisitedDeletions();
163}
164
165void SearchIPCRouter::OnLogEvent(int page_id, NTPLoggingEventType event) const {
166  if (!web_contents()->IsActiveEntry(page_id))
167    return;
168
169  delegate_->OnInstantSupportDetermined(true);
170  if (!policy_->ShouldProcessLogEvent())
171    return;
172
173  delegate_->OnLogEvent(event);
174}
175
176void SearchIPCRouter::set_delegate(Delegate* delegate) {
177  DCHECK(delegate);
178  delegate_ = delegate;
179}
180
181void SearchIPCRouter::set_policy(scoped_ptr<Policy> policy) {
182  DCHECK(policy.get());
183  policy_.reset(policy.release());
184}
185