search_ipc_router.h revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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
18class GURL;
19
20namespace content {
21class WebContents;
22}
23
24class SearchIPCRouterTest;
25
26// SearchIPCRouter is responsible for receiving and sending IPC messages between
27// the browser and the Instant page.
28class 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 when the page wants to paste the |text| (or the clipboard contents
66    // if the |text| is empty) into the omnibox.
67    virtual void PasteIntoOmnibox(const string16& text) = 0;
68
69    // Called when the SearchBox wants to verify the signed-in Chrome identity
70    // against the provided |identity|. Will make a round-trip to the browser
71    // and eventually return the result through SendChromeIdentityCheckResult.
72    virtual void OnChromeIdentityCheck(const string16& identity) = 0;
73  };
74
75  // An interface to be implemented by consumers of SearchIPCRouter objects to
76  // decide whether to process the message received from the page, and vice
77  // versa (decide whether to send messages to the page).
78  class Policy {
79   public:
80    virtual ~Policy() {}
81
82    // SearchIPCRouter calls these functions before sending/receiving messages
83    // to/from the page.
84    virtual bool ShouldProcessSetVoiceSearchSupport() = 0;
85    virtual bool ShouldProcessFocusOmnibox(bool is_active_tab) = 0;
86    virtual bool ShouldProcessNavigateToURL(bool is_active_tab) = 0;
87    virtual bool ShouldProcessDeleteMostVisitedItem() = 0;
88    virtual bool ShouldProcessUndoMostVisitedDeletion() = 0;
89    virtual bool ShouldProcessUndoAllMostVisitedDeletions() = 0;
90    virtual bool ShouldProcessLogEvent() = 0;
91    virtual bool ShouldProcessPasteIntoOmnibox(bool is_active_tab) = 0;
92    virtual bool ShouldProcessChromeIdentityCheck() = 0;
93    virtual bool ShouldSendSetPromoInformation() = 0;
94    virtual bool ShouldSendSetDisplayInstantResults() = 0;
95    virtual bool ShouldSendSetSuggestionToPrefetch() = 0;
96    virtual bool ShouldSendMostVisitedItems() = 0;
97    virtual bool ShouldSendThemeBackgroundInfo() = 0;
98    virtual bool ShouldSubmitQuery() = 0;
99  };
100
101  SearchIPCRouter(content::WebContents* web_contents, Delegate* delegate,
102                  scoped_ptr<Policy> policy);
103  virtual ~SearchIPCRouter();
104
105  // Tells the renderer to determine if the page supports the Instant API, which
106  // results in a call to OnInstantSupportDetermined() when the reply is
107  // received.
108  void DetermineIfPageSupportsInstant();
109
110  // Tells the renderer information it needs to display promos.
111  void SetPromoInformation(bool is_app_launcher_enabled);
112
113  // Tells the renderer whether to display the Instant results.
114  void SetDisplayInstantResults();
115
116  // Tells the renderer about the most visited items.
117  void SendMostVisitedItems(const std::vector<InstantMostVisitedItem>& items);
118
119  // Tells the renderer about the result of the Chrome identity check.
120  void SendChromeIdentityCheckResult(const string16& identity,
121                                     bool identity_match);
122
123  // Tells the renderer about the current theme background.
124  void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info);
125
126  // Tells the page the suggestion to be prefetched if any.
127  void SetSuggestionToPrefetch(const InstantSuggestion& suggestion);
128
129  // Tells the page that the user pressed Enter in the omnibox.
130  void Submit(const string16& text);
131
132  // Called when the tab corresponding to |this| instance is activated.
133  void OnTabActivated();
134
135  // Called when the tab corresponding to |this| instance is deactivated.
136  void OnTabDeactivated();
137
138 private:
139  friend class SearchIPCRouterTest;
140  FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
141                           DetermineIfPageSupportsInstant_Local);
142  FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
143                           DetermineIfPageSupportsInstant_NonLocal);
144  FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest,
145                           PageURLDoesntBelongToInstantRenderer);
146  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
147                           ProcessVoiceSearchSupportMsg);
148  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, ProcessLogEvent);
149  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, DoNotProcessLogEvent);
150  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
151                           ProcessChromeIdentityCheck);
152  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
153                           DoNotProcessChromeIdentityCheck);
154  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
155                           SendSetDisplayInstantResults);
156  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
157                           SendSetSuggestionToPrefetch);
158  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
159                           DoNotSendSetMessagesForIncognitoPage);
160  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, SendMostVisitedItems);
161  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
162                           DoNotSendMostVisitedItems);
163  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, SendThemeBackgroundInfo);
164  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
165                           DoNotSendThemeBackgroundInfo);
166  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, SubmitQuery);
167  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
168                           AppropriateMessagesSentToIncognitoPages);
169  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, ProcessFocusOmnibox);
170  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, DoNotProcessFocusOmnibox);
171  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, SendSetPromoInformation);
172  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
173                           DoNotSendSetPromoInformation);
174  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, ProcessNavigateToURL);
175  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
176                           ProcessDeleteMostVisitedItem);
177  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
178                           ProcessUndoMostVisitedDeletion);
179  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
180                           ProcessUndoAllMostVisitedDeletions);
181  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
182                           ProcessPasteIntoOmniboxMsg);
183  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
184                           DoNotProcessPasteIntoOmniboxMsg);
185  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
186                           DoNotProcessMessagesForIncognitoPage);
187  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest,
188                           DoNotProcessMessagesForInactiveTab);
189  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessVoiceSearchSupportMsg);
190  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreVoiceSearchSupportMsg);
191  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessFocusOmniboxMsg);
192  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreFocusOmniboxMsg);
193  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, HandleTabChangedEvents);
194  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, SendSetPromoInformationMsg);
195  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
196                           DoNotSendSetPromoInformationMsg);
197  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessLogEventMsg);
198  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreLogEventMsg);
199  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessChromeIdentityCheckMsg);
200  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreChromeIdentityCheckMsg);
201  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessNavigateToURLMsg);
202  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreNavigateToURLMsg);
203  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
204                           ProcessDeleteMostVisitedItemMsg);
205  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
206                           IgnoreDeleteMostVisitedItemMsg);
207  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
208                           ProcessUndoMostVisitedDeletionMsg);
209  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
210                           IgnoreUndoMostVisitedDeletionMsg);
211  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
212                           ProcessUndoAllMostVisitedDeletionsMsg);
213  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
214                           IgnoreUndoAllMostVisitedDeletionsMsg);
215  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest,
216                           IgnoreMessageIfThePageIsNotActive);
217  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessPasteAndOpenDropdownMsg);
218  FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnorePasteAndOpenDropdownMsg);
219
220  // Overridden from contents::WebContentsObserver:
221  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
222
223  void OnInstantSupportDetermined(int page_id, bool supports_instant) const;
224  void OnVoiceSearchSupportDetermined(int page_id,
225                                      bool supports_voice_search) const;
226  void OnFocusOmnibox(int page_id, OmniboxFocusState state) const;
227  void OnSearchBoxNavigate(int page_id,
228                           const GURL& url,
229                           WindowOpenDisposition disposition,
230                           bool is_most_visited_item_url) const;
231  void OnDeleteMostVisitedItem(int page_id, const GURL& url) const;
232  void OnUndoMostVisitedDeletion(int page_id, const GURL& url) const;
233  void OnUndoAllMostVisitedDeletions(int page_id) const;
234  void OnLogEvent(int page_id, NTPLoggingEventType event) const;
235  void OnPasteAndOpenDropDown(int page_id, const string16& text) const;
236  void OnChromeIdentityCheck(int page_id, const string16& identity) const;
237
238  // Used by unit tests to set a fake delegate.
239  void set_delegate(Delegate* delegate);
240
241  // Used by unit tests.
242  void set_policy(scoped_ptr<Policy> policy);
243
244  // Used by unit tests.
245  Policy* policy() const { return policy_.get(); }
246
247  Delegate* delegate_;
248  scoped_ptr<Policy> policy_;
249
250  // Set to true, when the tab corresponding to |this| instance is active.
251  bool is_active_tab_;
252
253  DISALLOW_COPY_AND_ASSIGN(SearchIPCRouter);
254};
255
256#endif  // CHROME_BROWSER_UI_SEARCH_SEARCH_IPC_ROUTER_H_
257