Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: chrome/browser/ui/app_list/search/mixer.cc

Issue 621823004: Simplifies the structure of app_list search a bit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/ui/app_list/search/mixer.h" 5 #include "chrome/browser/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 "chrome/browser/ui/app_list/search/chrome_search_result.h"
14 #include "ui/app_list/search_provider.h" 13 #include "ui/app_list/search_provider.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; 22 const size_t kMaxMainGroupResults = 4;
23 const size_t kMaxWebstoreResults = 2; 23 const size_t kMaxWebstoreResults = 2;
24 const size_t kMaxPeopleResults = 2; 24 const size_t kMaxPeopleResults = 2;
25 25
26 // A value to indicate no max number of results limit. 26 // A value to indicate no max number of results limit.
27 const size_t kNoMaxResultsLimit = 0; 27 const size_t kNoMaxResultsLimit = 0;
28 28
29 void UpdateResult(const ChromeSearchResult& source, 29 void UpdateResult(const SearchResult& source, SearchResult* target) {
30 ChromeSearchResult* target) {
31 target->set_title(source.title()); 30 target->set_title(source.title());
32 target->set_title_tags(source.title_tags()); 31 target->set_title_tags(source.title_tags());
33 target->set_details(source.details()); 32 target->set_details(source.details());
34 target->set_details_tags(source.details_tags()); 33 target->set_details_tags(source.details_tags());
35 } 34 }
36 35
37 } // namespace 36 } // namespace
38 37
39 Mixer::SortData::SortData() : result(NULL), score(0.0) { 38 Mixer::SortData::SortData() : result(NULL), score(0.0) {
40 } 39 }
41 40
42 Mixer::SortData::SortData(ChromeSearchResult* result, double score) 41 Mixer::SortData::SortData(SearchResult* result, double score)
43 : result(result), score(score) { 42 : result(result), score(score) {
44 } 43 }
45 44
46 bool Mixer::SortData::operator<(const SortData& other) const { 45 bool Mixer::SortData::operator<(const SortData& other) const {
47 // This data precedes (less than) |other| if it has higher score. 46 // This data precedes (less than) |other| if it has higher score.
48 return score > other.score; 47 return score > other.score;
49 } 48 }
50 49
51 // Used to group relevant providers together fox mixing their results. 50 // Used to group relevant providers together fox mixing their results.
52 class Mixer::Group { 51 class Mixer::Group {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 case PREFIX_SECONDARY: 91 case PREFIX_SECONDARY:
93 boost = 3.0; 92 boost = 3.0;
94 break; 93 break;
95 case UNKNOWN_RESULT: 94 case UNKNOWN_RESULT:
96 NOTREACHED() << "Unknown result in KnownResults?"; 95 NOTREACHED() << "Unknown result in KnownResults?";
97 break; 96 break;
98 } 97 }
99 } 98 }
100 99
101 results_.push_back( 100 results_.push_back(
102 SortData(static_cast<ChromeSearchResult*>(*result_it), 101 SortData(*result_it, (*result_it)->relevance() + boost));
103 (*result_it)->relevance() + boost));
104 } 102 }
105 } 103 }
106 104
107 std::sort(results_.begin(), results_.end()); 105 std::sort(results_.begin(), results_.end());
108 if (max_results_ != kNoMaxResultsLimit && results_.size() > max_results_) 106 if (max_results_ != kNoMaxResultsLimit && results_.size() > max_results_)
109 results_.resize(max_results_); 107 results_.resize(max_results_);
110 } 108 }
111 109
112 const SortedResults& results() const { return results_; } 110 const SortedResults& results() const { return results_; }
113 111
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 std::sort(results.begin(), results.end()); 173 std::sort(results.begin(), results.end());
176 RemoveDuplicates(&results); 174 RemoveDuplicates(&results);
177 if (results.size() > kMaxResults) 175 if (results.size() > kMaxResults)
178 results.resize(kMaxResults); 176 results.resize(kMaxResults);
179 177
180 Publish(results, ui_results_); 178 Publish(results, ui_results_);
181 } 179 }
182 180
183 void Mixer::Publish(const SortedResults& new_results, 181 void Mixer::Publish(const SortedResults& new_results,
184 AppListModel::SearchResults* ui_results) { 182 AppListModel::SearchResults* ui_results) {
185 typedef std::map<std::string, ChromeSearchResult*> IdToResultMap; 183 typedef std::map<std::string, SearchResult*> IdToResultMap;
186 184
187 // The following algorithm is used: 185 // The following algorithm is used:
188 // 1. Transform the |ui_results| list into an unordered map from result ID 186 // 1. Transform the |ui_results| list into an unordered map from result ID
189 // to item. 187 // to item.
190 // 2. Use the order of items in |new_results| to build an ordered list. If the 188 // 2. Use the order of items in |new_results| to build an ordered list. If the
191 // result IDs exist in the map, update and use the item in the map and delete 189 // result IDs exist in the map, update and use the item in the map and delete
192 // it from the map afterwards. Otherwise, clone new items from |new_results|. 190 // it from the map afterwards. Otherwise, clone new items from |new_results|.
193 // 3. Delete the objects remaining in the map as they are unused. 191 // 3. Delete the objects remaining in the map as they are unused.
194 192
195 // A map of the items in |ui_results| that takes ownership of the items. 193 // A map of the items in |ui_results| that takes ownership of the items.
196 IdToResultMap ui_results_map; 194 IdToResultMap ui_results_map;
197 for (size_t i = 0; i < ui_results->item_count(); ++i) { 195 for (size_t i = 0; i < ui_results->item_count(); ++i) {
198 ChromeSearchResult* ui_result = 196 SearchResult* ui_result = ui_results->GetItemAt(i);
199 static_cast<ChromeSearchResult*>(ui_results->GetItemAt(i));
200 ui_results_map[ui_result->id()] = ui_result; 197 ui_results_map[ui_result->id()] = ui_result;
201 } 198 }
202 // We have to erase all results at once so that observers are notified with 199 // We have to erase all results at once so that observers are notified with
203 // meaningful indexes. 200 // meaningful indexes.
204 ui_results->RemoveAll(); 201 ui_results->RemoveAll();
205 202
206 // Add items back to |ui_results| in the order of |new_results|. 203 // Add items back to |ui_results| in the order of |new_results|.
207 for (size_t i = 0; i < new_results.size(); ++i) { 204 for (size_t i = 0; i < new_results.size(); ++i) {
208 ChromeSearchResult* new_result = new_results[i].result; 205 SearchResult* new_result = new_results[i].result;
209 IdToResultMap::const_iterator ui_result_it = 206 IdToResultMap::const_iterator ui_result_it =
210 ui_results_map.find(new_result->id()); 207 ui_results_map.find(new_result->id());
211 if (ui_result_it != ui_results_map.end()) { 208 if (ui_result_it != ui_results_map.end()) {
212 // Update and use the old result if it exists. 209 // Update and use the old result if it exists.
213 ChromeSearchResult* ui_result = ui_result_it->second; 210 SearchResult* ui_result = ui_result_it->second;
214 UpdateResult(*new_result, ui_result); 211 UpdateResult(*new_result, ui_result);
215 212
216 // |ui_results| takes back ownership from |ui_results_map| here. 213 // |ui_results| takes back ownership from |ui_results_map| here.
217 ui_results->Add(ui_result); 214 ui_results->Add(ui_result);
218 215
219 // Remove the item from the map so that it ends up only with unused 216 // Remove the item from the map so that it ends up only with unused
220 // results. 217 // results.
221 ui_results_map.erase(ui_result->id()); 218 ui_results_map.erase(ui_result->id());
222 } else { 219 } else {
223 // Copy the result from |new_results| otherwise. 220 // Copy the result from |new_results| otherwise.
(...skipping 29 matching lines...) Expand all
253 250
254 void Mixer::FetchResults(const KnownResults& known_results) { 251 void Mixer::FetchResults(const KnownResults& known_results) {
255 for (Groups::iterator group_it = groups_.begin(); 252 for (Groups::iterator group_it = groups_.begin();
256 group_it != groups_.end(); 253 group_it != groups_.end();
257 ++group_it) { 254 ++group_it) {
258 (*group_it)->FetchResults(known_results); 255 (*group_it)->FetchResults(known_results);
259 } 256 }
260 } 257 }
261 258
262 } // namespace app_list 259 } // namespace app_list
OLDNEW
« no previous file with comments | « chrome/browser/ui/app_list/search/mixer.h ('k') | chrome/browser/ui/app_list/search/mixer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698