Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/browser/search_engines/template_url_service.cc

Issue 8334030: Merge search engines sync data type with Preferences. Sync the default search provider. Add some ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Merge to TOT and fixed additional conflicts from rsimha. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 : profile_(profile), 106 : profile_(profile),
107 loaded_(false), 107 loaded_(false),
108 load_failed_(false), 108 load_failed_(false),
109 load_handle_(0), 109 load_handle_(0),
110 default_search_provider_(NULL), 110 default_search_provider_(NULL),
111 is_default_search_managed_(false), 111 is_default_search_managed_(false),
112 next_id_(1), 112 next_id_(1),
113 time_provider_(&base::Time::Now), 113 time_provider_(&base::Time::Now),
114 models_associated_(false), 114 models_associated_(false),
115 processing_syncer_changes_(false), 115 processing_syncer_changes_(false),
116 sync_processor_(NULL) { 116 sync_processor_(NULL),
117 pending_synced_default_search_(false) {
117 DCHECK(profile_); 118 DCHECK(profile_);
118 Init(NULL, 0); 119 Init(NULL, 0);
119 } 120 }
120 121
121 TemplateURLService::TemplateURLService(const Initializer* initializers, 122 TemplateURLService::TemplateURLService(const Initializer* initializers,
122 const int count) 123 const int count)
123 : profile_(NULL), 124 : profile_(NULL),
124 loaded_(false), 125 loaded_(false),
125 load_failed_(false), 126 load_failed_(false),
126 load_handle_(0), 127 load_handle_(0),
127 service_(NULL), 128 service_(NULL),
128 default_search_provider_(NULL), 129 default_search_provider_(NULL),
129 is_default_search_managed_(false), 130 is_default_search_managed_(false),
130 next_id_(1), 131 next_id_(1),
131 time_provider_(&base::Time::Now), 132 time_provider_(&base::Time::Now),
132 models_associated_(false), 133 models_associated_(false),
133 processing_syncer_changes_(false), 134 processing_syncer_changes_(false),
134 sync_processor_(NULL) { 135 sync_processor_(NULL),
136 pending_synced_default_search_(false) {
135 Init(initializers, count); 137 Init(initializers, count);
136 } 138 }
137 139
138 TemplateURLService::~TemplateURLService() { 140 TemplateURLService::~TemplateURLService() {
139 if (load_handle_) { 141 if (load_handle_) {
140 DCHECK(service_.get()); 142 DCHECK(service_.get());
141 service_->CancelRequest(load_handle_); 143 service_->CancelRequest(load_handle_);
142 } 144 }
143 145
144 STLDeleteElements(&template_urls_); 146 STLDeleteElements(&template_urls_);
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 452
451 // We're not loaded, rely on the default search provider stored in prefs. 453 // We're not loaded, rely on the default search provider stored in prefs.
452 return initial_default_search_provider_.get(); 454 return initial_default_search_provider_.get();
453 } 455 }
454 456
455 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { 457 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() {
456 // See if the prepoluated default still exists. 458 // See if the prepoluated default still exists.
457 scoped_ptr<TemplateURL> prepopulated_default( 459 scoped_ptr<TemplateURL> prepopulated_default(
458 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs())); 460 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs()));
459 for (TemplateURLVector::iterator i = template_urls_.begin(); 461 for (TemplateURLVector::iterator i = template_urls_.begin();
460 i != template_urls_.end(); ) { 462 i != template_urls_.end(); ++i) {
461 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id()) 463 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id())
462 return *i; 464 return *i;
463 } 465 }
464 // If not, use the first of the templates. 466 // If not, use the first of the templates.
465 return template_urls_.empty() ? NULL : template_urls_[0]; 467 return template_urls_.empty() ? NULL : template_urls_[0];
466 } 468 }
467 469
468 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { 470 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) {
469 model_observers_.AddObserver(observer); 471 model_observers_.AddObserver(observer);
470 } 472 }
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 if (managed_default) { 574 if (managed_default) {
573 managed_default->set_created_by_policy(true); 575 managed_default->set_created_by_policy(true);
574 managed_default->set_id(0); 576 managed_default->set_id(0);
575 AddNoNotify(managed_default); 577 AddNoNotify(managed_default);
576 } 578 }
577 default_search_provider = managed_default; 579 default_search_provider = managed_default;
578 } 580 }
579 // Note that this saves the default search provider to prefs. 581 // Note that this saves the default search provider to prefs.
580 SetDefaultSearchProviderNoNotify(default_search_provider); 582 SetDefaultSearchProviderNoNotify(default_search_provider);
581 } else { 583 } else {
582 // If we had a managed default, replace it with the first provider of 584 // If we had a managed default, replace it with the synced default if
583 // the list. 585 // applicable, or the first provider of the list.
584 if (database_specified_a_default && 586 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider();
585 NULL == default_search_provider && 587 if (synced_default) {
586 !template_urls.empty()) 588 default_search_provider = synced_default;
589 pending_synced_default_search_ = false;
590 } else if (database_specified_a_default &&
591 NULL == default_search_provider &&
592 !template_urls.empty()) {
587 default_search_provider = template_urls[0]; 593 default_search_provider = template_urls[0];
594 }
588 595
589 // If the default search provider existed previously, then just 596 // If the default search provider existed previously, then just
590 // set the member variable. Otherwise, we'll set it using the method 597 // set the member variable. Otherwise, we'll set it using the method
591 // to ensure that it is saved properly after its id is set. 598 // to ensure that it is saved properly after its id is set.
592 if (default_search_provider && default_search_provider->id() != 0) { 599 if (default_search_provider && default_search_provider->id() != 0) {
593 default_search_provider_ = default_search_provider; 600 default_search_provider_ = default_search_provider;
594 default_search_provider = NULL; 601 default_search_provider = NULL;
595 } 602 }
596 SetTemplateURLs(template_urls); 603 SetTemplateURLs(template_urls);
597 604
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 if (!loaded()) 651 if (!loaded())
645 visits_to_add_.push_back(*visit_details.ptr()); 652 visits_to_add_.push_back(*visit_details.ptr());
646 else 653 else
647 UpdateKeywordSearchTermsForURL(*visit_details.ptr()); 654 UpdateKeywordSearchTermsForURL(*visit_details.ptr());
648 } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) { 655 } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) {
649 if (loaded_) 656 if (loaded_)
650 GoogleBaseURLChanged(); 657 GoogleBaseURLChanged();
651 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) { 658 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) {
652 const std::string* pref_name = content::Details<std::string>(details).ptr(); 659 const std::string* pref_name = content::Details<std::string>(details).ptr();
653 if (!pref_name || default_search_prefs_->IsObserved(*pref_name)) { 660 if (!pref_name || default_search_prefs_->IsObserved(*pref_name)) {
661 // Listen for changes to the default search from Sync. If it is
662 // specifically the synced default search provider GUID that changed, we
663 // have to set it (or wait for it).
664 PrefService* prefs = GetPrefs();
665 if (pref_name && *pref_name == prefs::kSyncedDefaultSearchProviderGUID &&
666 prefs) {
667 const TemplateURL* new_default_search = GetTemplateURLForGUID(
668 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
669 if (new_default_search && !is_default_search_managed_) {
670 if (new_default_search != GetDefaultSearchProvider()) {
671 SetDefaultSearchProvider(new_default_search);
672 pending_synced_default_search_ = false;
673 }
674 } else {
675 // If it's not there, or if default search is currently managed, set a
676 // flag to indicate that we waiting on the search engine entry to come
677 // in through Sync.
678 pending_synced_default_search_ = true;
679 }
680 }
681
654 // A preference related to default search engine has changed. 682 // A preference related to default search engine has changed.
655 // Update the model if needed. 683 // Update the model if needed.
656 UpdateDefaultSearch(); 684 UpdateDefaultSearch();
657 } 685 }
658 } else { 686 } else {
659 NOTREACHED(); 687 NOTREACHED();
660 } 688 }
661 } 689 }
662 690
663 SyncDataList TemplateURLService::GetAllSyncData( 691 SyncDataList TemplateURLService::GetAllSyncData(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 if (!turl.get()) { 729 if (!turl.get()) {
702 NOTREACHED() << "Failed to read search engine."; 730 NOTREACHED() << "Failed to read search engine.";
703 continue; 731 continue;
704 } 732 }
705 733
706 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid()); 734 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid());
707 const TemplateURL* existing_keyword_turl = 735 const TemplateURL* existing_keyword_turl =
708 GetTemplateURLForKeyword(turl->keyword()); 736 GetTemplateURLForKeyword(turl->keyword());
709 737
710 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { 738 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) {
711 Remove(existing_turl); 739 bool delete_default = (existing_turl == GetDefaultSearchProvider());
740
741 if (delete_default && is_default_search_managed_) {
742 NOTREACHED() << "Tried to delete managed default search provider";
743 } else {
744 if (delete_default)
745 default_search_provider_ = NULL;
746
747 Remove(existing_turl);
748
749 if (delete_default)
750 SetDefaultSearchProvider(FindNewDefaultSearchProvider());
751 }
712 } else if (iter->change_type() == SyncChange::ACTION_ADD && 752 } else if (iter->change_type() == SyncChange::ACTION_ADD &&
713 !existing_turl) { 753 !existing_turl) {
754 std::string guid = turl->sync_guid();
714 if (existing_keyword_turl) 755 if (existing_keyword_turl)
715 ResolveSyncKeywordConflict(turl.get(), &new_changes); 756 ResolveSyncKeywordConflict(turl.get(), &new_changes);
716 // Force the local ID to 0 so we can add it. 757 // Force the local ID to 0 so we can add it.
717 turl->set_id(0); 758 turl->set_id(0);
718 Add(turl.release()); 759 Add(turl.release());
760
761 // Possibly set the newly added |turl| as the default search provider.
762 SetDefaultSearchProviderIfNewlySynced(guid);
719 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && 763 } else if (iter->change_type() == SyncChange::ACTION_UPDATE &&
720 existing_turl) { 764 existing_turl) {
721 if (existing_keyword_turl) 765 // Possibly resolve a keyword conflict if they have the same keywords but
766 // are not the same entry.
767 if (existing_keyword_turl && existing_keyword_turl != existing_turl)
722 ResolveSyncKeywordConflict(turl.get(), &new_changes); 768 ResolveSyncKeywordConflict(turl.get(), &new_changes);
723 ResetTemplateURL(existing_turl, turl->short_name(), turl->keyword(), 769 ResetTemplateURL(existing_turl, turl->short_name(), turl->keyword(),
724 turl->url() ? turl->url()->url() : std::string()); 770 turl->url() ? turl->url()->url() : std::string());
725 } else { 771 } else {
726 // Something really unexpected happened. Either we received an 772 // Something really unexpected happened. Either we received an
727 // ACTION_INVALID, or Sync is in a crazy state: 773 // ACTION_INVALID, or Sync is in a crazy state:
728 // . Trying to DELETE or UPDATE a non-existent search engine. 774 // . Trying to DELETE or UPDATE a non-existent search engine.
729 // . Trying to ADD a search engine that already exists. 775 // . Trying to ADD a search engine that already exists.
730 NOTREACHED() << "Unexpected sync change state."; 776 NOTREACHED() << "Unexpected sync change state.";
731 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " + 777 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " +
(...skipping 14 matching lines...) Expand all
746 792
747 SyncError TemplateURLService::MergeDataAndStartSyncing( 793 SyncError TemplateURLService::MergeDataAndStartSyncing(
748 syncable::ModelType type, 794 syncable::ModelType type,
749 const SyncDataList& initial_sync_data, 795 const SyncDataList& initial_sync_data,
750 SyncChangeProcessor* sync_processor) { 796 SyncChangeProcessor* sync_processor) {
751 DCHECK(loaded()); 797 DCHECK(loaded());
752 DCHECK_EQ(type, syncable::SEARCH_ENGINES); 798 DCHECK_EQ(type, syncable::SEARCH_ENGINES);
753 DCHECK(!sync_processor_); 799 DCHECK(!sync_processor_);
754 sync_processor_ = sync_processor; 800 sync_processor_ = sync_processor;
755 801
802 // We just started syncing, so set our wait-for-default flag if we are
803 // expecting a default from Sync.
804 if (GetPrefs()) {
805 std::string default_guid = GetPrefs()->GetString(
806 prefs::kSyncedDefaultSearchProviderGUID);
807 const TemplateURL* current_default = GetDefaultSearchProvider();
808
809 if (!default_guid.empty() &&
810 (!current_default || current_default->sync_guid() != default_guid))
811 pending_synced_default_search_ = true;
812 }
813
756 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we 814 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we
757 // don't step on our own toes. 815 // don't step on our own toes.
758 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); 816 AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
759 817
760 SyncChangeList new_changes; 818 SyncChangeList new_changes;
761 819
762 // Build maps of our sync GUIDs to SyncData. 820 // Build maps of our sync GUIDs to SyncData.
763 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( 821 SyncDataMap local_data_map = CreateGUIDToSyncDataMap(
764 GetAllSyncData(syncable::SEARCH_ENGINES)); 822 GetAllSyncData(syncable::SEARCH_ENGINES));
765 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); 823 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 if (dupe_turl) { 863 if (dupe_turl) {
806 // Merge duplicates and remove the processed local TURL from the map. 864 // Merge duplicates and remove the processed local TURL from the map.
807 TemplateURL* modifiable_dupe_turl = 865 TemplateURL* modifiable_dupe_turl =
808 const_cast<TemplateURL*>(dupe_turl); 866 const_cast<TemplateURL*>(dupe_turl);
809 std::string old_guid = dupe_turl->sync_guid(); 867 std::string old_guid = dupe_turl->sync_guid();
810 MergeSyncAndLocalURLDuplicates(sync_turl.release(), 868 MergeSyncAndLocalURLDuplicates(sync_turl.release(),
811 modifiable_dupe_turl, 869 modifiable_dupe_turl,
812 &new_changes); 870 &new_changes);
813 local_data_map.erase(old_guid); 871 local_data_map.erase(old_guid);
814 } else { 872 } else {
873 std::string guid = sync_turl->sync_guid();
815 // Keyword conflict is possible in this case. Resolve it first before 874 // Keyword conflict is possible in this case. Resolve it first before
816 // adding the new TemplateURL. Note that we don't remove the local TURL 875 // adding the new TemplateURL. Note that we don't remove the local TURL
817 // from local_data_map in this case as it may still need to be pushed to 876 // from local_data_map in this case as it may still need to be pushed to
818 // the cloud. 877 // the cloud.
819 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); 878 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes);
820 // Force the local ID to 0 so we can add it. 879 // Force the local ID to 0 so we can add it.
821 sync_turl->set_id(0); 880 sync_turl->set_id(0);
822 Add(sync_turl.release()); 881 Add(sync_turl.release());
882
883 // Possibly set the newly added |turl| as the default search provider.
884 SetDefaultSearchProviderIfNewlySynced(guid);
823 } 885 }
824 } 886 }
825 } // for 887 } // for
826 888
827 // The remaining SyncData in local_data_map should be everything that needs to 889 // The remaining SyncData in local_data_map should be everything that needs to
828 // be pushed as ADDs to sync. 890 // be pushed as ADDs to sync.
829 for (SyncDataMap::const_iterator iter = local_data_map.begin(); 891 for (SyncDataMap::const_iterator iter = local_data_map.begin();
830 iter != local_data_map.end(); ++iter) { 892 iter != local_data_map.end(); ++iter) {
831 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second)); 893 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second));
832 } 894 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 const GURL& url, 1007 const GURL& url,
946 const string16& term) { 1008 const string16& term) {
947 HistoryService* history = profile_ ? 1009 HistoryService* history = profile_ ?
948 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL; 1010 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
949 if (!history) 1011 if (!history)
950 return; 1012 return;
951 history->SetKeywordSearchTermsForURL(url, t_url->id(), term); 1013 history->SetKeywordSearchTermsForURL(url, t_url->id(), term);
952 } 1014 }
953 1015
954 void TemplateURLService::Init(const Initializer* initializers, 1016 void TemplateURLService::Init(const Initializer* initializers,
955 int num_initializers) { 1017 int num_initializers) {
956 // Register for notifications. 1018 // Register for notifications.
957 if (profile_) { 1019 if (profile_) {
958 // TODO(sky): bug 1166191. The keywords should be moved into the history 1020 // TODO(sky): bug 1166191. The keywords should be moved into the history
959 // db, which will mean we no longer need this notification and the history 1021 // db, which will mean we no longer need this notification and the history
960 // backend can handle automatically adding the search terms as the user 1022 // backend can handle automatically adding the search terms as the user
961 // navigates. 1023 // navigates.
962 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, 1024 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED,
963 content::Source<Profile>(profile_->GetOriginalProfile())); 1025 content::Source<Profile>(profile_->GetOriginalProfile()));
964 PrefService* prefs = GetPrefs(); 1026 PrefService* prefs = GetPrefs();
965 default_search_prefs_.reset( 1027 default_search_prefs_.reset(
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
1481 DCHECK(is_default_search_managed_ && !new_is_default_managed); 1543 DCHECK(is_default_search_managed_ && !new_is_default_managed);
1482 is_default_search_managed_ = new_is_default_managed; 1544 is_default_search_managed_ = new_is_default_managed;
1483 // If we had a default, delete the previous default if created by policy 1545 // If we had a default, delete the previous default if created by policy
1484 // and set a likely default. 1546 // and set a likely default.
1485 if (NULL != default_search_provider_ && 1547 if (NULL != default_search_provider_ &&
1486 default_search_provider_->created_by_policy()) { 1548 default_search_provider_->created_by_policy()) {
1487 const TemplateURL* old_default = default_search_provider_; 1549 const TemplateURL* old_default = default_search_provider_;
1488 default_search_provider_ = NULL; 1550 default_search_provider_ = NULL;
1489 RemoveNoNotify(old_default); 1551 RemoveNoNotify(old_default);
1490 } 1552 }
1491 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); 1553
1554 // The likely default should be from Sync if we were waiting on Sync.
1555 // Otherwise, it should be FindNewDefaultSearchProvider.
1556 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider();
1557 if (synced_default)
1558 pending_synced_default_search_ = false;
1559 SetDefaultSearchProviderNoNotify(synced_default ? synced_default :
1560 FindNewDefaultSearchProvider());
1492 } 1561 }
1493 NotifyObservers(); 1562 NotifyObservers();
1494 } 1563 }
1495 1564
1496 void TemplateURLService::SetDefaultSearchProviderNoNotify( 1565 void TemplateURLService::SetDefaultSearchProviderNoNotify(
1497 const TemplateURL* url) { 1566 const TemplateURL* url) {
1498 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != 1567 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) !=
1499 template_urls_.end()); 1568 template_urls_.end());
1500 default_search_provider_ = url; 1569 default_search_provider_ = url;
1501 1570
(...skipping 11 matching lines...) Expand all
1513 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) 1582 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
1514 // Needs to be evaluated. See http://crbug.com/62328. 1583 // Needs to be evaluated. See http://crbug.com/62328.
1515 base::ThreadRestrictions::ScopedAllowIO allow_io; 1584 base::ThreadRestrictions::ScopedAllowIO allow_io;
1516 RLZTracker::RecordProductEvent(rlz_lib::CHROME, 1585 RLZTracker::RecordProductEvent(rlz_lib::CHROME,
1517 rlz_lib::CHROME_OMNIBOX, 1586 rlz_lib::CHROME_OMNIBOX,
1518 rlz_lib::SET_TO_GOOGLE); 1587 rlz_lib::SET_TO_GOOGLE);
1519 #endif 1588 #endif
1520 } 1589 }
1521 } 1590 }
1522 1591
1523 if (!is_default_search_managed_) 1592 if (!is_default_search_managed_) {
1524 SaveDefaultSearchProviderToPrefs(url); 1593 SaveDefaultSearchProviderToPrefs(url);
1525 1594
1595 // If we are syncing, we want to set the synced pref that will notify other
1596 // instances to change their default to this new search provider.
1597 if (sync_processor_ && !url->sync_guid().empty() && GetPrefs()) {
1598 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID,
1599 url->sync_guid());
1600 }
1601 }
1602
1526 if (service_.get()) 1603 if (service_.get())
1527 service_->SetDefaultSearchProvider(url); 1604 service_->SetDefaultSearchProvider(url);
1528 1605
1529 // Inform sync the change to the show_in_default_list flag. 1606 // Inform sync the change to the show_in_default_list flag.
1530 if (url) 1607 if (url)
1531 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); 1608 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE);
1532 } 1609 }
1533 1610
1534 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { 1611 void TemplateURLService::AddNoNotify(TemplateURL* template_url) {
1535 DCHECK(template_url); 1612 DCHECK(template_url);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1674 } 1751 }
1675 1752
1676 bool TemplateURLService::ResolveSyncKeywordConflict( 1753 bool TemplateURLService::ResolveSyncKeywordConflict(
1677 TemplateURL* sync_turl, 1754 TemplateURL* sync_turl,
1678 SyncChangeList* change_list) { 1755 SyncChangeList* change_list) {
1679 DCHECK(sync_turl); 1756 DCHECK(sync_turl);
1680 DCHECK(change_list); 1757 DCHECK(change_list);
1681 1758
1682 const TemplateURL* existing_turl = 1759 const TemplateURL* existing_turl =
1683 GetTemplateURLForKeyword(sync_turl->keyword()); 1760 GetTemplateURLForKeyword(sync_turl->keyword());
1684 if (!existing_turl) 1761 // If there is no conflict, or it's just conflicting with itself, return.
1762 if (!existing_turl || existing_turl->sync_guid() == sync_turl->sync_guid())
1685 return false; 1763 return false;
1686 1764
1687 if (existing_turl->last_modified() > sync_turl->last_modified() || 1765 if (existing_turl->last_modified() > sync_turl->last_modified() ||
1688 existing_turl->created_by_policy()) { 1766 existing_turl->created_by_policy()) {
1689 string16 new_keyword = UniquifyKeyword(*sync_turl); 1767 string16 new_keyword = UniquifyKeyword(*sync_turl);
1690 DCHECK(!GetTemplateURLForKeyword(new_keyword)); 1768 DCHECK(!GetTemplateURLForKeyword(new_keyword));
1691 sync_turl->set_keyword(new_keyword); 1769 sync_turl->set_keyword(new_keyword);
1692 // If we update the cloud TURL, we need to push an update back to sync 1770 // If we update the cloud TURL, we need to push an update back to sync
1693 // informing it that something has changed. 1771 // informing it that something has changed.
1694 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); 1772 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1738 // Change the local TURL's GUID to the server's GUID and push an update to 1816 // Change the local TURL's GUID to the server's GUID and push an update to
1739 // Sync. This ensures that the rest of local_url's fields are sync'd up to 1817 // Sync. This ensures that the rest of local_url's fields are sync'd up to
1740 // the server, and the next time local_url is synced, it is recognized by 1818 // the server, and the next time local_url is synced, it is recognized by
1741 // having the same GUID. 1819 // having the same GUID.
1742 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); 1820 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid());
1743 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 1821 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
1744 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); 1822 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data));
1745 } 1823 }
1746 } 1824 }
1747 1825
1826 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced(
1827 const std::string& guid) {
1828 // If we're not syncing or if default search is managed by policy, ignore.
1829 if (!sync_processor_ || is_default_search_managed_)
1830 return;
1831
1832 PrefService* prefs = GetPrefs();
1833 if (prefs && pending_synced_default_search_ &&
1834 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID) == guid) {
1835 // Make sure this actually exists. We should not be calling this unless we
1836 // really just added this TemplateURL.
1837 const TemplateURL* turl_from_sync = GetTemplateURLForGUID(guid);
1838 DCHECK(turl_from_sync);
1839 SetDefaultSearchProvider(turl_from_sync);
1840 pending_synced_default_search_ = false;
1841 }
1842 }
1843
1844 const TemplateURL* TemplateURLService::GetPendingSyncedDefaultSearchProvider() {
1845 PrefService* prefs = GetPrefs();
1846 if (!prefs || !pending_synced_default_search_)
1847 return NULL;
1848
1849 // Could be NULL if no such thing exists.
1850 return GetTemplateURLForGUID(
1851 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
1852 }
1853
1748 void TemplateURLService::PatchMissingSyncGUIDs( 1854 void TemplateURLService::PatchMissingSyncGUIDs(
1749 std::vector<TemplateURL*>* template_urls) { 1855 std::vector<TemplateURL*>* template_urls) {
1750 DCHECK(template_urls); 1856 DCHECK(template_urls);
1751 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); 1857 for (std::vector<TemplateURL*>::iterator i = template_urls->begin();
1752 i != template_urls->end(); ++i) { 1858 i != template_urls->end(); ++i) {
1753 TemplateURL* template_url = *i; 1859 TemplateURL* template_url = *i;
1754 DCHECK(template_url); 1860 DCHECK(template_url);
1755 if (template_url->sync_guid().empty()) { 1861 if (template_url->sync_guid().empty()) {
1756 template_url->set_sync_guid(guid::GenerateGUID()); 1862 template_url->set_sync_guid(guid::GenerateGUID());
1757 if (service_.get()) 1863 if (service_.get())
1758 service_->UpdateKeyword(*template_url); 1864 service_->UpdateKeyword(*template_url);
1759 } 1865 }
1760 } 1866 }
1761 } 1867 }
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/template_url_service.h ('k') | chrome/browser/search_engines/template_url_service_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698