| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 SyncChangeList* change_list, | 113 csync::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 SyncChange& change_i = (*change_list)[index]; | 116 const csync::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 SyncChange::SyncChangeType type = change_i.change_type(); | 119 csync::SyncChange::SyncChangeType type = change_i.change_type(); |
| 120 if ((type == SyncChange::ACTION_UPDATE || | 120 if ((type == csync::SyncChange::ACTION_UPDATE || |
| 121 type == SyncChange::ACTION_DELETE) && | 121 type == csync::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 == SyncChange::ACTION_ADD && | 124 if (type == csync::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 == SyncChange::ACTION_UPDATE) { | 127 if (type == csync::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 SyncChange& change_j = (*change_list)[j]; | 129 const csync::SyncChange& change_j = (*change_list)[j]; |
| 130 if ((SyncChange::ACTION_UPDATE == change_j.change_type()) && | 130 if ((csync::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 SyncChanges added by the merge and | 140 // This is done to eliminate incorrect csync::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 SyncChangeList* change_list) { | 145 csync::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 Loading... |
| 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 SyncDataList TemplateURLService::GetAllSyncData( | 866 csync::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 SyncDataList current_data; | 870 csync::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 SyncError TemplateURLService::ProcessSyncChanges( | 887 csync::SyncError TemplateURLService::ProcessSyncChanges( |
| 888 const tracked_objects::Location& from_here, | 888 const tracked_objects::Location& from_here, |
| 889 const SyncChangeList& change_list) { | 889 const csync::SyncChangeList& change_list) { |
| 890 if (!models_associated_) { | 890 if (!models_associated_) { |
| 891 SyncError error(FROM_HERE, "Models not yet associated.", | 891 csync::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 SyncChangeList new_changes; | 905 csync::SyncChangeList new_changes; |
| 906 SyncError error; | 906 csync::SyncError error; |
| 907 for (SyncChangeList::const_iterator iter = change_list.begin(); | 907 for (csync::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() == SyncChange::ACTION_DELETE) { | 926 if (iter->change_type() == csync::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() == SyncChange::ACTION_ADD) { | 951 } else if (iter->change_type() == csync::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() == SyncChange::ACTION_UPDATE) { | 971 } else if (iter->change_type() == csync::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 Loading... |
| 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 SyncError TemplateURLService::MergeDataAndStartSyncing( | 1013 csync::SyncError TemplateURLService::MergeDataAndStartSyncing( |
| 1014 syncable::ModelType type, | 1014 syncable::ModelType type, |
| 1015 const SyncDataList& initial_sync_data, | 1015 const csync::SyncDataList& initial_sync_data, |
| 1016 scoped_ptr<SyncChangeProcessor> sync_processor, | 1016 scoped_ptr<csync::SyncChangeProcessor> sync_processor, |
| 1017 scoped_ptr<SyncErrorFactory> sync_error_factory) { | 1017 scoped_ptr<csync::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 Loading... |
| 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 SyncChangeList new_changes; | 1048 csync::SyncChangeList new_changes; |
| 1049 | 1049 |
| 1050 // Build maps of our sync GUIDs to SyncData. | 1050 // Build maps of our sync GUIDs to csync::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(SyncChange(SyncChange::ACTION_DELETE, | 1069 new_changes.push_back(csync::SyncChange(csync::SyncChange::ACTION_DELETE, |
| 1070 iter->second)); | 1070 iter->second)); |
| 1071 continue; | 1071 continue; |
| 1072 } | 1072 } |
| 1073 | 1073 |
| 1074 if (local_turl) { | 1074 if (local_turl) { |
| 1075 // This local search engine is already synced. If the timestamp differs | 1075 // 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 | 1076 // from Sync, we need to update locally or to the cloud. Note that if the |
| 1077 // timestamps are equal, we touch neither. | 1077 // timestamps are equal, we touch neither. |
| 1078 if (sync_turl->last_modified() > local_turl->last_modified()) { | 1078 if (sync_turl->last_modified() > local_turl->last_modified()) { |
| 1079 // We've received an update from Sync. We should replace all synced | 1079 // We've received an update from Sync. We should replace all synced |
| 1080 // fields in the local TemplateURL. Note that this includes the | 1080 // fields in the local TemplateURL. Note that this includes the |
| 1081 // TemplateURLID and the TemplateURL may have to be reparsed. This | 1081 // TemplateURLID and the TemplateURL may have to be reparsed. This |
| 1082 // also makes the local data's last_modified timestamp equal to Sync's, | 1082 // also makes the local data's last_modified timestamp equal to Sync's, |
| 1083 // avoiding an Update on the next MergeData call. | 1083 // avoiding an Update on the next MergeData call. |
| 1084 UIThreadSearchTermsData search_terms_data(local_turl->profile()); | 1084 UIThreadSearchTermsData search_terms_data(local_turl->profile()); |
| 1085 if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data)) | 1085 if (UpdateNoNotify(local_turl, *sync_turl, search_terms_data)) |
| 1086 NotifyObservers(); | 1086 NotifyObservers(); |
| 1087 } else if (sync_turl->last_modified() < local_turl->last_modified()) { | 1087 } else if (sync_turl->last_modified() < local_turl->last_modified()) { |
| 1088 // Otherwise, we know we have newer data, so update Sync with our | 1088 // Otherwise, we know we have newer data, so update Sync with our |
| 1089 // data fields. | 1089 // data fields. |
| 1090 new_changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, | 1090 new_changes.push_back( |
| 1091 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, |
| 1091 local_data_map[local_turl->sync_guid()])); | 1092 local_data_map[local_turl->sync_guid()])); |
| 1092 } | 1093 } |
| 1093 local_data_map.erase(iter->first); | 1094 local_data_map.erase(iter->first); |
| 1094 } else { | 1095 } else { |
| 1095 // The search engine from the cloud has not been synced locally, but there | 1096 // The search engine from the cloud has not been synced locally, but there |
| 1096 // might be a local search engine that is a duplicate that needs to be | 1097 // might be a local search engine that is a duplicate that needs to be |
| 1097 // merged. | 1098 // merged. |
| 1098 TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl); | 1099 TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl); |
| 1099 if (dupe_turl) { | 1100 if (dupe_turl) { |
| 1100 // Merge duplicates and remove the processed local TURL from the map. | 1101 // Merge duplicates and remove the processed local TURL from the map. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1122 SetDefaultSearchProviderIfNewlySynced(guid); | 1123 SetDefaultSearchProviderIfNewlySynced(guid); |
| 1123 } | 1124 } |
| 1124 } | 1125 } |
| 1125 } | 1126 } |
| 1126 } | 1127 } |
| 1127 | 1128 |
| 1128 // The remaining SyncData in local_data_map should be everything that needs to | 1129 // The remaining SyncData in local_data_map should be everything that needs to |
| 1129 // be pushed as ADDs to sync. | 1130 // be pushed as ADDs to sync. |
| 1130 for (SyncDataMap::const_iterator iter = local_data_map.begin(); | 1131 for (SyncDataMap::const_iterator iter = local_data_map.begin(); |
| 1131 iter != local_data_map.end(); ++iter) { | 1132 iter != local_data_map.end(); ++iter) { |
| 1132 new_changes.push_back(SyncChange(SyncChange::ACTION_ADD, iter->second)); | 1133 new_changes.push_back( |
| 1134 csync::SyncChange(csync::SyncChange::ACTION_ADD, iter->second)); |
| 1133 } | 1135 } |
| 1134 | 1136 |
| 1135 // Do some post-processing on the change list to ensure that we are sending | 1137 // Do some post-processing on the change list to ensure that we are sending |
| 1136 // valid changes to sync_processor_. | 1138 // valid changes to sync_processor_. |
| 1137 PruneSyncChanges(&sync_data_map, &new_changes); | 1139 PruneSyncChanges(&sync_data_map, &new_changes); |
| 1138 | 1140 |
| 1139 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 1141 csync::SyncError error = |
| 1142 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
| 1140 if (error.IsSet()) | 1143 if (error.IsSet()) |
| 1141 return error; | 1144 return error; |
| 1142 | 1145 |
| 1143 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to | 1146 // The ACTION_DELETEs from this set are processed. Empty it so we don't try to |
| 1144 // reuse them on the next call to MergeDataAndStartSyncing. | 1147 // reuse them on the next call to MergeDataAndStartSyncing. |
| 1145 pre_sync_deletes_.clear(); | 1148 pre_sync_deletes_.clear(); |
| 1146 | 1149 |
| 1147 models_associated_ = true; | 1150 models_associated_ = true; |
| 1148 return SyncError(); | 1151 return csync::SyncError(); |
| 1149 } | 1152 } |
| 1150 | 1153 |
| 1151 void TemplateURLService::StopSyncing(syncable::ModelType type) { | 1154 void TemplateURLService::StopSyncing(syncable::ModelType type) { |
| 1152 DCHECK_EQ(type, syncable::SEARCH_ENGINES); | 1155 DCHECK_EQ(type, syncable::SEARCH_ENGINES); |
| 1153 models_associated_ = false; | 1156 models_associated_ = false; |
| 1154 sync_processor_.reset(); | 1157 sync_processor_.reset(); |
| 1155 sync_error_factory_.reset(); | 1158 sync_error_factory_.reset(); |
| 1156 } | 1159 } |
| 1157 | 1160 |
| 1158 void TemplateURLService::ProcessTemplateURLChange( | 1161 void TemplateURLService::ProcessTemplateURLChange( |
| 1159 const TemplateURL* turl, | 1162 const TemplateURL* turl, |
| 1160 SyncChange::SyncChangeType type) { | 1163 csync::SyncChange::SyncChangeType type) { |
| 1161 DCHECK_NE(type, SyncChange::ACTION_INVALID); | 1164 DCHECK_NE(type, csync::SyncChange::ACTION_INVALID); |
| 1162 DCHECK(turl); | 1165 DCHECK(turl); |
| 1163 | 1166 |
| 1164 if (!models_associated_) | 1167 if (!models_associated_) |
| 1165 return; // Not syncing. | 1168 return; // Not syncing. |
| 1166 | 1169 |
| 1167 if (processing_syncer_changes_) | 1170 if (processing_syncer_changes_) |
| 1168 return; // These are changes originating from us. Ignore. | 1171 return; // These are changes originating from us. Ignore. |
| 1169 | 1172 |
| 1170 // Avoid syncing Extension keywords. | 1173 // Avoid syncing Extension keywords. |
| 1171 // TODO(mpcomplete): If we allow editing extension keywords, then those should | 1174 // TODO(mpcomplete): If we allow editing extension keywords, then those should |
| 1172 // be persisted to disk and synced. | 1175 // be persisted to disk and synced. |
| 1173 if (turl->IsExtensionKeyword()) | 1176 if (turl->IsExtensionKeyword()) |
| 1174 return; | 1177 return; |
| 1175 | 1178 |
| 1176 // Avoid syncing keywords managed by policy. | 1179 // Avoid syncing keywords managed by policy. |
| 1177 if (turl->created_by_policy()) | 1180 if (turl->created_by_policy()) |
| 1178 return; | 1181 return; |
| 1179 | 1182 |
| 1180 SyncChangeList changes; | 1183 csync::SyncChangeList changes; |
| 1181 | 1184 |
| 1182 SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1185 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
| 1183 changes.push_back(SyncChange(type, sync_data)); | 1186 changes.push_back(csync::SyncChange(type, sync_data)); |
| 1184 | 1187 |
| 1185 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 1188 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 1186 } | 1189 } |
| 1187 | 1190 |
| 1188 // static | 1191 // static |
| 1189 SyncData TemplateURLService::CreateSyncDataFromTemplateURL( | 1192 csync::SyncData TemplateURLService::CreateSyncDataFromTemplateURL( |
| 1190 const TemplateURL& turl) { | 1193 const TemplateURL& turl) { |
| 1191 sync_pb::EntitySpecifics specifics; | 1194 sync_pb::EntitySpecifics specifics; |
| 1192 sync_pb::SearchEngineSpecifics* se_specifics = | 1195 sync_pb::SearchEngineSpecifics* se_specifics = |
| 1193 specifics.mutable_search_engine(); | 1196 specifics.mutable_search_engine(); |
| 1194 se_specifics->set_short_name(UTF16ToUTF8(turl.short_name())); | 1197 se_specifics->set_short_name(UTF16ToUTF8(turl.short_name())); |
| 1195 se_specifics->set_keyword(UTF16ToUTF8(turl.keyword())); | 1198 se_specifics->set_keyword(UTF16ToUTF8(turl.keyword())); |
| 1196 se_specifics->set_favicon_url(turl.favicon_url().spec()); | 1199 se_specifics->set_favicon_url(turl.favicon_url().spec()); |
| 1197 se_specifics->set_url(turl.url()); | 1200 se_specifics->set_url(turl.url()); |
| 1198 se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace()); | 1201 se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace()); |
| 1199 se_specifics->set_originating_url(turl.originating_url().spec()); | 1202 se_specifics->set_originating_url(turl.originating_url().spec()); |
| 1200 se_specifics->set_date_created(turl.date_created().ToInternalValue()); | 1203 se_specifics->set_date_created(turl.date_created().ToInternalValue()); |
| 1201 se_specifics->set_input_encodings(JoinString(turl.input_encodings(), ';')); | 1204 se_specifics->set_input_encodings(JoinString(turl.input_encodings(), ';')); |
| 1202 se_specifics->set_show_in_default_list(turl.show_in_default_list()); | 1205 se_specifics->set_show_in_default_list(turl.show_in_default_list()); |
| 1203 se_specifics->set_suggestions_url(turl.suggestions_url()); | 1206 se_specifics->set_suggestions_url(turl.suggestions_url()); |
| 1204 se_specifics->set_prepopulate_id(turl.prepopulate_id()); | 1207 se_specifics->set_prepopulate_id(turl.prepopulate_id()); |
| 1205 se_specifics->set_instant_url(turl.instant_url()); | 1208 se_specifics->set_instant_url(turl.instant_url()); |
| 1206 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); | 1209 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); |
| 1207 se_specifics->set_sync_guid(turl.sync_guid()); | 1210 se_specifics->set_sync_guid(turl.sync_guid()); |
| 1208 return SyncData::CreateLocalData(se_specifics->sync_guid(), | 1211 return csync::SyncData::CreateLocalData(se_specifics->sync_guid(), |
| 1209 se_specifics->keyword(), | 1212 se_specifics->keyword(), |
| 1210 specifics); | 1213 specifics); |
| 1211 } | 1214 } |
| 1212 | 1215 |
| 1213 // static | 1216 // static |
| 1214 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( | 1217 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( |
| 1215 Profile* profile, | 1218 Profile* profile, |
| 1216 TemplateURL* existing_turl, | 1219 TemplateURL* existing_turl, |
| 1217 const SyncData& sync_data, | 1220 const csync::SyncData& sync_data, |
| 1218 SyncChangeList* change_list) { | 1221 csync::SyncChangeList* change_list) { |
| 1219 DCHECK(change_list); | 1222 DCHECK(change_list); |
| 1220 | 1223 |
| 1221 sync_pb::SearchEngineSpecifics specifics = | 1224 sync_pb::SearchEngineSpecifics specifics = |
| 1222 sync_data.GetSpecifics().search_engine(); | 1225 sync_data.GetSpecifics().search_engine(); |
| 1223 | 1226 |
| 1224 // Past bugs might have caused either of these fields to be empty. Just | 1227 // Past bugs might have caused either of these fields to be empty. Just |
| 1225 // delete this data off the server. | 1228 // delete this data off the server. |
| 1226 if (specifics.url().empty() || specifics.sync_guid().empty()) { | 1229 if (specifics.url().empty() || specifics.sync_guid().empty()) { |
| 1227 change_list->push_back(SyncChange(SyncChange::ACTION_DELETE, sync_data)); | 1230 change_list->push_back( |
| 1231 csync::SyncChange(csync::SyncChange::ACTION_DELETE, sync_data)); |
| 1228 return NULL; | 1232 return NULL; |
| 1229 } | 1233 } |
| 1230 | 1234 |
| 1231 TemplateURLData data(existing_turl ? | 1235 TemplateURLData data(existing_turl ? |
| 1232 existing_turl->data() : TemplateURLData()); | 1236 existing_turl->data() : TemplateURLData()); |
| 1233 data.short_name = UTF8ToUTF16(specifics.short_name()); | 1237 data.short_name = UTF8ToUTF16(specifics.short_name()); |
| 1234 data.originating_url = GURL(specifics.originating_url()); | 1238 data.originating_url = GURL(specifics.originating_url()); |
| 1235 string16 keyword(UTF8ToUTF16(specifics.keyword())); | 1239 string16 keyword(UTF8ToUTF16(specifics.keyword())); |
| 1236 // NOTE: Once this code has shipped in a couple of stable releases, we can | 1240 // NOTE: Once this code has shipped in a couple of stable releases, we can |
| 1237 // probably remove the migration portion, comment out the | 1241 // probably remove the migration portion, comment out the |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1252 base::SplitString(specifics.input_encodings(), ';', &data.input_encodings); | 1256 base::SplitString(specifics.input_encodings(), ';', &data.input_encodings); |
| 1253 data.date_created = base::Time::FromInternalValue(specifics.date_created()); | 1257 data.date_created = base::Time::FromInternalValue(specifics.date_created()); |
| 1254 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); | 1258 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); |
| 1255 data.prepopulate_id = specifics.prepopulate_id(); | 1259 data.prepopulate_id = specifics.prepopulate_id(); |
| 1256 data.sync_guid = specifics.sync_guid(); | 1260 data.sync_guid = specifics.sync_guid(); |
| 1257 | 1261 |
| 1258 TemplateURL* turl = new TemplateURL(profile, data); | 1262 TemplateURL* turl = new TemplateURL(profile, data); |
| 1259 DCHECK(!turl->IsExtensionKeyword()); | 1263 DCHECK(!turl->IsExtensionKeyword()); |
| 1260 if (reset_keyword) { | 1264 if (reset_keyword) { |
| 1261 turl->ResetKeywordIfNecessary(true); | 1265 turl->ResetKeywordIfNecessary(true); |
| 1262 SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1266 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
| 1263 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 1267 change_list->push_back( |
| 1268 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, sync_data)); |
| 1264 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { | 1269 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { |
| 1265 if (!existing_turl) { | 1270 if (!existing_turl) { |
| 1266 // We're adding a new TemplateURL that uses the Google base URL, so set | 1271 // We're adding a new TemplateURL that uses the Google base URL, so set |
| 1267 // its keyword appropriately for the local environment. | 1272 // its keyword appropriately for the local environment. |
| 1268 turl->ResetKeywordIfNecessary(false); | 1273 turl->ResetKeywordIfNecessary(false); |
| 1269 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) { | 1274 } else if (existing_turl->IsGoogleSearchURLWithReplaceableKeyword()) { |
| 1270 // Ignore keyword changes triggered by the Google base URL changing on | 1275 // Ignore keyword changes triggered by the Google base URL changing on |
| 1271 // another client. If the base URL changes in this client as well, we'll | 1276 // another client. If the base URL changes in this client as well, we'll |
| 1272 // pick that up separately at the appropriate time. Otherwise, changing | 1277 // pick that up separately at the appropriate time. Otherwise, changing |
| 1273 // the keyword here could result in having the wrong keyword for the local | 1278 // the keyword here could result in having the wrong keyword for the local |
| 1274 // environment. | 1279 // environment. |
| 1275 turl->data_.SetKeyword(existing_turl->keyword()); | 1280 turl->data_.SetKeyword(existing_turl->keyword()); |
| 1276 } | 1281 } |
| 1277 } | 1282 } |
| 1278 | 1283 |
| 1279 return turl; | 1284 return turl; |
| 1280 } | 1285 } |
| 1281 | 1286 |
| 1282 // static | 1287 // static |
| 1283 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( | 1288 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( |
| 1284 const SyncDataList& sync_data) { | 1289 const csync::SyncDataList& sync_data) { |
| 1285 SyncDataMap data_map; | 1290 SyncDataMap data_map; |
| 1286 for (SyncDataList::const_iterator i(sync_data.begin()); i != sync_data.end(); | 1291 for (csync::SyncDataList::const_iterator i(sync_data.begin()); |
| 1292 i != sync_data.end(); |
| 1287 ++i) | 1293 ++i) |
| 1288 data_map[i->GetSpecifics().search_engine().sync_guid()] = *i; | 1294 data_map[i->GetSpecifics().search_engine().sync_guid()] = *i; |
| 1289 return data_map; | 1295 return data_map; |
| 1290 } | 1296 } |
| 1291 | 1297 |
| 1292 void TemplateURLService::SetKeywordSearchTermsForURL(const TemplateURL* t_url, | 1298 void TemplateURLService::SetKeywordSearchTermsForURL(const TemplateURL* t_url, |
| 1293 const GURL& url, | 1299 const GURL& url, |
| 1294 const string16& term) { | 1300 const string16& term) { |
| 1295 HistoryService* history = profile_ ? | 1301 HistoryService* history = profile_ ? |
| 1296 HistoryServiceFactory::GetForProfile(profile_, | 1302 HistoryServiceFactory::GetForProfile(profile_, |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1708 } | 1714 } |
| 1709 } | 1715 } |
| 1710 } | 1716 } |
| 1711 if (!existing_turl->sync_guid().empty()) | 1717 if (!existing_turl->sync_guid().empty()) |
| 1712 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; | 1718 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; |
| 1713 | 1719 |
| 1714 if (service_.get()) | 1720 if (service_.get()) |
| 1715 service_->UpdateKeyword(existing_turl->data()); | 1721 service_->UpdateKeyword(existing_turl->data()); |
| 1716 | 1722 |
| 1717 // Inform sync of the update. | 1723 // Inform sync of the update. |
| 1718 ProcessTemplateURLChange(existing_turl, SyncChange::ACTION_UPDATE); | 1724 ProcessTemplateURLChange(existing_turl, csync::SyncChange::ACTION_UPDATE); |
| 1719 | 1725 |
| 1720 if (default_search_provider_ == existing_turl) { | 1726 if (default_search_provider_ == existing_turl) { |
| 1721 bool success = SetDefaultSearchProviderNoNotify(existing_turl); | 1727 bool success = SetDefaultSearchProviderNoNotify(existing_turl); |
| 1722 DCHECK(success); | 1728 DCHECK(success); |
| 1723 } | 1729 } |
| 1724 return true; | 1730 return true; |
| 1725 } | 1731 } |
| 1726 | 1732 |
| 1727 PrefService* TemplateURLService::GetPrefs() { | 1733 PrefService* TemplateURLService::GetPrefs() { |
| 1728 return profile_ ? profile_->GetPrefs() : NULL; | 1734 return profile_ ? profile_->GetPrefs() : NULL; |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2039 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, | 2045 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, |
| 2040 url->sync_guid()); | 2046 url->sync_guid()); |
| 2041 } | 2047 } |
| 2042 } | 2048 } |
| 2043 | 2049 |
| 2044 if (service_.get()) | 2050 if (service_.get()) |
| 2045 service_->SetDefaultSearchProvider(url); | 2051 service_->SetDefaultSearchProvider(url); |
| 2046 | 2052 |
| 2047 // Inform sync the change to the show_in_default_list flag. | 2053 // Inform sync the change to the show_in_default_list flag. |
| 2048 if (url) | 2054 if (url) |
| 2049 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); | 2055 ProcessTemplateURLChange(url, csync::SyncChange::ACTION_UPDATE); |
| 2050 return true; | 2056 return true; |
| 2051 } | 2057 } |
| 2052 | 2058 |
| 2053 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, | 2059 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, |
| 2054 bool newly_adding) { | 2060 bool newly_adding) { |
| 2055 DCHECK(template_url); | 2061 DCHECK(template_url); |
| 2056 | 2062 |
| 2057 if (newly_adding) { | 2063 if (newly_adding) { |
| 2058 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 2064 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
| 2059 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), | 2065 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 2088 if (newly_adding) { | 2094 if (newly_adding) { |
| 2089 // Don't persist extension keywords to disk. They'll get re-added on each | 2095 // Don't persist extension keywords to disk. They'll get re-added on each |
| 2090 // launch as the extensions are loaded. | 2096 // launch as the extensions are loaded. |
| 2091 // TODO(mpcomplete): If we allow editing extension keywords, then those | 2097 // TODO(mpcomplete): If we allow editing extension keywords, then those |
| 2092 // should be persisted to disk and synced. | 2098 // should be persisted to disk and synced. |
| 2093 if (service_.get() && !template_url->IsExtensionKeyword()) | 2099 if (service_.get() && !template_url->IsExtensionKeyword()) |
| 2094 service_->AddKeyword(template_url->data()); | 2100 service_->AddKeyword(template_url->data()); |
| 2095 | 2101 |
| 2096 // Inform sync of the addition. Note that this will assign a GUID to | 2102 // Inform sync of the addition. Note that this will assign a GUID to |
| 2097 // template_url and add it to the guid_to_template_map_. | 2103 // template_url and add it to the guid_to_template_map_. |
| 2098 ProcessTemplateURLChange(template_url, SyncChange::ACTION_ADD); | 2104 ProcessTemplateURLChange(template_url, csync::SyncChange::ACTION_ADD); |
| 2099 } | 2105 } |
| 2100 | 2106 |
| 2101 return true; | 2107 return true; |
| 2102 } | 2108 } |
| 2103 | 2109 |
| 2104 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { | 2110 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { |
| 2105 TemplateURLVector::iterator i = | 2111 TemplateURLVector::iterator i = |
| 2106 std::find(template_urls_.begin(), template_urls_.end(), template_url); | 2112 std::find(template_urls_.begin(), template_urls_.end(), template_url); |
| 2107 if (i == template_urls_.end()) | 2113 if (i == template_urls_.end()) |
| 2108 return; | 2114 return; |
| 2109 | 2115 |
| 2110 if (template_url == default_search_provider_) { | 2116 if (template_url == default_search_provider_) { |
| 2111 // Should never delete the default search provider. | 2117 // Should never delete the default search provider. |
| 2112 NOTREACHED(); | 2118 NOTREACHED(); |
| 2113 return; | 2119 return; |
| 2114 } | 2120 } |
| 2115 | 2121 |
| 2116 RemoveFromMaps(template_url); | 2122 RemoveFromMaps(template_url); |
| 2117 | 2123 |
| 2118 // Remove it from the vector containing all TemplateURLs. | 2124 // Remove it from the vector containing all TemplateURLs. |
| 2119 template_urls_.erase(i); | 2125 template_urls_.erase(i); |
| 2120 | 2126 |
| 2121 // Extension keywords are not persisted. | 2127 // Extension keywords are not persisted. |
| 2122 // TODO(mpcomplete): If we allow editing extension keywords, then those should | 2128 // TODO(mpcomplete): If we allow editing extension keywords, then those should |
| 2123 // be persisted to disk and synced. | 2129 // be persisted to disk and synced. |
| 2124 if (service_.get() && !template_url->IsExtensionKeyword()) | 2130 if (service_.get() && !template_url->IsExtensionKeyword()) |
| 2125 service_->RemoveKeyword(template_url->id()); | 2131 service_->RemoveKeyword(template_url->id()); |
| 2126 | 2132 |
| 2127 // Inform sync of the deletion. | 2133 // Inform sync of the deletion. |
| 2128 ProcessTemplateURLChange(template_url, SyncChange::ACTION_DELETE); | 2134 ProcessTemplateURLChange(template_url, csync::SyncChange::ACTION_DELETE); |
| 2129 | 2135 |
| 2130 if (profile_) { | 2136 if (profile_) { |
| 2131 content::Source<Profile> source(profile_); | 2137 content::Source<Profile> source(profile_); |
| 2132 TemplateURLID id = template_url->id(); | 2138 TemplateURLID id = template_url->id(); |
| 2133 content::NotificationService::current()->Notify( | 2139 content::NotificationService::current()->Notify( |
| 2134 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, | 2140 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, |
| 2135 source, | 2141 source, |
| 2136 content::Details<TemplateURLID>(&id)); | 2142 content::Details<TemplateURLID>(&id)); |
| 2137 } | 2143 } |
| 2138 | 2144 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2230 do { | 2236 do { |
| 2231 keyword_candidate.append(ASCIIToUTF16("_")); | 2237 keyword_candidate.append(ASCIIToUTF16("_")); |
| 2232 } while (GetTemplateURLForKeyword(keyword_candidate)); | 2238 } while (GetTemplateURLForKeyword(keyword_candidate)); |
| 2233 | 2239 |
| 2234 return keyword_candidate; | 2240 return keyword_candidate; |
| 2235 } | 2241 } |
| 2236 | 2242 |
| 2237 bool TemplateURLService::ResolveSyncKeywordConflict( | 2243 bool TemplateURLService::ResolveSyncKeywordConflict( |
| 2238 TemplateURL* sync_turl, | 2244 TemplateURL* sync_turl, |
| 2239 TemplateURL* local_turl, | 2245 TemplateURL* local_turl, |
| 2240 SyncChangeList* change_list) { | 2246 csync::SyncChangeList* change_list) { |
| 2241 DCHECK(loaded_); | 2247 DCHECK(loaded_); |
| 2242 DCHECK(sync_turl); | 2248 DCHECK(sync_turl); |
| 2243 DCHECK(local_turl); | 2249 DCHECK(local_turl); |
| 2244 DCHECK(sync_turl->sync_guid() != local_turl->sync_guid()); | 2250 DCHECK(sync_turl->sync_guid() != local_turl->sync_guid()); |
| 2245 DCHECK(!local_turl->IsExtensionKeyword()); | 2251 DCHECK(!local_turl->IsExtensionKeyword()); |
| 2246 DCHECK(change_list); | 2252 DCHECK(change_list); |
| 2247 | 2253 |
| 2248 const bool local_is_better = | 2254 const bool local_is_better = |
| 2249 (local_turl->last_modified() > sync_turl->last_modified()) || | 2255 (local_turl->last_modified() > sync_turl->last_modified()) || |
| 2250 local_turl->created_by_policy() || | 2256 local_turl->created_by_policy() || |
| 2251 (local_turl == GetDefaultSearchProvider()); | 2257 (local_turl == GetDefaultSearchProvider()); |
| 2252 const bool can_replace_local = CanReplace(local_turl); | 2258 const bool can_replace_local = CanReplace(local_turl); |
| 2253 if (CanReplace(sync_turl) && (local_is_better || !can_replace_local)) { | 2259 if (CanReplace(sync_turl) && (local_is_better || !can_replace_local)) { |
| 2254 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 2260 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
| 2255 change_list->push_back(SyncChange(SyncChange::ACTION_DELETE, sync_data)); | 2261 change_list->push_back( |
| 2262 csync::SyncChange(csync::SyncChange::ACTION_DELETE, sync_data)); |
| 2256 return false; | 2263 return false; |
| 2257 } | 2264 } |
| 2258 if (can_replace_local) { | 2265 if (can_replace_local) { |
| 2259 // Since we're processing sync changes, the upcoming Remove() won't generate | 2266 // Since we're processing sync changes, the upcoming Remove() won't generate |
| 2260 // an ACTION_DELETE. We need to do it manually to keep the server in sync | 2267 // an ACTION_DELETE. We need to do it manually to keep the server in sync |
| 2261 // with us. Note that if we're being called from | 2268 // with us. Note that if we're being called from |
| 2262 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather | 2269 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather |
| 2263 // than having just been brought down, then this is wrong, because the | 2270 // than having just been brought down, then this is wrong, because the |
| 2264 // server doesn't yet know about this entity; but in this case, | 2271 // server doesn't yet know about this entity; but in this case, |
| 2265 // PruneSyncChanges() will prune out the ACTION_DELETE we create here. | 2272 // PruneSyncChanges() will prune out the ACTION_DELETE we create here. |
| 2266 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2273 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 2267 change_list->push_back(SyncChange(SyncChange::ACTION_DELETE, sync_data)); | 2274 change_list->push_back( |
| 2275 csync::SyncChange(csync::SyncChange::ACTION_DELETE, sync_data)); |
| 2268 Remove(local_turl); | 2276 Remove(local_turl); |
| 2269 } else if (local_is_better) { | 2277 } else if (local_is_better) { |
| 2270 string16 new_keyword = UniquifyKeyword(*sync_turl); | 2278 string16 new_keyword = UniquifyKeyword(*sync_turl); |
| 2271 DCHECK(!GetTemplateURLForKeyword(new_keyword)); | 2279 DCHECK(!GetTemplateURLForKeyword(new_keyword)); |
| 2272 sync_turl->data_.SetKeyword(new_keyword); | 2280 sync_turl->data_.SetKeyword(new_keyword); |
| 2273 // If we update the cloud TURL, we need to push an update back to sync | 2281 // If we update the cloud TURL, we need to push an update back to sync |
| 2274 // informing it that something has changed. | 2282 // informing it that something has changed. |
| 2275 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 2283 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
| 2276 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 2284 change_list->push_back( |
| 2285 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, sync_data)); |
| 2277 } else { | 2286 } else { |
| 2278 string16 new_keyword = UniquifyKeyword(*local_turl); | 2287 string16 new_keyword = UniquifyKeyword(*local_turl); |
| 2279 TemplateURLData data(local_turl->data()); | 2288 TemplateURLData data(local_turl->data()); |
| 2280 data.SetKeyword(new_keyword); | 2289 data.SetKeyword(new_keyword); |
| 2281 TemplateURL new_turl(local_turl->profile(), data); | 2290 TemplateURL new_turl(local_turl->profile(), data); |
| 2282 UIThreadSearchTermsData search_terms_data(local_turl->profile()); | 2291 UIThreadSearchTermsData search_terms_data(local_turl->profile()); |
| 2283 if (UpdateNoNotify(local_turl, new_turl, search_terms_data)) | 2292 if (UpdateNoNotify(local_turl, new_turl, search_terms_data)) |
| 2284 NotifyObservers(); | 2293 NotifyObservers(); |
| 2285 // Since we're processing sync changes, the UpdateNoNotify() above didn't | 2294 // Since we're processing sync changes, the UpdateNoNotify() above didn't |
| 2286 // generate an ACTION_UPDATE. We need to do it manually to keep the server | 2295 // generate an ACTION_UPDATE. We need to do it manually to keep the server |
| 2287 // in sync with us. Note that if we're being called from | 2296 // in sync with us. Note that if we're being called from |
| 2288 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather | 2297 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather |
| 2289 // than having just been brought down, then this is wrong, because the | 2298 // than having just been brought down, then this is wrong, because the |
| 2290 // server won't know about this entity until it processes the ACTION_ADD our | 2299 // server won't know about this entity until it processes the ACTION_ADD our |
| 2291 // caller will later generate; but in this case, PruneSyncChanges() will | 2300 // caller will later generate; but in this case, PruneSyncChanges() will |
| 2292 // prune out the ACTION_UPDATE we create here. | 2301 // prune out the ACTION_UPDATE we create here. |
| 2293 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2302 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 2294 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 2303 change_list->push_back( |
| 2304 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, sync_data)); |
| 2295 } | 2305 } |
| 2296 return true; | 2306 return true; |
| 2297 } | 2307 } |
| 2298 | 2308 |
| 2299 TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( | 2309 TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( |
| 2300 const TemplateURL& sync_turl) { | 2310 const TemplateURL& sync_turl) { |
| 2301 TemplateURL* existing_turl = GetTemplateURLForKeyword(sync_turl.keyword()); | 2311 TemplateURL* existing_turl = GetTemplateURLForKeyword(sync_turl.keyword()); |
| 2302 return existing_turl && (existing_turl->url() == sync_turl.url()) ? | 2312 return existing_turl && (existing_turl->url() == sync_turl.url()) ? |
| 2303 existing_turl : NULL; | 2313 existing_turl : NULL; |
| 2304 } | 2314 } |
| 2305 | 2315 |
| 2306 void TemplateURLService::MergeSyncAndLocalURLDuplicates( | 2316 void TemplateURLService::MergeSyncAndLocalURLDuplicates( |
| 2307 TemplateURL* sync_turl, | 2317 TemplateURL* sync_turl, |
| 2308 TemplateURL* local_turl, | 2318 TemplateURL* local_turl, |
| 2309 SyncChangeList* change_list) { | 2319 csync::SyncChangeList* change_list) { |
| 2310 DCHECK(loaded_); | 2320 DCHECK(loaded_); |
| 2311 DCHECK(sync_turl); | 2321 DCHECK(sync_turl); |
| 2312 DCHECK(local_turl); | 2322 DCHECK(local_turl); |
| 2313 DCHECK(change_list); | 2323 DCHECK(change_list); |
| 2314 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl); | 2324 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl); |
| 2315 if (sync_turl->last_modified() > local_turl->last_modified()) { | 2325 if (sync_turl->last_modified() > local_turl->last_modified()) { |
| 2316 // Fully replace local_url with Sync's copy. Note that because use Add | 2326 // Fully replace local_url with Sync's copy. Note that because use Add |
| 2317 // rather than ResetTemplateURL, |sync_url| is added with a fresh | 2327 // rather than ResetTemplateURL, |sync_url| is added with a fresh |
| 2318 // TemplateURLID. We don't need to sync the new ID back to the server since | 2328 // TemplateURLID. We don't need to sync the new ID back to the server since |
| 2319 // it's only relevant locally. | 2329 // it's only relevant locally. |
| 2320 bool delete_default = (local_turl == GetDefaultSearchProvider()); | 2330 bool delete_default = (local_turl == GetDefaultSearchProvider()); |
| 2321 DCHECK(!delete_default || !is_default_search_managed_); | 2331 DCHECK(!delete_default || !is_default_search_managed_); |
| 2322 if (delete_default) | 2332 if (delete_default) |
| 2323 default_search_provider_ = NULL; | 2333 default_search_provider_ = NULL; |
| 2324 | 2334 |
| 2325 // See comments in ResolveSyncKeywordConflict() regarding generating an | 2335 // See comments in ResolveSyncKeywordConflict() regarding generating an |
| 2326 // ACTION_DELETE manually since Remove() won't do it. | 2336 // ACTION_DELETE manually since Remove() won't do it. |
| 2327 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2337 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 2328 change_list->push_back(SyncChange(SyncChange::ACTION_DELETE, sync_data)); | 2338 change_list->push_back( |
| 2339 csync::SyncChange(csync::SyncChange::ACTION_DELETE, sync_data)); |
| 2329 Remove(local_turl); | 2340 Remove(local_turl); |
| 2330 | 2341 |
| 2331 // Force the local ID to kInvalidTemplateURLID so we can add it. | 2342 // Force the local ID to kInvalidTemplateURLID so we can add it. |
| 2332 sync_turl->data_.id = kInvalidTemplateURLID; | 2343 sync_turl->data_.id = kInvalidTemplateURLID; |
| 2333 Add(scoped_sync_turl.release()); | 2344 Add(scoped_sync_turl.release()); |
| 2334 if (delete_default) | 2345 if (delete_default) |
| 2335 SetDefaultSearchProvider(sync_turl); | 2346 SetDefaultSearchProvider(sync_turl); |
| 2336 } else { | 2347 } else { |
| 2337 // Change the local TURL's GUID to the server's GUID and push an update to | 2348 // Change the local TURL's GUID to the server's GUID and push an update to |
| 2338 // Sync. This ensures that the rest of local_url's fields are sync'd up to | 2349 // Sync. This ensures that the rest of local_url's fields are sync'd up to |
| 2339 // the server, and the next time local_url is synced, it is recognized by | 2350 // the server, and the next time local_url is synced, it is recognized by |
| 2340 // having the same GUID. | 2351 // having the same GUID. |
| 2341 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); | 2352 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); |
| 2342 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 2353 csync::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
| 2343 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 2354 change_list->push_back( |
| 2355 csync::SyncChange(csync::SyncChange::ACTION_UPDATE, sync_data)); |
| 2344 } | 2356 } |
| 2345 } | 2357 } |
| 2346 | 2358 |
| 2347 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( | 2359 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( |
| 2348 const std::string& guid) { | 2360 const std::string& guid) { |
| 2349 // If we're not syncing or if default search is managed by policy, ignore. | 2361 // If we're not syncing or if default search is managed by policy, ignore. |
| 2350 if (!sync_processor_.get() || is_default_search_managed_) | 2362 if (!sync_processor_.get() || is_default_search_managed_) |
| 2351 return; | 2363 return; |
| 2352 | 2364 |
| 2353 PrefService* prefs = GetPrefs(); | 2365 PrefService* prefs = GetPrefs(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2386 // TODO(mpcomplete): If we allow editing extension keywords, then those | 2398 // TODO(mpcomplete): If we allow editing extension keywords, then those |
| 2387 // should be persisted to disk and synced. | 2399 // should be persisted to disk and synced. |
| 2388 if (template_url->sync_guid().empty() && | 2400 if (template_url->sync_guid().empty() && |
| 2389 !template_url->IsExtensionKeyword()) { | 2401 !template_url->IsExtensionKeyword()) { |
| 2390 template_url->data_.sync_guid = base::GenerateGUID(); | 2402 template_url->data_.sync_guid = base::GenerateGUID(); |
| 2391 if (service_.get()) | 2403 if (service_.get()) |
| 2392 service_->UpdateKeyword(template_url->data()); | 2404 service_->UpdateKeyword(template_url->data()); |
| 2393 } | 2405 } |
| 2394 } | 2406 } |
| 2395 } | 2407 } |
| OLD | NEW |