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. 4 5 #ifndef COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ 6 #define COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ 7 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" 12 13 namespace base { 14 class DictionaryValue; 15 } 16 17 namespace user_prefs { 18 class PrefRegistrySyncable; 19 } 20 21 class PrefService; 22 class PrefValueMap; 23 struct TemplateURLData; 24 25 // DefaultSearchManager handles the loading and writing of the user's default 26 // search engine selection to and from prefs. 27 class DefaultSearchManager { 28 public: 29 static const char kDefaultSearchProviderDataPrefName[]; 30 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[]; 36 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[]; 44 45 static const char kSearchURLPostParams[]; 46 static const char kSuggestionsURLPostParams[]; 47 static const char kInstantURLPostParams[]; 48 static const char kImageURLPostParams[]; 49 50 static const char kSafeForAutoReplace[]; 51 static const char kInputEncodings[]; 52 53 static const char kDateCreated[]; 54 static const char kLastModified[]; 55 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[]; 61 62 enum Source { 63 FROM_FALLBACK = 0, 64 FROM_USER, 65 FROM_EXTENSION, 66 FROM_POLICY, 67 }; 68 69 typedef base::Callback<void(const TemplateURLData*, Source)> ObserverCallback; 70 71 DefaultSearchManager(PrefService* pref_service, 72 const ObserverCallback& change_observer); 73 74 ~DefaultSearchManager(); 75 76 // Register prefs needed for tracking the default search provider. 77 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 78 79 // Save default search provider pref values into the map provided. 80 static void AddPrefValueToMap(base::DictionaryValue* value, 81 PrefValueMap* pref_value_map); 82 83 // Testing code can call this with |disabled| set to true to cause 84 // GetDefaultSearchEngine() to return NULL instead of 85 // |fallback_default_search_| in cases where the DSE source is FROM_FALLBACK. 86 static void SetFallbackSearchEnginesDisabledForTesting(bool disabled); 87 88 // Gets a pointer to the current Default Search Engine. If NULL, indicates 89 // that Default Search is explicitly disabled. |source|, if not NULL, will be 90 // filled in with the source of the result. 91 TemplateURLData* GetDefaultSearchEngine(Source* source) const; 92 93 // Gets the source of the current Default Search Engine value. 94 Source GetDefaultSearchEngineSource() const; 95 96 // Write default search provider data to |pref_service_|. 97 void SetUserSelectedDefaultSearchEngine(const TemplateURLData& data); 98 99 // Override the default search provider with an extension. 100 void SetExtensionControlledDefaultSearchEngine(const TemplateURLData& data); 101 102 // Clear the extension-provided default search engine. Does not explicitly 103 // disable Default Search. The new current default search engine will be 104 // defined by policy, extensions, or pre-populated data. 105 void ClearExtensionControlledDefaultSearchEngine(); 106 107 // Clear the user's default search provider choice from |pref_service_|. Does 108 // not explicitly disable Default Search. The new default search 109 // engine will be defined by policy, extensions, or pre-populated data. 110 void ClearUserSelectedDefaultSearchEngine(); 111 112 private: 113 // Handles changes to kDefaultSearchProviderData pref. This includes sync and 114 // policy changes. Calls LoadDefaultSearchEngineFromPrefs() and 115 // NotifyObserver() if the effective DSE might have changed. 116 void OnDefaultSearchPrefChanged(); 117 118 // Handles changes to kSearchProviderOverrides pref. Calls 119 // LoadPrepopulatedDefaultSearch() and NotifyObserver() if the effective DSE 120 // might have changed. 121 void OnOverridesPrefChanged(); 122 123 // Updates |prefs_default_search_| with values from its corresponding 124 // pre-populated search provider record, if any. 125 void MergePrefsDataWithPrepopulated(); 126 127 // Reads default search provider data from |pref_service_|, updating 128 // |prefs_default_search_| and |default_search_controlled_by_policy_|. 129 // Invokes MergePrefsDataWithPrepopulated(). 130 void LoadDefaultSearchEngineFromPrefs(); 131 132 // Reads pre-populated search providers, which will be built-in or overridden 133 // by kSearchProviderOverrides. Updates |fallback_default_search_|. Invoke 134 // MergePrefsDataWithPrepopulated(). 135 void LoadPrepopulatedDefaultSearch(); 136 137 // Invokes |change_observer_| if it is not NULL. 138 void NotifyObserver(); 139 140 PrefService* pref_service_; 141 const ObserverCallback change_observer_; 142 PrefChangeRegistrar pref_change_registrar_; 143 144 // Default search engine provided by pre-populated data or by the 145 // |kSearchProviderOverrides| pref. This will be used when no other default 146 // search engine has been selected. 147 scoped_ptr<TemplateURLData> fallback_default_search_; 148 149 // Default search engine provided by prefs (either user prefs or policy 150 // prefs). This will be null if no value was set in the pref store. 151 scoped_ptr<TemplateURLData> extension_default_search_; 152 153 // Default search engine provided by extension (usings Settings Override API). 154 // This will be null if there are no extensions installed which provide 155 // default search engines. 156 scoped_ptr<TemplateURLData> prefs_default_search_; 157 158 // True if the default search is currently enforced by policy. 159 bool default_search_controlled_by_policy_; 160 161 DISALLOW_COPY_AND_ASSIGN(DefaultSearchManager); 162 }; 163 164 #endif // COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_ 165