1 // Copyright 2014 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_SEARCH_PROVIDER_H_
6 #define CHROME_BROWSER_UI_APP_LIST_SEARCH_SEARCH_PROVIDER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "ash/public/cpp/app_list/app_list_types.h"
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/strings/string16.h"
16 
17 class ChromeSearchResult;
18 
19 namespace app_list {
20 
21 enum class RankingItemType;
22 
23 class SearchProvider {
24  public:
25   using Results = std::vector<std::unique_ptr<ChromeSearchResult>>;
26   using ResultChangedCallback = base::Closure;
27 
28   SearchProvider();
29   virtual ~SearchProvider();
30 
31   // Invoked to start a query.
32   virtual void Start(const base::string16& query) = 0;
33   // Invoked when the UI view closes. In response, the |SearchProvider| may
34   // clear its caches.
ViewClosing()35   virtual void ViewClosing() {}
36   // Handles training signals if necessary. A given |SearchProvider| may receive
37   // training signals for results of any |RankingItemType|, so it is the
38   // |SearchProvider|'s responsibility to check |type| and ignore if necessary.
Train(const std::string & id,RankingItemType type)39   virtual void Train(const std::string& id, RankingItemType type) {}
40   // Invoked when the app list is shown. This can optionally be used by a
41   // provider to eg. warm up a cache of results.
AppListShown()42   virtual void AppListShown() {}
43   // Returns the main result type created by this provider.
44   virtual ash::AppListSearchResultType ResultType() = 0;
45 
set_result_changed_callback(const ResultChangedCallback & callback)46   void set_result_changed_callback(const ResultChangedCallback& callback) {
47     result_changed_callback_ = callback;
48   }
49 
results()50   const Results& results() const { return results_; }
51 
52  protected:
53   // Interface for the derived class to generate search results.
54   void Add(std::unique_ptr<ChromeSearchResult> result);
55 
56   // Swaps the internal results with |new_results|.
57   // This is useful when multiple results will be added, and the notification is
58   // desired to be done only once when all results are added.
59   void SwapResults(Results* new_results);
60 
61   // Clear results and call the |result_changed_callback_|.
62   void ClearResults();
63 
64   // Clear the results without calling the |result_changed_callback_|.
65   void ClearResultsSilently();
66 
67  private:
68   void FireResultChanged();
69 
70   ResultChangedCallback result_changed_callback_;
71   Results results_;
72 
73   DISALLOW_COPY_AND_ASSIGN(SearchProvider);
74 };
75 
76 }  // namespace app_list
77 
78 #endif  // CHROME_BROWSER_UI_APP_LIST_SEARCH_SEARCH_PROVIDER_H_
79