OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ |
6 #define CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ | 6 #define CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ |
7 | 7 |
| 8 #include "base/callback.h" |
8 #include "base/macros.h" | 9 #include "base/macros.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/prefs/pref_change_registrar.h" | 11 #include "base/prefs/pref_change_registrar.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 class DictionaryValue; | 14 class DictionaryValue; |
14 } | 15 } |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 class DictionaryValue; | 18 class DictionaryValue; |
18 } | 19 } |
19 | 20 |
20 namespace user_prefs { | 21 namespace user_prefs { |
21 class PrefRegistrySyncable; | 22 class PrefRegistrySyncable; |
22 } | 23 } |
23 | 24 |
24 class PrefService; | 25 class PrefService; |
25 class PrefValueMap; | 26 class PrefValueMap; |
26 struct TemplateURLData; | 27 struct TemplateURLData; |
27 | 28 |
28 // DefaultSearchManager handles the loading and writing of the user's default | 29 // DefaultSearchManager handles the loading and writing of the user's default |
29 // search engine selection to and from prefs. | 30 // search engine selection to and from prefs. |
30 | |
31 class DefaultSearchManager { | 31 class DefaultSearchManager { |
32 public: | 32 public: |
| 33 static const char kDefaultSearchProviderDataPrefName[]; |
| 34 |
33 static const char kID[]; | 35 static const char kID[]; |
34 static const char kShortName[]; | 36 static const char kShortName[]; |
35 static const char kKeyword[]; | 37 static const char kKeyword[]; |
36 static const char kPrepopulateID[]; | 38 static const char kPrepopulateID[]; |
37 static const char kSyncGUID[]; | 39 static const char kSyncGUID[]; |
38 | 40 |
39 static const char kURL[]; | 41 static const char kURL[]; |
40 static const char kSuggestionsURL[]; | 42 static const char kSuggestionsURL[]; |
41 static const char kInstantURL[]; | 43 static const char kInstantURL[]; |
42 static const char kImageURL[]; | 44 static const char kImageURL[]; |
(...skipping 18 matching lines...) Expand all Loading... |
61 static const char kCreatedByPolicy[]; | 63 static const char kCreatedByPolicy[]; |
62 static const char kDisabledByPolicy[]; | 64 static const char kDisabledByPolicy[]; |
63 | 65 |
64 enum Source { | 66 enum Source { |
65 FROM_FALLBACK = 0, | 67 FROM_FALLBACK = 0, |
66 FROM_USER, | 68 FROM_USER, |
67 FROM_EXTENSION, | 69 FROM_EXTENSION, |
68 FROM_POLICY, | 70 FROM_POLICY, |
69 }; | 71 }; |
70 | 72 |
71 explicit DefaultSearchManager(PrefService* pref_service); | 73 typedef base::Callback<void(const TemplateURLData*, Source)> ObserverCallback; |
| 74 |
| 75 DefaultSearchManager(PrefService* pref_service, |
| 76 const ObserverCallback& change_observer); |
| 77 |
72 ~DefaultSearchManager(); | 78 ~DefaultSearchManager(); |
73 | 79 |
74 // Register prefs needed for tracking the default search provider. | 80 // Register prefs needed for tracking the default search provider. |
75 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 81 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
76 | 82 |
77 // Save default search provider pref values into the map provided. | 83 // Save default search provider pref values into the map provided. |
78 static void AddPrefValueToMap(base::DictionaryValue* value, | 84 static void AddPrefValueToMap(base::DictionaryValue* value, |
79 PrefValueMap* pref_value_map); | 85 PrefValueMap* pref_value_map); |
80 | 86 |
81 // Gets a pointer to the current DefaultSearchEngine. If NULL, indicates that | 87 // Gets a pointer to the current Default Search Engine. If NULL, indicates |
82 // Default Search is explicitly disabled. | 88 // that Default Search is explicitly disabled. |source|, if not NULL, will be |
83 TemplateURLData* GetDefaultSearchEngine() const; | 89 // filled in with the source of the result. |
| 90 TemplateURLData* GetDefaultSearchEngine(Source* source) const; |
| 91 |
| 92 // Gets the source of the current Default Search Engine value. |
| 93 Source GetDefaultSearchEngineSource() const; |
84 | 94 |
85 // Write default search provider data to |pref_service_|. | 95 // Write default search provider data to |pref_service_|. |
86 void SetUserSelectedDefaultSearchEngine(const TemplateURLData& data); | 96 void SetUserSelectedDefaultSearchEngine(const TemplateURLData& data); |
87 | 97 |
88 // Override the default search provider with an extension. Takes ownership of | 98 // Override the default search provider with an extension. |
89 // |data|. | 99 void SetExtensionControlledDefaultSearchEngine(const TemplateURLData& data); |
90 void SetExtensionControlledDefaultSearchEngine(TemplateURLData* data); | |
91 | 100 |
92 // Clear the extension-provided default search engine. Does not explicitly | 101 // Clear the extension-provided default search engine. Does not explicitly |
93 // disable Default Search.The new current default search engine will be | 102 // disable Default Search.The new current default search engine will be |
94 // defined by policy, extensions, or pre-populated data. | 103 // defined by policy, extensions, or pre-populated data. |
95 void ClearExtensionControlledDefaultSearchEngine(); | 104 void ClearExtensionControlledDefaultSearchEngine(); |
96 | 105 |
97 // Clear the user's default search provider choice from |pref_service_|. Does | 106 // Clear the user's default search provider choice from |pref_service_|. Does |
98 // not explicitly disable Default Search. The new current default search | 107 // not explicitly disable Default Search. The new current default search |
99 // engine will be defined by policy, extensions, or pre-populated data. | 108 // engine will be defined by policy, extensions, or pre-populated data. |
100 void ClearUserSelectedDefaultSearchEngine(); | 109 void ClearUserSelectedDefaultSearchEngine(); |
101 | 110 |
102 // Determines the source of the current default search provider value. | |
103 Source GetDefaultSearchEngineSource() const; | |
104 | |
105 private: | 111 private: |
106 // Handle changes to kDefaultSearchProviderData pref. This includes sync and | 112 // Handles changes to kDefaultSearchProviderData pref. This includes sync and |
107 // policy changes. | 113 // policy changes. Calls LoadDefaultSearchEngineFromPrefs() and |
| 114 // NotifyObserver() if the effective DSE might have changed. |
108 void OnDefaultSearchPrefChanged(); | 115 void OnDefaultSearchPrefChanged(); |
109 | 116 |
110 // Handle changes to kSearchProviderOverrides pref, and update necessary | 117 // Handles changes to kSearchProviderOverrides pref. Calls |
111 // members. | 118 // LoadPrepopulatedDefaultSearch() and NotifyObserver() if the effective DSE |
| 119 // might have changed. |
112 void OnOverridesPrefChanged(); | 120 void OnOverridesPrefChanged(); |
113 | 121 |
114 // Update |default_search_controlled_by_policy_|. | 122 // Updates |prefs_default_search_| with values from its corresponding |
115 void UpdatePolicyStatus(); | 123 // pre-populated search provider record, if any. |
116 | |
117 // Merge data from prefs with prepopulated data. | |
118 void MergePrefsDataWithPrepopulated(); | 124 void MergePrefsDataWithPrepopulated(); |
119 | 125 |
120 // Read default search provider data from |pref_service_|. | 126 // Reads default search provider data from |pref_service_|, updating |
121 bool ReadDefaultSearchEngineFromPrefs(TemplateURLData** data); | 127 // |prefs_default_search_| and |default_search_controlled_by_policy_|. |
| 128 // Invokes MergePrefsDataWithPrepopulated(). |
| 129 void LoadDefaultSearchEngineFromPrefs(); |
| 130 |
| 131 // Reads pre-populated search providers, which may be built-in or overridden |
| 132 // by kSearchProviderOverrides. Updates |fallback_default_search_|. Invoke |
| 133 // MergePrefsDataWithPrepopulated(). |
| 134 void LoadPrepopulatedDefaultSearch(); |
| 135 |
| 136 // Invokes |change_observer_| if it is not NULL. |
| 137 void NotifyObserver(); |
122 | 138 |
123 PrefService* pref_service_; | 139 PrefService* pref_service_; |
| 140 const ObserverCallback change_observer_; |
124 PrefChangeRegistrar pref_change_registrar_; | 141 PrefChangeRegistrar pref_change_registrar_; |
125 | 142 |
126 scoped_ptr<TemplateURLData> fallback_default_search_; | 143 scoped_ptr<TemplateURLData> fallback_default_search_; |
127 scoped_ptr<TemplateURLData> extension_default_search_; | 144 scoped_ptr<TemplateURLData> extension_default_search_; |
128 scoped_ptr<TemplateURLData> prefs_default_search_; | 145 scoped_ptr<TemplateURLData> prefs_default_search_; |
129 | 146 |
130 bool default_search_controlled_by_policy_; | 147 bool default_search_controlled_by_policy_; |
131 | 148 |
132 DISALLOW_COPY_AND_ASSIGN(DefaultSearchManager); | 149 DISALLOW_COPY_AND_ASSIGN(DefaultSearchManager); |
133 }; | 150 }; |
134 | 151 |
135 #endif // CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ | 152 #endif // CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ |
OLD | NEW |