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

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

Issue 2635613002: Replace deprecated calls to GetMetadata in Physical Web omnibox provider (Closed)
Patch Set: fix tests 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
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 "components/omnibox/browser/physical_web_provider.h" 5 #include "components/omnibox/browser/physical_web_provider.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "components/bookmarks/browser/titled_url_index.h" 11 #include "components/bookmarks/browser/titled_url_index.h"
13 #include "components/bookmarks/browser/titled_url_node_sorter.h" 12 #include "components/bookmarks/browser/titled_url_node_sorter.h"
14 #include "components/omnibox/browser/autocomplete_provider_client.h" 13 #include "components/omnibox/browser/autocomplete_provider_client.h"
15 #include "components/omnibox/browser/autocomplete_provider_listener.h" 14 #include "components/omnibox/browser/autocomplete_provider_listener.h"
16 #include "components/omnibox/browser/history_url_provider.h" 15 #include "components/omnibox/browser/history_url_provider.h"
17 #include "components/omnibox/browser/omnibox_field_trial.h" 16 #include "components/omnibox/browser/omnibox_field_trial.h"
18 #include "components/omnibox/browser/physical_web_node.h" 17 #include "components/omnibox/browser/physical_web_node.h"
19 #include "components/omnibox/browser/titled_url_match_utils.h" 18 #include "components/omnibox/browser/titled_url_match_utils.h"
20 #include "components/omnibox/browser/url_prefix.h" 19 #include "components/omnibox/browser/url_prefix.h"
21 #include "components/omnibox/browser/verbatim_match.h" 20 #include "components/omnibox/browser/verbatim_match.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 69
71 physical_web::PhysicalWebDataSource* data_source = 70 physical_web::PhysicalWebDataSource* data_source =
72 client_->GetPhysicalWebDataSource(); 71 client_->GetPhysicalWebDataSource();
73 if (!data_source) { 72 if (!data_source) {
74 done_ = true; 73 done_ = true;
75 nearby_url_count_ = 0; 74 nearby_url_count_ = 0;
76 return; 75 return;
77 } 76 }
78 77
79 if (input.from_omnibox_focus()) { 78 if (input.from_omnibox_focus()) {
80 ConstructZeroSuggestMatches(data_source->GetMetadata()); 79 ConstructZeroSuggestMatches(data_source->GetMetadataList());
81 80
82 if (!matches_.empty()) { 81 if (!matches_.empty()) {
83 had_physical_web_suggestions_ = true; 82 had_physical_web_suggestions_ = true;
84 had_physical_web_suggestions_at_focus_or_later_ = true; 83 had_physical_web_suggestions_at_focus_or_later_ = true;
85 } 84 }
86 85
87 if (!zero_suggest_enabled_) { 86 if (!zero_suggest_enabled_) {
88 matches_.clear(); 87 matches_.clear();
89 } 88 }
90 89
91 // In zero-suggest, Physical Web matches should never be default. If the 90 // In zero-suggest, Physical Web matches should never be default. If the
92 // omnibox input is non-empty and we have at least one Physical Web match, 91 // omnibox input is non-empty and we have at least one Physical Web match,
93 // add the current URL as the default so that hitting enter after focusing 92 // add the current URL as the default so that hitting enter after focusing
94 // the omnibox causes the current page to reload. If the input field is 93 // the omnibox causes the current page to reload. If the input field is
95 // empty, no default match is required. 94 // empty, no default match is required.
96 if (!matches_.empty() && !input.text().empty()) { 95 if (!matches_.empty() && !input.text().empty()) {
97 matches_.push_back(VerbatimMatchForURL( 96 matches_.push_back(VerbatimMatchForURL(
98 client_, input, input.current_url(), history_url_provider_, -1)); 97 client_, input, input.current_url(), history_url_provider_, -1));
99 } 98 }
100 } else { 99 } else {
101 ConstructQuerySuggestMatches(data_source->GetMetadata(), input); 100 ConstructQuerySuggestMatches(data_source->GetMetadataList(), input);
102 101
103 if (!matches_.empty()) { 102 if (!matches_.empty()) {
104 had_physical_web_suggestions_ = true; 103 had_physical_web_suggestions_ = true;
105 had_physical_web_suggestions_at_focus_or_later_ = true; 104 had_physical_web_suggestions_at_focus_or_later_ = true;
106 } 105 }
107 106
108 if (!after_typing_enabled_) { 107 if (!after_typing_enabled_) {
109 matches_.clear(); 108 matches_.clear();
110 } 109 }
111 } 110 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), 154 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()),
156 zero_suggest_base_relevance_( 155 zero_suggest_base_relevance_(
157 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), 156 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()),
158 after_typing_base_relevance_( 157 after_typing_base_relevance_(
159 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()) {} 158 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()) {}
160 159
161 PhysicalWebProvider::~PhysicalWebProvider() { 160 PhysicalWebProvider::~PhysicalWebProvider() {
162 } 161 }
163 162
164 void PhysicalWebProvider::ConstructZeroSuggestMatches( 163 void PhysicalWebProvider::ConstructZeroSuggestMatches(
165 std::unique_ptr<base::ListValue> metadata_list) { 164 std::unique_ptr<physical_web::MetadataList> metadata_list) {
166 nearby_url_count_ = metadata_list->GetSize(); 165 nearby_url_count_ = metadata_list->size();
167 size_t used_slots = 0; 166 size_t used_slots = 0;
168 167
169 for (size_t i = 0; i < nearby_url_count_; ++i) { 168 for (size_t i = 0; i < nearby_url_count_; ++i) {
170 base::DictionaryValue* metadata_item = NULL; 169 const auto& metadata_item = (*metadata_list)[i];
171 if (!metadata_list->GetDictionary(i, &metadata_item)) { 170 std::string url_string = metadata_item.resolved_url.spec();
172 continue; 171 std::string title_string = metadata_item.title;
173 }
174
175 std::string url_string;
176 std::string title_string;
177 if (!metadata_item->GetString(physical_web::kResolvedUrlKey, &url_string) ||
178 !metadata_item->GetString(physical_web::kTitleKey, &title_string)) {
179 continue;
180 }
181 base::string16 title = 172 base::string16 title =
182 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); 173 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string));
183 174
184 // Add match items with decreasing relevance to preserve the ordering in 175 // Add match items with decreasing relevance to preserve the ordering in
185 // the metadata list. 176 // the metadata list.
186 int relevance = zero_suggest_base_relevance_ - used_slots; 177 int relevance = zero_suggest_base_relevance_ - used_slots;
187 178
188 // Append an overflow item if creating a match for each metadata item would 179 // Append an overflow item if creating a match for each metadata item would
189 // exceed the match limit. 180 // exceed the match limit.
190 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; 181 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots;
(...skipping 27 matching lines...) Expand all
218 209
219 matches_.push_back(match); 210 matches_.push_back(match);
220 ++used_slots; 211 ++used_slots;
221 } 212 }
222 213
223 UMA_HISTOGRAM_EXACT_LINEAR( 214 UMA_HISTOGRAM_EXACT_LINEAR(
224 "Omnibox.PhysicalWebProviderMatches", matches_.size(), kMaxMatches); 215 "Omnibox.PhysicalWebProviderMatches", matches_.size(), kMaxMatches);
225 } 216 }
226 217
227 void PhysicalWebProvider::ConstructQuerySuggestMatches( 218 void PhysicalWebProvider::ConstructQuerySuggestMatches(
228 std::unique_ptr<base::ListValue> metadata_list, 219 std::unique_ptr<physical_web::MetadataList> metadata_list,
229 const AutocompleteInput& input) { 220 const AutocompleteInput& input) {
230 // Passing nullptr for the TitledUrlNodeSorter will cause the returned match 221 // Passing nullptr for the TitledUrlNodeSorter will cause the returned match
231 // list to be unsorted. 222 // list to be unsorted.
232 bookmarks::TitledUrlIndex index(nullptr); 223 bookmarks::TitledUrlIndex index(nullptr);
233 std::vector<std::unique_ptr<PhysicalWebNode>> nodes; 224 std::vector<std::unique_ptr<PhysicalWebNode>> nodes;
234 const size_t metadata_count = metadata_list->GetSize(); 225 for (const auto& metadata_item : *metadata_list) {
235 for (size_t i = 0; i < metadata_count; ++i) { 226 nodes.push_back(base::MakeUnique<PhysicalWebNode>(metadata_item));
236 base::DictionaryValue* metadata_item = NULL; 227 index.Add(nodes.back().get());
237 if (metadata_list->GetDictionary(i, &metadata_item)) {
238 nodes.push_back(base::MakeUnique<PhysicalWebNode>(*metadata_item));
239 index.Add(nodes.back().get());
240 }
241 } 228 }
242 229
243 std::vector<bookmarks::TitledUrlMatch> titled_url_matches; 230 std::vector<bookmarks::TitledUrlMatch> titled_url_matches;
244 index.GetResultsMatching(input.text(), kPhysicalWebIndexMaxMatches, 231 index.GetResultsMatching(input.text(), kPhysicalWebIndexMaxMatches,
245 query_parser::MatchingAlgorithm::DEFAULT, 232 query_parser::MatchingAlgorithm::DEFAULT,
246 &titled_url_matches); 233 &titled_url_matches);
247 234
248 size_t used_slots = 0; 235 size_t used_slots = 0;
249 const base::string16 fixed_up_input(FixupUserInput(input).second); 236 const base::string16 fixed_up_input(FixupUserInput(input).second);
250 for (auto titled_url_match : titled_url_matches) { 237 for (auto titled_url_match : titled_url_matches) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 AutocompleteInput::FormattedStringWithEquivalentMeaning( 273 AutocompleteInput::FormattedStringWithEquivalentMeaning(
287 url, match.contents, client_->GetSchemeClassifier()); 274 url, match.contents, client_->GetSchemeClassifier());
288 275
289 match.description = 276 match.description =
290 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); 277 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION);
291 match.description_class.push_back( 278 match.description_class.push_back(
292 ACMatchClassification(0, ACMatchClassification::NONE)); 279 ACMatchClassification(0, ACMatchClassification::NONE));
293 280
294 matches_.push_back(match); 281 matches_.push_back(match);
295 } 282 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/physical_web_provider.h ('k') | components/omnibox/browser/physical_web_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698