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" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/sync/api/sync_change.h" | 13 #include "chrome/browser/sync/api/sync_change.h" |
| 14 #include "chrome/browser/sync/api/sync_error.h" |
14 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" | 15 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" |
15 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
17 #include "content/common/json_value_serializer.h" | 18 #include "content/common/json_value_serializer.h" |
18 #include "content/common/notification_service.h" | 19 #include "content/common/notification_service.h" |
19 | 20 |
20 using syncable::PREFERENCES; | 21 using syncable::PREFERENCES; |
21 | 22 |
22 PrefModelAssociator::PrefModelAssociator() | 23 PrefModelAssociator::PrefModelAssociator() |
23 : models_associated_(false), | 24 : models_associated_(false), |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 113 |
113 // Make sure we add it to our list of synced preferences so we know what | 114 // Make sure we add it to our list of synced preferences so we know what |
114 // the server is aware of. | 115 // the server is aware of. |
115 synced_preferences_.insert(pref_name); | 116 synced_preferences_.insert(pref_name); |
116 return; | 117 return; |
117 } | 118 } |
118 | 119 |
119 bool PrefModelAssociator::MergeDataAndStartSyncing( | 120 bool PrefModelAssociator::MergeDataAndStartSyncing( |
120 syncable::ModelType type, | 121 syncable::ModelType type, |
121 const SyncDataList& initial_sync_data, | 122 const SyncDataList& initial_sync_data, |
122 SyncChangeProcessor* sync_processor) { | 123 SyncChangeProcessor* sync_processor, |
| 124 SyncError* error) { |
123 DCHECK_EQ(type, PREFERENCES); | 125 DCHECK_EQ(type, PREFERENCES); |
124 DCHECK(CalledOnValidThread()); | 126 DCHECK(CalledOnValidThread()); |
125 DCHECK(!sync_processor_); | 127 DCHECK(!sync_processor_); |
126 sync_processor_ = sync_processor; | 128 sync_processor_ = sync_processor; |
127 | 129 |
128 SyncChangeList new_changes; | 130 SyncChangeList new_changes; |
129 std::set<std::string> remaining_preferences = registered_preferences_; | 131 std::set<std::string> remaining_preferences = registered_preferences_; |
130 | 132 |
131 // Go through and check for all preferences we care about that sync already | 133 // Go through and check for all preferences we care about that sync already |
132 // knows about. | 134 // knows about. |
(...skipping 18 matching lines...) Expand all Loading... |
151 | 153 |
152 // Go through and build sync data for any remaining preferences. | 154 // Go through and build sync data for any remaining preferences. |
153 for (std::set<std::string>::iterator pref_name_iter = | 155 for (std::set<std::string>::iterator pref_name_iter = |
154 remaining_preferences.begin(); | 156 remaining_preferences.begin(); |
155 pref_name_iter != remaining_preferences.end(); | 157 pref_name_iter != remaining_preferences.end(); |
156 ++pref_name_iter) { | 158 ++pref_name_iter) { |
157 InitPrefAndAssociate(SyncData(), *pref_name_iter, &new_changes); | 159 InitPrefAndAssociate(SyncData(), *pref_name_iter, &new_changes); |
158 } | 160 } |
159 | 161 |
160 // Push updates to sync. | 162 // Push updates to sync. |
161 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 163 if (!sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes, error)) { |
162 models_associated_ = true; | 164 return false; |
163 return true; | 165 } else { |
| 166 models_associated_ = true; |
| 167 return true; |
| 168 } |
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 bool 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 SyncError* error) { |
313 return; | 318 if (!models_associated_) { |
| 319 error->Reset(FROM_HERE, |
| 320 "Models not yet associated.", |
| 321 PREFERENCES); |
| 322 return false; |
| 323 } |
314 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); | 324 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); |
315 SyncChangeList::const_iterator iter; | 325 SyncChangeList::const_iterator iter; |
316 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { | 326 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { |
317 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); | 327 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); |
318 | 328 |
319 std::string name; | 329 std::string name; |
320 sync_pb::PreferenceSpecifics pref_specifics = | 330 sync_pb::PreferenceSpecifics pref_specifics = |
321 iter->sync_data().GetSpecifics().GetExtension(sync_pb::preference); | 331 iter->sync_data().GetSpecifics().GetExtension(sync_pb::preference); |
322 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, | 332 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, |
323 &name)); | 333 &name)); |
(...skipping 28 matching lines...) Expand all Loading... |
352 // policy controlled. | 362 // policy controlled. |
353 pref_service_->Set(pref_name, *value); | 363 pref_service_->Set(pref_name, *value); |
354 | 364 |
355 // Keep track of any newly synced preferences. | 365 // Keep track of any newly synced preferences. |
356 if (iter->change_type() == SyncChange::ACTION_ADD) { | 366 if (iter->change_type() == SyncChange::ACTION_ADD) { |
357 synced_preferences_.insert(name); | 367 synced_preferences_.insert(name); |
358 } | 368 } |
359 | 369 |
360 SendUpdateNotificationsIfNecessary(name); | 370 SendUpdateNotificationsIfNecessary(name); |
361 } | 371 } |
| 372 return true; |
362 } | 373 } |
363 | 374 |
364 Value* PrefModelAssociator::ReadPreferenceSpecifics( | 375 Value* PrefModelAssociator::ReadPreferenceSpecifics( |
365 const sync_pb::PreferenceSpecifics& preference, | 376 const sync_pb::PreferenceSpecifics& preference, |
366 std::string* name) { | 377 std::string* name) { |
367 base::JSONReader reader; | 378 base::JSONReader reader; |
368 scoped_ptr<Value> value(reader.JsonToValue(preference.value(), false, false)); | 379 scoped_ptr<Value> value(reader.JsonToValue(preference.value(), false, false)); |
369 if (!value.get()) { | 380 if (!value.get()) { |
370 std::string err = "Failed to deserialize preference value: " + | 381 std::string err = "Failed to deserialize preference value: " + |
371 reader.GetErrorMessage(); | 382 reader.GetErrorMessage(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 InitPrefAndAssociate(SyncData(), name, &changes); | 436 InitPrefAndAssociate(SyncData(), name, &changes); |
426 } else { | 437 } else { |
427 // We are already syncing this preference, just update it's sync node. | 438 // We are already syncing this preference, just update it's sync node. |
428 SyncData sync_data; | 439 SyncData sync_data; |
429 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { | 440 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { |
430 LOG(ERROR) << "Failed to update preference."; | 441 LOG(ERROR) << "Failed to update preference."; |
431 return; | 442 return; |
432 } | 443 } |
433 changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 444 changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
434 } | 445 } |
435 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 446 |
| 447 SyncError error; |
| 448 if (!sync_processor_->ProcessSyncChanges(FROM_HERE, changes, &error)) |
| 449 StopSyncing(PREFERENCES); |
436 } | 450 } |
OLD | NEW |