| 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 "chrome/browser/autocomplete/autocomplete_input.h" | 11 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 13 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 14 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 14 #include "chrome/browser/search/search.h" | 15 #include "chrome/browser/search/search.h" |
| 15 #include "chrome/common/autocomplete_match_type.h" | 16 #include "chrome/common/autocomplete_match_type.h" |
| 16 | 17 |
| 18 namespace { |
| 19 |
| 20 // This class implements a special version of AutocompleteMatch::MoreRelevant |
| 21 // that allows matches of particular types to be demoted in AutocompleteResult. |
| 22 class CompareWithDemoteByType { |
| 23 public: |
| 24 CompareWithDemoteByType( |
| 25 AutocompleteInput::PageClassification current_page_classification); |
| 26 |
| 27 // Returns the relevance score of |match| demoted appropriately by |
| 28 // |demotions_by_type_|. |
| 29 int GetDemotedRelevance(const AutocompleteMatch& match); |
| 30 |
| 31 // Comparison function. |
| 32 bool operator()(const AutocompleteMatch& elem1, |
| 33 const AutocompleteMatch& elem2); |
| 34 |
| 35 private: |
| 36 OmniboxFieldTrial::DemotionMultipliers demotions_; |
| 37 }; |
| 38 |
| 39 CompareWithDemoteByType::CompareWithDemoteByType( |
| 40 AutocompleteInput::PageClassification current_page_classification) { |
| 41 OmniboxFieldTrial::GetDemotionsByType(current_page_classification, |
| 42 &demotions_); |
| 43 } |
| 44 |
| 45 int CompareWithDemoteByType::GetDemotedRelevance( |
| 46 const AutocompleteMatch& match) { |
| 47 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = |
| 48 demotions_.find(match.type); |
| 49 return (demotion_it == demotions_.end()) ? |
| 50 match.relevance : (match.relevance * demotion_it->second); |
| 51 } |
| 52 |
| 53 bool CompareWithDemoteByType::operator()(const AutocompleteMatch& elem1, |
| 54 const AutocompleteMatch& elem2) { |
| 55 // Compute demoted relevance scores for each match. |
| 56 const int demoted_relevance1 = GetDemotedRelevance(elem1); |
| 57 const int demoted_relevance2 = GetDemotedRelevance(elem2); |
| 58 // For equal-relevance matches, we sort alphabetically, so that providers |
| 59 // who return multiple elements at the same priority get a "stable" sort |
| 60 // across multiple updates. |
| 61 return (demoted_relevance1 == demoted_relevance2) ? |
| 62 (elem1.contents < elem2.contents) : |
| 63 (demoted_relevance1 > demoted_relevance2); |
| 64 } |
| 65 |
| 66 }; // namespace |
| 67 |
| 17 // static | 68 // static |
| 18 const size_t AutocompleteResult::kMaxMatches = 6; | 69 const size_t AutocompleteResult::kMaxMatches = 6; |
| 19 const int AutocompleteResult::kLowestDefaultScore = 1200; | 70 const int AutocompleteResult::kLowestDefaultScore = 1200; |
| 20 | 71 |
| 21 void AutocompleteResult::Selection::Clear() { | 72 void AutocompleteResult::Selection::Clear() { |
| 22 destination_url = GURL(); | 73 destination_url = GURL(); |
| 23 provider_affinity = NULL; | 74 provider_affinity = NULL; |
| 24 is_history_what_you_typed_match = false; | 75 is_history_what_you_typed_match = false; |
| 25 } | 76 } |
| 26 | 77 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // ultimately be similar. If the assumption holds, this prevents seeing the | 114 // ultimately be similar. If the assumption holds, this prevents seeing the |
| 64 // new low-relevance match appear and then quickly get pushed off the bottom; | 115 // new low-relevance match appear and then quickly get pushed off the bottom; |
| 65 // if it doesn't, then once the providers are done and we expire the old | 116 // if it doesn't, then once the providers are done and we expire the old |
| 66 // matches, the new ones will all become visible, so we won't have lost | 117 // matches, the new ones will all become visible, so we won't have lost |
| 67 // anything permanently. | 118 // anything permanently. |
| 68 ProviderToMatches matches_per_provider, old_matches_per_provider; | 119 ProviderToMatches matches_per_provider, old_matches_per_provider; |
| 69 BuildProviderToMatches(&matches_per_provider); | 120 BuildProviderToMatches(&matches_per_provider); |
| 70 old_matches.BuildProviderToMatches(&old_matches_per_provider); | 121 old_matches.BuildProviderToMatches(&old_matches_per_provider); |
| 71 for (ProviderToMatches::const_iterator i(old_matches_per_provider.begin()); | 122 for (ProviderToMatches::const_iterator i(old_matches_per_provider.begin()); |
| 72 i != old_matches_per_provider.end(); ++i) { | 123 i != old_matches_per_provider.end(); ++i) { |
| 73 MergeMatchesByProvider(i->second, matches_per_provider[i->first]); | 124 MergeMatchesByProvider(input.current_page_classification(), |
| 125 i->second, matches_per_provider[i->first]); |
| 74 } | 126 } |
| 75 | 127 |
| 76 SortAndCull(input, profile); | 128 SortAndCull(input, profile); |
| 77 } | 129 } |
| 78 | 130 |
| 79 void AutocompleteResult::AppendMatches(const ACMatches& matches) { | 131 void AutocompleteResult::AppendMatches(const ACMatches& matches) { |
| 80 #ifndef NDEBUG | 132 #ifndef NDEBUG |
| 81 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { | 133 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { |
| 82 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->contents), i->contents); | 134 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->contents), i->contents); |
| 83 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->description), | 135 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->description), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 95 i->ComputeStrippedDestinationURL(profile); | 147 i->ComputeStrippedDestinationURL(profile); |
| 96 | 148 |
| 97 // Remove duplicates. | 149 // Remove duplicates. |
| 98 std::sort(matches_.begin(), matches_.end(), | 150 std::sort(matches_.begin(), matches_.end(), |
| 99 &AutocompleteMatch::DestinationSortFunc); | 151 &AutocompleteMatch::DestinationSortFunc); |
| 100 matches_.erase(std::unique(matches_.begin(), matches_.end(), | 152 matches_.erase(std::unique(matches_.begin(), matches_.end(), |
| 101 &AutocompleteMatch::DestinationsEqual), | 153 &AutocompleteMatch::DestinationsEqual), |
| 102 matches_.end()); | 154 matches_.end()); |
| 103 | 155 |
| 104 // Sort and trim to the most relevant kMaxMatches matches. | 156 // Sort and trim to the most relevant kMaxMatches matches. |
| 105 const size_t num_matches = std::min(kMaxMatches, matches_.size()); | 157 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); |
| 106 std::partial_sort(matches_.begin(), matches_.begin() + num_matches, | 158 CompareWithDemoteByType comparing_object(input.current_page_classification()); |
| 107 matches_.end(), &AutocompleteMatch::MoreRelevant); | 159 std::partial_sort(matches_.begin(), matches_.begin() + max_num_matches, |
| 160 matches_.end(), comparing_object); |
| 161 // In the process of trimming, drop all matches with a demoted relevance |
| 162 // score of 0. |
| 163 size_t num_matches; |
| 164 for (num_matches = 0u; (num_matches < max_num_matches) && |
| 165 (comparing_object.GetDemotedRelevance(*match_at(num_matches)) > 0); |
| 166 ++num_matches) {} |
| 108 matches_.resize(num_matches); | 167 matches_.resize(num_matches); |
| 109 | 168 |
| 110 default_match_ = begin(); | 169 default_match_ = begin(); |
| 111 | 170 |
| 112 // Set the alternate nav URL. | 171 // Set the alternate nav URL. |
| 113 alternate_nav_url_ = (default_match_ == end()) ? | 172 alternate_nav_url_ = (default_match_ == end()) ? |
| 114 GURL() : ComputeAlternateNavUrl(input, *default_match_); | 173 GURL() : ComputeAlternateNavUrl(input, *default_match_); |
| 115 } | 174 } |
| 116 | 175 |
| 117 bool AutocompleteResult::HasCopiedMatches() const { | 176 bool AutocompleteResult::HasCopiedMatches() const { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 273 |
| 215 matches_ = rhs.matches_; | 274 matches_ = rhs.matches_; |
| 216 // Careful! You can't just copy iterators from another container, you have to | 275 // Careful! You can't just copy iterators from another container, you have to |
| 217 // reconstruct them. | 276 // reconstruct them. |
| 218 default_match_ = (rhs.default_match_ == rhs.end()) ? | 277 default_match_ = (rhs.default_match_ == rhs.end()) ? |
| 219 end() : (begin() + (rhs.default_match_ - rhs.begin())); | 278 end() : (begin() + (rhs.default_match_ - rhs.begin())); |
| 220 | 279 |
| 221 alternate_nav_url_ = rhs.alternate_nav_url_; | 280 alternate_nav_url_ = rhs.alternate_nav_url_; |
| 222 } | 281 } |
| 223 | 282 |
| 224 void AutocompleteResult::AddMatch(const AutocompleteMatch& match) { | 283 void AutocompleteResult::AddMatch( |
| 284 AutocompleteInput::PageClassification page_classification, |
| 285 const AutocompleteMatch& match) { |
| 225 DCHECK(default_match_ != end()); | 286 DCHECK(default_match_ != end()); |
| 226 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); | 287 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); |
| 227 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), | 288 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), |
| 228 match.description); | 289 match.description); |
| 290 CompareWithDemoteByType comparing_object(page_classification); |
| 229 ACMatches::iterator insertion_point = | 291 ACMatches::iterator insertion_point = |
| 230 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); | 292 std::upper_bound(begin(), end(), match, comparing_object); |
| 231 matches_difference_type default_offset = default_match_ - begin(); | 293 matches_difference_type default_offset = default_match_ - begin(); |
| 232 if ((insertion_point - begin()) <= default_offset) | 294 if ((insertion_point - begin()) <= default_offset) |
| 233 ++default_offset; | 295 ++default_offset; |
| 234 matches_.insert(insertion_point, match); | 296 matches_.insert(insertion_point, match); |
| 235 default_match_ = begin() + default_offset; | 297 default_match_ = begin() + default_offset; |
| 236 } | 298 } |
| 237 | 299 |
| 238 void AutocompleteResult::BuildProviderToMatches( | 300 void AutocompleteResult::BuildProviderToMatches( |
| 239 ProviderToMatches* provider_to_matches) const { | 301 ProviderToMatches* provider_to_matches) const { |
| 240 for (ACMatches::const_iterator i(begin()); i != end(); ++i) | 302 for (ACMatches::const_iterator i(begin()); i != end(); ++i) |
| 241 (*provider_to_matches)[i->provider].push_back(*i); | 303 (*provider_to_matches)[i->provider].push_back(*i); |
| 242 } | 304 } |
| 243 | 305 |
| 244 // static | 306 // static |
| 245 bool AutocompleteResult::HasMatchByDestination(const AutocompleteMatch& match, | 307 bool AutocompleteResult::HasMatchByDestination(const AutocompleteMatch& match, |
| 246 const ACMatches& matches) { | 308 const ACMatches& matches) { |
| 247 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { | 309 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { |
| 248 if (i->destination_url == match.destination_url) | 310 if (i->destination_url == match.destination_url) |
| 249 return true; | 311 return true; |
| 250 } | 312 } |
| 251 return false; | 313 return false; |
| 252 } | 314 } |
| 253 | 315 |
| 254 void AutocompleteResult::MergeMatchesByProvider(const ACMatches& old_matches, | 316 void AutocompleteResult::MergeMatchesByProvider( |
| 255 const ACMatches& new_matches) { | 317 AutocompleteInput::PageClassification page_classification, |
| 318 const ACMatches& old_matches, |
| 319 const ACMatches& new_matches) { |
| 256 if (new_matches.size() >= old_matches.size()) | 320 if (new_matches.size() >= old_matches.size()) |
| 257 return; | 321 return; |
| 258 | 322 |
| 259 size_t delta = old_matches.size() - new_matches.size(); | 323 size_t delta = old_matches.size() - new_matches.size(); |
| 260 const int max_relevance = (new_matches.empty() ? | 324 const int max_relevance = (new_matches.empty() ? |
| 261 matches_.front().relevance : new_matches[0].relevance) - 1; | 325 matches_.front().relevance : new_matches[0].relevance) - 1; |
| 262 // Because the goal is a visibly-stable popup, rather than one that preserves | 326 // Because the goal is a visibly-stable popup, rather than one that preserves |
| 263 // the highest-relevance matches, we copy in the lowest-relevance matches | 327 // the highest-relevance matches, we copy in the lowest-relevance matches |
| 264 // first. This means that within each provider's "group" of matches, any | 328 // first. This means that within each provider's "group" of matches, any |
| 265 // synchronous matches (which tend to have the highest scores) will | 329 // synchronous matches (which tend to have the highest scores) will |
| 266 // "overwrite" the initial matches from that provider's previous results, | 330 // "overwrite" the initial matches from that provider's previous results, |
| 267 // minimally disturbing the rest of the matches. | 331 // minimally disturbing the rest of the matches. |
| 268 for (ACMatches::const_reverse_iterator i(old_matches.rbegin()); | 332 for (ACMatches::const_reverse_iterator i(old_matches.rbegin()); |
| 269 i != old_matches.rend() && delta > 0; ++i) { | 333 i != old_matches.rend() && delta > 0; ++i) { |
| 270 if (!HasMatchByDestination(*i, new_matches)) { | 334 if (!HasMatchByDestination(*i, new_matches)) { |
| 271 AutocompleteMatch match = *i; | 335 AutocompleteMatch match = *i; |
| 272 match.relevance = std::min(max_relevance, match.relevance); | 336 match.relevance = std::min(max_relevance, match.relevance); |
| 273 match.from_previous = true; | 337 match.from_previous = true; |
| 274 AddMatch(match); | 338 AddMatch(page_classification, match); |
| 275 delta--; | 339 delta--; |
| 276 } | 340 } |
| 277 } | 341 } |
| 278 } | 342 } |
| OLD | NEW |