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