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: '' 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 = GetPendingSyncedDefaultSearchProvder();
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));
jeanluc1 2011/11/08 19:49:41 Can GetString return NULL? What's the behavior of
jeanluc1 2011/11/08 19:49:41 Would GetPendingSyncedDefaultSearchProvder work he
SteveT 2011/11/08 21:40:04 PrefService::GetString can return an empty string
SteveT 2011/11/08 21:40:04 It would not as it only retrieves the TURL if we a
667 if (new_default_search && !is_default_search_managed_) {
668 if (new_default_search != GetDefaultSearchProvider())
669 SetDefaultSearchProvider(new_default_search);
670 } else {
671 // If it's not there, or if default search is currently managed, set a
672 // flag to indicate that we waiting on the search engine entry to come
673 // in through Sync.
674 pending_synced_default_search_ = true;
jeanluc1 2011/11/08 19:49:41 What if we get a second Observe call, this time wh
SteveT 2011/11/08 21:40:04 No it doesn't. We should set pending_synced_defaul
675 }
676 }
677
652 // A preference related to default search engine has changed. 678 // A preference related to default search engine has changed.
653 // Update the model if needed. 679 // Update the model if needed.
654 UpdateDefaultSearch(); 680 UpdateDefaultSearch();
655 } 681 }
656 } else { 682 } else {
657 NOTREACHED(); 683 NOTREACHED();
658 } 684 }
659 } 685 }
660 686
661 SyncDataList TemplateURLService::GetAllSyncData( 687 SyncDataList TemplateURLService::GetAllSyncData(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 if (!turl.get()) { 725 if (!turl.get()) {
700 NOTREACHED() << "Failed to read search engine."; 726 NOTREACHED() << "Failed to read search engine.";
701 continue; 727 continue;
702 } 728 }
703 729
704 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid()); 730 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid());
705 const TemplateURL* existing_keyword_turl = 731 const TemplateURL* existing_keyword_turl =
706 GetTemplateURLForKeyword(turl->keyword()); 732 GetTemplateURLForKeyword(turl->keyword());
707 733
708 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { 734 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) {
735 bool delete_default = (existing_turl == GetDefaultSearchProvider());
736 if (delete_default && is_default_search_managed_) {
737 // We are not allowed to delete managed default search providers.
738 continue;
jeanluc1 2011/11/08 19:49:41 Can we avoid the continue? There are so many othe
SteveT 2011/11/08 21:40:04 Actually, we can be a bit more defensive and trans
739 }
740 if (delete_default)
741 default_search_provider_ = NULL;
742
709 Remove(existing_turl); 743 Remove(existing_turl);
744
745 if (delete_default)
746 SetDefaultSearchProvider(FindNewDefaultSearchProvider());
710 } else if (iter->change_type() == SyncChange::ACTION_ADD && 747 } else if (iter->change_type() == SyncChange::ACTION_ADD &&
711 !existing_turl) { 748 !existing_turl) {
749 std::string guid = turl->sync_guid();
712 if (existing_keyword_turl) 750 if (existing_keyword_turl)
713 ResolveSyncKeywordConflict(turl.get(), &new_changes); 751 ResolveSyncKeywordConflict(turl.get(), &new_changes);
714 // Force the local ID to 0 so we can add it. 752 // Force the local ID to 0 so we can add it.
715 turl->set_id(0); 753 turl->set_id(0);
716 Add(turl.release()); 754 Add(turl.release());
755
756 // Possibly set the newly added |turl| as the default search provider.
757 CheckForSyncedDefaultSearchProvider(guid);
717 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && 758 } else if (iter->change_type() == SyncChange::ACTION_UPDATE &&
718 existing_turl) { 759 existing_turl) {
719 if (existing_keyword_turl) 760 // Possibly resolve a keyword conflict if they have the same keywords but
761 // are not the same entry.
762 if (existing_keyword_turl && existing_keyword_turl != existing_turl)
720 ResolveSyncKeywordConflict(turl.get(), &new_changes); 763 ResolveSyncKeywordConflict(turl.get(), &new_changes);
721 ResetTemplateURL(existing_turl, turl->short_name(), turl->keyword(), 764 ResetTemplateURL(existing_turl, turl->short_name(), turl->keyword(),
722 turl->url() ? turl->url()->url() : std::string()); 765 turl->url() ? turl->url()->url() : std::string());
723 } else { 766 } else {
724 // Something really unexpected happened. Either we received an 767 // Something really unexpected happened. Either we received an
725 // ACTION_INVALID, or Sync is in a crazy state: 768 // ACTION_INVALID, or Sync is in a crazy state:
726 // . Trying to DELETE or UPDATE a non-existent search engine. 769 // . Trying to DELETE or UPDATE a non-existent search engine.
727 // . Trying to ADD a search engine that already exists. 770 // . Trying to ADD a search engine that already exists.
728 NOTREACHED() << "Unexpected sync change state."; 771 NOTREACHED() << "Unexpected sync change state.";
729 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " + 772 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " +
(...skipping 14 matching lines...) Expand all
744 787
745 SyncError TemplateURLService::MergeDataAndStartSyncing( 788 SyncError TemplateURLService::MergeDataAndStartSyncing(
746 syncable::ModelType type, 789 syncable::ModelType type,
747 const SyncDataList& initial_sync_data, 790 const SyncDataList& initial_sync_data,
748 SyncChangeProcessor* sync_processor) { 791 SyncChangeProcessor* sync_processor) {
749 DCHECK(loaded()); 792 DCHECK(loaded());
750 DCHECK_EQ(type, syncable::SEARCH_ENGINES); 793 DCHECK_EQ(type, syncable::SEARCH_ENGINES);
751 DCHECK(!sync_processor_); 794 DCHECK(!sync_processor_);
752 sync_processor_ = sync_processor; 795 sync_processor_ = sync_processor;
753 796
797 // We just started syncing, so set our wait-for-default flag if we are
798 // expecting a default from Sync.
799 if (GetPrefs()) {
800 std::string default_guid = GetPrefs()->GetString(
801 prefs::kSyncedDefaultSearchProviderGUID);
jeanluc1 2011/11/08 19:49:41 Would GetPendingSyncedDefaultSearchProvder work he
SteveT 2011/11/08 21:40:04 Unfortunately not. At this point, we can't fully t
802 const TemplateURL* current_default = GetDefaultSearchProvider();
803
804 if (!default_guid.empty() &&
805 (!current_default || current_default->sync_guid() == default_guid))
Nicolas Zea 2011/11/08 19:57:27 Should that last condition be current_default->syn
SteveT 2011/11/08 21:40:04 Yes, that was a slip. I'll add a small test case t
806 pending_synced_default_search_ = true;
807 }
808
754 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we 809 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we
755 // don't step on our own toes. 810 // don't step on our own toes.
756 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); 811 AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
757 812
758 SyncChangeList new_changes; 813 SyncChangeList new_changes;
759 814
760 // Build maps of our sync GUIDs to SyncData. 815 // Build maps of our sync GUIDs to SyncData.
761 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( 816 SyncDataMap local_data_map = CreateGUIDToSyncDataMap(
762 GetAllSyncData(syncable::SEARCH_ENGINES)); 817 GetAllSyncData(syncable::SEARCH_ENGINES));
763 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); 818 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 if (dupe_turl) { 858 if (dupe_turl) {
804 // Merge duplicates and remove the processed local TURL from the map. 859 // Merge duplicates and remove the processed local TURL from the map.
805 TemplateURL* modifiable_dupe_turl = 860 TemplateURL* modifiable_dupe_turl =
806 const_cast<TemplateURL*>(dupe_turl); 861 const_cast<TemplateURL*>(dupe_turl);
807 std::string old_guid = dupe_turl->sync_guid(); 862 std::string old_guid = dupe_turl->sync_guid();
808 MergeSyncAndLocalURLDuplicates(sync_turl.release(), 863 MergeSyncAndLocalURLDuplicates(sync_turl.release(),
809 modifiable_dupe_turl, 864 modifiable_dupe_turl,
810 &new_changes); 865 &new_changes);
811 local_data_map.erase(old_guid); 866 local_data_map.erase(old_guid);
812 } else { 867 } else {
868 std::string guid = sync_turl->sync_guid();
813 // Keyword conflict is possible in this case. Resolve it first before 869 // 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 870 // 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 871 // from local_data_map in this case as it may still need to be pushed to
816 // the cloud. 872 // the cloud.
817 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); 873 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes);
818 // Force the local ID to 0 so we can add it. 874 // Force the local ID to 0 so we can add it.
819 sync_turl->set_id(0); 875 sync_turl->set_id(0);
820 Add(sync_turl.release()); 876 Add(sync_turl.release());
877
878 // Possibly set the newly added |turl| as the default search provider.
879 CheckForSyncedDefaultSearchProvider(guid);
821 } 880 }
822 } 881 }
823 } // for 882 } // for
824 883
825 // The remaining SyncData in local_data_map should be everything that needs to 884 // The remaining SyncData in local_data_map should be everything that needs to
826 // be pushed as ADDs to sync. 885 // be pushed as ADDs to sync.
827 for (SyncDataMap::const_iterator iter = local_data_map.begin(); 886 for (SyncDataMap::const_iterator iter = local_data_map.begin();
828 iter != local_data_map.end(); ++iter) { 887 iter != local_data_map.end(); ++iter) {
829 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second)); 888 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second));
830 } 889 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 const GURL& url, 1002 const GURL& url,
944 const string16& term) { 1003 const string16& term) {
945 HistoryService* history = profile_ ? 1004 HistoryService* history = profile_ ?
946 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL; 1005 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
947 if (!history) 1006 if (!history)
948 return; 1007 return;
949 history->SetKeywordSearchTermsForURL(url, t_url->id(), term); 1008 history->SetKeywordSearchTermsForURL(url, t_url->id(), term);
950 } 1009 }
951 1010
952 void TemplateURLService::Init(const Initializer* initializers, 1011 void TemplateURLService::Init(const Initializer* initializers,
953 int num_initializers) { 1012 int num_initializers) {
954 // Register for notifications. 1013 // Register for notifications.
955 if (profile_) { 1014 if (profile_) {
956 // TODO(sky): bug 1166191. The keywords should be moved into the history 1015 // 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 1016 // 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 1017 // backend can handle automatically adding the search terms as the user
959 // navigates. 1018 // navigates.
960 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, 1019 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED,
961 content::Source<Profile>(profile_->GetOriginalProfile())); 1020 content::Source<Profile>(profile_->GetOriginalProfile()));
962 PrefService* prefs = GetPrefs(); 1021 PrefService* prefs = GetPrefs();
963 default_search_prefs_.reset( 1022 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); 1538 DCHECK(is_default_search_managed_ && !new_is_default_managed);
1480 is_default_search_managed_ = new_is_default_managed; 1539 is_default_search_managed_ = new_is_default_managed;
1481 // If we had a default, delete the previous default if created by policy 1540 // If we had a default, delete the previous default if created by policy
1482 // and set a likely default. 1541 // and set a likely default.
1483 if (NULL != default_search_provider_ && 1542 if (NULL != default_search_provider_ &&
1484 default_search_provider_->created_by_policy()) { 1543 default_search_provider_->created_by_policy()) {
1485 const TemplateURL* old_default = default_search_provider_; 1544 const TemplateURL* old_default = default_search_provider_;
1486 default_search_provider_ = NULL; 1545 default_search_provider_ = NULL;
1487 RemoveNoNotify(old_default); 1546 RemoveNoNotify(old_default);
1488 } 1547 }
1489 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); 1548
1549 // The likely default should be from Sync if we were waiting on Sync.
1550 // Otherwise, it should be FindNewDefaultSearchProvider.
1551 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvder();
1552 if (synced_default)
1553 pending_synced_default_search_ = false;
1554 SetDefaultSearchProviderNoNotify(synced_default ? synced_default :
1555 FindNewDefaultSearchProvider());
1490 } 1556 }
1491 NotifyObservers(); 1557 NotifyObservers();
1492 } 1558 }
1493 1559
1494 void TemplateURLService::SetDefaultSearchProviderNoNotify( 1560 void TemplateURLService::SetDefaultSearchProviderNoNotify(
1495 const TemplateURL* url) { 1561 const TemplateURL* url) {
1496 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != 1562 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) !=
1497 template_urls_.end()); 1563 template_urls_.end());
1498 default_search_provider_ = url; 1564 default_search_provider_ = url;
1499 1565
(...skipping 11 matching lines...) Expand all
1511 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) 1577 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
1512 // Needs to be evaluated. See http://crbug.com/62328. 1578 // Needs to be evaluated. See http://crbug.com/62328.
1513 base::ThreadRestrictions::ScopedAllowIO allow_io; 1579 base::ThreadRestrictions::ScopedAllowIO allow_io;
1514 RLZTracker::RecordProductEvent(rlz_lib::CHROME, 1580 RLZTracker::RecordProductEvent(rlz_lib::CHROME,
1515 rlz_lib::CHROME_OMNIBOX, 1581 rlz_lib::CHROME_OMNIBOX,
1516 rlz_lib::SET_TO_GOOGLE); 1582 rlz_lib::SET_TO_GOOGLE);
1517 #endif 1583 #endif
1518 } 1584 }
1519 } 1585 }
1520 1586
1521 if (!is_default_search_managed_) 1587 if (!is_default_search_managed_) {
1522 SaveDefaultSearchProviderToPrefs(url); 1588 SaveDefaultSearchProviderToPrefs(url);
1523 1589
1590 // If we are syncing, we want to set the synced pref that will notify other
1591 // instances to change their default to this new search provider.
1592 if (sync_processor_ && !url->sync_guid().empty() && GetPrefs()) {
1593 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID,
1594 url->sync_guid());
1595 }
1596 }
1597
1524 if (service_.get()) 1598 if (service_.get())
1525 service_->SetDefaultSearchProvider(url); 1599 service_->SetDefaultSearchProvider(url);
1526 1600
1527 // Inform sync the change to the show_in_default_list flag. 1601 // Inform sync the change to the show_in_default_list flag.
1528 if (url) 1602 if (url)
1529 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); 1603 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE);
1530 } 1604 }
1531 1605
1532 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { 1606 void TemplateURLService::AddNoNotify(TemplateURL* template_url) {
1533 DCHECK(template_url); 1607 DCHECK(template_url);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 } 1746 }
1673 1747
1674 bool TemplateURLService::ResolveSyncKeywordConflict( 1748 bool TemplateURLService::ResolveSyncKeywordConflict(
1675 TemplateURL* sync_turl, 1749 TemplateURL* sync_turl,
1676 SyncChangeList* change_list) { 1750 SyncChangeList* change_list) {
1677 DCHECK(sync_turl); 1751 DCHECK(sync_turl);
1678 DCHECK(change_list); 1752 DCHECK(change_list);
1679 1753
1680 const TemplateURL* existing_turl = 1754 const TemplateURL* existing_turl =
1681 GetTemplateURLForKeyword(sync_turl->keyword()); 1755 GetTemplateURLForKeyword(sync_turl->keyword());
1682 if (!existing_turl) 1756 // If there is no conflict, or it's just conflicting with itself, return.
1757 if (!existing_turl || existing_turl->sync_guid() == sync_turl->sync_guid())
1683 return false; 1758 return false;
1684 1759
1685 if (existing_turl->last_modified() > sync_turl->last_modified() || 1760 if (existing_turl->last_modified() > sync_turl->last_modified() ||
1686 existing_turl->created_by_policy()) { 1761 existing_turl->created_by_policy()) {
1687 string16 new_keyword = UniquifyKeyword(*sync_turl); 1762 string16 new_keyword = UniquifyKeyword(*sync_turl);
1688 DCHECK(!GetTemplateURLForKeyword(new_keyword)); 1763 DCHECK(!GetTemplateURLForKeyword(new_keyword));
1689 sync_turl->set_keyword(new_keyword); 1764 sync_turl->set_keyword(new_keyword);
1690 // If we update the cloud TURL, we need to push an update back to sync 1765 // If we update the cloud TURL, we need to push an update back to sync
1691 // informing it that something has changed. 1766 // informing it that something has changed.
1692 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); 1767 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 } else { 1810 } else {
1736 // Change the local TURL's GUID to the server's GUID and push an update to 1811 // 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 1812 // 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 1813 // the server, and the next time local_url is synced, it is recognized by
1739 // having the same GUID. 1814 // having the same GUID.
1740 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); 1815 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid());
1741 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 1816 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
1742 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); 1817 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data));
1743 } 1818 }
1744 } 1819 }
1820
1821 void TemplateURLService::CheckForSyncedDefaultSearchProvider(
1822 const std::string& guid) {
1823 // If we're not syncing or if default search is managed by policy, ignore.
1824 if (!sync_processor_ || is_default_search_managed_)
1825 return;
1826
1827 PrefService* prefs = GetPrefs();
1828 if (prefs && pending_synced_default_search_ &&
1829 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID) == guid) {
1830 // Make sure this actually exists. We should not be calling this unless we
1831 // really just added this TemplateURL.
1832 const TemplateURL* turl_from_sync = GetTemplateURLForGUID(guid);
1833 DCHECK(turl_from_sync);
1834 SetDefaultSearchProvider(turl_from_sync);
1835 pending_synced_default_search_ = false;
1836 }
1837 }
1838
1839 const TemplateURL* TemplateURLService::GetPendingSyncedDefaultSearchProvder() {
jeanluc1 2011/11/08 19:49:41 Provder -> Provider
SteveT 2011/11/08 21:40:04 Done.
1840 PrefService* prefs = GetPrefs();
1841 if (!prefs || !pending_synced_default_search_)
1842 return NULL;
1843
1844 // Could be NULL if no such thing exists.
1845 return GetTemplateURLForGUID(
1846 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
1847 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698