OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/autocomplete_input.h" | 5 #include "chrome/browser/autocomplete/autocomplete_input.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/external_protocol/external_protocol_handler.h" | 9 #include "chrome/browser/external_protocol/external_protocol_handler.h" |
10 #include "chrome/browser/profiles/profile_io_data.h" | 10 #include "chrome/browser/profiles/profile_io_data.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 *cursor_position -= num_leading_chars_removed; | 25 *cursor_position -= num_leading_chars_removed; |
26 else | 26 else |
27 *cursor_position = 0; | 27 *cursor_position = 0; |
28 } | 28 } |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 AutocompleteInput::AutocompleteInput() | 32 AutocompleteInput::AutocompleteInput() |
33 : cursor_position_(base::string16::npos), | 33 : cursor_position_(base::string16::npos), |
34 current_page_classification_(AutocompleteInput::INVALID_SPEC), | 34 current_page_classification_(AutocompleteInput::INVALID_SPEC), |
35 type_(INVALID), | 35 type_(metrics::OmniboxInputType::INVALID), |
36 prevent_inline_autocomplete_(false), | 36 prevent_inline_autocomplete_(false), |
37 prefer_keyword_(false), | 37 prefer_keyword_(false), |
38 allow_exact_keyword_match_(true), | 38 allow_exact_keyword_match_(true), |
39 want_asynchronous_matches_(true) { | 39 want_asynchronous_matches_(true) { |
40 } | 40 } |
41 | 41 |
42 AutocompleteInput::AutocompleteInput( | 42 AutocompleteInput::AutocompleteInput( |
43 const base::string16& text, | 43 const base::string16& text, |
44 size_t cursor_position, | 44 size_t cursor_position, |
45 const base::string16& desired_tld, | 45 const base::string16& desired_tld, |
(...skipping 16 matching lines...) Expand all Loading... | |
62 // None of the providers care about leading white space so we always trim it. | 62 // None of the providers care about leading white space so we always trim it. |
63 // Providers that care about trailing white space handle trimming themselves. | 63 // Providers that care about trailing white space handle trimming themselves. |
64 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & | 64 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & |
65 base::TRIM_LEADING) != 0) | 65 base::TRIM_LEADING) != 0) |
66 AdjustCursorPositionIfNecessary(text.length() - text_.length(), | 66 AdjustCursorPositionIfNecessary(text.length() - text_.length(), |
67 &cursor_position_); | 67 &cursor_position_); |
68 | 68 |
69 GURL canonicalized_url; | 69 GURL canonicalized_url; |
70 type_ = Parse(text_, desired_tld, &parts_, &scheme_, &canonicalized_url); | 70 type_ = Parse(text_, desired_tld, &parts_, &scheme_, &canonicalized_url); |
71 | 71 |
72 if (type_ == INVALID) | 72 if (type_ == metrics::OmniboxInputType::INVALID) |
73 return; | 73 return; |
74 | 74 |
75 if (((type_ == UNKNOWN) || (type_ == URL)) && | 75 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || |
76 (type_ == metrics::OmniboxInputType::URL)) && | |
76 canonicalized_url.is_valid() && | 77 canonicalized_url.is_valid() && |
77 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || | 78 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || |
78 canonicalized_url.SchemeIsFileSystem() || | 79 canonicalized_url.SchemeIsFileSystem() || |
79 !canonicalized_url.host().empty())) | 80 !canonicalized_url.host().empty())) |
80 canonicalized_url_ = canonicalized_url; | 81 canonicalized_url_ = canonicalized_url; |
81 | 82 |
82 size_t chars_removed = RemoveForcedQueryStringIfNecessary(type_, &text_); | 83 size_t chars_removed = RemoveForcedQueryStringIfNecessary(type_, &text_); |
83 AdjustCursorPositionIfNecessary(chars_removed, &cursor_position_); | 84 AdjustCursorPositionIfNecessary(chars_removed, &cursor_position_); |
84 if (chars_removed) { | 85 if (chars_removed) { |
85 // Remove spaces between opening question mark and first actual character. | 86 // Remove spaces between opening question mark and first actual character. |
86 base::string16 trimmed_text; | 87 base::string16 trimmed_text; |
87 if ((base::TrimWhitespace(text_, base::TRIM_LEADING, &trimmed_text) & | 88 if ((base::TrimWhitespace(text_, base::TRIM_LEADING, &trimmed_text) & |
88 base::TRIM_LEADING) != 0) { | 89 base::TRIM_LEADING) != 0) { |
89 AdjustCursorPositionIfNecessary(text_.length() - trimmed_text.length(), | 90 AdjustCursorPositionIfNecessary(text_.length() - trimmed_text.length(), |
90 &cursor_position_); | 91 &cursor_position_); |
91 text_ = trimmed_text; | 92 text_ = trimmed_text; |
92 } | 93 } |
93 } | 94 } |
94 } | 95 } |
95 | 96 |
96 AutocompleteInput::~AutocompleteInput() { | 97 AutocompleteInput::~AutocompleteInput() { |
97 } | 98 } |
98 | 99 |
99 // static | 100 // static |
100 size_t AutocompleteInput::RemoveForcedQueryStringIfNecessary( | 101 size_t AutocompleteInput::RemoveForcedQueryStringIfNecessary( |
101 Type type, | 102 AutocompleteInput::Type type, |
102 base::string16* text) { | 103 base::string16* text) { |
103 if (type != FORCED_QUERY || text->empty() || (*text)[0] != L'?') | 104 if ((type != metrics::OmniboxInputType::FORCED_QUERY) || text->empty() || |
105 (*text)[0] != L'?') | |
104 return 0; | 106 return 0; |
105 // Drop the leading '?'. | 107 // Drop the leading '?'. |
106 text->erase(0, 1); | 108 text->erase(0, 1); |
107 return 1; | 109 return 1; |
108 } | 110 } |
109 | 111 |
110 // static | 112 // static |
111 std::string AutocompleteInput::TypeToString(Type type) { | 113 std::string AutocompleteInput::TypeToString(AutocompleteInput::Type type) { |
112 switch (type) { | 114 switch (type) { |
113 case INVALID: return "invalid"; | 115 case metrics::OmniboxInputType::INVALID: return "invalid"; |
114 case UNKNOWN: return "unknown"; | 116 case metrics::OmniboxInputType::UNKNOWN: return "unknown"; |
115 case URL: return "url"; | 117 case metrics::OmniboxInputType::DEPRECATED_REQUESTED_URL: |
116 case QUERY: return "query"; | 118 return "deprecated-requested-url"; |
117 case FORCED_QUERY: return "forced-query"; | 119 case metrics::OmniboxInputType::URL: return "url"; |
118 | 120 case metrics::OmniboxInputType::QUERY: return "query"; |
119 default: | 121 case metrics::OmniboxInputType::FORCED_QUERY: return "forced-query"; |
120 NOTREACHED(); | |
121 return std::string(); | |
122 } | 122 } |
123 return ""; | |
Ilya Sherman
2014/06/06 20:29:24
nit: s/""/std::string()
Mark P
2014/06/06 21:24:51
Done.
| |
123 } | 124 } |
124 | 125 |
125 // static | 126 // static |
126 AutocompleteInput::Type AutocompleteInput::Parse( | 127 AutocompleteInput::Type AutocompleteInput::Parse( |
127 const base::string16& text, | 128 const base::string16& text, |
128 const base::string16& desired_tld, | 129 const base::string16& desired_tld, |
129 url::Parsed* parts, | 130 url::Parsed* parts, |
130 base::string16* scheme, | 131 base::string16* scheme, |
131 GURL* canonicalized_url) { | 132 GURL* canonicalized_url) { |
132 size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0); | 133 size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0); |
133 if (first_non_white == base::string16::npos) | 134 if (first_non_white == base::string16::npos) |
134 return INVALID; // All whitespace. | 135 return metrics::OmniboxInputType::INVALID; // All whitespace. |
135 | 136 |
136 if (text[first_non_white] == L'?') { | 137 if (text[first_non_white] == L'?') { |
137 // If the first non-whitespace character is a '?', we magically treat this | 138 // If the first non-whitespace character is a '?', we magically treat this |
138 // as a query. | 139 // as a query. |
139 return FORCED_QUERY; | 140 return metrics::OmniboxInputType::FORCED_QUERY; |
140 } | 141 } |
141 | 142 |
142 // Ask our parsing back-end to help us understand what the user typed. We | 143 // Ask our parsing back-end to help us understand what the user typed. We |
143 // use the URLFixerUpper here because we want to be smart about what we | 144 // use the URLFixerUpper here because we want to be smart about what we |
144 // consider a scheme. For example, we shouldn't consider www.google.com:80 | 145 // consider a scheme. For example, we shouldn't consider www.google.com:80 |
145 // to have a scheme. | 146 // to have a scheme. |
146 url::Parsed local_parts; | 147 url::Parsed local_parts; |
147 if (!parts) | 148 if (!parts) |
148 parts = &local_parts; | 149 parts = &local_parts; |
149 const base::string16 parsed_scheme(URLFixerUpper::SegmentURL(text, parts)); | 150 const base::string16 parsed_scheme(URLFixerUpper::SegmentURL(text, parts)); |
150 if (scheme) | 151 if (scheme) |
151 *scheme = parsed_scheme; | 152 *scheme = parsed_scheme; |
152 | 153 |
153 // If we can't canonicalize the user's input, the rest of the autocomplete | 154 // If we can't canonicalize the user's input, the rest of the autocomplete |
154 // system isn't going to be able to produce a navigable URL match for it. | 155 // system isn't going to be able to produce a navigable URL match for it. |
155 // So we just return QUERY immediately in these cases. | 156 // So we just return QUERY immediately in these cases. |
156 GURL placeholder_canonicalized_url; | 157 GURL placeholder_canonicalized_url; |
157 if (!canonicalized_url) | 158 if (!canonicalized_url) |
158 canonicalized_url = &placeholder_canonicalized_url; | 159 canonicalized_url = &placeholder_canonicalized_url; |
159 *canonicalized_url = URLFixerUpper::FixupURL(base::UTF16ToUTF8(text), | 160 *canonicalized_url = URLFixerUpper::FixupURL(base::UTF16ToUTF8(text), |
160 base::UTF16ToUTF8(desired_tld)); | 161 base::UTF16ToUTF8(desired_tld)); |
161 if (!canonicalized_url->is_valid()) | 162 if (!canonicalized_url->is_valid()) |
162 return QUERY; | 163 return metrics::OmniboxInputType::QUERY; |
163 | 164 |
164 if (LowerCaseEqualsASCII(parsed_scheme, url::kFileScheme)) { | 165 if (LowerCaseEqualsASCII(parsed_scheme, url::kFileScheme)) { |
165 // A user might or might not type a scheme when entering a file URL. In | 166 // A user might or might not type a scheme when entering a file URL. In |
166 // either case, |parsed_scheme| will tell us that this is a file URL, but | 167 // either case, |parsed_scheme| will tell us that this is a file URL, but |
167 // |parts->scheme| might be empty, e.g. if the user typed "C:\foo". | 168 // |parts->scheme| might be empty, e.g. if the user typed "C:\foo". |
168 return URL; | 169 return metrics::OmniboxInputType::URL; |
169 } | 170 } |
170 | 171 |
171 // If the user typed a scheme, and it's HTTP or HTTPS, we know how to parse it | 172 // If the user typed a scheme, and it's HTTP or HTTPS, we know how to parse it |
172 // well enough that we can fall through to the heuristics below. If it's | 173 // well enough that we can fall through to the heuristics below. If it's |
173 // something else, we can just determine our action based on what we do with | 174 // something else, we can just determine our action based on what we do with |
174 // any input of this scheme. In theory we could do better with some schemes | 175 // any input of this scheme. In theory we could do better with some schemes |
175 // (e.g. "ftp" or "view-source") but I'll wait to spend the effort on that | 176 // (e.g. "ftp" or "view-source") but I'll wait to spend the effort on that |
176 // until I run into some cases that really need it. | 177 // until I run into some cases that really need it. |
177 if (parts->scheme.is_nonempty() && | 178 if (parts->scheme.is_nonempty() && |
178 !LowerCaseEqualsASCII(parsed_scheme, url::kHttpScheme) && | 179 !LowerCaseEqualsASCII(parsed_scheme, url::kHttpScheme) && |
179 !LowerCaseEqualsASCII(parsed_scheme, url::kHttpsScheme)) { | 180 !LowerCaseEqualsASCII(parsed_scheme, url::kHttpsScheme)) { |
180 // See if we know how to handle the URL internally. There are some schemes | 181 // See if we know how to handle the URL internally. There are some schemes |
181 // that we convert to other things before they reach the renderer or else | 182 // that we convert to other things before they reach the renderer or else |
182 // the renderer handles internally without reaching the net::URLRequest | 183 // the renderer handles internally without reaching the net::URLRequest |
183 // logic. They thus won't be listed as "handled protocols", but we should | 184 // logic. They thus won't be listed as "handled protocols", but we should |
184 // still claim to handle them. | 185 // still claim to handle them. |
185 if (ProfileIOData::IsHandledProtocol(base::UTF16ToASCII(parsed_scheme)) || | 186 if (ProfileIOData::IsHandledProtocol(base::UTF16ToASCII(parsed_scheme)) || |
186 LowerCaseEqualsASCII(parsed_scheme, content::kViewSourceScheme) || | 187 LowerCaseEqualsASCII(parsed_scheme, content::kViewSourceScheme) || |
187 LowerCaseEqualsASCII(parsed_scheme, url::kJavaScriptScheme) || | 188 LowerCaseEqualsASCII(parsed_scheme, url::kJavaScriptScheme) || |
188 LowerCaseEqualsASCII(parsed_scheme, url::kDataScheme)) | 189 LowerCaseEqualsASCII(parsed_scheme, url::kDataScheme)) |
189 return URL; | 190 return metrics::OmniboxInputType::URL; |
190 | 191 |
191 // Not an internal protocol. Check and see if the user has explicitly | 192 // Not an internal protocol. Check and see if the user has explicitly |
192 // opened this scheme as a URL before, or if the "scheme" is actually a | 193 // opened this scheme as a URL before, or if the "scheme" is actually a |
193 // username. We need to do this after the check above because some | 194 // username. We need to do this after the check above because some |
194 // handlable schemes (e.g. "javascript") may be treated as "blocked" by the | 195 // handlable schemes (e.g. "javascript") may be treated as "blocked" by the |
195 // external protocol handler because we don't want pages to open them, but | 196 // external protocol handler because we don't want pages to open them, but |
196 // users still can. | 197 // users still can. |
197 // Note that the protocol handler needs to be informed that omnibox input | 198 // Note that the protocol handler needs to be informed that omnibox input |
198 // should always be considered "user gesture-triggered", lest it always | 199 // should always be considered "user gesture-triggered", lest it always |
199 // return BLOCK. | 200 // return BLOCK. |
200 ExternalProtocolHandler::BlockState block_state = | 201 ExternalProtocolHandler::BlockState block_state = |
201 ExternalProtocolHandler::GetBlockState( | 202 ExternalProtocolHandler::GetBlockState( |
202 base::UTF16ToUTF8(parsed_scheme), true); | 203 base::UTF16ToUTF8(parsed_scheme), true); |
203 switch (block_state) { | 204 switch (block_state) { |
204 case ExternalProtocolHandler::DONT_BLOCK: | 205 case ExternalProtocolHandler::DONT_BLOCK: |
205 return URL; | 206 return metrics::OmniboxInputType::URL; |
206 | 207 |
207 case ExternalProtocolHandler::BLOCK: | 208 case ExternalProtocolHandler::BLOCK: |
208 // If we don't want the user to open the URL, don't let it be navigated | 209 // If we don't want the user to open the URL, don't let it be navigated |
209 // to at all. | 210 // to at all. |
210 return QUERY; | 211 return metrics::OmniboxInputType::QUERY; |
211 | 212 |
212 default: { | 213 default: { |
213 // We don't know about this scheme. It might be that the user typed a | 214 // We don't know about this scheme. It might be that the user typed a |
214 // URL of the form "username:password@foo.com". | 215 // URL of the form "username:password@foo.com". |
215 const base::string16 http_scheme_prefix = | 216 const base::string16 http_scheme_prefix = |
216 base::ASCIIToUTF16(std::string(url::kHttpScheme) + | 217 base::ASCIIToUTF16(std::string(url::kHttpScheme) + |
217 content::kStandardSchemeSeparator); | 218 content::kStandardSchemeSeparator); |
218 url::Parsed http_parts; | 219 url::Parsed http_parts; |
219 base::string16 http_scheme; | 220 base::string16 http_scheme; |
220 GURL http_canonicalized_url; | 221 GURL http_canonicalized_url; |
221 Type http_type = Parse(http_scheme_prefix + text, desired_tld, | 222 AutocompleteInput::Type http_type = |
222 &http_parts, &http_scheme, | 223 Parse(http_scheme_prefix + text, desired_tld, &http_parts, |
223 &http_canonicalized_url); | 224 &http_scheme, &http_canonicalized_url); |
224 DCHECK_EQ(std::string(url::kHttpScheme), | 225 DCHECK_EQ(std::string(url::kHttpScheme), |
225 base::UTF16ToUTF8(http_scheme)); | 226 base::UTF16ToUTF8(http_scheme)); |
226 | 227 |
227 if ((http_type == URL) && http_parts.username.is_nonempty() && | 228 if ((http_type == metrics::OmniboxInputType::URL) && |
229 http_parts.username.is_nonempty() && | |
228 http_parts.password.is_nonempty()) { | 230 http_parts.password.is_nonempty()) { |
229 // Manually re-jigger the parsed parts to match |text| (without the | 231 // Manually re-jigger the parsed parts to match |text| (without the |
230 // http scheme added). | 232 // http scheme added). |
231 http_parts.scheme.reset(); | 233 http_parts.scheme.reset(); |
232 url::Component* components[] = { | 234 url::Component* components[] = { |
233 &http_parts.username, | 235 &http_parts.username, |
234 &http_parts.password, | 236 &http_parts.password, |
235 &http_parts.host, | 237 &http_parts.host, |
236 &http_parts.port, | 238 &http_parts.port, |
237 &http_parts.path, | 239 &http_parts.path, |
238 &http_parts.query, | 240 &http_parts.query, |
239 &http_parts.ref, | 241 &http_parts.ref, |
240 }; | 242 }; |
241 for (size_t i = 0; i < arraysize(components); ++i) { | 243 for (size_t i = 0; i < arraysize(components); ++i) { |
242 URLFixerUpper::OffsetComponent( | 244 URLFixerUpper::OffsetComponent( |
243 -static_cast<int>(http_scheme_prefix.length()), components[i]); | 245 -static_cast<int>(http_scheme_prefix.length()), components[i]); |
244 } | 246 } |
245 | 247 |
246 *parts = http_parts; | 248 *parts = http_parts; |
247 if (scheme) | 249 if (scheme) |
248 scheme->clear(); | 250 scheme->clear(); |
249 *canonicalized_url = http_canonicalized_url; | 251 *canonicalized_url = http_canonicalized_url; |
250 | 252 |
251 return URL; | 253 return metrics::OmniboxInputType::URL; |
252 } | 254 } |
253 | 255 |
254 // We don't know about this scheme and it doesn't look like the user | 256 // We don't know about this scheme and it doesn't look like the user |
255 // typed a username and password. It's likely to be a search operator | 257 // typed a username and password. It's likely to be a search operator |
256 // like "site:" or "link:". We classify it as UNKNOWN so the user has | 258 // like "site:" or "link:". We classify it as UNKNOWN so the user has |
257 // the option of treating it as a URL if we're wrong. | 259 // the option of treating it as a URL if we're wrong. |
258 // Note that SegmentURL() is smart so we aren't tricked by "c:\foo" or | 260 // Note that SegmentURL() is smart so we aren't tricked by "c:\foo" or |
259 // "www.example.com:81" in this case. | 261 // "www.example.com:81" in this case. |
260 return UNKNOWN; | 262 return metrics::OmniboxInputType::UNKNOWN; |
261 } | 263 } |
262 } | 264 } |
263 } | 265 } |
264 | 266 |
265 // Either the user didn't type a scheme, in which case we need to distinguish | 267 // Either the user didn't type a scheme, in which case we need to distinguish |
266 // between an HTTP URL and a query, or the scheme is HTTP or HTTPS, in which | 268 // between an HTTP URL and a query, or the scheme is HTTP or HTTPS, in which |
267 // case we should reject invalid formulations. | 269 // case we should reject invalid formulations. |
268 | 270 |
269 // If we have an empty host it can't be a valid HTTP[S] URL. (This should | 271 // If we have an empty host it can't be a valid HTTP[S] URL. (This should |
270 // only trigger for input that begins with a colon, which GURL will parse as a | 272 // only trigger for input that begins with a colon, which GURL will parse as a |
271 // valid, non-standard URL; for standard URLs, an empty host would have | 273 // valid, non-standard URL; for standard URLs, an empty host would have |
272 // resulted in an invalid |canonicalized_url| above.) | 274 // resulted in an invalid |canonicalized_url| above.) |
273 if (!parts->host.is_nonempty()) | 275 if (!parts->host.is_nonempty()) |
274 return QUERY; | 276 return metrics::OmniboxInputType::QUERY; |
275 | 277 |
276 // Sanity-check: GURL should have failed to canonicalize this URL if it had an | 278 // Sanity-check: GURL should have failed to canonicalize this URL if it had an |
277 // invalid port. | 279 // invalid port. |
278 DCHECK_NE(url::PORT_INVALID, url::ParsePort(text.c_str(), parts->port)); | 280 DCHECK_NE(url::PORT_INVALID, url::ParsePort(text.c_str(), parts->port)); |
279 | 281 |
280 // Likewise, the RCDS can reject certain obviously-invalid hosts. (We also | 282 // Likewise, the RCDS can reject certain obviously-invalid hosts. (We also |
281 // use the registry length later below.) | 283 // use the registry length later below.) |
282 const base::string16 host(text.substr(parts->host.begin, parts->host.len)); | 284 const base::string16 host(text.substr(parts->host.begin, parts->host.len)); |
283 const size_t registry_length = | 285 const size_t registry_length = |
284 net::registry_controlled_domains::GetRegistryLength( | 286 net::registry_controlled_domains::GetRegistryLength( |
285 base::UTF16ToUTF8(host), | 287 base::UTF16ToUTF8(host), |
286 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | 288 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
287 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | 289 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
288 if (registry_length == std::string::npos) { | 290 if (registry_length == std::string::npos) { |
289 // Try to append the desired_tld. | 291 // Try to append the desired_tld. |
290 if (!desired_tld.empty()) { | 292 if (!desired_tld.empty()) { |
291 base::string16 host_with_tld(host); | 293 base::string16 host_with_tld(host); |
292 if (host[host.length() - 1] != '.') | 294 if (host[host.length() - 1] != '.') |
293 host_with_tld += '.'; | 295 host_with_tld += '.'; |
294 host_with_tld += desired_tld; | 296 host_with_tld += desired_tld; |
295 const size_t tld_length = | 297 const size_t tld_length = |
296 net::registry_controlled_domains::GetRegistryLength( | 298 net::registry_controlled_domains::GetRegistryLength( |
297 base::UTF16ToUTF8(host_with_tld), | 299 base::UTF16ToUTF8(host_with_tld), |
298 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | 300 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
299 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | 301 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
300 if (tld_length != std::string::npos) | 302 if (tld_length != std::string::npos) { |
301 return URL; // Something like "99999999999" that looks like a bad IP | 303 // Something like "99999999999" that looks like a bad IP |
302 // address, but becomes valid on attaching a TLD. | 304 // address, but becomes valid on attaching a TLD. |
305 return metrics::OmniboxInputType::URL; | |
306 } | |
303 } | 307 } |
304 return QUERY; // Could be a broken IP address, etc. | 308 // Could be a broken IP address, etc. |
309 return metrics::OmniboxInputType::QUERY; | |
305 } | 310 } |
306 | 311 |
307 | 312 |
308 // See if the hostname is valid. While IE and GURL allow hostnames to contain | 313 // See if the hostname is valid. While IE and GURL allow hostnames to contain |
309 // many other characters (perhaps for weird intranet machines), it's extremely | 314 // many other characters (perhaps for weird intranet machines), it's extremely |
310 // unlikely that a user would be trying to type those in for anything other | 315 // unlikely that a user would be trying to type those in for anything other |
311 // than a search query. | 316 // than a search query. |
312 url::CanonHostInfo host_info; | 317 url::CanonHostInfo host_info; |
313 const std::string canonicalized_host(net::CanonicalizeHost( | 318 const std::string canonicalized_host(net::CanonicalizeHost( |
314 base::UTF16ToUTF8(host), &host_info)); | 319 base::UTF16ToUTF8(host), &host_info)); |
(...skipping 13 matching lines...) Expand all Loading... | |
328 // "toys at amazon.com" will be treated as a search. | 333 // "toys at amazon.com" will be treated as a search. |
329 // * The user is typing some garbage string. Return QUERY. | 334 // * The user is typing some garbage string. Return QUERY. |
330 // | 335 // |
331 // Thus we fall down in the following cases: | 336 // Thus we fall down in the following cases: |
332 // * Trying to navigate to a hostname with spaces | 337 // * Trying to navigate to a hostname with spaces |
333 // * Trying to navigate to a hostname with invalid characters and an unknown | 338 // * Trying to navigate to a hostname with invalid characters and an unknown |
334 // TLD | 339 // TLD |
335 // These are rare, though probably possible in intranets. | 340 // These are rare, though probably possible in intranets. |
336 return (parts->scheme.is_nonempty() || | 341 return (parts->scheme.is_nonempty() || |
337 ((registry_length != 0) && | 342 ((registry_length != 0) && |
338 (host.find(' ') == base::string16::npos))) ? UNKNOWN : QUERY; | 343 (host.find(' ') == base::string16::npos))) ? |
344 metrics::OmniboxInputType::UNKNOWN : metrics::OmniboxInputType::QUERY; | |
339 } | 345 } |
340 | 346 |
341 // Now that we've ruled out all schemes other than http or https and done a | 347 // Now that we've ruled out all schemes other than http or https and done a |
342 // little more sanity checking, the presence of a scheme means this is likely | 348 // little more sanity checking, the presence of a scheme means this is likely |
343 // a URL. | 349 // a URL. |
344 if (parts->scheme.is_nonempty()) | 350 if (parts->scheme.is_nonempty()) |
345 return URL; | 351 return metrics::OmniboxInputType::URL; |
346 | 352 |
347 // See if the host is an IP address. | 353 // See if the host is an IP address. |
348 if (host_info.family == url::CanonHostInfo::IPV6) | 354 if (host_info.family == url::CanonHostInfo::IPV6) |
349 return URL; | 355 return metrics::OmniboxInputType::URL; |
350 // If the user originally typed a host that looks like an IP address (a | 356 // If the user originally typed a host that looks like an IP address (a |
351 // dotted quad), they probably want to open it. If the original input was | 357 // dotted quad), they probably want to open it. If the original input was |
352 // something else (like a single number), they probably wanted to search for | 358 // something else (like a single number), they probably wanted to search for |
353 // it, unless they explicitly typed a scheme. This is true even if the URL | 359 // it, unless they explicitly typed a scheme. This is true even if the URL |
354 // appears to have a path: "1.2/45" is more likely a search (for the answer | 360 // appears to have a path: "1.2/45" is more likely a search (for the answer |
355 // to a math problem) than a URL. However, if there are more non-host | 361 // to a math problem) than a URL. However, if there are more non-host |
356 // components, then maybe this really was intended to be a navigation. For | 362 // components, then maybe this really was intended to be a navigation. For |
357 // this reason we only check the dotted-quad case here, and save the "other | 363 // this reason we only check the dotted-quad case here, and save the "other |
358 // IP addresses" case for after we check the number of non-host components | 364 // IP addresses" case for after we check the number of non-host components |
359 // below. | 365 // below. |
360 if ((host_info.family == url::CanonHostInfo::IPV4) && | 366 if ((host_info.family == url::CanonHostInfo::IPV4) && |
361 (host_info.num_ipv4_components == 4)) | 367 (host_info.num_ipv4_components == 4)) |
362 return URL; | 368 return metrics::OmniboxInputType::URL; |
363 | 369 |
364 // Presence of a password means this is likely a URL. Note that unless the | 370 // Presence of a password means this is likely a URL. Note that unless the |
365 // user has typed an explicit "http://" or similar, we'll probably think that | 371 // user has typed an explicit "http://" or similar, we'll probably think that |
366 // the username is some unknown scheme, and bail out in the scheme-handling | 372 // the username is some unknown scheme, and bail out in the scheme-handling |
367 // code above. | 373 // code above. |
368 if (parts->password.is_nonempty()) | 374 if (parts->password.is_nonempty()) |
369 return URL; | 375 return metrics::OmniboxInputType::URL; |
370 | 376 |
371 // Trailing slashes force the input to be treated as a URL. | 377 // Trailing slashes force the input to be treated as a URL. |
372 if (parts->path.is_nonempty()) { | 378 if (parts->path.is_nonempty()) { |
373 char c = text[parts->path.end() - 1]; | 379 char c = text[parts->path.end() - 1]; |
374 if ((c == '\\') || (c == '/')) | 380 if ((c == '\\') || (c == '/')) |
375 return URL; | 381 return metrics::OmniboxInputType::URL; |
376 } | 382 } |
377 | 383 |
378 // If there is more than one recognized non-host component, this is likely to | 384 // If there is more than one recognized non-host component, this is likely to |
379 // be a URL, even if the TLD is unknown (in which case this is likely an | 385 // be a URL, even if the TLD is unknown (in which case this is likely an |
380 // intranet URL). | 386 // intranet URL). |
381 if (NumNonHostComponents(*parts) > 1) | 387 if (NumNonHostComponents(*parts) > 1) |
382 return URL; | 388 return metrics::OmniboxInputType::URL; |
383 | 389 |
384 // If the host has a known TLD or a port, it's probably a URL, with the | 390 // If the host has a known TLD or a port, it's probably a URL, with the |
385 // following exceptions: | 391 // following exceptions: |
386 // * Any "IP addresses" that make it here are more likely searches | 392 // * Any "IP addresses" that make it here are more likely searches |
387 // (see above). | 393 // (see above). |
388 // * If we reach here with a username, our input looks like "user@host[.tld]". | 394 // * If we reach here with a username, our input looks like "user@host[.tld]". |
389 // Because there is no scheme explicitly specified, we think this is more | 395 // Because there is no scheme explicitly specified, we think this is more |
390 // likely an email address than an HTTP auth attempt. Hence, we search by | 396 // likely an email address than an HTTP auth attempt. Hence, we search by |
391 // default and let users correct us on a case-by-case basis. | 397 // default and let users correct us on a case-by-case basis. |
392 // Note that we special-case "localhost" as a known hostname. | 398 // Note that we special-case "localhost" as a known hostname. |
393 if ((host_info.family != url::CanonHostInfo::IPV4) && | 399 if ((host_info.family != url::CanonHostInfo::IPV4) && |
394 ((registry_length != 0) || (host == base::ASCIIToUTF16("localhost") || | 400 ((registry_length != 0) || (host == base::ASCIIToUTF16("localhost") || |
395 parts->port.is_nonempty()))) | 401 parts->port.is_nonempty()))) { |
396 return parts->username.is_nonempty() ? UNKNOWN : URL; | 402 return parts->username.is_nonempty() ? metrics::OmniboxInputType::UNKNOWN : |
403 metrics::OmniboxInputType::URL; | |
404 } | |
397 | 405 |
398 // If we reach this point, we know there's no known TLD on the input, so if | 406 // If we reach this point, we know there's no known TLD on the input, so if |
399 // the user wishes to add a desired_tld, the fixup code will oblige; thus this | 407 // the user wishes to add a desired_tld, the fixup code will oblige; thus this |
400 // is a URL. | 408 // is a URL. |
401 if (!desired_tld.empty()) | 409 if (!desired_tld.empty()) |
402 return URL; | 410 return metrics::OmniboxInputType::URL; |
403 | 411 |
404 // No scheme, password, port, path, and no known TLD on the host. | 412 // No scheme, password, port, path, and no known TLD on the host. |
405 // This could be: | 413 // This could be: |
406 // * An "incomplete IP address"; likely a search (see above). | 414 // * An "incomplete IP address"; likely a search (see above). |
407 // * An email-like input like "user@host", where "host" has no known TLD. | 415 // * An email-like input like "user@host", where "host" has no known TLD. |
408 // It's not clear what the user means here and searching seems reasonable. | 416 // It's not clear what the user means here and searching seems reasonable. |
409 // * A single word "foo"; possibly an intranet site, but more likely a search. | 417 // * A single word "foo"; possibly an intranet site, but more likely a search. |
410 // This is ideally an UNKNOWN, and we can let the Alternate Nav URL code | 418 // This is ideally an UNKNOWN, and we can let the Alternate Nav URL code |
411 // catch our mistakes. | 419 // catch our mistakes. |
412 // * A URL with a valid TLD we don't know about yet. If e.g. a registrar adds | 420 // * A URL with a valid TLD we don't know about yet. If e.g. a registrar adds |
413 // "xxx" as a TLD, then until we add it to our data file, Chrome won't know | 421 // "xxx" as a TLD, then until we add it to our data file, Chrome won't know |
414 // "foo.xxx" is a real URL. So ideally this is a URL, but we can't really | 422 // "foo.xxx" is a real URL. So ideally this is a URL, but we can't really |
415 // distinguish this case from: | 423 // distinguish this case from: |
416 // * A "URL-like" string that's not really a URL (like | 424 // * A "URL-like" string that's not really a URL (like |
417 // "browser.tabs.closeButtons" or "java.awt.event.*"). This is ideally a | 425 // "browser.tabs.closeButtons" or "java.awt.event.*"). This is ideally a |
418 // QUERY. Since this is indistinguishable from the case above, and this | 426 // QUERY. Since this is indistinguishable from the case above, and this |
419 // case is much more likely, claim these are UNKNOWN, which should default | 427 // case is much more likely, claim these are UNKNOWN, which should default |
420 // to the right thing and let users correct us on a case-by-case basis. | 428 // to the right thing and let users correct us on a case-by-case basis. |
421 return UNKNOWN; | 429 return metrics::OmniboxInputType::UNKNOWN; |
422 } | 430 } |
423 | 431 |
424 // static | 432 // static |
425 void AutocompleteInput::ParseForEmphasizeComponents(const base::string16& text, | 433 void AutocompleteInput::ParseForEmphasizeComponents(const base::string16& text, |
426 url::Component* scheme, | 434 url::Component* scheme, |
427 url::Component* host) { | 435 url::Component* host) { |
428 url::Parsed parts; | 436 url::Parsed parts; |
429 base::string16 scheme_str; | 437 base::string16 scheme_str; |
430 Parse(text, base::string16(), &parts, &scheme_str, NULL); | 438 Parse(text, base::string16(), &parts, &scheme_str, NULL); |
431 | 439 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
516 text_ = text; | 524 text_ = text; |
517 cursor_position_ = cursor_position; | 525 cursor_position_ = cursor_position; |
518 parts_ = parts; | 526 parts_ = parts; |
519 } | 527 } |
520 | 528 |
521 void AutocompleteInput::Clear() { | 529 void AutocompleteInput::Clear() { |
522 text_.clear(); | 530 text_.clear(); |
523 cursor_position_ = base::string16::npos; | 531 cursor_position_ = base::string16::npos; |
524 current_url_ = GURL(); | 532 current_url_ = GURL(); |
525 current_page_classification_ = AutocompleteInput::INVALID_SPEC; | 533 current_page_classification_ = AutocompleteInput::INVALID_SPEC; |
526 type_ = INVALID; | 534 type_ = metrics::OmniboxInputType::INVALID; |
527 parts_ = url::Parsed(); | 535 parts_ = url::Parsed(); |
528 scheme_.clear(); | 536 scheme_.clear(); |
529 canonicalized_url_ = GURL(); | 537 canonicalized_url_ = GURL(); |
530 prevent_inline_autocomplete_ = false; | 538 prevent_inline_autocomplete_ = false; |
531 prefer_keyword_ = false; | 539 prefer_keyword_ = false; |
532 allow_exact_keyword_match_ = false; | 540 allow_exact_keyword_match_ = false; |
533 want_asynchronous_matches_ = true; | 541 want_asynchronous_matches_ = true; |
534 } | 542 } |
OLD | NEW |