Chromium Code Reviews| 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 // We must always persist the setting in the database since we have to |
|
sky
2011/10/24 17:23:21
How about changing this comment to:
Always persist
whywhat
2011/10/25 10:30:41
Done.
| |
| 440 return; | 442 // sync backup and its signature with the setting. So the cached default |
| 443 // search engine provider could be equal to |url| but its backup and | |
| 444 // signature might be missing or corrupted. We don't need to send | |
| 445 // notifications out each time, however. | |
| 441 SetDefaultSearchProviderNoNotify(url); | 446 SetDefaultSearchProviderNoNotify(url); |
| 442 NotifyObservers(); | 447 if (default_search_provider_ != url) |
| 448 NotifyObservers(); | |
| 443 } | 449 } |
| 444 | 450 |
| 445 const TemplateURL* TemplateURLService::GetDefaultSearchProvider() { | 451 const TemplateURL* TemplateURLService::GetDefaultSearchProvider() { |
| 446 if (loaded_ && !load_failed_) | 452 if (loaded_ && !load_failed_) |
| 447 return default_search_provider_; | 453 return default_search_provider_; |
| 448 | 454 |
| 449 // We're not loaded, rely on the default search provider stored in prefs. | 455 // We're not loaded, rely on the default search provider stored in prefs. |
| 450 return initial_default_search_provider_.get(); | 456 return initial_default_search_provider_.get(); |
| 451 } | 457 } |
| 452 | 458 |
| 459 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { | |
| 460 // See if the prepoluated default still exists. | |
| 461 scoped_ptr<TemplateURL> prepopulated_default( | |
| 462 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs())); | |
| 463 for (TemplateURLVector::iterator i = template_urls_.begin(); | |
| 464 i != template_urls_.end(); ) { | |
| 465 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id()) | |
| 466 return *i; | |
| 467 } | |
| 468 // If not, use the first of the templates. | |
| 469 if (!template_urls_.empty()) { | |
|
sky
2011/10/24 17:04:13
change this to:
return template_urls_.empty() ? NU
whywhat
2011/10/25 10:30:41
Done.
| |
| 470 return template_urls_[0]; | |
| 471 } | |
| 472 return NULL; | |
| 473 } | |
| 474 | |
| 453 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { | 475 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { |
| 454 model_observers_.AddObserver(observer); | 476 model_observers_.AddObserver(observer); |
| 455 } | 477 } |
| 456 | 478 |
| 457 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { | 479 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { |
| 458 model_observers_.RemoveObserver(observer); | 480 model_observers_.RemoveObserver(observer); |
| 459 } | 481 } |
| 460 | 482 |
| 461 void TemplateURLService::Load() { | 483 void TemplateURLService::Load() { |
| 462 if (loaded_ || load_handle_) | 484 if (loaded_ || load_handle_) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 504 &default_search_provider, | 526 &default_search_provider, |
| 505 &new_resource_keyword_version); | 527 &new_resource_keyword_version); |
| 506 | 528 |
| 507 bool database_specified_a_default = NULL != default_search_provider; | 529 bool database_specified_a_default = NULL != default_search_provider; |
| 508 | 530 |
| 509 // Check if default search provider is now managed. | 531 // Check if default search provider is now managed. |
| 510 scoped_ptr<TemplateURL> default_from_prefs; | 532 scoped_ptr<TemplateURL> default_from_prefs; |
| 511 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, | 533 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, |
| 512 &is_default_search_managed_); | 534 &is_default_search_managed_); |
| 513 | 535 |
| 536 // Check if the default search provider has been changed and notify | |
| 537 // Protector instance about it. Don't check if the default search is | |
| 538 // managed. | |
| 539 const TemplateURL* backup_default_search_provider = NULL; | |
| 540 if (!is_default_search_managed_ && | |
| 541 DidDefaultSearchProviderChange( | |
| 542 *result, | |
| 543 template_urls, | |
| 544 &backup_default_search_provider)) { | |
| 545 // Protector will delete itself when it's needed no longer. | |
| 546 protector::Protector* protector = new protector::Protector(profile()); | |
| 547 protector->ShowChange(protector::CreateDefaultSearchProviderChange( | |
| 548 default_search_provider, | |
| 549 backup_default_search_provider)); | |
| 550 } | |
| 551 | |
| 514 // Remove entries that were created because of policy as they may have | 552 // Remove entries that were created because of policy as they may have |
| 515 // changed since the database was saved. | 553 // changed since the database was saved. |
| 516 RemoveProvidersCreatedByPolicy(&template_urls, | 554 RemoveProvidersCreatedByPolicy(&template_urls, |
| 517 &default_search_provider, | 555 &default_search_provider, |
| 518 default_from_prefs.get()); | 556 default_from_prefs.get()); |
| 519 | 557 |
| 520 if (is_default_search_managed_) { | 558 if (is_default_search_managed_) { |
| 521 SetTemplateURLs(template_urls); | 559 SetTemplateURLs(template_urls); |
| 522 | 560 |
| 523 if (TemplateURLsHaveSamePrefs(default_search_provider, | 561 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()) { | 1485 default_search_provider_->created_by_policy()) { |
| 1448 const TemplateURL* old_default = default_search_provider_; | 1486 const TemplateURL* old_default = default_search_provider_; |
| 1449 default_search_provider_ = NULL; | 1487 default_search_provider_ = NULL; |
| 1450 RemoveNoNotify(old_default); | 1488 RemoveNoNotify(old_default); |
| 1451 } | 1489 } |
| 1452 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); | 1490 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); |
| 1453 } | 1491 } |
| 1454 NotifyObservers(); | 1492 NotifyObservers(); |
| 1455 } | 1493 } |
| 1456 | 1494 |
| 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( | 1495 void TemplateURLService::SetDefaultSearchProviderNoNotify( |
| 1474 const TemplateURL* url) { | 1496 const TemplateURL* url) { |
| 1475 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != | 1497 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != |
| 1476 template_urls_.end()); | 1498 template_urls_.end()); |
| 1477 default_search_provider_ = url; | 1499 default_search_provider_ = url; |
| 1478 | 1500 |
| 1479 if (url) { | 1501 if (url) { |
| 1480 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); | 1502 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); |
| 1481 // Don't mark the url as edited, otherwise we won't be able to rev the | 1503 // Don't mark the url as edited, otherwise we won't be able to rev the |
| 1482 // template urls we ship with. | 1504 // template urls we ship with. |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1714 } else { | 1736 } else { |
| 1715 // Change the local TURL's GUID to the server's GUID and push an update to | 1737 // 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 | 1738 // 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 | 1739 // the server, and the next time local_url is synced, it is recognized by |
| 1718 // having the same GUID. | 1740 // having the same GUID. |
| 1719 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); | 1741 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); |
| 1720 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 1742 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 1721 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 1743 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
| 1722 } | 1744 } |
| 1723 } | 1745 } |
| OLD | NEW |