Chromium Code Reviews| 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:// plus at least | |
| 34 // one more character and puts the text after the prefix in | |
| 35 // |terms_prefixed_by_http_or_https|. | |
| 36 void PopulateTermsPrefixedByScheme( | |
| 37 const base::string16& text, | |
| 38 std::vector<base::string16>* terms_prefixed_by_http_or_https) { | |
|
Peter Kasting
2015/06/11 05:21:44
Nit: Call this |terms_prefixed_by_scheme| to match
Mark P
2015/06/11 22:11:41
I'd rather the increase in precision of http_or_ht
| |
| 39 base::string16 kPrefixes[2] = { | |
|
Peter Kasting
2015/06/11 05:21:44
Even if you were to stick with string16 (which I d
Mark P
2015/06/11 22:11:41
Acknowledged. Now obsolete.
| |
| 40 base::UTF8ToUTF16(url::kHttpScheme) + | |
| 41 base::UTF8ToUTF16(url::kStandardSchemeSeparator), | |
| 42 base::UTF8ToUTF16(url::kHttpsScheme) + | |
| 43 base::UTF8ToUTF16(url::kStandardSchemeSeparator) | |
| 44 }; | |
| 45 std::vector<base::string16> terms; | |
| 46 // Split on whitespace rather than use ICU's word iterator because, for | |
| 47 // examples, ICU's iterator may break on punctuation (such as ://) or decide | |
|
Peter Kasting
2015/06/11 05:21:44
Nit: example
Mark P
2015/06/11 22:11:41
Done.
| |
| 48 // to split a single term in a hostname (if it seems to think that the | |
| 49 // hostname is multiple words). Neither of these behaviors are desirable. | |
|
Peter Kasting
2015/06/11 05:21:44
Nit: are -> is ("neither" is singular)
Mark P
2015/06/11 22:11:41
Done.
| |
| 50 base::SplitString(text, ' ', &terms); | |
| 51 for (const auto& term : terms) { | |
| 52 for (const auto& prefix : kPrefixes) { | |
| 53 if (StartsWith(term, prefix, false) && | |
| 54 (term.length() > prefix.length())) { | |
| 55 terms_prefixed_by_http_or_https->push_back( | |
| 56 term.substr(prefix.length())); | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 } | |
| 61 | |
| 32 } // namespace | 62 } // namespace |
| 33 | 63 |
| 34 AutocompleteInput::AutocompleteInput() | 64 AutocompleteInput::AutocompleteInput() |
| 35 : cursor_position_(base::string16::npos), | 65 : cursor_position_(base::string16::npos), |
| 36 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), | 66 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), |
| 37 type_(metrics::OmniboxInputType::INVALID), | 67 type_(metrics::OmniboxInputType::INVALID), |
| 38 prevent_inline_autocomplete_(false), | 68 prevent_inline_autocomplete_(false), |
| 39 prefer_keyword_(false), | 69 prefer_keyword_(false), |
| 40 allow_exact_keyword_match_(true), | 70 allow_exact_keyword_match_(true), |
| 41 want_asynchronous_matches_(true) { | 71 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. | 95 // 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. | 96 // Providers that care about trailing white space handle trimming themselves. |
| 67 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & | 97 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & |
| 68 base::TRIM_LEADING) != 0) | 98 base::TRIM_LEADING) != 0) |
| 69 AdjustCursorPositionIfNecessary(text.length() - text_.length(), | 99 AdjustCursorPositionIfNecessary(text.length() - text_.length(), |
| 70 &cursor_position_); | 100 &cursor_position_); |
| 71 | 101 |
| 72 GURL canonicalized_url; | 102 GURL canonicalized_url; |
| 73 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, | 103 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, |
| 74 &canonicalized_url); | 104 &canonicalized_url); |
| 105 PopulateTermsPrefixedByScheme(text_, &terms_prefixed_by_http_or_https_); | |
| 75 | 106 |
| 76 if (type_ == metrics::OmniboxInputType::INVALID) | 107 if (type_ == metrics::OmniboxInputType::INVALID) |
| 77 return; | 108 return; |
| 78 | 109 |
| 79 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || | 110 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || |
| 80 (type_ == metrics::OmniboxInputType::URL)) && | 111 (type_ == metrics::OmniboxInputType::URL)) && |
| 81 canonicalized_url.is_valid() && | 112 canonicalized_url.is_valid() && |
| 82 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || | 113 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || |
| 83 canonicalized_url.SchemeIsFileSystem() || | 114 canonicalized_url.SchemeIsFileSystem() || |
| 84 !canonicalized_url.host().empty())) | 115 !canonicalized_url.host().empty())) |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 current_url_ = GURL(); | 556 current_url_ = GURL(); |
| 526 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; | 557 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; |
| 527 type_ = metrics::OmniboxInputType::INVALID; | 558 type_ = metrics::OmniboxInputType::INVALID; |
| 528 parts_ = url::Parsed(); | 559 parts_ = url::Parsed(); |
| 529 scheme_.clear(); | 560 scheme_.clear(); |
| 530 canonicalized_url_ = GURL(); | 561 canonicalized_url_ = GURL(); |
| 531 prevent_inline_autocomplete_ = false; | 562 prevent_inline_autocomplete_ = false; |
| 532 prefer_keyword_ = false; | 563 prefer_keyword_ = false; |
| 533 allow_exact_keyword_match_ = false; | 564 allow_exact_keyword_match_ = false; |
| 534 want_asynchronous_matches_ = true; | 565 want_asynchronous_matches_ = true; |
| 566 terms_prefixed_by_http_or_https_.clear(); | |
| 535 } | 567 } |
| OLD | NEW |