 Chromium Code Reviews
 Chromium Code Reviews Issue 12033093:
  sync: Implementation of Priority Preferences.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 12033093:
  sync: Implementation of Priority Preferences.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| 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/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" | 
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" | 
| 14 #include "base/values.h" | 14 #include "base/values.h" | 
| 15 #include "chrome/browser/prefs/pref_service_syncable.h" | 15 #include "chrome/browser/prefs/pref_service_syncable.h" | 
| 16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" | 
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" | 
| 18 #include "sync/api/sync_change.h" | 18 #include "sync/api/sync_change.h" | 
| 19 #include "sync/api/sync_error_factory.h" | 19 #include "sync/api/sync_error_factory.h" | 
| 20 #include "sync/protocol/preference_specifics.pb.h" | 20 #include "sync/protocol/preference_specifics.pb.h" | 
| 21 #include "sync/protocol/sync.pb.h" | 21 #include "sync/protocol/sync.pb.h" | 
| 22 | 22 | 
| 23 using syncer::PREFERENCES; | 23 using syncer::PREFERENCES; | 
| 24 using syncer::PRIORITY_PREFERENCES; | |
| 24 | 25 | 
| 25 PrefModelAssociator::PrefModelAssociator() | 26 namespace { | 
| 27 | |
| 28 const sync_pb::PreferenceSpecifics& GetSpecifics(const syncer::ModelType& type, | |
| 
Nicolas Zea
2013/03/05 01:15:44
|type| isn't needed in either of these methods (yo
 
albertb
2013/03/15 21:08:45
Done for the first one. But the second one needs t
 | |
| 29 const syncer::SyncData& pref) { | |
| 30 DCHECK(type == PREFERENCES || type == PRIORITY_PREFERENCES); | |
| 31 if (type == syncer::PREFERENCES) { | |
| 32 return pref.GetSpecifics().preference(); | |
| 33 } else { | |
| 34 return pref.GetSpecifics().priority_preference().preference(); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 sync_pb::PreferenceSpecifics* GetMutableSpecifics( | |
| 39 const syncer::ModelType& type, | |
| 40 sync_pb::EntitySpecifics* specifics) { | |
| 41 DCHECK(type == PREFERENCES || type == PRIORITY_PREFERENCES); | |
| 42 if (type == syncer::PREFERENCES) { | |
| 43 return specifics->mutable_preference(); | |
| 44 } else { | |
| 45 return specifics->mutable_priority_preference()->mutable_preference(); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 PrefModelAssociator::PrefModelAssociator(syncer::ModelType type) | |
| 26 : models_associated_(false), | 52 : models_associated_(false), | 
| 27 processing_syncer_changes_(false), | 53 processing_syncer_changes_(false), | 
| 28 pref_service_(NULL) { | 54 pref_service_(NULL), | 
| 55 type_(type) { | |
| 29 DCHECK(CalledOnValidThread()); | 56 DCHECK(CalledOnValidThread()); | 
| 57 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES); | |
| 30 } | 58 } | 
| 31 | 59 | 
| 32 PrefModelAssociator::~PrefModelAssociator() { | 60 PrefModelAssociator::~PrefModelAssociator() { | 
| 33 DCHECK(CalledOnValidThread()); | 61 DCHECK(CalledOnValidThread()); | 
| 34 pref_service_ = NULL; | 62 pref_service_ = NULL; | 
| 35 } | 63 } | 
| 36 | 64 | 
| 37 void PrefModelAssociator::InitPrefAndAssociate( | 65 void PrefModelAssociator::InitPrefAndAssociate( | 
| 38 const syncer::SyncData& sync_pref, | 66 const syncer::SyncData& sync_pref, | 
| 39 const std::string& pref_name, | 67 const std::string& pref_name, | 
| 40 syncer::SyncChangeList* sync_changes) { | 68 syncer::SyncChangeList* sync_changes) { | 
| 41 const Value* user_pref_value = pref_service_->GetUserPrefValue( | 69 const Value* user_pref_value = pref_service_->GetUserPrefValue( | 
| 42 pref_name.c_str()); | 70 pref_name.c_str()); | 
| 43 VLOG(1) << "Associating preference " << pref_name; | 71 VLOG(1) << "Associating preference " << pref_name; | 
| 44 | 72 | 
| 45 if (sync_pref.IsValid()) { | 73 if (sync_pref.IsValid()) { | 
| 46 const sync_pb::PreferenceSpecifics& preference = | 74 const sync_pb::PreferenceSpecifics& preference = | 
| 47 sync_pref.GetSpecifics().preference(); | 75 GetSpecifics(type_, sync_pref); | 
| 48 DCHECK_EQ(pref_name, preference.name()); | 76 DCHECK_EQ(pref_name, preference.name()); | 
| 49 | 77 | 
| 50 base::JSONReader reader; | 78 base::JSONReader reader; | 
| 51 scoped_ptr<Value> sync_value(reader.ReadToValue(preference.value())); | 79 scoped_ptr<Value> sync_value(reader.ReadToValue(preference.value())); | 
| 52 if (!sync_value.get()) { | 80 if (!sync_value.get()) { | 
| 53 LOG(ERROR) << "Failed to deserialize preference value: " | 81 LOG(ERROR) << "Failed to deserialize preference value: " | 
| 54 << reader.GetErrorMessage(); | 82 << reader.GetErrorMessage(); | 
| 55 return; | 83 return; | 
| 56 } | 84 } | 
| 57 | 85 | 
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 // the server is aware of. | 145 // the server is aware of. | 
| 118 synced_preferences_.insert(pref_name); | 146 synced_preferences_.insert(pref_name); | 
| 119 return; | 147 return; | 
| 120 } | 148 } | 
| 121 | 149 | 
| 122 syncer::SyncMergeResult PrefModelAssociator::MergeDataAndStartSyncing( | 150 syncer::SyncMergeResult PrefModelAssociator::MergeDataAndStartSyncing( | 
| 123 syncer::ModelType type, | 151 syncer::ModelType type, | 
| 124 const syncer::SyncDataList& initial_sync_data, | 152 const syncer::SyncDataList& initial_sync_data, | 
| 125 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 153 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 
| 126 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { | 154 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { | 
| 127 DCHECK_EQ(type, PREFERENCES); | 155 DCHECK_EQ(type_, type); | 
| 128 DCHECK(CalledOnValidThread()); | 156 DCHECK(CalledOnValidThread()); | 
| 129 DCHECK(pref_service_); | 157 DCHECK(pref_service_); | 
| 130 DCHECK(!sync_processor_.get()); | 158 DCHECK(!sync_processor_.get()); | 
| 131 DCHECK(sync_processor.get()); | 159 DCHECK(sync_processor.get()); | 
| 132 DCHECK(sync_error_factory.get()); | 160 DCHECK(sync_error_factory.get()); | 
| 133 syncer::SyncMergeResult merge_result(type); | 161 syncer::SyncMergeResult merge_result(type); | 
| 134 sync_processor_ = sync_processor.Pass(); | 162 sync_processor_ = sync_processor.Pass(); | 
| 135 sync_error_factory_ = sync_error_factory.Pass(); | 163 sync_error_factory_ = sync_error_factory.Pass(); | 
| 136 | 164 | 
| 137 syncer::SyncChangeList new_changes; | 165 syncer::SyncChangeList new_changes; | 
| 138 std::set<std::string> remaining_preferences = registered_preferences_; | 166 std::set<std::string> remaining_preferences = registered_preferences_; | 
| 139 | 167 | 
| 140 // Go through and check for all preferences we care about that sync already | 168 // Go through and check for all preferences we care about that sync already | 
| 141 // knows about. | 169 // knows about. | 
| 142 for (syncer::SyncDataList::const_iterator sync_iter = | 170 for (syncer::SyncDataList::const_iterator sync_iter = | 
| 143 initial_sync_data.begin(); | 171 initial_sync_data.begin(); | 
| 144 sync_iter != initial_sync_data.end(); | 172 sync_iter != initial_sync_data.end(); | 
| 145 ++sync_iter) { | 173 ++sync_iter) { | 
| 146 DCHECK_EQ(PREFERENCES, sync_iter->GetDataType()); | 174 DCHECK_EQ(type_, sync_iter->GetDataType()); | 
| 147 std::string sync_pref_name = sync_iter->GetSpecifics().preference().name(); | 175 | 
| 176 const sync_pb::PreferenceSpecifics& preference = | |
| 177 GetSpecifics(type_, *sync_iter); | |
| 178 const std::string& sync_pref_name = preference.name(); | |
| 179 | |
| 148 if (remaining_preferences.count(sync_pref_name) == 0) { | 180 if (remaining_preferences.count(sync_pref_name) == 0) { | 
| 149 // We're not syncing this preference locally, ignore the sync data. | 181 // We're not syncing this preference locally, ignore the sync data. | 
| 150 // TODO(zea): Eventually we want to be able to have the syncable service | 182 // TODO(zea): Eventually we want to be able to have the syncable service | 
| 151 // reconstruct all sync data for it's datatype (therefore having | 183 // reconstruct all sync data for it's datatype (therefore having | 
| 152 // GetAllSyncData be a complete representation). We should store this data | 184 // GetAllSyncData be a complete representation). We should store this data | 
| 153 // somewhere, even if we don't use it. | 185 // somewhere, even if we don't use it. | 
| 154 continue; | 186 continue; | 
| 155 } | 187 } | 
| 156 | 188 | 
| 157 remaining_preferences.erase(sync_pref_name); | 189 remaining_preferences.erase(sync_pref_name); | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 171 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); | 203 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); | 
| 172 if (merge_result.error().IsSet()) | 204 if (merge_result.error().IsSet()) | 
| 173 return merge_result; | 205 return merge_result; | 
| 174 | 206 | 
| 175 models_associated_ = true; | 207 models_associated_ = true; | 
| 176 pref_service_->OnIsSyncingChanged(); | 208 pref_service_->OnIsSyncingChanged(); | 
| 177 return merge_result; | 209 return merge_result; | 
| 178 } | 210 } | 
| 179 | 211 | 
| 180 void PrefModelAssociator::StopSyncing(syncer::ModelType type) { | 212 void PrefModelAssociator::StopSyncing(syncer::ModelType type) { | 
| 181 DCHECK_EQ(type, PREFERENCES); | 213 DCHECK_EQ(type_, type); | 
| 182 models_associated_ = false; | 214 models_associated_ = false; | 
| 183 sync_processor_.reset(); | 215 sync_processor_.reset(); | 
| 184 sync_error_factory_.reset(); | 216 sync_error_factory_.reset(); | 
| 185 pref_service_->OnIsSyncingChanged(); | 217 pref_service_->OnIsSyncingChanged(); | 
| 186 } | 218 } | 
| 187 | 219 | 
| 188 scoped_ptr<Value> PrefModelAssociator::MergePreference( | 220 scoped_ptr<Value> PrefModelAssociator::MergePreference( | 
| 189 const std::string& name, | 221 const std::string& name, | 
| 190 const Value& local_value, | 222 const Value& local_value, | 
| 191 const Value& server_value) { | 223 const Value& server_value) { | 
| 192 if (name == prefs::kURLsToRestoreOnStartup) { | 224 if (name == prefs::kURLsToRestoreOnStartup) { | 
| 193 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); | 225 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); | 
| 194 } | 226 } | 
| 195 | 227 | 
| 196 if (name == prefs::kContentSettingsPatternPairs) { | 228 if (name == prefs::kContentSettingsPatternPairs) { | 
| 197 return scoped_ptr<Value>( | 229 return scoped_ptr<Value>( | 
| 198 MergeDictionaryValues(local_value, server_value)).Pass(); | 230 MergeDictionaryValues(local_value, server_value)).Pass(); | 
| 199 } | 231 } | 
| 200 | 232 | 
| 201 // If this is not a specially handled preference, server wins. | 233 // If this is not a specially handled preference, server wins. | 
| 202 return scoped_ptr<Value>(server_value.DeepCopy()).Pass(); | 234 return scoped_ptr<Value>(server_value.DeepCopy()).Pass(); | 
| 203 } | 235 } | 
| 204 | 236 | 
| 205 bool PrefModelAssociator::CreatePrefSyncData( | 237 bool PrefModelAssociator::CreatePrefSyncData( | 
| 206 const std::string& name, | 238 const std::string& name, | 
| 207 const Value& value, | 239 const Value& value, | 
| 208 syncer::SyncData* sync_data) { | 240 syncer::SyncData* sync_data) const { | 
| 209 if (value.IsType(Value::TYPE_NULL)) { | 241 if (value.IsType(Value::TYPE_NULL)) { | 
| 210 LOG(ERROR) << "Attempting to sync a null pref value for " << name; | 242 LOG(ERROR) << "Attempting to sync a null pref value for " << name; | 
| 211 return false; | 243 return false; | 
| 212 } | 244 } | 
| 213 | 245 | 
| 214 std::string serialized; | 246 std::string serialized; | 
| 215 // TODO(zea): consider JSONWriter::Write since you don't have to check | 247 // TODO(zea): consider JSONWriter::Write since you don't have to check | 
| 216 // failures to deserialize. | 248 // failures to deserialize. | 
| 217 JSONStringValueSerializer json(&serialized); | 249 JSONStringValueSerializer json(&serialized); | 
| 218 if (!json.Serialize(value)) { | 250 if (!json.Serialize(value)) { | 
| 219 LOG(ERROR) << "Failed to serialize preference value."; | 251 LOG(ERROR) << "Failed to serialize preference value."; | 
| 220 return false; | 252 return false; | 
| 221 } | 253 } | 
| 222 | 254 | 
| 223 sync_pb::EntitySpecifics specifics; | 255 sync_pb::EntitySpecifics specifics; | 
| 224 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); | 256 sync_pb::PreferenceSpecifics* pref_specifics = | 
| 257 GetMutableSpecifics(type_, &specifics); | |
| 258 | |
| 225 pref_specifics->set_name(name); | 259 pref_specifics->set_name(name); | 
| 226 pref_specifics->set_value(serialized); | 260 pref_specifics->set_value(serialized); | 
| 261 | |
| 227 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics); | 262 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics); | 
| 228 return true; | 263 return true; | 
| 229 } | 264 } | 
| 230 | 265 | 
| 231 Value* PrefModelAssociator::MergeListValues(const Value& from_value, | 266 Value* PrefModelAssociator::MergeListValues(const Value& from_value, | 
| 232 const Value& to_value) { | 267 const Value& to_value) { | 
| 233 if (from_value.GetType() == Value::TYPE_NULL) | 268 if (from_value.GetType() == Value::TYPE_NULL) | 
| 234 return to_value.DeepCopy(); | 269 return to_value.DeepCopy(); | 
| 235 if (to_value.GetType() == Value::TYPE_NULL) | 270 if (to_value.GetType() == Value::TYPE_NULL) | 
| 236 return from_value.DeepCopy(); | 271 return from_value.DeepCopy(); | 
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 return result; | 318 return result; | 
| 284 } | 319 } | 
| 285 | 320 | 
| 286 // Note: This will build a model of all preferences registered as syncable | 321 // Note: This will build a model of all preferences registered as syncable | 
| 287 // with user controlled data. We do not track any information for preferences | 322 // with user controlled data. We do not track any information for preferences | 
| 288 // not registered locally as syncable and do not inform the syncer of | 323 // not registered locally as syncable and do not inform the syncer of | 
| 289 // non-user controlled preferences. | 324 // non-user controlled preferences. | 
| 290 syncer::SyncDataList PrefModelAssociator::GetAllSyncData( | 325 syncer::SyncDataList PrefModelAssociator::GetAllSyncData( | 
| 291 syncer::ModelType type) | 326 syncer::ModelType type) | 
| 292 const { | 327 const { | 
| 293 DCHECK_EQ(PREFERENCES, type); | 328 DCHECK_EQ(type_, type); | 
| 294 syncer::SyncDataList current_data; | 329 syncer::SyncDataList current_data; | 
| 295 for (PreferenceSet::const_iterator iter = synced_preferences_.begin(); | 330 for (PreferenceSet::const_iterator iter = synced_preferences_.begin(); | 
| 296 iter != synced_preferences_.end(); | 331 iter != synced_preferences_.end(); | 
| 297 ++iter) { | 332 ++iter) { | 
| 298 std::string name = *iter; | 333 std::string name = *iter; | 
| 299 const PrefService::Preference* pref = | 334 const PrefService::Preference* pref = | 
| 300 pref_service_->FindPreference(name.c_str()); | 335 pref_service_->FindPreference(name.c_str()); | 
| 301 DCHECK(pref); | 336 DCHECK(pref); | 
| 302 if (!pref->IsUserControlled() || pref->IsDefaultValue()) | 337 if (!pref->IsUserControlled() || pref->IsDefaultValue()) | 
| 303 continue; // This is not data we care about. | 338 continue; // This is not data we care about. | 
| (...skipping 11 matching lines...) Expand all Loading... | |
| 315 const syncer::SyncChangeList& change_list) { | 350 const syncer::SyncChangeList& change_list) { | 
| 316 if (!models_associated_) { | 351 if (!models_associated_) { | 
| 317 syncer::SyncError error(FROM_HERE, | 352 syncer::SyncError error(FROM_HERE, | 
| 318 "Models not yet associated.", | 353 "Models not yet associated.", | 
| 319 PREFERENCES); | 354 PREFERENCES); | 
| 320 return error; | 355 return error; | 
| 321 } | 356 } | 
| 322 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true); | 357 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true); | 
| 323 syncer::SyncChangeList::const_iterator iter; | 358 syncer::SyncChangeList::const_iterator iter; | 
| 324 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { | 359 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { | 
| 325 DCHECK_EQ(PREFERENCES, iter->sync_data().GetDataType()); | 360 DCHECK_EQ(type_, iter->sync_data().GetDataType()); | 
| 326 | 361 | 
| 327 std::string name; | 362 std::string name; | 
| 328 sync_pb::PreferenceSpecifics pref_specifics = | 363 const sync_pb::PreferenceSpecifics& pref_specifics = | 
| 329 iter->sync_data().GetSpecifics().preference(); | 364 GetSpecifics(type_, iter->sync_data()); | 
| 365 | |
| 330 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, | 366 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, | 
| 331 &name)); | 367 &name)); | 
| 332 | 368 | 
| 333 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) { | 369 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) { | 
| 334 // We never delete preferences. | 370 // We never delete preferences. | 
| 335 NOTREACHED() << "Attempted to process sync delete change for " << name | 371 NOTREACHED() << "Attempted to process sync delete change for " << name | 
| 336 << ". Skipping."; | 372 << ". Skipping."; | 
| 337 continue; | 373 continue; | 
| 338 } | 374 } | 
| 339 | 375 | 
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 } | 479 } | 
| 444 | 480 | 
| 445 syncer::SyncError error = | 481 syncer::SyncError error = | 
| 446 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 482 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 
| 447 } | 483 } | 
| 448 | 484 | 
| 449 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { | 485 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { | 
| 450 DCHECK(pref_service_ == NULL); | 486 DCHECK(pref_service_ == NULL); | 
| 451 pref_service_ = pref_service; | 487 pref_service_ = pref_service; | 
| 452 } | 488 } | 
| OLD | NEW |