search.h revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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 Instant NTP should be shown and false if not.
130bool ShouldShowInstantNTP();
131
132// Returns true if the recent tabs link should be shown on the local NTP in
133// field trials.
134bool ShouldShowRecentTabsOnNTP();
135
136// Returns true if |my_url| matches |other_url|.
137bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url);
138
139// Transforms the input |url| into its "effective URL". The returned URL
140// facilitates grouping process-per-site. The |url| is transformed, for
141// example, from
142//
143//   https://www.google.com/search?espv=1&q=tractors
144//
145// to the privileged URL
146//
147//   chrome-search://www.google.com/search?espv=1&q=tractors
148//
149// Notice the scheme change.
150//
151// If the input is already a privileged URL then that same URL is returned.
152//
153// If |url| is that of the online NTP, its host is replaced with "online-ntp".
154// This forces the NTP and search results pages to have different SiteIntances,
155// and hence different processes.
156GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile);
157
158// Returns the staleness timeout (in seconds) that should be used to refresh the
159// InstantLoader.
160int GetInstantLoaderStalenessTimeoutSec();
161
162// Returns true if |contents| corresponds to a preloaded instant extended NTP.
163bool IsPreloadedInstantExtendedNTP(const content::WebContents* contents);
164
165// Rewrites |url| if
166//   1. |url| is kChromeUINewTabURL,
167//   2. InstantExtended is enabled, and
168//   3. The --instant-new-tab-url switch is set to a valid URL.
169// |url| is rewritten to the value of --instant-new-tab-url.
170bool HandleNewTabURLRewrite(GURL* url,
171                            content::BrowserContext* browser_context);
172// Reverses the operation from HandleNewTabURLRewrite.
173bool HandleNewTabURLReverseRewrite(GURL* url,
174                                   content::BrowserContext* browser_context);
175
176// Sets the Instant support |state| in the navigation |entry|.
177void SetInstantSupportStateInNavigationEntry(InstantSupportState state,
178                                             content::NavigationEntry* entry);
179
180// Returns the Instant support state attached to the NavigationEntry, or
181// INSTANT_SUPPORT_UNKNOWN otherwise.
182InstantSupportState GetInstantSupportStateFromNavigationEntry(
183    const content::NavigationEntry& entry);
184
185// -----------------------------------------------------
186// The following APIs are exposed for use in tests only.
187// -----------------------------------------------------
188
189// Forces the Instant Extended API to be enabled for tests.
190void EnableInstantExtendedAPIForTesting();
191
192// Forces the Instant Extended API to be disabled for tests.
193void DisableInstantExtendedAPIForTesting();
194
195// Type for a collection of experiment configuration parameters.
196typedef std::vector<std::pair<std::string, std::string> > FieldTrialFlags;
197
198// Given a field trial group name, parses out the group number and configuration
199// flags. On success, |flags| will be filled with the field trial flags. |flags|
200// must not be NULL. If not NULL, |group_number| will receive the experiment
201// group number.
202// Returns true iff |group_name| is successfully parsed and not disabled.
203// Note that |flags| may be successfully populated in some cases when false is
204// returned - in these cases it should not be used.
205// Exposed for testing only.
206bool GetFieldTrialInfo(const std::string& group_name,
207                       FieldTrialFlags* flags,
208                       uint64* group_number);
209
210// Given a FieldTrialFlags object, returns the string value of the provided
211// flag.
212// Exposed for testing only.
213std::string GetStringValueForFlagWithDefault(const std::string& flag,
214                                             const std::string& default_value,
215                                             const FieldTrialFlags& flags);
216
217// Given a FieldTrialFlags object, returns the uint64 value of the provided
218// flag.
219// Exposed for testing only.
220uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag,
221                                        uint64 default_value,
222                                        const FieldTrialFlags& flags);
223
224// Given a FieldTrialFlags object, returns the bool value of the provided flag.
225// Exposed for testing only.
226bool GetBoolValueForFlagWithDefault(const std::string& flag,
227                                    bool default_value,
228                                    const FieldTrialFlags& flags);
229
230// Let tests reset the gate that prevents metrics from being sent more than
231// once.
232void ResetInstantExtendedOptInStateGateForTest();
233
234}  // namespace chrome
235
236#endif  // CHROME_BROWSER_SEARCH_SEARCH_H_
237