Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search / search_ipc_router.h
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 #ifndef CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
6 #define CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
7
8 #include <vector>
9
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/common/instant_types.h"
13 #include "chrome/common/ntp_logging_events.h"
14 #include "chrome/common/omnibox_focus_state.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "ui/base/window_open_disposition.h"
17
18 class GURL;
19
20 namespace content {
21 class WebContents;
22 }
23
24 class SearchIPCRouterTest;
25
26 // SearchIPCRouter is responsible for receiving and sending IPC messages between
27 // the browser and the Instant page.
28 class SearchIPCRouter : public content::WebContentsObserver {
29  public:
30   // SearchIPCRouter calls its delegate in response to messages received from
31   // the page.
32   class Delegate {
33    public:
34     // Called upon determination of Instant API support in response to the page
35     // load event.
36     virtual void OnInstantSupportDetermined(bool supports_instant) = 0;
37
38     // Called upon determination of voice search API support.
39     virtual void OnSetVoiceSearchSupport(bool supports_voice_search) = 0;
40
41     // Called when the page wants the omnibox to be focused. |state| specifies
42     // the omnibox focus state.
43     virtual void FocusOmnibox(OmniboxFocusState state) = 0;
44
45     // Called when the page wants to navigate to |url|. Usually used by the
46     // page to navigate to privileged destinations (e.g. chrome:// URLs) or to
47     // navigate to URLs that are hidden from the page using Restricted IDs (rid
48     // in the API).
49     virtual void NavigateToURL(const GURL& url,
50                                WindowOpenDisposition disposition,
51                                bool is_most_visited_item_url) = 0;
52
53     // Called when the SearchBox wants to delete a Most Visited item.
54     virtual void OnDeleteMostVisitedItem(const GURL& url) = 0;
55
56     // Called when the SearchBox wants to undo a Most Visited deletion.
57     virtual void OnUndoMostVisitedDeletion(const GURL& url) = 0;
58
59     // Called when the SearchBox wants to undo all Most Visited deletions.
60     virtual void OnUndoAllMostVisitedDeletions() = 0;
61
62     // Called to signal that an event has occurred on the New Tab Page.
63     virtual void OnLogEvent(NTPLoggingEventType event) = 0;
64
65     // Called to log an impression from a given provider on the New Tab Page.
66     virtual void OnLogImpression(int position,
67                                  const base::string16& provider) = 0;
68
69     // Called when the page wants to paste the |text| (or the clipboard contents
70     // if the |text| is empty) into the omnibox.
71     virtual void PasteIntoOmnibox(const base::string16& text) = 0;
72
73     // Called when the SearchBox wants to verify the signed-in Chrome identity
74     // against the provided |identity|. Will make a round-trip to the browser
75     // and eventually return the result through SendChromeIdentityCheckResult.
76     virtual void OnChromeIdentityCheck(const base::string16& identity) = 0;
77   };
78
79   // An interface to be implemented by consumers of SearchIPCRouter objects to
80   // decide whether to process the message received from the page, and vice
81   // versa (decide whether to send messages to the page).
82   class Policy {
83    public:
84     virtual ~Policy() {}
85
86     // SearchIPCRouter calls these functions before sending/receiving messages
87     // to/from the page.
88     virtual bool ShouldProcessSetVoiceSearchSupport() = 0;
89     virtual bool ShouldProcessFocusOmnibox(bool is_active_tab) = 0;
90     virtual bool ShouldProcessNavigateToURL(bool is_active_tab) = 0;
91     virtual bool ShouldProcessDeleteMostVisitedItem() = 0;
92     virtual bool ShouldProcessUndoMostVisitedDeletion() = 0;
93     virtual bool ShouldProcessUndoAllMostVisitedDeletions() = 0;
94     virtual bool ShouldProcessLogEvent() = 0;
95     virtual bool ShouldProcessPasteIntoOmnibox(bool is_active_tab) = 0;
96     virtual bool ShouldProcessChromeIdentityCheck() = 0;
97     virtual bool ShouldSendSetPromoInformation() = 0;
98     virtual bool ShouldSendSetDisplayInstantResults() = 0;
99     virtual bool ShouldSendSetSuggestionToPrefetch() = 0;
100     virtual bool ShouldSendSetOmniboxStartMargin() = 0;
101     virtual bool ShouldSendMostVisitedItems() = 0;
102     virtual bool ShouldSendThemeBackgroundInfo() = 0;
103     virtual bool ShouldSendToggleVoiceSearch() = 0;
104     virtual bool ShouldSubmitQuery() = 0;
105   };
106
107   SearchIPCRouter(content::WebContents* web_contents, Delegate* delegate,
108                   scoped_ptr<Policy> policy);
109   virtual ~SearchIPCRouter();
110
111   // Tells the renderer to determine if the page supports the Instant API, which
112   // results in a call to OnInstantSupportDetermined() when the reply is
113   // received.
114   void DetermineIfPageSupportsInstant();
115
116   // Tells the renderer about the result of the Chrome identity check.
117   void SendChromeIdentityCheckResult(const base::string16& identity,
118                                      bool identity_match);
119
120   // Tells the renderer information it needs to display promos.
121   void SetPromoInformation(bool is_app_launcher_enabled);
122
123   // Tells the renderer whether to display the Instant results.
124   void SetDisplayInstantResults();
125
126   // Tells the page the suggestion to be prefetched if any.
127   void SetSuggestionToPrefetch(const InstantSuggestion& suggestion);
128
129   // Tells the page the left margin of the omnibox. This is used by the page to
130   // align text or assets properly with the omnibox.
131   void SetOmniboxStartMargin(int start_margin);
132
133   // Tells the renderer about the most visited items.
134   void SendMostVisitedItems(const std::vector<InstantMostVisitedItem>& items);
135
136   // Tells the renderer about the current theme background.
137   void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info);
138
139   // Tells the page to toggle voice search.
140   void ToggleVoiceSearch();
141
142   // Tells the page that the user pressed Enter in the omnibox.
143   void Submit(const base::string16& text);
144
145   // Called when the tab corresponding to |this| instance is activated.
146   void OnTabActivated();
147
148   // Called when the tab corresponding to |this| instance is deactivated.
149   void OnTabDeactivated();
150
151  private:
152   friend class SearchIPCRouterPolicyTest;
153   friend class SearchIPCRouterTest;
154   FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
155                            DetermineIfPageSupportsInstant_Local);
156   FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
157                            DetermineIfPageSupportsInstant_NonLocal);
158   FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
159                            PageURLDoesntBelongToInstantRenderer);
160   FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
161                            IgnoreMessageIfThePageIsNotActive);
162   FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
163                            DoNotSendSetDisplayInstantResultsMsg);
164   FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, HandleTabChangedEvents);
165
166   // Overridden from contents::WebContentsObserver:
167   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
168
169   void OnInstantSupportDetermined(int page_id, bool supports_instant) const;
170   void OnVoiceSearchSupportDetermined(int page_id,
171                                       bool supports_voice_search) const;
172   void OnFocusOmnibox(int page_id, OmniboxFocusState state) const;
173   void OnSearchBoxNavigate(int page_id,
174                            const GURL& url,
175                            WindowOpenDisposition disposition,
176                            bool is_most_visited_item_url) const;
177   void OnDeleteMostVisitedItem(int page_id, const GURL& url) const;
178   void OnUndoMostVisitedDeletion(int page_id, const GURL& url) const;
179   void OnUndoAllMostVisitedDeletions(int page_id) const;
180   void OnLogEvent(int page_id, NTPLoggingEventType event) const;
181   void OnLogImpression(int page_id,
182                        int position,
183                        const base::string16& provider) const;
184   void OnPasteAndOpenDropDown(int page_id, const base::string16& text) const;
185   void OnChromeIdentityCheck(int page_id, const base::string16& identity) const;
186
187   // Used by unit tests to set a fake delegate.
188   void set_delegate(Delegate* delegate);
189
190   // Used by unit tests.
191   void set_policy(scoped_ptr<Policy> policy);
192
193   // Used by unit tests.
194   Policy* policy() const { return policy_.get(); }
195
196   Delegate* delegate_;
197   scoped_ptr<Policy> policy_;
198
199   // Set to true, when the tab corresponding to |this| instance is active.
200   bool is_active_tab_;
201
202   DISALLOW_COPY_AND_ASSIGN(SearchIPCRouter);
203 };
204
205 #endif  // CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_