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 "components/sync_preferences/pref_model_associator.h" | 5 #include "components/sync_preferences/pref_model_associator.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 pref_service_ = NULL; | 70 pref_service_ = NULL; |
71 | 71 |
72 synced_pref_observers_.clear(); | 72 synced_pref_observers_.clear(); |
73 } | 73 } |
74 | 74 |
75 void PrefModelAssociator::InitPrefAndAssociate( | 75 void PrefModelAssociator::InitPrefAndAssociate( |
76 const syncer::SyncData& sync_pref, | 76 const syncer::SyncData& sync_pref, |
77 const std::string& pref_name, | 77 const std::string& pref_name, |
78 syncer::SyncChangeList* sync_changes) { | 78 syncer::SyncChangeList* sync_changes) { |
79 const base::Value* user_pref_value = | 79 const base::Value* user_pref_value = |
80 pref_service_->GetUserPrefValue(pref_name.c_str()); | 80 pref_service_->GetUserPrefValue(pref_name); |
81 VLOG(1) << "Associating preference " << pref_name; | 81 VLOG(1) << "Associating preference " << pref_name; |
82 | 82 |
83 if (sync_pref.IsValid()) { | 83 if (sync_pref.IsValid()) { |
84 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); | 84 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); |
85 DCHECK(pref_name == preference.name()); | 85 DCHECK(pref_name == preference.name()); |
86 base::JSONReader reader; | 86 base::JSONReader reader; |
87 std::unique_ptr<base::Value> sync_value( | 87 std::unique_ptr<base::Value> sync_value( |
88 reader.ReadToValue(preference.value())); | 88 reader.ReadToValue(preference.value())); |
89 if (!sync_value.get()) { | 89 if (!sync_value.get()) { |
90 LOG(ERROR) << "Failed to deserialize preference value: " | 90 LOG(ERROR) << "Failed to deserialize preference value: " |
91 << reader.GetErrorMessage(); | 91 << reader.GetErrorMessage(); |
92 return; | 92 return; |
93 } | 93 } |
94 | 94 |
95 if (user_pref_value) { | 95 if (user_pref_value) { |
96 DVLOG(1) << "Found user pref value for " << pref_name; | 96 DVLOG(1) << "Found user pref value for " << pref_name; |
97 // We have both server and local values. Merge them. | 97 // We have both server and local values. Merge them. |
98 std::unique_ptr<base::Value> new_value( | 98 std::unique_ptr<base::Value> new_value( |
99 MergePreference(pref_name, *user_pref_value, *sync_value)); | 99 MergePreference(pref_name, *user_pref_value, *sync_value)); |
100 | 100 |
101 // Update the local preference based on what we got from the | 101 // Update the local preference based on what we got from the |
102 // sync server. Note: this only updates the user value store, which is | 102 // sync server. Note: this only updates the user value store, which is |
103 // ignored if the preference is policy controlled. | 103 // ignored if the preference is policy controlled. |
104 if (new_value->IsType(base::Value::Type::NONE)) { | 104 if (new_value->IsType(base::Value::Type::NONE)) { |
105 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); | 105 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); |
106 pref_service_->ClearPref(pref_name.c_str()); | 106 pref_service_->ClearPref(pref_name); |
107 } else if (!new_value->IsType(user_pref_value->GetType())) { | 107 } else if (!new_value->IsType(user_pref_value->GetType())) { |
108 LOG(WARNING) << "Synced value for " << preference.name() | 108 LOG(WARNING) << "Synced value for " << preference.name() |
109 << " is of type " << new_value->GetType() | 109 << " is of type " << new_value->GetType() |
110 << " which doesn't match pref type " | 110 << " which doesn't match pref type " |
111 << user_pref_value->GetType(); | 111 << user_pref_value->GetType(); |
112 } else if (!user_pref_value->Equals(new_value.get())) { | 112 } else if (!user_pref_value->Equals(new_value.get())) { |
113 pref_service_->Set(pref_name.c_str(), *new_value); | 113 pref_service_->Set(pref_name, *new_value); |
114 } | 114 } |
115 | 115 |
116 // If the merge resulted in an updated value, inform the syncer. | 116 // If the merge resulted in an updated value, inform the syncer. |
117 if (!sync_value->Equals(new_value.get())) { | 117 if (!sync_value->Equals(new_value.get())) { |
118 syncer::SyncData sync_data; | 118 syncer::SyncData sync_data; |
119 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { | 119 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { |
120 LOG(ERROR) << "Failed to update preference."; | 120 LOG(ERROR) << "Failed to update preference."; |
121 return; | 121 return; |
122 } | 122 } |
123 | 123 |
124 sync_changes->push_back(syncer::SyncChange( | 124 sync_changes->push_back(syncer::SyncChange( |
125 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data)); | 125 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data)); |
126 } | 126 } |
127 } else if (!sync_value->IsType(base::Value::Type::NONE)) { | 127 } else if (!sync_value->IsType(base::Value::Type::NONE)) { |
128 // Only a server value exists. Just set the local user value. | 128 // Only a server value exists. Just set the local user value. |
129 pref_service_->Set(pref_name.c_str(), *sync_value); | 129 pref_service_->Set(pref_name, *sync_value); |
130 } else { | 130 } else { |
131 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); | 131 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); |
132 } | 132 } |
133 synced_preferences_.insert(preference.name()); | 133 synced_preferences_.insert(preference.name()); |
134 } else if (user_pref_value) { | 134 } else if (user_pref_value) { |
135 // The server does not know about this preference and should be added | 135 // The server does not know about this preference and should be added |
136 // to the syncer's database. | 136 // to the syncer's database. |
137 syncer::SyncData sync_data; | 137 syncer::SyncData sync_data; |
138 if (!CreatePrefSyncData(pref_name, *user_pref_value, &sync_data)) { | 138 if (!CreatePrefSyncData(pref_name, *user_pref_value, &sync_data)) { |
139 LOG(ERROR) << "Failed to update preference."; | 139 LOG(ERROR) << "Failed to update preference."; |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 // with user controlled data. We do not track any information for preferences | 336 // with user controlled data. We do not track any information for preferences |
337 // not registered locally as syncable and do not inform the syncer of | 337 // not registered locally as syncable and do not inform the syncer of |
338 // non-user controlled preferences. | 338 // non-user controlled preferences. |
339 syncer::SyncDataList PrefModelAssociator::GetAllSyncData( | 339 syncer::SyncDataList PrefModelAssociator::GetAllSyncData( |
340 syncer::ModelType type) const { | 340 syncer::ModelType type) const { |
341 DCHECK_EQ(type_, type); | 341 DCHECK_EQ(type_, type); |
342 syncer::SyncDataList current_data; | 342 syncer::SyncDataList current_data; |
343 for (PreferenceSet::const_iterator iter = synced_preferences_.begin(); | 343 for (PreferenceSet::const_iterator iter = synced_preferences_.begin(); |
344 iter != synced_preferences_.end(); ++iter) { | 344 iter != synced_preferences_.end(); ++iter) { |
345 std::string name = *iter; | 345 std::string name = *iter; |
346 const PrefService::Preference* pref = | 346 const PrefService::Preference* pref = pref_service_->FindPreference(name); |
347 pref_service_->FindPreference(name.c_str()); | |
348 DCHECK(pref); | 347 DCHECK(pref); |
349 if (!pref->IsUserControlled() || pref->IsDefaultValue()) | 348 if (!pref->IsUserControlled() || pref->IsDefaultValue()) |
350 continue; // This is not data we care about. | 349 continue; // This is not data we care about. |
351 // TODO(zea): plumb a way to read the user controlled value. | 350 // TODO(zea): plumb a way to read the user controlled value. |
352 syncer::SyncData sync_data; | 351 syncer::SyncData sync_data; |
353 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data)) | 352 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data)) |
354 continue; | 353 continue; |
355 current_data.push_back(sync_data); | 354 current_data.push_back(sync_data); |
356 } | 355 } |
357 return current_data; | 356 return current_data; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 | 467 |
469 void PrefModelAssociator::ProcessPrefChange(const std::string& name) { | 468 void PrefModelAssociator::ProcessPrefChange(const std::string& name) { |
470 if (processing_syncer_changes_) | 469 if (processing_syncer_changes_) |
471 return; // These are changes originating from us, ignore. | 470 return; // These are changes originating from us, ignore. |
472 | 471 |
473 // We only process changes if we've already associated models. | 472 // We only process changes if we've already associated models. |
474 if (!models_associated_) | 473 if (!models_associated_) |
475 return; | 474 return; |
476 | 475 |
477 const PrefService::Preference* preference = | 476 const PrefService::Preference* preference = |
478 pref_service_->FindPreference(name.c_str()); | 477 pref_service_->FindPreference(name); |
479 if (!preference) | 478 if (!preference) |
480 return; | 479 return; |
481 | 480 |
482 if (!IsPrefRegistered(name.c_str())) | 481 if (!IsPrefRegistered(name.c_str())) |
483 return; // We are not syncing this preference. | 482 return; // We are not syncing this preference. |
484 | 483 |
485 syncer::SyncChangeList changes; | 484 syncer::SyncChangeList changes; |
486 | 485 |
487 if (!preference->IsUserModifiable()) { | 486 if (!preference->IsUserModifiable()) { |
488 // If the preference is no longer user modifiable, it must now be controlled | 487 // If the preference is no longer user modifiable, it must now be controlled |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 bool from_sync) const { | 524 bool from_sync) const { |
526 auto observer_iter = synced_pref_observers_.find(path); | 525 auto observer_iter = synced_pref_observers_.find(path); |
527 if (observer_iter == synced_pref_observers_.end()) | 526 if (observer_iter == synced_pref_observers_.end()) |
528 return; | 527 return; |
529 SyncedPrefObserverList* observers = observer_iter->second.get(); | 528 SyncedPrefObserverList* observers = observer_iter->second.get(); |
530 for (auto& observer : *observers) | 529 for (auto& observer : *observers) |
531 observer.OnSyncedPrefChanged(path, from_sync); | 530 observer.OnSyncedPrefChanged(path, from_sync); |
532 } | 531 } |
533 | 532 |
534 } // namespace sync_preferences | 533 } // namespace sync_preferences |
OLD | NEW |