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/search_engines/template_url_prepopulate_data.h" | |
|
Peter Kasting
2012/10/26 22:13:49
Nit: Not necessary?
Bart N.
2012/10/26 23:36:55
Good catch, yes.
| |
| 13 | 14 |
| 14 // static | 15 // static |
| 15 const size_t AutocompleteResult::kMaxMatches = 6; | 16 const size_t AutocompleteResult::kMaxMatches = 6; |
| 16 const int AutocompleteResult::kLowestDefaultScore = 1200; | 17 const int AutocompleteResult::kLowestDefaultScore = 1200; |
| 17 | 18 |
| 18 void AutocompleteResult::Selection::Clear() { | 19 void AutocompleteResult::Selection::Clear() { |
| 19 destination_url = GURL(); | 20 destination_url = GURL(); |
| 20 provider_affinity = NULL; | 21 provider_affinity = NULL; |
| 21 is_history_what_you_typed_match = false; | 22 is_history_what_you_typed_match = false; |
| 22 } | 23 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 40 matches_ = rhs.matches_; | 41 matches_ = rhs.matches_; |
| 41 // Careful! You can't just copy iterators from another container, you have to | 42 // Careful! You can't just copy iterators from another container, you have to |
| 42 // reconstruct them. | 43 // reconstruct them. |
| 43 default_match_ = (rhs.default_match_ == rhs.end()) ? | 44 default_match_ = (rhs.default_match_ == rhs.end()) ? |
| 44 end() : (begin() + (rhs.default_match_ - rhs.begin())); | 45 end() : (begin() + (rhs.default_match_ - rhs.begin())); |
| 45 | 46 |
| 46 alternate_nav_url_ = rhs.alternate_nav_url_; | 47 alternate_nav_url_ = rhs.alternate_nav_url_; |
| 47 } | 48 } |
| 48 | 49 |
| 49 void AutocompleteResult::CopyOldMatches(const AutocompleteInput& input, | 50 void AutocompleteResult::CopyOldMatches(const AutocompleteInput& input, |
| 50 const AutocompleteResult& old_matches) { | 51 const AutocompleteResult& old_matches, |
| 52 Profile* profile) { | |
| 51 if (old_matches.empty()) | 53 if (old_matches.empty()) |
| 52 return; | 54 return; |
| 53 | 55 |
| 54 if (empty()) { | 56 if (empty()) { |
| 55 // If we've got no matches we can copy everything from the last result. | 57 // If we've got no matches we can copy everything from the last result. |
| 56 CopyFrom(old_matches); | 58 CopyFrom(old_matches); |
| 57 for (ACMatches::iterator i(begin()); i != end(); ++i) | 59 for (ACMatches::iterator i(begin()); i != end(); ++i) |
| 58 i->from_previous = true; | 60 i->from_previous = true; |
| 59 return; | 61 return; |
| 60 } | 62 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 75 // matches, the new ones will all become visible, so we won't have lost | 77 // matches, the new ones will all become visible, so we won't have lost |
| 76 // anything permanently. | 78 // anything permanently. |
| 77 ProviderToMatches matches_per_provider, old_matches_per_provider; | 79 ProviderToMatches matches_per_provider, old_matches_per_provider; |
| 78 BuildProviderToMatches(&matches_per_provider); | 80 BuildProviderToMatches(&matches_per_provider); |
| 79 old_matches.BuildProviderToMatches(&old_matches_per_provider); | 81 old_matches.BuildProviderToMatches(&old_matches_per_provider); |
| 80 for (ProviderToMatches::const_iterator i(old_matches_per_provider.begin()); | 82 for (ProviderToMatches::const_iterator i(old_matches_per_provider.begin()); |
| 81 i != old_matches_per_provider.end(); ++i) { | 83 i != old_matches_per_provider.end(); ++i) { |
| 82 MergeMatchesByProvider(i->second, matches_per_provider[i->first]); | 84 MergeMatchesByProvider(i->second, matches_per_provider[i->first]); |
| 83 } | 85 } |
| 84 | 86 |
| 85 SortAndCull(input); | 87 SortAndCull(input, profile); |
| 86 } | 88 } |
| 87 | 89 |
| 88 void AutocompleteResult::AppendMatches(const ACMatches& matches) { | 90 void AutocompleteResult::AppendMatches(const ACMatches& matches) { |
| 89 #ifndef NDEBUG | 91 #ifndef NDEBUG |
| 90 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { | 92 for (ACMatches::const_iterator i(matches.begin()); i != matches.end(); ++i) { |
| 91 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->contents), i->contents); | 93 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->contents), i->contents); |
| 92 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->description), | 94 DCHECK_EQ(AutocompleteMatch::SanitizeString(i->description), |
| 93 i->description); | 95 i->description); |
| 94 } | 96 } |
| 95 #endif | 97 #endif |
| 96 std::copy(matches.begin(), matches.end(), std::back_inserter(matches_)); | 98 std::copy(matches.begin(), matches.end(), std::back_inserter(matches_)); |
| 97 default_match_ = end(); | 99 default_match_ = end(); |
| 98 alternate_nav_url_ = GURL(); | 100 alternate_nav_url_ = GURL(); |
| 99 } | 101 } |
| 100 | 102 |
| 101 void AutocompleteResult::AddMatch(const AutocompleteMatch& match) { | 103 void AutocompleteResult::AddMatch(const AutocompleteMatch& match) { |
| 102 DCHECK(default_match_ != end()); | 104 DCHECK(default_match_ != end()); |
| 103 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); | 105 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.contents), match.contents); |
| 104 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), | 106 DCHECK_EQ(AutocompleteMatch::SanitizeString(match.description), |
| 105 match.description); | 107 match.description); |
| 106 ACMatches::iterator insertion_point = | 108 ACMatches::iterator insertion_point = |
| 107 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); | 109 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); |
| 108 matches_difference_type default_offset = default_match_ - begin(); | 110 matches_difference_type default_offset = default_match_ - begin(); |
| 109 if ((insertion_point - begin()) <= default_offset) | 111 if ((insertion_point - begin()) <= default_offset) |
| 110 ++default_offset; | 112 ++default_offset; |
| 111 matches_.insert(insertion_point, match); | 113 matches_.insert(insertion_point, match); |
| 112 default_match_ = begin() + default_offset; | 114 default_match_ = begin() + default_offset; |
| 113 } | 115 } |
| 114 | 116 |
| 115 void AutocompleteResult::SortAndCull(const AutocompleteInput& input) { | 117 void AutocompleteResult::SortAndCull(const AutocompleteInput& input, |
| 118 Profile* profile) { | |
| 116 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) | 119 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) |
| 117 i->ComputeStrippedDestinationURL(); | 120 i->ComputeStrippedDestinationURL(profile); |
| 118 | 121 |
| 119 // Remove duplicates. | 122 // Remove duplicates. |
| 120 std::sort(matches_.begin(), matches_.end(), | 123 std::sort(matches_.begin(), matches_.end(), |
| 121 &AutocompleteMatch::DestinationSortFunc); | 124 &AutocompleteMatch::DestinationSortFunc); |
| 122 matches_.erase(std::unique(matches_.begin(), matches_.end(), | 125 matches_.erase(std::unique(matches_.begin(), matches_.end(), |
| 123 &AutocompleteMatch::DestinationsEqual), | 126 &AutocompleteMatch::DestinationsEqual), |
| 124 matches_.end()); | 127 matches_.end()); |
| 125 | 128 |
| 126 // Sort and trim to the most relevant kMaxMatches matches. | 129 // Sort and trim to the most relevant kMaxMatches matches. |
| 127 const size_t num_matches = std::min(kMaxMatches, matches_.size()); | 130 const size_t num_matches = std::min(kMaxMatches, matches_.size()); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 241 i != old_matches.rend() && delta > 0; ++i) { | 244 i != old_matches.rend() && delta > 0; ++i) { |
| 242 if (!HasMatchByDestination(*i, new_matches)) { | 245 if (!HasMatchByDestination(*i, new_matches)) { |
| 243 AutocompleteMatch match = *i; | 246 AutocompleteMatch match = *i; |
| 244 match.relevance = std::min(max_relevance, match.relevance); | 247 match.relevance = std::min(max_relevance, match.relevance); |
| 245 match.from_previous = true; | 248 match.from_previous = true; |
| 246 AddMatch(match); | 249 AddMatch(match); |
| 247 delta--; | 250 delta--; |
| 248 } | 251 } |
| 249 } | 252 } |
| 250 } | 253 } |
| OLD | NEW |