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

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

Issue 6905044: Refactor preference syncing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review Created 9 years, 8 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_service.h" 5 #include "chrome/browser/prefs/pref_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/message_loop.h" 14 #include "base/message_loop.h"
15 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
16 #include "base/stl_util-inl.h" 16 #include "base/stl_util-inl.h"
17 #include "base/string_number_conversions.h" 17 #include "base/string_number_conversions.h"
18 #include "base/string_util.h" 18 #include "base/string_util.h"
19 #include "base/value_conversions.h" 19 #include "base/value_conversions.h"
20 #include "build/build_config.h" 20 #include "build/build_config.h"
21 #include "chrome/browser/extensions/extension_pref_store.h" 21 #include "chrome/browser/extensions/extension_pref_store.h"
22 #include "chrome/browser/policy/configuration_policy_pref_store.h" 22 #include "chrome/browser/policy/configuration_policy_pref_store.h"
23 #include "chrome/browser/prefs/command_line_pref_store.h" 23 #include "chrome/browser/prefs/command_line_pref_store.h"
24 #include "chrome/browser/prefs/default_pref_store.h" 24 #include "chrome/browser/prefs/default_pref_store.h"
25 #include "chrome/browser/prefs/overlay_persistent_pref_store.h" 25 #include "chrome/browser/prefs/overlay_persistent_pref_store.h"
26 #include "chrome/browser/prefs/pref_model_associator.h"
26 #include "chrome/browser/prefs/pref_notifier_impl.h" 27 #include "chrome/browser/prefs/pref_notifier_impl.h"
27 #include "chrome/browser/prefs/pref_value_store.h" 28 #include "chrome/browser/prefs/pref_value_store.h"
28 #include "chrome/browser/ui/profile_error_dialog.h" 29 #include "chrome/browser/ui/profile_error_dialog.h"
29 #include "chrome/common/json_pref_store.h" 30 #include "chrome/common/json_pref_store.h"
30 #include "content/browser/browser_thread.h" 31 #include "content/browser/browser_thread.h"
31 #include "content/common/notification_service.h" 32 #include "content/common/notification_service.h"
32 #include "grit/chromium_strings.h" 33 #include "grit/chromium_strings.h"
33 #include "grit/generated_resources.h" 34 #include "grit/generated_resources.h"
34 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
35 36
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 PrefStore* extension_prefs, 143 PrefStore* extension_prefs,
143 PrefStore* command_line_prefs, 144 PrefStore* command_line_prefs,
144 PersistentPrefStore* user_prefs, 145 PersistentPrefStore* user_prefs,
145 PrefStore* recommended_platform_prefs, 146 PrefStore* recommended_platform_prefs,
146 PrefStore* recommended_cloud_prefs, 147 PrefStore* recommended_cloud_prefs,
147 DefaultPrefStore* default_store, 148 DefaultPrefStore* default_store,
148 PrefServiceDelegate* delegate) 149 PrefServiceDelegate* delegate)
149 : user_pref_store_(user_prefs), 150 : user_pref_store_(user_prefs),
150 default_store_(default_store), 151 default_store_(default_store),
151 delegate_(delegate) { 152 delegate_(delegate) {
153 pref_sync_associator_ = new PrefModelAssociator(this);
152 pref_notifier_.reset(new PrefNotifierImpl(this)); 154 pref_notifier_.reset(new PrefNotifierImpl(this));
153 pref_value_store_.reset( 155 pref_value_store_.reset(
154 new PrefValueStore(managed_platform_prefs, 156 new PrefValueStore(managed_platform_prefs,
155 managed_cloud_prefs, 157 managed_cloud_prefs,
156 extension_prefs, 158 extension_prefs,
157 command_line_prefs, 159 command_line_prefs,
158 user_pref_store_, 160 user_pref_store_,
159 recommended_platform_prefs, 161 recommended_platform_prefs,
160 recommended_cloud_prefs, 162 recommended_cloud_prefs,
161 default_store, 163 default_store,
164 pref_sync_associator_,
162 pref_notifier_.get())); 165 pref_notifier_.get()));
163 InitFromStorage(); 166 InitFromStorage();
164 } 167 }
165 168
166 PrefService::PrefService(const PrefService& original, 169 PrefService::PrefService(const PrefService& original,
167 PrefStore* incognito_extension_prefs) 170 PrefStore* incognito_extension_prefs)
168 : user_pref_store_( 171 : user_pref_store_(
169 new OverlayPersistentPrefStore(original.user_pref_store_.get())), 172 new OverlayPersistentPrefStore(original.user_pref_store_.get())),
170 default_store_(original.default_store_.get()), 173 default_store_(original.default_store_.get()),
171 delegate_(NULL) { 174 delegate_(NULL) {
175 // Incognito mode doesn't sync, so no need to create PrefModelAssociator.
172 pref_notifier_.reset(new PrefNotifierImpl(this)); 176 pref_notifier_.reset(new PrefNotifierImpl(this));
173 pref_value_store_.reset(original.pref_value_store_->CloneAndSpecialize( 177 pref_value_store_.reset(original.pref_value_store_->CloneAndSpecialize(
174 NULL, // managed_platform_prefs 178 NULL, // managed_platform_prefs
175 NULL, // managed_cloud_prefs 179 NULL, // managed_cloud_prefs
176 incognito_extension_prefs, 180 incognito_extension_prefs,
177 NULL, // command_line_prefs 181 NULL, // command_line_prefs
178 user_pref_store_.get(), 182 user_pref_store_.get(),
179 NULL, // recommended_platform_prefs 183 NULL, // recommended_platform_prefs
180 NULL, // recommended_cloud_prefs 184 NULL, // recommended_cloud_prefs
181 default_store_.get(), 185 default_store_.get(),
186 NULL, // pref_sync_associator_
182 pref_notifier_.get())); 187 pref_notifier_.get()));
183 InitFromStorage(); 188 InitFromStorage();
184 } 189 }
185 190
186 PrefService::~PrefService() { 191 PrefService::~PrefService() {
187 DCHECK(CalledOnValidThread()); 192 DCHECK(CalledOnValidThread());
188 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); 193 STLDeleteContainerPointers(prefs_.begin(), prefs_.end());
189 prefs_.clear(); 194 prefs_.clear();
190 195
191 // Reset pointers so accesses after destruction reliably crash. 196 // Reset pointers so accesses after destruction reliably crash.
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 DCHECK(CalledOnValidThread()); 257 DCHECK(CalledOnValidThread());
253 user_pref_store_->ScheduleWritePrefs(); 258 user_pref_store_->ScheduleWritePrefs();
254 } 259 }
255 260
256 void PrefService::CommitPendingWrite() { 261 void PrefService::CommitPendingWrite() {
257 DCHECK(CalledOnValidThread()); 262 DCHECK(CalledOnValidThread());
258 user_pref_store_->CommitPendingWrite(); 263 user_pref_store_->CommitPendingWrite();
259 } 264 }
260 265
261 void PrefService::RegisterBooleanPref(const char* path, 266 void PrefService::RegisterBooleanPref(const char* path,
262 bool default_value) { 267 bool default_value,
263 RegisterPreference(path, Value::CreateBooleanValue(default_value)); 268 bool syncable) {
269 RegisterPreference(path, Value::CreateBooleanValue(default_value), syncable);
264 } 270 }
265 271
266 void PrefService::RegisterIntegerPref(const char* path, int default_value) { 272 void PrefService::RegisterIntegerPref(const char* path,
267 RegisterPreference(path, Value::CreateIntegerValue(default_value)); 273 int default_value,
274 bool syncable) {
275 RegisterPreference(path, Value::CreateIntegerValue(default_value), syncable);
268 } 276 }
269 277
270 void PrefService::RegisterDoublePref(const char* path, double default_value) { 278 void PrefService::RegisterDoublePref(const char* path,
271 RegisterPreference(path, Value::CreateDoubleValue(default_value)); 279 double default_value,
280 bool syncable) {
281 RegisterPreference(path, Value::CreateDoubleValue(default_value), syncable);
272 } 282 }
273 283
274 void PrefService::RegisterStringPref(const char* path, 284 void PrefService::RegisterStringPref(const char* path,
275 const std::string& default_value) { 285 const std::string& default_value,
276 RegisterPreference(path, Value::CreateStringValue(default_value)); 286 bool syncable) {
287 RegisterPreference(path, Value::CreateStringValue(default_value), syncable);
277 } 288 }
278 289
279 void PrefService::RegisterFilePathPref(const char* path, 290 void PrefService::RegisterFilePathPref(const char* path,
280 const FilePath& default_value) { 291 const FilePath& default_value,
281 RegisterPreference(path, Value::CreateStringValue(default_value.value())); 292 bool syncable) {
293 RegisterPreference(path,
294 Value::CreateStringValue(default_value.value()),
295 syncable);
282 } 296 }
283 297
284 void PrefService::RegisterListPref(const char* path) { 298 void PrefService::RegisterListPref(const char* path,
285 RegisterPreference(path, new ListValue()); 299 bool syncable) {
300 RegisterPreference(path, new ListValue(), syncable);
286 } 301 }
287 302
288 void PrefService::RegisterListPref(const char* path, ListValue* default_value) { 303 void PrefService::RegisterListPref(const char* path,
289 RegisterPreference(path, default_value); 304 ListValue* default_value,
290 } 305 bool syncable) {
291 306 RegisterPreference(path, default_value, syncable);
292 void PrefService::RegisterDictionaryPref(const char* path) {
293 RegisterPreference(path, new DictionaryValue());
294 } 307 }
295 308
296 void PrefService::RegisterDictionaryPref(const char* path, 309 void PrefService::RegisterDictionaryPref(const char* path,
297 DictionaryValue* default_value) { 310 bool syncable) {
298 RegisterPreference(path, default_value); 311 RegisterPreference(path, new DictionaryValue(), syncable);
312 }
313
314 void PrefService::RegisterDictionaryPref(const char* path,
315 DictionaryValue* default_value,
316 bool syncable) {
317 RegisterPreference(path, default_value, syncable);
299 } 318 }
300 319
301 void PrefService::RegisterLocalizedBooleanPref(const char* path, 320 void PrefService::RegisterLocalizedBooleanPref(const char* path,
302 int locale_default_message_id) { 321 int locale_default_message_id,
322 bool syncable) {
303 RegisterPreference( 323 RegisterPreference(
304 path, 324 path,
305 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); 325 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN,locale_default_message_id),
326 syncable);
306 } 327 }
307 328
308 void PrefService::RegisterLocalizedIntegerPref(const char* path, 329 void PrefService::RegisterLocalizedIntegerPref(const char* path,
309 int locale_default_message_id) { 330 int locale_default_message_id,
331 bool syncable) {
310 RegisterPreference( 332 RegisterPreference(
311 path, 333 path,
312 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); 334 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
335 syncable);
313 } 336 }
314 337
315 void PrefService::RegisterLocalizedDoublePref(const char* path, 338 void PrefService::RegisterLocalizedDoublePref(const char* path,
316 int locale_default_message_id) { 339 int locale_default_message_id,
340 bool syncable) {
317 RegisterPreference( 341 RegisterPreference(
318 path, 342 path,
319 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id)); 343 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
344 syncable);
320 } 345 }
321 346
322 void PrefService::RegisterLocalizedStringPref(const char* path, 347 void PrefService::RegisterLocalizedStringPref(const char* path,
323 int locale_default_message_id) { 348 int locale_default_message_id,
349 bool syncable) {
324 RegisterPreference( 350 RegisterPreference(
325 path, 351 path,
326 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); 352 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
353 syncable);
327 } 354 }
328 355
329 bool PrefService::GetBoolean(const char* path) const { 356 bool PrefService::GetBoolean(const char* path) const {
330 DCHECK(CalledOnValidThread()); 357 DCHECK(CalledOnValidThread());
331 358
332 bool result = false; 359 bool result = false;
333 360
334 const Preference* pref = FindPreference(path); 361 const Preference* pref = FindPreference(path);
335 if (!pref) { 362 if (!pref) {
336 NOTREACHED() << "Trying to read an unregistered pref: " << path; 363 NOTREACHED() << "Trying to read an unregistered pref: " << path;
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 void PrefService::AddPrefObserver(const char* path, 506 void PrefService::AddPrefObserver(const char* path,
480 NotificationObserver* obs) { 507 NotificationObserver* obs) {
481 pref_notifier_->AddPrefObserver(path, obs); 508 pref_notifier_->AddPrefObserver(path, obs);
482 } 509 }
483 510
484 void PrefService::RemovePrefObserver(const char* path, 511 void PrefService::RemovePrefObserver(const char* path,
485 NotificationObserver* obs) { 512 NotificationObserver* obs) {
486 pref_notifier_->RemovePrefObserver(path, obs); 513 pref_notifier_->RemovePrefObserver(path, obs);
487 } 514 }
488 515
489 void PrefService::RegisterPreference(const char* path, Value* default_value) { 516 void PrefService::RegisterPreference(const char* path,
517 Value* default_value,
518 bool syncable) {
490 DCHECK(CalledOnValidThread()); 519 DCHECK(CalledOnValidThread());
491 520
492 // The main code path takes ownership, but most don't. We'll be safe. 521 // The main code path takes ownership, but most don't. We'll be safe.
493 scoped_ptr<Value> scoped_value(default_value); 522 scoped_ptr<Value> scoped_value(default_value);
494 523
495 if (FindPreference(path)) { 524 if (FindPreference(path)) {
496 NOTREACHED() << "Tried to register duplicate pref " << path; 525 NOTREACHED() << "Tried to register duplicate pref " << path;
497 return; 526 return;
498 } 527 }
499 528
500 Value::ValueType orig_type = default_value->GetType(); 529 Value::ValueType orig_type = default_value->GetType();
501 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << 530 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) <<
502 "invalid preference type: " << orig_type; 531 "invalid preference type: " << orig_type;
503 532
504 // Hand off ownership. 533 // Hand off ownership.
505 default_store_->SetDefaultValue(path, scoped_value.release()); 534 default_store_->SetDefaultValue(path, scoped_value.release());
535 if (syncable && pref_sync_associator_.get())
536 pref_sync_associator_->RegisterPref(path);
506 } 537 }
507 538
508 void PrefService::ClearPref(const char* path) { 539 void PrefService::ClearPref(const char* path) {
509 DCHECK(CalledOnValidThread()); 540 DCHECK(CalledOnValidThread());
510 541
511 const Preference* pref = FindPreference(path); 542 const Preference* pref = FindPreference(path);
512 if (!pref) { 543 if (!pref) {
513 NOTREACHED() << "Trying to clear an unregistered pref: " << path; 544 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
514 return; 545 return;
515 } 546 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 } 603 }
573 std::string result("0"); 604 std::string result("0");
574 bool rv = pref->GetValue()->GetAsString(&result); 605 bool rv = pref->GetValue()->GetAsString(&result);
575 DCHECK(rv); 606 DCHECK(rv);
576 607
577 int64 val; 608 int64 val;
578 base::StringToInt64(result, &val); 609 base::StringToInt64(result, &val);
579 return val; 610 return val;
580 } 611 }
581 612
582 void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { 613 void PrefService::RegisterInt64Pref(
614 const char* path,
615 int64 default_value,
616 bool syncable) {
583 RegisterPreference( 617 RegisterPreference(
584 path, Value::CreateStringValue(base::Int64ToString(default_value))); 618 path,
619 Value::CreateStringValue(base::Int64ToString(default_value)),
620 syncable);
585 } 621 }
586 622
587 Value* PrefService::GetMutableUserPref(const char* path, 623 Value* PrefService::GetMutableUserPref(const char* path,
588 Value::ValueType type) { 624 Value::ValueType type) {
589 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); 625 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST);
590 DCHECK(CalledOnValidThread()); 626 DCHECK(CalledOnValidThread());
591 DLOG_IF(WARNING, IsManagedPreference(path)) << 627 DLOG_IF(WARNING, IsManagedPreference(path)) <<
592 "Attempt to change managed preference " << path; 628 "Attempt to change managed preference " << path;
593 629
594 const Preference* pref = FindPreference(path); 630 const Preference* pref = FindPreference(path);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); 735 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
700 } 736 }
701 737
702 bool PrefService::Preference::IsUserModifiable() const { 738 bool PrefService::Preference::IsUserModifiable() const {
703 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); 739 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
704 } 740 }
705 741
706 bool PrefService::Preference::IsExtensionModifiable() const { 742 bool PrefService::Preference::IsExtensionModifiable() const {
707 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); 743 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
708 } 744 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698