Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / search / search_ipc_router.h
blob899bdcd0f3c955e456824c301341295ef6485aa4
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 #ifndef CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
6 #define CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
8 #include <vector>
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "chrome/common/instant_types.h"
14 #include "chrome/common/ntp_logging_events.h"
15 #include "chrome/common/omnibox_focus_state.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "ui/base/window_open_disposition.h"
19 class GURL;
21 namespace content {
22 class WebContents;
25 class SearchIPCRouterTest;
27 // SearchIPCRouter is responsible for receiving and sending IPC messages between
28 // the browser and the Instant page.
29 class SearchIPCRouter : public content::WebContentsObserver {
30 public:
31 // SearchIPCRouter calls its delegate in response to messages received from
32 // the page.
33 class Delegate {
34 public:
35 // Called upon determination of Instant API support in response to the page
36 // load event.
37 virtual void OnInstantSupportDetermined(bool supports_instant) = 0;
39 // Called upon determination of voice search API support.
40 virtual void OnSetVoiceSearchSupport(bool supports_voice_search) = 0;
42 // Called when the page wants the omnibox to be focused. |state| specifies
43 // the omnibox focus state.
44 virtual void FocusOmnibox(OmniboxFocusState state) = 0;
46 // Called when the page wants to navigate to |url|. Usually used by the
47 // page to navigate to privileged destinations (e.g. chrome:// URLs) or to
48 // navigate to URLs that are hidden from the page using Restricted IDs (rid
49 // in the API).
50 virtual void NavigateToURL(const GURL& url,
51 WindowOpenDisposition disposition,
52 bool is_most_visited_item_url) = 0;
54 // Called when the SearchBox wants to delete a Most Visited item.
55 virtual void OnDeleteMostVisitedItem(const GURL& url) = 0;
57 // Called when the SearchBox wants to undo a Most Visited deletion.
58 virtual void OnUndoMostVisitedDeletion(const GURL& url) = 0;
60 // Called when the SearchBox wants to undo all Most Visited deletions.
61 virtual void OnUndoAllMostVisitedDeletions() = 0;
63 // Called to signal that an event has occurred on the New Tab Page at a
64 // particular time since navigation start.
65 virtual void OnLogEvent(NTPLoggingEventType event,
66 base::TimeDelta time) = 0;
68 // Called to log an impression from a given provider on the New Tab Page.
69 virtual void OnLogMostVisitedImpression(int position,
70 const base::string16& provider) = 0;
72 // Called to log a navigation from a given provider on the New Tab Page.
73 virtual void OnLogMostVisitedNavigation(int position,
74 const base::string16& provider) = 0;
76 // Called when the page wants to paste the |text| (or the clipboard contents
77 // if the |text| is empty) into the omnibox.
78 virtual void PasteIntoOmnibox(const base::string16& text) = 0;
80 // Called when the SearchBox wants to verify the signed-in Chrome identity
81 // against the provided |identity|. Will make a round-trip to the browser
82 // and eventually return the result through SendChromeIdentityCheckResult.
83 // Calls SendChromeIdentityCheckResult with true if the identity matches.
84 virtual void OnChromeIdentityCheck(const base::string16& identity) = 0;
86 // Called when the SearchBox wants to verify the signed-in Chrome identity
87 // against the provided |identity|. Will make a round-trip to the browser
88 // and eventually return the result through SendHistorySyncCheckResult.
89 // Calls SendHistorySyncCheckResult with true if the user syncs their
90 // history.
91 virtual void OnHistorySyncCheck() = 0;
94 // An interface to be implemented by consumers of SearchIPCRouter objects to
95 // decide whether to process the message received from the page, and vice
96 // versa (decide whether to send messages to the page).
97 class Policy {
98 public:
99 virtual ~Policy() {}
101 // SearchIPCRouter calls these functions before sending/receiving messages
102 // to/from the page.
103 virtual bool ShouldProcessSetVoiceSearchSupport() = 0;
104 virtual bool ShouldProcessFocusOmnibox(bool is_active_tab) = 0;
105 virtual bool ShouldProcessNavigateToURL(bool is_active_tab) = 0;
106 virtual bool ShouldProcessDeleteMostVisitedItem() = 0;
107 virtual bool ShouldProcessUndoMostVisitedDeletion() = 0;
108 virtual bool ShouldProcessUndoAllMostVisitedDeletions() = 0;
109 virtual bool ShouldProcessLogEvent() = 0;
110 virtual bool ShouldProcessPasteIntoOmnibox(bool is_active_tab) = 0;
111 virtual bool ShouldProcessChromeIdentityCheck() = 0;
112 virtual bool ShouldProcessHistorySyncCheck() = 0;
113 virtual bool ShouldSendSetPromoInformation() = 0;
114 virtual bool ShouldSendSetDisplayInstantResults() = 0;
115 virtual bool ShouldSendSetSuggestionToPrefetch() = 0;
116 virtual bool ShouldSendSetOmniboxStartMargin() = 0;
117 virtual bool ShouldSendSetInputInProgress(bool is_active_tab) = 0;
118 virtual bool ShouldSendOmniboxFocusChanged() = 0;
119 virtual bool ShouldSendMostVisitedItems() = 0;
120 virtual bool ShouldSendThemeBackgroundInfo() = 0;
121 virtual bool ShouldSendToggleVoiceSearch() = 0;
122 virtual bool ShouldSubmitQuery() = 0;
125 SearchIPCRouter(content::WebContents* web_contents, Delegate* delegate,
126 scoped_ptr<Policy> policy);
127 ~SearchIPCRouter() override;
129 // Tells the SearchIPCRouter that a new page in an Instant process committed.
130 void OnNavigationEntryCommitted();
132 // Tells the renderer to determine if the page supports the Instant API, which
133 // results in a call to OnInstantSupportDetermined() when the reply is
134 // received.
135 void DetermineIfPageSupportsInstant();
137 // Tells the renderer about the result of the Chrome identity check.
138 void SendChromeIdentityCheckResult(const base::string16& identity,
139 bool identity_match);
141 // Tells the renderer whether the user syncs history.
142 void SendHistorySyncCheckResult(bool sync_history);
144 // Tells the renderer information it needs to display promos.
145 void SetPromoInformation(bool is_app_launcher_enabled);
147 // Tells the renderer whether to display the Instant results.
148 void SetDisplayInstantResults();
150 // Tells the page the suggestion to be prefetched if any.
151 void SetSuggestionToPrefetch(const InstantSuggestion& suggestion);
153 // Tells the page the left margin of the omnibox. This is used by the page to
154 // align text or assets properly with the omnibox.
155 void SetOmniboxStartMargin(int start_margin);
157 // Tells the page that user input started or stopped.
158 void SetInputInProgress(bool input_in_progress);
160 // Tells the page that the omnibox focus has changed.
161 void OmniboxFocusChanged(OmniboxFocusState state,
162 OmniboxFocusChangeReason reason);
164 // Tells the renderer about the most visited items.
165 void SendMostVisitedItems(const std::vector<InstantMostVisitedItem>& items);
167 // Tells the renderer about the current theme background.
168 void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info);
170 // Tells the page to toggle voice search.
171 void ToggleVoiceSearch();
173 // Tells the page that the user pressed Enter in the omnibox.
174 void Submit(const base::string16& text,
175 const EmbeddedSearchRequestParams& params);
177 // Called when the tab corresponding to |this| instance is activated.
178 void OnTabActivated();
180 // Called when the tab corresponding to |this| instance is deactivated.
181 void OnTabDeactivated();
183 private:
184 friend class SearchIPCRouterPolicyTest;
185 friend class SearchIPCRouterTest;
186 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
187 DetermineIfPageSupportsInstant_Local);
188 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
189 DetermineIfPageSupportsInstant_NonLocal);
190 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
191 PageURLDoesntBelongToInstantRenderer);
192 FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
193 IgnoreMessageIfThePageIsNotActive);
194 FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
195 DoNotSendSetDisplayInstantResultsMsg);
196 FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, HandleTabChangedEvents);
198 // Overridden from contents::WebContentsObserver:
199 bool OnMessageReceived(const IPC::Message& message) override;
201 void OnInstantSupportDetermined(int page_seq_no, bool supports_instant) const;
202 void OnVoiceSearchSupportDetermined(int page_id,
203 bool supports_voice_search) const;
204 void OnFocusOmnibox(int page_id, OmniboxFocusState state) const;
205 void OnSearchBoxNavigate(int page_id,
206 const GURL& url,
207 WindowOpenDisposition disposition,
208 bool is_most_visited_item_url) const;
209 void OnDeleteMostVisitedItem(int page_seq_no, const GURL& url) const;
210 void OnUndoMostVisitedDeletion(int page_seq_no, const GURL& url) const;
211 void OnUndoAllMostVisitedDeletions(int page_seq_no) const;
212 void OnLogEvent(int page_seq_no,
213 NTPLoggingEventType event,
214 base::TimeDelta time) const;
215 void OnLogMostVisitedImpression(int page_seq_no,
216 int position,
217 const base::string16& provider) const;
218 void OnLogMostVisitedNavigation(int page_seq_no,
219 int position,
220 const base::string16& provider) const;
221 void OnPasteAndOpenDropDown(int page_seq_no,
222 const base::string16& text) const;
223 void OnChromeIdentityCheck(int page_seq_no,
224 const base::string16& identity) const;
225 void OnHistorySyncCheck(int page_seq_no) const;
227 // Used by unit tests to set a fake delegate.
228 void set_delegate_for_testing(Delegate* delegate);
230 // Used by unit tests.
231 void set_policy_for_testing(scoped_ptr<Policy> policy);
233 // Used by unit tests.
234 Policy* policy_for_testing() const { return policy_.get(); }
236 // Used by unit tests.
237 int page_seq_no_for_testing() const { return commit_counter_; }
239 Delegate* delegate_;
240 scoped_ptr<Policy> policy_;
242 // Holds the number of main frame commits executed in this tab. Used by the
243 // SearchIPCRouter to ensure that delayed IPC replies are ignored.
244 int commit_counter_;
246 // Set to true, when the tab corresponding to |this| instance is active.
247 bool is_active_tab_;
249 DISALLOW_COPY_AND_ASSIGN(SearchIPCRouter);
252 #endif // CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_