Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / autocomplete_input.h
blob0d2d35bf85751e245e131fde55c787492a30aed7
1 // Copyright 2014 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.
5 #ifndef COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_INPUT_H_
6 #define COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_INPUT_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/strings/string16.h"
13 #include "components/metrics/proto/omnibox_event.pb.h"
14 #include "components/metrics/proto/omnibox_input_type.pb.h"
15 #include "url/gurl.h"
16 #include "url/third_party/mozilla/url_parse.h"
18 class AutocompleteSchemeClassifier;
20 // The user input for an autocomplete query. Allows copying.
21 class AutocompleteInput {
22 public:
23 AutocompleteInput();
24 // |text| and |cursor_position| represent the input query and location of
25 // the cursor with the query respectively. |cursor_position| may be set to
26 // base::string16::npos if the input |text| doesn't come directly from the
27 // user's typing.
29 // |desired_tld| is the user's desired TLD, if one is not already present in
30 // the text to autocomplete. When this is non-empty, it also implies that
31 // "www." should be prepended to the domain where possible. The |desired_tld|
32 // should not contain a leading '.' (use "com" instead of ".com").
34 // If |current_url| is set to a valid search result page URL, providers can
35 // use it to perform query refinement. For example, if it is set to an image
36 // search result page, the search provider may generate an image search URL.
37 // Query refinement is only used by mobile ports, so only these set
38 // |current_url| to a non-empty string.
40 // |current_page_classification| represents the type of page the user is
41 // viewing and manner in which the user is accessing the omnibox; it's
42 // more than simply the URL. It includes, for example, whether the page
43 // is a search result page doing search term replacement or not.
45 // |prevent_inline_autocomplete| is true if the generated result set should
46 // not require inline autocomplete for the default match. This is difficult
47 // to explain in the abstract; the practical use case is that after the user
48 // deletes text in the edit, the HistoryURLProvider should make sure not to
49 // promote a match requiring inline autocomplete too highly.
51 // |prefer_keyword| should be true when the keyword UI is onscreen; this will
52 // bias the autocomplete result set toward the keyword provider when the input
53 // string is a bare keyword.
55 // |allow_exact_keyword_match| should be false when triggering keyword mode on
56 // the input string would be surprising or wrong, e.g. when highlighting text
57 // in a page and telling the browser to search for it or navigate to it. This
58 // parameter only applies to substituting keywords.
60 // If |want_asynchronous_matches| is false the controller asks the providers
61 // to only return matches which are synchronously available, which should mean
62 // that all providers will be done immediately.
64 // |from_omnibox_focus| is true when input is created as a result of the
65 // omnibox being focused, instead of due to user input changes. Most
66 // providers should not provide matches in this case. Providers which want to
67 // display matches on focus can use this flag to know when they can do so.
69 // |scheme_classifier| is passed to Parse() to help determine the type of
70 // input this is; see comments there.
71 AutocompleteInput(const base::string16& text,
72 size_t cursor_position,
73 const std::string& desired_tld,
74 const GURL& current_url,
75 metrics::OmniboxEventProto::PageClassification
76 current_page_classification,
77 bool prevent_inline_autocomplete,
78 bool prefer_keyword,
79 bool allow_exact_keyword_match,
80 bool want_asynchronous_matches,
81 bool from_omnibox_focus,
82 const AutocompleteSchemeClassifier& scheme_classifier);
83 ~AutocompleteInput();
85 // If type is |FORCED_QUERY| and |text| starts with '?', it is removed.
86 // Returns number of leading characters removed.
87 static size_t RemoveForcedQueryStringIfNecessary(
88 metrics::OmniboxInputType::Type type,
89 base::string16* text);
91 // Converts |type| to a string representation. Used in logging.
92 static std::string TypeToString(metrics::OmniboxInputType::Type type);
94 // Parses |text| (including an optional |desired_tld|) and returns the type of
95 // input this will be interpreted as. |scheme_classifier| is used to check
96 // the scheme in |text| is known and registered in the current environment.
97 // The components of the input are stored in the output parameter |parts|, if
98 // it is non-NULL. The scheme is stored in |scheme| if it is non-NULL. The
99 // canonicalized URL is stored in |canonicalized_url|; however, this URL is
100 // not guaranteed to be valid, especially if the parsed type is, e.g., QUERY.
101 static metrics::OmniboxInputType::Type Parse(
102 const base::string16& text,
103 const std::string& desired_tld,
104 const AutocompleteSchemeClassifier& scheme_classifier,
105 url::Parsed* parts,
106 base::string16* scheme,
107 GURL* canonicalized_url);
109 // Parses |text| and fill |scheme| and |host| by the positions of them.
110 // The results are almost as same as the result of Parse(), but if the scheme
111 // is view-source, this function returns the positions of scheme and host
112 // in the URL qualified by "view-source:" prefix.
113 static void ParseForEmphasizeComponents(
114 const base::string16& text,
115 const AutocompleteSchemeClassifier& scheme_classifier,
116 url::Component* scheme,
117 url::Component* host);
119 // Code that wants to format URLs with a format flag including
120 // net::kFormatUrlOmitTrailingSlashOnBareHostname risk changing the meaning if
121 // the result is then parsed as AutocompleteInput. Such code can call this
122 // function with the URL and its formatted string, and it will return a
123 // formatted string with the same meaning as the original URL (i.e. it will
124 // re-append a slash if necessary). Because this uses Parse() under the hood
125 // to determine the meaning of the different strings, callers need to supply a
126 // |scheme_classifier| to pass to Parse().
127 static base::string16 FormattedStringWithEquivalentMeaning(
128 const GURL& url,
129 const base::string16& formatted_url,
130 const AutocompleteSchemeClassifier& scheme_classifier);
132 // Returns the number of non-empty components in |parts| besides the host.
133 static int NumNonHostComponents(const url::Parsed& parts);
135 // Returns whether |text| begins "http:" or "view-source:http:".
136 static bool HasHTTPScheme(const base::string16& text);
138 // User-provided text to be completed.
139 const base::string16& text() const { return text_; }
141 // Returns 0-based cursor position within |text_| or base::string16::npos if
142 // not used.
143 size_t cursor_position() const { return cursor_position_; }
145 // Use of this setter is risky, since no other internal state is updated
146 // besides |text_|, |cursor_position_| and |parts_|. Only callers who know
147 // that they're not changing the type/scheme/etc. should use this.
148 void UpdateText(const base::string16& text,
149 size_t cursor_position,
150 const url::Parsed& parts);
152 // The current URL, or an invalid GURL if query refinement is not desired.
153 const GURL& current_url() const { return current_url_; }
155 // The type of page that is currently behind displayed and how it is
156 // displayed (e.g., with search term replacement or without).
157 metrics::OmniboxEventProto::PageClassification current_page_classification()
158 const {
159 return current_page_classification_;
162 // The type of input supplied.
163 metrics::OmniboxInputType::Type type() const { return type_; }
165 // Returns parsed URL components.
166 const url::Parsed& parts() const { return parts_; }
168 // The scheme parsed from the provided text; only meaningful when type_ is
169 // URL.
170 const base::string16& scheme() const { return scheme_; }
172 // The input as an URL to navigate to, if possible.
173 const GURL& canonicalized_url() const { return canonicalized_url_; }
175 // Returns whether inline autocompletion should be prevented.
176 bool prevent_inline_autocomplete() const {
177 return prevent_inline_autocomplete_;
180 // Returns whether, given an input string consisting solely of a substituting
181 // keyword, we should score it like a non-substituting keyword.
182 bool prefer_keyword() const { return prefer_keyword_; }
184 // Returns whether this input is allowed to be treated as an exact
185 // keyword match. If not, the default result is guaranteed not to be a
186 // keyword search, even if the input is "<keyword> <search string>".
187 bool allow_exact_keyword_match() const { return allow_exact_keyword_match_; }
189 // Returns whether providers should be allowed to make asynchronous requests
190 // when processing this input.
191 bool want_asynchronous_matches() const { return want_asynchronous_matches_; }
193 // Returns whether this input query was triggered due to the omnibox being
194 // focused.
195 bool from_omnibox_focus() const { return from_omnibox_focus_; }
197 // Returns the terms in |text_| that start with http:// or https:// plus
198 // at least one more character, stored without the scheme. Used in
199 // duplicate elimination to detect whether, for a given URL, the user may
200 // have started typing that URL with an explicit scheme; see comments on
201 // AutocompleteMatch::GURLToStrippedGURL().
202 const std::vector<base::string16>& terms_prefixed_by_http_or_https() const {
203 return terms_prefixed_by_http_or_https_;
206 // Resets all internal variables to the null-constructed state.
207 void Clear();
209 private:
210 friend class AutocompleteProviderTest;
212 // NOTE: Whenever adding a new field here, please make sure to update Clear()
213 // method.
214 base::string16 text_;
215 size_t cursor_position_;
216 GURL current_url_;
217 metrics::OmniboxEventProto::PageClassification current_page_classification_;
218 metrics::OmniboxInputType::Type type_;
219 url::Parsed parts_;
220 base::string16 scheme_;
221 GURL canonicalized_url_;
222 bool prevent_inline_autocomplete_;
223 bool prefer_keyword_;
224 bool allow_exact_keyword_match_;
225 bool want_asynchronous_matches_;
226 bool from_omnibox_focus_;
227 std::vector<base::string16> terms_prefixed_by_http_or_https_;
230 #endif // COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_INPUT_H_