OLD | NEW |
---|---|
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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 field_trial_triggered_ |= triggered; | 238 field_trial_triggered_ |= triggered; |
239 field_trial_triggered_in_session_ |= triggered; | 239 field_trial_triggered_in_session_ |= triggered; |
240 } | 240 } |
241 | 241 |
242 // Clear the previous results now that new results are available. | 242 // Clear the previous results now that new results are available. |
243 suggest_results->clear(); | 243 suggest_results->clear(); |
244 navigation_results->clear(); | 244 navigation_results->clear(); |
245 | 245 |
246 base::string16 result, title; | 246 base::string16 result, title; |
247 std::string type; | 247 std::string type; |
248 const base::string16 current_query_string16 = | |
249 base::ASCIIToUTF16(current_query_); | |
msw
2014/01/06 22:50:21
Use UTF8ToUTF16; or is |current_query_| really ASC
Mark P
2014/01/07 19:10:31
current_query_ is always a URL spec, which means i
| |
248 for (size_t index = 0; results->GetString(index, &result); ++index) { | 250 for (size_t index = 0; results->GetString(index, &result); ++index) { |
249 // Google search may return empty suggestions for weird input characters, | 251 // Google search may return empty suggestions for weird input characters, |
250 // they make no sense at all and can cause problems in our code. | 252 // they make no sense at all and can cause problems in our code. |
251 if (result.empty()) | 253 if (result.empty()) |
252 continue; | 254 continue; |
253 | 255 |
254 int relevance = kDefaultZeroSuggestRelevance; | 256 int relevance = kDefaultZeroSuggestRelevance; |
255 | 257 |
256 // Apply valid suggested relevance scores; discard invalid lists. | 258 // Apply valid suggested relevance scores; discard invalid lists. |
257 if (relevances != NULL && !relevances->GetInteger(index, &relevance)) | 259 if (relevances != NULL && !relevances->GetInteger(index, &relevance)) |
258 relevances = NULL; | 260 relevances = NULL; |
259 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) { | 261 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) { |
260 // Do not blindly trust the URL coming from the server to be valid. | 262 // Do not blindly trust the URL coming from the server to be valid. |
261 GURL url(URLFixerUpper::FixupURL( | 263 GURL url(URLFixerUpper::FixupURL( |
262 base::UTF16ToUTF8(result), std::string())); | 264 base::UTF16ToUTF8(result), std::string())); |
263 if (url.is_valid()) { | 265 if (url.is_valid()) { |
264 if (descriptions != NULL) | 266 if (descriptions != NULL) |
265 descriptions->GetString(index, &title); | 267 descriptions->GetString(index, &title); |
266 navigation_results->push_back(SearchProvider::NavigationResult( | 268 navigation_results->push_back(SearchProvider::NavigationResult( |
267 *this, url, title, false, relevance, relevances != NULL)); | 269 *this, url, title, false, relevance, relevances != NULL)); |
268 } | 270 } |
269 } else { | 271 } else { |
270 suggest_results->push_back(SearchProvider::SuggestResult( | 272 suggest_results->push_back(SearchProvider::SuggestResult( |
271 result, AutocompleteMatchType::SEARCH_SUGGEST, result, | 273 result, AutocompleteMatchType::SEARCH_SUGGEST, result, |
272 base::string16(), std::string(), std::string(), false, relevance, | 274 base::string16(), std::string(), std::string(), false, relevance, |
273 relevances != NULL, false)); | 275 relevances != NULL, false, current_query_string16)); |
274 } | 276 } |
275 } | 277 } |
276 } | 278 } |
277 | 279 |
278 void ZeroSuggestProvider::AddSuggestResultsToMap( | 280 void ZeroSuggestProvider::AddSuggestResultsToMap( |
279 const SearchProvider::SuggestResults& results, | 281 const SearchProvider::SuggestResults& results, |
280 const TemplateURL* template_url, | 282 const TemplateURL* template_url, |
281 SearchProvider::MatchMap* map) { | 283 SearchProvider::MatchMap* map) { |
282 for (size_t i = 0; i < results.size(); ++i) { | 284 for (size_t i = 0; i < results.size(); ++i) { |
283 AddMatchToMap(results[i].relevance(), AutocompleteMatchType::SEARCH_SUGGEST, | 285 AddMatchToMap(results[i].relevance(), AutocompleteMatchType::SEARCH_SUGGEST, |
284 template_url, results[i].suggestion(), i, map); | 286 template_url, results[i].suggestion(), i, map); |
285 } | 287 } |
286 } | 288 } |
287 | 289 |
288 void ZeroSuggestProvider::AddMatchToMap(int relevance, | 290 void ZeroSuggestProvider::AddMatchToMap(int relevance, |
289 AutocompleteMatch::Type type, | 291 AutocompleteMatch::Type type, |
290 const TemplateURL* template_url, | 292 const TemplateURL* template_url, |
291 const base::string16& query_string, | 293 const base::string16& query_string, |
292 int accepted_suggestion, | 294 int accepted_suggestion, |
293 SearchProvider::MatchMap* map) { | 295 SearchProvider::MatchMap* map) { |
296 // Pass in query_string as the input_text to avoid bolding. | |
294 SearchProvider::SuggestResult suggestion( | 297 SearchProvider::SuggestResult suggestion( |
295 query_string, type, query_string, base::string16(), std::string(), | 298 query_string, type, query_string, base::string16(), std::string(), |
296 std::string(), false, relevance, true, false); | 299 std::string(), false, relevance, true, false, query_string); |
297 // Pass in query_string as the input_text since we don't want any bolding. | |
298 // TODO(samarth|melevin): use the actual omnibox margin here as well instead | 300 // TODO(samarth|melevin): use the actual omnibox margin here as well instead |
299 // of passing in -1. | 301 // of passing in -1. |
300 AutocompleteMatch match = SearchProvider::CreateSearchSuggestion( | 302 AutocompleteMatch match = SearchProvider::CreateSearchSuggestion( |
301 this, AutocompleteInput(), query_string, suggestion, template_url, | 303 this, AutocompleteInput(), query_string, suggestion, template_url, |
302 accepted_suggestion, -1, true); | 304 accepted_suggestion, -1, true); |
303 if (!match.destination_url.is_valid()) | 305 if (!match.destination_url.is_valid()) |
304 return; | 306 return; |
305 | 307 |
306 // Try to add |match| to |map|. If a match for |query_string| is already in | 308 // Try to add |match| to |map|. If a match for |query_string| is already in |
307 // |map|, replace it if |match| is more relevant. | 309 // |map|, replace it if |match| is more relevant. |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 match.is_history_what_you_typed_match = false; | 460 match.is_history_what_you_typed_match = false; |
459 match.allowed_to_be_default_match = true; | 461 match.allowed_to_be_default_match = true; |
460 | 462 |
461 // The placeholder suggestion for the current URL has high relevance so | 463 // The placeholder suggestion for the current URL has high relevance so |
462 // that it is in the first suggestion slot and inline autocompleted. It | 464 // that it is in the first suggestion slot and inline autocompleted. It |
463 // gets dropped as soon as the user types something. | 465 // gets dropped as soon as the user types something. |
464 match.relevance = verbatim_relevance_; | 466 match.relevance = verbatim_relevance_; |
465 | 467 |
466 return match; | 468 return match; |
467 } | 469 } |
OLD | NEW |