| 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/autocomplete_result.h" | 5 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_input.h" | 13 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 14 #include "chrome/browser/autocomplete/autocomplete_match.h" | 14 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 15 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 15 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 16 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 16 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 17 #include "chrome/browser/search/search.h" | 17 #include "chrome/browser/search/search.h" |
| 18 #include "chrome/common/autocomplete_match_type.h" | 18 #include "chrome/common/autocomplete_match_type.h" |
| 19 #include "components/metrics/proto/omnibox_input_type.pb.h" | 19 |
| 20 using metrics::OmniboxEventProto; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // This class implements a special version of AutocompleteMatch::MoreRelevant | 24 // This class implements a special version of AutocompleteMatch::MoreRelevant |
| 24 // that allows matches of particular types to be demoted in AutocompleteResult. | 25 // that allows matches of particular types to be demoted in AutocompleteResult. |
| 25 class CompareWithDemoteByType { | 26 class CompareWithDemoteByType { |
| 26 public: | 27 public: |
| 27 CompareWithDemoteByType( | 28 CompareWithDemoteByType( |
| 28 AutocompleteInput::PageClassification current_page_classification); | 29 OmniboxEventProto::PageClassification current_page_classification); |
| 29 | 30 |
| 30 // Returns the relevance score of |match| demoted appropriately by | 31 // Returns the relevance score of |match| demoted appropriately by |
| 31 // |demotions_by_type_|. | 32 // |demotions_by_type_|. |
| 32 int GetDemotedRelevance(const AutocompleteMatch& match); | 33 int GetDemotedRelevance(const AutocompleteMatch& match); |
| 33 | 34 |
| 34 // Comparison function. | 35 // Comparison function. |
| 35 bool operator()(const AutocompleteMatch& elem1, | 36 bool operator()(const AutocompleteMatch& elem1, |
| 36 const AutocompleteMatch& elem2); | 37 const AutocompleteMatch& elem2); |
| 37 | 38 |
| 38 private: | 39 private: |
| 39 OmniboxFieldTrial::DemotionMultipliers demotions_; | 40 OmniboxFieldTrial::DemotionMultipliers demotions_; |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 CompareWithDemoteByType::CompareWithDemoteByType( | 43 CompareWithDemoteByType::CompareWithDemoteByType( |
| 43 AutocompleteInput::PageClassification current_page_classification) { | 44 OmniboxEventProto::PageClassification current_page_classification) { |
| 44 OmniboxFieldTrial::GetDemotionsByType(current_page_classification, | 45 OmniboxFieldTrial::GetDemotionsByType(current_page_classification, |
| 45 &demotions_); | 46 &demotions_); |
| 46 } | 47 } |
| 47 | 48 |
| 48 int CompareWithDemoteByType::GetDemotedRelevance( | 49 int CompareWithDemoteByType::GetDemotedRelevance( |
| 49 const AutocompleteMatch& match) { | 50 const AutocompleteMatch& match) { |
| 50 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = | 51 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = |
| 51 demotions_.find(match.type); | 52 demotions_.find(match.type); |
| 52 return (demotion_it == demotions_.end()) ? | 53 return (demotion_it == demotions_.end()) ? |
| 53 match.relevance : (match.relevance * demotion_it->second); | 54 match.relevance : (match.relevance * demotion_it->second); |
| 54 } | 55 } |
| 55 | 56 |
| 56 bool CompareWithDemoteByType::operator()(const AutocompleteMatch& elem1, | 57 bool CompareWithDemoteByType::operator()(const AutocompleteMatch& elem1, |
| 57 const AutocompleteMatch& elem2) { | 58 const AutocompleteMatch& elem2) { |
| 58 // Compute demoted relevance scores for each match. | 59 // Compute demoted relevance scores for each match. |
| 59 const int demoted_relevance1 = GetDemotedRelevance(elem1); | 60 const int demoted_relevance1 = GetDemotedRelevance(elem1); |
| 60 const int demoted_relevance2 = GetDemotedRelevance(elem2); | 61 const int demoted_relevance2 = GetDemotedRelevance(elem2); |
| 61 // For equal-relevance matches, we sort alphabetically, so that providers | 62 // For equal-relevance matches, we sort alphabetically, so that providers |
| 62 // who return multiple elements at the same priority get a "stable" sort | 63 // who return multiple elements at the same priority get a "stable" sort |
| 63 // across multiple updates. | 64 // across multiple updates. |
| 64 return (demoted_relevance1 == demoted_relevance2) ? | 65 return (demoted_relevance1 == demoted_relevance2) ? |
| 65 (elem1.contents < elem2.contents) : | 66 (elem1.contents < elem2.contents) : |
| 66 (demoted_relevance1 > demoted_relevance2); | 67 (demoted_relevance1 > demoted_relevance2); |
| 67 } | 68 } |
| 68 | 69 |
| 69 class DestinationSort { | 70 class DestinationSort { |
| 70 public: | 71 public: |
| 71 DestinationSort( | 72 DestinationSort( |
| 72 AutocompleteInput::PageClassification current_page_classification); | 73 OmniboxEventProto::PageClassification current_page_classification); |
| 73 bool operator()(const AutocompleteMatch& elem1, | 74 bool operator()(const AutocompleteMatch& elem1, |
| 74 const AutocompleteMatch& elem2); | 75 const AutocompleteMatch& elem2); |
| 75 | 76 |
| 76 private: | 77 private: |
| 77 CompareWithDemoteByType demote_by_type_; | 78 CompareWithDemoteByType demote_by_type_; |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 DestinationSort::DestinationSort( | 81 DestinationSort::DestinationSort( |
| 81 AutocompleteInput::PageClassification current_page_classification) : | 82 OmniboxEventProto::PageClassification current_page_classification) : |
| 82 demote_by_type_(current_page_classification) {} | 83 demote_by_type_(current_page_classification) {} |
| 83 | 84 |
| 84 bool DestinationSort::operator()(const AutocompleteMatch& elem1, | 85 bool DestinationSort::operator()(const AutocompleteMatch& elem1, |
| 85 const AutocompleteMatch& elem2) { | 86 const AutocompleteMatch& elem2) { |
| 86 // Sort identical destination_urls together. Place the most relevant matches | 87 // Sort identical destination_urls together. Place the most relevant matches |
| 87 // first, so that when we call std::unique(), these are the ones that get | 88 // first, so that when we call std::unique(), these are the ones that get |
| 88 // preserved. | 89 // preserved. |
| 89 if (AutocompleteMatch::DestinationsEqual(elem1, elem2) || | 90 if (AutocompleteMatch::DestinationsEqual(elem1, elem2) || |
| 90 (elem1.stripped_destination_url.is_empty() && | 91 (elem1.stripped_destination_url.is_empty() && |
| 91 elem2.stripped_destination_url.is_empty())) { | 92 elem2.stripped_destination_url.is_empty())) { |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 const AutocompleteInput& input, | 358 const AutocompleteInput& input, |
| 358 const AutocompleteMatch& match) { | 359 const AutocompleteMatch& match) { |
| 359 return ((input.type() == metrics::OmniboxInputType::UNKNOWN) && | 360 return ((input.type() == metrics::OmniboxInputType::UNKNOWN) && |
| 360 (AutocompleteMatch::IsSearchType(match.type)) && | 361 (AutocompleteMatch::IsSearchType(match.type)) && |
| 361 (match.transition != content::PAGE_TRANSITION_KEYWORD) && | 362 (match.transition != content::PAGE_TRANSITION_KEYWORD) && |
| 362 (input.canonicalized_url() != match.destination_url)) ? | 363 (input.canonicalized_url() != match.destination_url)) ? |
| 363 input.canonicalized_url() : GURL(); | 364 input.canonicalized_url() : GURL(); |
| 364 } | 365 } |
| 365 | 366 |
| 366 void AutocompleteResult::DedupMatchesByDestination( | 367 void AutocompleteResult::DedupMatchesByDestination( |
| 367 AutocompleteInput::PageClassification page_classification, | 368 OmniboxEventProto::PageClassification page_classification, |
| 368 bool set_duplicate_matches, | 369 bool set_duplicate_matches, |
| 369 ACMatches* matches) { | 370 ACMatches* matches) { |
| 370 DestinationSort destination_sort(page_classification); | 371 DestinationSort destination_sort(page_classification); |
| 371 // Sort matches such that duplicate matches are consecutive. | 372 // Sort matches such that duplicate matches are consecutive. |
| 372 std::sort(matches->begin(), matches->end(), destination_sort); | 373 std::sort(matches->begin(), matches->end(), destination_sort); |
| 373 | 374 |
| 374 if (set_duplicate_matches) { | 375 if (set_duplicate_matches) { |
| 375 // Set duplicate_matches for the first match before erasing duplicate | 376 // Set duplicate_matches for the first match before erasing duplicate |
| 376 // matches. | 377 // matches. |
| 377 for (ACMatches::iterator i(matches->begin()); i != matches->end(); ++i) { | 378 for (ACMatches::iterator i(matches->begin()); i != matches->end(); ++i) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 bool AutocompleteResult::HasMatchByDestination(const AutocompleteMatch& match, | 417 bool AutocompleteResult::HasMatchByDestination(const AutocompleteMatch& match, |
| 417 const ACMatches& matches) { | 418 const ACMatches& matches) { |
| 418 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { | 419 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { |
| 419 if (i->destination_url == match.destination_url) | 420 if (i->destination_url == match.destination_url) |
| 420 return true; | 421 return true; |
| 421 } | 422 } |
| 422 return false; | 423 return false; |
| 423 } | 424 } |
| 424 | 425 |
| 425 void AutocompleteResult::MergeMatchesByProvider( | 426 void AutocompleteResult::MergeMatchesByProvider( |
| 426 AutocompleteInput::PageClassification page_classification, | 427 OmniboxEventProto::PageClassification page_classification, |
| 427 const ACMatches& old_matches, | 428 const ACMatches& old_matches, |
| 428 const ACMatches& new_matches) { | 429 const ACMatches& new_matches) { |
| 429 if (new_matches.size() >= old_matches.size()) | 430 if (new_matches.size() >= old_matches.size()) |
| 430 return; | 431 return; |
| 431 | 432 |
| 432 size_t delta = old_matches.size() - new_matches.size(); | 433 size_t delta = old_matches.size() - new_matches.size(); |
| 433 const int max_relevance = (new_matches.empty() ? | 434 const int max_relevance = (new_matches.empty() ? |
| 434 matches_.front().relevance : new_matches[0].relevance) - 1; | 435 matches_.front().relevance : new_matches[0].relevance) - 1; |
| 435 // Because the goal is a visibly-stable popup, rather than one that preserves | 436 // Because the goal is a visibly-stable popup, rather than one that preserves |
| 436 // the highest-relevance matches, we copy in the lowest-relevance matches | 437 // the highest-relevance matches, we copy in the lowest-relevance matches |
| 437 // first. This means that within each provider's "group" of matches, any | 438 // first. This means that within each provider's "group" of matches, any |
| 438 // synchronous matches (which tend to have the highest scores) will | 439 // synchronous matches (which tend to have the highest scores) will |
| 439 // "overwrite" the initial matches from that provider's previous results, | 440 // "overwrite" the initial matches from that provider's previous results, |
| 440 // minimally disturbing the rest of the matches. | 441 // minimally disturbing the rest of the matches. |
| 441 for (ACMatches::const_reverse_iterator i(old_matches.rbegin()); | 442 for (ACMatches::const_reverse_iterator i(old_matches.rbegin()); |
| 442 i != old_matches.rend() && delta > 0; ++i) { | 443 i != old_matches.rend() && delta > 0; ++i) { |
| 443 if (!HasMatchByDestination(*i, new_matches)) { | 444 if (!HasMatchByDestination(*i, new_matches)) { |
| 444 AutocompleteMatch match = *i; | 445 AutocompleteMatch match = *i; |
| 445 match.relevance = std::min(max_relevance, match.relevance); | 446 match.relevance = std::min(max_relevance, match.relevance); |
| 446 match.from_previous = true; | 447 match.from_previous = true; |
| 447 matches_.push_back(match); | 448 matches_.push_back(match); |
| 448 delta--; | 449 delta--; |
| 449 } | 450 } |
| 450 } | 451 } |
| 451 } | 452 } |
| OLD | NEW |