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