| 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 SyncError PrefModelAssociator::MergeDataAndStartSyncing( | 119 bool 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 SyncError error = | 161 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
| 162 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | |
| 163 if (error.IsSet()) { | |
| 164 return error; | |
| 165 } | |
| 166 | |
| 167 models_associated_ = true; | 162 models_associated_ = true; |
| 168 return SyncError(); | 163 return true; |
| 169 } | 164 } |
| 170 | 165 |
| 171 void PrefModelAssociator::StopSyncing(syncable::ModelType type) { | 166 void PrefModelAssociator::StopSyncing(syncable::ModelType type) { |
| 172 DCHECK_EQ(type, PREFERENCES); | 167 DCHECK_EQ(type, PREFERENCES); |
| 173 models_associated_ = false; | 168 models_associated_ = false; |
| 174 sync_processor_ = NULL; | 169 sync_processor_ = NULL; |
| 175 } | 170 } |
| 176 | 171 |
| 177 Value* PrefModelAssociator::MergePreference( | 172 Value* PrefModelAssociator::MergePreference( |
| 178 const PrefService::Preference& local_pref, | 173 const PrefService::Preference& local_pref, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 continue; // This is not data we care about. | 299 continue; // This is not data we care about. |
| 305 // TODO(zea): plumb a way to read the user controlled value. | 300 // TODO(zea): plumb a way to read the user controlled value. |
| 306 SyncData sync_data; | 301 SyncData sync_data; |
| 307 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data)) | 302 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data)) |
| 308 continue; | 303 continue; |
| 309 current_data.push_back(sync_data); | 304 current_data.push_back(sync_data); |
| 310 } | 305 } |
| 311 return current_data; | 306 return current_data; |
| 312 } | 307 } |
| 313 | 308 |
| 314 SyncError PrefModelAssociator::ProcessSyncChanges( | 309 void PrefModelAssociator::ProcessSyncChanges( |
| 315 const tracked_objects::Location& from_here, | 310 const tracked_objects::Location& from_here, |
| 316 const SyncChangeList& change_list) { | 311 const SyncChangeList& change_list) { |
| 317 if (!models_associated_) { | 312 if (!models_associated_) |
| 318 SyncError error(FROM_HERE, | 313 return; |
| 319 "Models not yet associated.", | |
| 320 PREFERENCES); | |
| 321 return error; | |
| 322 } | |
| 323 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); | 314 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); |
| 324 SyncChangeList::const_iterator iter; | 315 SyncChangeList::const_iterator iter; |
| 325 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { | 316 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { |
| 326 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); | 317 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); |
| 327 | 318 |
| 328 std::string name; | 319 std::string name; |
| 329 sync_pb::PreferenceSpecifics pref_specifics = | 320 sync_pb::PreferenceSpecifics pref_specifics = |
| 330 iter->sync_data().GetSpecifics().GetExtension(sync_pb::preference); | 321 iter->sync_data().GetSpecifics().GetExtension(sync_pb::preference); |
| 331 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, | 322 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, |
| 332 &name)); | 323 &name)); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 361 // policy controlled. | 352 // policy controlled. |
| 362 pref_service_->Set(pref_name, *value); | 353 pref_service_->Set(pref_name, *value); |
| 363 | 354 |
| 364 // Keep track of any newly synced preferences. | 355 // Keep track of any newly synced preferences. |
| 365 if (iter->change_type() == SyncChange::ACTION_ADD) { | 356 if (iter->change_type() == SyncChange::ACTION_ADD) { |
| 366 synced_preferences_.insert(name); | 357 synced_preferences_.insert(name); |
| 367 } | 358 } |
| 368 | 359 |
| 369 SendUpdateNotificationsIfNecessary(name); | 360 SendUpdateNotificationsIfNecessary(name); |
| 370 } | 361 } |
| 371 return SyncError(); | |
| 372 } | 362 } |
| 373 | 363 |
| 374 Value* PrefModelAssociator::ReadPreferenceSpecifics( | 364 Value* PrefModelAssociator::ReadPreferenceSpecifics( |
| 375 const sync_pb::PreferenceSpecifics& preference, | 365 const sync_pb::PreferenceSpecifics& preference, |
| 376 std::string* name) { | 366 std::string* name) { |
| 377 base::JSONReader reader; | 367 base::JSONReader reader; |
| 378 scoped_ptr<Value> value(reader.JsonToValue(preference.value(), false, false)); | 368 scoped_ptr<Value> value(reader.JsonToValue(preference.value(), false, false)); |
| 379 if (!value.get()) { | 369 if (!value.get()) { |
| 380 std::string err = "Failed to deserialize preference value: " + | 370 std::string err = "Failed to deserialize preference value: " + |
| 381 reader.GetErrorMessage(); | 371 reader.GetErrorMessage(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 InitPrefAndAssociate(SyncData(), name, &changes); | 425 InitPrefAndAssociate(SyncData(), name, &changes); |
| 436 } else { | 426 } else { |
| 437 // We are already syncing this preference, just update it's sync node. | 427 // We are already syncing this preference, just update it's sync node. |
| 438 SyncData sync_data; | 428 SyncData sync_data; |
| 439 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { | 429 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { |
| 440 LOG(ERROR) << "Failed to update preference."; | 430 LOG(ERROR) << "Failed to update preference."; |
| 441 return; | 431 return; |
| 442 } | 432 } |
| 443 changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 433 changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
| 444 } | 434 } |
| 445 | 435 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 446 SyncError error = | |
| 447 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | |
| 448 if (error.IsSet()) | |
| 449 StopSyncing(PREFERENCES); | |
| 450 } | 436 } |
| OLD | NEW |