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 const size_t kMaxLauncherSearchResults = 2; | |
27 | 22 |
28 // A value to indicate no max number of results limit. | 23 // A value to indicate no max number of results limit. |
29 const size_t kNoMaxResultsLimit = 0; | 24 const size_t kNoMaxResultsLimit = 0; |
30 | 25 |
31 void UpdateResult(const SearchResult& source, SearchResult* target) { | 26 void UpdateResult(const SearchResult& source, SearchResult* target) { |
32 target->set_display_type(source.display_type()); | 27 target->set_display_type(source.display_type()); |
33 target->set_title(source.title()); | 28 target->set_title(source.title()); |
34 target->set_title_tags(source.title_tags()); | 29 target->set_title_tags(source.title_tags()); |
35 target->set_details(source.details()); | 30 target->set_details(source.details()); |
36 target->set_details_tags(source.details_tags()); | 31 target->set_details_tags(source.details_tags()); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 114 |
120 DISALLOW_COPY_AND_ASSIGN(Group); | 115 DISALLOW_COPY_AND_ASSIGN(Group); |
121 }; | 116 }; |
122 | 117 |
123 Mixer::Mixer(AppListModel::SearchResults* ui_results) | 118 Mixer::Mixer(AppListModel::SearchResults* ui_results) |
124 : ui_results_(ui_results) { | 119 : ui_results_(ui_results) { |
125 } | 120 } |
126 Mixer::~Mixer() { | 121 Mixer::~Mixer() { |
127 } | 122 } |
128 | 123 |
129 void Mixer::Init() { | 124 size_t Mixer::AddGroup(size_t max_results, double boost) { |
130 groups_[MAIN_GROUP].reset(new Group(kMaxMainGroupResults, 3.0)); | 125 groups_.push_back(new Group(max_results, boost)); |
131 groups_[OMNIBOX_GROUP].reset(new Group(kNoMaxResultsLimit, 2.0)); | 126 return groups_.size() - 1; |
132 groups_[WEBSTORE_GROUP].reset(new Group(kMaxWebstoreResults, 1.0)); | |
133 groups_[PEOPLE_GROUP].reset(new Group(kMaxPeopleResults, 0.0)); | |
134 groups_[SUGGESTIONS_GROUP].reset(new Group(kMaxSuggestionsResults, 3.0)); | |
135 groups_[LAUNCHER_SEARCH_API_GROUP].reset( | |
136 new Group(kMaxLauncherSearchResults, 0.0)); | |
137 } | 127 } |
138 | 128 |
139 void Mixer::AddProviderToGroup(GroupId group, SearchProvider* provider) { | 129 size_t Mixer::AddOmniboxGroup(size_t max_results, double boost) { |
140 groups_[group]->AddProvider(provider); | 130 // There should not already be an omnibox group. |
| 131 DCHECK(!has_omnibox_group_); |
| 132 size_t id = AddGroup(max_results, boost); |
| 133 omnibox_group_ = id; |
| 134 has_omnibox_group_ = true; |
| 135 return id; |
| 136 } |
| 137 |
| 138 void Mixer::AddProviderToGroup(size_t group_id, SearchProvider* provider) { |
| 139 groups_[group_id]->AddProvider(provider); |
141 } | 140 } |
142 | 141 |
143 void Mixer::MixAndPublish(bool is_voice_query, | 142 void Mixer::MixAndPublish(bool is_voice_query, |
144 const KnownResults& known_results) { | 143 const KnownResults& known_results) { |
145 FetchResults(is_voice_query, known_results); | 144 FetchResults(is_voice_query, known_results); |
146 | 145 |
147 SortedResults results; | 146 SortedResults results; |
148 results.reserve(kMaxResults); | 147 results.reserve(kMaxResults); |
149 | 148 |
150 const Group& main_group = *groups_[MAIN_GROUP]; | 149 // Add results from non-omnibox groups first. |
151 const Group& omnibox_group = *groups_[OMNIBOX_GROUP]; | 150 for (size_t i = 0; i < groups_.size(); ++i) { |
152 const Group& webstore_group = *groups_[WEBSTORE_GROUP]; | 151 if (!has_omnibox_group_ || i != omnibox_group_) { |
153 const Group& people_group = *groups_[PEOPLE_GROUP]; | 152 const Group& group = *groups_[i]; |
154 const Group& suggestions_group = *groups_[SUGGESTIONS_GROUP]; | 153 results.insert(results.end(), group.results().begin(), |
155 | 154 group.results().end()); |
156 // Adds main group and web store results first. | 155 } |
157 results.insert(results.end(), main_group.results().begin(), | 156 } |
158 main_group.results().end()); | |
159 results.insert(results.end(), webstore_group.results().begin(), | |
160 webstore_group.results().end()); | |
161 results.insert(results.end(), people_group.results().begin(), | |
162 people_group.results().end()); | |
163 results.insert(results.end(), suggestions_group.results().begin(), | |
164 suggestions_group.results().end()); | |
165 | 157 |
166 // Collapse duplicate apps from local and web store. | 158 // Collapse duplicate apps from local and web store. |
167 RemoveDuplicates(&results); | 159 RemoveDuplicates(&results); |
168 | 160 |
169 // Fill the remaining slots with omnibox results. Always add at least one | 161 // Fill the remaining slots with omnibox results. Always add at least one |
170 // omnibox result (even if there are no more slots; if we over-fill the | 162 // omnibox result (even if there are no more slots; if we over-fill the |
171 // vector, the web store and people results will be removed in a later step). | 163 // vector, the web store and people results will be removed in a later step). |
172 const size_t omnibox_results = | 164 if (has_omnibox_group_) { |
173 std::min(omnibox_group.results().size(), | 165 CHECK_LT(omnibox_group_, groups_.size()); |
174 results.size() < kMaxResults ? kMaxResults - results.size() : 1); | 166 const Group& omnibox_group = *groups_[omnibox_group_]; |
175 results.insert(results.end(), omnibox_group.results().begin(), | 167 const size_t omnibox_results = std::min( |
176 omnibox_group.results().begin() + omnibox_results); | 168 omnibox_group.results().size(), |
| 169 results.size() < kMaxResults ? kMaxResults - results.size() : 1); |
| 170 results.insert(results.end(), omnibox_group.results().begin(), |
| 171 omnibox_group.results().begin() + omnibox_results); |
| 172 } |
177 | 173 |
178 std::sort(results.begin(), results.end()); | 174 std::sort(results.begin(), results.end()); |
179 RemoveDuplicates(&results); | 175 RemoveDuplicates(&results); |
180 if (results.size() > kMaxResults) | 176 if (results.size() > kMaxResults) |
181 results.resize(kMaxResults); | 177 results.resize(kMaxResults); |
182 | 178 |
183 Publish(results, ui_results_); | 179 Publish(results, ui_results_); |
184 } | 180 } |
185 | 181 |
186 void Mixer::Publish(const SortedResults& new_results, | 182 void Mixer::Publish(const SortedResults& new_results, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 242 |
247 id_set.insert(id); | 243 id_set.insert(id); |
248 final.push_back(sort_data); | 244 final.push_back(sort_data); |
249 } | 245 } |
250 | 246 |
251 results->swap(final); | 247 results->swap(final); |
252 } | 248 } |
253 | 249 |
254 void Mixer::FetchResults(bool is_voice_query, | 250 void Mixer::FetchResults(bool is_voice_query, |
255 const KnownResults& known_results) { | 251 const KnownResults& known_results) { |
256 for (const auto& item : groups_) | 252 for (auto* group : groups_) |
257 item.second->FetchResults(is_voice_query, known_results); | 253 group->FetchResults(is_voice_query, known_results); |
258 } | 254 } |
259 | 255 |
260 } // namespace app_list | 256 } // namespace app_list |
OLD | NEW |