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" |
| (...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 // Don't demote the top match if applicable. | |
| 158 OmniboxFieldTrial::UndemotableTopMatchTypes undemotable_top_types; | |
| 159 OmniboxFieldTrial::GetUndemotableTopTypes(input.current_page_classification(), | |
| 160 &undemotable_top_types); | |
| 161 const bool preserve_top_match = !matches_.empty() && | |
| 162 (undemotable_top_types.find(matches_.begin()->type) != | |
|
Peter Kasting
2013/11/05 04:09:55
Nit: Instead of using find(), use count() and chec
H Fung
2013/11/05 06:11:27
Done. Actually, should ContainsKey() be used inst
Peter Kasting
2013/11/05 20:00:34
From stl_util.h? I don't really know why we have
| |
| 163 undemotable_top_types.end()); | |
| 164 | |
| 157 // Sort and trim to the most relevant kMaxMatches matches. | 165 // Sort and trim to the most relevant kMaxMatches matches. |
| 158 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); | 166 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); |
| 159 CompareWithDemoteByType comparing_object(input.current_page_classification()); | 167 CompareWithDemoteByType comparing_object(input.current_page_classification()); |
| 160 std::sort(matches_.begin(), matches_.end(), comparing_object); | 168 std::sort(preserve_top_match ? matches_.begin() + 1 : matches_.begin(), |
|
Peter Kasting
2013/11/05 04:09:55
Nit: Slightly briefer: matches.begin() + (preserve
H Fung
2013/11/05 06:11:27
Done.
| |
| 169 matches_.end(), comparing_object); | |
| 161 if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match && | 170 if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match && |
| 162 OmniboxFieldTrial::ReorderForLegalDefaultMatch( | 171 OmniboxFieldTrial::ReorderForLegalDefaultMatch( |
| 163 input.current_page_classification())) { | 172 input.current_page_classification())) { |
| 164 // Top match is not allowed to be the default match. Find the most | 173 // Top match is not allowed to be the default match. Find the most |
| 165 // relevant legal match and shift it to the front. | 174 // relevant legal match and shift it to the front. |
| 166 for (AutocompleteResult::iterator it = matches_.begin() + 1; | 175 for (AutocompleteResult::iterator it = matches_.begin() + 1; |
| 167 it != matches_.end(); ++it) { | 176 it != matches_.end(); ++it) { |
| 168 if (it->allowed_to_be_default_match) { | 177 if (it->allowed_to_be_default_match) { |
| 169 std::rotate(matches_.begin(), it, it + 1); | 178 std::rotate(matches_.begin(), it, it + 1); |
| 170 break; | 179 break; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 309 alternate_nav_url_ = rhs.alternate_nav_url_; | 318 alternate_nav_url_ = rhs.alternate_nav_url_; |
| 310 } | 319 } |
| 311 | 320 |
| 312 void AutocompleteResult::AddMatch( | 321 void AutocompleteResult::AddMatch( |
| 313 AutocompleteInput::PageClassification page_classification, | 322 AutocompleteInput::PageClassification page_classification, |
| 314 const AutocompleteMatch& match) { | 323 const AutocompleteMatch& match) { |
| 315 DCHECK(default_match_ != end()); | 324 DCHECK(default_match_ != end()); |
| 316 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); | 325 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); |
| 317 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), | 326 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), |
| 318 match.description); | 327 match.description); |
| 328 // GetUndemotableTopTypes() is not used here because it's done in | |
| 329 // SortAndCull(), and we depend on SortAndCull() to be called afterwards. | |
| 319 CompareWithDemoteByType comparing_object(page_classification); | 330 CompareWithDemoteByType comparing_object(page_classification); |
| 320 ACMatches::iterator insertion_point = | 331 ACMatches::iterator insertion_point = |
| 321 std::upper_bound(begin(), end(), match, comparing_object); | 332 std::upper_bound(begin(), end(), match, comparing_object); |
| 322 matches_difference_type default_offset = default_match_ - begin(); | 333 matches_difference_type default_offset = default_match_ - begin(); |
| 323 if ((insertion_point - begin()) <= default_offset) | 334 if ((insertion_point - begin()) <= default_offset) |
| 324 ++default_offset; | 335 ++default_offset; |
| 325 matches_.insert(insertion_point, match); | 336 matches_.insert(insertion_point, match); |
| 326 default_match_ = begin() + default_offset; | 337 default_match_ = begin() + default_offset; |
| 327 } | 338 } |
| 328 | 339 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 362 i != old_matches.rend() && delta > 0; ++i) { | 373 i != old_matches.rend() && delta > 0; ++i) { |
| 363 if (!HasMatchByDestination(*i, new_matches)) { | 374 if (!HasMatchByDestination(*i, new_matches)) { |
| 364 AutocompleteMatch match = *i; | 375 AutocompleteMatch match = *i; |
| 365 match.relevance = std::min(max_relevance, match.relevance); | 376 match.relevance = std::min(max_relevance, match.relevance); |
| 366 match.from_previous = true; | 377 match.from_previous = true; |
| 367 AddMatch(page_classification, match); | 378 AddMatch(page_classification, match); |
| 368 delta--; | 379 delta--; |
| 369 } | 380 } |
| 370 } | 381 } |
| 371 } | 382 } |
| OLD | NEW |