File: search_controller.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (249 lines) | stat: -rw-r--r-- 8,520 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_APP_LIST_SEARCH_SEARCH_CONTROLLER_H_
#define CHROME_BROWSER_ASH_APP_LIST_SEARCH_SEARCH_CONTROLLER_H_

#include <stddef.h>

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/ash/app_list/search/app_discovery_metrics_manager.h"
#include "chrome/browser/ash/app_list/search/burn_in_controller.h"
#include "chrome/browser/ash/app_list/search/common/keyword_util.h"
#include "chrome/browser/ash/app_list/search/federated_metrics_manager.h"
#include "chrome/browser/ash/app_list/search/ranking/launch_data.h"
#include "chrome/browser/ash/app_list/search/ranking/ranker_manager.h"
#include "chrome/browser/ash/app_list/search/search_file_scanner.h"
#include "chrome/browser/ash/app_list/search/types.h"

class AppListControllerDelegate;
class AppListModelUpdater;
class ChromeSearchResult;
class Profile;

namespace ash {
class AppListNotifier;

namespace federated {
class FederatedServiceController;
}  // namespace federated

}  // namespace ash

namespace app_list {

class AppSearchDataSource;
class SearchMetricsManager;
class SearchSessionMetricsManager;
class SearchProvider;
class SearchEngine;

// Long queries will be truncated down to this length.
inline constexpr int kMaxAllowedQueryLength = 500;

// A controller that collects queries from the AppListClient, dispatches them to
// search providers, then ranks and publishes the results to the AppListModel.
// Many methods are virtual for testing.
class SearchController {
 public:
  using ResultsChangedCallback = base::RepeatingCallback<void(ResultType)>;

  SearchController(AppListModelUpdater* model_updater,
                   AppListControllerDelegate* list_controller,
                   ash::AppListNotifier* notifier,
                   Profile* profile,
                   ash::federated::FederatedServiceController*
                       federated_service_controller_);
  virtual ~SearchController();

  SearchController(const SearchController&) = delete;
  SearchController& operator=(const SearchController&) = delete;

  class Observer : public base::CheckedObserver {
   public:
    // Called whenever results are added to the launcher, as a result of
    // zero-state or from a user query. This will be called multiple times per
    // query because launcher results arrive incrementally.
    //
    // Observers should not store the ChromeSearchResult* pointers or post them
    // to another sequence because they may be invalidated.
    virtual void OnResultsAdded(
        const std::u16string& query,
        const std::vector<KeywordInfo>& extracted_keyword_info,
        const std::vector<const ChromeSearchResult*>& results) {}
  };

  // Initializes required members of the SearchController. Must be called at
  // construction time.
  // This is separate from the constructor itself so that it can be omitted for
  // the TestSearchController mock.
  void Initialize();

  // Returns the search categories that are available for users to choose if
  // they want to have the results in the categories displayed in launcher
  // search.
  std::vector<ash::AppListSearchControlCategory> GetToggleableCategories()
      const;

  // Takes ownership of |provider|.
  virtual void AddProvider(std::unique_ptr<SearchProvider> provider);

  virtual void StartSearch(const std::u16string& query);
  virtual void ClearSearch();

  virtual void StartZeroState(base::OnceClosure on_done,
                              base::TimeDelta timeout);

  // Callback made when app list view is open or closed. |is_visible| should be
  // true when the view is open.
  void AppListViewChanging(bool is_visible);

  void OpenResult(ChromeSearchResult* result, int event_flags);
  void InvokeResultAction(ChromeSearchResult* result,
                          ash::SearchResultActionType action);

  // Update the controller with the given results.
  virtual void SetResults(ResultType result_type, Results results);

  // Publishes results to ash.
  void Publish();

  // Sends training signal to each of |providers_|.
  void Train(LaunchData&& launch_data);

  // Returns the AppSearchDataSource instance that should be used with app
  // search providers.
  AppSearchDataSource* GetAppSearchDataSource();

  ChromeSearchResult* FindSearchResult(const std::string& result_id);

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  void OnDefaultSearchIsGoogleSet(bool is_google);

  std::u16string get_query();

  base::Time session_start();

  // Test methods.

  // Removes, and deletes registered search providers that provide results for
  // `result_type` and adds a new "test" provider.
  // No-op if no providers for `result_type` were previously registered.
  // Expects that `provider` provides results for `result_type`.
  // Returns number of providers removed from the provider list.
  size_t ReplaceProvidersForResultTypeForTest(
      ResultType result_type,
      std::unique_ptr<SearchProvider> provider);

  ChromeSearchResult* GetResultByTitleForTest(const std::string& title);

  virtual void WaitForZeroStateCompletionForTest(base::OnceClosure callback);

  virtual void set_results_changed_callback_for_test(
      ResultsChangedCallback callback);

  void disable_ranking_for_test();

  void set_ranker_manager_for_test(
      std::unique_ptr<RankerManager> ranker_manager) {
    ranker_manager_ = std::move(ranker_manager);
  }

  BurnInController* burn_in_controller_for_test() {
    return burn_in_controller_.get();
  }

  const CategoriesList& categories_for_test() { return categories_; }

 private:
  // Rank the results of |provider_type|.
  void Rank(ResultType provider_type);

  void SetSearchResults(ResultType result_type);

  void SetZeroStateResults(ResultType result_type);

  void OnZeroStateTimedOut();

  void OnBurnInPeriodElapsed();

  void OnResultsChangedWithType(ResultType result_type);

  // The query associated with the most recent search.
  std::u16string last_query_;

  // How many search providers should block zero-state until they return
  // results.
  int total_zero_state_blockers_ = 0;

  // How many zero-state blocking providers have returned for this search.
  int returned_zero_state_blockers_ = 0;

  // A timer to trigger a Publish at the end of the timeout period passed to
  // StartZeroState.
  base::OneShotTimer zero_state_timeout_;

  // Callbacks to run when initial set of zero state results is published.
  // Non empty list indicates that results should be published when zero state
  // times out.
  base::OnceClosureList on_zero_state_done_;

  // The time when StartSearch was most recently called.
  base::Time session_start_;

  // Storage for all search results for the current query.
  ResultsMap results_;

  // Storage for category scores for the current query.
  CategoriesList categories_;

  bool disable_ranking_for_test_ = false;

  std::vector<ControlCategory> toggleable_categories_;

  // If set, called when results set by a provider change. Only set by tests.
  ResultsChangedCallback results_changed_callback_for_test_;

  const raw_ptr<Profile> profile_;

  std::unique_ptr<BurnInController> burn_in_controller_;
  std::unique_ptr<RankerManager> ranker_manager_;

  std::unique_ptr<SearchMetricsManager> metrics_manager_;
  std::unique_ptr<SearchSessionMetricsManager> session_metrics_manager_;
  std::unique_ptr<federated::FederatedMetricsManager>
      federated_metrics_manager_;
  std::unique_ptr<AppDiscoveryMetricsManager> app_discovery_metrics_manager_;

  std::unique_ptr<AppSearchDataSource> app_search_data_source_;

  // TODO(b/315709613):Temporary before it is moved to a new service.
  std::unique_ptr<SearchEngine> search_engine_;

  std::unique_ptr<SearchFileScanner> search_file_scanner_;

  const raw_ptr<AppListModelUpdater> model_updater_;
  const raw_ptr<AppListControllerDelegate> list_controller_;
  const raw_ptr<ash::AppListNotifier> notifier_;
  const raw_ptr<ash::federated::FederatedServiceController>
      federated_service_controller_;

  base::ObserverList<Observer> observer_list_;
};

}  // namespace app_list

#endif  // CHROME_BROWSER_ASH_APP_LIST_SEARCH_SEARCH_CONTROLLER_H_