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_SEARCH_SEARCH_H_
6 #define CHROME_BROWSER_SEARCH_SEARCH_H_
12 #include "base/basictypes.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/ui/search/search_model.h"
23 class NavigationEntry
;
27 namespace user_prefs
{
28 class PrefRegistrySyncable
;
34 // The user has not manually opted in/out of InstantExtended.
35 INSTANT_EXTENDED_NOT_SET
,
36 // The user has opted-in to InstantExtended.
37 INSTANT_EXTENDED_OPT_IN
,
38 // The user has opted-out of InstantExtended.
39 INSTANT_EXTENDED_OPT_OUT
,
40 INSTANT_EXTENDED_OPT_IN_STATE_ENUM_COUNT
,
43 enum DisplaySearchButtonConditions
{
44 DISPLAY_SEARCH_BUTTON_NEVER
,
45 DISPLAY_SEARCH_BUTTON_FOR_STR
, // STR = Search Term Replacement
46 DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP
, // IIP = Input In Progress
47 DISPLAY_SEARCH_BUTTON_ALWAYS
,
48 DISPLAY_SEARCH_BUTTON_NUM_VALUES
,
51 enum OriginChipPosition
{
53 ORIGIN_CHIP_LEADING_LOCATION_BAR
,
54 ORIGIN_CHIP_TRAILING_LOCATION_BAR
,
55 ORIGIN_CHIP_LEADING_MENU_BUTTON
,
56 ORIGIN_CHIP_NUM_VALUES
,
59 // Use this value for "start margin" to prevent the "es_sm" parameter from
61 extern const int kDisableStartMargin
;
63 // Returns whether the Instant Extended API is enabled.
64 bool IsInstantExtendedAPIEnabled();
66 // Returns whether the suggest is enabled for the given |profile|.
67 bool IsSuggestPrefEnabled(Profile
* profile
);
69 // Returns the value to pass to the &espv CGI parameter when loading the
70 // embedded search page from the user's default search provider. Returns 0 if
71 // the Instant Extended API is not enabled.
72 uint64
EmbeddedSearchPageVersion();
74 // Returns whether query extraction is enabled.
75 bool IsQueryExtractionEnabled();
77 // Extracts and returns search terms from |url|. Returns empty string if the URL
78 // is not secure or doesn't have a search term replacement key. Does not
79 // consider IsQueryExtractionEnabled() and Instant support state of the page and
80 // does not check for a privileged process, so most callers should use
81 // GetSearchTerms() below instead.
82 base::string16
GetSearchTermsFromURL(Profile
* profile
, const GURL
& url
);
84 // Returns the search terms attached to a specific NavigationEntry, or empty
85 // string otherwise. Does not consider IsQueryExtractionEnabled() and does not
86 // check Instant support, so most callers should use GetSearchTerms() below
88 base::string16
GetSearchTermsFromNavigationEntry(
89 const content::NavigationEntry
* entry
);
91 // Returns search terms if this WebContents is a search results page. It looks
92 // in the visible NavigationEntry first, to see if search terms have already
93 // been extracted. Failing that, it tries to extract search terms from the URL.
95 // Returns a blank string if search terms were not found, or if search terms
96 // extraction is disabled for this WebContents or profile, or if |contents|
97 // does not support Instant.
98 base::string16
GetSearchTerms(const content::WebContents
* contents
);
100 // Returns true if |url| should be rendered in the Instant renderer process.
101 bool ShouldAssignURLToInstantRenderer(const GURL
& url
, Profile
* profile
);
103 // Returns true if the Instant |url| should use process per site.
104 bool ShouldUseProcessPerSiteForInstantURL(const GURL
& url
, Profile
* profile
);
106 // Returns true if |url| corresponds to a New Tab page (it can be either an
107 // Instant Extended NTP or a non-extended NTP).
108 bool IsNTPURL(const GURL
& url
, Profile
* profile
);
110 // Returns true if the visible entry of |contents| is a New Tab Page rendered
111 // by Instant. A page that matches the search or Instant URL of the default
112 // search provider but does not have any search terms is considered an Instant
114 bool IsInstantNTP(const content::WebContents
* contents
);
116 // Same as IsInstantNTP but uses |nav_entry| to determine the URL for the page
117 // instead of using the visible entry.
118 bool NavEntryIsInstantNTP(const content::WebContents
* contents
,
119 const content::NavigationEntry
* nav_entry
);
121 // Returns the Instant URL of the default search engine. Returns an empty GURL
122 // if the engine doesn't have an Instant URL, or if it shouldn't be used (say
123 // because it doesn't satisfy the requirements for extended mode or if Instant
124 // is disabled through preferences). Callers must check that the returned URL is
125 // valid before using it. The value of |start_margin| is used for the "es_sm"
126 // parameter in the URL. |force_instant_results| forces a search page to update
127 // results incrementally even if that is otherwise disabled by google.com
129 // NOTE: This method expands the default search engine's instant_url template,
130 // so it shouldn't be called from SearchTermsData or other such code that would
131 // lead to an infinite recursion.
132 GURL
GetInstantURL(Profile
* profile
,
134 bool force_instant_results
);
136 // Returns URLs associated with the default search engine for |profile|.
137 std::vector
<GURL
> GetSearchURLs(Profile
* profile
);
139 // Returns the default search engine base page URL to prefetch search results.
140 // Returns an empty URL if 'prefetch_results' flag is set to false in field
142 GURL
GetSearchResultPrefetchBaseURL(Profile
* profile
);
144 // Returns true if 'prefetch_results' flag is set to true in field trials to
145 // prefetch high-confidence search suggestions.
146 bool ShouldPrefetchSearchResults();
148 // Returns the Local Instant URL of the New Tab Page.
149 // TODO(kmadhusu): Remove this function and update the call sites.
150 GURL
GetLocalInstantURL(Profile
* profile
);
152 // Returns true if 'hide_verbatim' flag is enabled in field trials
153 // to hide the top match in the native suggestions dropdown if it is a verbatim
154 // match. See comments on ShouldHideTopMatch in autocomplete_result.h.
155 bool ShouldHideTopVerbatimMatch();
157 // Returns when we should show a search button in the omnibox. This may be any
158 // of several values, some of which depend on whether the underlying state of
159 // the page would normally be to perform search term replacement; see also
160 // ToolbarModel::WouldPerformSearchTermReplacement().
161 DisplaySearchButtonConditions
GetDisplaySearchButtonConditions();
163 // Returns true if the origin chip should be shown in the toolbar. This
164 // also includes the related changes to the omnibox.
165 bool ShouldDisplayOriginChip();
167 // Returns a value indicating where the origin chip should be positioned on the
169 OriginChipPosition
GetOriginChipPosition();
171 // Transforms the input |url| into its "effective URL". The returned URL
172 // facilitates grouping process-per-site. The |url| is transformed, for
175 // https://www.google.com/search?espv=1&q=tractors
177 // to the privileged URL
179 // chrome-search://www.google.com/search?espv=1&q=tractors
181 // Notice the scheme change.
183 // If the input is already a privileged URL then that same URL is returned.
185 // If |url| is that of the online NTP, its host is replaced with "online-ntp".
186 // This forces the NTP and search results pages to have different SiteIntances,
187 // and hence different processes.
188 GURL
GetEffectiveURLForInstant(const GURL
& url
, Profile
* profile
);
191 // 1. |url| is kChromeUINewTabURL,
192 // 2. InstantExtended is enabled, and
193 // 3. The --instant-new-tab-url switch is set to a valid URL.
194 // |url| is rewritten to the value of --instant-new-tab-url.
195 bool HandleNewTabURLRewrite(GURL
* url
,
196 content::BrowserContext
* browser_context
);
197 // Reverses the operation from HandleNewTabURLRewrite.
198 bool HandleNewTabURLReverseRewrite(GURL
* url
,
199 content::BrowserContext
* browser_context
);
201 // Sets the Instant support |state| in the navigation |entry|.
202 void SetInstantSupportStateInNavigationEntry(InstantSupportState state
,
203 content::NavigationEntry
* entry
);
205 // Returns the Instant support state attached to the NavigationEntry, or
206 // INSTANT_SUPPORT_UNKNOWN otherwise.
207 InstantSupportState
GetInstantSupportStateFromNavigationEntry(
208 const content::NavigationEntry
& entry
);
210 // Returns true if the field trial flag is enabled to prefetch results on SRP.
211 bool ShouldPrefetchSearchResultsOnSRP();
213 // -----------------------------------------------------
214 // The following APIs are exposed for use in tests only.
215 // -----------------------------------------------------
217 // Forces query in the omnibox to be on for tests.
218 void EnableQueryExtractionForTesting();
220 // Type for a collection of experiment configuration parameters.
221 typedef std::vector
<std::pair
<std::string
, std::string
> > FieldTrialFlags
;
223 // Finds the active field trial group name and parses out the configuration
224 // flags. On success, |flags| will be filled with the field trial flags. |flags|
225 // must not be NULL. Returns true iff the active field trial is successfully
226 // parsed and not disabled.
227 // Note that |flags| may be successfully populated in some cases when false is
228 // returned - in these cases it should not be used.
229 // Exposed for testing only.
230 bool GetFieldTrialInfo(FieldTrialFlags
* flags
);
232 // Given a FieldTrialFlags object, returns the string value of the provided
234 // Exposed for testing only.
235 std::string
GetStringValueForFlagWithDefault(const std::string
& flag
,
236 const std::string
& default_value
,
237 const FieldTrialFlags
& flags
);
239 // Given a FieldTrialFlags object, returns the uint64 value of the provided
241 // Exposed for testing only.
242 uint64
GetUInt64ValueForFlagWithDefault(const std::string
& flag
,
243 uint64 default_value
,
244 const FieldTrialFlags
& flags
);
246 // Given a FieldTrialFlags object, returns the bool value of the provided flag.
247 // Exposed for testing only.
248 bool GetBoolValueForFlagWithDefault(const std::string
& flag
,
250 const FieldTrialFlags
& flags
);
252 // Returns the Cacheable New Tab Page URL for the given |profile|.
253 GURL
GetNewTabPageURL(Profile
* profile
);
255 } // namespace chrome
257 #endif // CHROME_BROWSER_SEARCH_SEARCH_H_