| 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" |
| 11 #include "chrome/common/net/url_fixer_upper.h" | 11 #include "chrome/common/net/url_fixer_upper.h" |
| 12 #include "content/public/common/url_constants.h" | 12 #include "content/public/common/url_constants.h" |
| 13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 15 #include "url/url_canon_ip.h" | 15 #include "url/url_canon_ip.h" |
| 16 #include "url/url_constants.h" |
| 16 #include "url/url_util.h" | 17 #include "url/url_util.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 void AdjustCursorPositionIfNecessary(size_t num_leading_chars_removed, | 21 void AdjustCursorPositionIfNecessary(size_t num_leading_chars_removed, |
| 21 size_t* cursor_position) { | 22 size_t* cursor_position) { |
| 22 if (*cursor_position == base::string16::npos) | 23 if (*cursor_position == base::string16::npos) |
| 23 return; | 24 return; |
| 24 if (num_leading_chars_removed < *cursor_position) | 25 if (num_leading_chars_removed < *cursor_position) |
| 25 *cursor_position -= num_leading_chars_removed; | 26 *cursor_position -= num_leading_chars_removed; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 return URL; | 169 return 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, content::kHttpScheme) && | 179 !LowerCaseEqualsASCII(parsed_scheme, url::kHttpScheme) && |
| 179 !LowerCaseEqualsASCII(parsed_scheme, content::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, content::kJavaScriptScheme) || | 188 LowerCaseEqualsASCII(parsed_scheme, content::kJavaScriptScheme) || |
| 188 LowerCaseEqualsASCII(parsed_scheme, content::kDataScheme)) | 189 LowerCaseEqualsASCII(parsed_scheme, content::kDataScheme)) |
| 189 return URL; | 190 return URL; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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 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(content::kHttpScheme) + | 217 base::ASCIIToUTF16(std::string(url::kHttpScheme) + |
| 217 content::kStandardSchemeSeparator); | 218 content::kStandardSchemeSeparator); |
| 218 url_parse::Parsed http_parts; | 219 url_parse::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 Type http_type = Parse(http_scheme_prefix + text, desired_tld, |
| 222 &http_parts, &http_scheme, | 223 &http_parts, &http_scheme, |
| 223 &http_canonicalized_url); | 224 &http_canonicalized_url); |
| 224 DCHECK_EQ(std::string(content::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 == URL) && http_parts.username.is_nonempty() && |
| 228 http_parts.password.is_nonempty()) { | 229 http_parts.password.is_nonempty()) { |
| 229 // Manually re-jigger the parsed parts to match |text| (without the | 230 // Manually re-jigger the parsed parts to match |text| (without the |
| 230 // http scheme added). | 231 // http scheme added). |
| 231 http_parts.scheme.reset(); | 232 http_parts.scheme.reset(); |
| 232 url_parse::Component* components[] = { | 233 url_parse::Component* components[] = { |
| 233 &http_parts.username, | 234 &http_parts.username, |
| 234 &http_parts.password, | 235 &http_parts.password, |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 return num_nonhost_components; | 500 return num_nonhost_components; |
| 500 } | 501 } |
| 501 | 502 |
| 502 // static | 503 // static |
| 503 bool AutocompleteInput::HasHTTPScheme(const base::string16& input) { | 504 bool AutocompleteInput::HasHTTPScheme(const base::string16& input) { |
| 504 std::string utf8_input(base::UTF16ToUTF8(input)); | 505 std::string utf8_input(base::UTF16ToUTF8(input)); |
| 505 url_parse::Component scheme; | 506 url_parse::Component scheme; |
| 506 if (url_util::FindAndCompareScheme(utf8_input, content::kViewSourceScheme, | 507 if (url_util::FindAndCompareScheme(utf8_input, content::kViewSourceScheme, |
| 507 &scheme)) | 508 &scheme)) |
| 508 utf8_input.erase(0, scheme.end() + 1); | 509 utf8_input.erase(0, scheme.end() + 1); |
| 509 return url_util::FindAndCompareScheme(utf8_input, content::kHttpScheme, NULL); | 510 return url_util::FindAndCompareScheme(utf8_input, url::kHttpScheme, NULL); |
| 510 } | 511 } |
| 511 | 512 |
| 512 void AutocompleteInput::UpdateText(const base::string16& text, | 513 void AutocompleteInput::UpdateText(const base::string16& text, |
| 513 size_t cursor_position, | 514 size_t cursor_position, |
| 514 const url_parse::Parsed& parts) { | 515 const url_parse::Parsed& parts) { |
| 515 DCHECK(cursor_position <= text.length() || | 516 DCHECK(cursor_position <= text.length() || |
| 516 cursor_position == base::string16::npos) | 517 cursor_position == base::string16::npos) |
| 517 << "Text: '" << text << "', cp: " << cursor_position; | 518 << "Text: '" << text << "', cp: " << cursor_position; |
| 518 text_ = text; | 519 text_ = text; |
| 519 cursor_position_ = cursor_position; | 520 cursor_position_ = cursor_position; |
| 520 parts_ = parts; | 521 parts_ = parts; |
| 521 } | 522 } |
| 522 | 523 |
| 523 void AutocompleteInput::Clear() { | 524 void AutocompleteInput::Clear() { |
| 524 text_.clear(); | 525 text_.clear(); |
| 525 cursor_position_ = base::string16::npos; | 526 cursor_position_ = base::string16::npos; |
| 526 current_url_ = GURL(); | 527 current_url_ = GURL(); |
| 527 current_page_classification_ = AutocompleteInput::INVALID_SPEC; | 528 current_page_classification_ = AutocompleteInput::INVALID_SPEC; |
| 528 type_ = INVALID; | 529 type_ = INVALID; |
| 529 parts_ = url_parse::Parsed(); | 530 parts_ = url_parse::Parsed(); |
| 530 scheme_.clear(); | 531 scheme_.clear(); |
| 531 canonicalized_url_ = GURL(); | 532 canonicalized_url_ = GURL(); |
| 532 prevent_inline_autocomplete_ = false; | 533 prevent_inline_autocomplete_ = false; |
| 533 prefer_keyword_ = false; | 534 prefer_keyword_ = false; |
| 534 allow_exact_keyword_match_ = false; | 535 allow_exact_keyword_match_ = false; |
| 535 want_asynchronous_matches_ = true; | 536 want_asynchronous_matches_ = true; |
| 536 } | 537 } |
| OLD | NEW |