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

Side by Side Diff: chrome/browser/autocomplete/zero_suggest_provider.cc

Issue 158053002: Part 4 of search provider refactoring. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove static comment Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/zero_suggest_provider.h" 5 #include "chrome/browser/autocomplete/zero_suggest_provider.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/i18n/case_conversion.h" 8 #include "base/i18n/case_conversion.h"
9 #include "base/json/json_string_value_serializer.h" 9 #include "base/json/json_string_value_serializer.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 AutocompleteProvider::TYPE_ZERO_SUGGEST), 168 AutocompleteProvider::TYPE_ZERO_SUGGEST),
169 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), 169 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)),
170 have_pending_request_(false), 170 have_pending_request_(false),
171 verbatim_relevance_(kDefaultVerbatimZeroSuggestRelevance), 171 verbatim_relevance_(kDefaultVerbatimZeroSuggestRelevance),
172 weak_ptr_factory_(this) { 172 weak_ptr_factory_(this) {
173 } 173 }
174 174
175 ZeroSuggestProvider::~ZeroSuggestProvider() { 175 ZeroSuggestProvider::~ZeroSuggestProvider() {
176 } 176 }
177 177
178 bool ZeroSuggestProvider::ShouldAppendExtraParams(
Mark P 2014/02/12 23:35:49 It would be nice to comment why this is true. (Ye
Maria 2014/02/13 21:07:36 Done.
179 const SuggestResult& result) const {
180 return true;
181 }
182
178 void ZeroSuggestProvider::FillResults(const base::Value& root_val, 183 void ZeroSuggestProvider::FillResults(const base::Value& root_val,
179 int* verbatim_relevance, 184 int* verbatim_relevance,
180 SuggestResults* suggest_results, 185 SuggestResults* suggest_results,
181 NavigationResults* navigation_results) { 186 NavigationResults* navigation_results) {
182 base::string16 query; 187 base::string16 query;
183 const base::ListValue* root_list = NULL; 188 const base::ListValue* root_list = NULL;
184 const base::ListValue* results = NULL; 189 const base::ListValue* results = NULL;
185 const base::ListValue* relevances = NULL; 190 const base::ListValue* relevances = NULL;
186 // The response includes the query, which should be empty for ZeroSuggest 191 // The response includes the query, which should be empty for ZeroSuggest
187 // responses. 192 // responses.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 base::string16(), std::string(), std::string(), false, relevance, 260 base::string16(), std::string(), std::string(), false, relevance,
256 relevances != NULL, false, current_query_string16)); 261 relevances != NULL, false, current_query_string16));
257 } 262 }
258 } 263 }
259 } 264 }
260 265
261 void ZeroSuggestProvider::AddSuggestResultsToMap( 266 void ZeroSuggestProvider::AddSuggestResultsToMap(
262 const SuggestResults& results, 267 const SuggestResults& results,
263 const TemplateURL* template_url, 268 const TemplateURL* template_url,
264 MatchMap* map) { 269 MatchMap* map) {
270 AutocompleteInput input;
265 for (size_t i = 0; i < results.size(); ++i) { 271 for (size_t i = 0; i < results.size(); ++i) {
266 AddMatchToMap(results[i].relevance(), AutocompleteMatchType::SEARCH_SUGGEST, 272 const base::string16& query_string(results[i].suggestion());
267 template_url, results[i].suggestion(), i, map); 273 const SuggestResult suggestion(
Mark P 2014/02/12 23:35:49 Can you make a TODO in a later changelist to clean
H Fung 2014/02/13 01:43:33 Thanks for noticing, Mark. What's the reason for
Mark P 2014/02/13 01:45:30 I don't recall. It may simply have to do with the
Maria 2014/02/13 21:07:36 Done.
274 query_string, AutocompleteMatchType::SEARCH_SUGGEST, query_string,
275 base::string16(), std::string(), std::string(), false,
276 results[i].relevance(), true, false, query_string);
277 // Set input's text to be query_string to avoid bolding
msw 2014/02/13 02:43:50 nit: |input|'s and |query_string|; add a period at
Maria 2014/02/13 21:07:36 Done.
278 input.UpdateText(query_string, base::string16::npos, input.parts());
279 AddMatchToMap(suggestion, input, template_url, std::string(), i, map);
268 } 280 }
269 } 281 }
270 282
271 void ZeroSuggestProvider::AddMatchToMap(int relevance,
272 AutocompleteMatch::Type type,
273 const TemplateURL* template_url,
274 const base::string16& query_string,
275 int accepted_suggestion,
276 MatchMap* map) {
277 // Pass in query_string as the input_text to avoid bolding.
278 SuggestResult suggestion(
279 query_string, type, query_string, base::string16(), std::string(),
280 std::string(), false, relevance, true, false, query_string);
281 // TODO(samarth|melevin): use the actual omnibox margin here as well instead
282 // of passing in -1.
283 AutocompleteMatch match = CreateSearchSuggestion(this, AutocompleteInput(),
284 query_string, suggestion, template_url, accepted_suggestion, -1, true);
285 if (!match.destination_url.is_valid())
286 return;
287
288 // Try to add |match| to |map|. If a match for |query_string| is already in
289 // |map|, replace it if |match| is more relevant.
290 // NOTE: Keep this ToLower() call in sync with url_database.cc.
291 MatchKey match_key(
292 std::make_pair(base::i18n::ToLower(query_string), std::string()));
293 const std::pair<MatchMap::iterator, bool> i(map->insert(
294 std::make_pair(match_key, match)));
295 // NOTE: We purposefully do a direct relevance comparison here instead of
296 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
297 // first" rather than "items alphabetically first" when the scores are equal.
298 // The only case this matters is when a user has results with the same score
299 // that differ only by capitalization; because the history system returns
300 // results sorted by recency, this means we'll pick the most recent such
301 // result even if the precision of our relevance score is too low to
302 // distinguish the two.
303 if (!i.second && (match.relevance > i.first->second.relevance))
304 i.first->second = match;
305 }
306
307 AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( 283 AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
308 const NavigationResult& navigation) { 284 const NavigationResult& navigation) {
309 AutocompleteMatch match(this, navigation.relevance(), false, 285 AutocompleteMatch match(this, navigation.relevance(), false,
310 AutocompleteMatchType::NAVSUGGEST); 286 AutocompleteMatchType::NAVSUGGEST);
311 match.destination_url = navigation.url(); 287 match.destination_url = navigation.url();
312 288
313 // Zero suggest results should always omit protocols and never appear bold. 289 // Zero suggest results should always omit protocols and never appear bold.
314 const std::string languages( 290 const std::string languages(
315 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); 291 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
316 match.contents = net::FormatUrl(navigation.url(), languages, 292 match.contents = net::FormatUrl(navigation.url(), languages,
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 match.is_history_what_you_typed_match = false; 421 match.is_history_what_you_typed_match = false;
446 match.allowed_to_be_default_match = true; 422 match.allowed_to_be_default_match = true;
447 423
448 // The placeholder suggestion for the current URL has high relevance so 424 // The placeholder suggestion for the current URL has high relevance so
449 // that it is in the first suggestion slot and inline autocompleted. It 425 // that it is in the first suggestion slot and inline autocompleted. It
450 // gets dropped as soon as the user types something. 426 // gets dropped as soon as the user types something.
451 match.relevance = verbatim_relevance_; 427 match.relevance = verbatim_relevance_;
452 428
453 return match; 429 return match;
454 } 430 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698