OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/omnibox/autocomplete_input.h" | 5 #include "components/omnibox/autocomplete_input.h" |
6 | 6 |
| 7 #include "base/strings/string_split.h" |
7 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
9 #include "components/metrics/proto/omnibox_event.pb.h" | 10 #include "components/metrics/proto/omnibox_event.pb.h" |
10 #include "components/omnibox/autocomplete_scheme_classifier.h" | 11 #include "components/omnibox/autocomplete_scheme_classifier.h" |
11 #include "components/omnibox/omnibox_field_trial.h" | 12 #include "components/omnibox/omnibox_field_trial.h" |
12 #include "components/url_fixer/url_fixer.h" | 13 #include "components/url_fixer/url_fixer.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 // Hardcode constant to avoid any dependencies on content/. | 21 // Hardcode constant to avoid any dependencies on content/. |
21 const char kViewSourceScheme[] = "view-source"; | 22 const char kViewSourceScheme[] = "view-source"; |
22 | 23 |
23 void AdjustCursorPositionIfNecessary(size_t num_leading_chars_removed, | 24 void AdjustCursorPositionIfNecessary(size_t num_leading_chars_removed, |
24 size_t* cursor_position) { | 25 size_t* cursor_position) { |
25 if (*cursor_position == base::string16::npos) | 26 if (*cursor_position == base::string16::npos) |
26 return; | 27 return; |
27 if (num_leading_chars_removed < *cursor_position) | 28 if (num_leading_chars_removed < *cursor_position) |
28 *cursor_position -= num_leading_chars_removed; | 29 *cursor_position -= num_leading_chars_removed; |
29 else | 30 else |
30 *cursor_position = 0; | 31 *cursor_position = 0; |
31 } | 32 } |
32 | 33 |
| 34 // Finds all terms in |text| that start with http:// or https:// plus at least |
| 35 // one more character and puts the text after the prefix in |
| 36 // |terms_prefixed_by_http_or_https|. |
| 37 void PopulateTermsPrefixedByHttpOrHttps( |
| 38 const base::string16& text, |
| 39 std::vector<base::string16>* terms_prefixed_by_http_or_https) { |
| 40 std::vector<base::string16> terms; |
| 41 // Split on whitespace rather than use ICU's word iterator because, for |
| 42 // example, ICU's iterator may break on punctuation (such as ://) or decide |
| 43 // to split a single term in a hostname (if it seems to think that the |
| 44 // hostname is multiple words). Neither of these behaviors is desirable. |
| 45 base::SplitString(text, ' ', &terms); |
| 46 const std::string separator(url::kStandardSchemeSeparator); |
| 47 for (const auto& term : terms) { |
| 48 const std::string term_utf8(base::UTF16ToUTF8(term)); |
| 49 static const char* kSchemes[2] = { url::kHttpScheme, url::kHttpsScheme }; |
| 50 for (const char* scheme : kSchemes) { |
| 51 const std::string prefix(scheme + separator); |
| 52 // Doing an ASCII comparison is okay because prefix is ASCII. |
| 53 if (base::StartsWith(term_utf8, prefix, |
| 54 base::CompareCase::INSENSITIVE_ASCII) && |
| 55 (term_utf8.length() > prefix.length())) { |
| 56 terms_prefixed_by_http_or_https->push_back( |
| 57 term.substr(prefix.length())); |
| 58 } |
| 59 } |
| 60 } |
| 61 } |
| 62 |
33 } // namespace | 63 } // namespace |
34 | 64 |
35 AutocompleteInput::AutocompleteInput() | 65 AutocompleteInput::AutocompleteInput() |
36 : cursor_position_(base::string16::npos), | 66 : cursor_position_(base::string16::npos), |
37 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), | 67 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), |
38 type_(metrics::OmniboxInputType::INVALID), | 68 type_(metrics::OmniboxInputType::INVALID), |
39 prevent_inline_autocomplete_(false), | 69 prevent_inline_autocomplete_(false), |
40 prefer_keyword_(false), | 70 prefer_keyword_(false), |
41 allow_exact_keyword_match_(true), | 71 allow_exact_keyword_match_(true), |
42 want_asynchronous_matches_(true), | 72 want_asynchronous_matches_(true), |
(...skipping 26 matching lines...) Expand all Loading... |
69 // None of the providers care about leading white space so we always trim it. | 99 // None of the providers care about leading white space so we always trim it. |
70 // Providers that care about trailing white space handle trimming themselves. | 100 // Providers that care about trailing white space handle trimming themselves. |
71 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & | 101 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & |
72 base::TRIM_LEADING) != 0) | 102 base::TRIM_LEADING) != 0) |
73 AdjustCursorPositionIfNecessary(text.length() - text_.length(), | 103 AdjustCursorPositionIfNecessary(text.length() - text_.length(), |
74 &cursor_position_); | 104 &cursor_position_); |
75 | 105 |
76 GURL canonicalized_url; | 106 GURL canonicalized_url; |
77 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, | 107 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, |
78 &canonicalized_url); | 108 &canonicalized_url); |
| 109 PopulateTermsPrefixedByHttpOrHttps(text_, &terms_prefixed_by_http_or_https_); |
79 | 110 |
80 if (type_ == metrics::OmniboxInputType::INVALID) | 111 if (type_ == metrics::OmniboxInputType::INVALID) |
81 return; | 112 return; |
82 | 113 |
83 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || | 114 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || |
84 (type_ == metrics::OmniboxInputType::URL)) && | 115 (type_ == metrics::OmniboxInputType::URL)) && |
85 canonicalized_url.is_valid() && | 116 canonicalized_url.is_valid() && |
86 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || | 117 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || |
87 canonicalized_url.SchemeIsFileSystem() || | 118 canonicalized_url.SchemeIsFileSystem() || |
88 !canonicalized_url.host().empty())) | 119 !canonicalized_url.host().empty())) |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; | 562 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; |
532 type_ = metrics::OmniboxInputType::INVALID; | 563 type_ = metrics::OmniboxInputType::INVALID; |
533 parts_ = url::Parsed(); | 564 parts_ = url::Parsed(); |
534 scheme_.clear(); | 565 scheme_.clear(); |
535 canonicalized_url_ = GURL(); | 566 canonicalized_url_ = GURL(); |
536 prevent_inline_autocomplete_ = false; | 567 prevent_inline_autocomplete_ = false; |
537 prefer_keyword_ = false; | 568 prefer_keyword_ = false; |
538 allow_exact_keyword_match_ = false; | 569 allow_exact_keyword_match_ = false; |
539 want_asynchronous_matches_ = true; | 570 want_asynchronous_matches_ = true; |
540 from_omnibox_focus_ = false; | 571 from_omnibox_focus_ = false; |
| 572 terms_prefixed_by_http_or_https_.clear(); |
541 } | 573 } |
OLD | NEW |