| 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 "components/metrics/proto/omnibox_event.pb.h" |
| 11 #include "components/url_fixer/url_fixer.h" | 12 #include "components/url_fixer/url_fixer.h" |
| 12 #include "content/public/common/url_constants.h" | 13 #include "content/public/common/url_constants.h" |
| 13 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 15 #include "url/url_canon_ip.h" | 16 #include "url/url_canon_ip.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; |
| 26 else | 27 else |
| 27 *cursor_position = 0; | 28 *cursor_position = 0; |
| 28 } | 29 } |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 AutocompleteInput::AutocompleteInput() | 33 AutocompleteInput::AutocompleteInput() |
| 33 : cursor_position_(base::string16::npos), | 34 : cursor_position_(base::string16::npos), |
| 34 current_page_classification_(AutocompleteInput::INVALID_SPEC), | 35 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), |
| 35 type_(metrics::OmniboxInputType::INVALID), | 36 type_(metrics::OmniboxInputType::INVALID), |
| 36 prevent_inline_autocomplete_(false), | 37 prevent_inline_autocomplete_(false), |
| 37 prefer_keyword_(false), | 38 prefer_keyword_(false), |
| 38 allow_exact_keyword_match_(true), | 39 allow_exact_keyword_match_(true), |
| 39 want_asynchronous_matches_(true) { | 40 want_asynchronous_matches_(true) { |
| 40 } | 41 } |
| 41 | 42 |
| 42 AutocompleteInput::AutocompleteInput( | 43 AutocompleteInput::AutocompleteInput( |
| 43 const base::string16& text, | 44 const base::string16& text, |
| 44 size_t cursor_position, | 45 size_t cursor_position, |
| 45 const base::string16& desired_tld, | 46 const base::string16& desired_tld, |
| 46 const GURL& current_url, | 47 const GURL& current_url, |
| 47 AutocompleteInput::PageClassification current_page_classification, | 48 metrics::OmniboxEventProto::PageClassification current_page_classification, |
| 48 bool prevent_inline_autocomplete, | 49 bool prevent_inline_autocomplete, |
| 49 bool prefer_keyword, | 50 bool prefer_keyword, |
| 50 bool allow_exact_keyword_match, | 51 bool allow_exact_keyword_match, |
| 51 bool want_asynchronous_matches) | 52 bool want_asynchronous_matches) |
| 52 : cursor_position_(cursor_position), | 53 : cursor_position_(cursor_position), |
| 53 current_url_(current_url), | 54 current_url_(current_url), |
| 54 current_page_classification_(current_page_classification), | 55 current_page_classification_(current_page_classification), |
| 55 prevent_inline_autocomplete_(prevent_inline_autocomplete), | 56 prevent_inline_autocomplete_(prevent_inline_autocomplete), |
| 56 prefer_keyword_(prefer_keyword), | 57 prefer_keyword_(prefer_keyword), |
| 57 allow_exact_keyword_match_(allow_exact_keyword_match), | 58 allow_exact_keyword_match_(allow_exact_keyword_match), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 text_ = trimmed_text; | 93 text_ = trimmed_text; |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 } | 96 } |
| 96 | 97 |
| 97 AutocompleteInput::~AutocompleteInput() { | 98 AutocompleteInput::~AutocompleteInput() { |
| 98 } | 99 } |
| 99 | 100 |
| 100 // static | 101 // static |
| 101 size_t AutocompleteInput::RemoveForcedQueryStringIfNecessary( | 102 size_t AutocompleteInput::RemoveForcedQueryStringIfNecessary( |
| 102 AutocompleteInput::Type type, | 103 metrics::OmniboxInputType::Type type, |
| 103 base::string16* text) { | 104 base::string16* text) { |
| 104 if ((type != metrics::OmniboxInputType::FORCED_QUERY) || text->empty() || | 105 if ((type != metrics::OmniboxInputType::FORCED_QUERY) || text->empty() || |
| 105 (*text)[0] != L'?') | 106 (*text)[0] != L'?') |
| 106 return 0; | 107 return 0; |
| 107 // Drop the leading '?'. | 108 // Drop the leading '?'. |
| 108 text->erase(0, 1); | 109 text->erase(0, 1); |
| 109 return 1; | 110 return 1; |
| 110 } | 111 } |
| 111 | 112 |
| 112 // static | 113 // static |
| 113 std::string AutocompleteInput::TypeToString(AutocompleteInput::Type type) { | 114 std::string AutocompleteInput::TypeToString( |
| 115 metrics::OmniboxInputType::Type type) { |
| 114 switch (type) { | 116 switch (type) { |
| 115 case metrics::OmniboxInputType::INVALID: return "invalid"; | 117 case metrics::OmniboxInputType::INVALID: return "invalid"; |
| 116 case metrics::OmniboxInputType::UNKNOWN: return "unknown"; | 118 case metrics::OmniboxInputType::UNKNOWN: return "unknown"; |
| 117 case metrics::OmniboxInputType::DEPRECATED_REQUESTED_URL: | 119 case metrics::OmniboxInputType::DEPRECATED_REQUESTED_URL: |
| 118 return "deprecated-requested-url"; | 120 return "deprecated-requested-url"; |
| 119 case metrics::OmniboxInputType::URL: return "url"; | 121 case metrics::OmniboxInputType::URL: return "url"; |
| 120 case metrics::OmniboxInputType::QUERY: return "query"; | 122 case metrics::OmniboxInputType::QUERY: return "query"; |
| 121 case metrics::OmniboxInputType::FORCED_QUERY: return "forced-query"; | 123 case metrics::OmniboxInputType::FORCED_QUERY: return "forced-query"; |
| 122 } | 124 } |
| 123 return std::string(); | 125 return std::string(); |
| 124 } | 126 } |
| 125 | 127 |
| 126 // static | 128 // static |
| 127 AutocompleteInput::Type AutocompleteInput::Parse( | 129 metrics::OmniboxInputType::Type AutocompleteInput::Parse( |
| 128 const base::string16& text, | 130 const base::string16& text, |
| 129 const base::string16& desired_tld, | 131 const base::string16& desired_tld, |
| 130 url::Parsed* parts, | 132 url::Parsed* parts, |
| 131 base::string16* scheme, | 133 base::string16* scheme, |
| 132 GURL* canonicalized_url) { | 134 GURL* canonicalized_url) { |
| 133 size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0); | 135 size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0); |
| 134 if (first_non_white == base::string16::npos) | 136 if (first_non_white == base::string16::npos) |
| 135 return metrics::OmniboxInputType::INVALID; // All whitespace. | 137 return metrics::OmniboxInputType::INVALID; // All whitespace. |
| 136 | 138 |
| 137 if (text[first_non_white] == L'?') { | 139 if (text[first_non_white] == L'?') { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 214 |
| 213 default: { | 215 default: { |
| 214 // We don't know about this scheme. It might be that the user typed a | 216 // We don't know about this scheme. It might be that the user typed a |
| 215 // URL of the form "username:password@foo.com". | 217 // URL of the form "username:password@foo.com". |
| 216 const base::string16 http_scheme_prefix = | 218 const base::string16 http_scheme_prefix = |
| 217 base::ASCIIToUTF16(std::string(url::kHttpScheme) + | 219 base::ASCIIToUTF16(std::string(url::kHttpScheme) + |
| 218 url::kStandardSchemeSeparator); | 220 url::kStandardSchemeSeparator); |
| 219 url::Parsed http_parts; | 221 url::Parsed http_parts; |
| 220 base::string16 http_scheme; | 222 base::string16 http_scheme; |
| 221 GURL http_canonicalized_url; | 223 GURL http_canonicalized_url; |
| 222 AutocompleteInput::Type http_type = | 224 metrics::OmniboxInputType::Type http_type = |
| 223 Parse(http_scheme_prefix + text, desired_tld, &http_parts, | 225 Parse(http_scheme_prefix + text, desired_tld, &http_parts, |
| 224 &http_scheme, &http_canonicalized_url); | 226 &http_scheme, &http_canonicalized_url); |
| 225 DCHECK_EQ(std::string(url::kHttpScheme), | 227 DCHECK_EQ(std::string(url::kHttpScheme), |
| 226 base::UTF16ToUTF8(http_scheme)); | 228 base::UTF16ToUTF8(http_scheme)); |
| 227 | 229 |
| 228 if ((http_type == metrics::OmniboxInputType::URL) && | 230 if ((http_type == metrics::OmniboxInputType::URL) && |
| 229 http_parts.username.is_nonempty() && | 231 http_parts.username.is_nonempty() && |
| 230 http_parts.password.is_nonempty()) { | 232 http_parts.password.is_nonempty()) { |
| 231 // Manually re-jigger the parsed parts to match |text| (without the | 233 // Manually re-jigger the parsed parts to match |text| (without the |
| 232 // http scheme added). | 234 // http scheme added). |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 << "Text: '" << text << "', cp: " << cursor_position; | 525 << "Text: '" << text << "', cp: " << cursor_position; |
| 524 text_ = text; | 526 text_ = text; |
| 525 cursor_position_ = cursor_position; | 527 cursor_position_ = cursor_position; |
| 526 parts_ = parts; | 528 parts_ = parts; |
| 527 } | 529 } |
| 528 | 530 |
| 529 void AutocompleteInput::Clear() { | 531 void AutocompleteInput::Clear() { |
| 530 text_.clear(); | 532 text_.clear(); |
| 531 cursor_position_ = base::string16::npos; | 533 cursor_position_ = base::string16::npos; |
| 532 current_url_ = GURL(); | 534 current_url_ = GURL(); |
| 533 current_page_classification_ = AutocompleteInput::INVALID_SPEC; | 535 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; |
| 534 type_ = metrics::OmniboxInputType::INVALID; | 536 type_ = metrics::OmniboxInputType::INVALID; |
| 535 parts_ = url::Parsed(); | 537 parts_ = url::Parsed(); |
| 536 scheme_.clear(); | 538 scheme_.clear(); |
| 537 canonicalized_url_ = GURL(); | 539 canonicalized_url_ = GURL(); |
| 538 prevent_inline_autocomplete_ = false; | 540 prevent_inline_autocomplete_ = false; |
| 539 prefer_keyword_ = false; | 541 prefer_keyword_ = false; |
| 540 allow_exact_keyword_match_ = false; | 542 allow_exact_keyword_match_ = false; |
| 541 want_asynchronous_matches_ = true; | 543 want_asynchronous_matches_ = true; |
| 542 } | 544 } |
| OLD | NEW |