OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 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 | 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 "base/memory/ptr_util.h" | 5 #include "base/memory/ptr_util.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "components/omnibox/browser/autocomplete_provider_client.h" | 8 #include "components/omnibox/browser/autocomplete_provider_client.h" |
9 #include "components/omnibox/browser/autocomplete_provider_listener.h" | 9 #include "components/omnibox/browser/autocomplete_provider_listener.h" |
10 #include "components/omnibox/browser/history_url_provider.h" | 10 #include "components/omnibox/browser/history_url_provider.h" |
11 #include "components/omnibox/browser/physical_web_provider.h" | 11 #include "components/omnibox/browser/physical_web_provider.h" |
12 #include "components/omnibox/browser/verbatim_match.h" | 12 #include "components/omnibox/browser/verbatim_match.h" |
13 #include "components/physical_web/data_source/physical_web_data_source.h" | 13 #include "components/physical_web/data_source/physical_web_data_source.h" |
14 #include "components/url_formatter/url_formatter.h" | 14 #include "components/url_formatter/url_formatter.h" |
15 #include "grit/components_strings.h" | 15 #include "grit/components_strings.h" |
16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/gfx/text_elider.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // The maximum number of match results to provide. If the number of nearby URLs | |
22 // exceeds this limit, an overflow item is created. Tapping the overflow item | |
23 // navigates to a page with the full list of nearby URLs. The overflow item is | |
24 // counted as a match result for the purposes of the match limit. | |
25 // | |
26 // ex: With kPhysicalWebMaxMatches == 1, there should be at most one suggestion | |
27 // created by this provider. If there is a single nearby URL, then the | |
28 // suggestion will be for that URL. If there are multiple nearby URLs, the | |
29 // suggestion will be the overflow item which navigates to the WebUI when | |
30 // tapped. | |
31 static const size_t kPhysicalWebMaxMatches = 1; | |
32 | |
33 // Relevance score of the first Physical Web URL autocomplete match. This score | 22 // Relevance score of the first Physical Web URL autocomplete match. This score |
34 // is intended to be between ClipboardURLProvider and ZeroSuggestProvider. | 23 // is intended to be between ClipboardURLProvider and ZeroSuggestProvider. |
35 // Subsequent matches should decrease in relevance to preserve the ordering | 24 // Subsequent matches should decrease in relevance to preserve the ordering |
36 // in the metadata list. | 25 // in the metadata list. |
37 static const int kPhysicalWebUrlBaseRelevance = 700; | 26 static const int kPhysicalWebUrlBaseRelevance = 700; |
38 | 27 |
| 28 // The maximum length of the page title's part of the overflow item's |
| 29 // description. Longer titles will be truncated to this length. In a normal |
| 30 // Physical Web match item (non-overflow item) we allow the omnibox display to |
| 31 // truncate the title instead. |
| 32 static const size_t kMaxTitleLengthInOverflow = 15; |
39 } | 33 } |
40 | 34 |
41 // static | 35 // static |
| 36 const size_t PhysicalWebProvider::kPhysicalWebMaxMatches = 1; |
| 37 |
| 38 // static |
42 PhysicalWebProvider* PhysicalWebProvider::Create( | 39 PhysicalWebProvider* PhysicalWebProvider::Create( |
43 AutocompleteProviderClient* client, | 40 AutocompleteProviderClient* client, |
44 HistoryURLProvider* history_url_provider) { | 41 HistoryURLProvider* history_url_provider) { |
45 return new PhysicalWebProvider(client, history_url_provider); | 42 return new PhysicalWebProvider(client, history_url_provider); |
46 } | 43 } |
47 | 44 |
48 void PhysicalWebProvider::Start(const AutocompleteInput& input, | 45 void PhysicalWebProvider::Start(const AutocompleteInput& input, |
49 bool minimal_changes) { | 46 bool minimal_changes) { |
50 DCHECK(kPhysicalWebMaxMatches < kMaxMatches); | 47 DCHECK(kPhysicalWebMaxMatches < kMaxMatches); |
51 | 48 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 if (!metadata_list->GetDictionary(i, &metadata_item)) { | 102 if (!metadata_list->GetDictionary(i, &metadata_item)) { |
106 continue; | 103 continue; |
107 } | 104 } |
108 | 105 |
109 std::string url_string; | 106 std::string url_string; |
110 std::string title_string; | 107 std::string title_string; |
111 if (!metadata_item->GetString("resolvedUrl", &url_string) || | 108 if (!metadata_item->GetString("resolvedUrl", &url_string) || |
112 !metadata_item->GetString("title", &title_string)) { | 109 !metadata_item->GetString("title", &title_string)) { |
113 continue; | 110 continue; |
114 } | 111 } |
| 112 base::string16 title = |
| 113 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); |
115 | 114 |
116 // Add match items with decreasing relevance to preserve the ordering in | 115 // Add match items with decreasing relevance to preserve the ordering in |
117 // the metadata list. | 116 // the metadata list. |
118 int relevance = kPhysicalWebUrlBaseRelevance - used_slots; | 117 int relevance = kPhysicalWebUrlBaseRelevance - used_slots; |
119 | 118 |
120 // Append an overflow item if creating a match for each metadata item would | 119 // Append an overflow item if creating a match for each metadata item would |
121 // exceed the match limit. | 120 // exceed the match limit. |
122 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; | 121 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; |
123 const size_t remaining_metadata = metadata_count - i; | 122 const size_t remaining_metadata = metadata_count - i; |
124 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { | 123 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { |
125 AppendOverflowItem(remaining_metadata, relevance); | 124 AppendOverflowItem(remaining_metadata, relevance, title); |
126 return; | 125 return; |
127 } | 126 } |
128 | 127 |
129 GURL url(url_string); | 128 GURL url(url_string); |
130 base::string16 title = base::UTF8ToUTF16(title_string); | |
131 | 129 |
132 AutocompleteMatch match(this, relevance, false, | 130 AutocompleteMatch match(this, relevance, false, |
133 AutocompleteMatchType::PHYSICAL_WEB); | 131 AutocompleteMatchType::PHYSICAL_WEB); |
134 match.destination_url = url; | 132 match.destination_url = url; |
135 | 133 |
136 // Physical Web results should omit http:// (but not https://) and never | 134 // Physical Web results should omit http:// (but not https://) and never |
137 // appear bold. | 135 // appear bold. |
138 match.contents = url_formatter::FormatUrl(url, | 136 match.contents = url_formatter::FormatUrl(url, |
139 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES, | 137 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES, |
140 nullptr, nullptr, nullptr); | 138 nullptr, nullptr, nullptr); |
141 match.contents_class.push_back( | 139 match.contents_class.push_back( |
142 ACMatchClassification(0, ACMatchClassification::URL)); | 140 ACMatchClassification(0, ACMatchClassification::URL)); |
143 | 141 |
144 match.fill_into_edit = | 142 match.fill_into_edit = |
145 AutocompleteInput::FormattedStringWithEquivalentMeaning( | 143 AutocompleteInput::FormattedStringWithEquivalentMeaning( |
146 url, match.contents, client_->GetSchemeClassifier()); | 144 url, match.contents, client_->GetSchemeClassifier()); |
147 | 145 |
148 match.description = AutocompleteMatch::SanitizeString(title); | 146 match.description = title; |
149 match.description_class.push_back( | 147 match.description_class.push_back( |
150 ACMatchClassification(0, ACMatchClassification::NONE)); | 148 ACMatchClassification(0, ACMatchClassification::NONE)); |
151 | 149 |
152 matches_.push_back(match); | 150 matches_.push_back(match); |
153 ++used_slots; | 151 ++used_slots; |
154 } | 152 } |
155 } | 153 } |
156 | 154 |
157 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count, | 155 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count, |
158 int relevance) { | 156 int relevance, |
| 157 const base::string16& title) { |
159 std::string url_string = "chrome://physical-web"; | 158 std::string url_string = "chrome://physical-web"; |
160 GURL url(url_string); | 159 GURL url(url_string); |
161 | 160 |
162 AutocompleteMatch match(this, relevance, false, | 161 AutocompleteMatch match(this, relevance, false, |
163 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW); | 162 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW); |
164 match.destination_url = url; | 163 match.destination_url = url; |
165 | 164 |
166 // Don't omit the chrome:// scheme when displaying the WebUI URL. | 165 base::string16 contents_title = gfx::TruncateString( |
167 match.contents = url_formatter::FormatUrl(url, | 166 title, kMaxTitleLengthInOverflow, gfx::CHARACTER_BREAK); |
168 url_formatter::kFormatUrlOmitNothing, net::UnescapeRule::SPACES, | 167 if (contents_title.empty()) { |
169 nullptr, nullptr, nullptr); | 168 match.contents = l10n_util::GetPluralStringFUTF16( |
| 169 IDS_PHYSICAL_WEB_OVERFLOW_EMPTY_TITLE, additional_url_count); |
| 170 } else { |
| 171 base::string16 contents_suffix = l10n_util::GetPluralStringFUTF16( |
| 172 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count - 1); |
| 173 match.contents = contents_title + base::UTF8ToUTF16(" ") + contents_suffix; |
| 174 } |
170 match.contents_class.push_back( | 175 match.contents_class.push_back( |
171 ACMatchClassification(0, ACMatchClassification::URL)); | 176 ACMatchClassification(0, ACMatchClassification::DIM)); |
172 | 177 |
173 match.fill_into_edit = | 178 match.fill_into_edit = |
174 AutocompleteInput::FormattedStringWithEquivalentMeaning( | 179 AutocompleteInput::FormattedStringWithEquivalentMeaning( |
175 url, match.contents, client_->GetSchemeClassifier()); | 180 url, match.contents, client_->GetSchemeClassifier()); |
176 | 181 |
177 match.description = l10n_util::GetPluralStringFUTF16( | 182 match.description = |
178 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count); | 183 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); |
179 match.description_class.push_back( | 184 match.description_class.push_back( |
180 ACMatchClassification(0, ACMatchClassification::NONE)); | 185 ACMatchClassification(0, ACMatchClassification::NONE)); |
181 | 186 |
182 matches_.push_back(match); | 187 matches_.push_back(match); |
183 } | 188 } |
OLD | NEW |