search.h revision a3f7b4e666c476898878fa745f637129375cd889
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
15class GURL;
16class Profile;
17class TemplateURL;
18class TemplateURLRef;
19
20namespace content {
21class NavigationEntry;
22class WebContents;
23}
24
25namespace user_prefs {
26class PrefRegistrySyncable;
27}
28
29namespace chrome {
30
31enum OptInState {
32  // The user has not manually opted in/out of InstantExtended.
33  INSTANT_EXTENDED_NOT_SET,
34  // The user has opted-in to InstantExtended.
35  INSTANT_EXTENDED_OPT_IN,
36  // The user has opted-out of InstantExtended.
37  INSTANT_EXTENDED_OPT_OUT,
38  INSTANT_EXTENDED_OPT_IN_STATE_ENUM_COUNT,
39};
40
41// Use this value for "start margin" to prevent the "es_sm" parameter from
42// being used.
43extern const int kDisableStartMargin;
44
45// Returns whether the Instant Extended API is enabled.
46bool IsInstantExtendedAPIEnabled();
47
48// Returns the value to pass to the &espv CGI parameter when loading the
49// embedded search page from the user's default search provider. Returns 0 if
50// the Instant Extended API is not enabled.
51uint64 EmbeddedSearchPageVersion();
52
53// Returns whether query extraction is enabled.
54bool IsQueryExtractionEnabled();
55
56// Extracts and returns search terms from |url|. Returns empty string if the URL
57// is not secure or doesn't have a search term replacement key.  Does not
58// consider IsQueryExtractionEnabled() and does not check for a privileged
59// process, so most callers should use GetSearchTerms() below instead.
60string16 GetSearchTermsFromURL(Profile* profile, const GURL& url);
61
62// Returns the search terms attached to a specific NavigationEntry, or empty
63// string otherwise. Does not consider IsQueryExtractionEnabled(), so most
64// callers should use GetSearchTerms() below instead.
65string16 GetSearchTermsFromNavigationEntry(
66    const content::NavigationEntry* entry);
67
68// Returns search terms if this WebContents is a search results page. It looks
69// in the visible NavigationEntry first, to see if search terms have already
70// been extracted. Failing that, it tries to extract search terms from the URL.
71// Returns a blank string if search terms were not found, or if search terms
72// extraction is disabled for this WebContents or profile.
73string16 GetSearchTerms(const content::WebContents* contents);
74
75// Returns true if |url| should be rendered in the Instant renderer process.
76bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile);
77
78// Returns true if the Instant |url| should use process per site.
79bool ShouldUseProcessPerSiteForInstantURL(const GURL& url, Profile* profile);
80
81// Returns true if the visible entry of |contents| is a New Tab Page rendered
82// by Instant. A page that matches the search or Instant URL of the default
83// search provider but does not have any search terms is considered an Instant
84// New Tab Page.
85bool IsInstantNTP(const content::WebContents* contents);
86
87// Same as IsInstantNTP but uses |nav_entry| to determine the URL for the page
88// instead of using the visible entry.
89bool NavEntryIsInstantNTP(const content::WebContents* contents,
90                          const content::NavigationEntry* nav_entry);
91
92// Returns the Instant URL of the default search engine. Returns an empty GURL
93// if the engine doesn't have an Instant URL, or if it shouldn't be used (say
94// because it doesn't satisfy the requirements for extended mode or if Instant
95// is disabled through preferences). Callers must check that the returned URL is
96// valid before using it. The value of |start_margin| is used for the "es_sm"
97// parameter in the URL.
98// NOTE: This method expands the default search engine's instant_url template,
99// so it shouldn't be called from SearchTermsData or other such code that would
100// lead to an infinite recursion.
101GURL GetInstantURL(Profile* profile, int start_margin);
102
103// Returns the Local Instant URL of the New Tab Page.
104// TODO(kmadhusu): Remove this function and update the call sites.
105GURL GetLocalInstantURL(Profile* profile);
106
107// Returns true if 'use_remote_ntp_on_startup' flag is enabled in field trials
108// to always show the remote NTP on browser startup.
109bool ShouldPreferRemoteNTPOnStartup();
110
111// Returns true if the Instant NTP should be shown and false if not.
112bool ShouldShowInstantNTP();
113
114// Returns true if the recent tabs link should be shown on the local NTP in
115// field trials.
116bool ShouldShowRecentTabsOnNTP();
117
118// Returns true if |my_url| matches |other_url|.
119bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url);
120
121// Transforms the input |url| into its "privileged URL". The returned URL
122// facilitates grouping process-per-site. The |url| is transformed, for
123// example, from
124//
125//   https://www.google.com/search?espv=1&q=tractors
126//
127// to the privileged URL
128//
129//   chrome-search://www.google.com/search?espv=1&q=tractors
130//
131// Notice the scheme change.
132//
133// If the input is already a privileged URL then that same URL is returned.
134//
135// If |url| is that of the online NTP, its host is replaced with "online-ntp".
136// This forces the NTP and search results pages to have different SiteIntances,
137// and hence different processes.
138GURL GetPrivilegedURLForInstant(const GURL& url, Profile* profile);
139
140// Returns true if the input |url| is a privileged Instant URL.
141bool IsPrivilegedURLForInstant(const GURL& url);
142
143// Returns the staleness timeout (in seconds) that should be used to refresh the
144// InstantLoader.
145int GetInstantLoaderStalenessTimeoutSec();
146
147// Returns true if |contents| corresponds to a preloaded instant extended NTP.
148bool IsPreloadedInstantExtendedNTP(const content::WebContents* contents);
149
150// -----------------------------------------------------
151// The following APIs are exposed for use in tests only.
152// -----------------------------------------------------
153
154// Forces the Instant Extended API to be enabled for tests.
155void EnableInstantExtendedAPIForTesting();
156
157// Forces the Instant Extended API to be disabled for tests.
158void DisableInstantExtendedAPIForTesting();
159
160// Type for a collection of experiment configuration parameters.
161typedef std::vector<std::pair<std::string, std::string> > FieldTrialFlags;
162
163// Given a field trial group name, parses out the group number and configuration
164// flags. On success, |flags| will be filled with the field trial flags. |flags|
165// must not be NULL. If not NULL, |group_number| will receive the experiment
166// group number.
167// Returns true iff field trial info was successfully parsed out of
168// |group_name|.
169// Exposed for testing only.
170bool GetFieldTrialInfo(const std::string& group_name,
171                       FieldTrialFlags* flags,
172                       uint64* group_number);
173
174// Given a FieldTrialFlags object, returns the string value of the provided
175// flag.
176// Exposed for testing only.
177std::string GetStringValueForFlagWithDefault(const std::string& flag,
178                                             const std::string& default_value,
179                                             const FieldTrialFlags& flags);
180
181// Given a FieldTrialFlags object, returns the uint64 value of the provided
182// flag.
183// Exposed for testing only.
184uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag,
185                                        uint64 default_value,
186                                        const FieldTrialFlags& flags);
187
188// Given a FieldTrialFlags object, returns the bool value of the provided flag.
189// Exposed for testing only.
190bool GetBoolValueForFlagWithDefault(const std::string& flag,
191                                    bool default_value,
192                                    const FieldTrialFlags& flags);
193
194// Let tests reset the gate that prevents metrics from being sent more than
195// once.
196void ResetInstantExtendedOptInStateGateForTest();
197
198}  // namespace chrome
199
200#endif  // CHROME_BROWSER_SEARCH_SEARCH_H_
201