| 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 "components/search_engines/default_search_manager.h" | 5 #include "components/search_engines/default_search_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/i18n/case_conversion.h" | 16 #include "base/i18n/case_conversion.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/strings/string_number_conversions.h" | |
| 19 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 20 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 23 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
| 24 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 25 #include "components/prefs/pref_value_map.h" | 24 #include "components/prefs/pref_value_map.h" |
| 26 #include "components/search_engines/search_engines_pref_names.h" | 25 #include "components/search_engines/search_engines_pref_names.h" |
| 27 #include "components/search_engines/template_url_data.h" | 26 #include "components/search_engines/template_url_data.h" |
| 28 #include "components/search_engines/template_url_prepopulate_data.h" | 27 #include "components/search_engines/template_url_prepopulate_data.h" |
| 28 #include "components/search_engines/util.h" |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 bool g_fallback_search_engines_disabled = false; | 32 bool g_fallback_search_engines_disabled = false; |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 // A dictionary to hold all data related to the Default Search Engine. | 36 // A dictionary to hold all data related to the Default Search Engine. |
| 37 // Eventually, this should replace all the data stored in the | 37 // Eventually, this should replace all the data stored in the |
| 38 // default_search_provider.* prefs. | 38 // default_search_provider.* prefs. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 152 |
| 153 void DefaultSearchManager::SetUserSelectedDefaultSearchEngine( | 153 void DefaultSearchManager::SetUserSelectedDefaultSearchEngine( |
| 154 const TemplateURLData& data) { | 154 const TemplateURLData& data) { |
| 155 if (!pref_service_) { | 155 if (!pref_service_) { |
| 156 prefs_default_search_.reset(new TemplateURLData(data)); | 156 prefs_default_search_.reset(new TemplateURLData(data)); |
| 157 MergePrefsDataWithPrepopulated(); | 157 MergePrefsDataWithPrepopulated(); |
| 158 NotifyObserver(); | 158 NotifyObserver(); |
| 159 return; | 159 return; |
| 160 } | 160 } |
| 161 | 161 |
| 162 base::DictionaryValue url_dict; | 162 std::unique_ptr<base::Value> url_dict(TemplateUrlDataToDictionary(data)); |
| 163 url_dict.SetString(kID, base::Int64ToString(data.id)); | 163 DCHECK(url_dict); |
| 164 url_dict.SetString(kShortName, data.short_name()); | 164 pref_service_->Set(kDefaultSearchProviderDataPrefName, *url_dict); |
| 165 url_dict.SetString(kKeyword, data.keyword()); | |
| 166 url_dict.SetInteger(kPrepopulateID, data.prepopulate_id); | |
| 167 url_dict.SetString(kSyncGUID, data.sync_guid); | |
| 168 | |
| 169 url_dict.SetString(kURL, data.url()); | |
| 170 url_dict.SetString(kSuggestionsURL, data.suggestions_url); | |
| 171 url_dict.SetString(kInstantURL, data.instant_url); | |
| 172 url_dict.SetString(kImageURL, data.image_url); | |
| 173 url_dict.SetString(kNewTabURL, data.new_tab_url); | |
| 174 url_dict.SetString(kFaviconURL, data.favicon_url.spec()); | |
| 175 url_dict.SetString(kOriginatingURL, data.originating_url.spec()); | |
| 176 | |
| 177 url_dict.SetString(kSearchURLPostParams, data.search_url_post_params); | |
| 178 url_dict.SetString(kSuggestionsURLPostParams, | |
| 179 data.suggestions_url_post_params); | |
| 180 url_dict.SetString(kInstantURLPostParams, data.instant_url_post_params); | |
| 181 url_dict.SetString(kImageURLPostParams, data.image_url_post_params); | |
| 182 | |
| 183 url_dict.SetBoolean(kSafeForAutoReplace, data.safe_for_autoreplace); | |
| 184 | |
| 185 url_dict.SetString(kDateCreated, | |
| 186 base::Int64ToString(data.date_created.ToInternalValue())); | |
| 187 url_dict.SetString(kLastModified, | |
| 188 base::Int64ToString(data.last_modified.ToInternalValue())); | |
| 189 url_dict.SetInteger(kUsageCount, data.usage_count); | |
| 190 | |
| 191 std::unique_ptr<base::ListValue> alternate_urls(new base::ListValue); | |
| 192 for (std::vector<std::string>::const_iterator it = | |
| 193 data.alternate_urls.begin(); | |
| 194 it != data.alternate_urls.end(); ++it) { | |
| 195 alternate_urls->AppendString(*it); | |
| 196 } | |
| 197 url_dict.Set(kAlternateURLs, alternate_urls.release()); | |
| 198 | |
| 199 std::unique_ptr<base::ListValue> encodings(new base::ListValue); | |
| 200 for (std::vector<std::string>::const_iterator it = | |
| 201 data.input_encodings.begin(); | |
| 202 it != data.input_encodings.end(); ++it) { | |
| 203 encodings->AppendString(*it); | |
| 204 } | |
| 205 url_dict.Set(kInputEncodings, encodings.release()); | |
| 206 | |
| 207 url_dict.SetString(kSearchTermsReplacementKey, | |
| 208 data.search_terms_replacement_key); | |
| 209 | |
| 210 url_dict.SetBoolean(kCreatedByPolicy, data.created_by_policy); | |
| 211 | |
| 212 pref_service_->Set(kDefaultSearchProviderDataPrefName, url_dict); | |
| 213 } | |
| 214 | |
| 215 void DefaultSearchManager::SetExtensionControlledDefaultSearchEngine( | |
| 216 const TemplateURLData& data) { | |
| 217 extension_default_search_.reset(new TemplateURLData(data)); | |
| 218 if (GetDefaultSearchEngineSource() == FROM_EXTENSION) | |
| 219 NotifyObserver(); | |
| 220 } | |
| 221 | |
| 222 void DefaultSearchManager::ClearExtensionControlledDefaultSearchEngine() { | |
| 223 Source old_source = GetDefaultSearchEngineSource(); | |
| 224 extension_default_search_.reset(); | |
| 225 if (old_source == FROM_EXTENSION) | |
| 226 NotifyObserver(); | |
| 227 } | 165 } |
| 228 | 166 |
| 229 void DefaultSearchManager::ClearUserSelectedDefaultSearchEngine() { | 167 void DefaultSearchManager::ClearUserSelectedDefaultSearchEngine() { |
| 230 if (pref_service_) { | 168 if (pref_service_) { |
| 231 pref_service_->ClearPref(kDefaultSearchProviderDataPrefName); | 169 pref_service_->ClearPref(kDefaultSearchProviderDataPrefName); |
| 232 } else { | 170 } else { |
| 233 prefs_default_search_.reset(); | 171 prefs_default_search_.reset(); |
| 234 NotifyObserver(); | 172 NotifyObserver(); |
| 235 } | 173 } |
| 236 } | 174 } |
| 237 | 175 |
| 238 void DefaultSearchManager::OnDefaultSearchPrefChanged() { | 176 void DefaultSearchManager::OnDefaultSearchPrefChanged() { |
| 239 Source source = GetDefaultSearchEngineSource(); | 177 Source source = GetDefaultSearchEngineSource(); |
| 240 LoadDefaultSearchEngineFromPrefs(); | 178 LoadDefaultSearchEngineFromPrefs(); |
| 241 | 179 |
| 242 // If we were/are FROM_USER or FROM_POLICY the effective DSE may have changed. | 180 if (source == FROM_FALLBACK) |
| 243 if (source != FROM_USER && source != FROM_POLICY) | |
| 244 source = GetDefaultSearchEngineSource(); | 181 source = GetDefaultSearchEngineSource(); |
| 245 if (source == FROM_USER || source == FROM_POLICY) | 182 // The effective DSE may have changed if it was/become non fallback. |
| 246 NotifyObserver(); | 183 if (source != FROM_FALLBACK) |
| 184 NotifyObserver(); |
| 247 } | 185 } |
| 248 | 186 |
| 249 void DefaultSearchManager::OnOverridesPrefChanged() { | 187 void DefaultSearchManager::OnOverridesPrefChanged() { |
| 250 LoadPrepopulatedDefaultSearch(); | 188 LoadPrepopulatedDefaultSearch(); |
| 251 | 189 |
| 252 TemplateURLData* effective_data = GetDefaultSearchEngine(NULL); | 190 TemplateURLData* effective_data = GetDefaultSearchEngine(NULL); |
| 253 if (effective_data && effective_data->prepopulate_id) { | 191 if (effective_data && effective_data->prepopulate_id) { |
| 254 // A user-selected, policy-selected or fallback pre-populated engine is | 192 // A user-selected, policy-selected or fallback pre-populated engine is |
| 255 // active and may have changed with this event. | 193 // active and may have changed with this event. |
| 256 NotifyObserver(); | 194 NotifyObserver(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 283 prefs_default_search_ = std::move(engine); | 221 prefs_default_search_ = std::move(engine); |
| 284 return; | 222 return; |
| 285 } | 223 } |
| 286 } | 224 } |
| 287 | 225 |
| 288 void DefaultSearchManager::LoadDefaultSearchEngineFromPrefs() { | 226 void DefaultSearchManager::LoadDefaultSearchEngineFromPrefs() { |
| 289 if (!pref_service_) | 227 if (!pref_service_) |
| 290 return; | 228 return; |
| 291 | 229 |
| 292 prefs_default_search_.reset(); | 230 prefs_default_search_.reset(); |
| 231 extension_default_search_.reset(); |
| 293 const PrefService::Preference* pref = | 232 const PrefService::Preference* pref = |
| 294 pref_service_->FindPreference(kDefaultSearchProviderDataPrefName); | 233 pref_service_->FindPreference(kDefaultSearchProviderDataPrefName); |
| 295 DCHECK(pref); | 234 DCHECK(pref); |
| 296 default_search_controlled_by_policy_ = pref->IsManaged(); | 235 default_search_controlled_by_policy_ = pref->IsManaged(); |
| 297 | 236 |
| 298 const base::DictionaryValue* url_dict = | 237 const base::DictionaryValue* url_dict = |
| 299 pref_service_->GetDictionary(kDefaultSearchProviderDataPrefName); | 238 pref_service_->GetDictionary(kDefaultSearchProviderDataPrefName); |
| 300 if (url_dict->empty()) | 239 if (url_dict->empty()) |
| 301 return; | 240 return; |
| 302 | 241 |
| 303 if (default_search_controlled_by_policy_) { | 242 if (default_search_controlled_by_policy_) { |
| 304 bool disabled_by_policy = false; | 243 bool disabled_by_policy = false; |
| 305 if (url_dict->GetBoolean(kDisabledByPolicy, &disabled_by_policy) && | 244 if (url_dict->GetBoolean(kDisabledByPolicy, &disabled_by_policy) && |
| 306 disabled_by_policy) | 245 disabled_by_policy) |
| 307 return; | 246 return; |
| 308 } | 247 } |
| 309 | 248 |
| 310 std::string search_url; | 249 std::string search_url; |
| 311 base::string16 keyword; | 250 base::string16 keyword; |
| 312 url_dict->GetString(kURL, &search_url); | 251 url_dict->GetString(kURL, &search_url); |
| 313 url_dict->GetString(kKeyword, &keyword); | 252 url_dict->GetString(kKeyword, &keyword); |
| 314 if (search_url.empty() || keyword.empty()) | 253 if (search_url.empty() || keyword.empty()) |
| 315 return; | 254 return; |
| 316 | 255 |
| 317 prefs_default_search_.reset(new TemplateURLData); | 256 // Check if default search preference is overriden by extension. |
| 318 prefs_default_search_->SetKeyword(keyword); | 257 if (pref->IsExtensionControlled()) { |
| 319 prefs_default_search_->SetURL(search_url); | 258 extension_default_search_ = TemplateUrlDataFromDictionary(*url_dict); |
| 320 | 259 } else { |
| 321 std::string id; | 260 prefs_default_search_ = TemplateUrlDataFromDictionary(*url_dict); |
| 322 url_dict->GetString(kID, &id); | 261 MergePrefsDataWithPrepopulated(); |
| 323 base::StringToInt64(id, &prefs_default_search_->id); | |
| 324 base::string16 short_name; | |
| 325 url_dict->GetString(kShortName, &short_name); | |
| 326 prefs_default_search_->SetShortName(short_name); | |
| 327 url_dict->GetInteger(kPrepopulateID, &prefs_default_search_->prepopulate_id); | |
| 328 url_dict->GetString(kSyncGUID, &prefs_default_search_->sync_guid); | |
| 329 | |
| 330 url_dict->GetString(kSuggestionsURL, &prefs_default_search_->suggestions_url); | |
| 331 url_dict->GetString(kInstantURL, &prefs_default_search_->instant_url); | |
| 332 url_dict->GetString(kImageURL, &prefs_default_search_->image_url); | |
| 333 url_dict->GetString(kNewTabURL, &prefs_default_search_->new_tab_url); | |
| 334 | |
| 335 std::string favicon_url; | |
| 336 std::string originating_url; | |
| 337 url_dict->GetString(kFaviconURL, &favicon_url); | |
| 338 url_dict->GetString(kOriginatingURL, &originating_url); | |
| 339 prefs_default_search_->favicon_url = GURL(favicon_url); | |
| 340 prefs_default_search_->originating_url = GURL(originating_url); | |
| 341 | |
| 342 url_dict->GetString(kSearchURLPostParams, | |
| 343 &prefs_default_search_->search_url_post_params); | |
| 344 url_dict->GetString(kSuggestionsURLPostParams, | |
| 345 &prefs_default_search_->suggestions_url_post_params); | |
| 346 url_dict->GetString(kInstantURLPostParams, | |
| 347 &prefs_default_search_->instant_url_post_params); | |
| 348 url_dict->GetString(kImageURLPostParams, | |
| 349 &prefs_default_search_->image_url_post_params); | |
| 350 | |
| 351 url_dict->GetBoolean(kSafeForAutoReplace, | |
| 352 &prefs_default_search_->safe_for_autoreplace); | |
| 353 | |
| 354 std::string date_created_str; | |
| 355 std::string last_modified_str; | |
| 356 url_dict->GetString(kDateCreated, &date_created_str); | |
| 357 url_dict->GetString(kLastModified, &last_modified_str); | |
| 358 | |
| 359 int64_t date_created = 0; | |
| 360 if (base::StringToInt64(date_created_str, &date_created)) { | |
| 361 prefs_default_search_->date_created = | |
| 362 base::Time::FromInternalValue(date_created); | |
| 363 } | 262 } |
| 364 | |
| 365 int64_t last_modified = 0; | |
| 366 if (base::StringToInt64(date_created_str, &last_modified)) { | |
| 367 prefs_default_search_->last_modified = | |
| 368 base::Time::FromInternalValue(last_modified); | |
| 369 } | |
| 370 | |
| 371 url_dict->GetInteger(kUsageCount, &prefs_default_search_->usage_count); | |
| 372 | |
| 373 const base::ListValue* alternate_urls = NULL; | |
| 374 if (url_dict->GetList(kAlternateURLs, &alternate_urls)) { | |
| 375 for (base::ListValue::const_iterator it = alternate_urls->begin(); | |
| 376 it != alternate_urls->end(); | |
| 377 ++it) { | |
| 378 std::string alternate_url; | |
| 379 if ((*it)->GetAsString(&alternate_url)) | |
| 380 prefs_default_search_->alternate_urls.push_back(alternate_url); | |
| 381 } | |
| 382 } | |
| 383 | |
| 384 const base::ListValue* encodings = NULL; | |
| 385 if (url_dict->GetList(kInputEncodings, &encodings)) { | |
| 386 for (base::ListValue::const_iterator it = encodings->begin(); | |
| 387 it != encodings->end(); | |
| 388 ++it) { | |
| 389 std::string encoding; | |
| 390 if ((*it)->GetAsString(&encoding)) | |
| 391 prefs_default_search_->input_encodings.push_back(encoding); | |
| 392 } | |
| 393 } | |
| 394 | |
| 395 url_dict->GetString(kSearchTermsReplacementKey, | |
| 396 &prefs_default_search_->search_terms_replacement_key); | |
| 397 | |
| 398 url_dict->GetBoolean(kCreatedByPolicy, | |
| 399 &prefs_default_search_->created_by_policy); | |
| 400 | |
| 401 prefs_default_search_->show_in_default_list = true; | |
| 402 MergePrefsDataWithPrepopulated(); | |
| 403 } | 263 } |
| 404 | 264 |
| 405 void DefaultSearchManager::LoadPrepopulatedDefaultSearch() { | 265 void DefaultSearchManager::LoadPrepopulatedDefaultSearch() { |
| 406 std::unique_ptr<TemplateURLData> data = | 266 std::unique_ptr<TemplateURLData> data = |
| 407 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(pref_service_); | 267 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(pref_service_); |
| 408 fallback_default_search_ = std::move(data); | 268 fallback_default_search_ = std::move(data); |
| 409 MergePrefsDataWithPrepopulated(); | 269 MergePrefsDataWithPrepopulated(); |
| 410 } | 270 } |
| 411 | 271 |
| 412 void DefaultSearchManager::NotifyObserver() { | 272 void DefaultSearchManager::NotifyObserver() { |
| 413 if (!change_observer_.is_null()) { | 273 if (!change_observer_.is_null()) { |
| 414 Source source = FROM_FALLBACK; | 274 Source source = FROM_FALLBACK; |
| 415 TemplateURLData* data = GetDefaultSearchEngine(&source); | 275 TemplateURLData* data = GetDefaultSearchEngine(&source); |
| 416 change_observer_.Run(data, source); | 276 change_observer_.Run(data, source); |
| 417 } | 277 } |
| 418 } | 278 } |
| OLD | NEW |