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
;
33 // For reporting Cacheable NTP navigations.
34 enum CacheableNTPLoad
{
35 CACHEABLE_NTP_LOAD_FAILED
= 0,
36 CACHEABLE_NTP_LOAD_SUCCEEDED
= 1,
37 CACHEABLE_NTP_LOAD_MAX
= 2
41 // The user has not manually opted in/out of InstantExtended.
42 INSTANT_EXTENDED_NOT_SET
,
43 // The user has opted-in to InstantExtended.
44 INSTANT_EXTENDED_OPT_IN
,
45 // The user has opted-out of InstantExtended.
46 INSTANT_EXTENDED_OPT_OUT
,
47 INSTANT_EXTENDED_OPT_IN_STATE_ENUM_COUNT
,
50 // Use this value for "start margin" to prevent the "es_sm" parameter from
52 extern const int kDisableStartMargin
;
54 // Returns whether the suggest is enabled for the given |profile|.
55 bool IsSuggestPrefEnabled(Profile
* profile
);
57 // Returns a string indicating whether InstantExtended is enabled, suitable
58 // for adding as a query string param to the homepage or search requests.
59 // Returns an empty string otherwise.
61 // |for_search| should be set to true for search requests, in which case this
62 // returns a non-empty string only if query extraction is enabled.
63 std::string
InstantExtendedEnabledParam(bool for_search
);
65 // Returns a string that will cause the search results page to update
66 // incrementally. Currently, Instant Extended passes a different param to
67 // search results pages that also has this effect, so by default this function
68 // returns the empty string when Instant Extended is enabled. However, when
69 // doing instant search result prerendering, we still need to pass this param,
70 // as Instant Extended does not cause incremental updates by default for the
71 // prerender page. Callers should set |for_prerender| in this case to force
72 // the returned string to be non-empty.
73 std::string
ForceInstantResultsParam(bool for_prerender
);
75 // Returns whether query extraction is enabled.
76 bool IsQueryExtractionEnabled();
78 // Extracts and returns search terms from |url|. Does not consider
79 // IsQueryExtractionEnabled() and Instant support state of the page and does
80 // not check for a privileged process, so most callers should use
81 // GetSearchTerms() below instead.
82 base::string16
ExtractSearchTermsFromURL(Profile
* profile
, const GURL
& url
);
84 // Returns true if it is okay to extract search terms from |url|. |url| must
85 // have a secure scheme and must contain the search terms replacement key for
86 // the default search provider.
87 bool IsQueryExtractionAllowedForURL(Profile
* profile
, const GURL
& url
);
89 // Returns the search terms attached to a specific NavigationEntry, or empty
90 // string otherwise. Does not consider IsQueryExtractionEnabled() and does not
91 // check Instant support, so most callers should use GetSearchTerms() below
93 base::string16
GetSearchTermsFromNavigationEntry(
94 const content::NavigationEntry
* entry
);
96 // Returns search terms if this WebContents is a search results page. It looks
97 // in the visible NavigationEntry first, to see if search terms have already
98 // been extracted. Failing that, it tries to extract search terms from the URL.
100 // Returns a blank string if search terms were not found, or if search terms
101 // extraction is disabled for this WebContents or profile, or if |contents|
102 // does not support Instant.
103 base::string16
GetSearchTerms(const content::WebContents
* contents
);
105 // Returns true if |url| should be rendered in the Instant renderer process.
106 bool ShouldAssignURLToInstantRenderer(const GURL
& url
, Profile
* profile
);
108 // Returns true if |contents| is rendered inside the Instant process for
110 bool IsRenderedInInstantProcess(const content::WebContents
* contents
,
113 // Returns true if the Instant |url| should use process per site.
114 bool ShouldUseProcessPerSiteForInstantURL(const GURL
& url
, Profile
* profile
);
116 // Returns true if |url| corresponds to a New Tab page (it can be either an
117 // Instant Extended NTP or a non-extended NTP).
118 bool IsNTPURL(const GURL
& url
, Profile
* profile
);
120 // Returns true if the visible entry of |contents| is a New Tab Page rendered
121 // by Instant. A page that matches the search or Instant URL of the default
122 // search provider but does not have any search terms is considered an Instant
124 bool IsInstantNTP(const content::WebContents
* contents
);
126 // Same as IsInstantNTP but uses |nav_entry| to determine the URL for the page
127 // instead of using the visible entry.
128 bool NavEntryIsInstantNTP(const content::WebContents
* contents
,
129 const content::NavigationEntry
* nav_entry
);
131 // Returns the Instant URL of the default search engine. Returns an empty GURL
132 // if the engine doesn't have an Instant URL, or if it shouldn't be used (say
133 // because it doesn't satisfy the requirements for extended mode or if Instant
134 // is disabled through preferences). Callers must check that the returned URL is
135 // valid before using it. |force_instant_results| forces a search page to update
136 // results incrementally even if that is otherwise disabled by google.com
138 // NOTE: This method expands the default search engine's instant_url template,
139 // so it shouldn't be called from SearchTermsData or other such code that would
140 // lead to an infinite recursion.
141 GURL
GetInstantURL(Profile
* profile
, bool force_instant_results
);
143 // Returns URLs associated with the default search engine for |profile|.
144 std::vector
<GURL
> GetSearchURLs(Profile
* profile
);
146 // Returns the default search engine base page URL to prefetch search results.
147 // Returns an empty URL if 'prefetch_results' flag is set to false in field
149 GURL
GetSearchResultPrefetchBaseURL(Profile
* profile
);
151 // Returns true if 'prefetch_results' flag is set to true in field trials to
152 // prefetch high-confidence search suggestions.
153 bool ShouldPrefetchSearchResults();
155 // Returns true if 'allow_prefetch_non_default_match' flag is enabled in field
156 // trials to allow prefetching the suggestion marked to be prefetched by the
157 // suggest server even if it is not the default match.
158 bool ShouldAllowPrefetchNonDefaultMatch();
160 // Returns true if 'prerender_instant_url_on_omnibox_focus' flag is enabled in
161 // field trials to prerender Instant search base page when the omnibox is
163 bool ShouldPrerenderInstantUrlOnOmniboxFocus();
165 // Returns true if 'reuse_instant_search_base_page' flag is set to true in field
166 // trials to reuse the prerendered page to commit any search query.
167 bool ShouldReuseInstantSearchBasePage();
169 // Returns the Local Instant URL of the New Tab Page.
170 // TODO(kmadhusu): Remove this function and update the call sites.
171 GURL
GetLocalInstantURL(Profile
* profile
);
173 // Returns true if the local new tab page should show a Google logo and search
174 // box for users whose default search provider is Google, or false if not.
175 bool ShouldShowGoogleLocalNTP();
177 // Transforms the input |url| into its "effective URL". The returned URL
178 // facilitates grouping process-per-site. The |url| is transformed, for
181 // https://www.google.com/search?espv=1&q=tractors
183 // to the privileged URL
185 // chrome-search://www.google.com/search?espv=1&q=tractors
187 // Notice the scheme change.
189 // If the input is already a privileged URL then that same URL is returned.
191 // If |url| is that of the online NTP, its host is replaced with "online-ntp".
192 // This forces the NTP and search results pages to have different SiteIntances,
193 // and hence different processes.
194 GURL
GetEffectiveURLForInstant(const GURL
& url
, Profile
* profile
);
197 // 1. |url| is kChromeUINewTabURL,
198 // 2. InstantExtended is enabled, and
199 // 3. The --instant-new-tab-url switch is set to a valid URL.
200 // |url| is rewritten to the value of --instant-new-tab-url.
201 bool HandleNewTabURLRewrite(GURL
* url
,
202 content::BrowserContext
* browser_context
);
203 // Reverses the operation from HandleNewTabURLRewrite.
204 bool HandleNewTabURLReverseRewrite(GURL
* url
,
205 content::BrowserContext
* browser_context
);
207 // Sets the Instant support |state| in the navigation |entry|.
208 void SetInstantSupportStateInNavigationEntry(InstantSupportState state
,
209 content::NavigationEntry
* entry
);
211 // Returns the Instant support state attached to the NavigationEntry, or
212 // INSTANT_SUPPORT_UNKNOWN otherwise.
213 InstantSupportState
GetInstantSupportStateFromNavigationEntry(
214 const content::NavigationEntry
& entry
);
216 // Returns true if the field trial flag is enabled to prefetch results on SRP.
217 bool ShouldPrefetchSearchResultsOnSRP();
219 // -----------------------------------------------------
220 // The following APIs are exposed for use in tests only.
221 // -----------------------------------------------------
223 // Forces query in the omnibox to be on for tests.
224 void EnableQueryExtractionForTesting();
226 // Returns the Cacheable New Tab Page URL for the given |profile|.
227 GURL
GetNewTabPageURL(Profile
* profile
);
229 // Returns true if 'use_alternate_instant_url' flag is set to true in field
230 // trials to use an alternate Instant search base page URL for prefetching
231 // search results. This allows experimentation of Instant search.
232 bool ShouldUseAltInstantURL();
234 // Returns true if 'use_search_path_for_instant' flag is set to true in field
235 // trials to use an '/search' path in an alternate Instant search base page URL
236 // for prefetching search results. This allows experimentation of Instant
238 bool ShouldUseSearchPathForInstant();
240 } // namespace chrome
242 #endif // CHROME_BROWSER_SEARCH_SEARCH_H_