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

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

Powered by Google App Engine
This is Rietveld 408576698