Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(493)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_result.cc

Issue 22031002: Omnibox: Create DemoteByType Experiment (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Peter's comments and Alexei's improvements. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 matches of particular types to be demoted in AutocompleteResult.
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 // An alias of GetDemotedRelevance().
28 int operator()(const AutocompleteMatch& elem) {
29 return GetDemotedRelevance(elem);
30 }
31
32 // Comparison function.
33 bool operator()(const AutocompleteMatch& elem1,
34 const AutocompleteMatch& elem2);
35
36 private:
37 OmniboxFieldTrial::DemotionMultipliers demotions_;
38 };
39
40 CompareWithDemoteByType::CompareWithDemoteByType(
41 AutocompleteInput::PageClassification current_page_classification) {
42 OmniboxFieldTrial::GetDemotionsByType(current_page_classification,
43 &demotions_);
44 }
45
46 int CompareWithDemoteByType::GetDemotedRelevance(
47 const AutocompleteMatch& match) {
48 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it =
49 demotions_.find(match.type);
50 return (demotion_it == demotions_.end()) ?
51 match.relevance : (match.relevance * demotion_it->second);
52 }
53
54 bool CompareWithDemoteByType::operator()(const AutocompleteMatch& elem1,
55 const AutocompleteMatch& elem2) {
56 // Compute demoted relevance scores for each match.
57 const int demoted_relevance1 = GetDemotedRelevance(elem1);
58 const int demoted_relevance2 = GetDemotedRelevance(elem2);
59 // For equal-relevance matches, we sort alphabetically, so that providers
60 // who return multiple elements at the same priority get a "stable" sort
61 // across multiple updates.
62 return (demoted_relevance1 == demoted_relevance2) ?
63 (elem1.contents < elem2.contents) :
64 (demoted_relevance1 > demoted_relevance2);
65 }
66
67 }; // namespace
13 68
14 // static 69 // static
15 const size_t AutocompleteResult::kMaxMatches = 6; 70 const size_t AutocompleteResult::kMaxMatches = 6;
16 const int AutocompleteResult::kLowestDefaultScore = 1200; 71 const int AutocompleteResult::kLowestDefaultScore = 1200;
17 72
18 void AutocompleteResult::Selection::Clear() { 73 void AutocompleteResult::Selection::Clear() {
19 destination_url = GURL(); 74 destination_url = GURL();
20 provider_affinity = NULL; 75 provider_affinity = NULL;
21 is_history_what_you_typed_match = false; 76 is_history_what_you_typed_match = false;
22 } 77 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 // ultimately be similar. If the assumption holds, this prevents seeing the 115 // 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; 116 // 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 117 // 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 118 // matches, the new ones will all become visible, so we won't have lost
64 // anything permanently. 119 // anything permanently.
65 ProviderToMatches matches_per_provider, old_matches_per_provider; 120 ProviderToMatches matches_per_provider, old_matches_per_provider;
66 BuildProviderToMatches(&matches_per_provider); 121 BuildProviderToMatches(&matches_per_provider);
67 old_matches.BuildProviderToMatches(&old_matches_per_provider); 122 old_matches.BuildProviderToMatches(&old_matches_per_provider);
68 for (ProviderToMatches::const_iterator i(old_matches_per_provider.begin()); 123 for (ProviderToMatches::const_iterator i(old_matches_per_provider.begin());
69 i != old_matches_per_provider.end(); ++i) { 124 i != old_matches_per_provider.end(); ++i) {
70 MergeMatchesByProvider(i->second, matches_per_provider[i->first]); 125 MergeMatchesByProvider(input.current_page_classification(),
126 i->second, matches_per_provider[i->first]);
71 } 127 }
72 128
73 SortAndCull(input, profile); 129 SortAndCull(input, profile);
74 } 130 }
75 131
76 void AutocompleteResult::AppendMatches(const ACMatches& matches) { 132 void AutocompleteResult::AppendMatches(const ACMatches& matches) {
77 #ifndef NDEBUG 133 #ifndef NDEBUG
78 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { 134 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) {
79 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->contents), i->contents); 135 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->contents), i->contents);
80 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->description), 136 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->description),
(...skipping 11 matching lines...) Expand all
92 i->ComputeStrippedDestinationURL(profile); 148 i->ComputeStrippedDestinationURL(profile);
93 149
94 // Remove duplicates. 150 // Remove duplicates.
95 std::sort(matches_.begin(), matches_.end(), 151 std::sort(matches_.begin(), matches_.end(),
96 &AutocompleteMatch::DestinationSortFunc); 152 &AutocompleteMatch::DestinationSortFunc);
97 matches_.erase(std::unique(matches_.begin(), matches_.end(), 153 matches_.erase(std::unique(matches_.begin(), matches_.end(),
98 &AutocompleteMatch::DestinationsEqual), 154 &AutocompleteMatch::DestinationsEqual),
99 matches_.end()); 155 matches_.end());
100 156
101 // Sort and trim to the most relevant kMaxMatches matches. 157 // Sort and trim to the most relevant kMaxMatches matches.
102 const size_t num_matches = std::min(kMaxMatches, matches_.size()); 158 size_t max_num_matches = std::min(kMaxMatches, matches_.size());
103 std::partial_sort(matches_.begin(), matches_.begin() + num_matches, 159 CompareWithDemoteByType comparing_object(input.current_page_classification());
104 matches_.end(), &AutocompleteMatch::MoreRelevant); 160 std::partial_sort(matches_.begin(), matches_.begin() + max_num_matches,
161 matches_.end(), comparing_object);
162 // In the process of trimming, drop all matches with a demoted relevance
163 // score of 0.
164 matches_.resize(std::count_if(matches_.begin(),
165 matches_.begin() + max_num_matches,
166 comparing_object));
167 /*
168 size_t num_matches;
169 for (num_matches = 0u; (num_matches < max_num_matches) &&
170 (comparing_object.GetDemotedRelevance(*match_at(num_matches)) > 0);
171 ++num_matches) {}
105 matches_.resize(num_matches); 172 matches_.resize(num_matches);
106 173 */
107 default_match_ = begin(); 174 default_match_ = begin();
108 175
109 // Set the alternate nav URL. 176 // Set the alternate nav URL.
110 alternate_nav_url_ = (default_match_ == end()) ? 177 alternate_nav_url_ = (default_match_ == end()) ?
111 GURL() : ComputeAlternateNavUrl(input, *default_match_); 178 GURL() : ComputeAlternateNavUrl(input, *default_match_);
112 } 179 }
113 180
114 bool AutocompleteResult::HasCopiedMatches() const { 181 bool AutocompleteResult::HasCopiedMatches() const {
115 for (ACMatches::const_iterator i(begin()); i != end(); ++i) { 182 for (ACMatches::const_iterator i(begin()); i != end(); ++i) {
116 if (i->from_previous) 183 if (i->from_previous)
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 260
194 matches_ = rhs.matches_; 261 matches_ = rhs.matches_;
195 // Careful! You can't just copy iterators from another container, you have to 262 // Careful! You can't just copy iterators from another container, you have to
196 // reconstruct them. 263 // reconstruct them.
197 default_match_ = (rhs.default_match_ == rhs.end()) ? 264 default_match_ = (rhs.default_match_ == rhs.end()) ?
198 end() : (begin() + (rhs.default_match_ - rhs.begin())); 265 end() : (begin() + (rhs.default_match_ - rhs.begin()));
199 266
200 alternate_nav_url_ = rhs.alternate_nav_url_; 267 alternate_nav_url_ = rhs.alternate_nav_url_;
201 } 268 }
202 269
203 void AutocompleteResult::AddMatch(const AutocompleteMatch& match) { 270 void AutocompleteResult::AddMatch(
271 AutocompleteInput::PageClassification page_classification,
272 const AutocompleteMatch& match) {
204 DCHECK(default_match_ != end()); 273 DCHECK(default_match_ != end());
205 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); 274 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents);
206 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), 275 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description),
207 match.description); 276 match.description);
277 CompareWithDemoteByType comparing_object(page_classification);
208 ACMatches::iterator insertion_point = 278 ACMatches::iterator insertion_point =
209 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); 279 std::upper_bound(begin(), end(), match, comparing_object);
210 matches_difference_type default_offset = default_match_ - begin(); 280 matches_difference_type default_offset = default_match_ - begin();
211 if ((insertion_point - begin()) <= default_offset) 281 if ((insertion_point - begin()) <= default_offset)
212 ++default_offset; 282 ++default_offset;
213 matches_.insert(insertion_point, match); 283 matches_.insert(insertion_point, match);
214 default_match_ = begin() + default_offset; 284 default_match_ = begin() + default_offset;
215 } 285 }
216 286
217 void AutocompleteResult::BuildProviderToMatches( 287 void AutocompleteResult::BuildProviderToMatches(
218 ProviderToMatches* provider_to_matches) const { 288 ProviderToMatches* provider_to_matches) const {
219 for (ACMatches::const_iterator i(begin()); i != end(); ++i) 289 for (ACMatches::const_iterator i(begin()); i != end(); ++i)
220 (*provider_to_matches)[i->provider].push_back(*i); 290 (*provider_to_matches)[i->provider].push_back(*i);
221 } 291 }
222 292
223 // static 293 // static
224 bool AutocompleteResult::HasMatchByDestination(const AutocompleteMatch& match, 294 bool AutocompleteResult::HasMatchByDestination(const AutocompleteMatch& match,
225 const ACMatches& matches) { 295 const ACMatches& matches) {
226 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { 296 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) {
227 if (i->destination_url == match.destination_url) 297 if (i->destination_url == match.destination_url)
228 return true; 298 return true;
229 } 299 }
230 return false; 300 return false;
231 } 301 }
232 302
233 void AutocompleteResult::MergeMatchesByProvider(const ACMatches& old_matches, 303 void AutocompleteResult::MergeMatchesByProvider(
234 const ACMatches& new_matches) { 304 AutocompleteInput::PageClassification page_classification,
305 const ACMatches& old_matches,
306 const ACMatches& new_matches) {
235 if (new_matches.size() >= old_matches.size()) 307 if (new_matches.size() >= old_matches.size())
236 return; 308 return;
237 309
238 size_t delta = old_matches.size() - new_matches.size(); 310 size_t delta = old_matches.size() - new_matches.size();
239 const int max_relevance = (new_matches.empty() ? 311 const int max_relevance = (new_matches.empty() ?
240 matches_.front().relevance : new_matches[0].relevance) - 1; 312 matches_.front().relevance : new_matches[0].relevance) - 1;
241 // Because the goal is a visibly-stable popup, rather than one that preserves 313 // 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 314 // the highest-relevance matches, we copy in the lowest-relevance matches
243 // first. This means that within each provider's "group" of matches, any 315 // first. This means that within each provider's "group" of matches, any
244 // synchronous matches (which tend to have the highest scores) will 316 // synchronous matches (which tend to have the highest scores) will
245 // "overwrite" the initial matches from that provider's previous results, 317 // "overwrite" the initial matches from that provider's previous results,
246 // minimally disturbing the rest of the matches. 318 // minimally disturbing the rest of the matches.
247 for (ACMatches::const_reverse_iterator i(old_matches.rbegin()); 319 for (ACMatches::const_reverse_iterator i(old_matches.rbegin());
248 i != old_matches.rend() && delta > 0; ++i) { 320 i != old_matches.rend() && delta > 0; ++i) {
249 if (!HasMatchByDestination(*i, new_matches)) { 321 if (!HasMatchByDestination(*i, new_matches)) {
250 AutocompleteMatch match = *i; 322 AutocompleteMatch match = *i;
251 match.relevance = std::min(max_relevance, match.relevance); 323 match.relevance = std::min(max_relevance, match.relevance);
252 match.from_previous = true; 324 match.from_previous = true;
253 AddMatch(match); 325 AddMatch(page_classification, match);
254 delta--; 326 delta--;
255 } 327 }
256 } 328 }
257 } 329 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698