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 std::string& text, | |
39 std::vector<std::string>* terms_prefixed_by_http_or_https) { | |
40 std::vector<std::string> 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 static const char* kSchemes[2] = { url::kHttpScheme, url::kHttpsScheme }; | |
49 for (const char* scheme : kSchemes) { | |
50 const std::string prefix(scheme + separator); | |
51 if (base::StartsWithASCII(term, prefix, false) && | |
brettw
2015/06/22 17:54:01
Can you use the new versions? I want to delete the
Mark P
2015/06/22 17:59:36
Done.
| |
52 (term.length() > prefix.length())) { | |
53 terms_prefixed_by_http_or_https->push_back( | |
54 term.substr(prefix.length())); | |
55 } | |
56 } | |
57 } | |
58 } | |
59 | |
33 } // namespace | 60 } // namespace |
34 | 61 |
35 AutocompleteInput::AutocompleteInput() | 62 AutocompleteInput::AutocompleteInput() |
36 : cursor_position_(base::string16::npos), | 63 : cursor_position_(base::string16::npos), |
37 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), | 64 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), |
38 type_(metrics::OmniboxInputType::INVALID), | 65 type_(metrics::OmniboxInputType::INVALID), |
39 prevent_inline_autocomplete_(false), | 66 prevent_inline_autocomplete_(false), |
40 prefer_keyword_(false), | 67 prefer_keyword_(false), |
41 allow_exact_keyword_match_(true), | 68 allow_exact_keyword_match_(true), |
42 want_asynchronous_matches_(true), | 69 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. | 96 // 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. | 97 // Providers that care about trailing white space handle trimming themselves. |
71 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & | 98 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & |
72 base::TRIM_LEADING) != 0) | 99 base::TRIM_LEADING) != 0) |
73 AdjustCursorPositionIfNecessary(text.length() - text_.length(), | 100 AdjustCursorPositionIfNecessary(text.length() - text_.length(), |
74 &cursor_position_); | 101 &cursor_position_); |
75 | 102 |
76 GURL canonicalized_url; | 103 GURL canonicalized_url; |
77 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, | 104 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, |
78 &canonicalized_url); | 105 &canonicalized_url); |
106 PopulateTermsPrefixedByHttpOrHttps(base::UTF16ToUTF8(text_), | |
107 &terms_prefixed_by_http_or_https_); | |
79 | 108 |
80 if (type_ == metrics::OmniboxInputType::INVALID) | 109 if (type_ == metrics::OmniboxInputType::INVALID) |
81 return; | 110 return; |
82 | 111 |
83 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || | 112 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || |
84 (type_ == metrics::OmniboxInputType::URL)) && | 113 (type_ == metrics::OmniboxInputType::URL)) && |
85 canonicalized_url.is_valid() && | 114 canonicalized_url.is_valid() && |
86 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || | 115 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || |
87 canonicalized_url.SchemeIsFileSystem() || | 116 canonicalized_url.SchemeIsFileSystem() || |
88 !canonicalized_url.host().empty())) | 117 !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; | 560 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; |
532 type_ = metrics::OmniboxInputType::INVALID; | 561 type_ = metrics::OmniboxInputType::INVALID; |
533 parts_ = url::Parsed(); | 562 parts_ = url::Parsed(); |
534 scheme_.clear(); | 563 scheme_.clear(); |
535 canonicalized_url_ = GURL(); | 564 canonicalized_url_ = GURL(); |
536 prevent_inline_autocomplete_ = false; | 565 prevent_inline_autocomplete_ = false; |
537 prefer_keyword_ = false; | 566 prefer_keyword_ = false; |
538 allow_exact_keyword_match_ = false; | 567 allow_exact_keyword_match_ = false; |
539 want_asynchronous_matches_ = true; | 568 want_asynchronous_matches_ = true; |
540 from_omnibox_focus_ = false; | 569 from_omnibox_focus_ = false; |
570 terms_prefixed_by_http_or_https_.clear(); | |
541 } | 571 } |
OLD | NEW |