- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / search / search.h
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 // Use this value for "start margin" to prevent the "es_sm" parameter from
44 // being used.
45 extern const int kDisableStartMargin;
46
47 // Returns whether the Instant Extended API is enabled.
48 bool IsInstantExtendedAPIEnabled();
49
50 // Returns whether the suggest is enabled for the given |profile|.
51 bool 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.
56 uint64 EmbeddedSearchPageVersion();
57
58 // Returns whether query extraction is enabled.
59 bool 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.
66 string16 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.
72 string16 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.
82 string16 GetSearchTerms(const content::WebContents* contents);
83
84 // Returns true if |url| should be rendered in the Instant renderer process.
85 bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile);
86
87 // Returns true if the Instant |url| should use process per site.
88 bool 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).
92 bool 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.
98 bool 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.
102 bool 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. |force_instant_results| forces a search page to update
111 // results incrementally even if that is otherwise disabled by google.com
112 // preferences.
113 // NOTE: This method expands the default search engine's instant_url template,
114 // so it shouldn't be called from SearchTermsData or other such code that would
115 // lead to an infinite recursion.
116 GURL GetInstantURL(Profile* profile,
117                    int start_margin,
118                    bool force_instant_results);
119
120 // Returns URLs associated with the default search engine for |profile|.
121 std::vector<GURL> GetSearchURLs(Profile* profile);
122
123 // Returns the Local Instant URL of the New Tab Page.
124 // TODO(kmadhusu): Remove this function and update the call sites.
125 GURL GetLocalInstantURL(Profile* profile);
126
127 // Returns true if 'hide_verbatim' flag is enabled in field trials
128 // to hide the top match in the native suggestions dropdown if it is a verbatim
129 // match.  See comments on ShouldHideTopMatch in autocomplete_result.h.
130 bool ShouldHideTopVerbatimMatch();
131
132 // Returns true if 'use_remote_ntp_on_startup' flag is enabled in field trials
133 // to always show the remote NTP on browser startup.
134 bool ShouldPreferRemoteNTPOnStartup();
135
136 // Returns true if the cacheable NTP should be shown and false if not.
137 // Exposed for testing.
138 bool ShouldUseCacheableNTP();
139
140 // Returns true if the Instant NTP should be shown and false if not.
141 bool ShouldShowInstantNTP();
142
143 // Returns true if the recent tabs link should be shown on the local NTP in
144 // field trials.
145 bool ShouldShowRecentTabsOnNTP();
146
147 // Returns true if Instant Extended should be disabled on the search results
148 // page.
149 bool ShouldSuppressInstantExtendedOnSRP();
150
151 // Transforms the input |url| into its "effective URL". The returned URL
152 // facilitates grouping process-per-site. The |url| is transformed, for
153 // example, from
154 //
155 //   https://www.google.com/search?espv=1&q=tractors
156 //
157 // to the privileged URL
158 //
159 //   chrome-search://www.google.com/search?espv=1&q=tractors
160 //
161 // Notice the scheme change.
162 //
163 // If the input is already a privileged URL then that same URL is returned.
164 //
165 // If |url| is that of the online NTP, its host is replaced with "online-ntp".
166 // This forces the NTP and search results pages to have different SiteIntances,
167 // and hence different processes.
168 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile);
169
170 // Returns the staleness timeout (in seconds) that should be used to refresh the
171 // InstantLoader.
172 int GetInstantLoaderStalenessTimeoutSec();
173
174 // Returns true if |contents| corresponds to a preloaded instant extended NTP.
175 bool IsPreloadedInstantExtendedNTP(const content::WebContents* contents);
176
177 // Rewrites |url| if
178 //   1. |url| is kChromeUINewTabURL,
179 //   2. InstantExtended is enabled, and
180 //   3. The --instant-new-tab-url switch is set to a valid URL.
181 // |url| is rewritten to the value of --instant-new-tab-url.
182 bool HandleNewTabURLRewrite(GURL* url,
183                             content::BrowserContext* browser_context);
184 // Reverses the operation from HandleNewTabURLRewrite.
185 bool HandleNewTabURLReverseRewrite(GURL* url,
186                                    content::BrowserContext* browser_context);
187
188 // Sets the Instant support |state| in the navigation |entry|.
189 void SetInstantSupportStateInNavigationEntry(InstantSupportState state,
190                                              content::NavigationEntry* entry);
191
192 // Returns the Instant support state attached to the NavigationEntry, or
193 // INSTANT_SUPPORT_UNKNOWN otherwise.
194 InstantSupportState GetInstantSupportStateFromNavigationEntry(
195     const content::NavigationEntry& entry);
196
197 // Returns true if the field trial flag is enabled to prefetch results on SRP.
198 bool ShouldPrefetchSearchResultsOnSRP();
199
200 // -----------------------------------------------------
201 // The following APIs are exposed for use in tests only.
202 // -----------------------------------------------------
203
204 // Forces the Instant Extended API to be enabled for tests.
205 void EnableInstantExtendedAPIForTesting();
206
207 // Forces the Instant Extended API to be disabled for tests.
208 void DisableInstantExtendedAPIForTesting();
209
210 // Type for a collection of experiment configuration parameters.
211 typedef std::vector<std::pair<std::string, std::string> > FieldTrialFlags;
212
213 // Finds the active field trial group name and parses out the group number and
214 // configuration flags. On success, |flags| will be filled with the field trial
215 // flags. |flags| must not be NULL. If not NULL, |group_number| will receive the
216 // experiment group number.
217 // Returns true iff the active field trial is successfully parsed and not
218 // disabled.
219 // Note that |flags| may be successfully populated in some cases when false is
220 // returned - in these cases it should not be used.
221 // Exposed for testing only.
222 bool GetFieldTrialInfo(FieldTrialFlags* flags,
223                        uint64* group_number);
224
225 // Given a FieldTrialFlags object, returns the string value of the provided
226 // flag.
227 // Exposed for testing only.
228 std::string GetStringValueForFlagWithDefault(const std::string& flag,
229                                              const std::string& default_value,
230                                              const FieldTrialFlags& flags);
231
232 // Given a FieldTrialFlags object, returns the uint64 value of the provided
233 // flag.
234 // Exposed for testing only.
235 uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag,
236                                         uint64 default_value,
237                                         const FieldTrialFlags& flags);
238
239 // Given a FieldTrialFlags object, returns the bool value of the provided flag.
240 // Exposed for testing only.
241 bool GetBoolValueForFlagWithDefault(const std::string& flag,
242                                     bool default_value,
243                                     const FieldTrialFlags& flags);
244
245 // Returns the Cacheable New Tab Page URL for the given |profile|.
246 GURL GetNewTabPageURL(Profile* profile);
247
248 // Let tests reset the gate that prevents metrics from being sent more than
249 // once.
250 void ResetInstantExtendedOptInStateGateForTest();
251
252 }  // namespace chrome
253
254 #endif  // CHROME_BROWSER_SEARCH_SEARCH_H_