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

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

Issue 10698014: [Sync] Rename csync namespace to syncer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 5 months 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/guid.h" 10 #include "base/guid.h"
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // to changes after it in |change_list|. 103 // to changes after it in |change_list|.
104 // The criteria is: 104 // The criteria is:
105 // 1) It is an ACTION_UPDATE or ACTION_DELETE and the sync_guid associated 105 // 1) It is an ACTION_UPDATE or ACTION_DELETE and the sync_guid associated
106 // with it is NOT found in |sync_data|. We can only update and remove 106 // with it is NOT found in |sync_data|. We can only update and remove
107 // entries that were originally from the Sync server. 107 // entries that were originally from the Sync server.
108 // 2) It is an ACTION_ADD and the sync_guid associated with it is found in 108 // 2) It is an ACTION_ADD and the sync_guid associated with it is found in
109 // |sync_data|. We cannot re-add entries that Sync already knew about. 109 // |sync_data|. We cannot re-add entries that Sync already knew about.
110 // 3) There is an update after an update for the same GUID. We prune earlier 110 // 3) There is an update after an update for the same GUID. We prune earlier
111 // ones just to save bandwidth (Sync would normally coalesce them). 111 // ones just to save bandwidth (Sync would normally coalesce them).
112 bool ShouldRemoveSyncChange(size_t index, 112 bool ShouldRemoveSyncChange(size_t index,
113 csync::SyncChangeList* change_list, 113 syncer::SyncChangeList* change_list,
114 const SyncDataMap* sync_data) { 114 const SyncDataMap* sync_data) {
115 DCHECK(index < change_list->size()); 115 DCHECK(index < change_list->size());
116 const csync::SyncChange& change_i = (*change_list)[index]; 116 const syncer::SyncChange& change_i = (*change_list)[index];
117 const std::string guid = change_i.sync_data().GetSpecifics() 117 const std::string guid = change_i.sync_data().GetSpecifics()
118 .search_engine().sync_guid(); 118 .search_engine().sync_guid();
119 csync::SyncChange::SyncChangeType type = change_i.change_type(); 119 syncer::SyncChange::SyncChangeType type = change_i.change_type();
120 if ((type == csync::SyncChange::ACTION_UPDATE || 120 if ((type == syncer::SyncChange::ACTION_UPDATE ||
121 type == csync::SyncChange::ACTION_DELETE) && 121 type == syncer::SyncChange::ACTION_DELETE) &&
122 sync_data->find(guid) == sync_data->end()) 122 sync_data->find(guid) == sync_data->end())
123 return true; 123 return true;
124 if (type == csync::SyncChange::ACTION_ADD && 124 if (type == syncer::SyncChange::ACTION_ADD &&
125 sync_data->find(guid) != sync_data->end()) 125 sync_data->find(guid) != sync_data->end())
126 return true; 126 return true;
127 if (type == csync::SyncChange::ACTION_UPDATE) { 127 if (type == syncer::SyncChange::ACTION_UPDATE) {
128 for (size_t j = index + 1; j < change_list->size(); j++) { 128 for (size_t j = index + 1; j < change_list->size(); j++) {
129 const csync::SyncChange& change_j = (*change_list)[j]; 129 const syncer::SyncChange& change_j = (*change_list)[j];
130 if ((csync::SyncChange::ACTION_UPDATE == change_j.change_type()) && 130 if ((syncer::SyncChange::ACTION_UPDATE == change_j.change_type()) &&
131 (change_j.sync_data().GetSpecifics().search_engine().sync_guid() == 131 (change_j.sync_data().GetSpecifics().search_engine().sync_guid() ==
132 guid)) 132 guid))
133 return true; 133 return true;
134 } 134 }
135 } 135 }
136 return false; 136 return false;
137 } 137 }
138 138
139 // Remove SyncChanges that should not be sent to the server from |change_list|. 139 // Remove SyncChanges that should not be sent to the server from |change_list|.
140 // This is done to eliminate incorrect csync::SyncChanges added by the merge and 140 // This is done to eliminate incorrect SyncChanges added by the merge and
141 // conflict resolution logic when it is unsure of whether or not an entry is new 141 // conflict resolution logic when it is unsure of whether or not an entry is new
142 // from Sync or originally from the local model. This also removes changes that 142 // from Sync or originally from the local model. This also removes changes that
143 // would be otherwise be coalesced by Sync in order to save bandwidth. 143 // would be otherwise be coalesced by Sync in order to save bandwidth.
144 void PruneSyncChanges(const SyncDataMap* sync_data, 144 void PruneSyncChanges(const SyncDataMap* sync_data,
145 csync::SyncChangeList* change_list) { 145 syncer::SyncChangeList* change_list) {
146 for (size_t i = 0; i < change_list->size(); ) { 146 for (size_t i = 0; i < change_list->size(); ) {
147 if (ShouldRemoveSyncChange(i, change_list, sync_data)) 147 if (ShouldRemoveSyncChange(i, change_list, sync_data))
148 change_list->erase(change_list->begin() + i); 148 change_list->erase(change_list->begin() + i);
149 else 149 else
150 ++i; 150 ++i;
151 } 151 }
152 } 152 }
153 153
154 // Helper class that reports a specific string as the Google base URL. We use 154 // Helper class that reports a specific string as the Google base URL. We use
155 // this so that code can calculate a TemplateURL's previous search URL after a 155 // this so that code can calculate a TemplateURL's previous search URL after a
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 // flag to indicate that we waiting on the search engine entry to come 856 // flag to indicate that we waiting on the search engine entry to come
857 // in through Sync. 857 // in through Sync.
858 pending_synced_default_search_ = true; 858 pending_synced_default_search_ = true;
859 } 859 }
860 UpdateDefaultSearch(); 860 UpdateDefaultSearch();
861 } else { 861 } else {
862 NOTREACHED(); 862 NOTREACHED();
863 } 863 }
864 } 864 }
865 865
866 csync::SyncDataList TemplateURLService::GetAllSyncData( 866 syncer::SyncDataList TemplateURLService::GetAllSyncData(
867 syncable::ModelType type) const { 867 syncable::ModelType type) const {
868 DCHECK_EQ(syncable::SEARCH_ENGINES, type); 868 DCHECK_EQ(syncable::SEARCH_ENGINES, type);
869 869
870 csync::SyncDataList current_data; 870 syncer::SyncDataList current_data;
871 for (TemplateURLVector::const_iterator iter = template_urls_.begin(); 871 for (TemplateURLVector::const_iterator iter = template_urls_.begin();
872 iter != template_urls_.end(); ++iter) { 872 iter != template_urls_.end(); ++iter) {
873 // We don't sync extension keywords. 873 // We don't sync extension keywords.
874 // TODO(mpcomplete): If we allow editing extension keywords, then those 874 // TODO(mpcomplete): If we allow editing extension keywords, then those
875 // should be persisted to disk and synced. 875 // should be persisted to disk and synced.
876 if ((*iter)->IsExtensionKeyword()) 876 if ((*iter)->IsExtensionKeyword())
877 continue; 877 continue;
878 // We don't sync keywords managed by policy. 878 // We don't sync keywords managed by policy.
879 if ((*iter)->created_by_policy()) 879 if ((*iter)->created_by_policy())
880 continue; 880 continue;
881 current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); 881 current_data.push_back(CreateSyncDataFromTemplateURL(**iter));
882 } 882 }
883 883
884 return current_data; 884 return current_data;
885 } 885 }
886 886
887 csync::SyncError TemplateURLService::ProcessSyncChanges( 887 syncer::SyncError TemplateURLService::ProcessSyncChanges(
888 const tracked_objects::Location& from_here, 888 const tracked_objects::Location& from_here,
889 const csync::SyncChangeList& change_list) { 889 const syncer::SyncChangeList& change_list) {
890 if (!models_associated_) { 890 if (!models_associated_) {
891 csync::SyncError error(FROM_HERE, "Models not yet associated.", 891 syncer::SyncError error(FROM_HERE, "Models not yet associated.",
892 syncable::SEARCH_ENGINES); 892 syncable::SEARCH_ENGINES);
893 return error; 893 return error;
894 } 894 }
895 DCHECK(loaded_); 895 DCHECK(loaded_);
896 896
897 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); 897 AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
898 898
899 // We've started syncing, so set our origin member to the base Sync value. 899 // We've started syncing, so set our origin member to the base Sync value.
900 // As we move through Sync Code, we may set this to increasingly specific 900 // As we move through Sync Code, we may set this to increasingly specific
901 // origins so we can tell what exactly caused a DSP change. 901 // origins so we can tell what exactly caused a DSP change.
902 AutoReset<DefaultSearchChangeOrigin> change_origin(&dsp_change_origin_, 902 AutoReset<DefaultSearchChangeOrigin> change_origin(&dsp_change_origin_,
903 DSP_CHANGE_SYNC_UNINTENTIONAL); 903 DSP_CHANGE_SYNC_UNINTENTIONAL);
904 904
905 csync::SyncChangeList new_changes; 905 syncer::SyncChangeList new_changes;
906 csync::SyncError error; 906 syncer::SyncError error;
907 for (csync::SyncChangeList::const_iterator iter = change_list.begin(); 907 for (syncer::SyncChangeList::const_iterator iter = change_list.begin();
908 iter != change_list.end(); ++iter) { 908 iter != change_list.end(); ++iter) {
909 DCHECK_EQ(syncable::SEARCH_ENGINES, iter->sync_data().GetDataType()); 909 DCHECK_EQ(syncable::SEARCH_ENGINES, iter->sync_data().GetDataType());
910 910
911 std::string guid = 911 std::string guid =
912 iter->sync_data().GetSpecifics().search_engine().sync_guid(); 912 iter->sync_data().GetSpecifics().search_engine().sync_guid();
913 TemplateURL* existing_turl = GetTemplateURLForGUID(guid); 913 TemplateURL* existing_turl = GetTemplateURLForGUID(guid);
914 scoped_ptr<TemplateURL> turl(CreateTemplateURLFromTemplateURLAndSyncData( 914 scoped_ptr<TemplateURL> turl(CreateTemplateURLFromTemplateURLAndSyncData(
915 profile_, existing_turl, iter->sync_data(), &new_changes)); 915 profile_, existing_turl, iter->sync_data(), &new_changes));
916 if (!turl.get()) 916 if (!turl.get())
917 continue; 917 continue;
918 918
919 // Explicitly don't check for conflicts against extension keywords; in this 919 // Explicitly don't check for conflicts against extension keywords; in this
920 // case the functions which modify the keyword map know how to handle the 920 // case the functions which modify the keyword map know how to handle the
921 // conflicts. 921 // conflicts.
922 // TODO(mpcomplete): If we allow editing extension keywords, then those will 922 // TODO(mpcomplete): If we allow editing extension keywords, then those will
923 // need to undergo conflict resolution. 923 // need to undergo conflict resolution.
924 TemplateURL* existing_keyword_turl = 924 TemplateURL* existing_keyword_turl =
925 FindNonExtensionTemplateURLForKeyword(turl->keyword()); 925 FindNonExtensionTemplateURLForKeyword(turl->keyword());
926 if (iter->change_type() == csync::SyncChange::ACTION_DELETE) { 926 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
927 if (!existing_turl) { 927 if (!existing_turl) {
928 NOTREACHED() << "Unexpected sync change state."; 928 NOTREACHED() << "Unexpected sync change state.";
929 error = sync_error_factory_->CreateAndUploadError( 929 error = sync_error_factory_->CreateAndUploadError(
930 FROM_HERE, 930 FROM_HERE,
931 "ProcessSyncChanges failed on ChangeType ACTION_DELETE"); 931 "ProcessSyncChanges failed on ChangeType ACTION_DELETE");
932 LOG(ERROR) << "Trying to delete a non-existent TemplateURL."; 932 LOG(ERROR) << "Trying to delete a non-existent TemplateURL.";
933 continue; 933 continue;
934 } 934 }
935 bool delete_default = (existing_turl == GetDefaultSearchProvider()); 935 bool delete_default = (existing_turl == GetDefaultSearchProvider());
936 936
937 if (delete_default && is_default_search_managed_) { 937 if (delete_default && is_default_search_managed_) {
938 NOTREACHED() << "Tried to delete managed default search provider"; 938 NOTREACHED() << "Tried to delete managed default search provider";
939 } else { 939 } else {
940 if (delete_default) 940 if (delete_default)
941 default_search_provider_ = NULL; 941 default_search_provider_ = NULL;
942 942
943 Remove(existing_turl); 943 Remove(existing_turl);
944 944
945 if (delete_default) { 945 if (delete_default) {
946 AutoReset<DefaultSearchChangeOrigin> change_origin( 946 AutoReset<DefaultSearchChangeOrigin> change_origin(
947 &dsp_change_origin_, DSP_CHANGE_SYNC_DELETE); 947 &dsp_change_origin_, DSP_CHANGE_SYNC_DELETE);
948 SetDefaultSearchProvider(FindNewDefaultSearchProvider()); 948 SetDefaultSearchProvider(FindNewDefaultSearchProvider());
949 } 949 }
950 } 950 }
951 } else if (iter->change_type() == csync::SyncChange::ACTION_ADD) { 951 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) {
952 if (existing_turl) { 952 if (existing_turl) {
953 NOTREACHED() << "Unexpected sync change state."; 953 NOTREACHED() << "Unexpected sync change state.";
954 error = sync_error_factory_->CreateAndUploadError( 954 error = sync_error_factory_->CreateAndUploadError(
955 FROM_HERE, 955 FROM_HERE,
956 "ProcessSyncChanges failed on ChangeType ACTION_ADD"); 956 "ProcessSyncChanges failed on ChangeType ACTION_ADD");
957 LOG(ERROR) << "Trying to add an existing TemplateURL."; 957 LOG(ERROR) << "Trying to add an existing TemplateURL.";
958 continue; 958 continue;
959 } 959 }
960 std::string guid = turl->sync_guid(); 960 std::string guid = turl->sync_guid();
961 if (!existing_keyword_turl || ResolveSyncKeywordConflict(turl.get(), 961 if (!existing_keyword_turl || ResolveSyncKeywordConflict(turl.get(),
962 existing_keyword_turl, &new_changes)) { 962 existing_keyword_turl, &new_changes)) {
963 // Force the local ID to kInvalidTemplateURLID so we can add it. 963 // Force the local ID to kInvalidTemplateURLID so we can add it.
964 TemplateURLData data(turl->data()); 964 TemplateURLData data(turl->data());
965 data.id = kInvalidTemplateURLID; 965 data.id = kInvalidTemplateURLID;
966 Add(new TemplateURL(profile_, data)); 966 Add(new TemplateURL(profile_, data));
967 967
968 // Possibly set the newly added |turl| as the default search provider. 968 // Possibly set the newly added |turl| as the default search provider.
969 SetDefaultSearchProviderIfNewlySynced(guid); 969 SetDefaultSearchProviderIfNewlySynced(guid);
970 } 970 }
971 } else if (iter->change_type() == csync::SyncChange::ACTION_UPDATE) { 971 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) {
972 if (!existing_turl) { 972 if (!existing_turl) {
973 NOTREACHED() << "Unexpected sync change state."; 973 NOTREACHED() << "Unexpected sync change state.";
974 error = sync_error_factory_->CreateAndUploadError( 974 error = sync_error_factory_->CreateAndUploadError(
975 FROM_HERE, 975 FROM_HERE,
976 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE"); 976 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE");
977 LOG(ERROR) << "Trying to update a non-existent TemplateURL."; 977 LOG(ERROR) << "Trying to update a non-existent TemplateURL.";
978 continue; 978 continue;
979 } 979 }
980 // Possibly resolve a keyword conflict if they have the same keywords but 980 // Possibly resolve a keyword conflict if they have the same keywords but
981 // are not the same entry. 981 // are not the same entry.
(...skipping 21 matching lines...) Expand all
1003 // If something went wrong, we want to prematurely exit to avoid pushing 1003 // If something went wrong, we want to prematurely exit to avoid pushing
1004 // inconsistent data to Sync. We return the last error we received. 1004 // inconsistent data to Sync. We return the last error we received.
1005 if (error.IsSet()) 1005 if (error.IsSet())
1006 return error; 1006 return error;
1007 1007
1008 error = sync_processor_->ProcessSyncChanges(from_here, new_changes); 1008 error = sync_processor_->ProcessSyncChanges(from_here, new_changes);
1009 1009
1010 return error; 1010 return error;
1011 } 1011 }
1012 1012
1013 csync::SyncError TemplateURLService::MergeDataAndStartSyncing( 1013 syncer::SyncError TemplateURLService::MergeDataAndStartSyncing(
1014 syncable::ModelType type, 1014 syncable::ModelType type,
1015 const csync::SyncDataList& initial_sync_data, 1015 const syncer::SyncDataList& initial_sync_data,
1016 scoped_ptr<csync::SyncChangeProcessor> sync_processor, 1016 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1017 scoped_ptr<csync::SyncErrorFactory> sync_error_factory) { 1017 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
1018 DCHECK(loaded_); 1018 DCHECK(loaded_);
1019 DCHECK_EQ(type, syncable::SEARCH_ENGINES); 1019 DCHECK_EQ(type, syncable::SEARCH_ENGINES);
1020 DCHECK(!sync_processor_.get()); 1020 DCHECK(!sync_processor_.get());
1021 DCHECK(sync_processor.get()); 1021 DCHECK(sync_processor.get());
1022 DCHECK(sync_error_factory.get()); 1022 DCHECK(sync_error_factory.get());
1023 sync_processor_ = sync_processor.Pass(); 1023 sync_processor_ = sync_processor.Pass();
1024 sync_error_factory_ = sync_error_factory.Pass(); 1024 sync_error_factory_ = sync_error_factory.Pass();
1025 1025
1026 // We just started syncing, so set our wait-for-default flag if we are 1026 // We just started syncing, so set our wait-for-default flag if we are
1027 // expecting a default from Sync. 1027 // expecting a default from Sync.
(...skipping 10 matching lines...) Expand all
1038 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we 1038 // We do a lot of calls to Add/Remove/ResetTemplateURL here, so ensure we
1039 // don't step on our own toes. 1039 // don't step on our own toes.
1040 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); 1040 AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
1041 1041
1042 // We've started syncing, so set our origin member to the base Sync value. 1042 // We've started syncing, so set our origin member to the base Sync value.
1043 // As we move through Sync Code, we may set this to increasingly specific 1043 // As we move through Sync Code, we may set this to increasingly specific
1044 // origins so we can tell what exactly caused a DSP change. 1044 // origins so we can tell what exactly caused a DSP change.
1045 AutoReset<DefaultSearchChangeOrigin> change_origin(&dsp_change_origin_, 1045 AutoReset<DefaultSearchChangeOrigin> change_origin(&dsp_change_origin_,
1046 DSP_CHANGE_SYNC_UNINTENTIONAL); 1046 DSP_CHANGE_SYNC_UNINTENTIONAL);
1047 1047
1048 csync::SyncChangeList new_changes; 1048 syncer::SyncChangeList new_changes;
1049 1049
1050 // Build maps of our sync GUIDs to csync::SyncData. 1050 // Build maps of our sync GUIDs to syncer::SyncData.
1051 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( 1051 SyncDataMap local_data_map = CreateGUIDToSyncDataMap(
1052 GetAllSyncData(syncable::SEARCH_ENGINES)); 1052 GetAllSyncData(syncable::SEARCH_ENGINES));
1053 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); 1053 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data);
1054 1054
1055 for (SyncDataMap::const_iterator iter = sync_data_map.begin(); 1055 for (SyncDataMap::const_iterator iter = sync_data_map.begin();
1056 iter != sync_data_map.end(); ++iter) { 1056 iter != sync_data_map.end(); ++iter) {
1057 TemplateURL* local_turl = GetTemplateURLForGUID(iter->first); 1057 TemplateURL* local_turl = GetTemplateURLForGUID(iter->first);
1058 scoped_ptr<TemplateURL> sync_turl( 1058 scoped_ptr<TemplateURL> sync_turl(
1059 CreateTemplateURLFromTemplateURLAndSyncData(profile_, local_turl, 1059 CreateTemplateURLFromTemplateURLAndSyncData(profile_, local_turl,
1060 iter->second, &new_changes)); 1060 iter->second, &new_changes));
1061 if (!sync_turl.get()) 1061 if (!sync_turl.get())
1062 continue; 1062 continue;
1063 1063
1064 if (pre_sync_deletes_.find(sync_turl->sync_guid()) != 1064 if (pre_sync_deletes_.find(sync_turl->sync_guid()) !=
1065 pre_sync_deletes_.end()) { 1065 pre_sync_deletes_.end()) {
1066 // This entry was deleted before the initial sync began (possibly through 1066 // This entry was deleted before the initial sync began (possibly through
1067 // preprocessing in TemplateURLService's loading code). Ignore it and send 1067 // preprocessing in TemplateURLService's loading code). Ignore it and send
1068 // an ACTION_DELETE up to the server. 1068 // an ACTION_DELETE up to the server.
1069 new_changes.push_back(csync::SyncChange(csync::SyncChange::ACTION_DELETE, 1069 new_changes.push_back(
1070 iter->second)); 1070 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE,
1071 iter->second));
1071 continue; 1072 continue;
1072 } 1073 }
1073 1074
1074 if (local_turl) { 1075 if (local_turl) {
1075 // This local search engine is already synced. If the timestamp differs 1076 // This local search engine is already synced. If the timestamp differs
1076 // from Sync, we need to update locally or to the cloud. Note that if the 1077 // from Sync, we need to update locally or to the cloud. Note that if the
1077 // timestamps are equal, we touch neither. 1078 // timestamps are equal, we touch neither.
1078 if (sync_turl->last_modified() > local_turl->last_modified()) { 1079 if (sync_turl->last_modified() > local_turl->last_modified()) {
1079 // We've received an update from Sync. We should replace all synced 1080 // We've received an update from Sync. We should replace all synced
1080 // fields in the local TemplateURL. Note that this includes the 1081 // fields in the local TemplateURL. Note that this includes the
1081 // TemplateURLID and the TemplateURL may have to be reparsed. This 1082 // TemplateURLID and the TemplateURL may have to be reparsed. This
1082 // also makes the local data's last_modified timestamp equal to Sync's, 1083 // also makes the local data's last_modified timestamp equal to Sync's,
1083 // avoiding an Update on the next MergeData call. 1084 // avoiding an Update on the next MergeData call.
1084 UIThreadSearchTermsData search_terms_data(local_turl->profile()); 1085 UIThreadSearchTermsData search_terms_data(local_turl->profile());
1085 if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data)) 1086 if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data))
1086 NotifyObservers(); 1087 NotifyObservers();
1087 } else if (sync_turl->last_modified() < local_turl->last_modified()) { 1088 } else if (sync_turl->last_modified() < local_turl->last_modified()) {
1088 // Otherwise, we know we have newer data, so update Sync with our 1089 // Otherwise, we know we have newer data, so update Sync with our
1089 // data fields. 1090 // data fields.
1090 new_changes.push_back( 1091 new_changes.push_back(
1091 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, 1092 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE,
1092 local_data_map[local_turl->sync_guid()])); 1093 local_data_map[local_turl->sync_guid()]));
1093 } 1094 }
1094 local_data_map.erase(iter->first); 1095 local_data_map.erase(iter->first);
1095 } else { 1096 } else {
1096 // The search engine from the cloud has not been synced locally, but there 1097 // The search engine from the cloud has not been synced locally, but there
1097 // might be a local search engine that is a duplicate that needs to be 1098 // might be a local search engine that is a duplicate that needs to be
1098 // merged. 1099 // merged.
1099 TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl); 1100 TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl);
1100 if (dupe_turl) { 1101 if (dupe_turl) {
1101 // Merge duplicates and remove the processed local TURL from the map. 1102 // Merge duplicates and remove the processed local TURL from the map.
(...skipping 22 matching lines...) Expand all
1124 } 1125 }
1125 } 1126 }
1126 } 1127 }
1127 } 1128 }
1128 1129
1129 // The remaining SyncData in local_data_map should be everything that needs to 1130 // The remaining SyncData in local_data_map should be everything that needs to
1130 // be pushed as ADDs to sync. 1131 // be pushed as ADDs to sync.
1131 for (SyncDataMap::const_iterator iter = local_data_map.begin(); 1132 for (SyncDataMap::const_iterator iter = local_data_map.begin();
1132 iter != local_data_map.end(); ++iter) { 1133 iter != local_data_map.end(); ++iter) {
1133 new_changes.push_back( 1134 new_changes.push_back(
1134 csync::SyncChange(csync::SyncChange::ACTION_ADD, iter->second)); 1135 syncer::SyncChange(syncer::SyncChange::ACTION_ADD, iter->second));
1135 } 1136 }
1136 1137
1137 // Do some post-processing on the change list to ensure that we are sending 1138 // Do some post-processing on the change list to ensure that we are sending
1138 // valid changes to sync_processor_. 1139 // valid changes to sync_processor_.
1139 PruneSyncChanges(&sync_data_map, &new_changes); 1140 PruneSyncChanges(&sync_data_map, &new_changes);
1140 1141
1141 csync::SyncError error = 1142 syncer::SyncError error =
1142 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); 1143 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes);
1143 if (error.IsSet()) 1144 if (error.IsSet())
1144 return error; 1145 return error;
1145 1146
1146 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to 1147 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to
1147 // reuse them on the next call to MergeDataAndStartSyncing. 1148 // reuse them on the next call to MergeDataAndStartSyncing.
1148 pre_sync_deletes_.clear(); 1149 pre_sync_deletes_.clear();
1149 1150
1150 models_associated_ = true; 1151 models_associated_ = true;
1151 return csync::SyncError(); 1152 return syncer::SyncError();
1152 } 1153 }
1153 1154
1154 void TemplateURLService::StopSyncing(syncable::ModelType type) { 1155 void TemplateURLService::StopSyncing(syncable::ModelType type) {
1155 DCHECK_EQ(type, syncable::SEARCH_ENGINES); 1156 DCHECK_EQ(type, syncable::SEARCH_ENGINES);
1156 models_associated_ = false; 1157 models_associated_ = false;
1157 sync_processor_.reset(); 1158 sync_processor_.reset();
1158 sync_error_factory_.reset(); 1159 sync_error_factory_.reset();
1159 } 1160 }
1160 1161
1161 void TemplateURLService::ProcessTemplateURLChange( 1162 void TemplateURLService::ProcessTemplateURLChange(
1162 const TemplateURL* turl, 1163 const TemplateURL* turl,
1163 csync::SyncChange::SyncChangeType type) { 1164 syncer::SyncChange::SyncChangeType type) {
1164 DCHECK_NE(type, csync::SyncChange::ACTION_INVALID); 1165 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID);
1165 DCHECK(turl); 1166 DCHECK(turl);
1166 1167
1167 if (!models_associated_) 1168 if (!models_associated_)
1168 return; // Not syncing. 1169 return; // Not syncing.
1169 1170
1170 if (processing_syncer_changes_) 1171 if (processing_syncer_changes_)
1171 return; // These are changes originating from us. Ignore. 1172 return; // These are changes originating from us. Ignore.
1172 1173
1173 // Avoid syncing Extension keywords. 1174 // Avoid syncing Extension keywords.
1174 // TODO(mpcomplete): If we allow editing extension keywords, then those should 1175 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1175 // be persisted to disk and synced. 1176 // be persisted to disk and synced.
1176 if (turl->IsExtensionKeyword()) 1177 if (turl->IsExtensionKeyword())
1177 return; 1178 return;
1178 1179
1179 // Avoid syncing keywords managed by policy. 1180 // Avoid syncing keywords managed by policy.
1180 if (turl->created_by_policy()) 1181 if (turl->created_by_policy())
1181 return; 1182 return;
1182 1183
1183 csync::SyncChangeList changes; 1184 syncer::SyncChangeList changes;
1184 1185
1185 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1186 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1186 changes.push_back(csync::SyncChange(type, sync_data)); 1187 changes.push_back(syncer::SyncChange(type, sync_data));
1187 1188
1188 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); 1189 sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
1189 } 1190 }
1190 1191
1191 // static 1192 // static
1192 csync::SyncData TemplateURLService::CreateSyncDataFromTemplateURL( 1193 syncer::SyncData TemplateURLService::CreateSyncDataFromTemplateURL(
1193 const TemplateURL& turl) { 1194 const TemplateURL& turl) {
1194 sync_pb::EntitySpecifics specifics; 1195 sync_pb::EntitySpecifics specifics;
1195 sync_pb::SearchEngineSpecifics* se_specifics = 1196 sync_pb::SearchEngineSpecifics* se_specifics =
1196 specifics.mutable_search_engine(); 1197 specifics.mutable_search_engine();
1197 se_specifics->set_short_name(UTF16ToUTF8(turl.short_name())); 1198 se_specifics->set_short_name(UTF16ToUTF8(turl.short_name()));
1198 se_specifics->set_keyword(UTF16ToUTF8(turl.keyword())); 1199 se_specifics->set_keyword(UTF16ToUTF8(turl.keyword()));
1199 se_specifics->set_favicon_url(turl.favicon_url().spec()); 1200 se_specifics->set_favicon_url(turl.favicon_url().spec());
1200 se_specifics->set_url(turl.url()); 1201 se_specifics->set_url(turl.url());
1201 se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace()); 1202 se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace());
1202 se_specifics->set_originating_url(turl.originating_url().spec()); 1203 se_specifics->set_originating_url(turl.originating_url().spec());
1203 se_specifics->set_date_created(turl.date_created().ToInternalValue()); 1204 se_specifics->set_date_created(turl.date_created().ToInternalValue());
1204 se_specifics->set_input_encodings(JoinString(turl.input_encodings(), ';')); 1205 se_specifics->set_input_encodings(JoinString(turl.input_encodings(), ';'));
1205 se_specifics->set_show_in_default_list(turl.show_in_default_list()); 1206 se_specifics->set_show_in_default_list(turl.show_in_default_list());
1206 se_specifics->set_suggestions_url(turl.suggestions_url()); 1207 se_specifics->set_suggestions_url(turl.suggestions_url());
1207 se_specifics->set_prepopulate_id(turl.prepopulate_id()); 1208 se_specifics->set_prepopulate_id(turl.prepopulate_id());
1208 se_specifics->set_instant_url(turl.instant_url()); 1209 se_specifics->set_instant_url(turl.instant_url());
1209 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); 1210 se_specifics->set_last_modified(turl.last_modified().ToInternalValue());
1210 se_specifics->set_sync_guid(turl.sync_guid()); 1211 se_specifics->set_sync_guid(turl.sync_guid());
1211 return csync::SyncData::CreateLocalData(se_specifics->sync_guid(), 1212 return syncer::SyncData::CreateLocalData(se_specifics->sync_guid(),
1212 se_specifics->keyword(), 1213 se_specifics->keyword(),
1213 specifics); 1214 specifics);
1214 } 1215 }
1215 1216
1216 // static 1217 // static
1217 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( 1218 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData(
1218 Profile* profile, 1219 Profile* profile,
1219 TemplateURL* existing_turl, 1220 TemplateURL* existing_turl,
1220 const csync::SyncData& sync_data, 1221 const syncer::SyncData& sync_data,
1221 csync::SyncChangeList* change_list) { 1222 syncer::SyncChangeList* change_list) {
1222 DCHECK(change_list); 1223 DCHECK(change_list);
1223 1224
1224 sync_pb::SearchEngineSpecifics specifics = 1225 sync_pb::SearchEngineSpecifics specifics =
1225 sync_data.GetSpecifics().search_engine(); 1226 sync_data.GetSpecifics().search_engine();
1226 1227
1227 // Past bugs might have caused either of these fields to be empty. Just 1228 // Past bugs might have caused either of these fields to be empty. Just
1228 // delete this data off the server. 1229 // delete this data off the server.
1229 if (specifics.url().empty() || specifics.sync_guid().empty()) { 1230 if (specifics.url().empty() || specifics.sync_guid().empty()) {
1230 change_list->push_back( 1231 change_list->push_back(
1231 csync::SyncChange(csync::SyncChange::ACTION_DELETE, sync_data)); 1232 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data));
1232 return NULL; 1233 return NULL;
1233 } 1234 }
1234 1235
1235 TemplateURLData data(existing_turl ? 1236 TemplateURLData data(existing_turl ?
1236 existing_turl->data() : TemplateURLData()); 1237 existing_turl->data() : TemplateURLData());
1237 data.short_name = UTF8ToUTF16(specifics.short_name()); 1238 data.short_name = UTF8ToUTF16(specifics.short_name());
1238 data.originating_url = GURL(specifics.originating_url()); 1239 data.originating_url = GURL(specifics.originating_url());
1239 string16 keyword(UTF8ToUTF16(specifics.keyword())); 1240 string16 keyword(UTF8ToUTF16(specifics.keyword()));
1240 // NOTE: Once this code has shipped in a couple of stable releases, we can 1241 // NOTE: Once this code has shipped in a couple of stable releases, we can
1241 // probably remove the migration portion, comment out the 1242 // probably remove the migration portion, comment out the
(...skipping 20 matching lines...) Expand all
1262 data.date_created = base::Time::FromInternalValue(specifics.date_created()); 1263 data.date_created = base::Time::FromInternalValue(specifics.date_created());
1263 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); 1264 data.last_modified = base::Time::FromInternalValue(specifics.last_modified());
1264 data.prepopulate_id = specifics.prepopulate_id(); 1265 data.prepopulate_id = specifics.prepopulate_id();
1265 data.sync_guid = specifics.sync_guid(); 1266 data.sync_guid = specifics.sync_guid();
1266 1267
1267 TemplateURL* turl = new TemplateURL(profile, data); 1268 TemplateURL* turl = new TemplateURL(profile, data);
1268 DCHECK(!turl->IsExtensionKeyword()); 1269 DCHECK(!turl->IsExtensionKeyword());
1269 if (reset_keyword || deduped) { 1270 if (reset_keyword || deduped) {
1270 if (reset_keyword) 1271 if (reset_keyword)
1271 turl->ResetKeywordIfNecessary(true); 1272 turl->ResetKeywordIfNecessary(true);
1272 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1273 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1273 change_list->push_back( 1274 change_list->push_back(
1274 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, sync_data)); 1275 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data));
1275 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { 1276 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) {
1276 if (!existing_turl) { 1277 if (!existing_turl) {
1277 // We're adding a new TemplateURL that uses the Google base URL, so set 1278 // We're adding a new TemplateURL that uses the Google base URL, so set
1278 // its keyword appropriately for the local environment. 1279 // its keyword appropriately for the local environment.
1279 turl->ResetKeywordIfNecessary(false); 1280 turl->ResetKeywordIfNecessary(false);
1280 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) { 1281 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) {
1281 // Ignore keyword changes triggered by the Google base URL changing on 1282 // Ignore keyword changes triggered by the Google base URL changing on
1282 // another client. If the base URL changes in this client as well, we'll 1283 // another client. If the base URL changes in this client as well, we'll
1283 // pick that up separately at the appropriate time. Otherwise, changing 1284 // pick that up separately at the appropriate time. Otherwise, changing
1284 // the keyword here could result in having the wrong keyword for the local 1285 // the keyword here could result in having the wrong keyword for the local
1285 // environment. 1286 // environment.
1286 turl->data_.SetKeyword(existing_turl->keyword()); 1287 turl->data_.SetKeyword(existing_turl->keyword());
1287 } 1288 }
1288 } 1289 }
1289 1290
1290 return turl; 1291 return turl;
1291 } 1292 }
1292 1293
1293 // static 1294 // static
1294 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( 1295 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap(
1295 const csync::SyncDataList& sync_data) { 1296 const syncer::SyncDataList& sync_data) {
1296 SyncDataMap data_map; 1297 SyncDataMap data_map;
1297 for (csync::SyncDataList::const_iterator i(sync_data.begin()); 1298 for (syncer::SyncDataList::const_iterator i(sync_data.begin());
1298 i != sync_data.end(); 1299 i != sync_data.end();
1299 ++i) 1300 ++i)
1300 data_map[i->GetSpecifics().search_engine().sync_guid()] = *i; 1301 data_map[i->GetSpecifics().search_engine().sync_guid()] = *i;
1301 return data_map; 1302 return data_map;
1302 } 1303 }
1303 1304
1304 void TemplateURLService::SetKeywordSearchTermsForURL(const TemplateURL* t_url, 1305 void TemplateURLService::SetKeywordSearchTermsForURL(const TemplateURL* t_url,
1305 const GURL& url, 1306 const GURL& url,
1306 const string16& term) { 1307 const string16& term) {
1307 HistoryService* history = profile_ ? 1308 HistoryService* history = profile_ ?
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
1720 } 1721 }
1721 } 1722 }
1722 } 1723 }
1723 if (!existing_turl->sync_guid().empty()) 1724 if (!existing_turl->sync_guid().empty())
1724 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; 1725 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl;
1725 1726
1726 if (service_.get()) 1727 if (service_.get())
1727 service_->UpdateKeyword(existing_turl->data()); 1728 service_->UpdateKeyword(existing_turl->data());
1728 1729
1729 // Inform sync of the update. 1730 // Inform sync of the update.
1730 ProcessTemplateURLChange(existing_turl, csync::SyncChange::ACTION_UPDATE); 1731 ProcessTemplateURLChange(existing_turl, syncer::SyncChange::ACTION_UPDATE);
1731 1732
1732 if (default_search_provider_ == existing_turl) { 1733 if (default_search_provider_ == existing_turl) {
1733 bool success = SetDefaultSearchProviderNoNotify(existing_turl); 1734 bool success = SetDefaultSearchProviderNoNotify(existing_turl);
1734 DCHECK(success); 1735 DCHECK(success);
1735 } 1736 }
1736 return true; 1737 return true;
1737 } 1738 }
1738 1739
1739 PrefService* TemplateURLService::GetPrefs() { 1740 PrefService* TemplateURLService::GetPrefs() {
1740 return profile_ ? profile_->GetPrefs() : NULL; 1741 return profile_ ? profile_->GetPrefs() : NULL;
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
2051 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, 2052 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID,
2052 url->sync_guid()); 2053 url->sync_guid());
2053 } 2054 }
2054 } 2055 }
2055 2056
2056 if (service_.get()) 2057 if (service_.get())
2057 service_->SetDefaultSearchProvider(url); 2058 service_->SetDefaultSearchProvider(url);
2058 2059
2059 // Inform sync the change to the show_in_default_list flag. 2060 // Inform sync the change to the show_in_default_list flag.
2060 if (url) 2061 if (url)
2061 ProcessTemplateURLChange(url, csync::SyncChange::ACTION_UPDATE); 2062 ProcessTemplateURLChange(url, syncer::SyncChange::ACTION_UPDATE);
2062 return true; 2063 return true;
2063 } 2064 }
2064 2065
2065 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, 2066 bool TemplateURLService::AddNoNotify(TemplateURL* template_url,
2066 bool newly_adding) { 2067 bool newly_adding) {
2067 DCHECK(template_url); 2068 DCHECK(template_url);
2068 2069
2069 if (newly_adding) { 2070 if (newly_adding) {
2070 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); 2071 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
2071 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), 2072 DCHECK(std::find(template_urls_.begin(), template_urls_.end(),
(...skipping 28 matching lines...) Expand all
2100 if (newly_adding) { 2101 if (newly_adding) {
2101 // Don't persist extension keywords to disk. They'll get re-added on each 2102 // Don't persist extension keywords to disk. They'll get re-added on each
2102 // launch as the extensions are loaded. 2103 // launch as the extensions are loaded.
2103 // TODO(mpcomplete): If we allow editing extension keywords, then those 2104 // TODO(mpcomplete): If we allow editing extension keywords, then those
2104 // should be persisted to disk and synced. 2105 // should be persisted to disk and synced.
2105 if (service_.get() && !template_url->IsExtensionKeyword()) 2106 if (service_.get() && !template_url->IsExtensionKeyword())
2106 service_->AddKeyword(template_url->data()); 2107 service_->AddKeyword(template_url->data());
2107 2108
2108 // Inform sync of the addition. Note that this will assign a GUID to 2109 // Inform sync of the addition. Note that this will assign a GUID to
2109 // template_url and add it to the guid_to_template_map_. 2110 // template_url and add it to the guid_to_template_map_.
2110 ProcessTemplateURLChange(template_url, csync::SyncChange::ACTION_ADD); 2111 ProcessTemplateURLChange(template_url, syncer::SyncChange::ACTION_ADD);
2111 } 2112 }
2112 2113
2113 return true; 2114 return true;
2114 } 2115 }
2115 2116
2116 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { 2117 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) {
2117 TemplateURLVector::iterator i = 2118 TemplateURLVector::iterator i =
2118 std::find(template_urls_.begin(), template_urls_.end(), template_url); 2119 std::find(template_urls_.begin(), template_urls_.end(), template_url);
2119 if (i == template_urls_.end()) 2120 if (i == template_urls_.end())
2120 return; 2121 return;
2121 2122
2122 if (template_url == default_search_provider_) { 2123 if (template_url == default_search_provider_) {
2123 // Should never delete the default search provider. 2124 // Should never delete the default search provider.
2124 NOTREACHED(); 2125 NOTREACHED();
2125 return; 2126 return;
2126 } 2127 }
2127 2128
2128 RemoveFromMaps(template_url); 2129 RemoveFromMaps(template_url);
2129 2130
2130 // Remove it from the vector containing all TemplateURLs. 2131 // Remove it from the vector containing all TemplateURLs.
2131 template_urls_.erase(i); 2132 template_urls_.erase(i);
2132 2133
2133 // Extension keywords are not persisted. 2134 // Extension keywords are not persisted.
2134 // TODO(mpcomplete): If we allow editing extension keywords, then those should 2135 // TODO(mpcomplete): If we allow editing extension keywords, then those should
2135 // be persisted to disk and synced. 2136 // be persisted to disk and synced.
2136 if (service_.get() && !template_url->IsExtensionKeyword()) 2137 if (service_.get() && !template_url->IsExtensionKeyword())
2137 service_->RemoveKeyword(template_url->id()); 2138 service_->RemoveKeyword(template_url->id());
2138 2139
2139 // Inform sync of the deletion. 2140 // Inform sync of the deletion.
2140 ProcessTemplateURLChange(template_url, csync::SyncChange::ACTION_DELETE); 2141 ProcessTemplateURLChange(template_url, syncer::SyncChange::ACTION_DELETE);
2141 2142
2142 if (profile_) { 2143 if (profile_) {
2143 content::Source<Profile> source(profile_); 2144 content::Source<Profile> source(profile_);
2144 TemplateURLID id = template_url->id(); 2145 TemplateURLID id = template_url->id();
2145 content::NotificationService::current()->Notify( 2146 content::NotificationService::current()->Notify(
2146 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, 2147 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED,
2147 source, 2148 source,
2148 content::Details<TemplateURLID>(&id)); 2149 content::Details<TemplateURLID>(&id));
2149 } 2150 }
2150 2151
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
2242 do { 2243 do {
2243 keyword_candidate.append(ASCIIToUTF16("_")); 2244 keyword_candidate.append(ASCIIToUTF16("_"));
2244 } while (GetTemplateURLForKeyword(keyword_candidate)); 2245 } while (GetTemplateURLForKeyword(keyword_candidate));
2245 2246
2246 return keyword_candidate; 2247 return keyword_candidate;
2247 } 2248 }
2248 2249
2249 bool TemplateURLService::ResolveSyncKeywordConflict( 2250 bool TemplateURLService::ResolveSyncKeywordConflict(
2250 TemplateURL* sync_turl, 2251 TemplateURL* sync_turl,
2251 TemplateURL* local_turl, 2252 TemplateURL* local_turl,
2252 csync::SyncChangeList* change_list) { 2253 syncer::SyncChangeList* change_list) {
2253 DCHECK(loaded_); 2254 DCHECK(loaded_);
2254 DCHECK(sync_turl); 2255 DCHECK(sync_turl);
2255 DCHECK(local_turl); 2256 DCHECK(local_turl);
2256 DCHECK(sync_turl->sync_guid() != local_turl->sync_guid()); 2257 DCHECK(sync_turl->sync_guid() != local_turl->sync_guid());
2257 DCHECK(!local_turl->IsExtensionKeyword()); 2258 DCHECK(!local_turl->IsExtensionKeyword());
2258 DCHECK(change_list); 2259 DCHECK(change_list);
2259 2260
2260 const bool local_is_better = 2261 const bool local_is_better =
2261 (local_turl->last_modified() > sync_turl->last_modified()) || 2262 (local_turl->last_modified() > sync_turl->last_modified()) ||
2262 local_turl->created_by_policy() || 2263 local_turl->created_by_policy() ||
2263 (local_turl == GetDefaultSearchProvider()); 2264 (local_turl == GetDefaultSearchProvider());
2264 const bool can_replace_local = CanReplace(local_turl); 2265 const bool can_replace_local = CanReplace(local_turl);
2265 if (CanReplace(sync_turl) && (local_is_better || !can_replace_local)) { 2266 if (CanReplace(sync_turl) && (local_is_better || !can_replace_local)) {
2266 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); 2267 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl);
2267 change_list->push_back( 2268 change_list->push_back(
2268 csync::SyncChange(csync::SyncChange::ACTION_DELETE, sync_data)); 2269 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data));
2269 return false; 2270 return false;
2270 } 2271 }
2271 if (can_replace_local) { 2272 if (can_replace_local) {
2272 // Since we're processing sync changes, the upcoming Remove() won't generate 2273 // Since we're processing sync changes, the upcoming Remove() won't generate
2273 // an ACTION_DELETE. We need to do it manually to keep the server in sync 2274 // an ACTION_DELETE. We need to do it manually to keep the server in sync
2274 // with us. Note that if we're being called from 2275 // with us. Note that if we're being called from
2275 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather 2276 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather
2276 // than having just been brought down, then this is wrong, because the 2277 // than having just been brought down, then this is wrong, because the
2277 // server doesn't yet know about this entity; but in this case, 2278 // server doesn't yet know about this entity; but in this case,
2278 // PruneSyncChanges() will prune out the ACTION_DELETE we create here. 2279 // PruneSyncChanges() will prune out the ACTION_DELETE we create here.
2279 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 2280 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
2280 change_list->push_back( 2281 change_list->push_back(
2281 csync::SyncChange(csync::SyncChange::ACTION_DELETE, sync_data)); 2282 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data));
2282 Remove(local_turl); 2283 Remove(local_turl);
2283 } else if (local_is_better) { 2284 } else if (local_is_better) {
2284 string16 new_keyword = UniquifyKeyword(*sync_turl); 2285 string16 new_keyword = UniquifyKeyword(*sync_turl);
2285 DCHECK(!GetTemplateURLForKeyword(new_keyword)); 2286 DCHECK(!GetTemplateURLForKeyword(new_keyword));
2286 sync_turl->data_.SetKeyword(new_keyword); 2287 sync_turl->data_.SetKeyword(new_keyword);
2287 // If we update the cloud TURL, we need to push an update back to sync 2288 // If we update the cloud TURL, we need to push an update back to sync
2288 // informing it that something has changed. 2289 // informing it that something has changed.
2289 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); 2290 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl);
2290 change_list->push_back( 2291 change_list->push_back(
2291 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, sync_data)); 2292 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data));
2292 } else { 2293 } else {
2293 string16 new_keyword = UniquifyKeyword(*local_turl); 2294 string16 new_keyword = UniquifyKeyword(*local_turl);
2294 TemplateURLData data(local_turl->data()); 2295 TemplateURLData data(local_turl->data());
2295 data.SetKeyword(new_keyword); 2296 data.SetKeyword(new_keyword);
2296 TemplateURL new_turl(local_turl->profile(), data); 2297 TemplateURL new_turl(local_turl->profile(), data);
2297 UIThreadSearchTermsData search_terms_data(local_turl->profile()); 2298 UIThreadSearchTermsData search_terms_data(local_turl->profile());
2298 if (UpdateNoNotify(local_turl, new_turl, search_terms_data)) 2299 if (UpdateNoNotify(local_turl, new_turl, search_terms_data))
2299 NotifyObservers(); 2300 NotifyObservers();
2300 // Since we're processing sync changes, the UpdateNoNotify() above didn't 2301 // Since we're processing sync changes, the UpdateNoNotify() above didn't
2301 // generate an ACTION_UPDATE. We need to do it manually to keep the server 2302 // generate an ACTION_UPDATE. We need to do it manually to keep the server
2302 // in sync with us. Note that if we're being called from 2303 // in sync with us. Note that if we're being called from
2303 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather 2304 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather
2304 // than having just been brought down, then this is wrong, because the 2305 // than having just been brought down, then this is wrong, because the
2305 // server won't know about this entity until it processes the ACTION_ADD our 2306 // server won't know about this entity until it processes the ACTION_ADD our
2306 // caller will later generate; but in this case, PruneSyncChanges() will 2307 // caller will later generate; but in this case, PruneSyncChanges() will
2307 // prune out the ACTION_UPDATE we create here. 2308 // prune out the ACTION_UPDATE we create here.
2308 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 2309 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
2309 change_list->push_back( 2310 change_list->push_back(
2310 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, sync_data)); 2311 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data));
2311 } 2312 }
2312 return true; 2313 return true;
2313 } 2314 }
2314 2315
2315 TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( 2316 TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL(
2316 const TemplateURL& sync_turl) { 2317 const TemplateURL& sync_turl) {
2317 TemplateURL* existing_turl = GetTemplateURLForKeyword(sync_turl.keyword()); 2318 TemplateURL* existing_turl = GetTemplateURLForKeyword(sync_turl.keyword());
2318 return existing_turl && (existing_turl->url() == sync_turl.url()) ? 2319 return existing_turl && (existing_turl->url() == sync_turl.url()) ?
2319 existing_turl : NULL; 2320 existing_turl : NULL;
2320 } 2321 }
2321 2322
2322 void TemplateURLService::MergeSyncAndLocalURLDuplicates( 2323 void TemplateURLService::MergeSyncAndLocalURLDuplicates(
2323 TemplateURL* sync_turl, 2324 TemplateURL* sync_turl,
2324 TemplateURL* local_turl, 2325 TemplateURL* local_turl,
2325 csync::SyncChangeList* change_list) { 2326 syncer::SyncChangeList* change_list) {
2326 DCHECK(loaded_); 2327 DCHECK(loaded_);
2327 DCHECK(sync_turl); 2328 DCHECK(sync_turl);
2328 DCHECK(local_turl); 2329 DCHECK(local_turl);
2329 DCHECK(change_list); 2330 DCHECK(change_list);
2330 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl); 2331 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl);
2331 if (sync_turl->last_modified() > local_turl->last_modified()) { 2332 if (sync_turl->last_modified() > local_turl->last_modified()) {
2332 // Fully replace local_url with Sync's copy. Note that because use Add 2333 // Fully replace local_url with Sync's copy. Note that because use Add
2333 // rather than ResetTemplateURL, |sync_url| is added with a fresh 2334 // rather than ResetTemplateURL, |sync_url| is added with a fresh
2334 // TemplateURLID. We don't need to sync the new ID back to the server since 2335 // TemplateURLID. We don't need to sync the new ID back to the server since
2335 // it's only relevant locally. 2336 // it's only relevant locally.
2336 bool delete_default = (local_turl == GetDefaultSearchProvider()); 2337 bool delete_default = (local_turl == GetDefaultSearchProvider());
2337 DCHECK(!delete_default || !is_default_search_managed_); 2338 DCHECK(!delete_default || !is_default_search_managed_);
2338 if (delete_default) 2339 if (delete_default)
2339 default_search_provider_ = NULL; 2340 default_search_provider_ = NULL;
2340 2341
2341 // See comments in ResolveSyncKeywordConflict() regarding generating an 2342 // See comments in ResolveSyncKeywordConflict() regarding generating an
2342 // ACTION_DELETE manually since Remove() won't do it. 2343 // ACTION_DELETE manually since Remove() won't do it.
2343 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 2344 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
2344 change_list->push_back( 2345 change_list->push_back(
2345 csync::SyncChange(csync::SyncChange::ACTION_DELETE, sync_data)); 2346 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data));
2346 Remove(local_turl); 2347 Remove(local_turl);
2347 2348
2348 // Force the local ID to kInvalidTemplateURLID so we can add it. 2349 // Force the local ID to kInvalidTemplateURLID so we can add it.
2349 sync_turl->data_.id = kInvalidTemplateURLID; 2350 sync_turl->data_.id = kInvalidTemplateURLID;
2350 Add(scoped_sync_turl.release()); 2351 Add(scoped_sync_turl.release());
2351 if (delete_default) 2352 if (delete_default)
2352 SetDefaultSearchProvider(sync_turl); 2353 SetDefaultSearchProvider(sync_turl);
2353 } else { 2354 } else {
2354 // Change the local TURL's GUID to the server's GUID and push an update to 2355 // Change the local TURL's GUID to the server's GUID and push an update to
2355 // Sync. This ensures that the rest of local_url's fields are sync'd up to 2356 // Sync. This ensures that the rest of local_url's fields are sync'd up to
2356 // the server, and the next time local_url is synced, it is recognized by 2357 // the server, and the next time local_url is synced, it is recognized by
2357 // having the same GUID. 2358 // having the same GUID.
2358 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); 2359 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid());
2359 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 2360 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
2360 change_list->push_back( 2361 change_list->push_back(
2361 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, sync_data)); 2362 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data));
2362 } 2363 }
2363 } 2364 }
2364 2365
2365 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( 2366 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced(
2366 const std::string& guid) { 2367 const std::string& guid) {
2367 // If we're not syncing or if default search is managed by policy, ignore. 2368 // If we're not syncing or if default search is managed by policy, ignore.
2368 if (!sync_processor_.get() || is_default_search_managed_) 2369 if (!sync_processor_.get() || is_default_search_managed_)
2369 return; 2370 return;
2370 2371
2371 PrefService* prefs = GetPrefs(); 2372 PrefService* prefs = GetPrefs();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2404 // TODO(mpcomplete): If we allow editing extension keywords, then those 2405 // TODO(mpcomplete): If we allow editing extension keywords, then those
2405 // should be persisted to disk and synced. 2406 // should be persisted to disk and synced.
2406 if (template_url->sync_guid().empty() && 2407 if (template_url->sync_guid().empty() &&
2407 !template_url->IsExtensionKeyword()) { 2408 !template_url->IsExtensionKeyword()) {
2408 template_url->data_.sync_guid = base::GenerateGUID(); 2409 template_url->data_.sync_guid = base::GenerateGUID();
2409 if (service_.get()) 2410 if (service_.get())
2410 service_->UpdateKeyword(template_url->data()); 2411 service_->UpdateKeyword(template_url->data());
2411 } 2412 }
2412 } 2413 }
2413 } 2414 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698