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<std::pair<std::string, base::string16> >* | |
40 terms_prefixed_by_http_or_https) { | |
41 std::vector<base::string16> terms; | |
42 // Split on whitespace rather than use ICU's word iterator because, for | |
43 // example, ICU's iterator may break on punctuation (such as ://) or decide | |
44 // to split a single term in a hostname (if it seems to think that the | |
45 // hostname is multiple words). Neither of these behaviors is desirable. | |
46 base::SplitString(text, ' ', &terms); | |
47 const std::string separator(url::kStandardSchemeSeparator); | |
48 for (const auto& term : terms) { | |
49 const std::string term_utf8(base::UTF16ToUTF8(term)); | |
50 static const char* kSchemes[2] = { url::kHttpScheme, url::kHttpsScheme }; | |
51 for (const char* scheme : kSchemes) { | |
52 const std::string prefix(scheme + separator); | |
53 // Doing an ASCII comparison is okay because prefix is in ASCII. | |
Peter Kasting
2015/06/29 05:04:59
Tiny nit: "...prefix is ASCII" (no "in") reads sli
Mark P
2015/06/30 04:23:17
Done.
| |
54 if (base::StartsWith(term_utf8, prefix, | |
55 base::CompareCase::INSENSITIVE_ASCII) && | |
56 (term_utf8.length() > prefix.length())) { | |
57 terms_prefixed_by_http_or_https->push_back( | |
58 make_pair(term_utf8.substr(prefix.length()), | |
59 term.substr(prefix.length()))); | |
60 } | |
61 } | |
62 } | |
63 } | |
64 | |
33 } // namespace | 65 } // namespace |
34 | 66 |
35 AutocompleteInput::AutocompleteInput() | 67 AutocompleteInput::AutocompleteInput() |
36 : cursor_position_(base::string16::npos), | 68 : cursor_position_(base::string16::npos), |
37 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), | 69 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), |
38 type_(metrics::OmniboxInputType::INVALID), | 70 type_(metrics::OmniboxInputType::INVALID), |
39 prevent_inline_autocomplete_(false), | 71 prevent_inline_autocomplete_(false), |
40 prefer_keyword_(false), | 72 prefer_keyword_(false), |
41 allow_exact_keyword_match_(true), | 73 allow_exact_keyword_match_(true), |
42 want_asynchronous_matches_(true), | 74 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. | 101 // 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. | 102 // Providers that care about trailing white space handle trimming themselves. |
71 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & | 103 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & |
72 base::TRIM_LEADING) != 0) | 104 base::TRIM_LEADING) != 0) |
73 AdjustCursorPositionIfNecessary(text.length() - text_.length(), | 105 AdjustCursorPositionIfNecessary(text.length() - text_.length(), |
74 &cursor_position_); | 106 &cursor_position_); |
75 | 107 |
76 GURL canonicalized_url; | 108 GURL canonicalized_url; |
77 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, | 109 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, |
78 &canonicalized_url); | 110 &canonicalized_url); |
111 PopulateTermsPrefixedByHttpOrHttps(text_, &terms_prefixed_by_http_or_https_); | |
79 | 112 |
80 if (type_ == metrics::OmniboxInputType::INVALID) | 113 if (type_ == metrics::OmniboxInputType::INVALID) |
81 return; | 114 return; |
82 | 115 |
83 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || | 116 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || |
84 (type_ == metrics::OmniboxInputType::URL)) && | 117 (type_ == metrics::OmniboxInputType::URL)) && |
85 canonicalized_url.is_valid() && | 118 canonicalized_url.is_valid() && |
86 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || | 119 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || |
87 canonicalized_url.SchemeIsFileSystem() || | 120 canonicalized_url.SchemeIsFileSystem() || |
88 !canonicalized_url.host().empty())) | 121 !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; | 564 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; |
532 type_ = metrics::OmniboxInputType::INVALID; | 565 type_ = metrics::OmniboxInputType::INVALID; |
533 parts_ = url::Parsed(); | 566 parts_ = url::Parsed(); |
534 scheme_.clear(); | 567 scheme_.clear(); |
535 canonicalized_url_ = GURL(); | 568 canonicalized_url_ = GURL(); |
536 prevent_inline_autocomplete_ = false; | 569 prevent_inline_autocomplete_ = false; |
537 prefer_keyword_ = false; | 570 prefer_keyword_ = false; |
538 allow_exact_keyword_match_ = false; | 571 allow_exact_keyword_match_ = false; |
539 want_asynchronous_matches_ = true; | 572 want_asynchronous_matches_ = true; |
540 from_omnibox_focus_ = false; | 573 from_omnibox_focus_ = false; |
574 terms_prefixed_by_http_or_https_.clear(); | |
541 } | 575 } |
OLD | NEW |