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 "ui/app_list/search_provider.h" | 13 #include "ui/app_list/search_provider.h" |
14 #include "ui/app_list/search_result.h" | 14 #include "ui/app_list/search_result.h" |
15 | 15 |
16 namespace app_list { | 16 namespace app_list { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Maximum number of results to show. | 20 // Maximum number of results to show. |
21 const size_t kMaxResults = 6; | 21 const size_t kMaxResults = 6; |
22 const size_t kMaxMainGroupResults = 4; | |
23 const size_t kMaxWebstoreResults = 2; | |
24 const size_t kMaxPeopleResults = 2; | |
25 const size_t kMaxSuggestionsResults = 6; | |
26 | 22 |
27 // A value to indicate no max number of results limit. | 23 // A value to indicate no max number of results limit. |
28 const size_t kNoMaxResultsLimit = 0; | 24 const size_t kNoMaxResultsLimit = 0; |
29 | 25 |
30 void UpdateResult(const SearchResult& source, SearchResult* target) { | 26 void UpdateResult(const SearchResult& source, SearchResult* target) { |
31 target->set_display_type(source.display_type()); | 27 target->set_display_type(source.display_type()); |
32 target->set_title(source.title()); | 28 target->set_title(source.title()); |
33 target->set_title_tags(source.title_tags()); | 29 target->set_title_tags(source.title_tags()); |
34 target->set_details(source.details()); | 30 target->set_details(source.details()); |
35 target->set_details_tags(source.details_tags()); | 31 target->set_details_tags(source.details_tags()); |
36 } | 32 } |
37 | 33 |
38 } // namespace | 34 } // namespace |
39 | 35 |
| 36 const size_t Mixer::OMNIBOX_GROUP = 1; |
| 37 |
40 Mixer::SortData::SortData() : result(NULL), score(0.0) { | 38 Mixer::SortData::SortData() : result(NULL), score(0.0) { |
41 } | 39 } |
42 | 40 |
43 Mixer::SortData::SortData(SearchResult* result, double score) | 41 Mixer::SortData::SortData(SearchResult* result, double score) |
44 : result(result), score(score) { | 42 : result(result), score(score) { |
45 } | 43 } |
46 | 44 |
47 bool Mixer::SortData::operator<(const SortData& other) const { | 45 bool Mixer::SortData::operator<(const SortData& other) const { |
48 // This data precedes (less than) |other| if it has higher score. | 46 // This data precedes (less than) |other| if it has higher score. |
49 return score > other.score; | 47 return score > other.score; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 116 |
119 DISALLOW_COPY_AND_ASSIGN(Group); | 117 DISALLOW_COPY_AND_ASSIGN(Group); |
120 }; | 118 }; |
121 | 119 |
122 Mixer::Mixer(AppListModel::SearchResults* ui_results) | 120 Mixer::Mixer(AppListModel::SearchResults* ui_results) |
123 : ui_results_(ui_results) { | 121 : ui_results_(ui_results) { |
124 } | 122 } |
125 Mixer::~Mixer() { | 123 Mixer::~Mixer() { |
126 } | 124 } |
127 | 125 |
128 void Mixer::Init() { | 126 size_t Mixer::AddGroup(size_t max_results, double boost) { |
129 groups_[MAIN_GROUP].reset(new Group(kMaxMainGroupResults, 3.0)); | 127 groups_.push_back(new Group(max_results, boost)); |
130 groups_[OMNIBOX_GROUP].reset(new Group(kNoMaxResultsLimit, 2.0)); | 128 return groups_.size() - 1; |
131 groups_[WEBSTORE_GROUP].reset(new Group(kMaxWebstoreResults, 1.0)); | |
132 groups_[PEOPLE_GROUP].reset(new Group(kMaxPeopleResults, 0.0)); | |
133 groups_[SUGGESTIONS_GROUP].reset(new Group(kMaxSuggestionsResults, 3.0)); | |
134 } | 129 } |
135 | 130 |
136 void Mixer::AddProviderToGroup(GroupId group, SearchProvider* provider) { | 131 void Mixer::AddProviderToGroup(size_t group_id, SearchProvider* provider) { |
137 groups_[group]->AddProvider(provider); | 132 groups_[group_id]->AddProvider(provider); |
138 } | 133 } |
139 | 134 |
140 void Mixer::MixAndPublish(bool is_voice_query, | 135 void Mixer::MixAndPublish(bool is_voice_query, |
141 const KnownResults& known_results) { | 136 const KnownResults& known_results) { |
142 FetchResults(is_voice_query, known_results); | 137 FetchResults(is_voice_query, known_results); |
143 | 138 |
144 SortedResults results; | 139 SortedResults results; |
145 results.reserve(kMaxResults); | 140 results.reserve(kMaxResults); |
146 | 141 |
147 const Group& main_group = *groups_[MAIN_GROUP]; | 142 // Add results from non-omnibox groups first. |
148 const Group& omnibox_group = *groups_[OMNIBOX_GROUP]; | 143 for (size_t i = 0; i < groups_.size(); ++i) { |
149 const Group& webstore_group = *groups_[WEBSTORE_GROUP]; | 144 if (i != OMNIBOX_GROUP) { |
150 const Group& people_group = *groups_[PEOPLE_GROUP]; | 145 const Group& group = *groups_[i]; |
151 const Group& suggestions_group = *groups_[SUGGESTIONS_GROUP]; | 146 results.insert(results.end(), group.results().begin(), |
152 | 147 group.results().end()); |
153 // Adds main group and web store results first. | 148 } |
154 results.insert(results.end(), main_group.results().begin(), | 149 } |
155 main_group.results().end()); | |
156 results.insert(results.end(), webstore_group.results().begin(), | |
157 webstore_group.results().end()); | |
158 results.insert(results.end(), people_group.results().begin(), | |
159 people_group.results().end()); | |
160 results.insert(results.end(), suggestions_group.results().begin(), | |
161 suggestions_group.results().end()); | |
162 | 150 |
163 // Collapse duplicate apps from local and web store. | 151 // Collapse duplicate apps from local and web store. |
164 RemoveDuplicates(&results); | 152 RemoveDuplicates(&results); |
165 | 153 |
166 // Fill the remaining slots with omnibox results. Always add at least one | 154 // Fill the remaining slots with omnibox results. Always add at least one |
167 // omnibox result (even if there are no more slots; if we over-fill the | 155 // omnibox result (even if there are no more slots; if we over-fill the |
168 // vector, the web store and people results will be removed in a later step). | 156 // vector, the web store and people results will be removed in a later step). |
| 157 CHECK_LT(OMNIBOX_GROUP, groups_.size()); |
| 158 const Group& omnibox_group = *groups_[OMNIBOX_GROUP]; |
169 const size_t omnibox_results = | 159 const size_t omnibox_results = |
170 std::min(omnibox_group.results().size(), | 160 std::min(omnibox_group.results().size(), |
171 results.size() < kMaxResults ? kMaxResults - results.size() : 1); | 161 results.size() < kMaxResults ? kMaxResults - results.size() : 1); |
172 results.insert(results.end(), omnibox_group.results().begin(), | 162 results.insert(results.end(), omnibox_group.results().begin(), |
173 omnibox_group.results().begin() + omnibox_results); | 163 omnibox_group.results().begin() + omnibox_results); |
174 | 164 |
175 std::sort(results.begin(), results.end()); | 165 std::sort(results.begin(), results.end()); |
176 RemoveDuplicates(&results); | 166 RemoveDuplicates(&results); |
177 if (results.size() > kMaxResults) | 167 if (results.size() > kMaxResults) |
178 results.resize(kMaxResults); | 168 results.resize(kMaxResults); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 232 |
243 id_set.insert(id); | 233 id_set.insert(id); |
244 final.push_back(sort_data); | 234 final.push_back(sort_data); |
245 } | 235 } |
246 | 236 |
247 results->swap(final); | 237 results->swap(final); |
248 } | 238 } |
249 | 239 |
250 void Mixer::FetchResults(bool is_voice_query, | 240 void Mixer::FetchResults(bool is_voice_query, |
251 const KnownResults& known_results) { | 241 const KnownResults& known_results) { |
252 for (const auto& item : groups_) | 242 for (auto* group : groups_) |
253 item.second->FetchResults(is_voice_query, known_results); | 243 group->FetchResults(is_voice_query, known_results); |
254 } | 244 } |
255 | 245 |
256 } // namespace app_list | 246 } // namespace app_list |
OLD | NEW |