| 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/prefs/pref_model_associator.h" | 5 #include "chrome/browser/prefs/pref_model_associator.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/tracked.h" | 10 #include "base/tracked.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // we'll send the new user controlled value to the syncer. | 109 // we'll send the new user controlled value to the syncer. |
| 110 return; | 110 return; |
| 111 } | 111 } |
| 112 | 112 |
| 113 // Make sure we add it to our list of synced preferences so we know what | 113 // Make sure we add it to our list of synced preferences so we know what |
| 114 // the server is aware of. | 114 // the server is aware of. |
| 115 synced_preferences_.insert(pref_name); | 115 synced_preferences_.insert(pref_name); |
| 116 return; | 116 return; |
| 117 } | 117 } |
| 118 | 118 |
| 119 bool PrefModelAssociator::MergeDataAndStartSyncing( | 119 SyncError PrefModelAssociator::MergeDataAndStartSyncing( |
| 120 syncable::ModelType type, | 120 syncable::ModelType type, |
| 121 const SyncDataList& initial_sync_data, | 121 const SyncDataList& initial_sync_data, |
| 122 SyncChangeProcessor* sync_processor) { | 122 SyncChangeProcessor* sync_processor) { |
| 123 DCHECK_EQ(type, PREFERENCES); | 123 DCHECK_EQ(type, PREFERENCES); |
| 124 DCHECK(CalledOnValidThread()); | 124 DCHECK(CalledOnValidThread()); |
| 125 DCHECK(!sync_processor_); | 125 DCHECK(!sync_processor_); |
| 126 sync_processor_ = sync_processor; | 126 sync_processor_ = sync_processor; |
| 127 | 127 |
| 128 SyncChangeList new_changes; | 128 SyncChangeList new_changes; |
| 129 std::set<std::string> remaining_preferences = registered_preferences_; | 129 std::set<std::string> remaining_preferences = registered_preferences_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 151 | 151 |
| 152 // Go through and build sync data for any remaining preferences. | 152 // Go through and build sync data for any remaining preferences. |
| 153 for (std::set<std::string>::iterator pref_name_iter = | 153 for (std::set<std::string>::iterator pref_name_iter = |
| 154 remaining_preferences.begin(); | 154 remaining_preferences.begin(); |
| 155 pref_name_iter != remaining_preferences.end(); | 155 pref_name_iter != remaining_preferences.end(); |
| 156 ++pref_name_iter) { | 156 ++pref_name_iter) { |
| 157 InitPrefAndAssociate(SyncData(), *pref_name_iter, &new_changes); | 157 InitPrefAndAssociate(SyncData(), *pref_name_iter, &new_changes); |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Push updates to sync. | 160 // Push updates to sync. |
| 161 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 161 SyncError error = |
| 162 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
| 163 if (error.IsSet()) { |
| 164 return error; |
| 165 } |
| 166 |
| 162 models_associated_ = true; | 167 models_associated_ = true; |
| 163 return true; | 168 return SyncError(); |
| 164 } | 169 } |
| 165 | 170 |
| 166 void PrefModelAssociator::StopSyncing(syncable::ModelType type) { | 171 void PrefModelAssociator::StopSyncing(syncable::ModelType type) { |
| 167 DCHECK_EQ(type, PREFERENCES); | 172 DCHECK_EQ(type, PREFERENCES); |
| 168 models_associated_ = false; | 173 models_associated_ = false; |
| 169 sync_processor_ = NULL; | 174 sync_processor_ = NULL; |
| 170 } | 175 } |
| 171 | 176 |
| 172 Value* PrefModelAssociator::MergePreference( | 177 Value* PrefModelAssociator::MergePreference( |
| 173 const PrefService::Preference& local_pref, | 178 const PrefService::Preference& local_pref, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 continue; // This is not data we care about. | 304 continue; // This is not data we care about. |
| 300 // TODO(zea): plumb a way to read the user controlled value. | 305 // TODO(zea): plumb a way to read the user controlled value. |
| 301 SyncData sync_data; | 306 SyncData sync_data; |
| 302 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data)) | 307 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data)) |
| 303 continue; | 308 continue; |
| 304 current_data.push_back(sync_data); | 309 current_data.push_back(sync_data); |
| 305 } | 310 } |
| 306 return current_data; | 311 return current_data; |
| 307 } | 312 } |
| 308 | 313 |
| 309 void PrefModelAssociator::ProcessSyncChanges( | 314 SyncError PrefModelAssociator::ProcessSyncChanges( |
| 310 const tracked_objects::Location& from_here, | 315 const tracked_objects::Location& from_here, |
| 311 const SyncChangeList& change_list) { | 316 const SyncChangeList& change_list) { |
| 312 if (!models_associated_) | 317 if (!models_associated_) { |
| 313 return; | 318 SyncError error(FROM_HERE, |
| 319 "Models not yet associated.", |
| 320 PREFERENCES); |
| 321 return error; |
| 322 } |
| 314 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); | 323 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); |
| 315 SyncChangeList::const_iterator iter; | 324 SyncChangeList::const_iterator iter; |
| 316 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { | 325 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { |
| 317 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); | 326 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); |
| 318 | 327 |
| 319 std::string name; | 328 std::string name; |
| 320 sync_pb::PreferenceSpecifics pref_specifics = | 329 sync_pb::PreferenceSpecifics pref_specifics = |
| 321 iter->sync_data().GetSpecifics().GetExtension(sync_pb::preference); | 330 iter->sync_data().GetSpecifics().GetExtension(sync_pb::preference); |
| 322 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, | 331 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, |
| 323 &name)); | 332 &name)); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 352 // policy controlled. | 361 // policy controlled. |
| 353 pref_service_->Set(pref_name, *value); | 362 pref_service_->Set(pref_name, *value); |
| 354 | 363 |
| 355 // Keep track of any newly synced preferences. | 364 // Keep track of any newly synced preferences. |
| 356 if (iter->change_type() == SyncChange::ACTION_ADD) { | 365 if (iter->change_type() == SyncChange::ACTION_ADD) { |
| 357 synced_preferences_.insert(name); | 366 synced_preferences_.insert(name); |
| 358 } | 367 } |
| 359 | 368 |
| 360 SendUpdateNotificationsIfNecessary(name); | 369 SendUpdateNotificationsIfNecessary(name); |
| 361 } | 370 } |
| 371 return SyncError(); |
| 362 } | 372 } |
| 363 | 373 |
| 364 Value* PrefModelAssociator::ReadPreferenceSpecifics( | 374 Value* PrefModelAssociator::ReadPreferenceSpecifics( |
| 365 const sync_pb::PreferenceSpecifics& preference, | 375 const sync_pb::PreferenceSpecifics& preference, |
| 366 std::string* name) { | 376 std::string* name) { |
| 367 base::JSONReader reader; | 377 base::JSONReader reader; |
| 368 scoped_ptr<Value> value(reader.JsonToValue(preference.value(), false, false)); | 378 scoped_ptr<Value> value(reader.JsonToValue(preference.value(), false, false)); |
| 369 if (!value.get()) { | 379 if (!value.get()) { |
| 370 std::string err = "Failed to deserialize preference value: " + | 380 std::string err = "Failed to deserialize preference value: " + |
| 371 reader.GetErrorMessage(); | 381 reader.GetErrorMessage(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 InitPrefAndAssociate(SyncData(), name, &changes); | 435 InitPrefAndAssociate(SyncData(), name, &changes); |
| 426 } else { | 436 } else { |
| 427 // We are already syncing this preference, just update it's sync node. | 437 // We are already syncing this preference, just update it's sync node. |
| 428 SyncData sync_data; | 438 SyncData sync_data; |
| 429 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { | 439 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { |
| 430 LOG(ERROR) << "Failed to update preference."; | 440 LOG(ERROR) << "Failed to update preference."; |
| 431 return; | 441 return; |
| 432 } | 442 } |
| 433 changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 443 changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
| 434 } | 444 } |
| 435 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 445 |
| 446 SyncError error = |
| 447 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 448 if (error.IsSet()) |
| 449 StopSyncing(PREFERENCES); |
| 436 } | 450 } |
| OLD | NEW |