Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: components/omnibox/browser/titled_url_match_utils.cc

Issue 2583763003: Factor out AutocompleteMatch creation from BookmarkProvider (Closed)
Patch Set: TEST_F -> TEST Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/titled_url_match_utils.h"
6
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/bookmarks/browser/titled_url_node.h"
10 #include "components/omnibox/browser/autocomplete_match.h"
11 #include "components/omnibox/browser/history_provider.h"
12 #include "components/omnibox/browser/url_prefix.h"
13 #include "components/url_formatter/url_formatter.h"
14
15 namespace {
16
17 // Converts |positions| into ACMatchClassifications and returns the
18 // classifications. |text_length| is used to determine the need to add an
19 // 'unhighlighted' classification span so the tail of the source string
20 // properly highlighted.
21 ACMatchClassifications ClassificationsFromMatchPositions(
22 const bookmarks::TitledUrlMatch::MatchPositions& positions,
23 size_t text_length,
24 bool is_url) {
25 ACMatchClassification::Style url_style =
26 is_url ? ACMatchClassification::URL : ACMatchClassification::NONE;
27 ACMatchClassifications classifications;
28 if (positions.empty()) {
29 if (text_length > 0) {
30 classifications.push_back(ACMatchClassification(0, url_style));
31 }
32 return classifications;
33 }
34
35 for (bookmarks::TitledUrlMatch::MatchPositions::const_iterator i =
36 positions.begin();
37 i != positions.end(); ++i) {
38 AutocompleteMatch::ACMatchClassifications new_class;
39 AutocompleteMatch::ClassifyLocationInString(
40 i->first, i->second - i->first, text_length, url_style, &new_class);
41 classifications =
42 AutocompleteMatch::MergeClassifications(classifications, new_class);
43 }
44 return classifications;
45 }
46
47 } // namespace
48
49 namespace bookmarks {
50
51 AutocompleteMatch TitledUrlMatchToAutocompleteMatch(
52 const TitledUrlMatch& titled_url_match,
53 AutocompleteMatchType::Type type,
54 int relevance,
55 AutocompleteProvider* provider,
56 const AutocompleteSchemeClassifier& scheme_classifier,
57 const AutocompleteInput& input,
58 const base::string16& fixed_up_input_text) {
59 const GURL& url = titled_url_match.node->GetTitledUrlNodeUrl();
60 base::string16 title = titled_url_match.node->GetTitledUrlNodeTitle();
61
62 // The AutocompleteMatch we construct is non-deletable because the only way to
63 // support this would be to delete the underlying object that created the
64 // titled_url_match. E.g., for the bookmark provider this would mean deleting
65 // the underlying bookmark, which is unlikely to be what the user intends.
66 AutocompleteMatch match(provider, relevance, false, type);
67 TitledUrlMatch::MatchPositions new_title_match_positions =
68 titled_url_match.title_match_positions;
69 CorrectTitleAndMatchPositions(&title, &new_title_match_positions);
70 const base::string16& url_utf16 = base::UTF8ToUTF16(url.spec());
71 size_t inline_autocomplete_offset = URLPrefix::GetInlineAutocompleteOffset(
72 input.text(), fixed_up_input_text, false, url_utf16);
73 match.destination_url = url;
74 const size_t match_start =
75 titled_url_match.url_match_positions.empty()
76 ? 0
77 : titled_url_match.url_match_positions[0].first;
78 const bool trim_http =
79 !AutocompleteInput::HasHTTPScheme(input.text()) &&
80 ((match_start == base::string16::npos) || (match_start != 0));
81 std::vector<size_t> offsets = TitledUrlMatch::OffsetsFromMatchPositions(
82 titled_url_match.url_match_positions);
83 // In addition to knowing how |offsets| is transformed, we need to know how
84 // |inline_autocomplete_offset| is transformed. We add it to the end of
85 // |offsets|, compute how everything is transformed, then remove it from the
86 // end.
87 offsets.push_back(inline_autocomplete_offset);
88 match.contents = url_formatter::FormatUrlWithOffsets(
89 url, url_formatter::kFormatUrlOmitAll &
90 ~(trim_http ? 0 : url_formatter::kFormatUrlOmitHTTP),
91 net::UnescapeRule::SPACES, nullptr, nullptr, &offsets);
92 inline_autocomplete_offset = offsets.back();
93 offsets.pop_back();
94 TitledUrlMatch::MatchPositions new_url_match_positions =
95 TitledUrlMatch::ReplaceOffsetsInMatchPositions(
96 titled_url_match.url_match_positions, offsets);
97 match.contents_class = ClassificationsFromMatchPositions(
98 new_url_match_positions, match.contents.size(), true);
99 match.fill_into_edit =
100 AutocompleteInput::FormattedStringWithEquivalentMeaning(
101 url, match.contents, scheme_classifier);
102 if (inline_autocomplete_offset != base::string16::npos) {
103 // |inline_autocomplete_offset| may be beyond the end of the
104 // |fill_into_edit| if the user has typed an URL with a scheme and the
105 // last character typed is a slash. That slash is removed by the
106 // FormatURLWithOffsets call above.
107 if (inline_autocomplete_offset < match.fill_into_edit.length()) {
108 match.inline_autocompletion =
109 match.fill_into_edit.substr(inline_autocomplete_offset);
110 }
111 match.allowed_to_be_default_match =
112 match.inline_autocompletion.empty() ||
113 !HistoryProvider::PreventInlineAutocomplete(input);
114 }
115 match.description = title;
116 match.description_class = ClassificationsFromMatchPositions(
117 titled_url_match.title_match_positions, match.description.size(), false);
118
119 return match;
120 }
121
122 void CorrectTitleAndMatchPositions(
123 base::string16* title,
124 TitledUrlMatch::MatchPositions* title_match_positions) {
125 size_t leading_whitespace_chars = title->length();
126 base::TrimWhitespace(*title, base::TRIM_LEADING, title);
127 leading_whitespace_chars -= title->length();
128 if (leading_whitespace_chars == 0)
129 return;
130 for (TitledUrlMatch::MatchPositions::iterator it =
131 title_match_positions->begin();
132 it != title_match_positions->end(); ++it) {
133 it->first -= leading_whitespace_chars;
134 it->second -= leading_whitespace_chars;
135 }
136 }
137
138 } // namespace bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698