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