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

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

Issue 2583763003: Factor out AutocompleteMatch creation from BookmarkProvider (Closed)
Patch Set: changes for mpearson@ 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/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 bookmarks {
Mark P 2017/01/05 20:56:02 nit: prefer putting this at line 49 or so, i.e., n
mattreynolds 2017/01/05 21:21:37 Done.
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 AutocompleteMatch TitledUrlMatchToAutocompleteMatch(
50 const bookmarks::TitledUrlMatch& titled_url_match,
51 AutocompleteMatchType::Type type,
52 int relevance,
53 AutocompleteProvider* provider,
54 const AutocompleteSchemeClassifier& scheme_classifier,
55 const AutocompleteInput& input,
56 const base::string16& fixed_up_input_text) {
57 const GURL& url = titled_url_match.node->GetTitledUrlNodeUrl();
58 base::string16 title = titled_url_match.node->GetTitledUrlNodeTitle();
59
60 // The AutocompleteMatch we construct is non-deletable because the only way to
61 // support this would be to delete the underlying object that created the
62 // titled_url_match. E.g., for the bookmark provider this would mean deleting
63 // the underlying bookmark, which is unlikely to be what the user intends.
64 AutocompleteMatch match(provider, relevance, false, type);
65 bookmarks::TitledUrlMatch::MatchPositions new_title_match_positions =
66 titled_url_match.title_match_positions;
67 bookmarks::TitledUrlMatch::CorrectTitleAndMatchPositions(
68 &title, &new_title_match_positions);
69 const base::string16& url_utf16 = base::UTF8ToUTF16(url.spec());
70 size_t inline_autocomplete_offset = URLPrefix::GetInlineAutocompleteOffset(
71 input.text(), fixed_up_input_text, false, url_utf16);
72 match.destination_url = url;
73 const size_t match_start =
74 titled_url_match.url_match_positions.empty()
75 ? 0
76 : titled_url_match.url_match_positions[0].first;
77 const bool trim_http =
78 !AutocompleteInput::HasHTTPScheme(input.text()) &&
79 ((match_start == base::string16::npos) || (match_start != 0));
80 std::vector<size_t> offsets =
81 bookmarks::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 bookmarks::TitledUrlMatch::MatchPositions new_url_match_positions =
95 bookmarks::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 } // namespace bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698