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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 Profile* profile) { | 146 Profile* profile) { |
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 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); | |
157 | |
158 // Don't demote the top match if applicable. | |
159 OmniboxFieldTrial::UndemotableTopMatchTypes undemotable_top_types; | |
160 OmniboxFieldTrial::GetUndemotableTopTypes(input.current_page_classification(), | |
161 &undemotable_top_types); | |
162 const bool have_top_undemotable_match = !matches_.empty() && | |
163 undemotable_top_types.find(matches_.begin()->type) != | |
164 undemotable_top_types.end(); | |
Mark P
2013/11/04 20:30:12
nit: parens around != please
H Fung
2013/11/04 21:39:57
Done.
| |
165 AutocompleteMatch top_undemotable_match; | |
166 if (have_top_undemotable_match) { | |
167 top_undemotable_match = *matches_.begin(); | |
168 matches_.erase(matches_.begin()); | |
169 } | |
156 | 170 |
157 // Sort and trim to the most relevant kMaxMatches matches. | 171 // Sort and trim to the most relevant kMaxMatches matches. |
158 size_t max_num_matches = std::min(kMaxMatches, matches_.size()); | |
159 CompareWithDemoteByType comparing_object(input.current_page_classification()); | 172 CompareWithDemoteByType comparing_object(input.current_page_classification()); |
160 std::sort(matches_.begin(), matches_.end(), comparing_object); | 173 std::sort(matches_.begin(), matches_.end(), comparing_object); |
174 if (have_top_undemotable_match) | |
175 matches_.insert(matches_.begin(), top_undemotable_match); | |
161 if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match && | 176 if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match && |
162 OmniboxFieldTrial::ReorderForLegalDefaultMatch( | 177 OmniboxFieldTrial::ReorderForLegalDefaultMatch( |
163 input.current_page_classification())) { | 178 input.current_page_classification())) { |
164 // Top match is not allowed to be the default match. Find the most | 179 // Top match is not allowed to be the default match. Find the most |
165 // relevant legal match and shift it to the front. | 180 // relevant legal match and shift it to the front. |
166 for (AutocompleteResult::iterator it = matches_.begin() + 1; | 181 for (AutocompleteResult::iterator it = matches_.begin() + 1; |
167 it != matches_.end(); ++it) { | 182 it != matches_.end(); ++it) { |
168 if (it->allowed_to_be_default_match) { | 183 if (it->allowed_to_be_default_match) { |
169 std::rotate(matches_.begin(), it, it + 1); | 184 std::rotate(matches_.begin(), it, it + 1); |
170 break; | 185 break; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 alternate_nav_url_ = rhs.alternate_nav_url_; | 324 alternate_nav_url_ = rhs.alternate_nav_url_; |
310 } | 325 } |
311 | 326 |
312 void AutocompleteResult::AddMatch( | 327 void AutocompleteResult::AddMatch( |
313 AutocompleteInput::PageClassification page_classification, | 328 AutocompleteInput::PageClassification page_classification, |
314 const AutocompleteMatch& match) { | 329 const AutocompleteMatch& match) { |
315 DCHECK(default_match_ != end()); | 330 DCHECK(default_match_ != end()); |
316 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); | 331 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); |
317 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), | 332 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), |
318 match.description); | 333 match.description); |
334 // There is logic in SortAndCull() to possibly not demote the top match (so | |
335 // that CompareWithDemoteByType is not called on the top match). That logic | |
336 // is not replicated here since we depend on SortAndCull() on |matches_| after | |
337 // this function is called. | |
Mark P
2013/11/04 20:30:12
This comment made me realize the comment in autoco
H Fung
2013/11/04 21:39:57
Actually, SortAndCull() is called both before and
| |
319 CompareWithDemoteByType comparing_object(page_classification); | 338 CompareWithDemoteByType comparing_object(page_classification); |
320 ACMatches::iterator insertion_point = | 339 ACMatches::iterator insertion_point = |
321 std::upper_bound(begin(), end(), match, comparing_object); | 340 std::upper_bound(begin(), end(), match, comparing_object); |
322 matches_difference_type default_offset = default_match_ - begin(); | 341 matches_difference_type default_offset = default_match_ - begin(); |
323 if ((insertion_point - begin()) <= default_offset) | 342 if ((insertion_point - begin()) <= default_offset) |
324 ++default_offset; | 343 ++default_offset; |
325 matches_.insert(insertion_point, match); | 344 matches_.insert(insertion_point, match); |
326 default_match_ = begin() + default_offset; | 345 default_match_ = begin() + default_offset; |
327 } | 346 } |
328 | 347 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
362 i != old_matches.rend() && delta > 0; ++i) { | 381 i != old_matches.rend() && delta > 0; ++i) { |
363 if (!HasMatchByDestination(*i, new_matches)) { | 382 if (!HasMatchByDestination(*i, new_matches)) { |
364 AutocompleteMatch match = *i; | 383 AutocompleteMatch match = *i; |
365 match.relevance = std::min(max_relevance, match.relevance); | 384 match.relevance = std::min(max_relevance, match.relevance); |
366 match.from_previous = true; | 385 match.from_previous = true; |
367 AddMatch(page_classification, match); | 386 AddMatch(page_classification, match); |
368 delta--; | 387 delta--; |
369 } | 388 } |
370 } | 389 } |
371 } | 390 } |
OLD | NEW |