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

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

Issue 2319033006: Include a page title in the Physical Web omnibox overflow item (Closed)
Patch Set: changes for mpearson@, refactor unit tests Created 4 years, 3 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
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 // 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 // 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 // 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 // counted as a match result for the purposes of the match limit.
25 // 26 //
26 // ex: With kPhysicalWebMaxMatches == 1, there should be at most one suggestion 27 // 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 // 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 for that URL. If there are multiple nearby URLs, the
29 // suggestion will be the overflow item which navigates to the WebUI when 30 // suggestion will be the overflow item which navigates to the WebUI when
30 // tapped. 31 // tapped.
31 static const size_t kPhysicalWebMaxMatches = 1; 32 static const size_t kPhysicalWebMaxMatches = 1;
32 33
33 // Relevance score of the first Physical Web URL autocomplete match. This score 34 // Relevance score of the first Physical Web URL autocomplete match. This score
34 // is intended to be between ClipboardURLProvider and ZeroSuggestProvider. 35 // is intended to be between ClipboardURLProvider and ZeroSuggestProvider.
35 // Subsequent matches should decrease in relevance to preserve the ordering 36 // Subsequent matches should decrease in relevance to preserve the ordering
36 // in the metadata list. 37 // in the metadata list.
37 static const int kPhysicalWebUrlBaseRelevance = 700; 38 static const int kPhysicalWebUrlBaseRelevance = 700;
38 39
40 // The maximum length of the page title's part of the overflow item's
41 // description. Longer titles will be truncated to this length. In a normal
42 // Physical Web match item (non-overflow item) we allow the omnibox display to
43 // truncate the title instead.
44 static const size_t kMaxTitleLength = 15;
Mark P 2016/09/13 23:34:42 Please put overflow in the name somewhere here.
mattreynolds 2016/09/14 19:04:41 Done.
39 } 45 }
40 46
41 // static 47 // static
42 PhysicalWebProvider* PhysicalWebProvider::Create( 48 PhysicalWebProvider* PhysicalWebProvider::Create(
43 AutocompleteProviderClient* client, 49 AutocompleteProviderClient* client,
44 HistoryURLProvider* history_url_provider) { 50 HistoryURLProvider* history_url_provider) {
45 return new PhysicalWebProvider(client, history_url_provider); 51 return new PhysicalWebProvider(client, history_url_provider);
46 } 52 }
47 53
48 void PhysicalWebProvider::Start(const AutocompleteInput& input, 54 void PhysicalWebProvider::Start(const AutocompleteInput& input,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 std::string title_string; 116 std::string title_string;
111 if (!metadata_item->GetString("resolvedUrl", &url_string) || 117 if (!metadata_item->GetString("resolvedUrl", &url_string) ||
112 !metadata_item->GetString("title", &title_string)) { 118 !metadata_item->GetString("title", &title_string)) {
113 continue; 119 continue;
114 } 120 }
115 121
116 // Add match items with decreasing relevance to preserve the ordering in 122 // Add match items with decreasing relevance to preserve the ordering in
117 // the metadata list. 123 // the metadata list.
118 int relevance = kPhysicalWebUrlBaseRelevance - used_slots; 124 int relevance = kPhysicalWebUrlBaseRelevance - used_slots;
119 125
126 base::string16 title =
Mark P 2016/09/13 23:34:43 nit: This goes best right after line 120.
mattreynolds 2016/09/14 19:04:41 Done.
127 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string));
128
120 // Append an overflow item if creating a match for each metadata item would 129 // Append an overflow item if creating a match for each metadata item would
121 // exceed the match limit. 130 // exceed the match limit.
122 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; 131 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots;
123 const size_t remaining_metadata = metadata_count - i; 132 const size_t remaining_metadata = metadata_count - i;
124 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { 133 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) {
125 AppendOverflowItem(remaining_metadata, relevance); 134 AppendOverflowItem(remaining_metadata, relevance, title);
126 return; 135 return;
127 } 136 }
128 137
129 GURL url(url_string); 138 GURL url(url_string);
130 base::string16 title = base::UTF8ToUTF16(title_string);
131 139
132 AutocompleteMatch match(this, relevance, false, 140 AutocompleteMatch match(this, relevance, false,
133 AutocompleteMatchType::PHYSICAL_WEB); 141 AutocompleteMatchType::PHYSICAL_WEB);
134 match.destination_url = url; 142 match.destination_url = url;
135 143
136 // Physical Web results should omit http:// (but not https://) and never 144 // Physical Web results should omit http:// (but not https://) and never
137 // appear bold. 145 // appear bold.
138 match.contents = url_formatter::FormatUrl(url, 146 match.contents = url_formatter::FormatUrl(url,
139 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES, 147 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES,
140 nullptr, nullptr, nullptr); 148 nullptr, nullptr, nullptr);
141 match.contents_class.push_back( 149 match.contents_class.push_back(
142 ACMatchClassification(0, ACMatchClassification::URL)); 150 ACMatchClassification(0, ACMatchClassification::URL));
143 151
144 match.fill_into_edit = 152 match.fill_into_edit =
145 AutocompleteInput::FormattedStringWithEquivalentMeaning( 153 AutocompleteInput::FormattedStringWithEquivalentMeaning(
146 url, match.contents, client_->GetSchemeClassifier()); 154 url, match.contents, client_->GetSchemeClassifier());
147 155
148 match.description = AutocompleteMatch::SanitizeString(title); 156 match.description = title;
149 match.description_class.push_back( 157 match.description_class.push_back(
150 ACMatchClassification(0, ACMatchClassification::NONE)); 158 ACMatchClassification(0, ACMatchClassification::NONE));
151 159
152 matches_.push_back(match); 160 matches_.push_back(match);
153 ++used_slots; 161 ++used_slots;
154 } 162 }
155 } 163 }
156 164
157 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count, 165 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count,
158 int relevance) { 166 int relevance,
167 const base::string16& title) {
159 std::string url_string = "chrome://physical-web"; 168 std::string url_string = "chrome://physical-web";
160 GURL url(url_string); 169 GURL url(url_string);
161 170
162 AutocompleteMatch match(this, relevance, false, 171 AutocompleteMatch match(this, relevance, false,
163 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW); 172 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW);
164 match.destination_url = url; 173 match.destination_url = url;
165 174
166 // Don't omit the chrome:// scheme when displaying the WebUI URL. 175 base::string16 contents_title =
167 match.contents = url_formatter::FormatUrl(url, 176 gfx::TruncateString(title, kMaxTitleLength, gfx::CHARACTER_BREAK);
168 url_formatter::kFormatUrlOmitNothing, net::UnescapeRule::SPACES, 177 base::string16 contents;
Mark P 2016/09/13 23:34:42 nit: consider setting match.contents directly in t
mattreynolds 2016/09/14 19:04:41 Done.
169 nullptr, nullptr, nullptr); 178 if (contents_title.empty()) {
179 contents = l10n_util::GetPluralStringFUTF16(
180 IDS_PHYSICAL_WEB_OVERFLOW_EMPTY_TITLE, additional_url_count);
181 } else {
182 base::string16 contents_suffix = l10n_util::GetPluralStringFUTF16(
183 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count - 1);
184 contents = contents_title + base::UTF8ToUTF16(" ") + contents_suffix;
185 }
186
187 match.contents = contents;
170 match.contents_class.push_back( 188 match.contents_class.push_back(
171 ACMatchClassification(0, ACMatchClassification::URL)); 189 ACMatchClassification(0, ACMatchClassification::DIM));
172 190
173 match.fill_into_edit = 191 match.fill_into_edit =
174 AutocompleteInput::FormattedStringWithEquivalentMeaning( 192 AutocompleteInput::FormattedStringWithEquivalentMeaning(
175 url, match.contents, client_->GetSchemeClassifier()); 193 url, match.contents, client_->GetSchemeClassifier());
176 194
177 match.description = l10n_util::GetPluralStringFUTF16( 195 match.description =
178 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count); 196 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION);
179 match.description_class.push_back( 197 match.description_class.push_back(
180 ACMatchClassification(0, ACMatchClassification::NONE)); 198 ACMatchClassification(0, ACMatchClassification::NONE));
181 199
182 matches_.push_back(match); 200 matches_.push_back(match);
183 } 201 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698