| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ui/app_list/search/mixer.h" | 5 #include "ui/app_list/search/mixer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "ui/app_list/search_provider.h" | 14 #include "ui/app_list/search_provider.h" |
| 15 #include "ui/app_list/search_result.h" | 15 #include "ui/app_list/search_result.h" |
| 16 | 16 |
| 17 namespace app_list { | 17 namespace app_list { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Maximum number of results to show. | |
| 22 const size_t kMinResults = 6; | |
| 23 | |
| 24 void UpdateResult(const SearchResult& source, SearchResult* target) { | 21 void UpdateResult(const SearchResult& source, SearchResult* target) { |
| 25 target->set_display_type(source.display_type()); | 22 target->set_display_type(source.display_type()); |
| 26 target->set_title(source.title()); | 23 target->set_title(source.title()); |
| 27 target->set_title_tags(source.title_tags()); | 24 target->set_title_tags(source.title_tags()); |
| 28 target->set_details(source.details()); | 25 target->set_details(source.details()); |
| 29 target->set_details_tags(source.details_tags()); | 26 target->set_details_tags(source.details_tags()); |
| 30 } | 27 } |
| 31 | 28 |
| 32 } // namespace | 29 } // namespace |
| 33 | 30 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 size_t Mixer::AddGroup(size_t max_results, double multiplier) { | 128 size_t Mixer::AddGroup(size_t max_results, double multiplier) { |
| 132 groups_.push_back(new Group(max_results, multiplier)); | 129 groups_.push_back(new Group(max_results, multiplier)); |
| 133 return groups_.size() - 1; | 130 return groups_.size() - 1; |
| 134 } | 131 } |
| 135 | 132 |
| 136 void Mixer::AddProviderToGroup(size_t group_id, SearchProvider* provider) { | 133 void Mixer::AddProviderToGroup(size_t group_id, SearchProvider* provider) { |
| 137 groups_[group_id]->AddProvider(provider); | 134 groups_[group_id]->AddProvider(provider); |
| 138 } | 135 } |
| 139 | 136 |
| 140 void Mixer::MixAndPublish(bool is_voice_query, | 137 void Mixer::MixAndPublish(bool is_voice_query, |
| 141 const KnownResults& known_results) { | 138 const KnownResults& known_results, |
| 139 size_t num_max_results) { |
| 142 FetchResults(is_voice_query, known_results); | 140 FetchResults(is_voice_query, known_results); |
| 143 | 141 |
| 144 SortedResults results; | 142 SortedResults results; |
| 145 results.reserve(kMinResults); | 143 results.reserve(num_max_results); |
| 146 | 144 |
| 147 // Add results from each group. Limit to the maximum number of results in each | 145 // Add results from each group. Limit to the maximum number of results in each |
| 148 // group. | 146 // group. |
| 149 for (const Group* group : groups_) { | 147 for (const Group* group : groups_) { |
| 150 size_t num_results = | 148 size_t num_results = |
| 151 std::min(group->results().size(), group->max_results()); | 149 std::min(group->results().size(), group->max_results()); |
| 152 results.insert(results.end(), group->results().begin(), | 150 results.insert(results.end(), group->results().begin(), |
| 153 group->results().begin() + num_results); | 151 group->results().begin() + num_results); |
| 154 } | 152 } |
| 155 // Remove results with duplicate IDs before sorting. If two providers give a | 153 // Remove results with duplicate IDs before sorting. If two providers give a |
| 156 // result with the same ID, the result from the provider with the *lower group | 154 // result with the same ID, the result from the provider with the *lower group |
| 157 // number* will be kept (e.g., an app result takes priority over a web store | 155 // number* will be kept (e.g., an app result takes priority over a web store |
| 158 // result with the same ID). | 156 // result with the same ID). |
| 159 RemoveDuplicates(&results); | 157 RemoveDuplicates(&results); |
| 160 std::sort(results.begin(), results.end()); | 158 std::sort(results.begin(), results.end()); |
| 161 | 159 |
| 162 if (results.size() < kMinResults) { | 160 if (results.size() < num_max_results) { |
| 163 size_t original_size = results.size(); | 161 size_t original_size = results.size(); |
| 164 // We didn't get enough results. Insert all the results again, and this | 162 // We didn't get enough results. Insert all the results again, and this |
| 165 // time, do not limit the maximum number of results from each group. (This | 163 // time, do not limit the maximum number of results from each group. (This |
| 166 // will result in duplicates, which will be removed by RemoveDuplicates.) | 164 // will result in duplicates, which will be removed by RemoveDuplicates.) |
| 167 for (const Group* group : groups_) { | 165 for (const Group* group : groups_) { |
| 168 results.insert(results.end(), group->results().begin(), | 166 results.insert(results.end(), group->results().begin(), |
| 169 group->results().end()); | 167 group->results().end()); |
| 170 } | 168 } |
| 171 RemoveDuplicates(&results); | 169 RemoveDuplicates(&results); |
| 172 // Sort just the newly added results. This ensures that, for example, if | 170 // Sort just the newly added results. This ensures that, for example, if |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 results->swap(final); | 246 results->swap(final); |
| 249 } | 247 } |
| 250 | 248 |
| 251 void Mixer::FetchResults(bool is_voice_query, | 249 void Mixer::FetchResults(bool is_voice_query, |
| 252 const KnownResults& known_results) { | 250 const KnownResults& known_results) { |
| 253 for (auto* group : groups_) | 251 for (auto* group : groups_) |
| 254 group->FetchResults(is_voice_query, known_results); | 252 group->FetchResults(is_voice_query, known_results); |
| 255 } | 253 } |
| 256 | 254 |
| 257 } // namespace app_list | 255 } // namespace app_list |
| OLD | NEW |