1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_
6 #define CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_change_registrar.h"
14 class DictionaryValue
;
17 namespace user_prefs
{
18 class PrefRegistrySyncable
;
23 struct TemplateURLData
;
25 // DefaultSearchManager handles the loading and writing of the user's default
26 // search engine selection to and from prefs.
27 class DefaultSearchManager
{
29 static const char kDefaultSearchProviderDataPrefName
[];
31 static const char kID
[];
32 static const char kShortName
[];
33 static const char kKeyword
[];
34 static const char kPrepopulateID
[];
35 static const char kSyncGUID
[];
37 static const char kURL
[];
38 static const char kSuggestionsURL
[];
39 static const char kInstantURL
[];
40 static const char kImageURL
[];
41 static const char kNewTabURL
[];
42 static const char kFaviconURL
[];
43 static const char kOriginatingURL
[];
45 static const char kSearchURLPostParams
[];
46 static const char kSuggestionsURLPostParams
[];
47 static const char kInstantURLPostParams
[];
48 static const char kImageURLPostParams
[];
50 static const char kSafeForAutoReplace
[];
51 static const char kInputEncodings
[];
53 static const char kDateCreated
[];
54 static const char kLastModified
[];
56 static const char kUsageCount
[];
57 static const char kAlternateURLs
[];
58 static const char kSearchTermsReplacementKey
[];
59 static const char kCreatedByPolicy
[];
60 static const char kDisabledByPolicy
[];
69 typedef base::Callback
<void(const TemplateURLData
*, Source
)> ObserverCallback
;
71 DefaultSearchManager(PrefService
* pref_service
,
72 const ObserverCallback
& change_observer
);
74 ~DefaultSearchManager();
76 // Register prefs needed for tracking the default search provider.
77 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
79 // Save default search provider pref values into the map provided.
80 static void AddPrefValueToMap(base::DictionaryValue
* value
,
81 PrefValueMap
* pref_value_map
);
83 // Gets a pointer to the current Default Search Engine. If NULL, indicates
84 // that Default Search is explicitly disabled. |source|, if not NULL, will be
85 // filled in with the source of the result.
86 TemplateURLData
* GetDefaultSearchEngine(Source
* source
) const;
88 // Gets the source of the current Default Search Engine value.
89 Source
GetDefaultSearchEngineSource() const;
91 // Write default search provider data to |pref_service_|.
92 void SetUserSelectedDefaultSearchEngine(const TemplateURLData
& data
);
94 // Override the default search provider with an extension.
95 void SetExtensionControlledDefaultSearchEngine(const TemplateURLData
& data
);
97 // Clear the extension-provided default search engine. Does not explicitly
98 // disable Default Search. The new current default search engine will be
99 // defined by policy, extensions, or pre-populated data.
100 void ClearExtensionControlledDefaultSearchEngine();
102 // Clear the user's default search provider choice from |pref_service_|. Does
103 // not explicitly disable Default Search. The new default search
104 // engine will be defined by policy, extensions, or pre-populated data.
105 void ClearUserSelectedDefaultSearchEngine();
108 // Handles changes to kDefaultSearchProviderData pref. This includes sync and
109 // policy changes. Calls LoadDefaultSearchEngineFromPrefs() and
110 // NotifyObserver() if the effective DSE might have changed.
111 void OnDefaultSearchPrefChanged();
113 // Handles changes to kSearchProviderOverrides pref. Calls
114 // LoadPrepopulatedDefaultSearch() and NotifyObserver() if the effective DSE
115 // might have changed.
116 void OnOverridesPrefChanged();
118 // Updates |prefs_default_search_| with values from its corresponding
119 // pre-populated search provider record, if any.
120 void MergePrefsDataWithPrepopulated();
122 // Reads default search provider data from |pref_service_|, updating
123 // |prefs_default_search_| and |default_search_controlled_by_policy_|.
124 // Invokes MergePrefsDataWithPrepopulated().
125 void LoadDefaultSearchEngineFromPrefs();
127 // Reads pre-populated search providers, which will be built-in or overridden
128 // by kSearchProviderOverrides. Updates |fallback_default_search_|. Invoke
129 // MergePrefsDataWithPrepopulated().
130 void LoadPrepopulatedDefaultSearch();
132 // Invokes |change_observer_| if it is not NULL.
133 void NotifyObserver();
135 PrefService
* pref_service_
;
136 const ObserverCallback change_observer_
;
137 PrefChangeRegistrar pref_change_registrar_
;
139 // Default search engine provided by pre-populated data or by the
140 // |kSearchProviderOverrides| pref. This will be used when no other default
141 // search engine has been selected.
142 scoped_ptr
<TemplateURLData
> fallback_default_search_
;
144 // Default search engine provided by prefs (either user prefs or policy
145 // prefs). This will be null if no value was set in the pref store.
146 scoped_ptr
<TemplateURLData
> extension_default_search_
;
148 // Default search engine provided by extension (usings Settings Override API).
149 // This will be null if there are no extensions installed which provide
150 // default search engines.
151 scoped_ptr
<TemplateURLData
> prefs_default_search_
;
153 // True if the default search is currently enforced by policy.
154 bool default_search_controlled_by_policy_
;
156 DISALLOW_COPY_AND_ASSIGN(DefaultSearchManager
);
159 #endif // CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_