Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: chrome/browser/prefs/pref_model_associator.cc

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

Powered by Google App Engine
This is Rietveld 408576698