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 suggest is enabled for the given |profile|. 63 bool IsSuggestPrefEnabled(Profile* profile); 64 65 // Returns a string indicating whether InstantExtended is enabled, suitable 66 // for adding as a query string param to the homepage or search requests. 67 // Returns an empty string otherwise. 68 // 69 // |for_search| should be set to true for search requests, in which case this 70 // returns a non-empty string only if query extraction is enabled. 71 std::string InstantExtendedEnabledParam(bool for_search); 72 73 // Returns a string that will cause the search results page to update 74 // incrementally. Currently, Instant Extended passes a different param to 75 // search results pages that also has this effect, so by default this function 76 // returns the empty string when Instant Extended is enabled. However, when 77 // doing instant search result prerendering, we still need to pass this param, 78 // as Instant Extended does not cause incremental updates by default for the 79 // prerender page. Callers should set |for_prerender| in this case to force 80 // the returned string to be non-empty. 81 std::string ForceInstantResultsParam(bool for_prerender); 82 83 // Returns whether query extraction is enabled. 84 bool IsQueryExtractionEnabled(); 85 86 // Extracts and returns search terms from |url|. Does not consider 87 // IsQueryExtractionEnabled() and Instant support state of the page and does 88 // not check for a privileged process, so most callers should use 89 // GetSearchTerms() below instead. 90 base::string16 ExtractSearchTermsFromURL(Profile* profile, const GURL& url); 91 92 // Returns true if it is okay to extract search terms from |url|. |url| must 93 // have a secure scheme and must contain the search terms replacement key for 94 // the default search provider. 95 bool IsQueryExtractionAllowedForURL(Profile* profile, const GURL& url); 96 97 // Returns the search terms attached to a specific NavigationEntry, or empty 98 // string otherwise. Does not consider IsQueryExtractionEnabled() and does not 99 // check Instant support, so most callers should use GetSearchTerms() below 100 // instead. 101 base::string16 GetSearchTermsFromNavigationEntry( 102 const content::NavigationEntry* entry); 103 104 // Returns search terms if this WebContents is a search results page. It looks 105 // in the visible NavigationEntry first, to see if search terms have already 106 // been extracted. Failing that, it tries to extract search terms from the URL. 107 // 108 // Returns a blank string if search terms were not found, or if search terms 109 // extraction is disabled for this WebContents or profile, or if |contents| 110 // does not support Instant. 111 base::string16 GetSearchTerms(const content::WebContents* contents); 112 113 // Returns true if |url| should be rendered in the Instant renderer process. 114 bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile); 115 116 // Returns true if |contents| is rendered inside the Instant process for 117 // |profile|. 118 bool IsRenderedInInstantProcess(const content::WebContents* contents, 119 Profile* profile); 120 121 // Returns true if the Instant |url| should use process per site. 122 bool ShouldUseProcessPerSiteForInstantURL(const GURL& url, Profile* profile); 123 124 // Returns true if |url| corresponds to a New Tab page (it can be either an 125 // Instant Extended NTP or a non-extended NTP). 126 bool IsNTPURL(const GURL& url, Profile* profile); 127 128 // Returns true if the visible entry of |contents| is a New Tab Page rendered 129 // by Instant. A page that matches the search or Instant URL of the default 130 // search provider but does not have any search terms is considered an Instant 131 // New Tab Page. 132 bool IsInstantNTP(const content::WebContents* contents); 133 134 // Same as IsInstantNTP but uses |nav_entry| to determine the URL for the page 135 // instead of using the visible entry. 136 bool NavEntryIsInstantNTP(const content::WebContents* contents, 137 const content::NavigationEntry* nav_entry); 138 139 // Returns the Instant URL of the default search engine. Returns an empty GURL 140 // if the engine doesn't have an Instant URL, or if it shouldn't be used (say 141 // because it doesn't satisfy the requirements for extended mode or if Instant 142 // is disabled through preferences). Callers must check that the returned URL is 143 // valid before using it. |force_instant_results| forces a search page to update 144 // results incrementally even if that is otherwise disabled by google.com 145 // preferences. 146 // NOTE: This method expands the default search engine's instant_url template, 147 // so it shouldn't be called from SearchTermsData or other such code that would 148 // lead to an infinite recursion. 149 GURL GetInstantURL(Profile* profile, bool force_instant_results); 150 151 // Returns URLs associated with the default search engine for |profile|. 152 std::vector<GURL> GetSearchURLs(Profile* profile); 153 154 // Returns the default search engine base page URL to prefetch search results. 155 // Returns an empty URL if 'prefetch_results' flag is set to false in field 156 // trials. 157 GURL GetSearchResultPrefetchBaseURL(Profile* profile); 158 159 // Returns true if 'prefetch_results' flag is set to true in field trials to 160 // prefetch high-confidence search suggestions. 161 bool ShouldPrefetchSearchResults(); 162 163 // Returns true if 'allow_prefetch_non_default_match' flag is enabled in field 164 // trials to allow prefetching the suggestion marked to be prefetched by the 165 // suggest server even if it is not the default match. 166 bool ShouldAllowPrefetchNonDefaultMatch(); 167 168 // Returns true if 'prerender_instant_url_on_omnibox_focus' flag is enabled in 169 // field trials to prerender Instant search base page when the omnibox is 170 // focused. 171 bool ShouldPrerenderInstantUrlOnOmniboxFocus(); 172 173 // Returns true if 'reuse_instant_search_base_page' flag is set to true in field 174 // trials to reuse the prerendered page to commit any search query. 175 bool ShouldReuseInstantSearchBasePage(); 176 177 // Returns the Local Instant URL of the New Tab Page. 178 // TODO(kmadhusu): Remove this function and update the call sites. 179 GURL GetLocalInstantURL(Profile* profile); 180 181 // Returns when we should show a search button in the omnibox. This may be any 182 // of several values, some of which depend on whether the underlying state of 183 // the page would normally be to perform search term replacement; see also 184 // ToolbarModel::WouldPerformSearchTermReplacement(). 185 DisplaySearchButtonConditions GetDisplaySearchButtonConditions(); 186 187 // Returns true if the origin chip should be shown. 188 bool ShouldDisplayOriginChip(); 189 190 // Returns a value indicating when the origin chip should be shown. 191 OriginChipCondition GetOriginChipCondition(); 192 193 // Returns true if the local new tab page should show a Google logo and search 194 // box for users whose default search provider is Google, or false if not. 195 bool ShouldShowGoogleLocalNTP(); 196 197 // Transforms the input |url| into its "effective URL". The returned URL 198 // facilitates grouping process-per-site. The |url| is transformed, for 199 // example, from 200 // 201 // https://www.google.com/search?espv=1&q=tractors 202 // 203 // to the privileged URL 204 // 205 // chrome-search://www.google.com/search?espv=1&q=tractors 206 // 207 // Notice the scheme change. 208 // 209 // If the input is already a privileged URL then that same URL is returned. 210 // 211 // If |url| is that of the online NTP, its host is replaced with "online-ntp". 212 // This forces the NTP and search results pages to have different SiteIntances, 213 // and hence different processes. 214 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile); 215 216 // Rewrites |url| if 217 // 1. |url| is kChromeUINewTabURL, 218 // 2. InstantExtended is enabled, and 219 // 3. The --instant-new-tab-url switch is set to a valid URL. 220 // |url| is rewritten to the value of --instant-new-tab-url. 221 bool HandleNewTabURLRewrite(GURL* url, 222 content::BrowserContext* browser_context); 223 // Reverses the operation from HandleNewTabURLRewrite. 224 bool HandleNewTabURLReverseRewrite(GURL* url, 225 content::BrowserContext* browser_context); 226 227 // Sets the Instant support |state| in the navigation |entry|. 228 void SetInstantSupportStateInNavigationEntry(InstantSupportState state, 229 content::NavigationEntry* entry); 230 231 // Returns the Instant support state attached to the NavigationEntry, or 232 // INSTANT_SUPPORT_UNKNOWN otherwise. 233 InstantSupportState GetInstantSupportStateFromNavigationEntry( 234 const content::NavigationEntry& entry); 235 236 // Returns true if the field trial flag is enabled to prefetch results on SRP. 237 bool ShouldPrefetchSearchResultsOnSRP(); 238 239 // ----------------------------------------------------- 240 // The following APIs are exposed for use in tests only. 241 // ----------------------------------------------------- 242 243 // Forces query in the omnibox to be on for tests. 244 void EnableQueryExtractionForTesting(); 245 246 // Returns the Cacheable New Tab Page URL for the given |profile|. 247 GURL GetNewTabPageURL(Profile* profile); 248 249 // Returns true if 'use_alternate_instant_url' flag is set to true in field 250 // trials to use an alternate Instant search base page URL for prefetching 251 // search results. This allows experimentation of Instant search. 252 bool ShouldUseAltInstantURL(); 253 254 // Returns true if 'use_search_path_for_instant' flag is set to true in field 255 // trials to use an '/search' path in an alternate Instant search base page URL 256 // for prefetching search results. This allows experimentation of Instant 257 // search. 258 bool ShouldUseSearchPathForInstant(); 259 260 } // namespace chrome 261 262 #endif // CHROME_BROWSER_SEARCH_SEARCH_H_ 263