| 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 #include "chrome/browser/search_engines/default_search_manager.h" | 5 #include "chrome/browser/search_engines/default_search_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 11 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/logging.h" |
| 12 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 13 #include "base/prefs/pref_value_map.h" | 16 #include "base/prefs/pref_value_map.h" |
| 14 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 17 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/search_engines/template_url.h" | 24 #include "chrome/browser/search_engines/template_url.h" |
| 25 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 22 #include "chrome/browser/search_engines/template_url_service.h" | 26 #include "chrome/browser/search_engines/template_url_service.h" |
| 23 #include "chrome/browser/search_engines/util.h" | 27 #include "chrome/browser/search_engines/util.h" |
| 24 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
| 25 #include "components/user_prefs/pref_registry_syncable.h" | 29 #include "components/user_prefs/pref_registry_syncable.h" |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 33 bool IsDefaultSearchProviderManaged(PrefService* pref_service) { |
| 34 const PrefService::Preference* pref = pref_service->FindPreference( |
| 35 DefaultSearchManager::kDefaultSearchProviderDataPrefName); |
| 36 DCHECK(pref); |
| 37 return pref->IsManaged(); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 29 // A dictionary to hold all data related to the Default Search Engine. | 42 // A dictionary to hold all data related to the Default Search Engine. |
| 30 // Eventually, this should replace all the data stored in the | 43 // Eventually, this should replace all the data stored in the |
| 31 // default_search_provider.* prefs. | 44 // default_search_provider.* prefs. |
| 32 const char kDefaultSearchProviderData[] = | 45 const char DefaultSearchManager::kDefaultSearchProviderDataPrefName[] = |
| 33 "default_search_provider_data.template_url_data"; | 46 "default_search_provider_data.template_url_data"; |
| 34 | 47 |
| 35 } // namespace | |
| 36 | |
| 37 const char DefaultSearchManager::kID[] = "id"; | 48 const char DefaultSearchManager::kID[] = "id"; |
| 38 const char DefaultSearchManager::kShortName[] = "short_name"; | 49 const char DefaultSearchManager::kShortName[] = "short_name"; |
| 39 const char DefaultSearchManager::kKeyword[] = "keyword"; | 50 const char DefaultSearchManager::kKeyword[] = "keyword"; |
| 40 const char DefaultSearchManager::kPrepopulateID[] = "prepopulate_id"; | 51 const char DefaultSearchManager::kPrepopulateID[] = "prepopulate_id"; |
| 41 const char DefaultSearchManager::kSyncGUID[] = "synced_guid"; | 52 const char DefaultSearchManager::kSyncGUID[] = "synced_guid"; |
| 42 | 53 |
| 43 const char DefaultSearchManager::kURL[] = "url"; | 54 const char DefaultSearchManager::kURL[] = "url"; |
| 44 const char DefaultSearchManager::kSuggestionsURL[] = "suggestions_url"; | 55 const char DefaultSearchManager::kSuggestionsURL[] = "suggestions_url"; |
| 45 const char DefaultSearchManager::kInstantURL[] = "instant_url"; | 56 const char DefaultSearchManager::kInstantURL[] = "instant_url"; |
| 46 const char DefaultSearchManager::kImageURL[] = "image_url"; | 57 const char DefaultSearchManager::kImageURL[] = "image_url"; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 63 const char DefaultSearchManager::kDateCreated[] = "date_created"; | 74 const char DefaultSearchManager::kDateCreated[] = "date_created"; |
| 64 const char DefaultSearchManager::kLastModified[] = "last_modified"; | 75 const char DefaultSearchManager::kLastModified[] = "last_modified"; |
| 65 | 76 |
| 66 const char DefaultSearchManager::kUsageCount[] = "usage_count"; | 77 const char DefaultSearchManager::kUsageCount[] = "usage_count"; |
| 67 const char DefaultSearchManager::kAlternateURLs[] = "alternate_urls"; | 78 const char DefaultSearchManager::kAlternateURLs[] = "alternate_urls"; |
| 68 const char DefaultSearchManager::kSearchTermsReplacementKey[] = | 79 const char DefaultSearchManager::kSearchTermsReplacementKey[] = |
| 69 "search_terms_replacement_key"; | 80 "search_terms_replacement_key"; |
| 70 const char DefaultSearchManager::kCreatedByPolicy[] = "created_by_policy"; | 81 const char DefaultSearchManager::kCreatedByPolicy[] = "created_by_policy"; |
| 71 const char DefaultSearchManager::kDisabledByPolicy[] = "disabled_by_policy"; | 82 const char DefaultSearchManager::kDisabledByPolicy[] = "disabled_by_policy"; |
| 72 | 83 |
| 73 DefaultSearchManager::DefaultSearchManager(PrefService* pref_service) | 84 DefaultSearchManager::DefaultSearchManager( |
| 74 : pref_service_(pref_service) { | 85 PrefService* pref_service, |
| 75 DCHECK(pref_service_); | 86 const ObserverCallback& change_observer) |
| 87 : pref_service_(pref_service), |
| 88 change_observer_(change_observer), |
| 89 default_search_controlled_by_policy_(false) { |
| 90 if (pref_service_) { |
| 91 pref_change_registrar_.Init(pref_service_); |
| 92 pref_change_registrar_.Add( |
| 93 kDefaultSearchProviderDataPrefName, |
| 94 base::Bind(&DefaultSearchManager::OnDefaultSearchPrefChanged, |
| 95 base::Unretained(this))); |
| 96 pref_change_registrar_.Add( |
| 97 prefs::kSearchProviderOverrides, |
| 98 base::Bind(&DefaultSearchManager::OnOverridesPrefChanged, |
| 99 base::Unretained(this))); |
| 100 } |
| 101 LoadPrepopulatedDefaultSearch(); |
| 102 LoadDefaultSearchEngineFromPrefs(); |
| 76 } | 103 } |
| 77 | 104 |
| 78 DefaultSearchManager::~DefaultSearchManager() { | 105 DefaultSearchManager::~DefaultSearchManager() { |
| 79 } | 106 } |
| 80 | 107 |
| 81 // static | 108 // static |
| 82 void DefaultSearchManager::RegisterProfilePrefs( | 109 void DefaultSearchManager::RegisterProfilePrefs( |
| 83 user_prefs::PrefRegistrySyncable* registry) { | 110 user_prefs::PrefRegistrySyncable* registry) { |
| 84 registry->RegisterDictionaryPref( | 111 registry->RegisterDictionaryPref( |
| 85 kDefaultSearchProviderData, | 112 kDefaultSearchProviderDataPrefName, |
| 86 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 113 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 87 } | 114 } |
| 88 | 115 |
| 89 // static | 116 // static |
| 90 void DefaultSearchManager::AddPrefValueToMap(base::DictionaryValue* value, | 117 void DefaultSearchManager::AddPrefValueToMap(base::DictionaryValue* value, |
| 91 PrefValueMap* pref_value_map) { | 118 PrefValueMap* pref_value_map) { |
| 92 pref_value_map->SetValue(kDefaultSearchProviderData, value); | 119 pref_value_map->SetValue(kDefaultSearchProviderDataPrefName, value); |
| 93 } | 120 } |
| 94 | 121 |
| 95 bool DefaultSearchManager::GetDefaultSearchEngine(TemplateURLData* data) { | 122 TemplateURLData* DefaultSearchManager::GetDefaultSearchEngine( |
| 96 const base::DictionaryValue* url_dict = | 123 Source* source) const { |
| 97 pref_service_->GetDictionary(kDefaultSearchProviderData); | 124 if (default_search_controlled_by_policy_) { |
| 98 | 125 if (source) |
| 99 if (url_dict->empty()) | 126 *source = FROM_POLICY; |
| 100 return false; | 127 return prefs_default_search_.get(); |
| 101 | 128 } |
| 102 std::string search_url; | 129 if (extension_default_search_) { |
| 103 base::string16 keyword; | 130 if (source) |
| 104 url_dict->GetString(kURL, &search_url); | 131 *source = FROM_EXTENSION; |
| 105 url_dict->GetString(kKeyword, &keyword); | 132 return extension_default_search_.get(); |
| 106 if (search_url.empty()) | 133 } |
| 107 return false; | 134 if (prefs_default_search_) { |
| 108 if (keyword.empty()) | 135 if (source) |
| 109 keyword = TemplateURLService::GenerateKeyword(GURL(search_url)); | 136 *source = FROM_USER; |
| 110 data->SetKeyword(keyword); | 137 return prefs_default_search_.get(); |
| 111 data->SetURL(search_url); | |
| 112 | |
| 113 std::string id; | |
| 114 url_dict->GetString(kID, &id); | |
| 115 base::StringToInt64(id, &data->id); | |
| 116 url_dict->GetString(kShortName, &data->short_name); | |
| 117 url_dict->GetInteger(kPrepopulateID, &data->prepopulate_id); | |
| 118 url_dict->GetString(kSyncGUID, &data->sync_guid); | |
| 119 | |
| 120 url_dict->GetString(kSuggestionsURL, &data->suggestions_url); | |
| 121 url_dict->GetString(kInstantURL, &data->instant_url); | |
| 122 url_dict->GetString(kImageURL, &data->image_url); | |
| 123 url_dict->GetString(kNewTabURL, &data->new_tab_url); | |
| 124 | |
| 125 std::string favicon_url; | |
| 126 std::string originating_url; | |
| 127 url_dict->GetString(kFaviconURL, &favicon_url); | |
| 128 url_dict->GetString(kOriginatingURL, &originating_url); | |
| 129 data->favicon_url = GURL(favicon_url); | |
| 130 data->originating_url = GURL(originating_url); | |
| 131 | |
| 132 url_dict->GetString(kSearchURLPostParams, &data->search_url_post_params); | |
| 133 url_dict->GetString(kSuggestionsURLPostParams, | |
| 134 &data->suggestions_url_post_params); | |
| 135 url_dict->GetString(kInstantURLPostParams, &data->instant_url_post_params); | |
| 136 url_dict->GetString(kImageURLPostParams, &data->image_url_post_params); | |
| 137 | |
| 138 url_dict->GetBoolean(kSafeForAutoReplace, &data->safe_for_autoreplace); | |
| 139 | |
| 140 double date_created = 0.0; | |
| 141 double last_modified = 0.0; | |
| 142 url_dict->GetDouble(kDateCreated, &date_created); | |
| 143 url_dict->GetDouble(kLastModified, &last_modified); | |
| 144 data->date_created = base::Time::FromInternalValue(date_created); | |
| 145 data->last_modified = base::Time::FromInternalValue(last_modified); | |
| 146 | |
| 147 url_dict->GetInteger(kUsageCount, &data->usage_count); | |
| 148 | |
| 149 const base::ListValue* alternate_urls; | |
| 150 url_dict->GetList(kAlternateURLs, &alternate_urls); | |
| 151 data->alternate_urls.clear(); | |
| 152 for (base::ListValue::const_iterator it = alternate_urls->begin(); | |
| 153 it != alternate_urls->end(); ++it) { | |
| 154 std::string alternate_url; | |
| 155 if ((*it)->GetAsString(&alternate_url)) | |
| 156 data->alternate_urls.push_back(alternate_url); | |
| 157 } | 138 } |
| 158 | 139 |
| 159 const base::ListValue* encodings; | 140 if (source) |
| 160 url_dict->GetList(kInputEncodings, &encodings); | 141 *source = FROM_FALLBACK; |
| 161 data->input_encodings.clear(); | 142 return TemplateURLService::FallbackSearchEnginesDisabled() |
| 162 for (base::ListValue::const_iterator it = encodings->begin(); | 143 ? NULL |
| 163 it != encodings->end(); ++it) { | 144 : fallback_default_search_.get(); |
| 164 std::string encoding; | 145 } |
| 165 if ((*it)->GetAsString(&encoding)) | |
| 166 data->input_encodings.push_back(encoding); | |
| 167 } | |
| 168 | 146 |
| 169 url_dict->GetString(kSearchTermsReplacementKey, | 147 DefaultSearchManager::Source |
| 170 &data->search_terms_replacement_key); | 148 DefaultSearchManager::GetDefaultSearchEngineSource() const { |
| 171 | 149 Source source; |
| 172 url_dict->GetBoolean(kCreatedByPolicy, &data->created_by_policy); | 150 GetDefaultSearchEngine(&source); |
| 173 | 151 return source; |
| 174 data->show_in_default_list = true; | |
| 175 | |
| 176 return true; | |
| 177 } | 152 } |
| 178 | 153 |
| 179 void DefaultSearchManager::SetUserSelectedDefaultSearchEngine( | 154 void DefaultSearchManager::SetUserSelectedDefaultSearchEngine( |
| 180 const TemplateURLData& data) { | 155 const TemplateURLData& data) { |
| 156 if (!pref_service_) { |
| 157 prefs_default_search_.reset(new TemplateURLData(data)); |
| 158 MergePrefsDataWithPrepopulated(); |
| 159 NotifyObserver(); |
| 160 return; |
| 161 } |
| 162 |
| 181 base::DictionaryValue url_dict; | 163 base::DictionaryValue url_dict; |
| 182 url_dict.SetString(kID, base::Int64ToString(data.id)); | 164 url_dict.SetString(kID, base::Int64ToString(data.id)); |
| 183 url_dict.SetString(kShortName, data.short_name); | 165 url_dict.SetString(kShortName, data.short_name); |
| 184 url_dict.SetString(kKeyword, data.keyword()); | 166 url_dict.SetString(kKeyword, data.keyword()); |
| 185 url_dict.SetInteger(kPrepopulateID, data.prepopulate_id); | 167 url_dict.SetInteger(kPrepopulateID, data.prepopulate_id); |
| 186 url_dict.SetString(kSyncGUID, data.sync_guid); | 168 url_dict.SetString(kSyncGUID, data.sync_guid); |
| 187 | 169 |
| 188 url_dict.SetString(kURL, data.url()); | 170 url_dict.SetString(kURL, data.url()); |
| 189 url_dict.SetString(kSuggestionsURL, data.suggestions_url); | 171 url_dict.SetString(kSuggestionsURL, data.suggestions_url); |
| 190 url_dict.SetString(kInstantURL, data.instant_url); | 172 url_dict.SetString(kInstantURL, data.instant_url); |
| 191 url_dict.SetString(kImageURL, data.image_url); | 173 url_dict.SetString(kImageURL, data.image_url); |
| 192 url_dict.SetString(kNewTabURL, data.new_tab_url); | 174 url_dict.SetString(kNewTabURL, data.new_tab_url); |
| 193 url_dict.SetString(kFaviconURL, data.favicon_url.spec()); | 175 url_dict.SetString(kFaviconURL, data.favicon_url.spec()); |
| 194 url_dict.SetString(kOriginatingURL, data.originating_url.spec()); | 176 url_dict.SetString(kOriginatingURL, data.originating_url.spec()); |
| 195 | 177 |
| 196 url_dict.SetString(kSearchURLPostParams, data.search_url_post_params); | 178 url_dict.SetString(kSearchURLPostParams, data.search_url_post_params); |
| 197 url_dict.SetString(kSuggestionsURLPostParams, | 179 url_dict.SetString(kSuggestionsURLPostParams, |
| 198 data.suggestions_url_post_params); | 180 data.suggestions_url_post_params); |
| 199 url_dict.SetString(kInstantURLPostParams, data.instant_url_post_params); | 181 url_dict.SetString(kInstantURLPostParams, data.instant_url_post_params); |
| 200 url_dict.SetString(kImageURLPostParams, data.image_url_post_params); | 182 url_dict.SetString(kImageURLPostParams, data.image_url_post_params); |
| 201 | 183 |
| 202 url_dict.SetBoolean(kSafeForAutoReplace, data.safe_for_autoreplace); | 184 url_dict.SetBoolean(kSafeForAutoReplace, data.safe_for_autoreplace); |
| 203 | 185 |
| 204 url_dict.SetDouble(kDateCreated, data.date_created.ToInternalValue()); | 186 url_dict.SetString(kDateCreated, |
| 205 url_dict.SetDouble(kLastModified, data.last_modified.ToInternalValue()); | 187 base::Int64ToString(data.date_created.ToInternalValue())); |
| 188 url_dict.SetString(kLastModified, |
| 189 base::Int64ToString(data.last_modified.ToInternalValue())); |
| 206 url_dict.SetInteger(kUsageCount, data.usage_count); | 190 url_dict.SetInteger(kUsageCount, data.usage_count); |
| 207 | 191 |
| 208 scoped_ptr<base::ListValue> alternate_urls(new base::ListValue); | 192 scoped_ptr<base::ListValue> alternate_urls(new base::ListValue); |
| 209 for (std::vector<std::string>::const_iterator it = | 193 for (std::vector<std::string>::const_iterator it = |
| 210 data.alternate_urls.begin(); | 194 data.alternate_urls.begin(); |
| 211 it != data.alternate_urls.end(); ++it) { | 195 it != data.alternate_urls.end(); ++it) { |
| 212 alternate_urls->AppendString(*it); | 196 alternate_urls->AppendString(*it); |
| 213 } | 197 } |
| 214 url_dict.Set(kAlternateURLs, alternate_urls.release()); | 198 url_dict.Set(kAlternateURLs, alternate_urls.release()); |
| 215 | 199 |
| 216 scoped_ptr<base::ListValue> encodings(new base::ListValue); | 200 scoped_ptr<base::ListValue> encodings(new base::ListValue); |
| 217 for (std::vector<std::string>::const_iterator it = | 201 for (std::vector<std::string>::const_iterator it = |
| 218 data.input_encodings.begin(); | 202 data.input_encodings.begin(); |
| 219 it != data.input_encodings.end(); ++it) { | 203 it != data.input_encodings.end(); ++it) { |
| 220 encodings->AppendString(*it); | 204 encodings->AppendString(*it); |
| 221 } | 205 } |
| 222 url_dict.Set(kInputEncodings, encodings.release()); | 206 url_dict.Set(kInputEncodings, encodings.release()); |
| 223 | 207 |
| 224 url_dict.SetString(kSearchTermsReplacementKey, | 208 url_dict.SetString(kSearchTermsReplacementKey, |
| 225 data.search_terms_replacement_key); | 209 data.search_terms_replacement_key); |
| 226 | 210 |
| 227 url_dict.SetBoolean(kCreatedByPolicy, data.created_by_policy); | 211 url_dict.SetBoolean(kCreatedByPolicy, data.created_by_policy); |
| 228 | 212 |
| 229 pref_service_->Set(kDefaultSearchProviderData, url_dict); | 213 pref_service_->Set(kDefaultSearchProviderDataPrefName, url_dict); |
| 214 } |
| 215 |
| 216 void DefaultSearchManager::SetExtensionControlledDefaultSearchEngine( |
| 217 const TemplateURLData& data) { |
| 218 extension_default_search_.reset(new TemplateURLData(data)); |
| 219 if (GetDefaultSearchEngineSource() == FROM_EXTENSION) |
| 220 NotifyObserver(); |
| 221 } |
| 222 |
| 223 void DefaultSearchManager::ClearExtensionControlledDefaultSearchEngine() { |
| 224 Source old_source = GetDefaultSearchEngineSource(); |
| 225 extension_default_search_.reset(); |
| 226 if (old_source == FROM_EXTENSION) |
| 227 NotifyObserver(); |
| 230 } | 228 } |
| 231 | 229 |
| 232 void DefaultSearchManager::ClearUserSelectedDefaultSearchEngine() { | 230 void DefaultSearchManager::ClearUserSelectedDefaultSearchEngine() { |
| 233 pref_service_->ClearPref(kDefaultSearchProviderData); | 231 if (pref_service_) { |
| 234 } | 232 pref_service_->ClearPref(kDefaultSearchProviderDataPrefName); |
| 233 } else { |
| 234 prefs_default_search_.reset(); |
| 235 NotifyObserver(); |
| 236 } |
| 237 } |
| 238 |
| 239 void DefaultSearchManager::OnDefaultSearchPrefChanged() { |
| 240 Source source = GetDefaultSearchEngineSource(); |
| 241 LoadDefaultSearchEngineFromPrefs(); |
| 242 |
| 243 // If we were/are FROM_USER or FROM_POLICY the effective DSE may have changed. |
| 244 if (source != FROM_USER && source != FROM_POLICY) |
| 245 source = GetDefaultSearchEngineSource(); |
| 246 if (source == FROM_USER || source == FROM_POLICY) |
| 247 NotifyObserver(); |
| 248 } |
| 249 |
| 250 void DefaultSearchManager::OnOverridesPrefChanged() { |
| 251 LoadPrepopulatedDefaultSearch(); |
| 252 |
| 253 TemplateURLData* effective_data = GetDefaultSearchEngine(NULL); |
| 254 if (effective_data && effective_data->prepopulate_id) { |
| 255 // A user-selected, policy-selected or fallback pre-populated engine is |
| 256 // active and may have changed with this event. |
| 257 NotifyObserver(); |
| 258 } |
| 259 } |
| 260 |
| 261 void DefaultSearchManager::MergePrefsDataWithPrepopulated() { |
| 262 if (!prefs_default_search_ || !prefs_default_search_->prepopulate_id) |
| 263 return; |
| 264 |
| 265 size_t default_search_index; |
| 266 ScopedVector<TemplateURLData> prepopulated_urls = |
| 267 TemplateURLPrepopulateData::GetPrepopulatedEngines(pref_service_, |
| 268 &default_search_index); |
| 269 |
| 270 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { |
| 271 if (prepopulated_urls[i]->prepopulate_id == |
| 272 prefs_default_search_->prepopulate_id) { |
| 273 if (!prefs_default_search_->safe_for_autoreplace) { |
| 274 prepopulated_urls[i]->safe_for_autoreplace = false; |
| 275 prepopulated_urls[i]->SetKeyword(prefs_default_search_->keyword()); |
| 276 prepopulated_urls[i]->short_name = prefs_default_search_->short_name; |
| 277 } |
| 278 prepopulated_urls[i]->id = prefs_default_search_->id; |
| 279 prepopulated_urls[i]->sync_guid = prefs_default_search_->sync_guid; |
| 280 prepopulated_urls[i]->date_created = prefs_default_search_->date_created; |
| 281 prepopulated_urls[i]->last_modified = |
| 282 prefs_default_search_->last_modified; |
| 283 prefs_default_search_.reset(prepopulated_urls[i]); |
| 284 prepopulated_urls.weak_erase(prepopulated_urls.begin() + i); |
| 285 return; |
| 286 } |
| 287 } |
| 288 } |
| 289 |
| 290 void DefaultSearchManager::LoadDefaultSearchEngineFromPrefs() { |
| 291 if (!pref_service_) |
| 292 return; |
| 293 |
| 294 prefs_default_search_.reset(); |
| 295 default_search_controlled_by_policy_ = |
| 296 IsDefaultSearchProviderManaged(pref_service_); |
| 297 |
| 298 const base::DictionaryValue* url_dict = |
| 299 pref_service_->GetDictionary(kDefaultSearchProviderDataPrefName); |
| 300 if (url_dict->empty()) |
| 301 return; |
| 302 |
| 303 if (default_search_controlled_by_policy_) { |
| 304 bool disabled_by_policy = false; |
| 305 if (url_dict->GetBoolean(kDisabledByPolicy, &disabled_by_policy) && |
| 306 disabled_by_policy) |
| 307 return; |
| 308 } |
| 309 |
| 310 std::string search_url; |
| 311 base::string16 keyword; |
| 312 url_dict->GetString(kURL, &search_url); |
| 313 url_dict->GetString(kKeyword, &keyword); |
| 314 if (search_url.empty() || keyword.empty()) |
| 315 return; |
| 316 |
| 317 prefs_default_search_.reset(new TemplateURLData); |
| 318 prefs_default_search_->SetKeyword(keyword); |
| 319 prefs_default_search_->SetURL(search_url); |
| 320 |
| 321 std::string id; |
| 322 url_dict->GetString(kID, &id); |
| 323 base::StringToInt64(id, &prefs_default_search_->id); |
| 324 url_dict->GetString(kShortName, &prefs_default_search_->short_name); |
| 325 url_dict->GetInteger(kPrepopulateID, &prefs_default_search_->prepopulate_id); |
| 326 url_dict->GetString(kSyncGUID, &prefs_default_search_->sync_guid); |
| 327 |
| 328 url_dict->GetString(kSuggestionsURL, &prefs_default_search_->suggestions_url); |
| 329 url_dict->GetString(kInstantURL, &prefs_default_search_->instant_url); |
| 330 url_dict->GetString(kImageURL, &prefs_default_search_->image_url); |
| 331 url_dict->GetString(kNewTabURL, &prefs_default_search_->new_tab_url); |
| 332 |
| 333 std::string favicon_url; |
| 334 std::string originating_url; |
| 335 url_dict->GetString(kFaviconURL, &favicon_url); |
| 336 url_dict->GetString(kOriginatingURL, &originating_url); |
| 337 prefs_default_search_->favicon_url = GURL(favicon_url); |
| 338 prefs_default_search_->originating_url = GURL(originating_url); |
| 339 |
| 340 url_dict->GetString(kSearchURLPostParams, |
| 341 &prefs_default_search_->search_url_post_params); |
| 342 url_dict->GetString(kSuggestionsURLPostParams, |
| 343 &prefs_default_search_->suggestions_url_post_params); |
| 344 url_dict->GetString(kInstantURLPostParams, |
| 345 &prefs_default_search_->instant_url_post_params); |
| 346 url_dict->GetString(kImageURLPostParams, |
| 347 &prefs_default_search_->image_url_post_params); |
| 348 |
| 349 url_dict->GetBoolean(kSafeForAutoReplace, |
| 350 &prefs_default_search_->safe_for_autoreplace); |
| 351 |
| 352 std::string date_created_str; |
| 353 std::string last_modified_str; |
| 354 url_dict->GetString(kDateCreated, &date_created_str); |
| 355 url_dict->GetString(kLastModified, &last_modified_str); |
| 356 |
| 357 int64 date_created = 0; |
| 358 if (base::StringToInt64(date_created_str, &date_created)) { |
| 359 prefs_default_search_->date_created = |
| 360 base::Time::FromInternalValue(date_created); |
| 361 } |
| 362 |
| 363 int64 last_modified = 0; |
| 364 if (base::StringToInt64(date_created_str, &last_modified)) { |
| 365 prefs_default_search_->last_modified = |
| 366 base::Time::FromInternalValue(last_modified); |
| 367 } |
| 368 |
| 369 url_dict->GetInteger(kUsageCount, &prefs_default_search_->usage_count); |
| 370 |
| 371 const base::ListValue* alternate_urls = NULL; |
| 372 if (url_dict->GetList(kAlternateURLs, &alternate_urls)) { |
| 373 for (base::ListValue::const_iterator it = alternate_urls->begin(); |
| 374 it != alternate_urls->end(); |
| 375 ++it) { |
| 376 std::string alternate_url; |
| 377 if ((*it)->GetAsString(&alternate_url)) |
| 378 prefs_default_search_->alternate_urls.push_back(alternate_url); |
| 379 } |
| 380 } |
| 381 |
| 382 const base::ListValue* encodings = NULL; |
| 383 if (url_dict->GetList(kInputEncodings, &encodings)) { |
| 384 for (base::ListValue::const_iterator it = encodings->begin(); |
| 385 it != encodings->end(); |
| 386 ++it) { |
| 387 std::string encoding; |
| 388 if ((*it)->GetAsString(&encoding)) |
| 389 prefs_default_search_->input_encodings.push_back(encoding); |
| 390 } |
| 391 } |
| 392 |
| 393 url_dict->GetString(kSearchTermsReplacementKey, |
| 394 &prefs_default_search_->search_terms_replacement_key); |
| 395 |
| 396 url_dict->GetBoolean(kCreatedByPolicy, |
| 397 &prefs_default_search_->created_by_policy); |
| 398 |
| 399 prefs_default_search_->show_in_default_list = true; |
| 400 MergePrefsDataWithPrepopulated(); |
| 401 } |
| 402 |
| 403 void DefaultSearchManager::LoadPrepopulatedDefaultSearch() { |
| 404 scoped_ptr<TemplateURLData> data = |
| 405 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(pref_service_); |
| 406 fallback_default_search_ = data.Pass(); |
| 407 MergePrefsDataWithPrepopulated(); |
| 408 } |
| 409 |
| 410 void DefaultSearchManager::NotifyObserver() { |
| 411 if (!change_observer_.is_null()) { |
| 412 Source source = FROM_FALLBACK; |
| 413 TemplateURLData* data = GetDefaultSearchEngine(&source); |
| 414 change_observer_.Run(data, source); |
| 415 } |
| 416 } |
| OLD | NEW |