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 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1099 iter->second, &new_changes)); | 1099 iter->second, &new_changes)); |
1100 if (!sync_turl.get()) | 1100 if (!sync_turl.get()) |
1101 continue; | 1101 continue; |
1102 | 1102 |
1103 if (pre_sync_deletes_.find(sync_turl->sync_guid()) != | 1103 if (pre_sync_deletes_.find(sync_turl->sync_guid()) != |
1104 pre_sync_deletes_.end()) { | 1104 pre_sync_deletes_.end()) { |
1105 // This entry was deleted before the initial sync began (possibly through | 1105 // This entry was deleted before the initial sync began (possibly through |
1106 // preprocessing in TemplateURLService's loading code). Ignore it and send | 1106 // preprocessing in TemplateURLService's loading code). Ignore it and send |
1107 // an ACTION_DELETE up to the server. | 1107 // an ACTION_DELETE up to the server. |
1108 new_changes.push_back( | 1108 new_changes.push_back( |
1109 syncer::SyncChange(FROM_HERE, | 1109 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, |
1110 syncer::SyncChange::ACTION_DELETE, | |
1111 iter->second)); | 1110 iter->second)); |
1112 continue; | 1111 continue; |
1113 } | 1112 } |
1114 | 1113 |
1115 if (local_turl) { | 1114 if (local_turl) { |
1116 // This local search engine is already synced. If the timestamp differs | 1115 // This local search engine is already synced. If the timestamp differs |
1117 // from Sync, we need to update locally or to the cloud. Note that if the | 1116 // from Sync, we need to update locally or to the cloud. Note that if the |
1118 // timestamps are equal, we touch neither. | 1117 // timestamps are equal, we touch neither. |
1119 if (sync_turl->last_modified() > local_turl->last_modified()) { | 1118 if (sync_turl->last_modified() > local_turl->last_modified()) { |
1120 // We've received an update from Sync. We should replace all synced | 1119 // We've received an update from Sync. We should replace all synced |
1121 // fields in the local TemplateURL. Note that this includes the | 1120 // fields in the local TemplateURL. Note that this includes the |
1122 // TemplateURLID and the TemplateURL may have to be reparsed. This | 1121 // TemplateURLID and the TemplateURL may have to be reparsed. This |
1123 // also makes the local data's last_modified timestamp equal to Sync's, | 1122 // also makes the local data's last_modified timestamp equal to Sync's, |
1124 // avoiding an Update on the next MergeData call. | 1123 // avoiding an Update on the next MergeData call. |
1125 UIThreadSearchTermsData search_terms_data(local_turl->profile()); | 1124 UIThreadSearchTermsData search_terms_data(local_turl->profile()); |
1126 if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data)) | 1125 if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data)) |
1127 NotifyObservers(); | 1126 NotifyObservers(); |
1128 } else if (sync_turl->last_modified() < local_turl->last_modified()) { | 1127 } else if (sync_turl->last_modified() < local_turl->last_modified()) { |
1129 // Otherwise, we know we have newer data, so update Sync with our | 1128 // Otherwise, we know we have newer data, so update Sync with our |
1130 // data fields. | 1129 // data fields. |
1131 new_changes.push_back( | 1130 new_changes.push_back( |
1132 syncer::SyncChange(FROM_HERE, | 1131 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, |
1133 syncer::SyncChange::ACTION_UPDATE, | 1132 local_data_map[local_turl->sync_guid()])); |
1134 local_data_map[local_turl->sync_guid()])); | |
1135 } | 1133 } |
1136 local_data_map.erase(iter->first); | 1134 local_data_map.erase(iter->first); |
1137 } else { | 1135 } else { |
1138 // The search engine from the cloud has not been synced locally, but there | 1136 // The search engine from the cloud has not been synced locally, but there |
1139 // might be a local search engine that is a duplicate that needs to be | 1137 // might be a local search engine that is a duplicate that needs to be |
1140 // merged. | 1138 // merged. |
1141 TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl); | 1139 TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl); |
1142 if (dupe_turl) { | 1140 if (dupe_turl) { |
1143 // Merge duplicates and remove the processed local TURL from the map. | 1141 // Merge duplicates and remove the processed local TURL from the map. |
1144 std::string old_guid = dupe_turl->sync_guid(); | 1142 std::string old_guid = dupe_turl->sync_guid(); |
(...skipping 21 matching lines...) Expand all Loading... |
1166 } | 1164 } |
1167 } | 1165 } |
1168 } | 1166 } |
1169 } | 1167 } |
1170 | 1168 |
1171 // The remaining SyncData in local_data_map should be everything that needs to | 1169 // The remaining SyncData in local_data_map should be everything that needs to |
1172 // be pushed as ADDs to sync. | 1170 // be pushed as ADDs to sync. |
1173 for (SyncDataMap::const_iterator iter = local_data_map.begin(); | 1171 for (SyncDataMap::const_iterator iter = local_data_map.begin(); |
1174 iter != local_data_map.end(); ++iter) { | 1172 iter != local_data_map.end(); ++iter) { |
1175 new_changes.push_back( | 1173 new_changes.push_back( |
1176 syncer::SyncChange(FROM_HERE, | 1174 syncer::SyncChange(syncer::SyncChange::ACTION_ADD, iter->second)); |
1177 syncer::SyncChange::ACTION_ADD, | |
1178 iter->second)); | |
1179 } | 1175 } |
1180 | 1176 |
1181 // Do some post-processing on the change list to ensure that we are sending | 1177 // Do some post-processing on the change list to ensure that we are sending |
1182 // valid changes to sync_processor_. | 1178 // valid changes to sync_processor_. |
1183 PruneSyncChanges(&sync_data_map, &new_changes); | 1179 PruneSyncChanges(&sync_data_map, &new_changes); |
1184 | 1180 |
1185 syncer::SyncError error = | 1181 syncer::SyncError error = |
1186 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 1182 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
1187 if (error.IsSet()) | 1183 if (error.IsSet()) |
1188 return error; | 1184 return error; |
1189 | 1185 |
1190 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to | 1186 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to |
1191 // reuse them on the next call to MergeDataAndStartSyncing. | 1187 // reuse them on the next call to MergeDataAndStartSyncing. |
1192 pre_sync_deletes_.clear(); | 1188 pre_sync_deletes_.clear(); |
1193 | 1189 |
1194 models_associated_ = true; | 1190 models_associated_ = true; |
1195 return syncer::SyncError(); | 1191 return syncer::SyncError(); |
1196 } | 1192 } |
1197 | 1193 |
1198 void TemplateURLService::StopSyncing(syncer::ModelType type) { | 1194 void TemplateURLService::StopSyncing(syncer::ModelType type) { |
1199 DCHECK_EQ(type, syncer::SEARCH_ENGINES); | 1195 DCHECK_EQ(type, syncer::SEARCH_ENGINES); |
1200 models_associated_ = false; | 1196 models_associated_ = false; |
1201 sync_processor_.reset(); | 1197 sync_processor_.reset(); |
1202 sync_error_factory_.reset(); | 1198 sync_error_factory_.reset(); |
1203 } | 1199 } |
1204 | 1200 |
1205 void TemplateURLService::ProcessTemplateURLChange( | 1201 void TemplateURLService::ProcessTemplateURLChange( |
1206 const tracked_objects::Location& from_here, | |
1207 const TemplateURL* turl, | 1202 const TemplateURL* turl, |
1208 syncer::SyncChange::SyncChangeType type) { | 1203 syncer::SyncChange::SyncChangeType type) { |
1209 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID); | 1204 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID); |
1210 DCHECK(turl); | 1205 DCHECK(turl); |
1211 | 1206 |
1212 if (!models_associated_) | 1207 if (!models_associated_) |
1213 return; // Not syncing. | 1208 return; // Not syncing. |
1214 | 1209 |
1215 if (processing_syncer_changes_) | 1210 if (processing_syncer_changes_) |
1216 return; // These are changes originating from us. Ignore. | 1211 return; // These are changes originating from us. Ignore. |
1217 | 1212 |
1218 // Avoid syncing Extension keywords. | 1213 // Avoid syncing Extension keywords. |
1219 // TODO(mpcomplete): If we allow editing extension keywords, then those should | 1214 // TODO(mpcomplete): If we allow editing extension keywords, then those should |
1220 // be persisted to disk and synced. | 1215 // be persisted to disk and synced. |
1221 if (turl->IsExtensionKeyword()) | 1216 if (turl->IsExtensionKeyword()) |
1222 return; | 1217 return; |
1223 | 1218 |
1224 // Avoid syncing keywords managed by policy. | 1219 // Avoid syncing keywords managed by policy. |
1225 if (turl->created_by_policy()) | 1220 if (turl->created_by_policy()) |
1226 return; | 1221 return; |
1227 | 1222 |
1228 syncer::SyncChangeList changes; | 1223 syncer::SyncChangeList changes; |
1229 | 1224 |
1230 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1225 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
1231 changes.push_back(syncer::SyncChange(from_here, | 1226 changes.push_back(syncer::SyncChange(type, sync_data)); |
1232 type, | |
1233 sync_data)); | |
1234 | 1227 |
1235 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 1228 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
1236 } | 1229 } |
1237 | 1230 |
1238 // static | 1231 // static |
1239 syncer::SyncData TemplateURLService::CreateSyncDataFromTemplateURL( | 1232 syncer::SyncData TemplateURLService::CreateSyncDataFromTemplateURL( |
1240 const TemplateURL& turl) { | 1233 const TemplateURL& turl) { |
1241 sync_pb::EntitySpecifics specifics; | 1234 sync_pb::EntitySpecifics specifics; |
1242 sync_pb::SearchEngineSpecifics* se_specifics = | 1235 sync_pb::SearchEngineSpecifics* se_specifics = |
1243 specifics.mutable_search_engine(); | 1236 specifics.mutable_search_engine(); |
(...skipping 24 matching lines...) Expand all Loading... |
1268 syncer::SyncChangeList* change_list) { | 1261 syncer::SyncChangeList* change_list) { |
1269 DCHECK(change_list); | 1262 DCHECK(change_list); |
1270 | 1263 |
1271 sync_pb::SearchEngineSpecifics specifics = | 1264 sync_pb::SearchEngineSpecifics specifics = |
1272 sync_data.GetSpecifics().search_engine(); | 1265 sync_data.GetSpecifics().search_engine(); |
1273 | 1266 |
1274 // Past bugs might have caused either of these fields to be empty. Just | 1267 // Past bugs might have caused either of these fields to be empty. Just |
1275 // delete this data off the server. | 1268 // delete this data off the server. |
1276 if (specifics.url().empty() || specifics.sync_guid().empty()) { | 1269 if (specifics.url().empty() || specifics.sync_guid().empty()) { |
1277 change_list->push_back( | 1270 change_list->push_back( |
1278 syncer::SyncChange(FROM_HERE, | 1271 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); |
1279 syncer::SyncChange::ACTION_DELETE, | |
1280 sync_data)); | |
1281 return NULL; | 1272 return NULL; |
1282 } | 1273 } |
1283 | 1274 |
1284 TemplateURLData data(existing_turl ? | 1275 TemplateURLData data(existing_turl ? |
1285 existing_turl->data() : TemplateURLData()); | 1276 existing_turl->data() : TemplateURLData()); |
1286 data.short_name = UTF8ToUTF16(specifics.short_name()); | 1277 data.short_name = UTF8ToUTF16(specifics.short_name()); |
1287 data.originating_url = GURL(specifics.originating_url()); | 1278 data.originating_url = GURL(specifics.originating_url()); |
1288 string16 keyword(UTF8ToUTF16(specifics.keyword())); | 1279 string16 keyword(UTF8ToUTF16(specifics.keyword())); |
1289 // NOTE: Once this code has shipped in a couple of stable releases, we can | 1280 // NOTE: Once this code has shipped in a couple of stable releases, we can |
1290 // probably remove the migration portion, comment out the | 1281 // probably remove the migration portion, comment out the |
(...skipping 22 matching lines...) Expand all Loading... |
1313 data.prepopulate_id = specifics.prepopulate_id(); | 1304 data.prepopulate_id = specifics.prepopulate_id(); |
1314 data.sync_guid = specifics.sync_guid(); | 1305 data.sync_guid = specifics.sync_guid(); |
1315 | 1306 |
1316 TemplateURL* turl = new TemplateURL(profile, data); | 1307 TemplateURL* turl = new TemplateURL(profile, data); |
1317 DCHECK(!turl->IsExtensionKeyword()); | 1308 DCHECK(!turl->IsExtensionKeyword()); |
1318 if (reset_keyword || deduped) { | 1309 if (reset_keyword || deduped) { |
1319 if (reset_keyword) | 1310 if (reset_keyword) |
1320 turl->ResetKeywordIfNecessary(true); | 1311 turl->ResetKeywordIfNecessary(true); |
1321 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1312 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
1322 change_list->push_back( | 1313 change_list->push_back( |
1323 syncer::SyncChange(FROM_HERE, | 1314 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); |
1324 syncer::SyncChange::ACTION_UPDATE, | |
1325 sync_data)); | |
1326 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { | 1315 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { |
1327 if (!existing_turl) { | 1316 if (!existing_turl) { |
1328 // We're adding a new TemplateURL that uses the Google base URL, so set | 1317 // We're adding a new TemplateURL that uses the Google base URL, so set |
1329 // its keyword appropriately for the local environment. | 1318 // its keyword appropriately for the local environment. |
1330 turl->ResetKeywordIfNecessary(false); | 1319 turl->ResetKeywordIfNecessary(false); |
1331 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) { | 1320 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) { |
1332 // Ignore keyword changes triggered by the Google base URL changing on | 1321 // Ignore keyword changes triggered by the Google base URL changing on |
1333 // another client. If the base URL changes in this client as well, we'll | 1322 // another client. If the base URL changes in this client as well, we'll |
1334 // pick that up separately at the appropriate time. Otherwise, changing | 1323 // pick that up separately at the appropriate time. Otherwise, changing |
1335 // the keyword here could result in having the wrong keyword for the local | 1324 // 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... |
1771 } | 1760 } |
1772 } | 1761 } |
1773 } | 1762 } |
1774 if (!existing_turl->sync_guid().empty()) | 1763 if (!existing_turl->sync_guid().empty()) |
1775 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; | 1764 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; |
1776 | 1765 |
1777 if (service_.get()) | 1766 if (service_.get()) |
1778 service_->UpdateKeyword(existing_turl->data()); | 1767 service_->UpdateKeyword(existing_turl->data()); |
1779 | 1768 |
1780 // Inform sync of the update. | 1769 // Inform sync of the update. |
1781 ProcessTemplateURLChange(FROM_HERE, | 1770 ProcessTemplateURLChange(existing_turl, syncer::SyncChange::ACTION_UPDATE); |
1782 existing_turl, | |
1783 syncer::SyncChange::ACTION_UPDATE); | |
1784 | 1771 |
1785 if (default_search_provider_ == existing_turl) { | 1772 if (default_search_provider_ == existing_turl) { |
1786 bool success = SetDefaultSearchProviderNoNotify(existing_turl); | 1773 bool success = SetDefaultSearchProviderNoNotify(existing_turl); |
1787 DCHECK(success); | 1774 DCHECK(success); |
1788 } | 1775 } |
1789 return true; | 1776 return true; |
1790 } | 1777 } |
1791 | 1778 |
1792 PrefService* TemplateURLService::GetPrefs() { | 1779 PrefService* TemplateURLService::GetPrefs() { |
1793 return profile_ ? profile_->GetPrefs() : NULL; | 1780 return profile_ ? profile_->GetPrefs() : NULL; |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2104 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, | 2091 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, |
2105 url->sync_guid()); | 2092 url->sync_guid()); |
2106 } | 2093 } |
2107 } | 2094 } |
2108 | 2095 |
2109 if (service_.get()) | 2096 if (service_.get()) |
2110 service_->SetDefaultSearchProvider(url); | 2097 service_->SetDefaultSearchProvider(url); |
2111 | 2098 |
2112 // Inform sync the change to the show_in_default_list flag. | 2099 // Inform sync the change to the show_in_default_list flag. |
2113 if (url) | 2100 if (url) |
2114 ProcessTemplateURLChange(FROM_HERE, | 2101 ProcessTemplateURLChange(url, syncer::SyncChange::ACTION_UPDATE); |
2115 url, | |
2116 syncer::SyncChange::ACTION_UPDATE); | |
2117 return true; | 2102 return true; |
2118 } | 2103 } |
2119 | 2104 |
2120 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, | 2105 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, |
2121 bool newly_adding) { | 2106 bool newly_adding) { |
2122 DCHECK(template_url); | 2107 DCHECK(template_url); |
2123 | 2108 |
2124 if (newly_adding) { | 2109 if (newly_adding) { |
2125 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 2110 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
2126 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), | 2111 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), |
(...skipping 28 matching lines...) Expand all Loading... |
2155 if (newly_adding) { | 2140 if (newly_adding) { |
2156 // Don't persist extension keywords to disk. They'll get re-added on each | 2141 // Don't persist extension keywords to disk. They'll get re-added on each |
2157 // launch as the extensions are loaded. | 2142 // launch as the extensions are loaded. |
2158 // TODO(mpcomplete): If we allow editing extension keywords, then those | 2143 // TODO(mpcomplete): If we allow editing extension keywords, then those |
2159 // should be persisted to disk and synced. | 2144 // should be persisted to disk and synced. |
2160 if (service_.get() && !template_url->IsExtensionKeyword()) | 2145 if (service_.get() && !template_url->IsExtensionKeyword()) |
2161 service_->AddKeyword(template_url->data()); | 2146 service_->AddKeyword(template_url->data()); |
2162 | 2147 |
2163 // Inform sync of the addition. Note that this will assign a GUID to | 2148 // Inform sync of the addition. Note that this will assign a GUID to |
2164 // template_url and add it to the guid_to_template_map_. | 2149 // template_url and add it to the guid_to_template_map_. |
2165 ProcessTemplateURLChange(FROM_HERE, | 2150 ProcessTemplateURLChange(template_url, syncer::SyncChange::ACTION_ADD); |
2166 template_url, | |
2167 syncer::SyncChange::ACTION_ADD); | |
2168 } | 2151 } |
2169 | 2152 |
2170 return true; | 2153 return true; |
2171 } | 2154 } |
2172 | 2155 |
2173 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { | 2156 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { |
2174 TemplateURLVector::iterator i = | 2157 TemplateURLVector::iterator i = |
2175 std::find(template_urls_.begin(), template_urls_.end(), template_url); | 2158 std::find(template_urls_.begin(), template_urls_.end(), template_url); |
2176 if (i == template_urls_.end()) | 2159 if (i == template_urls_.end()) |
2177 return; | 2160 return; |
2178 | 2161 |
2179 if (template_url == default_search_provider_) { | 2162 if (template_url == default_search_provider_) { |
2180 // Should never delete the default search provider. | 2163 // Should never delete the default search provider. |
2181 NOTREACHED(); | 2164 NOTREACHED(); |
2182 return; | 2165 return; |
2183 } | 2166 } |
2184 | 2167 |
2185 RemoveFromMaps(template_url); | 2168 RemoveFromMaps(template_url); |
2186 | 2169 |
2187 // Remove it from the vector containing all TemplateURLs. | 2170 // Remove it from the vector containing all TemplateURLs. |
2188 template_urls_.erase(i); | 2171 template_urls_.erase(i); |
2189 | 2172 |
2190 // Extension keywords are not persisted. | 2173 // Extension keywords are not persisted. |
2191 // TODO(mpcomplete): If we allow editing extension keywords, then those should | 2174 // TODO(mpcomplete): If we allow editing extension keywords, then those should |
2192 // be persisted to disk and synced. | 2175 // be persisted to disk and synced. |
2193 if (service_.get() && !template_url->IsExtensionKeyword()) | 2176 if (service_.get() && !template_url->IsExtensionKeyword()) |
2194 service_->RemoveKeyword(template_url->id()); | 2177 service_->RemoveKeyword(template_url->id()); |
2195 | 2178 |
2196 // Inform sync of the deletion. | 2179 // Inform sync of the deletion. |
2197 ProcessTemplateURLChange(FROM_HERE, | 2180 ProcessTemplateURLChange(template_url, syncer::SyncChange::ACTION_DELETE); |
2198 template_url, | |
2199 syncer::SyncChange::ACTION_DELETE); | |
2200 | 2181 |
2201 if (profile_) { | 2182 if (profile_) { |
2202 content::Source<Profile> source(profile_); | 2183 content::Source<Profile> source(profile_); |
2203 TemplateURLID id = template_url->id(); | 2184 TemplateURLID id = template_url->id(); |
2204 content::NotificationService::current()->Notify( | 2185 content::NotificationService::current()->Notify( |
2205 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, | 2186 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, |
2206 source, | 2187 source, |
2207 content::Details<TemplateURLID>(&id)); | 2188 content::Details<TemplateURLID>(&id)); |
2208 } | 2189 } |
2209 | 2190 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2319 DCHECK(!local_turl->IsExtensionKeyword()); | 2300 DCHECK(!local_turl->IsExtensionKeyword()); |
2320 DCHECK(change_list); | 2301 DCHECK(change_list); |
2321 | 2302 |
2322 const bool local_is_better = | 2303 const bool local_is_better = |
2323 (local_turl->last_modified() > sync_turl->last_modified()) || | 2304 (local_turl->last_modified() > sync_turl->last_modified()) || |
2324 local_turl->created_by_policy() || | 2305 local_turl->created_by_policy() || |
2325 (local_turl == GetDefaultSearchProvider()); | 2306 (local_turl == GetDefaultSearchProvider()); |
2326 const bool can_replace_local = CanReplace(local_turl); | 2307 const bool can_replace_local = CanReplace(local_turl); |
2327 if (CanReplace(sync_turl) && (local_is_better || !can_replace_local)) { | 2308 if (CanReplace(sync_turl) && (local_is_better || !can_replace_local)) { |
2328 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 2309 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
2329 change_list->push_back(syncer::SyncChange(FROM_HERE, | 2310 change_list->push_back( |
2330 syncer::SyncChange::ACTION_DELETE, | 2311 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); |
2331 sync_data)); | |
2332 return false; | 2312 return false; |
2333 } | 2313 } |
2334 if (can_replace_local) { | 2314 if (can_replace_local) { |
2335 // Since we're processing sync changes, the upcoming Remove() won't generate | 2315 // Since we're processing sync changes, the upcoming Remove() won't generate |
2336 // an ACTION_DELETE. We need to do it manually to keep the server in sync | 2316 // an ACTION_DELETE. We need to do it manually to keep the server in sync |
2337 // with us. Note that if we're being called from | 2317 // with us. Note that if we're being called from |
2338 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather | 2318 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather |
2339 // than having just been brought down, then this is wrong, because the | 2319 // than having just been brought down, then this is wrong, because the |
2340 // server doesn't yet know about this entity; but in this case, | 2320 // server doesn't yet know about this entity; but in this case, |
2341 // PruneSyncChanges() will prune out the ACTION_DELETE we create here. | 2321 // PruneSyncChanges() will prune out the ACTION_DELETE we create here. |
2342 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2322 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
2343 change_list->push_back( | 2323 change_list->push_back( |
2344 syncer::SyncChange(FROM_HERE, | 2324 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); |
2345 syncer::SyncChange::ACTION_DELETE, | |
2346 sync_data)); | |
2347 Remove(local_turl); | 2325 Remove(local_turl); |
2348 } else if (local_is_better) { | 2326 } else if (local_is_better) { |
2349 string16 new_keyword = UniquifyKeyword(*sync_turl, false); | 2327 string16 new_keyword = UniquifyKeyword(*sync_turl, false); |
2350 DCHECK(!GetTemplateURLForKeyword(new_keyword)); | 2328 DCHECK(!GetTemplateURLForKeyword(new_keyword)); |
2351 sync_turl->data_.SetKeyword(new_keyword); | 2329 sync_turl->data_.SetKeyword(new_keyword); |
2352 // If we update the cloud TURL, we need to push an update back to sync | 2330 // If we update the cloud TURL, we need to push an update back to sync |
2353 // informing it that something has changed. | 2331 // informing it that something has changed. |
2354 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 2332 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
2355 change_list->push_back(syncer::SyncChange(FROM_HERE, | 2333 change_list->push_back( |
2356 syncer::SyncChange::ACTION_UPDATE, | 2334 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); |
2357 sync_data)); | |
2358 } else { | 2335 } else { |
2359 string16 new_keyword = UniquifyKeyword(*local_turl, false); | 2336 string16 new_keyword = UniquifyKeyword(*local_turl, false); |
2360 TemplateURLData data(local_turl->data()); | 2337 TemplateURLData data(local_turl->data()); |
2361 data.SetKeyword(new_keyword); | 2338 data.SetKeyword(new_keyword); |
2362 TemplateURL new_turl(local_turl->profile(), data); | 2339 TemplateURL new_turl(local_turl->profile(), data); |
2363 UIThreadSearchTermsData search_terms_data(local_turl->profile()); | 2340 UIThreadSearchTermsData search_terms_data(local_turl->profile()); |
2364 if (UpdateNoNotify(local_turl, new_turl, search_terms_data)) | 2341 if (UpdateNoNotify(local_turl, new_turl, search_terms_data)) |
2365 NotifyObservers(); | 2342 NotifyObservers(); |
2366 // Since we're processing sync changes, the UpdateNoNotify() above didn't | 2343 // Since we're processing sync changes, the UpdateNoNotify() above didn't |
2367 // generate an ACTION_UPDATE. We need to do it manually to keep the server | 2344 // generate an ACTION_UPDATE. We need to do it manually to keep the server |
2368 // in sync with us. Note that if we're being called from | 2345 // in sync with us. Note that if we're being called from |
2369 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather | 2346 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather |
2370 // than having just been brought down, then this is wrong, because the | 2347 // than having just been brought down, then this is wrong, because the |
2371 // server won't know about this entity until it processes the ACTION_ADD our | 2348 // server won't know about this entity until it processes the ACTION_ADD our |
2372 // caller will later generate; but in this case, PruneSyncChanges() will | 2349 // caller will later generate; but in this case, PruneSyncChanges() will |
2373 // prune out the ACTION_UPDATE we create here. | 2350 // prune out the ACTION_UPDATE we create here. |
2374 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2351 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
2375 change_list->push_back(syncer::SyncChange(FROM_HERE, | 2352 change_list->push_back( |
2376 syncer::SyncChange::ACTION_UPDATE, | 2353 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); |
2377 sync_data)); | |
2378 } | 2354 } |
2379 return true; | 2355 return true; |
2380 } | 2356 } |
2381 | 2357 |
2382 TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( | 2358 TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( |
2383 const TemplateURL& sync_turl) { | 2359 const TemplateURL& sync_turl) { |
2384 TemplateURL* existing_turl = GetTemplateURLForKeyword(sync_turl.keyword()); | 2360 TemplateURL* existing_turl = GetTemplateURLForKeyword(sync_turl.keyword()); |
2385 return existing_turl && (existing_turl->url() == sync_turl.url()) ? | 2361 return existing_turl && (existing_turl->url() == sync_turl.url()) ? |
2386 existing_turl : NULL; | 2362 existing_turl : NULL; |
2387 } | 2363 } |
(...skipping 13 matching lines...) Expand all Loading... |
2401 // TemplateURLID. We don't need to sync the new ID back to the server since | 2377 // TemplateURLID. We don't need to sync the new ID back to the server since |
2402 // it's only relevant locally. | 2378 // it's only relevant locally. |
2403 bool delete_default = (local_turl == GetDefaultSearchProvider()); | 2379 bool delete_default = (local_turl == GetDefaultSearchProvider()); |
2404 DCHECK(!delete_default || !is_default_search_managed_); | 2380 DCHECK(!delete_default || !is_default_search_managed_); |
2405 if (delete_default) | 2381 if (delete_default) |
2406 default_search_provider_ = NULL; | 2382 default_search_provider_ = NULL; |
2407 | 2383 |
2408 // See comments in ResolveSyncKeywordConflict() regarding generating an | 2384 // See comments in ResolveSyncKeywordConflict() regarding generating an |
2409 // ACTION_DELETE manually since Remove() won't do it. | 2385 // ACTION_DELETE manually since Remove() won't do it. |
2410 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2386 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
2411 change_list->push_back(syncer::SyncChange(FROM_HERE, | 2387 change_list->push_back( |
2412 syncer::SyncChange::ACTION_DELETE, | 2388 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); |
2413 sync_data)); | |
2414 Remove(local_turl); | 2389 Remove(local_turl); |
2415 | 2390 |
2416 // Force the local ID to kInvalidTemplateURLID so we can add it. | 2391 // Force the local ID to kInvalidTemplateURLID so we can add it. |
2417 sync_turl->data_.id = kInvalidTemplateURLID; | 2392 sync_turl->data_.id = kInvalidTemplateURLID; |
2418 Add(scoped_sync_turl.release()); | 2393 Add(scoped_sync_turl.release()); |
2419 if (delete_default) | 2394 if (delete_default) |
2420 SetDefaultSearchProvider(sync_turl); | 2395 SetDefaultSearchProvider(sync_turl); |
2421 } else { | 2396 } else { |
2422 // Change the local TURL's GUID to the server's GUID and push an update to | 2397 // Change the local TURL's GUID to the server's GUID and push an update to |
2423 // Sync. This ensures that the rest of local_url's fields are sync'd up to | 2398 // Sync. This ensures that the rest of local_url's fields are sync'd up to |
2424 // the server, and the next time local_url is synced, it is recognized by | 2399 // the server, and the next time local_url is synced, it is recognized by |
2425 // having the same GUID. | 2400 // having the same GUID. |
2426 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); | 2401 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); |
2427 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2402 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
2428 change_list->push_back(syncer::SyncChange(FROM_HERE, | 2403 change_list->push_back( |
2429 syncer::SyncChange::ACTION_UPDATE, | 2404 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); |
2430 sync_data)); | |
2431 } | 2405 } |
2432 } | 2406 } |
2433 | 2407 |
2434 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( | 2408 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( |
2435 const std::string& guid) { | 2409 const std::string& guid) { |
2436 // If we're not syncing or if default search is managed by policy, ignore. | 2410 // If we're not syncing or if default search is managed by policy, ignore. |
2437 if (!sync_processor_.get() || is_default_search_managed_) | 2411 if (!sync_processor_.get() || is_default_search_managed_) |
2438 return; | 2412 return; |
2439 | 2413 |
2440 PrefService* prefs = GetPrefs(); | 2414 PrefService* prefs = GetPrefs(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2473 // TODO(mpcomplete): If we allow editing extension keywords, then those | 2447 // TODO(mpcomplete): If we allow editing extension keywords, then those |
2474 // should be persisted to disk and synced. | 2448 // should be persisted to disk and synced. |
2475 if (template_url->sync_guid().empty() && | 2449 if (template_url->sync_guid().empty() && |
2476 !template_url->IsExtensionKeyword()) { | 2450 !template_url->IsExtensionKeyword()) { |
2477 template_url->data_.sync_guid = base::GenerateGUID(); | 2451 template_url->data_.sync_guid = base::GenerateGUID(); |
2478 if (service_.get()) | 2452 if (service_.get()) |
2479 service_->UpdateKeyword(template_url->data()); | 2453 service_->UpdateKeyword(template_url->data()); |
2480 } | 2454 } |
2481 } | 2455 } |
2482 } | 2456 } |
OLD | NEW |