OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/settings/syncable_settings_storage.h" | 5 #include "chrome/browser/extensions/settings/syncable_settings_storage.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "chrome/browser/extensions/settings/settings_sync_util.h" | 8 #include "chrome/browser/extensions/settings/settings_sync_util.h" |
9 #include "chrome/browser/sync/api/sync_data.h" | 9 #include "chrome/browser/sync/api/sync_data.h" |
10 #include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h" | 10 #include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 } | 141 } |
142 return OverwriteLocalSettingsWithSync(sync_state, maybe_settings.settings()); | 142 return OverwriteLocalSettingsWithSync(sync_state, maybe_settings.settings()); |
143 } | 143 } |
144 | 144 |
145 SyncError SyncableSettingsStorage::SendLocalSettingsToSync( | 145 SyncError SyncableSettingsStorage::SendLocalSettingsToSync( |
146 const DictionaryValue& settings) { | 146 const DictionaryValue& settings) { |
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
148 DCHECK(sync_processor_); | 148 DCHECK(sync_processor_); |
149 | 149 |
150 SyncChangeList changes; | 150 SyncChangeList changes; |
151 for (DictionaryValue::key_iterator it = settings.begin_keys(); | 151 for (DictionaryValue::Iterator it(settings); it.HasNext(); it.Advance()) { |
152 it != settings.end_keys(); ++it) { | |
153 Value* value; | |
154 settings.GetWithoutPathExpansion(*it, &value); | |
155 changes.push_back( | 152 changes.push_back( |
156 settings_sync_util::CreateAdd(extension_id_, *it, *value)); | 153 settings_sync_util::CreateAdd(extension_id_, it.key(), it.value())); |
157 } | 154 } |
158 | 155 |
159 if (changes.empty()) { | 156 if (changes.empty()) { |
160 return SyncError(); | 157 return SyncError(); |
161 } | 158 } |
162 | 159 |
163 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 160 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
164 if (error.IsSet()) { | 161 if (error.IsSet()) { |
165 StopSyncing(); | 162 StopSyncing(); |
166 return error; | 163 return error; |
167 } | 164 } |
168 | 165 |
169 for (DictionaryValue::key_iterator it = settings.begin_keys(); | 166 for (DictionaryValue::key_iterator it = settings.begin_keys(); |
170 it != settings.end_keys(); ++it) { | 167 it != settings.end_keys(); ++it) { |
171 synced_keys_.insert(*it); | 168 synced_keys_.insert(*it); |
172 } | 169 } |
173 return SyncError(); | 170 return SyncError(); |
174 } | 171 } |
175 | 172 |
176 SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync( | 173 SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync( |
177 const DictionaryValue& sync_state, const DictionaryValue& settings) { | 174 const DictionaryValue& sync_state, const DictionaryValue& settings) { |
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
179 // Treat this as a list of changes to sync and use ProcessSyncChanges. | 176 // Treat this as a list of changes to sync and use ProcessSyncChanges. |
180 // This gives notifications etc for free. | 177 // This gives notifications etc for free. |
181 scoped_ptr<DictionaryValue> new_sync_state(sync_state.DeepCopy()); | 178 scoped_ptr<DictionaryValue> new_sync_state(sync_state.DeepCopy()); |
182 | 179 |
183 SettingSyncDataList changes; | 180 SettingSyncDataList changes; |
184 for (DictionaryValue::key_iterator it = settings.begin_keys(); | 181 for (DictionaryValue::Iterator it(settings); it.HasNext(); it.Advance()) { |
185 it != settings.end_keys(); ++it) { | |
186 Value* orphaned_sync_value = NULL; | 182 Value* orphaned_sync_value = NULL; |
187 if (new_sync_state->RemoveWithoutPathExpansion(*it, &orphaned_sync_value)) { | 183 if (new_sync_state->RemoveWithoutPathExpansion( |
| 184 it.key(), &orphaned_sync_value)) { |
188 scoped_ptr<Value> sync_value(orphaned_sync_value); | 185 scoped_ptr<Value> sync_value(orphaned_sync_value); |
189 Value* local_value = NULL; | 186 if (sync_value->Equals(&it.value())) { |
190 settings.GetWithoutPathExpansion(*it, &local_value); | |
191 if (sync_value->Equals(local_value)) { | |
192 // Sync and local values are the same, no changes to send. | 187 // Sync and local values are the same, no changes to send. |
193 synced_keys_.insert(*it); | 188 synced_keys_.insert(it.key()); |
194 } else { | 189 } else { |
195 // Sync value is different, update local setting with new value. | 190 // Sync value is different, update local setting with new value. |
196 changes.push_back( | 191 changes.push_back( |
197 SettingSyncData( | 192 SettingSyncData( |
198 SyncChange::ACTION_UPDATE, | 193 SyncChange::ACTION_UPDATE, |
199 extension_id_, | 194 extension_id_, |
200 *it, | 195 it.key(), |
201 sync_value.release())); | 196 sync_value.release())); |
202 } | 197 } |
203 } else { | 198 } else { |
204 // Not synced, delete local setting. | 199 // Not synced, delete local setting. |
205 changes.push_back( | 200 changes.push_back( |
206 SettingSyncData( | 201 SettingSyncData( |
207 SyncChange::ACTION_DELETE, | 202 SyncChange::ACTION_DELETE, |
208 extension_id_, | 203 extension_id_, |
209 *it, | 204 it.key(), |
210 new DictionaryValue())); | 205 new DictionaryValue())); |
211 } | 206 } |
212 } | 207 } |
213 | 208 |
214 // Add all new settings to local settings. | 209 // Add all new settings to local settings. |
215 while (!new_sync_state->empty()) { | 210 while (!new_sync_state->empty()) { |
216 std::string key = *new_sync_state->begin_keys(); | 211 std::string key = *new_sync_state->begin_keys(); |
217 Value* value = NULL; | 212 Value* value = NULL; |
218 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); | 213 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); |
219 changes.push_back( | 214 changes.push_back( |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 FROM_HERE, | 433 FROM_HERE, |
439 std::string("Error pushing sync remove to local settings: ") + | 434 std::string("Error pushing sync remove to local settings: ") + |
440 result.error(), | 435 result.error(), |
441 sync_type_); | 436 sync_type_); |
442 } | 437 } |
443 changes->push_back(SettingChange(key, old_value, NULL)); | 438 changes->push_back(SettingChange(key, old_value, NULL)); |
444 return SyncError(); | 439 return SyncError(); |
445 } | 440 } |
446 | 441 |
447 } // namespace extensions | 442 } // namespace extensions |
OLD | NEW |