File: default_search_manager.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 (228 lines) | stat: -rw-r--r-- 9,086 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_
#define COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_

#include <memory>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/values.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/search_engines/reconciling_template_url_data_holder.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_service.h"
#include "components/search_engines/template_url_prepopulate_data_resolver.h"

namespace search_engines {
class SearchEngineChoiceService;
}

namespace user_prefs {
class PrefRegistrySyncable;
}

class PrefService;
class PrefValueMap;
struct TemplateURLData;

// DefaultSearchManager handles the loading and writing of the user's default
// search engine selection to and from prefs.
class DefaultSearchManager
    : public search_engines::SearchEngineChoiceService::Observer {
 public:
  // A dictionary to hold all data related to the Default Search Engine.
  // Eventually, this should replace all the data stored in the
  // default_search_provider.* prefs.
  static constexpr char kDefaultSearchProviderDataPrefName[] =
      "default_search_provider_data.template_url_data";

  // A mirrored copy of the Default Search Engine data.
  static constexpr char kMirroredDefaultSearchProviderDataPrefName[] =
      "default_search_provider_data.mirrored_template_url_data";

  static const char kID[];
  static const char kShortName[];
  static const char kKeyword[];
  static const char kPrepopulateID[];
  static const char kSyncGUID[];

  static const char kURL[];
  static const char kSuggestionsURL[];
  static const char kImageURL[];
  static const char kImageTranslateURL[];
  static const char kNewTabURL[];
  static const char kContextualSearchURL[];
  static const char kFaviconURL[];
  static const char kLogoURL[];
  static const char kDoodleURL[];
  static const char kOriginatingURL[];

  static const char kSearchURLPostParams[];
  static const char kSuggestionsURLPostParams[];
  static const char kImageURLPostParams[];
  static const char kImageSearchBrandingLabel[];
  static const char kSearchIntentParams[];
  static const char kImageTranslateSourceLanguageParamKey[];
  static const char kImageTranslateTargetLanguageParamKey[];

  static const char kSafeForAutoReplace[];
  static const char kInputEncodings[];

  static const char kDateCreated[];
  static const char kLastModified[];
  static const char kLastVisited[];

  static const char kUsageCount[];
  static const char kAlternateURLs[];
  static const char kPolicyOrigin[];
  static const char kDisabledByPolicy[];
  static const char kCreatedFromPlayAPI[];
  static const char kFeaturedByPolicy[];
  static const char kRequireShortcut[];
  static const char kPreconnectToSearchUrl[];
  static const char kPrefetchLikelyNavigations[];
  static const char kIsActive[];
  static const char kStarterPackId[];
  static const char kEnforcedByPolicy[];

  static const char kDefaultSearchEngineMirroredMetric[];

  enum Source {
    // Default search engine chosen either from prepopulated engines set for
    // current country or overridden from kSearchProviderOverrides preference.
    FROM_FALLBACK = 0,
    // User selected engine.
    FROM_USER,
    // Search engine set by extension overriding default search.
    FROM_EXTENSION,
    // Search engine controlled externally through enterprise configuration
    // management (e.g. windows group policy).
    FROM_POLICY,
    // Search engine recommended externally through enterprise configuration
    // management but allows for user modification.
    FROM_POLICY_RECOMMENDED,
  };

  using ObserverCallback =
      base::RepeatingCallback<void(const TemplateURLData*, Source)>;

  DefaultSearchManager(
      PrefService* pref_service,
      search_engines::SearchEngineChoiceService* search_engine_choice_service,
      TemplateURLPrepopulateData::Resolver& prepopulate_data_resolver,
      const ObserverCallback& change_observer);

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

  ~DefaultSearchManager() override;

  // Register prefs needed for tracking the default search provider.
  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  // Save default search provider pref values into the map provided.
  static void AddPrefValueToMap(base::Value::Dict value,
                                PrefValueMap* pref_value_map);

  // Testing code can call this with |disabled| set to true to cause
  // GetDefaultSearchEngine() to return nullptr instead of
  // |fallback_default_search_| in cases where the DSE source is FROM_FALLBACK.
  static void SetFallbackSearchEnginesDisabledForTesting(bool disabled);

  // Gets a pointer to the current Default Search Engine. If NULL, indicates
  // that Default Search is explicitly disabled. |source|, if not NULL, will be
  // filled in with the source of the result.
  const TemplateURLData* GetDefaultSearchEngine(Source* source) const;

  // Returns a pointer to the highest-ranking search provider while ignoring
  // any extension-provided search engines.
  std::unique_ptr<TemplateURLData> GetDefaultSearchEngineIgnoringExtensions()
      const;

  // Gets the source of the current Default Search Engine value.
  Source GetDefaultSearchEngineSource() const;

  // Returns a pointer to the fallback engine.
  const TemplateURLData* GetFallbackSearchEngine() const;

  // Write default search provider data to |pref_service_|.
  void SetUserSelectedDefaultSearchEngine(const TemplateURLData& data);

  // Clear the user's default search provider choice from |pref_service_|. Does
  // not explicitly disable Default Search. The new default search
  // engine will be defined by policy, extensions, or pre-populated data.
  void ClearUserSelectedDefaultSearchEngine();

 private:
  // Handles changes to kDefaultSearchProviderData pref. This includes sync and
  // policy changes. Calls LoadDefaultSearchEngineFromPrefs() and
  // NotifyObserver() if the effective DSE might have changed.
  void OnDefaultSearchPrefChanged();

  // Handles changes to kSearchProviderOverrides pref. Calls
  // LoadPrepopulatedFallbackSearch() and NotifyObserver() if the effective DSE
  // might have changed.
  void OnOverridesPrefChanged();

  // Reads default search provider data from |pref_service_|, updating
  // |prefs_default_search_|, |default_search_mandatory_by_policy_|, and
  // |default_search_recommended_by_policy_|.
  // Invokes MergePrefsDataWithPrepopulated().
  void LoadDefaultSearchEngineFromPrefs();

  // Reads guest search provider, which was previously saved for future guest
  // session. Updates |saved_guest_search_|.
  void LoadSavedGuestSearch();

  // Reads pre-populated search providers, which will be built-in or overridden
  // by kSearchProviderOverrides. Updates |fallback_default_search_|. Invoke
  // MergePrefsDataWithPrepopulated().
  void LoadPrepopulatedFallbackSearch();

  // Invokes |change_observer_| if it is not NULL.
  void NotifyObserver();

  // search_engines::SearchEngineChoiceService::Observer
  void OnSavedGuestSearchChanged() override;

  const raw_ptr<PrefService> pref_service_;
  const raw_ptr<search_engines::SearchEngineChoiceService>
      search_engine_choice_service_ = nullptr;

  const ObserverCallback change_observer_;
  PrefChangeRegistrar pref_change_registrar_;
  base::ScopedObservation<search_engines::SearchEngineChoiceService,
                          search_engines::SearchEngineChoiceService::Observer>
      search_engine_choice_service_observation_{this};

  raw_ref<TemplateURLPrepopulateData::Resolver> prepopulate_data_resolver_;

  // Default search engine provided by pre-populated data or by the
  // |kSearchProviderOverrides| pref. This will be used when no other default
  // search engine has been selected.
  std::unique_ptr<TemplateURLData> fallback_default_search_;

  // Default search engine provided by extension (usings Settings Override API).
  // This will be null if there are no extensions installed which provide
  // default search engines.
  std::unique_ptr<TemplateURLData> extension_default_search_;

  // Default search engine provided by prefs (either user prefs or policy
  // prefs). This will be null if no value was set in the pref store.
  ReconcilingTemplateURLDataHolder prefs_default_search_;

  // Default search engine provided by previous SearchEngineChoice in guest
  // mode.
  std::unique_ptr<TemplateURLData> saved_guest_search_;

  // True if the default search is currently enforced by policy.
  bool default_search_mandatory_by_policy_ = false;

  // True if the default search is currently recommended by policy.
  bool default_search_recommended_by_policy_ = false;
};

#endif  // COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_