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/api/storage/syncable_settings_storage.h" | 5 #include "chrome/browser/extensions/api/storage/syncable_settings_storage.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
| 9 #include "base/memory/ptr_util.h" |
9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" | 11 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" |
11 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 12 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
12 #include "components/sync/api/sync_data.h" | 13 #include "components/sync/api/sync_data.h" |
13 #include "components/sync/protocol/extension_setting_specifics.pb.h" | 14 #include "components/sync/protocol/extension_setting_specifics.pb.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 #include "extensions/browser/api/storage/settings_namespace.h" | 16 #include "extensions/browser/api/storage/settings_namespace.h" |
16 | 17 |
17 using content::BrowserThread; | 18 using content::BrowserThread; |
18 | 19 |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 std::unique_ptr<SettingSyncDataList> changes(new SettingSyncDataList()); | 213 std::unique_ptr<SettingSyncDataList> changes(new SettingSyncDataList()); |
213 | 214 |
214 for (base::DictionaryValue::Iterator it(*local_state); !it.IsAtEnd(); | 215 for (base::DictionaryValue::Iterator it(*local_state); !it.IsAtEnd(); |
215 it.Advance()) { | 216 it.Advance()) { |
216 std::unique_ptr<base::Value> sync_value; | 217 std::unique_ptr<base::Value> sync_value; |
217 if (sync_state->RemoveWithoutPathExpansion(it.key(), &sync_value)) { | 218 if (sync_state->RemoveWithoutPathExpansion(it.key(), &sync_value)) { |
218 if (sync_value->Equals(&it.value())) { | 219 if (sync_value->Equals(&it.value())) { |
219 // Sync and local values are the same, no changes to send. | 220 // Sync and local values are the same, no changes to send. |
220 } else { | 221 } else { |
221 // Sync value is different, update local setting with new value. | 222 // Sync value is different, update local setting with new value. |
222 changes->push_back(new SettingSyncData( | 223 changes->push_back(base::MakeUnique<SettingSyncData>( |
223 syncer::SyncChange::ACTION_UPDATE, extension_id_, it.key(), | 224 syncer::SyncChange::ACTION_UPDATE, extension_id_, it.key(), |
224 std::move(sync_value))); | 225 std::move(sync_value))); |
225 } | 226 } |
226 } else { | 227 } else { |
227 // Not synced, delete local setting. | 228 // Not synced, delete local setting. |
228 changes->push_back(new SettingSyncData( | 229 changes->push_back(base::MakeUnique<SettingSyncData>( |
229 syncer::SyncChange::ACTION_DELETE, extension_id_, it.key(), | 230 syncer::SyncChange::ACTION_DELETE, extension_id_, it.key(), |
230 std::unique_ptr<base::Value>(new base::DictionaryValue()))); | 231 std::unique_ptr<base::Value>(new base::DictionaryValue()))); |
231 } | 232 } |
232 } | 233 } |
233 | 234 |
234 // Add all new settings to local settings. | 235 // Add all new settings to local settings. |
235 while (!sync_state->empty()) { | 236 while (!sync_state->empty()) { |
236 // It's not possible to iterate over a DictionaryValue and modify it at the | 237 // It's not possible to iterate over a DictionaryValue and modify it at the |
237 // same time, so hack around that restriction. | 238 // same time, so hack around that restriction. |
238 std::string key = base::DictionaryValue::Iterator(*sync_state).key(); | 239 std::string key = base::DictionaryValue::Iterator(*sync_state).key(); |
239 std::unique_ptr<base::Value> value; | 240 std::unique_ptr<base::Value> value; |
240 CHECK(sync_state->RemoveWithoutPathExpansion(key, &value)); | 241 CHECK(sync_state->RemoveWithoutPathExpansion(key, &value)); |
241 changes->push_back(new SettingSyncData( | 242 changes->push_back(base::MakeUnique<SettingSyncData>( |
242 syncer::SyncChange::ACTION_ADD, extension_id_, key, std::move(value))); | 243 syncer::SyncChange::ACTION_ADD, extension_id_, key, std::move(value))); |
243 } | 244 } |
244 | 245 |
245 if (changes->empty()) | 246 if (changes->empty()) |
246 return syncer::SyncError(); | 247 return syncer::SyncError(); |
247 return ProcessSyncChanges(std::move(changes)); | 248 return ProcessSyncChanges(std::move(changes)); |
248 } | 249 } |
249 | 250 |
250 void SyncableSettingsStorage::StopSyncing() { | 251 void SyncableSettingsStorage::StopSyncing() { |
251 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 252 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
252 sync_processor_.reset(); | 253 sync_processor_.reset(); |
253 } | 254 } |
254 | 255 |
255 syncer::SyncError SyncableSettingsStorage::ProcessSyncChanges( | 256 syncer::SyncError SyncableSettingsStorage::ProcessSyncChanges( |
256 std::unique_ptr<SettingSyncDataList> sync_changes) { | 257 std::unique_ptr<SettingSyncDataList> sync_changes) { |
257 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 258 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
258 DCHECK(!sync_changes->empty()) << "No sync changes for " << extension_id_; | 259 DCHECK(!sync_changes->empty()) << "No sync changes for " << extension_id_; |
259 | 260 |
260 if (!sync_processor_.get()) { | 261 if (!sync_processor_.get()) { |
261 return syncer::SyncError( | 262 return syncer::SyncError( |
262 FROM_HERE, | 263 FROM_HERE, |
263 syncer::SyncError::DATATYPE_ERROR, | 264 syncer::SyncError::DATATYPE_ERROR, |
264 std::string("Sync is inactive for ") + extension_id_, | 265 std::string("Sync is inactive for ") + extension_id_, |
265 syncer::UNSPECIFIED); | 266 syncer::UNSPECIFIED); |
266 } | 267 } |
267 | 268 |
268 std::vector<syncer::SyncError> errors; | 269 std::vector<syncer::SyncError> errors; |
269 ValueStoreChangeList changes; | 270 ValueStoreChangeList changes; |
270 | 271 |
271 for (SettingSyncDataList::iterator it = sync_changes->begin(); | 272 for (const std::unique_ptr<SettingSyncData>& sync_change : *sync_changes) { |
272 it != sync_changes->end(); ++it) { | 273 DCHECK_EQ(extension_id_, sync_change->extension_id()); |
273 DCHECK_EQ(extension_id_, (*it)->extension_id()); | 274 const std::string& key = sync_change->key(); |
274 const std::string& key = (*it)->key(); | 275 std::unique_ptr<base::Value> change_value = sync_change->PassValue(); |
275 std::unique_ptr<base::Value> change_value = (*it)->PassValue(); | |
276 | 276 |
277 std::unique_ptr<base::Value> current_value; | 277 std::unique_ptr<base::Value> current_value; |
278 { | 278 { |
279 ReadResult maybe_settings = Get(key); | 279 ReadResult maybe_settings = Get(key); |
280 if (!maybe_settings->status().ok()) { | 280 if (!maybe_settings->status().ok()) { |
281 errors.push_back(syncer::SyncError( | 281 errors.push_back(syncer::SyncError( |
282 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 282 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
283 base::StringPrintf("Error getting current sync state for %s/%s: %s", | 283 base::StringPrintf("Error getting current sync state for %s/%s: %s", |
284 extension_id_.c_str(), key.c_str(), | 284 extension_id_.c_str(), key.c_str(), |
285 maybe_settings->status().message.c_str()), | 285 maybe_settings->status().message.c_str()), |
286 sync_processor_->type())); | 286 sync_processor_->type())); |
287 continue; | 287 continue; |
288 } | 288 } |
289 maybe_settings->settings().RemoveWithoutPathExpansion(key, | 289 maybe_settings->settings().RemoveWithoutPathExpansion(key, |
290 ¤t_value); | 290 ¤t_value); |
291 } | 291 } |
292 | 292 |
293 syncer::SyncError error; | 293 syncer::SyncError error; |
294 | 294 |
295 switch ((*it)->change_type()) { | 295 switch (sync_change->change_type()) { |
296 case syncer::SyncChange::ACTION_ADD: | 296 case syncer::SyncChange::ACTION_ADD: |
297 if (!current_value.get()) { | 297 if (!current_value.get()) { |
298 error = OnSyncAdd(key, std::move(change_value), &changes); | 298 error = OnSyncAdd(key, std::move(change_value), &changes); |
299 } else { | 299 } else { |
300 // Already a value; hopefully a local change has beaten sync in a | 300 // Already a value; hopefully a local change has beaten sync in a |
301 // race and change's not a bug, so pretend change's an update. | 301 // race and change's not a bug, so pretend change's an update. |
302 LOG(WARNING) << "Got add from sync for existing setting " << | 302 LOG(WARNING) << "Got add from sync for existing setting " << |
303 extension_id_ << "/" << key; | 303 extension_id_ << "/" << key; |
304 error = OnSyncUpdate(key, std::move(current_value), | 304 error = OnSyncUpdate(key, std::move(current_value), |
305 std::move(change_value), &changes); | 305 std::move(change_value), &changes); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 397 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
398 base::StringPrintf("Error pushing sync remove to local settings: %s", | 398 base::StringPrintf("Error pushing sync remove to local settings: %s", |
399 result->status().message.c_str()), | 399 result->status().message.c_str()), |
400 sync_processor_->type()); | 400 sync_processor_->type()); |
401 } | 401 } |
402 changes->push_back(ValueStoreChange(key, std::move(old_value), nullptr)); | 402 changes->push_back(ValueStoreChange(key, std::move(old_value), nullptr)); |
403 return syncer::SyncError(); | 403 return syncer::SyncError(); |
404 } | 404 } |
405 | 405 |
406 } // namespace extensions | 406 } // namespace extensions |
OLD | NEW |