Home | History | Annotate | Download | only in search
      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 // Use this value for "start margin" to prevent the "es_sm" parameter from
     52 // being used.
     53 extern const int kDisableStartMargin;
     54 
     55 // Returns whether the Instant Extended API is enabled.
     56 bool IsInstantExtendedAPIEnabled();
     57 
     58 // Returns whether the suggest is enabled for the given |profile|.
     59 bool IsSuggestPrefEnabled(Profile* profile);
     60 
     61 // Returns the value to pass to the &espv CGI parameter when loading the
     62 // embedded search page from the user's default search provider. Returns 0 if
     63 // the Instant Extended API is not enabled.
     64 uint64 EmbeddedSearchPageVersion();
     65 
     66 // Returns whether query extraction is enabled.
     67 bool IsQueryExtractionEnabled();
     68 
     69 // Extracts and returns search terms from |url|. Returns empty string if the URL
     70 // is not secure or doesn't have a search term replacement key.  Does not
     71 // consider IsQueryExtractionEnabled() and Instant support state of the page and
     72 // does not check for a privileged process, so most callers should use
     73 // GetSearchTerms() below instead.
     74 base::string16 GetSearchTermsFromURL(Profile* profile, const GURL& url);
     75 
     76 // Returns the search terms attached to a specific NavigationEntry, or empty
     77 // string otherwise. Does not consider IsQueryExtractionEnabled() and does not
     78 // check Instant support, so most callers should use GetSearchTerms() below
     79 // instead.
     80 base::string16 GetSearchTermsFromNavigationEntry(
     81     const content::NavigationEntry* entry);
     82 
     83 // Returns search terms if this WebContents is a search results page. It looks
     84 // in the visible NavigationEntry first, to see if search terms have already
     85 // been extracted. Failing that, it tries to extract search terms from the URL.
     86 //
     87 // Returns a blank string if search terms were not found, or if search terms
     88 // extraction is disabled for this WebContents or profile, or if |contents|
     89 // does not support Instant.
     90 base::string16 GetSearchTerms(const content::WebContents* contents);
     91 
     92 // Returns true if |url| should be rendered in the Instant renderer process.
     93 bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile);
     94 
     95 // Returns true if the Instant |url| should use process per site.
     96 bool ShouldUseProcessPerSiteForInstantURL(const GURL& url, Profile* profile);
     97 
     98 // Returns true if |url| corresponds to a New Tab page (it can be either an
     99 // Instant Extended NTP or a non-extended NTP).
    100 bool IsNTPURL(const GURL& url, Profile* profile);
    101 
    102 // Returns true if the visible entry of |contents| is a New Tab Page rendered
    103 // by Instant. A page that matches the search or Instant URL of the default
    104 // search provider but does not have any search terms is considered an Instant
    105 // New Tab Page.
    106 bool IsInstantNTP(const content::WebContents* contents);
    107 
    108 // Same as IsInstantNTP but uses |nav_entry| to determine the URL for the page
    109 // instead of using the visible entry.
    110 bool NavEntryIsInstantNTP(const content::WebContents* contents,
    111                           const content::NavigationEntry* nav_entry);
    112 
    113 // Returns the Instant URL of the default search engine. Returns an empty GURL
    114 // if the engine doesn't have an Instant URL, or if it shouldn't be used (say
    115 // because it doesn't satisfy the requirements for extended mode or if Instant
    116 // is disabled through preferences). Callers must check that the returned URL is
    117 // valid before using it. The value of |start_margin| is used for the "es_sm"
    118 // parameter in the URL. |force_instant_results| forces a search page to update
    119 // results incrementally even if that is otherwise disabled by google.com
    120 // preferences.
    121 // NOTE: This method expands the default search engine's instant_url template,
    122 // so it shouldn't be called from SearchTermsData or other such code that would
    123 // lead to an infinite recursion.
    124 GURL GetInstantURL(Profile* profile,
    125                    int start_margin,
    126                    bool force_instant_results);
    127 
    128 // Returns URLs associated with the default search engine for |profile|.
    129 std::vector<GURL> GetSearchURLs(Profile* profile);
    130 
    131 // Returns the default search engine base page URL to prefetch search results.
    132 // Returns an empty URL if 'prefetch_results' flag is set to false in field
    133 // trials.
    134 GURL GetSearchResultPrefetchBaseURL(Profile* profile);
    135 
    136 // Returns true if 'prefetch_results' flag is set to true in field trials to
    137 // prefetch high-confidence search suggestions.
    138 bool ShouldPrefetchSearchResults();
    139 
    140 // Returns the Local Instant URL of the New Tab Page.
    141 // TODO(kmadhusu): Remove this function and update the call sites.
    142 GURL GetLocalInstantURL(Profile* profile);
    143 
    144 // Returns true if 'hide_verbatim' flag is enabled in field trials
    145 // to hide the top match in the native suggestions dropdown if it is a verbatim
    146 // match.  See comments on ShouldHideTopMatch in autocomplete_result.h.
    147 bool ShouldHideTopVerbatimMatch();
    148 
    149 // Returns true if the cacheable NTP should be shown and false if not.
    150 // Exposed for testing.
    151 bool ShouldUseCacheableNTP();
    152 
    153 // Returns true if the Instant NTP should be shown and false if not.
    154 bool ShouldShowInstantNTP();
    155 
    156 // Returns when we should show a search button in the omnibox.  This may be any
    157 // of several values, some of which depend on whether the underlying state of
    158 // the page would normally be to perform search term replacement; see also
    159 // ToolbarModel::WouldPerformSearchTermReplacement().
    160 DisplaySearchButtonConditions GetDisplaySearchButtonConditions();
    161 
    162 // Returns true if the origin chip should be shown next to the omnibox. This
    163 // also includes the related changes to the omnibox.
    164 bool ShouldDisplayOriginChip();
    165 
    166 // Transforms the input |url| into its "effective URL". The returned URL
    167 // facilitates grouping process-per-site. The |url| is transformed, for
    168 // example, from
    169 //
    170 //   https://www.google.com/search?espv=1&q=tractors
    171 //
    172 // to the privileged URL
    173 //
    174 //   chrome-search://www.google.com/search?espv=1&q=tractors
    175 //
    176 // Notice the scheme change.
    177 //
    178 // If the input is already a privileged URL then that same URL is returned.
    179 //
    180 // If |url| is that of the online NTP, its host is replaced with "online-ntp".
    181 // This forces the NTP and search results pages to have different SiteIntances,
    182 // and hence different processes.
    183 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile);
    184 
    185 // Returns the staleness timeout (in seconds) that should be used to refresh the
    186 // InstantLoader.
    187 int GetInstantLoaderStalenessTimeoutSec();
    188 
    189 // Returns true if |contents| corresponds to a preloaded instant extended NTP.
    190 bool IsPreloadedInstantExtendedNTP(const content::WebContents* contents);
    191 
    192 // Rewrites |url| if
    193 //   1. |url| is kChromeUINewTabURL,
    194 //   2. InstantExtended is enabled, and
    195 //   3. The --instant-new-tab-url switch is set to a valid URL.
    196 // |url| is rewritten to the value of --instant-new-tab-url.
    197 bool HandleNewTabURLRewrite(GURL* url,
    198                             content::BrowserContext* browser_context);
    199 // Reverses the operation from HandleNewTabURLRewrite.
    200 bool HandleNewTabURLReverseRewrite(GURL* url,
    201                                    content::BrowserContext* browser_context);
    202 
    203 // Sets the Instant support |state| in the navigation |entry|.
    204 void SetInstantSupportStateInNavigationEntry(InstantSupportState state,
    205                                              content::NavigationEntry* entry);
    206 
    207 // Returns the Instant support state attached to the NavigationEntry, or
    208 // INSTANT_SUPPORT_UNKNOWN otherwise.
    209 InstantSupportState GetInstantSupportStateFromNavigationEntry(
    210     const content::NavigationEntry& entry);
    211 
    212 // Returns true if the field trial flag is enabled to prefetch results on SRP.
    213 bool ShouldPrefetchSearchResultsOnSRP();
    214 
    215 // -----------------------------------------------------
    216 // The following APIs are exposed for use in tests only.
    217 // -----------------------------------------------------
    218 
    219 // Forces query in the omnibox to be on for tests.
    220 void EnableQueryExtractionForTesting();
    221 
    222 // Type for a collection of experiment configuration parameters.
    223 typedef std::vector<std::pair<std::string, std::string> > FieldTrialFlags;
    224 
    225 // Finds the active field trial group name and parses out the configuration
    226 // flags. On success, |flags| will be filled with the field trial flags. |flags|
    227 // must not be NULL. Returns true iff the active field trial is successfully
    228 // parsed and not disabled.
    229 // Note that |flags| may be successfully populated in some cases when false is
    230 // returned - in these cases it should not be used.
    231 // Exposed for testing only.
    232 bool GetFieldTrialInfo(FieldTrialFlags* flags);
    233 
    234 // Given a FieldTrialFlags object, returns the string value of the provided
    235 // flag.
    236 // Exposed for testing only.
    237 std::string GetStringValueForFlagWithDefault(const std::string& flag,
    238                                              const std::string& default_value,
    239                                              const FieldTrialFlags& flags);
    240 
    241 // Given a FieldTrialFlags object, returns the uint64 value of the provided
    242 // flag.
    243 // Exposed for testing only.
    244 uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag,
    245                                         uint64 default_value,
    246                                         const FieldTrialFlags& flags);
    247 
    248 // Given a FieldTrialFlags object, returns the bool value of the provided flag.
    249 // Exposed for testing only.
    250 bool GetBoolValueForFlagWithDefault(const std::string& flag,
    251                                     bool default_value,
    252                                     const FieldTrialFlags& flags);
    253 
    254 // Returns the Cacheable New Tab Page URL for the given |profile|.
    255 GURL GetNewTabPageURL(Profile* profile);
    256 
    257 }  // namespace chrome
    258 
    259 #endif  // CHROME_BROWSER_SEARCH_SEARCH_H_
    260