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/syncable_extension_settings_storage.h" | 5 #include "chrome/browser/extensions/syncable_extension_settings_storage.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "chrome/browser/extensions/extension_settings_sync_util.h" | 8 #include "chrome/browser/extensions/extension_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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 SyncError SyncableExtensionSettingsStorage::OverwriteLocalSettingsWithSync( | 169 SyncError SyncableExtensionSettingsStorage::OverwriteLocalSettingsWithSync( |
170 const DictionaryValue& sync_state, const DictionaryValue& settings) { | 170 const DictionaryValue& sync_state, const DictionaryValue& settings) { |
171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
172 // Treat this as a list of changes to sync and use ProcessSyncChanges. | 172 // Treat this as a list of changes to sync and use ProcessSyncChanges. |
173 // This gives notifications etc for free. | 173 // This gives notifications etc for free. |
174 scoped_ptr<DictionaryValue> new_sync_state(sync_state.DeepCopy()); | 174 scoped_ptr<DictionaryValue> new_sync_state(sync_state.DeepCopy()); |
175 | 175 |
176 ExtensionSettingSyncDataList changes; | 176 ExtensionSettingSyncDataList changes; |
177 for (DictionaryValue::key_iterator it = settings.begin_keys(); | 177 for (DictionaryValue::key_iterator it = settings.begin_keys(); |
178 it != settings.end_keys(); ++it) { | 178 it != settings.end_keys(); ++it) { |
179 Value* sync_value = NULL; | 179 Value* orphaned_sync_value = NULL; |
180 if (new_sync_state->RemoveWithoutPathExpansion(*it, &sync_value)) { | 180 if (new_sync_state->RemoveWithoutPathExpansion(*it, &orphaned_sync_value)) { |
| 181 scoped_ptr<Value> sync_value(orphaned_sync_value); |
181 Value* local_value = NULL; | 182 Value* local_value = NULL; |
182 settings.GetWithoutPathExpansion(*it, &local_value); | 183 settings.GetWithoutPathExpansion(*it, &local_value); |
183 if (!local_value->Equals(sync_value)) { | 184 if (!sync_value->Equals(local_value)) { |
184 // Sync value is different, update local setting with new value. | 185 // Sync value is different, update local setting with new value. |
185 changes.push_back( | 186 changes.push_back( |
186 ExtensionSettingSyncData( | 187 ExtensionSettingSyncData( |
187 SyncChange::ACTION_UPDATE, extension_id_, *it, sync_value)); | 188 SyncChange::ACTION_UPDATE, |
| 189 extension_id_, |
| 190 *it, |
| 191 sync_value.release())); |
188 } | 192 } |
189 } else { | 193 } else { |
190 // Not synced, delete local setting. | 194 // Not synced, delete local setting. |
191 changes.push_back( | 195 changes.push_back( |
192 ExtensionSettingSyncData( | 196 ExtensionSettingSyncData( |
193 SyncChange::ACTION_DELETE, | 197 SyncChange::ACTION_DELETE, |
194 extension_id_, | 198 extension_id_, |
195 *it, | 199 *it, |
196 new DictionaryValue())); | 200 new DictionaryValue())); |
197 } | 201 } |
198 } | 202 } |
199 | 203 |
200 // Add all new settings to local settings. | 204 // Add all new settings to local settings. |
201 while (!new_sync_state->empty()) { | 205 while (!new_sync_state->empty()) { |
202 std::string key = *new_sync_state->begin_keys(); | 206 std::string key = *new_sync_state->begin_keys(); |
203 Value* value; | 207 Value* value = NULL; |
204 new_sync_state->RemoveWithoutPathExpansion(key, &value); | 208 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); |
205 changes.push_back( | 209 changes.push_back( |
206 ExtensionSettingSyncData( | 210 ExtensionSettingSyncData( |
207 SyncChange::ACTION_ADD, extension_id_, key, value)); | 211 SyncChange::ACTION_ADD, extension_id_, key, value)); |
208 } | 212 } |
209 | 213 |
210 if (changes.empty()) { | 214 if (changes.empty()) { |
211 return SyncError(); | 215 return SyncError(); |
212 } | 216 } |
213 | 217 |
214 std::vector<SyncError> sync_errors(ProcessSyncChanges(changes)); | 218 std::vector<SyncError> sync_errors(ProcessSyncChanges(changes)); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 if (error.IsSet()) { | 336 if (error.IsSet()) { |
333 LOG(WARNING) << "Failed to send changes to sync: " << error.message(); | 337 LOG(WARNING) << "Failed to send changes to sync: " << error.message(); |
334 return; | 338 return; |
335 } | 339 } |
336 | 340 |
337 for (std::vector<std::string>::const_iterator it = keys.begin(); | 341 for (std::vector<std::string>::const_iterator it = keys.begin(); |
338 it != keys.end(); ++it) { | 342 it != keys.end(); ++it) { |
339 synced_keys_.erase(*it); | 343 synced_keys_.erase(*it); |
340 } | 344 } |
341 } | 345 } |
OLD | NEW |