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 PopulateTermsPrefixedByHttpOrHttps( | |
| 37 const std::string& text, | |
| 38 std::vector<std::string>* terms_prefixed_by_http_or_https) { | |
| 39 std::vector<std::string> terms; | |
| 40 // Split on whitespace rather than use ICU's word iterator because, for | |
| 41 // example, ICU's iterator may break on punctuation (such as ://) or decide | |
| 42 // to split a single term in a hostname (if it seems to think that the | |
| 43 // hostname is multiple words). Neither of these behaviors is desirable. | |
| 44 base::SplitString(text, ' ', &terms); | |
| 45 const std::string separator(url::kStandardSchemeSeparator); | |
| 46 for (const auto& term : terms) { | |
| 47 static const char* kSchemes[2] = { url::kHttpScheme, url::kHttpsScheme }; | |
| 48 for (const char* scheme : kSchemes) { | |
| 49 const std::string prefix(scheme + separator); | |
| 50 if (StartsWithASCII(term, prefix, false) && | |
|
Mark P
2015/06/12 20:23:20
This can still be a StartsWithASCII, right?
Or may
Peter Kasting
2015/06/15 20:47:16
No, this can still be StartsWithASCII().
Mark P
2015/06/15 23:37:17
Acknowledged.
| |
| 51 (term.length() > prefix.length())) { | |
| 52 terms_prefixed_by_http_or_https->push_back( | |
| 53 term.substr(prefix.length())); | |
| 54 } | |
| 55 } | |
| 56 } | |
| 57 } | |
| 58 | |
| 32 } // namespace | 59 } // namespace |
| 33 | 60 |
| 34 AutocompleteInput::AutocompleteInput() | 61 AutocompleteInput::AutocompleteInput() |
| 35 : cursor_position_(base::string16::npos), | 62 : cursor_position_(base::string16::npos), |
| 36 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), | 63 current_page_classification_(metrics::OmniboxEventProto::INVALID_SPEC), |
| 37 type_(metrics::OmniboxInputType::INVALID), | 64 type_(metrics::OmniboxInputType::INVALID), |
| 38 prevent_inline_autocomplete_(false), | 65 prevent_inline_autocomplete_(false), |
| 39 prefer_keyword_(false), | 66 prefer_keyword_(false), |
| 40 allow_exact_keyword_match_(true), | 67 allow_exact_keyword_match_(true), |
| 41 want_asynchronous_matches_(true) { | 68 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. | 92 // 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. | 93 // Providers that care about trailing white space handle trimming themselves. |
| 67 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & | 94 if ((base::TrimWhitespace(text, base::TRIM_LEADING, &text_) & |
| 68 base::TRIM_LEADING) != 0) | 95 base::TRIM_LEADING) != 0) |
| 69 AdjustCursorPositionIfNecessary(text.length() - text_.length(), | 96 AdjustCursorPositionIfNecessary(text.length() - text_.length(), |
| 70 &cursor_position_); | 97 &cursor_position_); |
| 71 | 98 |
| 72 GURL canonicalized_url; | 99 GURL canonicalized_url; |
| 73 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, | 100 type_ = Parse(text_, desired_tld, scheme_classifier, &parts_, &scheme_, |
| 74 &canonicalized_url); | 101 &canonicalized_url); |
| 102 PopulateTermsPrefixedByHttpOrHttps(base::UTF16ToUTF8(text_), | |
| 103 &terms_prefixed_by_http_or_https_); | |
| 75 | 104 |
| 76 if (type_ == metrics::OmniboxInputType::INVALID) | 105 if (type_ == metrics::OmniboxInputType::INVALID) |
| 77 return; | 106 return; |
| 78 | 107 |
| 79 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || | 108 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || |
| 80 (type_ == metrics::OmniboxInputType::URL)) && | 109 (type_ == metrics::OmniboxInputType::URL)) && |
| 81 canonicalized_url.is_valid() && | 110 canonicalized_url.is_valid() && |
| 82 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || | 111 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || |
| 83 canonicalized_url.SchemeIsFileSystem() || | 112 canonicalized_url.SchemeIsFileSystem() || |
| 84 !canonicalized_url.host().empty())) | 113 !canonicalized_url.host().empty())) |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 current_url_ = GURL(); | 554 current_url_ = GURL(); |
| 526 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; | 555 current_page_classification_ = metrics::OmniboxEventProto::INVALID_SPEC; |
| 527 type_ = metrics::OmniboxInputType::INVALID; | 556 type_ = metrics::OmniboxInputType::INVALID; |
| 528 parts_ = url::Parsed(); | 557 parts_ = url::Parsed(); |
| 529 scheme_.clear(); | 558 scheme_.clear(); |
| 530 canonicalized_url_ = GURL(); | 559 canonicalized_url_ = GURL(); |
| 531 prevent_inline_autocomplete_ = false; | 560 prevent_inline_autocomplete_ = false; |
| 532 prefer_keyword_ = false; | 561 prefer_keyword_ = false; |
| 533 allow_exact_keyword_match_ = false; | 562 allow_exact_keyword_match_ = false; |
| 534 want_asynchronous_matches_ = true; | 563 want_asynchronous_matches_ = true; |
| 564 terms_prefixed_by_http_or_https_.clear(); | |
| 535 } | 565 } |
| OLD | NEW |