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. 4 5 #ifndef CHROME_BROWSER_SEARCH_SEARCH_H_ 6 #define CHROME_BROWSER_SEARCH_SEARCH_H_ 7 8 #include <string> 9 #include <utility> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/strings/string16.h" 14 #include "chrome/browser/ui/search/search_model.h" 15 16 class GURL; 17 class Profile; 18 class TemplateURL; 19 class TemplateURLRef; 20 21 namespace content { 22 class BrowserContext; 23 class NavigationEntry; 24 class WebContents; 25 } 26 27 namespace user_prefs { 28 class PrefRegistrySyncable; 29 } 30 31 namespace chrome { 32 33 enum OptInState { 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, 41 }; 42 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, 49 }; 50 51 enum OriginChipCondition { 52 ORIGIN_CHIP_DISABLED = 0, 53 ORIGIN_CHIP_ALWAYS, 54 ORIGIN_CHIP_ON_SRP, 55 ORIGIN_CHIP_NUM_VALUES, 56 }; 57 58 // Use this value for "start margin" to prevent the "es_sm" parameter from 59 // being used. 60 extern const int kDisableStartMargin; 61 62 // Returns whether the Instant Extended API is enabled. 63 bool IsInstantExtendedAPIEnabled(); 64 65 // Returns whether the suggest is enabled for the given |profile|. 66 bool IsSuggestPrefEnabled(Profile* profile); 67 68 // Returns the value to pass to the &espv CGI parameter when loading the 69 // embedded search page from the user's default search provider. Returns 0 if 70 // the Instant Extended API is not enabled. 71 uint64 EmbeddedSearchPageVersion(); 72 73 // Returns a string indicating whether InstantExtended is enabled, suitable 74 // for adding as a query string param to the homepage or search requests. 75 // Returns an empty string otherwise. 76 // 77 // |for_search| should be set to true for search requests, in which case this 78 // returns a non-empty string only if query extraction is enabled. 79 std::string InstantExtendedEnabledParam(bool for_search); 80 81 // Returns a string that will cause the search results page to update 82 // incrementally. Currently, Instant Extended passes a different param to 83 // search results pages that also has this effect, so by default this function 84 // returns the empty string when Instant Extended is enabled. However, when 85 // doing instant search result prerendering, we still need to pass this param, 86 // as Instant Extended does not cause incremental updates by default for the 87 // prerender page. Callers should set |for_prerender| in this case to force 88 // the returned string to be non-empty. 89 std::string ForceInstantResultsParam(bool for_prerender); 90 91 // Returns whether query extraction is enabled. 92 bool IsQueryExtractionEnabled(); 93 94 // Extracts and returns search terms from |url|. Does not consider 95 // IsQueryExtractionEnabled() and Instant support state of the page and does 96 // not check for a privileged process, so most callers should use 97 // GetSearchTerms() below instead. 98 base::string16 ExtractSearchTermsFromURL(Profile* profile, const GURL& url); 99 100 // Returns true if it is okay to extract search terms from |url|. |url| must 101 // have a secure scheme and must contain the search terms replacement key for 102 // the default search provider. 103 bool IsQueryExtractionAllowedForURL(Profile* profile, const GURL& url); 104 105 // Returns the search terms attached to a specific NavigationEntry, or empty 106 // string otherwise. Does not consider IsQueryExtractionEnabled() and does not 107 // check Instant support, so most callers should use GetSearchTerms() below 108 // instead. 109 base::string16 GetSearchTermsFromNavigationEntry( 110 const content::NavigationEntry* entry); 111 112 // Returns search terms if this WebContents is a search results page. It looks 113 // in the visible NavigationEntry first, to see if search terms have already 114 // been extracted. Failing that, it tries to extract search terms from the URL. 115 // 116 // Returns a blank string if search terms were not found, or if search terms 117 // extraction is disabled for this WebContents or profile, or if |contents| 118 // does not support Instant. 119 base::string16 GetSearchTerms(const content::WebContents* contents); 120 121 // Returns true if |url| should be rendered in the Instant renderer process. 122 bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile); 123 124 // Returns true if |contents| is rendered inside the Instant process for 125 // |profile|. 126 bool IsRenderedInInstantProcess(const content::WebContents* contents, 127 Profile* profile); 128 129 // Returns true if the Instant |url| should use process per site. 130 bool ShouldUseProcessPerSiteForInstantURL(const GURL& url, Profile* profile); 131 132 // Returns true if |url| corresponds to a New Tab page (it can be either an 133 // Instant Extended NTP or a non-extended NTP). 134 bool IsNTPURL(const GURL& url, Profile* profile); 135 136 // Returns true if the visible entry of |contents| is a New Tab Page rendered 137 // by Instant. A page that matches the search or Instant URL of the default 138 // search provider but does not have any search terms is considered an Instant 139 // New Tab Page. 140 bool IsInstantNTP(const content::WebContents* contents); 141 142 // Same as IsInstantNTP but uses |nav_entry| to determine the URL for the page 143 // instead of using the visible entry. 144 bool NavEntryIsInstantNTP(const content::WebContents* contents, 145 const content::NavigationEntry* nav_entry); 146 147 // Returns the Instant URL of the default search engine. Returns an empty GURL 148 // if the engine doesn't have an Instant URL, or if it shouldn't be used (say 149 // because it doesn't satisfy the requirements for extended mode or if Instant 150 // is disabled through preferences). Callers must check that the returned URL is 151 // valid before using it. The value of |start_margin| is used for the "es_sm" 152 // parameter in the URL. |force_instant_results| forces a search page to update 153 // results incrementally even if that is otherwise disabled by google.com 154 // preferences. 155 // NOTE: This method expands the default search engine's instant_url template, 156 // so it shouldn't be called from SearchTermsData or other such code that would 157 // lead to an infinite recursion. 158 GURL GetInstantURL(Profile* profile, 159 int start_margin, 160 bool force_instant_results); 161 162 // Returns URLs associated with the default search engine for |profile|. 163 std::vector<GURL> GetSearchURLs(Profile* profile); 164 165 // Returns the default search engine base page URL to prefetch search results. 166 // Returns an empty URL if 'prefetch_results' flag is set to false in field 167 // trials. 168 GURL GetSearchResultPrefetchBaseURL(Profile* profile); 169 170 // Returns true if 'prefetch_results' flag is set to true in field trials to 171 // prefetch high-confidence search suggestions. 172 bool ShouldPrefetchSearchResults(); 173 174 // Returns true if 'allow_prefetch_non_default_match' flag is enabled in field 175 // trials to allow prefetching the suggestion marked to be prefetched by the 176 // suggest server even if it is not the default match. 177 bool ShouldAllowPrefetchNonDefaultMatch(); 178 179 // Returns true if 'prerender_instant_url_on_omnibox_focus' flag is enabled in 180 // field trials to prerender Instant search base page when the omnibox is 181 // focused. 182 bool ShouldPrerenderInstantUrlOnOmniboxFocus(); 183 184 // Returns true if 'reuse_instant_search_base_page' flag is set to true in field 185 // trials to reuse the prerendered page to commit any search query. 186 bool ShouldReuseInstantSearchBasePage(); 187 188 // Returns the Local Instant URL of the New Tab Page. 189 // TODO(kmadhusu): Remove this function and update the call sites. 190 GURL GetLocalInstantURL(Profile* profile); 191 192 // Returns true if 'hide_verbatim' flag is enabled in field trials 193 // to hide the top match in the native suggestions dropdown if it is a verbatim 194 // match. See comments on ShouldHideTopMatch in autocomplete_result.h. 195 bool ShouldHideTopVerbatimMatch(); 196 197 // Returns when we should show a search button in the omnibox. This may be any 198 // of several values, some of which depend on whether the underlying state of 199 // the page would normally be to perform search term replacement; see also 200 // ToolbarModel::WouldPerformSearchTermReplacement(). 201 DisplaySearchButtonConditions GetDisplaySearchButtonConditions(); 202 203 // Returns true if the origin chip should be shown. 204 bool ShouldDisplayOriginChip(); 205 206 // Returns a value indicating when the origin chip should be shown. 207 OriginChipCondition GetOriginChipCondition(); 208 209 // Returns true if the local new tab page should show a Google logo and search 210 // box for users whose default search provider is Google, or false if not. 211 bool ShouldShowGoogleLocalNTP(); 212 213 // Transforms the input |url| into its "effective URL". The returned URL 214 // facilitates grouping process-per-site. The |url| is transformed, for 215 // example, from 216 // 217 // https://www.google.com/search?espv=1&q=tractors 218 // 219 // to the privileged URL 220 // 221 // chrome-search://www.google.com/search?espv=1&q=tractors 222 // 223 // Notice the scheme change. 224 // 225 // If the input is already a privileged URL then that same URL is returned. 226 // 227 // If |url| is that of the online NTP, its host is replaced with "online-ntp". 228 // This forces the NTP and search results pages to have different SiteIntances, 229 // and hence different processes. 230 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile); 231 232 // Rewrites |url| if 233 // 1. |url| is kChromeUINewTabURL, 234 // 2. InstantExtended is enabled, and 235 // 3. The --instant-new-tab-url switch is set to a valid URL. 236 // |url| is rewritten to the value of --instant-new-tab-url. 237 bool HandleNewTabURLRewrite(GURL* url, 238 content::BrowserContext* browser_context); 239 // Reverses the operation from HandleNewTabURLRewrite. 240 bool HandleNewTabURLReverseRewrite(GURL* url, 241 content::BrowserContext* browser_context); 242 243 // Sets the Instant support |state| in the navigation |entry|. 244 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, 245 content::NavigationEntry* entry); 246 247 // Returns the Instant support state attached to the NavigationEntry, or 248 // INSTANT_SUPPORT_UNKNOWN otherwise. 249 InstantSupportState GetInstantSupportStateFromNavigationEntry( 250 const content::NavigationEntry& entry); 251 252 // Returns true if the field trial flag is enabled to prefetch results on SRP. 253 bool ShouldPrefetchSearchResultsOnSRP(); 254 255 // ----------------------------------------------------- 256 // The following APIs are exposed for use in tests only. 257 // ----------------------------------------------------- 258 259 // Forces query in the omnibox to be on for tests. 260 void EnableQueryExtractionForTesting(); 261 262 // Type for a collection of experiment configuration parameters. 263 typedef std::vector<std::pair<std::string, std::string> > FieldTrialFlags; 264 265 // Finds the active field trial group name and parses out the configuration 266 // flags. On success, |flags| will be filled with the field trial flags. |flags| 267 // must not be NULL. Returns true iff the active field trial is successfully 268 // parsed and not disabled. 269 // Note that |flags| may be successfully populated in some cases when false is 270 // returned - in these cases it should not be used. 271 // Exposed for testing only. 272 bool GetFieldTrialInfo(FieldTrialFlags* flags); 273 274 // Given a FieldTrialFlags object, returns the string value of the provided 275 // flag. 276 // Exposed for testing only. 277 std::string GetStringValueForFlagWithDefault(const std::string& flag, 278 const std::string& default_value, 279 const FieldTrialFlags& flags); 280 281 // Given a FieldTrialFlags object, returns the uint64 value of the provided 282 // flag. 283 // Exposed for testing only. 284 uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag, 285 uint64 default_value, 286 const FieldTrialFlags& flags); 287 288 // Given a FieldTrialFlags object, returns the bool value of the provided flag. 289 // Exposed for testing only. 290 bool GetBoolValueForFlagWithDefault(const std::string& flag, 291 bool default_value, 292 const FieldTrialFlags& flags); 293 294 // Returns the Cacheable New Tab Page URL for the given |profile|. 295 GURL GetNewTabPageURL(Profile* profile); 296 297 // Returns true if 'use_alternate_instant_url' flag is set to true in field 298 // trials to use an alternate Instant search base page URL for prefetching 299 // search results. This allows experimentation of Instant search. 300 bool ShouldUseAltInstantURL(); 301 302 } // namespace chrome 303 304 #endif // CHROME_BROWSER_SEARCH_SEARCH_H_ 305