| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/template_url_service.h" | 5 #include "chrome/browser/search_engines/template_url_service.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/environment.h" | 9 #include "base/environment.h" |
| 10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/string_split.h" | 13 #include "base/string_split.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/google/google_url_tracker.h" | 18 #include "chrome/browser/google/google_url_tracker.h" |
| 19 #include "chrome/browser/history/history.h" | 19 #include "chrome/browser/history/history.h" |
| 20 #include "chrome/browser/history/history_notifications.h" | 20 #include "chrome/browser/history/history_notifications.h" |
| 21 #include "chrome/browser/net/url_fixer_upper.h" | 21 #include "chrome/browser/net/url_fixer_upper.h" |
| 22 #include "chrome/browser/prefs/pref_service.h" | 22 #include "chrome/browser/prefs/pref_service.h" |
| 23 #include "chrome/browser/prefs/pref_set_observer.h" | 23 #include "chrome/browser/prefs/pref_set_observer.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/protector/protector.h" |
| 26 #include "chrome/browser/protector/setting_change.h" |
| 25 #include "chrome/browser/rlz/rlz.h" | 27 #include "chrome/browser/rlz/rlz.h" |
| 26 #include "chrome/browser/search_engines/search_host_to_urls_map.h" | 28 #include "chrome/browser/search_engines/search_host_to_urls_map.h" |
| 27 #include "chrome/browser/search_engines/search_terms_data.h" | 29 #include "chrome/browser/search_engines/search_terms_data.h" |
| 28 #include "chrome/browser/search_engines/template_url.h" | 30 #include "chrome/browser/search_engines/template_url.h" |
| 29 #include "chrome/browser/search_engines/template_url_service_observer.h" | 31 #include "chrome/browser/search_engines/template_url_service_observer.h" |
| 30 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 32 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 31 #include "chrome/browser/search_engines/util.h" | 33 #include "chrome/browser/search_engines/util.h" |
| 32 #include "chrome/browser/sync/api/sync_change.h" | 34 #include "chrome/browser/sync/api/sync_change.h" |
| 33 #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h" | 35 #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h" |
| 34 #include "chrome/common/chrome_notification_types.h" | 36 #include "chrome/common/chrome_notification_types.h" |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 url->url() && | 431 url->url() && |
| 430 url->url()->SupportsReplacement() && | 432 url->url()->SupportsReplacement() && |
| 431 !is_default_search_managed(); | 433 !is_default_search_managed(); |
| 432 } | 434 } |
| 433 | 435 |
| 434 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { | 436 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { |
| 435 if (is_default_search_managed_) { | 437 if (is_default_search_managed_) { |
| 436 NOTREACHED(); | 438 NOTREACHED(); |
| 437 return; | 439 return; |
| 438 } | 440 } |
| 439 if (default_search_provider_ == url) | 441 // Always persist the setting in the database, that way if the backup |
| 440 return; | 442 // signature has changed out from under us it gets reset correctly. |
| 441 SetDefaultSearchProviderNoNotify(url); | 443 SetDefaultSearchProviderNoNotify(url); |
| 442 NotifyObservers(); | 444 NotifyObservers(); |
| 443 } | 445 } |
| 444 | 446 |
| 445 const TemplateURL* TemplateURLService::GetDefaultSearchProvider() { | 447 const TemplateURL* TemplateURLService::GetDefaultSearchProvider() { |
| 446 if (loaded_ && !load_failed_) | 448 if (loaded_ && !load_failed_) |
| 447 return default_search_provider_; | 449 return default_search_provider_; |
| 448 | 450 |
| 449 // We're not loaded, rely on the default search provider stored in prefs. | 451 // We're not loaded, rely on the default search provider stored in prefs. |
| 450 return initial_default_search_provider_.get(); | 452 return initial_default_search_provider_.get(); |
| 451 } | 453 } |
| 452 | 454 |
| 455 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { |
| 456 // See if the prepoluated default still exists. |
| 457 scoped_ptr<TemplateURL> prepopulated_default( |
| 458 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs())); |
| 459 for (TemplateURLVector::iterator i = template_urls_.begin(); |
| 460 i != template_urls_.end(); ) { |
| 461 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id()) |
| 462 return *i; |
| 463 } |
| 464 // If not, use the first of the templates. |
| 465 return template_urls_.empty() ? NULL : template_urls_[0]; |
| 466 } |
| 467 |
| 453 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { | 468 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { |
| 454 model_observers_.AddObserver(observer); | 469 model_observers_.AddObserver(observer); |
| 455 } | 470 } |
| 456 | 471 |
| 457 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { | 472 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { |
| 458 model_observers_.RemoveObserver(observer); | 473 model_observers_.RemoveObserver(observer); |
| 459 } | 474 } |
| 460 | 475 |
| 461 void TemplateURLService::Load() { | 476 void TemplateURLService::Load() { |
| 462 if (loaded_ || load_handle_) | 477 if (loaded_ || load_handle_) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 &default_search_provider, | 519 &default_search_provider, |
| 505 &new_resource_keyword_version); | 520 &new_resource_keyword_version); |
| 506 | 521 |
| 507 bool database_specified_a_default = NULL != default_search_provider; | 522 bool database_specified_a_default = NULL != default_search_provider; |
| 508 | 523 |
| 509 // Check if default search provider is now managed. | 524 // Check if default search provider is now managed. |
| 510 scoped_ptr<TemplateURL> default_from_prefs; | 525 scoped_ptr<TemplateURL> default_from_prefs; |
| 511 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, | 526 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, |
| 512 &is_default_search_managed_); | 527 &is_default_search_managed_); |
| 513 | 528 |
| 529 // Check if the default search provider has been changed and notify |
| 530 // Protector instance about it. Don't check if the default search is |
| 531 // managed. |
| 532 const TemplateURL* backup_default_search_provider = NULL; |
| 533 if (!is_default_search_managed_ && |
| 534 DidDefaultSearchProviderChange( |
| 535 *result, |
| 536 template_urls, |
| 537 &backup_default_search_provider)) { |
| 538 // Protector will delete itself when it's needed no longer. |
| 539 protector::Protector* protector = new protector::Protector(profile()); |
| 540 protector->ShowChange(protector::CreateDefaultSearchProviderChange( |
| 541 default_search_provider, |
| 542 backup_default_search_provider)); |
| 543 } |
| 544 |
| 514 // Remove entries that were created because of policy as they may have | 545 // Remove entries that were created because of policy as they may have |
| 515 // changed since the database was saved. | 546 // changed since the database was saved. |
| 516 RemoveProvidersCreatedByPolicy(&template_urls, | 547 RemoveProvidersCreatedByPolicy(&template_urls, |
| 517 &default_search_provider, | 548 &default_search_provider, |
| 518 default_from_prefs.get()); | 549 default_from_prefs.get()); |
| 519 | 550 |
| 520 if (is_default_search_managed_) { | 551 if (is_default_search_managed_) { |
| 521 SetTemplateURLs(template_urls); | 552 SetTemplateURLs(template_urls); |
| 522 | 553 |
| 523 if (TemplateURLsHaveSamePrefs(default_search_provider, | 554 if (TemplateURLsHaveSamePrefs(default_search_provider, |
| (...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1447 default_search_provider_->created_by_policy()) { | 1478 default_search_provider_->created_by_policy()) { |
| 1448 const TemplateURL* old_default = default_search_provider_; | 1479 const TemplateURL* old_default = default_search_provider_; |
| 1449 default_search_provider_ = NULL; | 1480 default_search_provider_ = NULL; |
| 1450 RemoveNoNotify(old_default); | 1481 RemoveNoNotify(old_default); |
| 1451 } | 1482 } |
| 1452 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); | 1483 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); |
| 1453 } | 1484 } |
| 1454 NotifyObservers(); | 1485 NotifyObservers(); |
| 1455 } | 1486 } |
| 1456 | 1487 |
| 1457 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { | |
| 1458 // See if the prepoluated default still exists. | |
| 1459 scoped_ptr<TemplateURL> prepopulated_default( | |
| 1460 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs())); | |
| 1461 for (TemplateURLVector::iterator i = template_urls_.begin(); | |
| 1462 i != template_urls_.end(); ) { | |
| 1463 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id()) | |
| 1464 return *i; | |
| 1465 } | |
| 1466 // If not, use the first of the templates. | |
| 1467 if (!template_urls_.empty()) { | |
| 1468 return template_urls_[0]; | |
| 1469 } | |
| 1470 return NULL; | |
| 1471 } | |
| 1472 | |
| 1473 void TemplateURLService::SetDefaultSearchProviderNoNotify( | 1488 void TemplateURLService::SetDefaultSearchProviderNoNotify( |
| 1474 const TemplateURL* url) { | 1489 const TemplateURL* url) { |
| 1475 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != | 1490 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != |
| 1476 template_urls_.end()); | 1491 template_urls_.end()); |
| 1477 default_search_provider_ = url; | 1492 default_search_provider_ = url; |
| 1478 | 1493 |
| 1479 if (url) { | 1494 if (url) { |
| 1480 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); | 1495 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); |
| 1481 // Don't mark the url as edited, otherwise we won't be able to rev the | 1496 // Don't mark the url as edited, otherwise we won't be able to rev the |
| 1482 // template urls we ship with. | 1497 // template urls we ship with. |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1714 } else { | 1729 } else { |
| 1715 // Change the local TURL's GUID to the server's GUID and push an update to | 1730 // Change the local TURL's GUID to the server's GUID and push an update to |
| 1716 // Sync. This ensures that the rest of local_url's fields are sync'd up to | 1731 // Sync. This ensures that the rest of local_url's fields are sync'd up to |
| 1717 // the server, and the next time local_url is synced, it is recognized by | 1732 // the server, and the next time local_url is synced, it is recognized by |
| 1718 // having the same GUID. | 1733 // having the same GUID. |
| 1719 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); | 1734 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); |
| 1720 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 1735 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 1721 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 1736 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
| 1722 } | 1737 } |
| 1723 } | 1738 } |
| OLD | NEW |