| OLD | NEW |
| 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 1072 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1083 iter->second, &new_changes)); | 1083 iter->second, &new_changes)); |
| 1084 if (!sync_turl.get()) | 1084 if (!sync_turl.get()) |
| 1085 continue; | 1085 continue; |
| 1086 | 1086 |
| 1087 if (pre_sync_deletes_.find(sync_turl->sync_guid()) != | 1087 if (pre_sync_deletes_.find(sync_turl->sync_guid()) != |
| 1088 pre_sync_deletes_.end()) { | 1088 pre_sync_deletes_.end()) { |
| 1089 // This entry was deleted before the initial sync began (possibly through | 1089 // This entry was deleted before the initial sync began (possibly through |
| 1090 // preprocessing in TemplateURLService's loading code). Ignore it and send | 1090 // preprocessing in TemplateURLService's loading code). Ignore it and send |
| 1091 // an ACTION_DELETE up to the server. | 1091 // an ACTION_DELETE up to the server. |
| 1092 new_changes.push_back( | 1092 new_changes.push_back( |
| 1093 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, | 1093 syncer::SyncChange(FROM_HERE, |
| 1094 syncer::SyncChange::ACTION_DELETE, |
| 1094 iter->second)); | 1095 iter->second)); |
| 1095 continue; | 1096 continue; |
| 1096 } | 1097 } |
| 1097 | 1098 |
| 1098 if (local_turl) { | 1099 if (local_turl) { |
| 1099 // This local search engine is already synced. If the timestamp differs | 1100 // This local search engine is already synced. If the timestamp differs |
| 1100 // from Sync, we need to update locally or to the cloud. Note that if the | 1101 // from Sync, we need to update locally or to the cloud. Note that if the |
| 1101 // timestamps are equal, we touch neither. | 1102 // timestamps are equal, we touch neither. |
| 1102 if (sync_turl->last_modified() > local_turl->last_modified()) { | 1103 if (sync_turl->last_modified() > local_turl->last_modified()) { |
| 1103 // We've received an update from Sync. We should replace all synced | 1104 // We've received an update from Sync. We should replace all synced |
| 1104 // fields in the local TemplateURL. Note that this includes the | 1105 // fields in the local TemplateURL. Note that this includes the |
| 1105 // TemplateURLID and the TemplateURL may have to be reparsed. This | 1106 // TemplateURLID and the TemplateURL may have to be reparsed. This |
| 1106 // also makes the local data's last_modified timestamp equal to Sync's, | 1107 // also makes the local data's last_modified timestamp equal to Sync's, |
| 1107 // avoiding an Update on the next MergeData call. | 1108 // avoiding an Update on the next MergeData call. |
| 1108 UIThreadSearchTermsData search_terms_data(local_turl->profile()); | 1109 UIThreadSearchTermsData search_terms_data(local_turl->profile()); |
| 1109 if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data)) | 1110 if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data)) |
| 1110 NotifyObservers(); | 1111 NotifyObservers(); |
| 1111 } else if (sync_turl->last_modified() < local_turl->last_modified()) { | 1112 } else if (sync_turl->last_modified() < local_turl->last_modified()) { |
| 1112 // Otherwise, we know we have newer data, so update Sync with our | 1113 // Otherwise, we know we have newer data, so update Sync with our |
| 1113 // data fields. | 1114 // data fields. |
| 1114 new_changes.push_back( | 1115 new_changes.push_back( |
| 1115 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, | 1116 syncer::SyncChange(FROM_HERE, |
| 1116 local_data_map[local_turl->sync_guid()])); | 1117 syncer::SyncChange::ACTION_UPDATE, |
| 1118 local_data_map[local_turl->sync_guid()])); |
| 1117 } | 1119 } |
| 1118 local_data_map.erase(iter->first); | 1120 local_data_map.erase(iter->first); |
| 1119 } else { | 1121 } else { |
| 1120 // The search engine from the cloud has not been synced locally, but there | 1122 // The search engine from the cloud has not been synced locally, but there |
| 1121 // might be a local search engine that is a duplicate that needs to be | 1123 // might be a local search engine that is a duplicate that needs to be |
| 1122 // merged. | 1124 // merged. |
| 1123 TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl); | 1125 TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl); |
| 1124 if (dupe_turl) { | 1126 if (dupe_turl) { |
| 1125 // Merge duplicates and remove the processed local TURL from the map. | 1127 // Merge duplicates and remove the processed local TURL from the map. |
| 1126 std::string old_guid = dupe_turl->sync_guid(); | 1128 std::string old_guid = dupe_turl->sync_guid(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1148 } | 1150 } |
| 1149 } | 1151 } |
| 1150 } | 1152 } |
| 1151 } | 1153 } |
| 1152 | 1154 |
| 1153 // The remaining SyncData in local_data_map should be everything that needs to | 1155 // The remaining SyncData in local_data_map should be everything that needs to |
| 1154 // be pushed as ADDs to sync. | 1156 // be pushed as ADDs to sync. |
| 1155 for (SyncDataMap::const_iterator iter = local_data_map.begin(); | 1157 for (SyncDataMap::const_iterator iter = local_data_map.begin(); |
| 1156 iter != local_data_map.end(); ++iter) { | 1158 iter != local_data_map.end(); ++iter) { |
| 1157 new_changes.push_back( | 1159 new_changes.push_back( |
| 1158 syncer::SyncChange(syncer::SyncChange::ACTION_ADD, iter->second)); | 1160 syncer::SyncChange(FROM_HERE, |
| 1161 syncer::SyncChange::ACTION_ADD, |
| 1162 iter->second)); |
| 1159 } | 1163 } |
| 1160 | 1164 |
| 1161 // Do some post-processing on the change list to ensure that we are sending | 1165 // Do some post-processing on the change list to ensure that we are sending |
| 1162 // valid changes to sync_processor_. | 1166 // valid changes to sync_processor_. |
| 1163 PruneSyncChanges(&sync_data_map, &new_changes); | 1167 PruneSyncChanges(&sync_data_map, &new_changes); |
| 1164 | 1168 |
| 1165 syncer::SyncError error = | 1169 syncer::SyncError error = |
| 1166 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 1170 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
| 1167 if (error.IsSet()) | 1171 if (error.IsSet()) |
| 1168 return error; | 1172 return error; |
| 1169 | 1173 |
| 1170 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to | 1174 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to |
| 1171 // reuse them on the next call to MergeDataAndStartSyncing. | 1175 // reuse them on the next call to MergeDataAndStartSyncing. |
| 1172 pre_sync_deletes_.clear(); | 1176 pre_sync_deletes_.clear(); |
| 1173 | 1177 |
| 1174 models_associated_ = true; | 1178 models_associated_ = true; |
| 1175 return syncer::SyncError(); | 1179 return syncer::SyncError(); |
| 1176 } | 1180 } |
| 1177 | 1181 |
| 1178 void TemplateURLService::StopSyncing(syncer::ModelType type) { | 1182 void TemplateURLService::StopSyncing(syncer::ModelType type) { |
| 1179 DCHECK_EQ(type, syncer::SEARCH_ENGINES); | 1183 DCHECK_EQ(type, syncer::SEARCH_ENGINES); |
| 1180 models_associated_ = false; | 1184 models_associated_ = false; |
| 1181 sync_processor_.reset(); | 1185 sync_processor_.reset(); |
| 1182 sync_error_factory_.reset(); | 1186 sync_error_factory_.reset(); |
| 1183 } | 1187 } |
| 1184 | 1188 |
| 1185 void TemplateURLService::ProcessTemplateURLChange( | 1189 void TemplateURLService::ProcessTemplateURLChange( |
| 1190 const tracked_objects::Location& from_here, |
| 1186 const TemplateURL* turl, | 1191 const TemplateURL* turl, |
| 1187 syncer::SyncChange::SyncChangeType type) { | 1192 syncer::SyncChange::SyncChangeType type) { |
| 1188 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID); | 1193 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID); |
| 1189 DCHECK(turl); | 1194 DCHECK(turl); |
| 1190 | 1195 |
| 1191 if (!models_associated_) | 1196 if (!models_associated_) |
| 1192 return; // Not syncing. | 1197 return; // Not syncing. |
| 1193 | 1198 |
| 1194 if (processing_syncer_changes_) | 1199 if (processing_syncer_changes_) |
| 1195 return; // These are changes originating from us. Ignore. | 1200 return; // These are changes originating from us. Ignore. |
| 1196 | 1201 |
| 1197 // Avoid syncing Extension keywords. | 1202 // Avoid syncing Extension keywords. |
| 1198 // TODO(mpcomplete): If we allow editing extension keywords, then those should | 1203 // TODO(mpcomplete): If we allow editing extension keywords, then those should |
| 1199 // be persisted to disk and synced. | 1204 // be persisted to disk and synced. |
| 1200 if (turl->IsExtensionKeyword()) | 1205 if (turl->IsExtensionKeyword()) |
| 1201 return; | 1206 return; |
| 1202 | 1207 |
| 1203 // Avoid syncing keywords managed by policy. | 1208 // Avoid syncing keywords managed by policy. |
| 1204 if (turl->created_by_policy()) | 1209 if (turl->created_by_policy()) |
| 1205 return; | 1210 return; |
| 1206 | 1211 |
| 1207 syncer::SyncChangeList changes; | 1212 syncer::SyncChangeList changes; |
| 1208 | 1213 |
| 1209 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1214 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
| 1210 changes.push_back(syncer::SyncChange(type, sync_data)); | 1215 changes.push_back(syncer::SyncChange(from_here, |
| 1216 type, |
| 1217 sync_data)); |
| 1211 | 1218 |
| 1212 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 1219 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 1213 } | 1220 } |
| 1214 | 1221 |
| 1215 // static | 1222 // static |
| 1216 syncer::SyncData TemplateURLService::CreateSyncDataFromTemplateURL( | 1223 syncer::SyncData TemplateURLService::CreateSyncDataFromTemplateURL( |
| 1217 const TemplateURL& turl) { | 1224 const TemplateURL& turl) { |
| 1218 sync_pb::EntitySpecifics specifics; | 1225 sync_pb::EntitySpecifics specifics; |
| 1219 sync_pb::SearchEngineSpecifics* se_specifics = | 1226 sync_pb::SearchEngineSpecifics* se_specifics = |
| 1220 specifics.mutable_search_engine(); | 1227 specifics.mutable_search_engine(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1245 syncer::SyncChangeList* change_list) { | 1252 syncer::SyncChangeList* change_list) { |
| 1246 DCHECK(change_list); | 1253 DCHECK(change_list); |
| 1247 | 1254 |
| 1248 sync_pb::SearchEngineSpecifics specifics = | 1255 sync_pb::SearchEngineSpecifics specifics = |
| 1249 sync_data.GetSpecifics().search_engine(); | 1256 sync_data.GetSpecifics().search_engine(); |
| 1250 | 1257 |
| 1251 // Past bugs might have caused either of these fields to be empty. Just | 1258 // Past bugs might have caused either of these fields to be empty. Just |
| 1252 // delete this data off the server. | 1259 // delete this data off the server. |
| 1253 if (specifics.url().empty() || specifics.sync_guid().empty()) { | 1260 if (specifics.url().empty() || specifics.sync_guid().empty()) { |
| 1254 change_list->push_back( | 1261 change_list->push_back( |
| 1255 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); | 1262 syncer::SyncChange(FROM_HERE, |
| 1263 syncer::SyncChange::ACTION_DELETE, |
| 1264 sync_data)); |
| 1256 return NULL; | 1265 return NULL; |
| 1257 } | 1266 } |
| 1258 | 1267 |
| 1259 TemplateURLData data(existing_turl ? | 1268 TemplateURLData data(existing_turl ? |
| 1260 existing_turl->data() : TemplateURLData()); | 1269 existing_turl->data() : TemplateURLData()); |
| 1261 data.short_name = UTF8ToUTF16(specifics.short_name()); | 1270 data.short_name = UTF8ToUTF16(specifics.short_name()); |
| 1262 data.originating_url = GURL(specifics.originating_url()); | 1271 data.originating_url = GURL(specifics.originating_url()); |
| 1263 string16 keyword(UTF8ToUTF16(specifics.keyword())); | 1272 string16 keyword(UTF8ToUTF16(specifics.keyword())); |
| 1264 // NOTE: Once this code has shipped in a couple of stable releases, we can | 1273 // NOTE: Once this code has shipped in a couple of stable releases, we can |
| 1265 // probably remove the migration portion, comment out the | 1274 // probably remove the migration portion, comment out the |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1288 data.prepopulate_id = specifics.prepopulate_id(); | 1297 data.prepopulate_id = specifics.prepopulate_id(); |
| 1289 data.sync_guid = specifics.sync_guid(); | 1298 data.sync_guid = specifics.sync_guid(); |
| 1290 | 1299 |
| 1291 TemplateURL* turl = new TemplateURL(profile, data); | 1300 TemplateURL* turl = new TemplateURL(profile, data); |
| 1292 DCHECK(!turl->IsExtensionKeyword()); | 1301 DCHECK(!turl->IsExtensionKeyword()); |
| 1293 if (reset_keyword || deduped) { | 1302 if (reset_keyword || deduped) { |
| 1294 if (reset_keyword) | 1303 if (reset_keyword) |
| 1295 turl->ResetKeywordIfNecessary(true); | 1304 turl->ResetKeywordIfNecessary(true); |
| 1296 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1305 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
| 1297 change_list->push_back( | 1306 change_list->push_back( |
| 1298 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); | 1307 syncer::SyncChange(FROM_HERE, |
| 1308 syncer::SyncChange::ACTION_UPDATE, |
| 1309 sync_data)); |
| 1299 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { | 1310 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { |
| 1300 if (!existing_turl) { | 1311 if (!existing_turl) { |
| 1301 // We're adding a new TemplateURL that uses the Google base URL, so set | 1312 // We're adding a new TemplateURL that uses the Google base URL, so set |
| 1302 // its keyword appropriately for the local environment. | 1313 // its keyword appropriately for the local environment. |
| 1303 turl->ResetKeywordIfNecessary(false); | 1314 turl->ResetKeywordIfNecessary(false); |
| 1304 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) { | 1315 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) { |
| 1305 // Ignore keyword changes triggered by the Google base URL changing on | 1316 // Ignore keyword changes triggered by the Google base URL changing on |
| 1306 // another client. If the base URL changes in this client as well, we'll | 1317 // another client. If the base URL changes in this client as well, we'll |
| 1307 // pick that up separately at the appropriate time. Otherwise, changing | 1318 // pick that up separately at the appropriate time. Otherwise, changing |
| 1308 // the keyword here could result in having the wrong keyword for the local | 1319 // the keyword here could result in having the wrong keyword for the local |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1744 } | 1755 } |
| 1745 } | 1756 } |
| 1746 } | 1757 } |
| 1747 if (!existing_turl->sync_guid().empty()) | 1758 if (!existing_turl->sync_guid().empty()) |
| 1748 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; | 1759 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; |
| 1749 | 1760 |
| 1750 if (service_.get()) | 1761 if (service_.get()) |
| 1751 service_->UpdateKeyword(existing_turl->data()); | 1762 service_->UpdateKeyword(existing_turl->data()); |
| 1752 | 1763 |
| 1753 // Inform sync of the update. | 1764 // Inform sync of the update. |
| 1754 ProcessTemplateURLChange(existing_turl, syncer::SyncChange::ACTION_UPDATE); | 1765 ProcessTemplateURLChange(FROM_HERE, |
| 1766 existing_turl, |
| 1767 syncer::SyncChange::ACTION_UPDATE); |
| 1755 | 1768 |
| 1756 if (default_search_provider_ == existing_turl) { | 1769 if (default_search_provider_ == existing_turl) { |
| 1757 bool success = SetDefaultSearchProviderNoNotify(existing_turl); | 1770 bool success = SetDefaultSearchProviderNoNotify(existing_turl); |
| 1758 DCHECK(success); | 1771 DCHECK(success); |
| 1759 } | 1772 } |
| 1760 return true; | 1773 return true; |
| 1761 } | 1774 } |
| 1762 | 1775 |
| 1763 PrefService* TemplateURLService::GetPrefs() { | 1776 PrefService* TemplateURLService::GetPrefs() { |
| 1764 return profile_ ? profile_->GetPrefs() : NULL; | 1777 return profile_ ? profile_->GetPrefs() : NULL; |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2075 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, | 2088 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, |
| 2076 url->sync_guid()); | 2089 url->sync_guid()); |
| 2077 } | 2090 } |
| 2078 } | 2091 } |
| 2079 | 2092 |
| 2080 if (service_.get()) | 2093 if (service_.get()) |
| 2081 service_->SetDefaultSearchProvider(url); | 2094 service_->SetDefaultSearchProvider(url); |
| 2082 | 2095 |
| 2083 // Inform sync the change to the show_in_default_list flag. | 2096 // Inform sync the change to the show_in_default_list flag. |
| 2084 if (url) | 2097 if (url) |
| 2085 ProcessTemplateURLChange(url, syncer::SyncChange::ACTION_UPDATE); | 2098 ProcessTemplateURLChange(FROM_HERE, |
| 2099 url, |
| 2100 syncer::SyncChange::ACTION_UPDATE); |
| 2086 return true; | 2101 return true; |
| 2087 } | 2102 } |
| 2088 | 2103 |
| 2089 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, | 2104 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, |
| 2090 bool newly_adding) { | 2105 bool newly_adding) { |
| 2091 DCHECK(template_url); | 2106 DCHECK(template_url); |
| 2092 | 2107 |
| 2093 if (newly_adding) { | 2108 if (newly_adding) { |
| 2094 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 2109 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
| 2095 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), | 2110 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 2124 if (newly_adding) { | 2139 if (newly_adding) { |
| 2125 // Don't persist extension keywords to disk. They'll get re-added on each | 2140 // Don't persist extension keywords to disk. They'll get re-added on each |
| 2126 // launch as the extensions are loaded. | 2141 // launch as the extensions are loaded. |
| 2127 // TODO(mpcomplete): If we allow editing extension keywords, then those | 2142 // TODO(mpcomplete): If we allow editing extension keywords, then those |
| 2128 // should be persisted to disk and synced. | 2143 // should be persisted to disk and synced. |
| 2129 if (service_.get() && !template_url->IsExtensionKeyword()) | 2144 if (service_.get() && !template_url->IsExtensionKeyword()) |
| 2130 service_->AddKeyword(template_url->data()); | 2145 service_->AddKeyword(template_url->data()); |
| 2131 | 2146 |
| 2132 // Inform sync of the addition. Note that this will assign a GUID to | 2147 // Inform sync of the addition. Note that this will assign a GUID to |
| 2133 // template_url and add it to the guid_to_template_map_. | 2148 // template_url and add it to the guid_to_template_map_. |
| 2134 ProcessTemplateURLChange(template_url, syncer::SyncChange::ACTION_ADD); | 2149 ProcessTemplateURLChange(FROM_HERE, |
| 2150 template_url, |
| 2151 syncer::SyncChange::ACTION_ADD); |
| 2135 } | 2152 } |
| 2136 | 2153 |
| 2137 return true; | 2154 return true; |
| 2138 } | 2155 } |
| 2139 | 2156 |
| 2140 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { | 2157 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { |
| 2141 TemplateURLVector::iterator i = | 2158 TemplateURLVector::iterator i = |
| 2142 std::find(template_urls_.begin(), template_urls_.end(), template_url); | 2159 std::find(template_urls_.begin(), template_urls_.end(), template_url); |
| 2143 if (i == template_urls_.end()) | 2160 if (i == template_urls_.end()) |
| 2144 return; | 2161 return; |
| 2145 | 2162 |
| 2146 if (template_url == default_search_provider_) { | 2163 if (template_url == default_search_provider_) { |
| 2147 // Should never delete the default search provider. | 2164 // Should never delete the default search provider. |
| 2148 NOTREACHED(); | 2165 NOTREACHED(); |
| 2149 return; | 2166 return; |
| 2150 } | 2167 } |
| 2151 | 2168 |
| 2152 RemoveFromMaps(template_url); | 2169 RemoveFromMaps(template_url); |
| 2153 | 2170 |
| 2154 // Remove it from the vector containing all TemplateURLs. | 2171 // Remove it from the vector containing all TemplateURLs. |
| 2155 template_urls_.erase(i); | 2172 template_urls_.erase(i); |
| 2156 | 2173 |
| 2157 // Extension keywords are not persisted. | 2174 // Extension keywords are not persisted. |
| 2158 // TODO(mpcomplete): If we allow editing extension keywords, then those should | 2175 // TODO(mpcomplete): If we allow editing extension keywords, then those should |
| 2159 // be persisted to disk and synced. | 2176 // be persisted to disk and synced. |
| 2160 if (service_.get() && !template_url->IsExtensionKeyword()) | 2177 if (service_.get() && !template_url->IsExtensionKeyword()) |
| 2161 service_->RemoveKeyword(template_url->id()); | 2178 service_->RemoveKeyword(template_url->id()); |
| 2162 | 2179 |
| 2163 // Inform sync of the deletion. | 2180 // Inform sync of the deletion. |
| 2164 ProcessTemplateURLChange(template_url, syncer::SyncChange::ACTION_DELETE); | 2181 ProcessTemplateURLChange(FROM_HERE, |
| 2182 template_url, |
| 2183 syncer::SyncChange::ACTION_DELETE); |
| 2165 | 2184 |
| 2166 if (profile_) { | 2185 if (profile_) { |
| 2167 content::Source<Profile> source(profile_); | 2186 content::Source<Profile> source(profile_); |
| 2168 TemplateURLID id = template_url->id(); | 2187 TemplateURLID id = template_url->id(); |
| 2169 content::NotificationService::current()->Notify( | 2188 content::NotificationService::current()->Notify( |
| 2170 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, | 2189 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, |
| 2171 source, | 2190 source, |
| 2172 content::Details<TemplateURLID>(&id)); | 2191 content::Details<TemplateURLID>(&id)); |
| 2173 } | 2192 } |
| 2174 | 2193 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2281 DCHECK(!local_turl->IsExtensionKeyword()); | 2300 DCHECK(!local_turl->IsExtensionKeyword()); |
| 2282 DCHECK(change_list); | 2301 DCHECK(change_list); |
| 2283 | 2302 |
| 2284 const bool local_is_better = | 2303 const bool local_is_better = |
| 2285 (local_turl->last_modified() > sync_turl->last_modified()) || | 2304 (local_turl->last_modified() > sync_turl->last_modified()) || |
| 2286 local_turl->created_by_policy() || | 2305 local_turl->created_by_policy() || |
| 2287 (local_turl == GetDefaultSearchProvider()); | 2306 (local_turl == GetDefaultSearchProvider()); |
| 2288 const bool can_replace_local = CanReplace(local_turl); | 2307 const bool can_replace_local = CanReplace(local_turl); |
| 2289 if (CanReplace(sync_turl) && (local_is_better || !can_replace_local)) { | 2308 if (CanReplace(sync_turl) && (local_is_better || !can_replace_local)) { |
| 2290 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 2309 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
| 2291 change_list->push_back( | 2310 change_list->push_back(syncer::SyncChange(FROM_HERE, |
| 2292 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); | 2311 syncer::SyncChange::ACTION_DELETE, |
| 2312 sync_data)); |
| 2293 return false; | 2313 return false; |
| 2294 } | 2314 } |
| 2295 if (can_replace_local) { | 2315 if (can_replace_local) { |
| 2296 // Since we're processing sync changes, the upcoming Remove() won't generate | 2316 // Since we're processing sync changes, the upcoming Remove() won't generate |
| 2297 // an ACTION_DELETE. We need to do it manually to keep the server in sync | 2317 // an ACTION_DELETE. We need to do it manually to keep the server in sync |
| 2298 // with us. Note that if we're being called from | 2318 // with us. Note that if we're being called from |
| 2299 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather | 2319 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather |
| 2300 // than having just been brought down, then this is wrong, because the | 2320 // than having just been brought down, then this is wrong, because the |
| 2301 // server doesn't yet know about this entity; but in this case, | 2321 // server doesn't yet know about this entity; but in this case, |
| 2302 // PruneSyncChanges() will prune out the ACTION_DELETE we create here. | 2322 // PruneSyncChanges() will prune out the ACTION_DELETE we create here. |
| 2303 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2323 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 2304 change_list->push_back( | 2324 change_list->push_back( |
| 2305 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); | 2325 syncer::SyncChange(FROM_HERE, |
| 2326 syncer::SyncChange::ACTION_DELETE, |
| 2327 sync_data)); |
| 2306 Remove(local_turl); | 2328 Remove(local_turl); |
| 2307 } else if (local_is_better) { | 2329 } else if (local_is_better) { |
| 2308 string16 new_keyword = UniquifyKeyword(*sync_turl); | 2330 string16 new_keyword = UniquifyKeyword(*sync_turl); |
| 2309 DCHECK(!GetTemplateURLForKeyword(new_keyword)); | 2331 DCHECK(!GetTemplateURLForKeyword(new_keyword)); |
| 2310 sync_turl->data_.SetKeyword(new_keyword); | 2332 sync_turl->data_.SetKeyword(new_keyword); |
| 2311 // If we update the cloud TURL, we need to push an update back to sync | 2333 // If we update the cloud TURL, we need to push an update back to sync |
| 2312 // informing it that something has changed. | 2334 // informing it that something has changed. |
| 2313 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 2335 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
| 2314 change_list->push_back( | 2336 change_list->push_back(syncer::SyncChange(FROM_HERE, |
| 2315 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); | 2337 syncer::SyncChange::ACTION_UPDATE, |
| 2338 sync_data)); |
| 2316 } else { | 2339 } else { |
| 2317 string16 new_keyword = UniquifyKeyword(*local_turl); | 2340 string16 new_keyword = UniquifyKeyword(*local_turl); |
| 2318 TemplateURLData data(local_turl->data()); | 2341 TemplateURLData data(local_turl->data()); |
| 2319 data.SetKeyword(new_keyword); | 2342 data.SetKeyword(new_keyword); |
| 2320 TemplateURL new_turl(local_turl->profile(), data); | 2343 TemplateURL new_turl(local_turl->profile(), data); |
| 2321 UIThreadSearchTermsData search_terms_data(local_turl->profile()); | 2344 UIThreadSearchTermsData search_terms_data(local_turl->profile()); |
| 2322 if (UpdateNoNotify(local_turl, new_turl, search_terms_data)) | 2345 if (UpdateNoNotify(local_turl, new_turl, search_terms_data)) |
| 2323 NotifyObservers(); | 2346 NotifyObservers(); |
| 2324 // Since we're processing sync changes, the UpdateNoNotify() above didn't | 2347 // Since we're processing sync changes, the UpdateNoNotify() above didn't |
| 2325 // generate an ACTION_UPDATE. We need to do it manually to keep the server | 2348 // generate an ACTION_UPDATE. We need to do it manually to keep the server |
| 2326 // in sync with us. Note that if we're being called from | 2349 // in sync with us. Note that if we're being called from |
| 2327 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather | 2350 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather |
| 2328 // than having just been brought down, then this is wrong, because the | 2351 // than having just been brought down, then this is wrong, because the |
| 2329 // server won't know about this entity until it processes the ACTION_ADD our | 2352 // server won't know about this entity until it processes the ACTION_ADD our |
| 2330 // caller will later generate; but in this case, PruneSyncChanges() will | 2353 // caller will later generate; but in this case, PruneSyncChanges() will |
| 2331 // prune out the ACTION_UPDATE we create here. | 2354 // prune out the ACTION_UPDATE we create here. |
| 2332 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2355 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 2333 change_list->push_back( | 2356 change_list->push_back(syncer::SyncChange(FROM_HERE, |
| 2334 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); | 2357 syncer::SyncChange::ACTION_UPDATE, |
| 2358 sync_data)); |
| 2335 } | 2359 } |
| 2336 return true; | 2360 return true; |
| 2337 } | 2361 } |
| 2338 | 2362 |
| 2339 TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( | 2363 TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( |
| 2340 const TemplateURL& sync_turl) { | 2364 const TemplateURL& sync_turl) { |
| 2341 TemplateURL* existing_turl = GetTemplateURLForKeyword(sync_turl.keyword()); | 2365 TemplateURL* existing_turl = GetTemplateURLForKeyword(sync_turl.keyword()); |
| 2342 return existing_turl && (existing_turl->url() == sync_turl.url()) ? | 2366 return existing_turl && (existing_turl->url() == sync_turl.url()) ? |
| 2343 existing_turl : NULL; | 2367 existing_turl : NULL; |
| 2344 } | 2368 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 2358 // TemplateURLID. We don't need to sync the new ID back to the server since | 2382 // TemplateURLID. We don't need to sync the new ID back to the server since |
| 2359 // it's only relevant locally. | 2383 // it's only relevant locally. |
| 2360 bool delete_default = (local_turl == GetDefaultSearchProvider()); | 2384 bool delete_default = (local_turl == GetDefaultSearchProvider()); |
| 2361 DCHECK(!delete_default || !is_default_search_managed_); | 2385 DCHECK(!delete_default || !is_default_search_managed_); |
| 2362 if (delete_default) | 2386 if (delete_default) |
| 2363 default_search_provider_ = NULL; | 2387 default_search_provider_ = NULL; |
| 2364 | 2388 |
| 2365 // See comments in ResolveSyncKeywordConflict() regarding generating an | 2389 // See comments in ResolveSyncKeywordConflict() regarding generating an |
| 2366 // ACTION_DELETE manually since Remove() won't do it. | 2390 // ACTION_DELETE manually since Remove() won't do it. |
| 2367 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2391 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 2368 change_list->push_back( | 2392 change_list->push_back(syncer::SyncChange(FROM_HERE, |
| 2369 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); | 2393 syncer::SyncChange::ACTION_DELETE, |
| 2394 sync_data)); |
| 2370 Remove(local_turl); | 2395 Remove(local_turl); |
| 2371 | 2396 |
| 2372 // Force the local ID to kInvalidTemplateURLID so we can add it. | 2397 // Force the local ID to kInvalidTemplateURLID so we can add it. |
| 2373 sync_turl->data_.id = kInvalidTemplateURLID; | 2398 sync_turl->data_.id = kInvalidTemplateURLID; |
| 2374 Add(scoped_sync_turl.release()); | 2399 Add(scoped_sync_turl.release()); |
| 2375 if (delete_default) | 2400 if (delete_default) |
| 2376 SetDefaultSearchProvider(sync_turl); | 2401 SetDefaultSearchProvider(sync_turl); |
| 2377 } else { | 2402 } else { |
| 2378 // Change the local TURL's GUID to the server's GUID and push an update to | 2403 // Change the local TURL's GUID to the server's GUID and push an update to |
| 2379 // Sync. This ensures that the rest of local_url's fields are sync'd up to | 2404 // Sync. This ensures that the rest of local_url's fields are sync'd up to |
| 2380 // the server, and the next time local_url is synced, it is recognized by | 2405 // the server, and the next time local_url is synced, it is recognized by |
| 2381 // having the same GUID. | 2406 // having the same GUID. |
| 2382 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); | 2407 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); |
| 2383 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2408 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 2384 change_list->push_back( | 2409 change_list->push_back(syncer::SyncChange(FROM_HERE, |
| 2385 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); | 2410 syncer::SyncChange::ACTION_UPDATE, |
| 2411 sync_data)); |
| 2386 } | 2412 } |
| 2387 } | 2413 } |
| 2388 | 2414 |
| 2389 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( | 2415 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( |
| 2390 const std::string& guid) { | 2416 const std::string& guid) { |
| 2391 // If we're not syncing or if default search is managed by policy, ignore. | 2417 // If we're not syncing or if default search is managed by policy, ignore. |
| 2392 if (!sync_processor_.get() || is_default_search_managed_) | 2418 if (!sync_processor_.get() || is_default_search_managed_) |
| 2393 return; | 2419 return; |
| 2394 | 2420 |
| 2395 PrefService* prefs = GetPrefs(); | 2421 PrefService* prefs = GetPrefs(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2428 // TODO(mpcomplete): If we allow editing extension keywords, then those | 2454 // TODO(mpcomplete): If we allow editing extension keywords, then those |
| 2429 // should be persisted to disk and synced. | 2455 // should be persisted to disk and synced. |
| 2430 if (template_url->sync_guid().empty() && | 2456 if (template_url->sync_guid().empty() && |
| 2431 !template_url->IsExtensionKeyword()) { | 2457 !template_url->IsExtensionKeyword()) { |
| 2432 template_url->data_.sync_guid = base::GenerateGUID(); | 2458 template_url->data_.sync_guid = base::GenerateGUID(); |
| 2433 if (service_.get()) | 2459 if (service_.get()) |
| 2434 service_->UpdateKeyword(template_url->data()); | 2460 service_->UpdateKeyword(template_url->data()); |
| 2435 } | 2461 } |
| 2436 } | 2462 } |
| 2437 } | 2463 } |
| OLD | NEW |