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" |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) | 147 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) |
148 i->ComputeStrippedDestinationURL(profile); | 148 i->ComputeStrippedDestinationURL(profile); |
149 | 149 |
150 // Remove duplicates. | 150 // Remove duplicates. |
151 std::sort(matches_.begin(), matches_.end(), | 151 std::sort(matches_.begin(), matches_.end(), |
152 &AutocompleteMatch::DestinationSortFunc); | 152 &AutocompleteMatch::DestinationSortFunc); |
153 matches_.erase(std::unique(matches_.begin(), matches_.end(), | 153 matches_.erase(std::unique(matches_.begin(), matches_.end(), |
154 &AutocompleteMatch::DestinationsEqual), | 154 &AutocompleteMatch::DestinationsEqual), |
155 matches_.end()); | 155 matches_.end()); |
156 | 156 |
| 157 // Find the top match before possibly applying demotions. |
| 158 if (!matches_.empty()) |
| 159 std::partial_sort(matches_.begin(), matches_.begin() + 1, matches_.end(), |
| 160 &AutocompleteMatch::MoreRelevant); |
| 161 // Don't demote the top match if applicable. |
| 162 OmniboxFieldTrial::UndemotableTopMatchTypes undemotable_top_types = |
| 163 OmniboxFieldTrial::GetUndemotableTopTypes( |
| 164 input.current_page_classification()); |
| 165 const bool preserve_top_match = !matches_.empty() && |
| 166 (undemotable_top_types.count(matches_.begin()->type) != 0); |
| 167 |
157 // Sort and trim to the most relevant kMaxMatches matches. | 168 // Sort and trim to the most relevant kMaxMatches matches. |
158 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); | 169 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); |
159 CompareWithDemoteByType comparing_object(input.current_page_classification()); | 170 CompareWithDemoteByType comparing_object(input.current_page_classification()); |
160 std::sort(matches_.begin(), matches_.end(), comparing_object); | 171 std::sort(matches_.begin() + (preserve_top_match ? 1 : 0), matches_.end(), |
| 172 comparing_object); |
161 if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match && | 173 if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match && |
162 OmniboxFieldTrial::ReorderForLegalDefaultMatch( | 174 OmniboxFieldTrial::ReorderForLegalDefaultMatch( |
163 input.current_page_classification())) { | 175 input.current_page_classification())) { |
164 // Top match is not allowed to be the default match. Find the most | 176 // Top match is not allowed to be the default match. Find the most |
165 // relevant legal match and shift it to the front. | 177 // relevant legal match and shift it to the front. |
166 for (AutocompleteResult::iterator it = matches_.begin() + 1; | 178 for (AutocompleteResult::iterator it = matches_.begin() + 1; |
167 it != matches_.end(); ++it) { | 179 it != matches_.end(); ++it) { |
168 if (it->allowed_to_be_default_match) { | 180 if (it->allowed_to_be_default_match) { |
169 std::rotate(matches_.begin(), it, it + 1); | 181 std::rotate(matches_.begin(), it, it + 1); |
170 break; | 182 break; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 alternate_nav_url_ = rhs.alternate_nav_url_; | 321 alternate_nav_url_ = rhs.alternate_nav_url_; |
310 } | 322 } |
311 | 323 |
312 void AutocompleteResult::AddMatch( | 324 void AutocompleteResult::AddMatch( |
313 AutocompleteInput::PageClassification page_classification, | 325 AutocompleteInput::PageClassification page_classification, |
314 const AutocompleteMatch& match) { | 326 const AutocompleteMatch& match) { |
315 DCHECK(default_match_ != end()); | 327 DCHECK(default_match_ != end()); |
316 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); | 328 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); |
317 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), | 329 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), |
318 match.description); | 330 match.description); |
| 331 // GetUndemotableTopTypes() is not used here because it's done in |
| 332 // SortAndCull(), and we depend on SortAndCull() to be called afterwards. |
319 CompareWithDemoteByType comparing_object(page_classification); | 333 CompareWithDemoteByType comparing_object(page_classification); |
320 ACMatches::iterator insertion_point = | 334 ACMatches::iterator insertion_point = |
321 std::upper_bound(begin(), end(), match, comparing_object); | 335 std::upper_bound(begin(), end(), match, comparing_object); |
322 matches_difference_type default_offset = default_match_ - begin(); | 336 matches_difference_type default_offset = default_match_ - begin(); |
323 if ((insertion_point - begin()) <= default_offset) | 337 if ((insertion_point - begin()) <= default_offset) |
324 ++default_offset; | 338 ++default_offset; |
325 matches_.insert(insertion_point, match); | 339 matches_.insert(insertion_point, match); |
326 default_match_ = begin() + default_offset; | 340 default_match_ = begin() + default_offset; |
327 } | 341 } |
328 | 342 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 i != old_matches.rend() && delta > 0; ++i) { | 376 i != old_matches.rend() && delta > 0; ++i) { |
363 if (!HasMatchByDestination(*i, new_matches)) { | 377 if (!HasMatchByDestination(*i, new_matches)) { |
364 AutocompleteMatch match = *i; | 378 AutocompleteMatch match = *i; |
365 match.relevance = std::min(max_relevance, match.relevance); | 379 match.relevance = std::min(max_relevance, match.relevance); |
366 match.from_previous = true; | 380 match.from_previous = true; |
367 AddMatch(page_classification, match); | 381 AddMatch(page_classification, match); |
368 delta--; | 382 delta--; |
369 } | 383 } |
370 } | 384 } |
371 } | 385 } |
OLD | NEW |