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