Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / default_search_manager.h
1 // Copyright 2014 The Chromium Authors
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 COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_
6 #define COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_
7
8 #include <memory>
9
10 #include "base/functional/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/values.h"
13 #include "components/prefs/pref_change_registrar.h"
14
15 namespace user_prefs {
16 class PrefRegistrySyncable;
17 }
18
19 class PrefService;
20 class PrefValueMap;
21 struct TemplateURLData;
22
23 // DefaultSearchManager handles the loading and writing of the user's default
24 // search engine selection to and from prefs.
25 class DefaultSearchManager {
26  public:
27   static const char kDefaultSearchProviderDataPrefName[];
28
29   static const char kID[];
30   static const char kShortName[];
31   static const char kKeyword[];
32   static const char kPrepopulateID[];
33   static const char kSyncGUID[];
34
35   static const char kURL[];
36   static const char kSuggestionsURL[];
37   static const char kImageURL[];
38   static const char kImageTranslateURL[];
39   static const char kNewTabURL[];
40   static const char kContextualSearchURL[];
41   static const char kFaviconURL[];
42   static const char kLogoURL[];
43   static const char kDoodleURL[];
44   static const char kOriginatingURL[];
45
46   static const char kSearchURLPostParams[];
47   static const char kSuggestionsURLPostParams[];
48   static const char kImageURLPostParams[];
49   static const char kSideSearchParam[];
50   static const char kSideImageSearchParam[];
51   static const char kImageSearchBrandingLabel[];
52   static const char kSearchIntentParams[];
53   static const char kImageTranslateSourceLanguageParamKey[];
54   static const char kImageTranslateTargetLanguageParamKey[];
55
56   static const char kSafeForAutoReplace[];
57   static const char kInputEncodings[];
58
59   static const char kDateCreated[];
60   static const char kLastModified[];
61   static const char kLastVisited[];
62
63   static const char kUsageCount[];
64   static const char kAlternateURLs[];
65   static const char kCreatedByPolicy[];
66   static const char kDisabledByPolicy[];
67   static const char kCreatedFromPlayAPI[];
68   static const char kPreconnectToSearchUrl[];
69   static const char kPrefetchLikelyNavigations[];
70   static const char kIsActive[];
71   static const char kStarterPackId[];
72   static const char kEnforcedByPolicy[];
73
74   enum Source {
75     // Default search engine chosen either from prepopulated engines set for
76     // current country or overriden from kSearchProviderOverrides preference.
77     FROM_FALLBACK = 0,
78     // User selected engine.
79     FROM_USER,
80     // Search engine set by extension overriding default search.
81     FROM_EXTENSION,
82     // Search engine controlled externally through enterprise configuration
83     // management (e.g. windows group policy).
84     FROM_POLICY,
85     // Search engine recommended externally through enterprise configuration
86     // management but allows for user modification.
87     FROM_POLICY_RECOMMENDED,
88   };
89
90   using ObserverCallback =
91       base::RepeatingCallback<void(const TemplateURLData*, Source)>;
92
93   DefaultSearchManager(PrefService* pref_service,
94                        const ObserverCallback& change_observer);
95
96   DefaultSearchManager(const DefaultSearchManager&) = delete;
97   DefaultSearchManager& operator=(const DefaultSearchManager&) = delete;
98
99   ~DefaultSearchManager();
100
101   // Register prefs needed for tracking the default search provider.
102   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
103
104   // Save default search provider pref values into the map provided.
105   static void AddPrefValueToMap(base::Value::Dict value,
106                                 PrefValueMap* pref_value_map);
107
108   // Testing code can call this with |disabled| set to true to cause
109   // GetDefaultSearchEngine() to return nullptr instead of
110   // |fallback_default_search_| in cases where the DSE source is FROM_FALLBACK.
111   static void SetFallbackSearchEnginesDisabledForTesting(bool disabled);
112
113   // Gets a pointer to the current Default Search Engine. If NULL, indicates
114   // that Default Search is explicitly disabled. |source|, if not NULL, will be
115   // filled in with the source of the result.
116   const TemplateURLData* GetDefaultSearchEngine(Source* source) const;
117
118   // Returns a pointer to the highest-ranking search provider while ignoring
119   // any extension-provided search engines.
120   std::unique_ptr<TemplateURLData> GetDefaultSearchEngineIgnoringExtensions()
121       const;
122
123   // Gets the source of the current Default Search Engine value.
124   Source GetDefaultSearchEngineSource() const;
125
126   // Returns a pointer to the fallback engine.
127   const TemplateURLData* GetFallbackSearchEngine() const;
128
129   // Write default search provider data to |pref_service_|.
130   void SetUserSelectedDefaultSearchEngine(const TemplateURLData& data);
131
132   // Clear the user's default search provider choice from |pref_service_|. Does
133   // not explicitly disable Default Search. The new default search
134   // engine will be defined by policy, extensions, or pre-populated data.
135   void ClearUserSelectedDefaultSearchEngine();
136
137  private:
138   // Handles changes to kDefaultSearchProviderData pref. This includes sync and
139   // policy changes. Calls LoadDefaultSearchEngineFromPrefs() and
140   // NotifyObserver() if the effective DSE might have changed.
141   void OnDefaultSearchPrefChanged();
142
143   // Handles changes to kSearchProviderOverrides pref. Calls
144   // LoadPrepopulatedDefaultSearch() and NotifyObserver() if the effective DSE
145   // might have changed.
146   void OnOverridesPrefChanged();
147
148   // Updates |prefs_default_search_| with values from its corresponding
149   // pre-populated search provider record, if any.
150   void MergePrefsDataWithPrepopulated();
151
152   // Reads default search provider data from |pref_service_|, updating
153   // |prefs_default_search_|, |default_search_mandatory_by_policy_|, and
154   // |default_search_recommended_by_policy_|.
155   // Invokes MergePrefsDataWithPrepopulated().
156   void LoadDefaultSearchEngineFromPrefs();
157
158   // Reads pre-populated search providers, which will be built-in or overridden
159   // by kSearchProviderOverrides. Updates |fallback_default_search_|. Invoke
160   // MergePrefsDataWithPrepopulated().
161   void LoadPrepopulatedDefaultSearch();
162
163   // Invokes |change_observer_| if it is not NULL.
164   void NotifyObserver();
165
166   raw_ptr<PrefService> pref_service_;
167   const ObserverCallback change_observer_;
168   PrefChangeRegistrar pref_change_registrar_;
169
170   // Default search engine provided by pre-populated data or by the
171   // |kSearchProviderOverrides| pref. This will be used when no other default
172   // search engine has been selected.
173   std::unique_ptr<TemplateURLData> fallback_default_search_;
174
175   // Default search engine provided by extension (usings Settings Override API).
176   // This will be null if there are no extensions installed which provide
177   // default search engines.
178   std::unique_ptr<TemplateURLData> extension_default_search_;
179
180   // Default search engine provided by prefs (either user prefs or policy
181   // prefs). This will be null if no value was set in the pref store.
182   std::unique_ptr<TemplateURLData> prefs_default_search_;
183
184   // True if the default search is currently enforced by policy.
185   bool default_search_mandatory_by_policy_ = false;
186
187   // True if the default search is currently recommended by policy.
188   bool default_search_recommended_by_policy_ = false;
189 };
190
191 #endif  // COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_