File: quick_insert_search_request.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,931 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_QUICK_INSERT_SEARCH_QUICK_INSERT_SEARCH_REQUEST_H_
#define ASH_QUICK_INSERT_SEARCH_QUICK_INSERT_SEARCH_REQUEST_H_

#include <array>
#include <cstddef>
#include <optional>
#include <string_view>
#include <vector>

#include "ash/ash_export.h"
#include "ash/public/cpp/app_list/app_list_types.h"
#include "ash/quick_insert/quick_insert_category.h"
#include "ash/quick_insert/quick_insert_search_result.h"
#include "ash/quick_insert/search/quick_insert_search_debouncer.h"
#include "ash/quick_insert/search/quick_insert_search_source.h"
#include "base/containers/span.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/types/cxx23_to_underlying.h"
#include "base/values.h"

namespace endpoint_fetcher {
class EndpointFetcher;
}

namespace ash {

class QuickInsertClient;
class QuickInsertClipboardHistoryProvider;

// Represents a single Quick Insert search query. Constructing this class starts
// a search, and destructing it stops the search.
class ASH_EXPORT QuickInsertSearchRequest {
 public:
  using SearchResultsCallback =
      base::RepeatingCallback<void(QuickInsertSearchSource source,
                                   std::vector<QuickInsertSearchResult> results,
                                   bool has_more_results)>;
  using DoneCallback = base::OnceCallback<void(bool interrupted)>;

  static constexpr base::TimeDelta kGifDebouncingDelay =
      base::Milliseconds(200);

  // `done_closure` is guaranteed to be called strictly after the last call to
  // `callback`.
  QuickInsertSearchRequest(
      std::u16string_view query,
      std::optional<QuickInsertCategory> category,
      SearchResultsCallback callback,
      DoneCallback done_callback,
      QuickInsertClient* client,
      base::span<const QuickInsertCategory> available_categories = {},
      bool caps_lock_state_to_search = false,
      bool search_case_transforms = false);
  QuickInsertSearchRequest(const QuickInsertSearchRequest&) = delete;
  QuickInsertSearchRequest& operator=(const QuickInsertSearchRequest&) = delete;
  ~QuickInsertSearchRequest();

 private:
  void StartGifSearch(std::string_view query);
  void HandleSearchSourceResults(QuickInsertSearchSource source,
                                 std::vector<QuickInsertSearchResult> results,
                                 bool has_more_results);

  void HandleActionSearchResults(std::vector<QuickInsertSearchResult> results);
  void HandleCrosSearchResults(AppListSearchResultType type,
                               std::vector<QuickInsertSearchResult> results);
  void HandleDateSearchResults(std::vector<QuickInsertSearchResult> results);
  void HandleMathSearchResults(std::optional<QuickInsertSearchResult> result);
  void HandleClipboardSearchResults(
      std::vector<QuickInsertSearchResult> results);
  void HandleEditorSearchResults(QuickInsertSearchSource source,
                                 std::optional<QuickInsertSearchResult> result);
  void HandleLobsterSearchResults(
      QuickInsertSearchSource source,
      std::optional<QuickInsertSearchResult> result);
  void HandleGifSearchResponse(std::vector<QuickInsertSearchResult> results);

  // Sets the search for the source to be started right now.
  // `CHECK` fails if a search was already started.
  void MarkSearchStarted(QuickInsertSearchSource source);
  // Sets the search for the source to be not started, and emits a metric for
  // the source.
  // `CHECK` fails if a search wasn't started.
  void MarkSearchEnded(QuickInsertSearchSource source);
  std::optional<base::TimeTicks> SwapSearchStart(
      QuickInsertSearchSource source,
      std::optional<base::TimeTicks> new_value);

  void MaybeCallDoneClosure();

  bool is_category_specific_search_;
  const raw_ref<QuickInsertClient> client_;

  std::unique_ptr<QuickInsertClipboardHistoryProvider> clipboard_provider_;
  QuickInsertSearchDebouncer gif_search_debouncer_;
  std::unique_ptr<endpoint_fetcher::EndpointFetcher> gif_fetcher_;

  SearchResultsCallback current_callback_;
  // Set to true once all the searches have started at the end of the ctor.
  bool can_call_done_closure_ = false;
  // Guaranteed to be non-null in the ctor.
  // Guaranteed to be null after it is called - it will never be reassigned.
  // Once called, `current_callback_` will also be reset to null.
  DoneCallback done_callback_;

  static constexpr size_t kNumSources =
      base::to_underlying(QuickInsertSearchSource::kMaxValue) + 1;
  std::array<std::optional<base::TimeTicks>, kNumSources> search_starts_;

  base::WeakPtrFactory<QuickInsertSearchRequest> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // ASH_QUICK_INSERT_SEARCH_QUICK_INSERT_SEARCH_REQUEST_H_