72d89fe854f9540f1f8e2e5053c4d34ac681ce5d
[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 enum DisplaySearchButtonConditions {
44   DISPLAY_SEARCH_BUTTON_NEVER,
45   DISPLAY_SEARCH_BUTTON_FOR_STR,         // STR = Search Term Replacement
46   DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP,  // IIP = Input In Progress
47   DISPLAY_SEARCH_BUTTON_ALWAYS,
48   DISPLAY_SEARCH_BUTTON_NUM_VALUES,
49 };
50
51 enum OriginChipPosition {
52   ORIGIN_CHIP_DISABLED,
53   ORIGIN_CHIP_LEADING_LOCATION_BAR,
54   ORIGIN_CHIP_TRAILING_LOCATION_BAR,
55   ORIGIN_CHIP_LEADING_MENU_BUTTON,
56   ORIGIN_CHIP_NUM_VALUES,
57 };
58
59 enum OriginChipV2HideTrigger {
60   ORIGIN_CHIP_V2_DISABLED,
61   ORIGIN_CHIP_V2_HIDE_ON_MOUSE_RELEASE,
62   ORIGIN_CHIP_V2_HIDE_ON_USER_INPUT,
63   ORIGIN_CHIP_V2_NUM_VALUES,
64 };
65
66 // Use this value for "start margin" to prevent the "es_sm" parameter from
67 // being used.
68 extern const int kDisableStartMargin;
69
70 // Returns whether the Instant Extended API is enabled.
71 bool IsInstantExtendedAPIEnabled();
72
73 // Returns whether the suggest is enabled for the given |profile|.
74 bool IsSuggestPrefEnabled(Profile* profile);
75
76 // Returns the value to pass to the &espv CGI parameter when loading the
77 // embedded search page from the user's default search provider. Returns 0 if
78 // the Instant Extended API is not enabled.
79 uint64 EmbeddedSearchPageVersion();
80
81 // Returns a string indicating whether InstantExtended is enabled, suitable
82 // for adding as a query string param to the homepage or search requests.
83 // Returns an empty string otherwise.
84 //
85 // |for_search| should be set to true for search requests, in which case this
86 // returns a non-empty string only if query extraction is enabled.
87 std::string InstantExtendedEnabledParam(bool for_search);
88
89 // Returns a string that will cause the search results page to update
90 // incrementally. Currently, Instant Extended passes a different param to
91 // search results pages that also has this effect, so by default this function
92 // returns the empty string when Instant Extended is enabled. However, when
93 // doing instant search result prerendering, we still need to pass this param,
94 // as Instant Extended does not cause incremental updates by default for the
95 // prerender page. Callers should set |for_prerender| in this case to force
96 // the returned string to be non-empty.
97 std::string ForceInstantResultsParam(bool for_prerender);
98
99 // Returns whether query extraction is enabled.
100 bool IsQueryExtractionEnabled();
101
102 // Extracts and returns search terms from |url|. Does not consider
103 // IsQueryExtractionEnabled() and Instant support state of the page and does
104 // not check for a privileged process, so most callers should use
105 // GetSearchTerms() below instead.
106 base::string16 ExtractSearchTermsFromURL(Profile* profile, const GURL& url);
107
108 // Returns true if it is okay to extract search terms from |url|. |url| must
109 // have a secure scheme and must contain the search terms replacement key for
110 // the default search provider.
111 bool IsQueryExtractionAllowedForURL(Profile* profile, const GURL& url);
112
113 // Returns the search terms attached to a specific NavigationEntry, or empty
114 // string otherwise. Does not consider IsQueryExtractionEnabled() and does not
115 // check Instant support, so most callers should use GetSearchTerms() below
116 // instead.
117 base::string16 GetSearchTermsFromNavigationEntry(
118     const content::NavigationEntry* entry);
119
120 // Returns search terms if this WebContents is a search results page. It looks
121 // in the visible NavigationEntry first, to see if search terms have already
122 // been extracted. Failing that, it tries to extract search terms from the URL.
123 //
124 // Returns a blank string if search terms were not found, or if search terms
125 // extraction is disabled for this WebContents or profile, or if |contents|
126 // does not support Instant.
127 base::string16 GetSearchTerms(const content::WebContents* contents);
128
129 // Returns true if |url| should be rendered in the Instant renderer process.
130 bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile);
131
132 // Returns true if the Instant |url| should use process per site.
133 bool ShouldUseProcessPerSiteForInstantURL(const GURL& url, Profile* profile);
134
135 // Returns true if |url| corresponds to a New Tab page (it can be either an
136 // Instant Extended NTP or a non-extended NTP).
137 bool IsNTPURL(const GURL& url, Profile* profile);
138
139 // Returns true if the visible entry of |contents| is a New Tab Page rendered
140 // by Instant. A page that matches the search or Instant URL of the default
141 // search provider but does not have any search terms is considered an Instant
142 // New Tab Page.
143 bool IsInstantNTP(const content::WebContents* contents);
144
145 // Same as IsInstantNTP but uses |nav_entry| to determine the URL for the page
146 // instead of using the visible entry.
147 bool NavEntryIsInstantNTP(const content::WebContents* contents,
148                           const content::NavigationEntry* nav_entry);
149
150 // Returns the Instant URL of the default search engine. Returns an empty GURL
151 // if the engine doesn't have an Instant URL, or if it shouldn't be used (say
152 // because it doesn't satisfy the requirements for extended mode or if Instant
153 // is disabled through preferences). Callers must check that the returned URL is
154 // valid before using it. The value of |start_margin| is used for the "es_sm"
155 // parameter in the URL. |force_instant_results| forces a search page to update
156 // results incrementally even if that is otherwise disabled by google.com
157 // preferences.
158 // NOTE: This method expands the default search engine's instant_url template,
159 // so it shouldn't be called from SearchTermsData or other such code that would
160 // lead to an infinite recursion.
161 GURL GetInstantURL(Profile* profile,
162                    int start_margin,
163                    bool force_instant_results);
164
165 // Returns URLs associated with the default search engine for |profile|.
166 std::vector<GURL> GetSearchURLs(Profile* profile);
167
168 // Returns the default search engine base page URL to prefetch search results.
169 // Returns an empty URL if 'prefetch_results' flag is set to false in field
170 // trials.
171 GURL GetSearchResultPrefetchBaseURL(Profile* profile);
172
173 // Returns true if 'prefetch_results' flag is set to true in field trials to
174 // prefetch high-confidence search suggestions.
175 bool ShouldPrefetchSearchResults();
176
177 // Returns true if 'reuse_instant_search_base_page' flag is set to true in field
178 // trials to reuse the prerendered page to commit any search query.
179 bool ShouldReuseInstantSearchBasePage();
180
181 // Returns the Local Instant URL of the New Tab Page.
182 // TODO(kmadhusu): Remove this function and update the call sites.
183 GURL GetLocalInstantURL(Profile* profile);
184
185 // Returns true if 'hide_verbatim' flag is enabled in field trials
186 // to hide the top match in the native suggestions dropdown if it is a verbatim
187 // match.  See comments on ShouldHideTopMatch in autocomplete_result.h.
188 bool ShouldHideTopVerbatimMatch();
189
190 // Returns when we should show a search button in the omnibox.  This may be any
191 // of several values, some of which depend on whether the underlying state of
192 // the page would normally be to perform search term replacement; see also
193 // ToolbarModel::WouldPerformSearchTermReplacement().
194 DisplaySearchButtonConditions GetDisplaySearchButtonConditions();
195
196 // Returns true if the origin chip should be shown in the toolbar. This
197 // also includes the related changes to the omnibox. Always returns false if
198 // ShouldDisplayOriginChipV2() returns true.
199 bool ShouldDisplayOriginChip();
200
201 // Returns a value indicating where the origin chip should be positioned on the
202 // toolbar.
203 OriginChipPosition GetOriginChipPosition();
204
205 // Returns true if version 2 of the origin chip should be shown.  This version
206 // places the origin chip inside the location bar instead of the toolbar and
207 // adds show/hide behavior and animations to make the relationship between the
208 // chip and the text in the Omnibox clearer.
209 bool ShouldDisplayOriginChipV2();
210
211 // Returns a value indicating what event should trigger hiding the origin chip
212 // in the location bar.
213 OriginChipV2HideTrigger GetOriginChipV2HideTrigger();
214
215 // Returns true if the local new tab page should show a Google logo and search
216 // box for users whose default search provider is Google, or false if not.
217 bool ShouldShowGoogleLocalNTP();
218
219 // Transforms the input |url| into its "effective URL". The returned URL
220 // facilitates grouping process-per-site. The |url| is transformed, for
221 // example, from
222 //
223 //   https://www.google.com/search?espv=1&q=tractors
224 //
225 // to the privileged URL
226 //
227 //   chrome-search://www.google.com/search?espv=1&q=tractors
228 //
229 // Notice the scheme change.
230 //
231 // If the input is already a privileged URL then that same URL is returned.
232 //
233 // If |url| is that of the online NTP, its host is replaced with "online-ntp".
234 // This forces the NTP and search results pages to have different SiteIntances,
235 // and hence different processes.
236 GURL GetEffectiveURLForInstant(const GURL& url, Profile* profile);
237
238 // Rewrites |url| if
239 //   1. |url| is kChromeUINewTabURL,
240 //   2. InstantExtended is enabled, and
241 //   3. The --instant-new-tab-url switch is set to a valid URL.
242 // |url| is rewritten to the value of --instant-new-tab-url.
243 bool HandleNewTabURLRewrite(GURL* url,
244                             content::BrowserContext* browser_context);
245 // Reverses the operation from HandleNewTabURLRewrite.
246 bool HandleNewTabURLReverseRewrite(GURL* url,
247                                    content::BrowserContext* browser_context);
248
249 // Sets the Instant support |state| in the navigation |entry|.
250 void SetInstantSupportStateInNavigationEntry(InstantSupportState state,
251                                              content::NavigationEntry* entry);
252
253 // Returns the Instant support state attached to the NavigationEntry, or
254 // INSTANT_SUPPORT_UNKNOWN otherwise.
255 InstantSupportState GetInstantSupportStateFromNavigationEntry(
256     const content::NavigationEntry& entry);
257
258 // Returns true if the field trial flag is enabled to prefetch results on SRP.
259 bool ShouldPrefetchSearchResultsOnSRP();
260
261 // -----------------------------------------------------
262 // The following APIs are exposed for use in tests only.
263 // -----------------------------------------------------
264
265 // Forces query in the omnibox to be on for tests.
266 void EnableQueryExtractionForTesting();
267
268 // Type for a collection of experiment configuration parameters.
269 typedef std::vector<std::pair<std::string, std::string> > FieldTrialFlags;
270
271 // Finds the active field trial group name and parses out the configuration
272 // flags. On success, |flags| will be filled with the field trial flags. |flags|
273 // must not be NULL. Returns true iff the active field trial is successfully
274 // parsed and not disabled.
275 // Note that |flags| may be successfully populated in some cases when false is
276 // returned - in these cases it should not be used.
277 // Exposed for testing only.
278 bool GetFieldTrialInfo(FieldTrialFlags* flags);
279
280 // Given a FieldTrialFlags object, returns the string value of the provided
281 // flag.
282 // Exposed for testing only.
283 std::string GetStringValueForFlagWithDefault(const std::string& flag,
284                                              const std::string& default_value,
285                                              const FieldTrialFlags& flags);
286
287 // Given a FieldTrialFlags object, returns the uint64 value of the provided
288 // flag.
289 // Exposed for testing only.
290 uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag,
291                                         uint64 default_value,
292                                         const FieldTrialFlags& flags);
293
294 // Given a FieldTrialFlags object, returns the bool value of the provided flag.
295 // Exposed for testing only.
296 bool GetBoolValueForFlagWithDefault(const std::string& flag,
297                                     bool default_value,
298                                     const FieldTrialFlags& flags);
299
300 // Returns the Cacheable New Tab Page URL for the given |profile|.
301 GURL GetNewTabPageURL(Profile* profile);
302
303 }  // namespace chrome
304
305 #endif  // CHROME_BROWSER_SEARCH_SEARCH_H_