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