• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
~Policy()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 ShouldSendMostVisitedItems() = 0;
101     virtual bool ShouldSendThemeBackgroundInfo() = 0;
102     virtual bool ShouldSendToggleVoiceSearch() = 0;
103     virtual bool ShouldSubmitQuery() = 0;
104   };
105 
106   SearchIPCRouter(content::WebContents* web_contents, Delegate* delegate,
107                   scoped_ptr<Policy> policy);
108   virtual ~SearchIPCRouter();
109 
110   // Tells the renderer to determine if the page supports the Instant API, which
111   // results in a call to OnInstantSupportDetermined() when the reply is
112   // received.
113   void DetermineIfPageSupportsInstant();
114 
115   // Tells the renderer about the result of the Chrome identity check.
116   void SendChromeIdentityCheckResult(const base::string16& identity,
117                                      bool identity_match);
118 
119   // Tells the renderer information it needs to display promos.
120   void SetPromoInformation(bool is_app_launcher_enabled);
121 
122   // Tells the renderer whether to display the Instant results.
123   void SetDisplayInstantResults();
124 
125   // Tells the page the suggestion to be prefetched if any.
126   void SetSuggestionToPrefetch(const InstantSuggestion& suggestion);
127 
128   // Tells the renderer about the most visited items.
129   void SendMostVisitedItems(const std::vector<InstantMostVisitedItem>& items);
130 
131   // Tells the renderer about the current theme background.
132   void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info);
133 
134   // Tells the page to toggle voice search.
135   void ToggleVoiceSearch();
136 
137   // Tells the page that the user pressed Enter in the omnibox.
138   void Submit(const base::string16& text);
139 
140   // Called when the tab corresponding to |this| instance is activated.
141   void OnTabActivated();
142 
143   // Called when the tab corresponding to |this| instance is deactivated.
144   void OnTabDeactivated();
145 
146  private:
147   friend class SearchIPCRouterPolicyTest;
148   friend class SearchIPCRouterTest;
149   FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
150                            DetermineIfPageSupportsInstant_Local);
151   FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
152                            DetermineIfPageSupportsInstant_NonLocal);
153   FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
154                            PageURLDoesntBelongToInstantRenderer);
155   FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
156                            IgnoreMessageIfThePageIsNotActive);
157   FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
158                            DoNotSendSetDisplayInstantResultsMsg);
159   FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, HandleTabChangedEvents);
160 
161   // Overridden from contents::WebContentsObserver:
162   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
163 
164   void OnInstantSupportDetermined(int page_id, bool supports_instant) const;
165   void OnVoiceSearchSupportDetermined(int page_id,
166                                       bool supports_voice_search) const;
167   void OnFocusOmnibox(int page_id, OmniboxFocusState state) const;
168   void OnSearchBoxNavigate(int page_id,
169                            const GURL& url,
170                            WindowOpenDisposition disposition,
171                            bool is_most_visited_item_url) const;
172   void OnDeleteMostVisitedItem(int page_id, const GURL& url) const;
173   void OnUndoMostVisitedDeletion(int page_id, const GURL& url) const;
174   void OnUndoAllMostVisitedDeletions(int page_id) const;
175   void OnLogEvent(int page_id, NTPLoggingEventType event) const;
176   void OnLogImpression(int page_id,
177                        int position,
178                        const base::string16& provider) const;
179   void OnPasteAndOpenDropDown(int page_id, const base::string16& text) const;
180   void OnChromeIdentityCheck(int page_id, const base::string16& identity) const;
181 
182   // Used by unit tests to set a fake delegate.
183   void set_delegate(Delegate* delegate);
184 
185   // Used by unit tests.
186   void set_policy(scoped_ptr<Policy> policy);
187 
188   // Used by unit tests.
policy()189   Policy* policy() const { return policy_.get(); }
190 
191   Delegate* delegate_;
192   scoped_ptr<Policy> policy_;
193 
194   // Set to true, when the tab corresponding to |this| instance is active.
195   bool is_active_tab_;
196 
197   DISALLOW_COPY_AND_ASSIGN(SearchIPCRouter);
198 };
199 
200 #endif  // CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
201