| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/autocomplete_result.h" | 5 #include "components/omnibox/browser/autocomplete_result.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "components/metrics/proto/omnibox_event.pb.h" | 13 #include "components/metrics/proto/omnibox_event.pb.h" |
| 14 #include "components/metrics/proto/omnibox_input_type.pb.h" | 14 #include "components/metrics/proto/omnibox_input_type.pb.h" |
| 15 #include "components/omnibox/browser/autocomplete_input.h" | 15 #include "components/omnibox/browser/autocomplete_input.h" |
| 16 #include "components/omnibox/browser/autocomplete_match.h" | 16 #include "components/omnibox/browser/autocomplete_match.h" |
| 17 #include "components/omnibox/browser/autocomplete_provider.h" | 17 #include "components/omnibox/browser/autocomplete_provider.h" |
| 18 #include "components/omnibox/browser/match_compare.h" |
| 18 #include "components/omnibox/browser/omnibox_field_trial.h" | 19 #include "components/omnibox/browser/omnibox_field_trial.h" |
| 19 #include "components/omnibox/browser/omnibox_switches.h" | 20 #include "components/omnibox/browser/omnibox_switches.h" |
| 20 #include "components/search/search.h" | 21 #include "components/search/search.h" |
| 21 #include "components/url_formatter/url_fixer.h" | 22 #include "components/url_formatter/url_fixer.h" |
| 22 | 23 |
| 23 using metrics::OmniboxEventProto; | 24 using metrics::OmniboxEventProto; |
| 24 | 25 |
| 25 namespace { | |
| 26 | |
| 27 // This class implements a special version of AutocompleteMatch::MoreRelevant | |
| 28 // that allows matches of particular types to be demoted in AutocompleteResult. | |
| 29 class CompareWithDemoteByType { | |
| 30 public: | |
| 31 CompareWithDemoteByType( | |
| 32 OmniboxEventProto::PageClassification current_page_classification); | |
| 33 | |
| 34 // Returns the relevance score of |match| demoted appropriately by | |
| 35 // |demotions_by_type_|. | |
| 36 int GetDemotedRelevance(const AutocompleteMatch& match); | |
| 37 | |
| 38 // Comparison function. | |
| 39 bool operator()(const AutocompleteMatch& elem1, | |
| 40 const AutocompleteMatch& elem2); | |
| 41 | |
| 42 private: | |
| 43 OmniboxFieldTrial::DemotionMultipliers demotions_; | |
| 44 }; | |
| 45 | |
| 46 CompareWithDemoteByType::CompareWithDemoteByType( | |
| 47 OmniboxEventProto::PageClassification current_page_classification) { | |
| 48 OmniboxFieldTrial::GetDemotionsByType(current_page_classification, | |
| 49 &demotions_); | |
| 50 } | |
| 51 | |
| 52 int CompareWithDemoteByType::GetDemotedRelevance( | |
| 53 const AutocompleteMatch& match) { | |
| 54 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = | |
| 55 demotions_.find(match.type); | |
| 56 return (demotion_it == demotions_.end()) ? | |
| 57 match.relevance : (match.relevance * demotion_it->second); | |
| 58 } | |
| 59 | |
| 60 bool CompareWithDemoteByType::operator()(const AutocompleteMatch& elem1, | |
| 61 const AutocompleteMatch& elem2) { | |
| 62 // Compute demoted relevance scores for each match. | |
| 63 const int demoted_relevance1 = GetDemotedRelevance(elem1); | |
| 64 const int demoted_relevance2 = GetDemotedRelevance(elem2); | |
| 65 // For equal-relevance matches, we sort alphabetically, so that providers | |
| 66 // who return multiple elements at the same priority get a "stable" sort | |
| 67 // across multiple updates. | |
| 68 return (demoted_relevance1 == demoted_relevance2) ? | |
| 69 (elem1.contents < elem2.contents) : | |
| 70 (demoted_relevance1 > demoted_relevance2); | |
| 71 } | |
| 72 | |
| 73 class DestinationSort { | |
| 74 public: | |
| 75 DestinationSort( | |
| 76 OmniboxEventProto::PageClassification current_page_classification); | |
| 77 bool operator()(const AutocompleteMatch& elem1, | |
| 78 const AutocompleteMatch& elem2); | |
| 79 | |
| 80 private: | |
| 81 CompareWithDemoteByType demote_by_type_; | |
| 82 }; | |
| 83 | |
| 84 DestinationSort::DestinationSort( | |
| 85 OmniboxEventProto::PageClassification current_page_classification) : | |
| 86 demote_by_type_(current_page_classification) {} | |
| 87 | |
| 88 bool DestinationSort::operator()(const AutocompleteMatch& elem1, | |
| 89 const AutocompleteMatch& elem2) { | |
| 90 // Sort identical destination_urls together. Place the most relevant matches | |
| 91 // first, so that when we call std::unique(), these are the ones that get | |
| 92 // preserved. | |
| 93 if (AutocompleteMatch::DestinationsEqual(elem1, elem2) || | |
| 94 (elem1.stripped_destination_url.is_empty() && | |
| 95 elem2.stripped_destination_url.is_empty())) { | |
| 96 return demote_by_type_(elem1, elem2); | |
| 97 } | |
| 98 return elem1.stripped_destination_url < elem2.stripped_destination_url; | |
| 99 } | |
| 100 | |
| 101 }; // namespace | |
| 102 | |
| 103 // static | 26 // static |
| 104 const size_t AutocompleteResult::kMaxMatches = 6; | 27 const size_t AutocompleteResult::kMaxMatches = 6; |
| 105 | 28 |
| 106 void AutocompleteResult::Selection::Clear() { | 29 void AutocompleteResult::Selection::Clear() { |
| 107 destination_url = GURL(); | 30 destination_url = GURL(); |
| 108 provider_affinity = NULL; | 31 provider_affinity = NULL; |
| 109 is_history_what_you_typed_match = false; | 32 is_history_what_you_typed_match = false; |
| 110 } | 33 } |
| 111 | 34 |
| 112 AutocompleteResult::AutocompleteResult() { | 35 AutocompleteResult::AutocompleteResult() { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 const std::string& languages, | 130 const std::string& languages, |
| 208 TemplateURLService* template_url_service) { | 131 TemplateURLService* template_url_service) { |
| 209 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) | 132 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) |
| 210 i->ComputeStrippedDestinationURL(input, languages, template_url_service); | 133 i->ComputeStrippedDestinationURL(input, languages, template_url_service); |
| 211 | 134 |
| 212 DedupMatchesByDestination(input.current_page_classification(), true, | 135 DedupMatchesByDestination(input.current_page_classification(), true, |
| 213 &matches_); | 136 &matches_); |
| 214 | 137 |
| 215 // Sort and trim to the most relevant kMaxMatches matches. | 138 // Sort and trim to the most relevant kMaxMatches matches. |
| 216 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); | 139 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); |
| 217 CompareWithDemoteByType comparing_object(input.current_page_classification()); | 140 CompareWithDemoteByType<AutocompleteMatch> |
| 141 comparing_object(input.current_page_classification()); |
| 218 std::sort(matches_.begin(), matches_.end(), comparing_object); | 142 std::sort(matches_.begin(), matches_.end(), comparing_object); |
| 219 if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match) { | 143 if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match) { |
| 220 // Top match is not allowed to be the default match. Find the most | 144 // Top match is not allowed to be the default match. Find the most |
| 221 // relevant legal match and shift it to the front. | 145 // relevant legal match and shift it to the front. |
| 222 for (AutocompleteResult::iterator it = matches_.begin() + 1; | 146 for (AutocompleteResult::iterator it = matches_.begin() + 1; |
| 223 it != matches_.end(); ++it) { | 147 it != matches_.end(); ++it) { |
| 224 if (it->allowed_to_be_default_match) { | 148 if (it->allowed_to_be_default_match) { |
| 225 std::rotate(matches_.begin(), it, it + 1); | 149 std::rotate(matches_.begin(), it, it + 1); |
| 226 break; | 150 break; |
| 227 } | 151 } |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 (AutocompleteMatch::IsSearchType(match.type)) && | 289 (AutocompleteMatch::IsSearchType(match.type)) && |
| 366 (match.transition != ui::PAGE_TRANSITION_KEYWORD) && | 290 (match.transition != ui::PAGE_TRANSITION_KEYWORD) && |
| 367 (input.canonicalized_url() != match.destination_url)) ? | 291 (input.canonicalized_url() != match.destination_url)) ? |
| 368 input.canonicalized_url() : GURL(); | 292 input.canonicalized_url() : GURL(); |
| 369 } | 293 } |
| 370 | 294 |
| 371 void AutocompleteResult::DedupMatchesByDestination( | 295 void AutocompleteResult::DedupMatchesByDestination( |
| 372 OmniboxEventProto::PageClassification page_classification, | 296 OmniboxEventProto::PageClassification page_classification, |
| 373 bool set_duplicate_matches, | 297 bool set_duplicate_matches, |
| 374 ACMatches* matches) { | 298 ACMatches* matches) { |
| 375 DestinationSort destination_sort(page_classification); | 299 DestinationSort<AutocompleteMatch> destination_sort(page_classification); |
| 376 // Sort matches such that duplicate matches are consecutive. | 300 // Sort matches such that duplicate matches are consecutive. |
| 377 std::sort(matches->begin(), matches->end(), destination_sort); | 301 std::sort(matches->begin(), matches->end(), destination_sort); |
| 378 | 302 |
| 379 if (set_duplicate_matches) { | 303 if (set_duplicate_matches) { |
| 380 // Set duplicate_matches for the first match before erasing duplicate | 304 // Set duplicate_matches for the first match before erasing duplicate |
| 381 // matches. | 305 // matches. |
| 382 for (ACMatches::iterator i(matches->begin()); i != matches->end(); ++i) { | 306 for (ACMatches::iterator i(matches->begin()); i != matches->end(); ++i) { |
| 383 for (int j = 1; (i + j != matches->end()) && | 307 for (int j = 1; (i + j != matches->end()) && |
| 384 AutocompleteMatch::DestinationsEqual(*i, *(i + j)); ++j) { | 308 AutocompleteMatch::DestinationsEqual(*i, *(i + j)); ++j) { |
| 385 AutocompleteMatch& dup_match(*(i + j)); | 309 AutocompleteMatch& dup_match(*(i + j)); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 i != old_matches.rend() && delta > 0; ++i) { | 388 i != old_matches.rend() && delta > 0; ++i) { |
| 465 if (!HasMatchByDestination(*i, new_matches)) { | 389 if (!HasMatchByDestination(*i, new_matches)) { |
| 466 AutocompleteMatch match = *i; | 390 AutocompleteMatch match = *i; |
| 467 match.relevance = std::min(max_relevance, match.relevance); | 391 match.relevance = std::min(max_relevance, match.relevance); |
| 468 match.from_previous = true; | 392 match.from_previous = true; |
| 469 matches_.push_back(match); | 393 matches_.push_back(match); |
| 470 delta--; | 394 delta--; |
| 471 } | 395 } |
| 472 } | 396 } |
| 473 } | 397 } |
| OLD | NEW |