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.
4
5#ifndef CHROME_BROWSER_UI_APP_LIST_SEARCH_MIXER_H_
6#define CHROME_BROWSER_UI_APP_LIST_SEARCH_MIXER_H_
7
8#include "base/basictypes.h"
9#include "base/gtest_prod_util.h"
10#include "base/memory/scoped_vector.h"
11#include "chrome/browser/ui/app_list/search/history_types.h"
12#include "ui/app_list/app_list_model.h"
13
14namespace app_list {
15
16namespace test {
17FORWARD_DECLARE_TEST(MixerTest, Publish);
18}
19
20class ChromeSearchResult;
21class SearchProvider;
22
23// Mixer collects results from providers, sorts them and publishes them to the
24// SearchResults UI model. The targeted results have 6 slots to hold the
25// result. These slots could be viewed as having three groups: main group
26// (local apps and contacts), omnibox group and web store group. The
27// main group takes no more than 4 slots. The web store takes no more than 2
28// slots. The omnibox group takes all the remaining slots.
29class Mixer {
30 public:
31  // The enum represents mixer groups. Note this must matches the order
32  // of group creation in Init().
33  enum GroupId {
34    MAIN_GROUP = 0,
35    OMNIBOX_GROUP = 1,
36    WEBSTORE_GROUP = 2,
37    PEOPLE_GROUP = 3,
38  };
39
40  explicit Mixer(AppListModel::SearchResults* ui_results);
41  ~Mixer();
42
43  // Creates mixer groups.
44  void Init();
45
46  // Associates a provider with a mixer group.
47  void AddProviderToGroup(GroupId group, SearchProvider* provider);
48
49  // Collects the results, sorts and publishes them.
50  void MixAndPublish(const KnownResults& known_results);
51
52 private:
53  FRIEND_TEST_ALL_PREFIXES(test::MixerTest, Publish);
54
55  // Used for sorting and mixing results.
56  struct SortData {
57    SortData();
58    SortData(ChromeSearchResult* result, double score);
59
60    bool operator<(const SortData& other) const;
61
62    ChromeSearchResult* result;  // Not owned.
63    double score;
64  };
65  typedef std::vector<Mixer::SortData> SortedResults;
66
67  class Group;
68  typedef ScopedVector<Group> Groups;
69
70  // Publishes the given |new_results| to |ui_results|, deleting any existing
71  // results that are not in |new_results|. Results that already exist in
72  // |ui_results| are reused to avoid flickering caused by icon reload.
73  static void Publish(const SortedResults& results,
74                      AppListModel::SearchResults* ui_results);
75
76  // Removes duplicates from |results|.
77  static void RemoveDuplicates(SortedResults* results);
78
79  void FetchResults(const KnownResults& known_results);
80
81  AppListModel::SearchResults* ui_results_;  // Not owned.
82  Groups groups_;
83
84  DISALLOW_COPY_AND_ASSIGN(Mixer);
85};
86
87}  // namespace app_list
88
89#endif  // CHROME_BROWSER_UI_APP_LIST_SEARCH_MIXER_H_
90