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/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_namespace.h" | 8 #include "chrome/browser/extensions/settings/settings_namespace.h" |
9 #include "chrome/browser/extensions/settings/settings_sync_util.h" | 9 #include "chrome/browser/extensions/settings/settings_sync_util.h" |
10 #include "chrome/browser/sync/api/sync_data.h" | 10 #include "chrome/browser/sync/api/sync_data.h" |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 return OverwriteLocalSettingsWithSync(sync_state, maybe_settings.settings()); | 159 return OverwriteLocalSettingsWithSync(sync_state, maybe_settings.settings()); |
160 } | 160 } |
161 | 161 |
162 SyncError SyncableSettingsStorage::SendLocalSettingsToSync( | 162 SyncError SyncableSettingsStorage::SendLocalSettingsToSync( |
163 const DictionaryValue& settings) { | 163 const DictionaryValue& settings) { |
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
165 DCHECK(sync_processor_); | 165 DCHECK(sync_processor_); |
166 | 166 |
167 SyncChangeList changes; | 167 SyncChangeList changes; |
168 for (DictionaryValue::Iterator it(settings); it.HasNext(); it.Advance()) { | 168 for (DictionaryValue::Iterator it(settings); it.HasNext(); it.Advance()) { |
169 changes.push_back( | 169 changes.push_back(settings_sync_util::CreateAdd( |
170 settings_sync_util::CreateAdd(extension_id_, it.key(), it.value())); | 170 extension_id_, it.key(), it.value(), sync_type_)); |
171 } | 171 } |
172 | 172 |
173 if (changes.empty()) { | 173 if (changes.empty()) { |
174 return SyncError(); | 174 return SyncError(); |
175 } | 175 } |
176 | 176 |
177 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 177 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
178 if (error.IsSet()) { | 178 if (error.IsSet()) { |
179 StopSyncing(); | 179 StopSyncing(); |
180 return error; | 180 return error; |
(...skipping 22 matching lines...) Expand all Loading... |
203 if (sync_value->Equals(&it.value())) { | 203 if (sync_value->Equals(&it.value())) { |
204 // Sync and local values are the same, no changes to send. | 204 // Sync and local values are the same, no changes to send. |
205 synced_keys_.insert(it.key()); | 205 synced_keys_.insert(it.key()); |
206 } else { | 206 } else { |
207 // Sync value is different, update local setting with new value. | 207 // Sync value is different, update local setting with new value. |
208 changes.push_back( | 208 changes.push_back( |
209 SettingSyncData( | 209 SettingSyncData( |
210 SyncChange::ACTION_UPDATE, | 210 SyncChange::ACTION_UPDATE, |
211 extension_id_, | 211 extension_id_, |
212 it.key(), | 212 it.key(), |
213 sync_value.release())); | 213 sync_value.Pass())); |
214 } | 214 } |
215 } else { | 215 } else { |
216 // Not synced, delete local setting. | 216 // Not synced, delete local setting. |
217 changes.push_back( | 217 changes.push_back( |
218 SettingSyncData( | 218 SettingSyncData( |
219 SyncChange::ACTION_DELETE, | 219 SyncChange::ACTION_DELETE, |
220 extension_id_, | 220 extension_id_, |
221 it.key(), | 221 it.key(), |
222 new DictionaryValue())); | 222 scoped_ptr<Value>(new DictionaryValue()))); |
223 } | 223 } |
224 } | 224 } |
225 | 225 |
226 // Add all new settings to local settings. | 226 // Add all new settings to local settings. |
227 while (!new_sync_state->empty()) { | 227 while (!new_sync_state->empty()) { |
228 std::string key = *new_sync_state->begin_keys(); | 228 std::string key = *new_sync_state->begin_keys(); |
229 Value* value = NULL; | 229 Value* value = NULL; |
230 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); | 230 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); |
231 changes.push_back( | 231 changes.push_back( |
232 SettingSyncData( | 232 SettingSyncData( |
233 SyncChange::ACTION_ADD, extension_id_, key, value)); | 233 SyncChange::ACTION_ADD, |
| 234 extension_id_, |
| 235 key, |
| 236 scoped_ptr<Value>(value))); |
234 } | 237 } |
235 | 238 |
236 if (changes.empty()) { | 239 if (changes.empty()) { |
237 return SyncError(); | 240 return SyncError(); |
238 } | 241 } |
239 return ProcessSyncChanges(changes); | 242 return ProcessSyncChanges(changes); |
240 } | 243 } |
241 | 244 |
242 void SyncableSettingsStorage::StopSyncing() { | 245 void SyncableSettingsStorage::StopSyncing() { |
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 std::set<std::string> added_keys; | 358 std::set<std::string> added_keys; |
356 std::set<std::string> deleted_keys; | 359 std::set<std::string> deleted_keys; |
357 | 360 |
358 for (SettingChangeList::const_iterator it = changes.begin(); | 361 for (SettingChangeList::const_iterator it = changes.begin(); |
359 it != changes.end(); ++it) { | 362 it != changes.end(); ++it) { |
360 const std::string& key = it->key(); | 363 const std::string& key = it->key(); |
361 const Value* value = it->new_value(); | 364 const Value* value = it->new_value(); |
362 if (value) { | 365 if (value) { |
363 if (synced_keys_.count(key)) { | 366 if (synced_keys_.count(key)) { |
364 // New value, key is synced; send ACTION_UPDATE. | 367 // New value, key is synced; send ACTION_UPDATE. |
365 sync_changes.push_back( | 368 sync_changes.push_back(settings_sync_util::CreateUpdate( |
366 settings_sync_util::CreateUpdate( | 369 extension_id_, key, *value, sync_type_)); |
367 extension_id_, key, *value)); | |
368 } else { | 370 } else { |
369 // New value, key is not synced; send ACTION_ADD. | 371 // New value, key is not synced; send ACTION_ADD. |
370 sync_changes.push_back( | 372 sync_changes.push_back(settings_sync_util::CreateAdd( |
371 settings_sync_util::CreateAdd( | 373 extension_id_, key, *value, sync_type_)); |
372 extension_id_, key, *value)); | |
373 added_keys.insert(key); | 374 added_keys.insert(key); |
374 } | 375 } |
375 } else { | 376 } else { |
376 if (synced_keys_.count(key)) { | 377 if (synced_keys_.count(key)) { |
377 // Clearing value, key is synced; send ACTION_DELETE. | 378 // Clearing value, key is synced; send ACTION_DELETE. |
378 sync_changes.push_back( | 379 sync_changes.push_back(settings_sync_util::CreateDelete( |
379 settings_sync_util::CreateDelete(extension_id_, key)); | 380 extension_id_, key, sync_type_)); |
380 deleted_keys.insert(key); | 381 deleted_keys.insert(key); |
381 } else { | 382 } else { |
382 LOG(WARNING) << "Deleted " << key << " but not in synced_keys_"; | 383 LOG(WARNING) << "Deleted " << key << " but not in synced_keys_"; |
383 } | 384 } |
384 } | 385 } |
385 } | 386 } |
386 | 387 |
387 if (sync_changes.empty()) { | 388 if (sync_changes.empty()) { |
388 return; | 389 return; |
389 } | 390 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 FROM_HERE, | 452 FROM_HERE, |
452 std::string("Error pushing sync remove to local settings: ") + | 453 std::string("Error pushing sync remove to local settings: ") + |
453 result.error(), | 454 result.error(), |
454 sync_type_); | 455 sync_type_); |
455 } | 456 } |
456 changes->push_back(SettingChange(key, old_value, NULL)); | 457 changes->push_back(SettingChange(key, old_value, NULL)); |
457 return SyncError(); | 458 return SyncError(); |
458 } | 459 } |
459 | 460 |
460 } // namespace extensions | 461 } // namespace extensions |
OLD | NEW |