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/url_fixer/url_fixer.h" | 12 #include "components/url_fixer/url_fixer.h" |
12 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
14 #include "url/url_canon_ip.h" | 15 #include "url/url_canon_ip.h" |
15 #include "url/url_util.h" | 16 #include "url/url_util.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Hardcode constant to avoid any dependencies on content/. | 20 // Hardcode constant to avoid any dependencies on content/. |
20 const char kViewSourceScheme[] = "view-source"; | 21 const char kViewSourceScheme[] = "view-source"; |
21 | 22 |
22 void AdjustCursorPositionIfNecessary(size_t num_leading_chars_removed, | 23 void AdjustCursorPositionIfNecessary(size_t num_leading_chars_removed, |
23 size_t* cursor_position) { | 24 size_t* cursor_position) { |
24 if (*cursor_position == base::string16::npos) | 25 if (*cursor_position == base::string16::npos) |
25 return; | 26 return; |
26 if (num_leading_chars_removed < *cursor_position) | 27 if (num_leading_chars_removed < *cursor_position) |
27 *cursor_position -= num_leading_chars_removed; | 28 *cursor_position -= num_leading_chars_removed; |
28 else | 29 else |
29 *cursor_position = 0; | 30 *cursor_position = 0; |
30 } | 31 } |
31 | 32 |
33 // Finds all terms in |text| that start with http:// or https:// and puts the | |
34 // text after the prefix in |http_following_terms|. Each term has to include | |
35 // at least one additional character after the prefix. | |
Peter Kasting
2015/06/09 20:36:39
Nit: Could say "...or https:// plus at least one m
Mark P
2015/06/10 23:38:34
Done.
| |
36 void PopulateHttpTerms(const base::string16& text, | |
Peter Kasting
2015/06/09 20:36:38
Nit: If you change the member name, change this to
Mark P
2015/06/10 23:38:34
Selected PopulateTermsPrefixedByScheme().
| |
37 std::vector<base::string16>* http_following_terms) { | |
38 const base::string16& http_scheme = | |
Peter Kasting
2015/06/09 20:36:38
Rather than converting these to string16s, I think
Mark P
2015/06/10 23:38:34
I don't like this idea. Everywhere in the autocom
Peter Kasting
2015/06/11 00:07:38
This isn't an intentional pattern. The intentiona
Mark P
2015/06/11 22:11:41
Okay, I did it. Easy enough.
| |
39 base::UTF8ToUTF16(url::kHttpScheme) + | |
40 base::UTF8ToUTF16(url::kStandardSchemeSeparator); | |
41 const base::string16& https_scheme = | |
42 base::UTF8ToUTF16(url::kHttpsScheme) + | |
43 base::UTF8ToUTF16(url::kStandardSchemeSeparator); | |
44 std::vector<base::string16> words; | |
Peter Kasting
2015/06/09 20:36:38
Nit: words -> terms (for consistency)?
Mark P
2015/06/10 23:38:34
Done.
| |
45 base::SplitString(text, ' ', &words); | |
Peter Kasting
2015/06/09 20:36:38
Nit: Consider a comment about why we don't use ICU
Mark P
2015/06/10 23:38:34
Done.
| |
46 for (auto it : words) { | |
Peter Kasting
2015/06/09 20:36:38
Nit: const auto&, to avoid a copy
Mark P
2015/06/10 23:38:34
Done.
| |
47 if (StartsWith(it, http_scheme, false) && | |
Peter Kasting
2015/06/09 20:36:38
Nit: This is shorter overall and no less efficient
Mark P
2015/06/10 23:38:34
Done. (well, the string16 version of the above)
| |
48 (it.length() > http_scheme.length())) { | |
49 http_following_terms->push_back(it.substr(http_scheme.length())); | |
50 } else if (StartsWith(it, https_scheme, false) && | |
51 (it.length() > https_scheme.length())) { | |
52 http_following_terms->push_back(it.substr(https_scheme.length())); | |
53 } | |
54 } | |
55 } | |
56 | |
32 } // namespace | 57 } // namespace |
33 | 58 |
34 AutocompleteInput::AutocompleteInput() | 59 AutocompleteInput::AutocompleteInput() |
35 : cursor_position_(base::string16::npos), | 60 : cursor_position_(base::string16::npos), |
36 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), | 61 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), |
37 type_(metrics::OmniboxInputType::INVALID), | 62 type_(metrics::OmniboxInputType::INVALID), |
38 prevent_inline_autocomplete_(false), | 63 prevent_inline_autocomplete_(false), |
39 prefer_keyword_(false), | 64 prefer_keyword_(false), |
40 allow_exact_keyword_match_(true), | 65 allow_exact_keyword_match_(true), |
41 want_asynchronous_matches_(true) { | 66 want_asynchronous_matches_(true) { |
(...skipping 23 matching lines...) Expand all Loading... | |
65 // None of the providers care about leading white space so we always trim it. | 90 // None of the providers care about leading white space so we always trim it. |
66 // Providers that care about trailing white space handle trimming themselves. | 91 // Providers that care about trailing white space handle trimming themselves. |
67 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & | 92 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & |
68 base::TRIM_LEADING) != 0) | 93 base::TRIM_LEADING) != 0) |
69 AdjustCursorPositionIfNecessary(text.length() - text_.length(), | 94 AdjustCursorPositionIfNecessary(text.length() - text_.length(), |
70 &cursor_position_); | 95 &cursor_position_); |
71 | 96 |
72 GURL canonicalized_url; | 97 GURL canonicalized_url; |
73 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, | 98 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, |
74 &canonicalized_url); | 99 &canonicalized_url); |
100 PopulateHttpTerms(text_, &http_following_terms_); | |
75 | 101 |
76 if (type_ == metrics::OmniboxInputType::INVALID) | 102 if (type_ == metrics::OmniboxInputType::INVALID) |
77 return; | 103 return; |
78 | 104 |
79 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || | 105 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || |
80 (type_ == metrics::OmniboxInputType::URL)) && | 106 (type_ == metrics::OmniboxInputType::URL)) && |
81 canonicalized_url.is_valid() && | 107 canonicalized_url.is_valid() && |
82 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || | 108 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || |
83 canonicalized_url.SchemeIsFileSystem() || | 109 canonicalized_url.SchemeIsFileSystem() || |
84 !canonicalized_url.host().empty())) | 110 !canonicalized_url.host().empty())) |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
525 current_url_ = GURL(); | 551 current_url_ = GURL(); |
526 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; | 552 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; |
527 type_ = metrics::OmniboxInputType::INVALID; | 553 type_ = metrics::OmniboxInputType::INVALID; |
528 parts_ = url::Parsed(); | 554 parts_ = url::Parsed(); |
529 scheme_.clear(); | 555 scheme_.clear(); |
530 canonicalized_url_ = GURL(); | 556 canonicalized_url_ = GURL(); |
531 prevent_inline_autocomplete_ = false; | 557 prevent_inline_autocomplete_ = false; |
532 prefer_keyword_ = false; | 558 prefer_keyword_ = false; |
533 allow_exact_keyword_match_ = false; | 559 allow_exact_keyword_match_ = false; |
534 want_asynchronous_matches_ = true; | 560 want_asynchronous_matches_ = true; |
561 http_following_terms_.clear(); | |
535 } | 562 } |
OLD | NEW |