| 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/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" | 10 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 return syncer::SyncError(); | 187 return syncer::SyncError(); |
| 188 | 188 |
| 189 // Transform the current settings into a list of sync changes. | 189 // Transform the current settings into a list of sync changes. |
| 190 ValueStoreChangeList changes; | 190 ValueStoreChangeList changes; |
| 191 while (!local_state->empty()) { | 191 while (!local_state->empty()) { |
| 192 // It's not possible to iterate over a DictionaryValue and modify it at the | 192 // It's not possible to iterate over a DictionaryValue and modify it at the |
| 193 // same time, so hack around that restriction. | 193 // same time, so hack around that restriction. |
| 194 std::string key = base::DictionaryValue::Iterator(*local_state).key(); | 194 std::string key = base::DictionaryValue::Iterator(*local_state).key(); |
| 195 std::unique_ptr<base::Value> value; | 195 std::unique_ptr<base::Value> value; |
| 196 local_state->RemoveWithoutPathExpansion(key, &value); | 196 local_state->RemoveWithoutPathExpansion(key, &value); |
| 197 changes.push_back(ValueStoreChange(key, nullptr, value.release())); | 197 changes.push_back(ValueStoreChange(key, nullptr, std::move(value))); |
| 198 } | 198 } |
| 199 | 199 |
| 200 syncer::SyncError error = sync_processor_->SendChanges(changes); | 200 syncer::SyncError error = sync_processor_->SendChanges(changes); |
| 201 if (error.IsSet()) | 201 if (error.IsSet()) |
| 202 StopSyncing(); | 202 StopSyncing(); |
| 203 return error; | 203 return error; |
| 204 } | 204 } |
| 205 | 205 |
| 206 syncer::SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync( | 206 syncer::SyncError SyncableSettingsStorage::OverwriteLocalSettingsWithSync( |
| 207 std::unique_ptr<base::DictionaryValue> sync_state, | 207 std::unique_ptr<base::DictionaryValue> sync_state, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 ((*it)->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, change_value.release(), &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, current_value.release(), | 304 error = OnSyncUpdate(key, std::move(current_value), |
| 305 change_value.release(), &changes); | 305 std::move(change_value), &changes); |
| 306 } | 306 } |
| 307 break; | 307 break; |
| 308 | 308 |
| 309 case syncer::SyncChange::ACTION_UPDATE: | 309 case syncer::SyncChange::ACTION_UPDATE: |
| 310 if (current_value.get()) { | 310 if (current_value.get()) { |
| 311 error = OnSyncUpdate(key, current_value.release(), | 311 error = OnSyncUpdate(key, std::move(current_value), |
| 312 change_value.release(), &changes); | 312 std::move(change_value), &changes); |
| 313 } else { | 313 } else { |
| 314 // Similarly, pretend change's an add. | 314 // Similarly, pretend change's an add. |
| 315 LOG(WARNING) << "Got update from sync for nonexistent setting" << | 315 LOG(WARNING) << "Got update from sync for nonexistent setting" << |
| 316 extension_id_ << "/" << key; | 316 extension_id_ << "/" << key; |
| 317 error = OnSyncAdd(key, change_value.release(), &changes); | 317 error = OnSyncAdd(key, std::move(change_value), &changes); |
| 318 } | 318 } |
| 319 break; | 319 break; |
| 320 | 320 |
| 321 case syncer::SyncChange::ACTION_DELETE: | 321 case syncer::SyncChange::ACTION_DELETE: |
| 322 if (current_value.get()) { | 322 if (current_value.get()) { |
| 323 error = OnSyncDelete(key, current_value.release(), &changes); | 323 error = OnSyncDelete(key, std::move(current_value), &changes); |
| 324 } else { | 324 } else { |
| 325 // Similarly, ignore change. | 325 // Similarly, ignore change. |
| 326 LOG(WARNING) << "Got delete from sync for nonexistent setting " << | 326 LOG(WARNING) << "Got delete from sync for nonexistent setting " << |
| 327 extension_id_ << "/" << key; | 327 extension_id_ << "/" << key; |
| 328 } | 328 } |
| 329 break; | 329 break; |
| 330 | 330 |
| 331 default: | 331 default: |
| 332 NOTREACHED(); | 332 NOTREACHED(); |
| 333 } | 333 } |
| 334 | 334 |
| 335 if (error.IsSet()) { | 335 if (error.IsSet()) { |
| 336 errors.push_back(error); | 336 errors.push_back(error); |
| 337 } | 337 } |
| 338 } | 338 } |
| 339 | 339 |
| 340 sync_processor_->NotifyChanges(changes); | 340 sync_processor_->NotifyChanges(changes); |
| 341 | 341 |
| 342 observers_->Notify(FROM_HERE, &SettingsObserver::OnSettingsChanged, | 342 observers_->Notify(FROM_HERE, &SettingsObserver::OnSettingsChanged, |
| 343 extension_id_, settings_namespace::SYNC, | 343 extension_id_, settings_namespace::SYNC, |
| 344 ValueStoreChange::ToJson(changes)); | 344 ValueStoreChange::ToJson(changes)); |
| 345 | 345 |
| 346 // TODO(kalman): Something sensible with multiple errors. | 346 // TODO(kalman): Something sensible with multiple errors. |
| 347 return errors.empty() ? syncer::SyncError() : errors[0]; | 347 return errors.empty() ? syncer::SyncError() : errors[0]; |
| 348 } | 348 } |
| 349 | 349 |
| 350 syncer::SyncError SyncableSettingsStorage::OnSyncAdd( | 350 syncer::SyncError SyncableSettingsStorage::OnSyncAdd( |
| 351 const std::string& key, | 351 const std::string& key, |
| 352 base::Value* new_value, | 352 std::unique_ptr<base::Value> new_value, |
| 353 ValueStoreChangeList* changes) { | 353 ValueStoreChangeList* changes) { |
| 354 DCHECK(new_value); | 354 DCHECK(new_value); |
| 355 WriteResult result = | 355 WriteResult result = |
| 356 HandleResult(delegate_->Set(IGNORE_QUOTA, key, *new_value)); | 356 HandleResult(delegate_->Set(IGNORE_QUOTA, key, *new_value)); |
| 357 if (!result->status().ok()) { | 357 if (!result->status().ok()) { |
| 358 return syncer::SyncError( | 358 return syncer::SyncError( |
| 359 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 359 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 360 base::StringPrintf("Error pushing sync add to local settings: %s", | 360 base::StringPrintf("Error pushing sync add to local settings: %s", |
| 361 result->status().message.c_str()), | 361 result->status().message.c_str()), |
| 362 sync_processor_->type()); | 362 sync_processor_->type()); |
| 363 } | 363 } |
| 364 changes->push_back(ValueStoreChange(key, NULL, new_value)); | 364 changes->push_back(ValueStoreChange(key, nullptr, std::move(new_value))); |
| 365 return syncer::SyncError(); | 365 return syncer::SyncError(); |
| 366 } | 366 } |
| 367 | 367 |
| 368 syncer::SyncError SyncableSettingsStorage::OnSyncUpdate( | 368 syncer::SyncError SyncableSettingsStorage::OnSyncUpdate( |
| 369 const std::string& key, | 369 const std::string& key, |
| 370 base::Value* old_value, | 370 std::unique_ptr<base::Value> old_value, |
| 371 base::Value* new_value, | 371 std::unique_ptr<base::Value> new_value, |
| 372 ValueStoreChangeList* changes) { | 372 ValueStoreChangeList* changes) { |
| 373 DCHECK(old_value); | 373 DCHECK(old_value); |
| 374 DCHECK(new_value); | 374 DCHECK(new_value); |
| 375 WriteResult result = | 375 WriteResult result = |
| 376 HandleResult(delegate_->Set(IGNORE_QUOTA, key, *new_value)); | 376 HandleResult(delegate_->Set(IGNORE_QUOTA, key, *new_value)); |
| 377 if (!result->status().ok()) { | 377 if (!result->status().ok()) { |
| 378 return syncer::SyncError( | 378 return syncer::SyncError( |
| 379 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 379 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 380 base::StringPrintf("Error pushing sync update to local settings: %s", | 380 base::StringPrintf("Error pushing sync update to local settings: %s", |
| 381 result->status().message.c_str()), | 381 result->status().message.c_str()), |
| 382 sync_processor_->type()); | 382 sync_processor_->type()); |
| 383 } | 383 } |
| 384 changes->push_back(ValueStoreChange(key, old_value, new_value)); | 384 changes->push_back( |
| 385 ValueStoreChange(key, std::move(old_value), std::move(new_value))); |
| 385 return syncer::SyncError(); | 386 return syncer::SyncError(); |
| 386 } | 387 } |
| 387 | 388 |
| 388 syncer::SyncError SyncableSettingsStorage::OnSyncDelete( | 389 syncer::SyncError SyncableSettingsStorage::OnSyncDelete( |
| 389 const std::string& key, | 390 const std::string& key, |
| 390 base::Value* old_value, | 391 std::unique_ptr<base::Value> old_value, |
| 391 ValueStoreChangeList* changes) { | 392 ValueStoreChangeList* changes) { |
| 392 DCHECK(old_value); | 393 DCHECK(old_value); |
| 393 WriteResult result = HandleResult(delegate_->Remove(key)); | 394 WriteResult result = HandleResult(delegate_->Remove(key)); |
| 394 if (!result->status().ok()) { | 395 if (!result->status().ok()) { |
| 395 return syncer::SyncError( | 396 return syncer::SyncError( |
| 396 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 397 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 397 base::StringPrintf("Error pushing sync remove to local settings: %s", | 398 base::StringPrintf("Error pushing sync remove to local settings: %s", |
| 398 result->status().message.c_str()), | 399 result->status().message.c_str()), |
| 399 sync_processor_->type()); | 400 sync_processor_->type()); |
| 400 } | 401 } |
| 401 changes->push_back(ValueStoreChange(key, old_value, NULL)); | 402 changes->push_back(ValueStoreChange(key, std::move(old_value), nullptr)); |
| 402 return syncer::SyncError(); | 403 return syncer::SyncError(); |
| 403 } | 404 } |
| 404 | 405 |
| 405 } // namespace extensions | 406 } // namespace extensions |
| OLD | NEW |