File: zero_suggest_provider.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 (176 lines) | stat: -rw-r--r-- 7,782 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
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file contains the zero-suggest autocomplete provider. This experimental
// provider is invoked when the user focuses in the omnibox prior to editing,
// and generates search query suggestions based on the current URL.

#ifndef COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_
#define COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_

#include <memory>
#include <string>

#include "base/gtest_prod_util.h"
#include "components/omnibox/browser/autocomplete_enums.h"
#include "components/omnibox/browser/autocomplete_provider_debouncer.h"
#include "components/omnibox/browser/base_search_provider.h"

class AutocompleteProviderListener;
class PrefRegistrySimple;

namespace network {
class SimpleURLLoader;
}

// Autocomplete provider for searches based on the current URL.
//
// The controller will call Start() when the user focuses the omnibox. After
// construction, the autocomplete controller repeatedly calls Start() with some
// user input, each time expecting to receive an updated set of matches.
//
// TODO(jered): Consider deleting this class and building this functionality
// into SearchProvider after dogfood and after we break the association between
// omnibox text and suggestions.
class ZeroSuggestProvider : public BaseSearchProvider {
 public:
  // The result type that can be processed by ZeroSuggestProvider.
  // Public for testing purposes and for use in LocalHistoryZeroSuggestProvider.
  enum class ResultType {
    kNone = 0,

    // The remote endpoint is queried for zero-prefix suggestions. The endpoint
    // is sent the user's authentication state, but not the current page URL.
    kRemoteNoURL = 1,

    // The emote endpoint is queried for zero-prefix suggestions. The endpoint
    // is sent both the user's authentication state and the current page URL.
    kRemoteSendURL = 2,
  };

  // Creates and returns an instance of this provider.
  static ZeroSuggestProvider* Create(AutocompleteProviderClient* client,
                                     AutocompleteProviderListener* listener);

  // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
  static AutocompleteMatch NavigationToMatch(
      AutocompleteProvider* provider,
      AutocompleteProviderClient* client,
      const SearchSuggestionParser::NavigationResult& navigation);

  // Registers a preference used to cache the zero suggest response.
  static void RegisterProfilePrefs(PrefRegistrySimple* registry);

  // Returns the type of results that should be generated for the given context
  // and their eligibility.
  // This method is static to avoid depending on the provider state.
  static std::pair<ResultType, bool> GetResultTypeAndEligibility(
      const AutocompleteProviderClient* client,
      const AutocompleteInput& input);

  // AutocompleteProvider:
  void StartPrefetch(const AutocompleteInput& input) override;
  void Start(const AutocompleteInput& input, bool minimal_changes) override;
  void Stop(AutocompleteStopReason stop_reason) override;
  void DeleteMatch(const AutocompleteMatch& match) override;
  void AddProviderInfo(ProvidersInfo* provider_info) const override;

  // Returns the list of experiment stats corresponding to |matches_|. Will be
  // logged to SearchboxStats as part of a GWS experiment, if any.
  const SearchSuggestionParser::ExperimentStatsV2s& experiment_stats_v2s()
      const {
    return experiment_stats_v2s_;
  }

  // Returns the list of GWS event ID hashes corresponding to `matches_`. Will
  // be logged to SearchboxStats as needed.
  const SearchSuggestionParser::GwsEventIdHashes& gws_event_id_hashes() const {
    return gws_event_id_hashes_;
  }

  ResultType GetResultTypeRunningForTesting() const {
    return result_type_running_;
  }

  ZeroSuggestProvider(AutocompleteProviderClient* client,
                      AutocompleteProviderListener* listener);
  ZeroSuggestProvider(const ZeroSuggestProvider&) = delete;
  ZeroSuggestProvider& operator=(const ZeroSuggestProvider&) = delete;

 protected:
  ~ZeroSuggestProvider() override;

 private:
  FRIEND_TEST_ALL_PREFIXES(ZeroSuggestProviderTest,
                           TestCacheStateWithSRPPrefetchDisabled);
  FRIEND_TEST_ALL_PREFIXES(ZeroSuggestProviderTest,
                           TestCacheStateWithWebPrefetchDisabled);
  // BaseSearchProvider:
  bool ShouldAppendExtraParams(
      const SearchSuggestionParser::SuggestResult& result) const override;
  void RecordDeletionResult(bool success) override;

  // Called when the non-prefetch network request has completed.
  // `input` and `result_type` are bound to this callback. The former is the
  // input for which the request was made and the latter indicates the result
  // type being received in this callback.
  void OnURLLoadComplete(const AutocompleteInput& input,
                         const ResultType result_type,
                         const network::SimpleURLLoader* source,
                         const int response_code,
                         std::unique_ptr<std::string> response_body);
  // Called when the prefetch network request has completed.
  // `input` and `result_type` are bound to this callback. The former is the
  // input for which the request was made and the latter indicates the result
  // type being received in this callback.
  void OnPrefetchURLLoadComplete(const AutocompleteInput& input,
                                 const ResultType result_type,
                                 const network::SimpleURLLoader* source,
                                 const int response_code,
                                 std::unique_ptr<std::string> response_body);

  // Called by `debouncer_`.
  void RunZeroSuggestPrefetch(const AutocompleteInput& input,
                              const ResultType result_type);

  // Called either in Start() with |results| populated from the cached response,
  // where |matches_| are empty; or in OnURLLoadComplete() with |results|
  // populated from the remote response, where |matches_| may not be empty.
  //
  // Uses |results| and |input| to populate |matches_| and its associated
  // metadata. Also logs how many results were received. Note that an empty
  // result set will clear |matches_|.
  void ConvertSuggestResultsToAutocompleteMatches(
      const SearchSuggestionParser::Results& results,
      const AutocompleteInput& input);

  // The result type that is currently being retrieved and processed for
  // non-prefetch requests.
  // Set in Start() and used in Stop() for logging purposes.
  ResultType result_type_running_{ResultType::kNone};

  // Loader used to retrieve results for non-prefetch requests.
  std::unique_ptr<network::SimpleURLLoader> loader_;

  // Loader used to retrieve results for ZPS prefetch requests on NTP.
  std::unique_ptr<network::SimpleURLLoader> ntp_prefetch_loader_;

  // Loader used to retrieve results for ZPS prefetch requests on SRP/Web.
  std::unique_ptr<network::SimpleURLLoader> srp_web_prefetch_loader_;

  // Debouncer used to throttle the frequency of ZPS prefetch requests (to
  // minimize the performance impact on the remote Suggest service).
  std::unique_ptr<AutocompleteProviderDebouncer> debouncer_;

  // The list of experiment stats corresponding to |matches_|.
  SearchSuggestionParser::ExperimentStatsV2s experiment_stats_v2s_;

  // The list of GWS event ID hashes corresponding to `matches_`.
  SearchSuggestionParser::GwsEventIdHashes gws_event_id_hashes_;

  // For callbacks that may be run after destruction.
  base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_{this};
};

#endif  // COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_