| 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/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/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/sync/api/sync_change.h" | 14 #include "chrome/browser/sync/api/sync_change.h" |
| 15 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" | 15 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" |
| 16 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 18 | 19 |
| 19 using syncable::PREFERENCES; | 20 using syncable::PREFERENCES; |
| 20 | 21 |
| 21 PrefModelAssociator::PrefModelAssociator() | 22 PrefModelAssociator::PrefModelAssociator() |
| 22 : models_associated_(false), | 23 : models_associated_(false), |
| 23 processing_syncer_changes_(false), | 24 processing_syncer_changes_(false), |
| 24 pref_service_(NULL), | 25 pref_service_(NULL), |
| 25 sync_processor_(NULL) { | 26 sync_processor_(NULL) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 39 const PrefService::Preference* pref = | 40 const PrefService::Preference* pref = |
| 40 pref_service_->FindPreference(pref_name.c_str()); | 41 pref_service_->FindPreference(pref_name.c_str()); |
| 41 DCHECK(pref); | 42 DCHECK(pref); |
| 42 VLOG(1) << "Associating preference " << pref_name; | 43 VLOG(1) << "Associating preference " << pref_name; |
| 43 | 44 |
| 44 base::JSONReader reader; | 45 base::JSONReader reader; |
| 45 if (sync_pref.IsValid()) { | 46 if (sync_pref.IsValid()) { |
| 46 // The server has a value for the preference, we have to reconcile it with | 47 // The server has a value for the preference, we have to reconcile it with |
| 47 // ours. | 48 // ours. |
| 48 const sync_pb::PreferenceSpecifics& preference = | 49 const sync_pb::PreferenceSpecifics& preference = |
| 49 sync_pref.GetSpecifics().GetExtension(sync_pb::preference); | 50 sync_pref.GetSpecifics().preference(); |
| 50 DCHECK_EQ(pref->name(), preference.name()); | 51 DCHECK_EQ(pref->name(), preference.name()); |
| 51 | 52 |
| 52 scoped_ptr<Value> value( | 53 scoped_ptr<Value> value( |
| 53 reader.JsonToValue(preference.value(), false, false)); | 54 reader.JsonToValue(preference.value(), false, false)); |
| 54 if (!value.get()) { | 55 if (!value.get()) { |
| 55 LOG(ERROR) << "Failed to deserialize preference value: " | 56 LOG(ERROR) << "Failed to deserialize preference value: " |
| 56 << reader.GetErrorMessage(); | 57 << reader.GetErrorMessage(); |
| 57 return; | 58 return; |
| 58 } | 59 } |
| 59 | 60 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 118 |
| 118 SyncChangeList new_changes; | 119 SyncChangeList new_changes; |
| 119 std::set<std::string> remaining_preferences = registered_preferences_; | 120 std::set<std::string> remaining_preferences = registered_preferences_; |
| 120 | 121 |
| 121 // Go through and check for all preferences we care about that sync already | 122 // Go through and check for all preferences we care about that sync already |
| 122 // knows about. | 123 // knows about. |
| 123 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); | 124 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); |
| 124 sync_iter != initial_sync_data.end(); | 125 sync_iter != initial_sync_data.end(); |
| 125 ++sync_iter) { | 126 ++sync_iter) { |
| 126 DCHECK_EQ(PREFERENCES, sync_iter->GetDataType()); | 127 DCHECK_EQ(PREFERENCES, sync_iter->GetDataType()); |
| 127 std::string sync_pref_name = sync_iter->GetSpecifics(). | 128 std::string sync_pref_name = sync_iter->GetSpecifics().preference().name(); |
| 128 GetExtension(sync_pb::preference).name(); | |
| 129 if (remaining_preferences.count(sync_pref_name) == 0) { | 129 if (remaining_preferences.count(sync_pref_name) == 0) { |
| 130 // We're not syncing this preference locally, ignore the sync data. | 130 // We're not syncing this preference locally, ignore the sync data. |
| 131 // TODO(zea): Eventually we want to be able to have the syncable service | 131 // TODO(zea): Eventually we want to be able to have the syncable service |
| 132 // reconstruct all sync data for it's datatype (therefore having | 132 // reconstruct all sync data for it's datatype (therefore having |
| 133 // GetAllSyncData be a complete representation). We should store this data | 133 // GetAllSyncData be a complete representation). We should store this data |
| 134 // somewhere, even if we don't use it. | 134 // somewhere, even if we don't use it. |
| 135 continue; | 135 continue; |
| 136 } | 136 } |
| 137 | 137 |
| 138 remaining_preferences.erase(sync_pref_name); | 138 remaining_preferences.erase(sync_pref_name); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 std::string serialized; | 191 std::string serialized; |
| 192 // TODO(zea): consider JSONWriter::Write since you don't have to check | 192 // TODO(zea): consider JSONWriter::Write since you don't have to check |
| 193 // failures to deserialize. | 193 // failures to deserialize. |
| 194 JSONStringValueSerializer json(&serialized); | 194 JSONStringValueSerializer json(&serialized); |
| 195 if (!json.Serialize(value)) { | 195 if (!json.Serialize(value)) { |
| 196 LOG(ERROR) << "Failed to serialize preference value."; | 196 LOG(ERROR) << "Failed to serialize preference value."; |
| 197 return false; | 197 return false; |
| 198 } | 198 } |
| 199 | 199 |
| 200 sync_pb::EntitySpecifics specifics; | 200 sync_pb::EntitySpecifics specifics; |
| 201 sync_pb::PreferenceSpecifics* pref_specifics = specifics.MutableExtension( | 201 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); |
| 202 sync_pb::preference); | |
| 203 pref_specifics->set_name(name); | 202 pref_specifics->set_name(name); |
| 204 pref_specifics->set_value(serialized); | 203 pref_specifics->set_value(serialized); |
| 205 *sync_data = SyncData::CreateLocalData(name, name, specifics); | 204 *sync_data = SyncData::CreateLocalData(name, name, specifics); |
| 206 return true; | 205 return true; |
| 207 } | 206 } |
| 208 | 207 |
| 209 Value* PrefModelAssociator::MergeListValues(const Value& from_value, | 208 Value* PrefModelAssociator::MergeListValues(const Value& from_value, |
| 210 const Value& to_value) { | 209 const Value& to_value) { |
| 211 if (from_value.GetType() == Value::TYPE_NULL) | 210 if (from_value.GetType() == Value::TYPE_NULL) |
| 212 return to_value.DeepCopy(); | 211 return to_value.DeepCopy(); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 PREFERENCES); | 298 PREFERENCES); |
| 300 return error; | 299 return error; |
| 301 } | 300 } |
| 302 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); | 301 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); |
| 303 SyncChangeList::const_iterator iter; | 302 SyncChangeList::const_iterator iter; |
| 304 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { | 303 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { |
| 305 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); | 304 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); |
| 306 | 305 |
| 307 std::string name; | 306 std::string name; |
| 308 sync_pb::PreferenceSpecifics pref_specifics = | 307 sync_pb::PreferenceSpecifics pref_specifics = |
| 309 iter->sync_data().GetSpecifics().GetExtension(sync_pb::preference); | 308 iter->sync_data().GetSpecifics().preference(); |
| 310 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, | 309 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, |
| 311 &name)); | 310 &name)); |
| 312 | 311 |
| 313 if (iter->change_type() == SyncChange::ACTION_DELETE) { | 312 if (iter->change_type() == SyncChange::ACTION_DELETE) { |
| 314 // We never delete preferences. | 313 // We never delete preferences. |
| 315 NOTREACHED() << "Attempted to process sync delete change for " << name | 314 NOTREACHED() << "Attempted to process sync delete change for " << name |
| 316 << ". Skipping."; | 315 << ". Skipping."; |
| 317 continue; | 316 continue; |
| 318 } | 317 } |
| 319 | 318 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 SyncError error = | 426 SyncError error = |
| 428 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 427 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 429 if (error.IsSet()) | 428 if (error.IsSet()) |
| 430 StopSyncing(PREFERENCES); | 429 StopSyncing(PREFERENCES); |
| 431 } | 430 } |
| 432 | 431 |
| 433 void PrefModelAssociator::SetPrefService(PrefService* pref_service) { | 432 void PrefModelAssociator::SetPrefService(PrefService* pref_service) { |
| 434 DCHECK(pref_service_ == NULL); | 433 DCHECK(pref_service_ == NULL); |
| 435 pref_service_ = pref_service; | 434 pref_service_ = pref_service; |
| 436 } | 435 } |
| OLD | NEW |