| 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 #ifndef UI_APP_LIST_SEARCH_MIXER_H_ | 5 #ifndef UI_APP_LIST_SEARCH_MIXER_H_ |
| 6 #define UI_APP_LIST_SEARCH_MIXER_H_ | 6 #define UI_APP_LIST_SEARCH_MIXER_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 #include <vector> | 8 #include <vector> |
| 10 | 9 |
| 11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 12 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/scoped_vector.h" |
| 14 #include "ui/app_list/app_list_export.h" | 13 #include "ui/app_list/app_list_export.h" |
| 15 #include "ui/app_list/app_list_model.h" | 14 #include "ui/app_list/app_list_model.h" |
| 16 #include "ui/app_list/search/history_types.h" | 15 #include "ui/app_list/search/history_types.h" |
| 17 | 16 |
| 18 namespace app_list { | 17 namespace app_list { |
| 19 | 18 |
| 20 namespace test { | 19 namespace test { |
| 21 FORWARD_DECLARE_TEST(MixerTest, Publish); | 20 FORWARD_DECLARE_TEST(MixerTest, Publish); |
| 22 } | 21 } |
| 23 | 22 |
| 24 class SearchProvider; | 23 class SearchProvider; |
| 25 class SearchResult; | 24 class SearchResult; |
| 26 | 25 |
| 27 // Mixer collects results from providers, sorts them and publishes them to the | 26 // Mixer collects results from providers, sorts them and publishes them to the |
| 28 // SearchResults UI model. The targeted results have 6 slots to hold the | 27 // SearchResults UI model. The targeted results have 6 slots to hold the |
| 29 // result. These slots could be viewed as having three groups: main group | 28 // result. The search controller can specify any number of groups, each with a |
| 30 // (local apps and contacts), omnibox group and web store group. The | 29 // different number of results and priority boost. The "omnibox" group is |
| 31 // main group takes no more than 4 slots. The web store takes no more than 2 | 30 // expected to contain omnibox results, and will be treated specially. |
| 32 // slots. The omnibox group takes all the remaining slots. | |
| 33 class APP_LIST_EXPORT Mixer { | 31 class APP_LIST_EXPORT Mixer { |
| 34 public: | 32 public: |
| 35 // The enum represents mixer groups. Each must have a Group added in Init(). | |
| 36 enum GroupId { | |
| 37 MAIN_GROUP = 0, | |
| 38 OMNIBOX_GROUP = 1, | |
| 39 WEBSTORE_GROUP = 2, | |
| 40 PEOPLE_GROUP = 3, | |
| 41 SUGGESTIONS_GROUP = 4, | |
| 42 LAUNCHER_SEARCH_API_GROUP = 5 | |
| 43 }; | |
| 44 | |
| 45 explicit Mixer(AppListModel::SearchResults* ui_results); | 33 explicit Mixer(AppListModel::SearchResults* ui_results); |
| 46 ~Mixer(); | 34 ~Mixer(); |
| 47 | 35 |
| 48 // Creates mixer groups. | 36 // Adds a new mixer group. A maximum of |max_results| results will be |
| 49 void Init(); | 37 // displayed from this group (if 0, will allow unlimited results from this |
| 38 // group). Each result in the group will have its score boosted by |boost|. |
| 39 // Returns the group's group_id. |
| 40 size_t AddGroup(size_t max_results, double boost); |
| 41 |
| 42 // Adds a new mixer group for the special "omnibox" group. This group will be |
| 43 // treated specially by the Mixer (it will be truncated such that it fills the |
| 44 // remaining slots without overflowing, but with at least one result). A |
| 45 // maximum of one group should be added using this method. |
| 46 size_t AddOmniboxGroup(size_t max_results, double boost); |
| 50 | 47 |
| 51 // Associates a provider with a mixer group. | 48 // Associates a provider with a mixer group. |
| 52 void AddProviderToGroup(GroupId group, SearchProvider* provider); | 49 void AddProviderToGroup(size_t group_id, SearchProvider* provider); |
| 53 | 50 |
| 54 // Collects the results, sorts and publishes them. | 51 // Collects the results, sorts and publishes them. |
| 55 void MixAndPublish(bool is_voice_query, const KnownResults& known_results); | 52 void MixAndPublish(bool is_voice_query, const KnownResults& known_results); |
| 56 | 53 |
| 57 private: | 54 private: |
| 58 FRIEND_TEST_ALL_PREFIXES(test::MixerTest, Publish); | 55 FRIEND_TEST_ALL_PREFIXES(test::MixerTest, Publish); |
| 59 | 56 |
| 60 // Used for sorting and mixing results. | 57 // Used for sorting and mixing results. |
| 61 struct APP_LIST_EXPORT SortData { | 58 struct APP_LIST_EXPORT SortData { |
| 62 SortData(); | 59 SortData(); |
| 63 SortData(SearchResult* result, double score); | 60 SortData(SearchResult* result, double score); |
| 64 | 61 |
| 65 bool operator<(const SortData& other) const; | 62 bool operator<(const SortData& other) const; |
| 66 | 63 |
| 67 SearchResult* result; // Not owned. | 64 SearchResult* result; // Not owned. |
| 68 double score; | 65 double score; |
| 69 }; | 66 }; |
| 70 typedef std::vector<Mixer::SortData> SortedResults; | 67 typedef std::vector<Mixer::SortData> SortedResults; |
| 71 | 68 |
| 72 class Group; | 69 class Group; |
| 73 typedef std::map<GroupId, linked_ptr<Group>> Groups; | 70 typedef ScopedVector<Group> Groups; |
| 74 | 71 |
| 75 // Publishes the given |new_results| to |ui_results|, deleting any existing | 72 // Publishes the given |new_results| to |ui_results|, deleting any existing |
| 76 // results that are not in |new_results|. Results that already exist in | 73 // results that are not in |new_results|. Results that already exist in |
| 77 // |ui_results| are reused to avoid flickering caused by icon reload. | 74 // |ui_results| are reused to avoid flickering caused by icon reload. |
| 78 static void Publish(const SortedResults& results, | 75 static void Publish(const SortedResults& results, |
| 79 AppListModel::SearchResults* ui_results); | 76 AppListModel::SearchResults* ui_results); |
| 80 | 77 |
| 81 // Removes duplicates from |results|. | 78 // Removes duplicates from |results|. |
| 82 static void RemoveDuplicates(SortedResults* results); | 79 static void RemoveDuplicates(SortedResults* results); |
| 83 | 80 |
| 84 void FetchResults(bool is_voice_query, const KnownResults& known_results); | 81 void FetchResults(bool is_voice_query, const KnownResults& known_results); |
| 85 | 82 |
| 86 AppListModel::SearchResults* ui_results_; // Not owned. | 83 AppListModel::SearchResults* ui_results_; // Not owned. |
| 87 Groups groups_; | 84 Groups groups_; |
| 88 | 85 |
| 86 // The ID of the omnibox group. The group with this ID will be treated |
| 87 // specially by the Mixer. |
| 88 // TODO(mgiuca): Omnibox group should not be treated specially. |
| 89 size_t omnibox_group_; |
| 90 // Whether |omnibox_group_| has been set. |
| 91 bool has_omnibox_group_; |
| 92 |
| 89 DISALLOW_COPY_AND_ASSIGN(Mixer); | 93 DISALLOW_COPY_AND_ASSIGN(Mixer); |
| 90 }; | 94 }; |
| 91 | 95 |
| 92 } // namespace app_list | 96 } // namespace app_list |
| 93 | 97 |
| 94 #endif // UI_APP_LIST_SEARCH_MIXER_H_ | 98 #endif // UI_APP_LIST_SEARCH_MIXER_H_ |
| OLD | NEW |