Reland of Fix crash on signing in when Google Play Services is out of date. (patchset...
[chromium-blink-merge.git] / ui / app_list / search / mixer.h
blobea650d28df327e6895442f2b5540e616a2346f27
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
3 // found in the LICENSE file.
5 #ifndef UI_APP_LIST_SEARCH_MIXER_H_
6 #define UI_APP_LIST_SEARCH_MIXER_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_vector.h"
13 #include "ui/app_list/app_list_export.h"
14 #include "ui/app_list/app_list_model.h"
15 #include "ui/app_list/search/history_types.h"
17 namespace app_list {
19 namespace test {
20 FORWARD_DECLARE_TEST(MixerTest, Publish);
23 class SearchProvider;
24 class SearchResult;
26 // Mixer collects results from providers, sorts them and publishes them to the
27 // SearchResults UI model. The targeted results have 6 slots to hold the
28 // result. The search controller can specify any number of groups, each with a
29 // different number of results and priority boost. The "omnibox" group is
30 // expected to contain omnibox results, and will be treated specially.
31 class APP_LIST_EXPORT Mixer {
32 public:
33 explicit Mixer(AppListModel::SearchResults* ui_results);
34 ~Mixer();
36 // Adds a new mixer group. A maximum of |max_results| results will be
37 // displayed from this group (if 0, will allow unlimited results from this
38 // group). Behaviour depends on the AppListMixer field trial:
39 // - If default: Each result in the group will have its score boosted by
40 // |boost|. |multiplier| is ignored.
41 // - If "Blended": |max_results| is a "soft" maximum; if there aren't enough
42 // results from all groups, more than |max_results| may be chosen from this
43 // group. Each result in the group will have its score multiplied by
44 // |multiplier|. |boost| is ignored.
45 // Returns the group's group_id.
46 size_t AddGroup(size_t max_results, double boost, double multiplier);
48 // Adds a new mixer group for the special "omnibox" group. This group will be
49 // treated specially by the Mixer (it will be truncated such that it fills the
50 // remaining slots without overflowing, but with at least one result). A
51 // maximum of one group should be added using this method.
52 size_t AddOmniboxGroup(size_t max_results, double boost, double multiplier);
54 // Associates a provider with a mixer group.
55 void AddProviderToGroup(size_t group_id, SearchProvider* provider);
57 // Collects the results, sorts and publishes them.
58 void MixAndPublish(bool is_voice_query, const KnownResults& known_results);
60 private:
61 FRIEND_TEST_ALL_PREFIXES(test::MixerTest, Publish);
63 // Used for sorting and mixing results.
64 struct APP_LIST_EXPORT SortData {
65 SortData();
66 SortData(SearchResult* result, double score);
68 bool operator<(const SortData& other) const;
70 SearchResult* result; // Not owned.
71 double score;
73 typedef std::vector<Mixer::SortData> SortedResults;
75 class Group;
76 typedef ScopedVector<Group> Groups;
78 // Publishes the given |new_results| to |ui_results|, deleting any existing
79 // results that are not in |new_results|. Results that already exist in
80 // |ui_results| are reused to avoid flickering caused by icon reload.
81 static void Publish(const SortedResults& results,
82 AppListModel::SearchResults* ui_results);
84 // Removes entries from |results| with duplicate IDs. When two or more results
85 // have the same ID, the earliest one in the |results| list is kept.
86 // NOTE: This is not necessarily the one with the highest *score*, as
87 // |results| may not have been sorted yet.
88 static void RemoveDuplicates(SortedResults* results);
90 void FetchResults(bool is_voice_query, const KnownResults& known_results);
92 AppListModel::SearchResults* ui_results_; // Not owned.
93 Groups groups_;
95 // The ID of the omnibox group. The group with this ID will be treated
96 // specially by the Mixer. Ignored if the AppListMixer field trial is
97 // "Blended".
98 // TODO(mgiuca): Remove this after the field trial is complete.
99 size_t omnibox_group_ = 0;
100 // Whether |omnibox_group_| has been set.
101 bool has_omnibox_group_ = false;
103 DISALLOW_COPY_AND_ASSIGN(Mixer);
106 } // namespace app_list
108 #endif // UI_APP_LIST_SEARCH_MIXER_H_