1 // Copyright 2012 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_TAB_HELPER_H_
6 #define CHROME_BROWSER_UI_SEARCH_SEARCH_TAB_HELPER_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "chrome/browser/search/instant_service_observer.h"
14 #include "chrome/browser/ui/search/search_ipc_router.h"
15 #include "chrome/browser/ui/search/search_model.h"
16 #include "chrome/common/instant_types.h"
17 #include "chrome/common/ntp_logging_events.h"
18 #include "chrome/common/omnibox_focus_state.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/browser/web_contents_user_data.h"
21 #include "ui/base/window_open_disposition.h"
25 struct LoadCommittedDetails
;
29 class InstantPageTest
;
33 class SearchIPCRouterTest
;
34 class SearchTabHelperDelegate
;
36 // Per-tab search "helper". Acts as the owner and controller of the tab's
39 // When the page is finished loading, SearchTabHelper determines the instant
40 // support for the page. When a navigation entry is committed (except for
41 // in-page navigations), SearchTabHelper resets the instant support state to
42 // INSTANT_SUPPORT_UNKNOWN and cause support to be determined again.
43 class SearchTabHelper
: public content::WebContentsObserver
,
44 public content::WebContentsUserData
<SearchTabHelper
>,
45 public InstantServiceObserver
,
46 public SearchIPCRouter::Delegate
{
48 virtual ~SearchTabHelper();
50 SearchModel
* model() {
54 // Sets up the initial state correctly for a preloaded NTP.
55 void InitForPreloadedNTP();
57 // Invoked when the omnibox input state is changed in some way that might
58 // affect the search mode.
59 void OmniboxInputStateChanged();
61 // Called to indicate that the omnibox focus state changed with the given
63 void OmniboxFocusChanged(OmniboxFocusState state
,
64 OmniboxFocusChangeReason reason
);
66 // Invoked when the active navigation entry is updated in some way that might
67 // affect the search mode. This is used by Instant when it "fixes up" the
68 // virtual URL of the active entry. Regular navigations are captured through
69 // the notification system and shouldn't call this method.
70 void NavigationEntryUpdated();
72 // Invoked to update the instant support state.
73 void InstantSupportChanged(bool supports_instant
);
75 // Returns true if the page supports instant. If the instant support state is
76 // not determined or if the page does not support instant returns false.
77 bool SupportsInstant() const;
79 // Sends the current SearchProvider suggestion to the Instant page if any.
80 void SetSuggestionToPrefetch(const InstantSuggestion
& suggestion
);
82 // Tells the page that the user pressed Enter in the omnibox.
83 void Submit(const base::string16
& text
);
85 // Called when the tab corresponding to |this| instance is activated.
86 void OnTabActivated();
88 // Called when the tab corresponding to |this| instance is deactivated.
89 void OnTabDeactivated();
91 // Tells the page to toggle voice search.
92 void ToggleVoiceSearch();
94 // Returns true if the underlying page is a search results page.
95 bool IsSearchResultsPage();
97 void set_delegate(SearchTabHelperDelegate
* delegate
) { delegate_
= delegate
; }
100 friend class content::WebContentsUserData
<SearchTabHelper
>;
101 friend class InstantPageTest
;
102 friend class SearchIPCRouterPolicyTest
;
103 friend class SearchIPCRouterTest
;
104 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest
,
105 DetermineIfPageSupportsInstant_Local
);
106 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest
,
107 DetermineIfPageSupportsInstant_NonLocal
);
108 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest
,
109 PageURLDoesntBelongToInstantRenderer
);
110 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest
,
111 OnChromeIdentityCheckMatch
);
112 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest
,
113 OnChromeIdentityCheckMismatch
);
114 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest
,
115 OnChromeIdentityCheckSignedOutMatch
);
116 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest
,
117 OnChromeIdentityCheckSignedOutMismatch
);
118 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperWindowTest
,
119 OnProvisionalLoadFailRedirectNTPToLocal
);
120 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperWindowTest
,
121 OnProvisionalLoadFailDontRedirectIfAborted
);
122 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperWindowTest
,
123 OnProvisionalLoadFailDontRedirectNonNTP
);
124 FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest
,
125 IgnoreMessageIfThePageIsNotActive
);
126 FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest
,
127 DoNotSendSetDisplayInstantResultsMsg
);
128 FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest
, HandleTabChangedEvents
);
129 FRIEND_TEST_ALL_PREFIXES(InstantPageTest
,
130 DetermineIfPageSupportsInstant_Local
);
131 FRIEND_TEST_ALL_PREFIXES(InstantPageTest
,
132 DetermineIfPageSupportsInstant_NonLocal
);
133 FRIEND_TEST_ALL_PREFIXES(InstantPageTest
,
134 PageURLDoesntBelongToInstantRenderer
);
135 FRIEND_TEST_ALL_PREFIXES(InstantPageTest
, PageSupportsInstant
);
137 explicit SearchTabHelper(content::WebContents
* web_contents
);
139 // Overridden from contents::WebContentsObserver:
140 virtual void RenderViewCreated(
141 content::RenderViewHost
* render_view_host
) OVERRIDE
;
142 virtual void DidStartNavigationToPendingEntry(
144 content::NavigationController::ReloadType reload_type
) OVERRIDE
;
145 virtual void DidNavigateMainFrame(
146 const content::LoadCommittedDetails
& details
,
147 const content::FrameNavigateParams
& params
) OVERRIDE
;
148 virtual void DidFailProvisionalLoad(
150 const base::string16
& frame_unique_name
,
152 const GURL
& validated_url
,
154 const base::string16
& error_description
,
155 content::RenderViewHost
* render_view_host
) OVERRIDE
;
156 virtual void DidFinishLoad(
158 const GURL
& validated_url
,
160 content::RenderViewHost
* render_view_host
) OVERRIDE
;
161 virtual void NavigationEntryCommitted(
162 const content::LoadCommittedDetails
& load_details
) OVERRIDE
;
164 // Overridden from SearchIPCRouter::Delegate:
165 virtual void OnInstantSupportDetermined(bool supports_instant
) OVERRIDE
;
166 virtual void OnSetVoiceSearchSupport(bool supports_voice_search
) OVERRIDE
;
167 virtual void FocusOmnibox(OmniboxFocusState state
) OVERRIDE
;
168 virtual void NavigateToURL(const GURL
& url
,
169 WindowOpenDisposition disposition
,
170 bool is_most_visited_item_url
) OVERRIDE
;
171 virtual void OnDeleteMostVisitedItem(const GURL
& url
) OVERRIDE
;
172 virtual void OnUndoMostVisitedDeletion(const GURL
& url
) OVERRIDE
;
173 virtual void OnUndoAllMostVisitedDeletions() OVERRIDE
;
174 virtual void OnLogEvent(NTPLoggingEventType event
) OVERRIDE
;
175 virtual void OnLogMostVisitedImpression(
176 int position
, const base::string16
& provider
) OVERRIDE
;
177 virtual void OnLogMostVisitedNavigation(
178 int position
, const base::string16
& provider
) OVERRIDE
;
179 virtual void PasteIntoOmnibox(const base::string16
& text
) OVERRIDE
;
180 virtual void OnChromeIdentityCheck(const base::string16
& identity
) OVERRIDE
;
182 // Overridden from InstantServiceObserver:
183 virtual void ThemeInfoChanged(const ThemeBackgroundInfo
& theme_info
) OVERRIDE
;
184 virtual void MostVisitedItemsChanged(
185 const std::vector
<InstantMostVisitedItem
>& items
) OVERRIDE
;
186 virtual void OmniboxStartMarginChanged(int omnibox_start_margin
) OVERRIDE
;
188 // Removes recommended URLs if a matching URL is already open in the Browser,
189 // if the Most Visited Tile Placement experiment is enabled, and the client is
190 // in the experiment group.
191 void MaybeRemoveMostVisitedItems(std::vector
<InstantMostVisitedItem
>* items
);
193 // Sets the mode of the model based on the current URL of web_contents().
194 // Only updates the origin part of the mode if |update_origin| is true,
195 // otherwise keeps the current origin. If |is_preloaded_ntp| is true, the mode
196 // is set to NTP regardless of the current URL; this is used to ensure that
197 // InstantController can bind InstantTab to new tab pages immediately.
198 void UpdateMode(bool update_origin
, bool is_preloaded_ntp
);
200 // Tells the renderer to determine if the page supports the Instant API, which
201 // results in a call to OnInstantSupportDetermined() when the reply is
203 void DetermineIfPageSupportsInstant();
205 // Used by unit tests.
206 SearchIPCRouter
& ipc_router() { return ipc_router_
; }
208 Profile
* profile() const;
210 // Helper function to navigate the given contents to the local fallback
211 // Instant URL and trim the history correctly.
212 void RedirectToLocalNTP();
214 // Returns whether input is in progress, i.e. if the omnibox has focus and the
215 // active tab is in mode SEARCH_SUGGESTIONS.
216 bool IsInputInProgress() const;
218 // Returns the OmniboxView for |web_contents_| or NULL if not available.
219 OmniboxView
* GetOmniboxView() const;
221 const bool is_search_enabled_
;
223 // Model object for UI that cares about search state.
226 content::WebContents
* web_contents_
;
228 SearchIPCRouter ipc_router_
;
230 InstantService
* instant_service_
;
232 // Delegate for notifying our owner about the SearchTabHelper state. Not owned
234 // NULL on iOS and Android because they don't use the Instant framework.
235 SearchTabHelperDelegate
* delegate_
;
237 DISALLOW_COPY_AND_ASSIGN(SearchTabHelper
);
240 #endif // CHROME_BROWSER_UI_SEARCH_SEARCH_TAB_HELPER_H_