Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / app_list / search / mixer.h
blobc4eea8b7c3d2d1b4287ea6ade29a5651013755c3
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 <map>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/linked_ptr.h"
14 #include "ui/app_list/app_list_export.h"
15 #include "ui/app_list/app_list_model.h"
16 #include "ui/app_list/search/history_types.h"
18 namespace app_list {
20 namespace test {
21 FORWARD_DECLARE_TEST(MixerTest, Publish);
24 class SearchProvider;
25 class SearchResult;
27 // 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
29 // result. These slots could be viewed as having three groups: main group
30 // (local apps and contacts), omnibox group and web store group. The
31 // main group takes no more than 4 slots. The web store takes no more than 2
32 // slots. The omnibox group takes all the remaining slots.
33 class APP_LIST_EXPORT Mixer {
34 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
45 explicit Mixer(AppListModel::SearchResults* ui_results);
46 ~Mixer();
48 // Creates mixer groups.
49 void Init();
51 // Associates a provider with a mixer group.
52 void AddProviderToGroup(GroupId group, SearchProvider* provider);
54 // Collects the results, sorts and publishes them.
55 void MixAndPublish(bool is_voice_query, const KnownResults& known_results);
57 private:
58 FRIEND_TEST_ALL_PREFIXES(test::MixerTest, Publish);
60 // Used for sorting and mixing results.
61 struct APP_LIST_EXPORT SortData {
62 SortData();
63 SortData(SearchResult* result, double score);
65 bool operator<(const SortData& other) const;
67 SearchResult* result; // Not owned.
68 double score;
70 typedef std::vector<Mixer::SortData> SortedResults;
72 class Group;
73 typedef std::map<GroupId, linked_ptr<Group>> Groups;
75 // Publishes the given |new_results| to |ui_results|, deleting any existing
76 // results that are not in |new_results|. Results that already exist in
77 // |ui_results| are reused to avoid flickering caused by icon reload.
78 static void Publish(const SortedResults& results,
79 AppListModel::SearchResults* ui_results);
81 // Removes duplicates from |results|.
82 static void RemoveDuplicates(SortedResults* results);
84 void FetchResults(bool is_voice_query, const KnownResults& known_results);
86 AppListModel::SearchResults* ui_results_; // Not owned.
87 Groups groups_;
89 DISALLOW_COPY_AND_ASSIGN(Mixer);
92 } // namespace app_list
94 #endif // UI_APP_LIST_SEARCH_MIXER_H_