OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/omnibox/browser/autocomplete_provider_utils.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "components/bookmarks/browser/titled_url_node.h" |
| 9 #include "components/omnibox/browser/autocomplete_match.h" |
| 10 #include "components/omnibox/browser/history_provider.h" |
| 11 #include "components/omnibox/browser/url_prefix.h" |
| 12 #include "components/url_formatter/url_formatter.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Converts |positions| into ACMatchClassifications and returns the |
| 17 // classifications. |text_length| is used to determine the need to add an |
| 18 // 'unhighlighted' classification span so the tail of the source string |
| 19 // properly highlighted. |
| 20 ACMatchClassifications ClassificationsFromMatchPositions( |
| 21 const bookmarks::TitledUrlMatch::MatchPositions& positions, |
| 22 size_t text_length, |
| 23 bool is_url) { |
| 24 ACMatchClassification::Style url_style = |
| 25 is_url ? ACMatchClassification::URL : ACMatchClassification::NONE; |
| 26 ACMatchClassifications classifications; |
| 27 if (positions.empty()) { |
| 28 if (text_length > 0) { |
| 29 classifications.push_back(ACMatchClassification(0, url_style)); |
| 30 } |
| 31 return classifications; |
| 32 } |
| 33 |
| 34 for (bookmarks::TitledUrlMatch::MatchPositions::const_iterator i = |
| 35 positions.begin(); |
| 36 i != positions.end(); |
| 37 ++i) { |
| 38 AutocompleteMatch::ACMatchClassifications new_class; |
| 39 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, |
| 40 text_length, url_style, &new_class); |
| 41 classifications = AutocompleteMatch::MergeClassifications( |
| 42 classifications, new_class); |
| 43 } |
| 44 return classifications; |
| 45 } |
| 46 } // namespace |
| 47 |
| 48 AutocompleteMatch TitledUrlMatchToAutocompleteMatch( |
| 49 AutocompleteProvider* provider, |
| 50 const AutocompleteSchemeClassifier& scheme_classifier, |
| 51 const AutocompleteInput& input, |
| 52 const base::string16& fixed_up_input_text, |
| 53 const bookmarks::TitledUrlMatch& titled_url_match, |
| 54 AutocompleteMatchType::Type type, |
| 55 int relevance) { |
| 56 const GURL& url = titled_url_match.node->GetTitledUrlNodeUrl(); |
| 57 base::string16 title = titled_url_match.node->GetTitledUrlNodeTitle(); |
| 58 |
| 59 AutocompleteMatch match(provider, 0, false, type); |
| 60 bookmarks::TitledUrlMatch::MatchPositions new_title_match_positions = |
| 61 titled_url_match.title_match_positions; |
| 62 bookmarks::TitledUrlMatch::CorrectTitleAndMatchPositions( |
| 63 &title, &new_title_match_positions); |
| 64 const base::string16& url_utf16 = base::UTF8ToUTF16(url.spec()); |
| 65 size_t inline_autocomplete_offset = URLPrefix::GetInlineAutocompleteOffset( |
| 66 input.text(), fixed_up_input_text, false, url_utf16); |
| 67 match.destination_url = url; |
| 68 const size_t match_start = titled_url_match.url_match_positions.empty() ? |
| 69 0 : titled_url_match.url_match_positions[0].first; |
| 70 const bool trim_http = !AutocompleteInput::HasHTTPScheme(input.text()) && |
| 71 ((match_start == base::string16::npos) || (match_start != 0)); |
| 72 std::vector<size_t> offsets = |
| 73 bookmarks::TitledUrlMatch::OffsetsFromMatchPositions( |
| 74 titled_url_match.url_match_positions); |
| 75 // In addition to knowing how |offsets| is transformed, we need to know how |
| 76 // |inline_autocomplete_offset| is transformed. We add it to the end of |
| 77 // |offsets|, compute how everything is transformed, then remove it from the |
| 78 // end. |
| 79 offsets.push_back(inline_autocomplete_offset); |
| 80 match.contents = url_formatter::FormatUrlWithOffsets( |
| 81 url, url_formatter::kFormatUrlOmitAll & |
| 82 ~(trim_http ? 0 : url_formatter::kFormatUrlOmitHTTP), |
| 83 net::UnescapeRule::SPACES, nullptr, nullptr, &offsets); |
| 84 inline_autocomplete_offset = offsets.back(); |
| 85 offsets.pop_back(); |
| 86 bookmarks::TitledUrlMatch::MatchPositions new_url_match_positions = |
| 87 bookmarks::TitledUrlMatch::ReplaceOffsetsInMatchPositions( |
| 88 titled_url_match.url_match_positions, offsets); |
| 89 match.contents_class = |
| 90 ClassificationsFromMatchPositions(new_url_match_positions, |
| 91 match.contents.size(), |
| 92 true); |
| 93 match.fill_into_edit = |
| 94 AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 95 url, match.contents, scheme_classifier); |
| 96 if (inline_autocomplete_offset != base::string16::npos) { |
| 97 // |inline_autocomplete_offset| may be beyond the end of the |
| 98 // |fill_into_edit| if the user has typed an URL with a scheme and the |
| 99 // last character typed is a slash. That slash is removed by the |
| 100 // FormatURLWithOffsets call above. |
| 101 if (inline_autocomplete_offset < match.fill_into_edit.length()) { |
| 102 match.inline_autocompletion = |
| 103 match.fill_into_edit.substr(inline_autocomplete_offset); |
| 104 } |
| 105 match.allowed_to_be_default_match = match.inline_autocompletion.empty() || |
| 106 !HistoryProvider::PreventInlineAutocomplete(input); |
| 107 } |
| 108 match.description = title; |
| 109 match.description_class = |
| 110 ClassificationsFromMatchPositions(titled_url_match.title_match_positions, |
| 111 match.description.size(), |
| 112 false); |
| 113 |
| 114 match.relevance = relevance; |
| 115 |
| 116 return match; |
| 117 } |
OLD | NEW |