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

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: '' 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 if (managed_default) { 572 if (managed_default) {
571 managed_default->set_created_by_policy(true); 573 managed_default->set_created_by_policy(true);
572 managed_default->set_id(0); 574 managed_default->set_id(0);
573 AddNoNotify(managed_default); 575 AddNoNotify(managed_default);
574 } 576 }
575 default_search_provider = managed_default; 577 default_search_provider = managed_default;
576 } 578 }
577 // Note that this saves the default search provider to prefs. 579 // Note that this saves the default search provider to prefs.
578 SetDefaultSearchProviderNoNotify(default_search_provider); 580 SetDefaultSearchProviderNoNotify(default_search_provider);
579 } else { 581 } else {
580 // If we had a managed default, replace it with the first provider of 582 // If we had a managed default, replace it with the synced default if
581 // the list. 583 // applicable, or the first provider of the list.
582 if (database_specified_a_default && 584 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider();
583 NULL == default_search_provider && 585 if (synced_default) {
584 !template_urls.empty()) 586 default_search_provider = synced_default;
587 pending_synced_default_search_ = false;
588 } else if (database_specified_a_default &&
589 NULL == default_search_provider &&
590 !template_urls.empty()) {
585 default_search_provider = template_urls[0]; 591 default_search_provider = template_urls[0];
592 }
586 593
587 // If the default search provider existed previously, then just 594 // If the default search provider existed previously, then just
588 // set the member variable. Otherwise, we'll set it using the method 595 // set the member variable. Otherwise, we'll set it using the method
589 // to ensure that it is saved properly after its id is set. 596 // to ensure that it is saved properly after its id is set.
590 if (default_search_provider && default_search_provider->id() != 0) { 597 if (default_search_provider && default_search_provider->id() != 0) {
591 default_search_provider_ = default_search_provider; 598 default_search_provider_ = default_search_provider;
592 default_search_provider = NULL; 599 default_search_provider = NULL;
593 } 600 }
594 SetTemplateURLs(template_urls); 601 SetTemplateURLs(template_urls);
595 602
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 if (!loaded()) 649 if (!loaded())
643 visits_to_add_.push_back(*visit_details.ptr()); 650 visits_to_add_.push_back(*visit_details.ptr());
644 else 651 else
645 UpdateKeywordSearchTermsForURL(*visit_details.ptr()); 652 UpdateKeywordSearchTermsForURL(*visit_details.ptr());
646 } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) { 653 } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) {
647 if (loaded_) 654 if (loaded_)
648 GoogleBaseURLChanged(); 655 GoogleBaseURLChanged();
649 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) { 656 } else if (type == chrome::NOTIFICATION_PREF_CHANGED) {
650 const std::string* pref_name = content::Details<std::string>(details).ptr(); 657 const std::string* pref_name = content::Details<std::string>(details).ptr();
651 if (!pref_name || default_search_prefs_->IsObserved(*pref_name)) { 658 if (!pref_name || default_search_prefs_->IsObserved(*pref_name)) {
659 // Listen for changes to the default search from Sync. If it is
660 // specifically the synced default search provider GUID that changed, we
661 // have to set it (or wait for it).
662 PrefService* prefs = GetPrefs();
663 if (pref_name && *pref_name == prefs::kSyncedDefaultSearchProviderGUID &&
664 prefs) {
665 const TemplateURL* new_default_search = GetTemplateURLForGUID(
666 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
667 if (new_default_search && !is_default_search_managed_) {
668 if (new_default_search != GetDefaultSearchProvider()) {
669 SetDefaultSearchProvider(new_default_search);
670 pending_synced_default_search_ = false;
671 }
672 } else {
673 // If it's not there, or if default search is currently managed, set a
674 // flag to indicate that we waiting on the search engine entry to come
675 // in through Sync.
676 pending_synced_default_search_ = true;
677 }
678 }
679
652 // A preference related to default search engine has changed. 680 // A preference related to default search engine has changed.
653 // Update the model if needed. 681 // Update the model if needed.
654 UpdateDefaultSearch(); 682 UpdateDefaultSearch();
655 } 683 }
656 } else { 684 } else {
657 NOTREACHED(); 685 NOTREACHED();
658 } 686 }
659 } 687 }
660 688
661 SyncDataList TemplateURLService::GetAllSyncData( 689 SyncDataList TemplateURLService::GetAllSyncData(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 if (!turl.get()) { 727 if (!turl.get()) {
700 NOTREACHED() << "Failed to read search engine."; 728 NOTREACHED() << "Failed to read search engine.";
701 continue; 729 continue;
702 } 730 }
703 731
704 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid()); 732 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid());
705 const TemplateURL* existing_keyword_turl = 733 const TemplateURL* existing_keyword_turl =
706 GetTemplateURLForKeyword(turl->keyword()); 734 GetTemplateURLForKeyword(turl->keyword());
707 735
708 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { 736 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) {
709 Remove(existing_turl); 737 bool delete_default = (existing_turl == GetDefaultSearchProvider());
738
739 if (delete_default && is_default_search_managed_) {
740 NOTREACHED() << "Tried to delete managed default search provider";
741 } else {
742 if (delete_default)
743 default_search_provider_ = NULL;
744
745 Remove(existing_turl);
746
747 if (delete_default)
748 SetDefaultSearchProvider(FindNewDefaultSearchProvider());
749 }
710 } else if (iter->change_type() == SyncChange::ACTION_ADD && 750 } else if (iter->change_type() == SyncChange::ACTION_ADD &&
711 !existing_turl) { 751 !existing_turl) {
752 std::string guid = turl->sync_guid();
712 if (existing_keyword_turl) 753 if (existing_keyword_turl)
713 ResolveSyncKeywordConflict(turl.get(), &new_changes); 754 ResolveSyncKeywordConflict(turl.get(), &new_changes);
714 // Force the local ID to 0 so we can add it. 755 // Force the local ID to 0 so we can add it.
715 turl->set_id(0); 756 turl->set_id(0);
716 Add(turl.release()); 757 Add(turl.release());
758
759 // Possibly set the newly added |turl| as the default search provider.
760 SetDefaultSearchProviderIfNewlySynced(guid);
717 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && 761 } else if (iter->change_type() == SyncChange::ACTION_UPDATE &&
718 existing_turl) { 762 existing_turl) {
719 if (existing_keyword_turl) 763 // Possibly resolve a keyword conflict if they have the same keywords but
764 // are not the same entry.
765 if (existing_keyword_turl && existing_keyword_turl != existing_turl)
720 ResolveSyncKeywordConflict(turl.get(), &new_changes); 766 ResolveSyncKeywordConflict(turl.get(), &new_changes);
721 ResetTemplateURL(existing_turl, turl->short_name(), turl->keyword(), 767 ResetTemplateURL(existing_turl, turl->short_name(), turl->keyword(),
722 turl->url() ? turl->url()->url() : std::string()); 768 turl->url() ? turl->url()->url() : std::string());
723 } else { 769 } else {
724 // Something really unexpected happened. Either we received an 770 // Something really unexpected happened. Either we received an
725 // ACTION_INVALID, or Sync is in a crazy state: 771 // ACTION_INVALID, or Sync is in a crazy state:
726 // . Trying to DELETE or UPDATE a non-existent search engine. 772 // . Trying to DELETE or UPDATE a non-existent search engine.
727 // . Trying to ADD a search engine that already exists. 773 // . Trying to ADD a search engine that already exists.
728 NOTREACHED() << "Unexpected sync change state."; 774 NOTREACHED() << "Unexpected sync change state.";
729 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " + 775 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " +
(...skipping 14 matching lines...) Expand all
744 790
745 SyncError TemplateURLService::MergeDataAndStartSyncing( 791 SyncError TemplateURLService::MergeDataAndStartSyncing(
746 syncable::ModelType type, 792 syncable::ModelType type,
747 const SyncDataList& initial_sync_data, 793 const SyncDataList& initial_sync_data,
748 SyncChangeProcessor* sync_processor) { 794 SyncChangeProcessor* sync_processor) {
749 DCHECK(loaded()); 795 DCHECK(loaded());
750 DCHECK_EQ(type, syncable::SEARCH_ENGINES); 796 DCHECK_EQ(type, syncable::SEARCH_ENGINES);
751 DCHECK(!sync_processor_); 797 DCHECK(!sync_processor_);
752 sync_processor_ = sync_processor; 798 sync_processor_ = sync_processor;
753 799
800 // We just started syncing, so set our wait-for-default flag if we are
801 // expecting a default from Sync.
802 if (GetPrefs()) {
803 std::string default_guid = GetPrefs()->GetString(
804 prefs::kSyncedDefaultSearchProviderGUID);
805 const TemplateURL* current_default = GetDefaultSearchProvider();
806
807 if (!default_guid.empty() &&
808 (!current_default || current_default->sync_guid() != default_guid))
809 pending_synced_default_search_ = true;
810 }
811
754 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we 812 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we
755 // don't step on our own toes. 813 // don't step on our own toes.
756 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); 814 AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
757 815
758 SyncChangeList new_changes; 816 SyncChangeList new_changes;
759 817
760 // Build maps of our sync GUIDs to SyncData. 818 // Build maps of our sync GUIDs to SyncData.
761 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( 819 SyncDataMap local_data_map = CreateGUIDToSyncDataMap(
762 GetAllSyncData(syncable::SEARCH_ENGINES)); 820 GetAllSyncData(syncable::SEARCH_ENGINES));
763 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); 821 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 if (dupe_turl) { 861 if (dupe_turl) {
804 // Merge duplicates and remove the processed local TURL from the map. 862 // Merge duplicates and remove the processed local TURL from the map.
805 TemplateURL* modifiable_dupe_turl = 863 TemplateURL* modifiable_dupe_turl =
806 const_cast<TemplateURL*>(dupe_turl); 864 const_cast<TemplateURL*>(dupe_turl);
807 std::string old_guid = dupe_turl->sync_guid(); 865 std::string old_guid = dupe_turl->sync_guid();
808 MergeSyncAndLocalURLDuplicates(sync_turl.release(), 866 MergeSyncAndLocalURLDuplicates(sync_turl.release(),
809 modifiable_dupe_turl, 867 modifiable_dupe_turl,
810 &new_changes); 868 &new_changes);
811 local_data_map.erase(old_guid); 869 local_data_map.erase(old_guid);
812 } else { 870 } else {
871 std::string guid = sync_turl->sync_guid();
813 // Keyword conflict is possible in this case. Resolve it first before 872 // Keyword conflict is possible in this case. Resolve it first before
814 // adding the new TemplateURL. Note that we don't remove the local TURL 873 // adding the new TemplateURL. Note that we don't remove the local TURL
815 // from local_data_map in this case as it may still need to be pushed to 874 // from local_data_map in this case as it may still need to be pushed to
816 // the cloud. 875 // the cloud.
817 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); 876 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes);
818 // Force the local ID to 0 so we can add it. 877 // Force the local ID to 0 so we can add it.
819 sync_turl->set_id(0); 878 sync_turl->set_id(0);
820 Add(sync_turl.release()); 879 Add(sync_turl.release());
880
881 // Possibly set the newly added |turl| as the default search provider.
882 SetDefaultSearchProviderIfNewlySynced(guid);
821 } 883 }
822 } 884 }
823 } // for 885 } // for
824 886
825 // The remaining SyncData in local_data_map should be everything that needs to 887 // The remaining SyncData in local_data_map should be everything that needs to
826 // be pushed as ADDs to sync. 888 // be pushed as ADDs to sync.
827 for (SyncDataMap::const_iterator iter = local_data_map.begin(); 889 for (SyncDataMap::const_iterator iter = local_data_map.begin();
828 iter != local_data_map.end(); ++iter) { 890 iter != local_data_map.end(); ++iter) {
829 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second)); 891 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second));
830 } 892 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 const GURL& url, 1005 const GURL& url,
944 const string16& term) { 1006 const string16& term) {
945 HistoryService* history = profile_ ? 1007 HistoryService* history = profile_ ?
946 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL; 1008 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
947 if (!history) 1009 if (!history)
948 return; 1010 return;
949 history->SetKeywordSearchTermsForURL(url, t_url->id(), term); 1011 history->SetKeywordSearchTermsForURL(url, t_url->id(), term);
950 } 1012 }
951 1013
952 void TemplateURLService::Init(const Initializer* initializers, 1014 void TemplateURLService::Init(const Initializer* initializers,
953 int num_initializers) { 1015 int num_initializers) {
954 // Register for notifications. 1016 // Register for notifications.
955 if (profile_) { 1017 if (profile_) {
956 // TODO(sky): bug 1166191. The keywords should be moved into the history 1018 // TODO(sky): bug 1166191. The keywords should be moved into the history
957 // db, which will mean we no longer need this notification and the history 1019 // db, which will mean we no longer need this notification and the history
958 // backend can handle automatically adding the search terms as the user 1020 // backend can handle automatically adding the search terms as the user
959 // navigates. 1021 // navigates.
960 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, 1022 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED,
961 content::Source<Profile>(profile_->GetOriginalProfile())); 1023 content::Source<Profile>(profile_->GetOriginalProfile()));
962 PrefService* prefs = GetPrefs(); 1024 PrefService* prefs = GetPrefs();
963 default_search_prefs_.reset( 1025 default_search_prefs_.reset(
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
1479 DCHECK(is_default_search_managed_ && !new_is_default_managed); 1541 DCHECK(is_default_search_managed_ && !new_is_default_managed);
1480 is_default_search_managed_ = new_is_default_managed; 1542 is_default_search_managed_ = new_is_default_managed;
1481 // If we had a default, delete the previous default if created by policy 1543 // If we had a default, delete the previous default if created by policy
1482 // and set a likely default. 1544 // and set a likely default.
1483 if (NULL != default_search_provider_ && 1545 if (NULL != default_search_provider_ &&
1484 default_search_provider_->created_by_policy()) { 1546 default_search_provider_->created_by_policy()) {
1485 const TemplateURL* old_default = default_search_provider_; 1547 const TemplateURL* old_default = default_search_provider_;
1486 default_search_provider_ = NULL; 1548 default_search_provider_ = NULL;
1487 RemoveNoNotify(old_default); 1549 RemoveNoNotify(old_default);
1488 } 1550 }
1489 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); 1551
1552 // The likely default should be from Sync if we were waiting on Sync.
1553 // Otherwise, it should be FindNewDefaultSearchProvider.
1554 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider();
1555 if (synced_default)
1556 pending_synced_default_search_ = false;
1557 SetDefaultSearchProviderNoNotify(synced_default ? synced_default :
1558 FindNewDefaultSearchProvider());
1490 } 1559 }
1491 NotifyObservers(); 1560 NotifyObservers();
1492 } 1561 }
1493 1562
1494 void TemplateURLService::SetDefaultSearchProviderNoNotify( 1563 void TemplateURLService::SetDefaultSearchProviderNoNotify(
1495 const TemplateURL* url) { 1564 const TemplateURL* url) {
1496 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != 1565 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) !=
1497 template_urls_.end()); 1566 template_urls_.end());
1498 default_search_provider_ = url; 1567 default_search_provider_ = url;
1499 1568
(...skipping 11 matching lines...) Expand all
1511 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) 1580 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
1512 // Needs to be evaluated. See http://crbug.com/62328. 1581 // Needs to be evaluated. See http://crbug.com/62328.
1513 base::ThreadRestrictions::ScopedAllowIO allow_io; 1582 base::ThreadRestrictions::ScopedAllowIO allow_io;
1514 RLZTracker::RecordProductEvent(rlz_lib::CHROME, 1583 RLZTracker::RecordProductEvent(rlz_lib::CHROME,
1515 rlz_lib::CHROME_OMNIBOX, 1584 rlz_lib::CHROME_OMNIBOX,
1516 rlz_lib::SET_TO_GOOGLE); 1585 rlz_lib::SET_TO_GOOGLE);
1517 #endif 1586 #endif
1518 } 1587 }
1519 } 1588 }
1520 1589
1521 if (!is_default_search_managed_) 1590 if (!is_default_search_managed_) {
1522 SaveDefaultSearchProviderToPrefs(url); 1591 SaveDefaultSearchProviderToPrefs(url);
1523 1592
1593 // If we are syncing, we want to set the synced pref that will notify other
1594 // instances to change their default to this new search provider.
1595 if (sync_processor_ && !url->sync_guid().empty() && GetPrefs()) {
1596 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID,
1597 url->sync_guid());
1598 }
1599 }
1600
1524 if (service_.get()) 1601 if (service_.get())
1525 service_->SetDefaultSearchProvider(url); 1602 service_->SetDefaultSearchProvider(url);
1526 1603
1527 // Inform sync the change to the show_in_default_list flag. 1604 // Inform sync the change to the show_in_default_list flag.
1528 if (url) 1605 if (url)
1529 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); 1606 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE);
1530 } 1607 }
1531 1608
1532 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { 1609 void TemplateURLService::AddNoNotify(TemplateURL* template_url) {
1533 DCHECK(template_url); 1610 DCHECK(template_url);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 } 1749 }
1673 1750
1674 bool TemplateURLService::ResolveSyncKeywordConflict( 1751 bool TemplateURLService::ResolveSyncKeywordConflict(
1675 TemplateURL* sync_turl, 1752 TemplateURL* sync_turl,
1676 SyncChangeList* change_list) { 1753 SyncChangeList* change_list) {
1677 DCHECK(sync_turl); 1754 DCHECK(sync_turl);
1678 DCHECK(change_list); 1755 DCHECK(change_list);
1679 1756
1680 const TemplateURL* existing_turl = 1757 const TemplateURL* existing_turl =
1681 GetTemplateURLForKeyword(sync_turl->keyword()); 1758 GetTemplateURLForKeyword(sync_turl->keyword());
1682 if (!existing_turl) 1759 // If there is no conflict, or it's just conflicting with itself, return.
1760 if (!existing_turl || existing_turl->sync_guid() == sync_turl->sync_guid())
1683 return false; 1761 return false;
1684 1762
1685 if (existing_turl->last_modified() > sync_turl->last_modified() || 1763 if (existing_turl->last_modified() > sync_turl->last_modified() ||
1686 existing_turl->created_by_policy()) { 1764 existing_turl->created_by_policy()) {
1687 string16 new_keyword = UniquifyKeyword(*sync_turl); 1765 string16 new_keyword = UniquifyKeyword(*sync_turl);
1688 DCHECK(!GetTemplateURLForKeyword(new_keyword)); 1766 DCHECK(!GetTemplateURLForKeyword(new_keyword));
1689 sync_turl->set_keyword(new_keyword); 1767 sync_turl->set_keyword(new_keyword);
1690 // If we update the cloud TURL, we need to push an update back to sync 1768 // If we update the cloud TURL, we need to push an update back to sync
1691 // informing it that something has changed. 1769 // informing it that something has changed.
1692 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); 1770 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 } else { 1813 } else {
1736 // Change the local TURL's GUID to the server's GUID and push an update to 1814 // Change the local TURL's GUID to the server's GUID and push an update to
1737 // Sync. This ensures that the rest of local_url's fields are sync'd up to 1815 // Sync. This ensures that the rest of local_url's fields are sync'd up to
1738 // the server, and the next time local_url is synced, it is recognized by 1816 // the server, and the next time local_url is synced, it is recognized by
1739 // having the same GUID. 1817 // having the same GUID.
1740 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); 1818 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid());
1741 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 1819 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
1742 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); 1820 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data));
1743 } 1821 }
1744 } 1822 }
1823
1824 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced(
1825 const std::string& guid) {
1826 // If we're not syncing or if default search is managed by policy, ignore.
1827 if (!sync_processor_ || is_default_search_managed_)
1828 return;
1829
1830 PrefService* prefs = GetPrefs();
1831 if (prefs && pending_synced_default_search_ &&
1832 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID) == guid) {
1833 // Make sure this actually exists. We should not be calling this unless we
1834 // really just added this TemplateURL.
1835 const TemplateURL* turl_from_sync = GetTemplateURLForGUID(guid);
1836 DCHECK(turl_from_sync);
1837 SetDefaultSearchProvider(turl_from_sync);
1838 pending_synced_default_search_ = false;
1839 }
1840 }
1841
1842 const TemplateURL* TemplateURLService::GetPendingSyncedDefaultSearchProvider() {
1843 PrefService* prefs = GetPrefs();
1844 if (!prefs || !pending_synced_default_search_)
1845 return NULL;
1846
1847 // Could be NULL if no such thing exists.
1848 return GetTemplateURLForGUID(
1849 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
1850 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698