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

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: Rebase + comments Created 9 years, 7 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_.reset(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_.get(),
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.
192 pref_value_store_.reset(); 197 pref_value_store_.reset();
193 user_pref_store_ = NULL; 198 user_pref_store_ = NULL;
194 default_store_ = NULL; 199 default_store_ = NULL;
200 if (pref_sync_associator_.get())
201 pref_sync_associator_->DisassociateModels();
202 pref_sync_associator_.reset();
195 } 203 }
196 204
197 void PrefService::OnPrefsRead(PersistentPrefStore::PrefReadError error, 205 void PrefService::OnPrefsRead(PersistentPrefStore::PrefReadError error,
198 bool no_dir) { 206 bool no_dir) {
199 if (no_dir) { 207 if (no_dir) {
200 // Bad news. When profile is created, the process that creates the directory 208 // Bad news. When profile is created, the process that creates the directory
201 // is explicitly started. So if directory is missing it probably means that 209 // is explicitly started. So if directory is missing it probably means that
202 // Chromium hasn't sufficient privileges. 210 // Chromium hasn't sufficient privileges.
203 CHECK(delegate_); 211 CHECK(delegate_);
204 delegate_->OnPrefsLoaded(this, false); 212 delegate_->OnPrefsLoaded(this, false);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 DCHECK(CalledOnValidThread()); 260 DCHECK(CalledOnValidThread());
253 user_pref_store_->ScheduleWritePrefs(); 261 user_pref_store_->ScheduleWritePrefs();
254 } 262 }
255 263
256 void PrefService::CommitPendingWrite() { 264 void PrefService::CommitPendingWrite() {
257 DCHECK(CalledOnValidThread()); 265 DCHECK(CalledOnValidThread());
258 user_pref_store_->CommitPendingWrite(); 266 user_pref_store_->CommitPendingWrite();
259 } 267 }
260 268
261 void PrefService::RegisterBooleanPref(const char* path, 269 void PrefService::RegisterBooleanPref(const char* path,
262 bool default_value) { 270 bool default_value,
263 RegisterPreference(path, Value::CreateBooleanValue(default_value)); 271 bool syncable) {
272 RegisterPreference(path, Value::CreateBooleanValue(default_value), syncable);
264 } 273 }
265 274
266 void PrefService::RegisterIntegerPref(const char* path, int default_value) { 275 void PrefService::RegisterIntegerPref(const char* path,
267 RegisterPreference(path, Value::CreateIntegerValue(default_value)); 276 int default_value,
277 bool syncable) {
278 RegisterPreference(path, Value::CreateIntegerValue(default_value), syncable);
268 } 279 }
269 280
270 void PrefService::RegisterDoublePref(const char* path, double default_value) { 281 void PrefService::RegisterDoublePref(const char* path,
271 RegisterPreference(path, Value::CreateDoubleValue(default_value)); 282 double default_value,
283 bool syncable) {
284 RegisterPreference(path, Value::CreateDoubleValue(default_value), syncable);
272 } 285 }
273 286
274 void PrefService::RegisterStringPref(const char* path, 287 void PrefService::RegisterStringPref(const char* path,
275 const std::string& default_value) { 288 const std::string& default_value,
276 RegisterPreference(path, Value::CreateStringValue(default_value)); 289 bool syncable) {
290 RegisterPreference(path, Value::CreateStringValue(default_value), syncable);
277 } 291 }
278 292
279 void PrefService::RegisterFilePathPref(const char* path, 293 void PrefService::RegisterFilePathPref(const char* path,
280 const FilePath& default_value) { 294 const FilePath& default_value,
281 RegisterPreference(path, Value::CreateStringValue(default_value.value())); 295 bool syncable) {
296 RegisterPreference(path,
297 Value::CreateStringValue(default_value.value()),
298 syncable);
282 } 299 }
283 300
284 void PrefService::RegisterListPref(const char* path) { 301 void PrefService::RegisterListPref(const char* path,
285 RegisterPreference(path, new ListValue()); 302 bool syncable) {
303 RegisterPreference(path, new ListValue(), syncable);
286 } 304 }
287 305
288 void PrefService::RegisterListPref(const char* path, ListValue* default_value) { 306 void PrefService::RegisterListPref(const char* path,
289 RegisterPreference(path, default_value); 307 ListValue* default_value,
290 } 308 bool syncable) {
291 309 RegisterPreference(path, default_value, syncable);
292 void PrefService::RegisterDictionaryPref(const char* path) {
293 RegisterPreference(path, new DictionaryValue());
294 } 310 }
295 311
296 void PrefService::RegisterDictionaryPref(const char* path, 312 void PrefService::RegisterDictionaryPref(const char* path,
297 DictionaryValue* default_value) { 313 bool syncable) {
298 RegisterPreference(path, default_value); 314 RegisterPreference(path, new DictionaryValue(), syncable);
315 }
316
317 void PrefService::RegisterDictionaryPref(const char* path,
318 DictionaryValue* default_value,
319 bool syncable) {
320 RegisterPreference(path, default_value, syncable);
299 } 321 }
300 322
301 void PrefService::RegisterLocalizedBooleanPref(const char* path, 323 void PrefService::RegisterLocalizedBooleanPref(const char* path,
302 int locale_default_message_id) { 324 int locale_default_message_id,
325 bool syncable) {
303 RegisterPreference( 326 RegisterPreference(
304 path, 327 path,
305 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); 328 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN,locale_default_message_id),
329 syncable);
306 } 330 }
307 331
308 void PrefService::RegisterLocalizedIntegerPref(const char* path, 332 void PrefService::RegisterLocalizedIntegerPref(const char* path,
309 int locale_default_message_id) { 333 int locale_default_message_id,
334 bool syncable) {
310 RegisterPreference( 335 RegisterPreference(
311 path, 336 path,
312 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); 337 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
338 syncable);
313 } 339 }
314 340
315 void PrefService::RegisterLocalizedDoublePref(const char* path, 341 void PrefService::RegisterLocalizedDoublePref(const char* path,
316 int locale_default_message_id) { 342 int locale_default_message_id,
343 bool syncable) {
317 RegisterPreference( 344 RegisterPreference(
318 path, 345 path,
319 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id)); 346 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
347 syncable);
320 } 348 }
321 349
322 void PrefService::RegisterLocalizedStringPref(const char* path, 350 void PrefService::RegisterLocalizedStringPref(const char* path,
323 int locale_default_message_id) { 351 int locale_default_message_id,
352 bool syncable) {
324 RegisterPreference( 353 RegisterPreference(
325 path, 354 path,
326 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); 355 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
356 syncable);
327 } 357 }
328 358
329 bool PrefService::GetBoolean(const char* path) const { 359 bool PrefService::GetBoolean(const char* path) const {
330 DCHECK(CalledOnValidThread()); 360 DCHECK(CalledOnValidThread());
331 361
332 bool result = false; 362 bool result = false;
333 363
334 const Preference* pref = FindPreference(path); 364 const Preference* pref = FindPreference(path);
335 if (!pref) { 365 if (!pref) {
336 NOTREACHED() << "Trying to read an unregistered pref: " << path; 366 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, 509 void PrefService::AddPrefObserver(const char* path,
480 NotificationObserver* obs) { 510 NotificationObserver* obs) {
481 pref_notifier_->AddPrefObserver(path, obs); 511 pref_notifier_->AddPrefObserver(path, obs);
482 } 512 }
483 513
484 void PrefService::RemovePrefObserver(const char* path, 514 void PrefService::RemovePrefObserver(const char* path,
485 NotificationObserver* obs) { 515 NotificationObserver* obs) {
486 pref_notifier_->RemovePrefObserver(path, obs); 516 pref_notifier_->RemovePrefObserver(path, obs);
487 } 517 }
488 518
489 void PrefService::RegisterPreference(const char* path, Value* default_value) { 519 void PrefService::RegisterPreference(const char* path,
520 Value* default_value,
521 bool syncable) {
490 DCHECK(CalledOnValidThread()); 522 DCHECK(CalledOnValidThread());
491 523
492 // The main code path takes ownership, but most don't. We'll be safe. 524 // The main code path takes ownership, but most don't. We'll be safe.
493 scoped_ptr<Value> scoped_value(default_value); 525 scoped_ptr<Value> scoped_value(default_value);
494 526
495 if (FindPreference(path)) { 527 if (FindPreference(path)) {
496 NOTREACHED() << "Tried to register duplicate pref " << path; 528 NOTREACHED() << "Tried to register duplicate pref " << path;
497 return; 529 return;
498 } 530 }
499 531
500 Value::ValueType orig_type = default_value->GetType(); 532 Value::ValueType orig_type = default_value->GetType();
501 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << 533 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) <<
502 "invalid preference type: " << orig_type; 534 "invalid preference type: " << orig_type;
503 535
504 // Hand off ownership. 536 // Hand off ownership.
505 default_store_->SetDefaultValue(path, scoped_value.release()); 537 default_store_->SetDefaultValue(path, scoped_value.release());
538 if (syncable && pref_sync_associator_.get())
539 pref_sync_associator_->RegisterPref(path);
506 } 540 }
507 541
508 void PrefService::ClearPref(const char* path) { 542 void PrefService::ClearPref(const char* path) {
509 DCHECK(CalledOnValidThread()); 543 DCHECK(CalledOnValidThread());
510 544
511 const Preference* pref = FindPreference(path); 545 const Preference* pref = FindPreference(path);
512 if (!pref) { 546 if (!pref) {
513 NOTREACHED() << "Trying to clear an unregistered pref: " << path; 547 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
514 return; 548 return;
515 } 549 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 } 588 }
555 std::string result("0"); 589 std::string result("0");
556 bool rv = pref->GetValue()->GetAsString(&result); 590 bool rv = pref->GetValue()->GetAsString(&result);
557 DCHECK(rv); 591 DCHECK(rv);
558 592
559 int64 val; 593 int64 val;
560 base::StringToInt64(result, &val); 594 base::StringToInt64(result, &val);
561 return val; 595 return val;
562 } 596 }
563 597
564 void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { 598 void PrefService::RegisterInt64Pref(
599 const char* path,
600 int64 default_value,
601 bool syncable) {
565 RegisterPreference( 602 RegisterPreference(
566 path, Value::CreateStringValue(base::Int64ToString(default_value))); 603 path,
604 Value::CreateStringValue(base::Int64ToString(default_value)),
605 syncable);
567 } 606 }
568 607
569 Value* PrefService::GetMutableUserPref(const char* path, 608 Value* PrefService::GetMutableUserPref(const char* path,
570 Value::ValueType type) { 609 Value::ValueType type) {
571 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); 610 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST);
572 DCHECK(CalledOnValidThread()); 611 DCHECK(CalledOnValidThread());
573 DLOG_IF(WARNING, IsManagedPreference(path)) << 612 DLOG_IF(WARNING, IsManagedPreference(path)) <<
574 "Attempt to change managed preference " << path; 613 "Attempt to change managed preference " << path;
575 614
576 const Preference* pref = FindPreference(path); 615 const Preference* pref = FindPreference(path);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 if (pref->GetType() != new_value->GetType()) { 658 if (pref->GetType() != new_value->GetType()) {
620 NOTREACHED() << "Trying to set pref " << path 659 NOTREACHED() << "Trying to set pref " << path
621 << " of type " << pref->GetType() 660 << " of type " << pref->GetType()
622 << " to value of type " << new_value->GetType(); 661 << " to value of type " << new_value->GetType();
623 return; 662 return;
624 } 663 }
625 664
626 user_pref_store_->SetValue(path, owned_value.release()); 665 user_pref_store_->SetValue(path, owned_value.release());
627 } 666 }
628 667
668 SyncableService* PrefService::GetSyncableService() {
669 return pref_sync_associator_.get();
670 }
671
629 /////////////////////////////////////////////////////////////////////////////// 672 ///////////////////////////////////////////////////////////////////////////////
630 // PrefService::Preference 673 // PrefService::Preference
631 674
632 PrefService::Preference::Preference(const PrefService* service, 675 PrefService::Preference::Preference(const PrefService* service,
633 const char* name, 676 const char* name,
634 Value::ValueType type) 677 Value::ValueType type)
635 : name_(name), 678 : name_(name),
636 type_(type), 679 type_(type),
637 pref_service_(service) { 680 pref_service_(service) {
638 DCHECK(name); 681 DCHECK(name);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); 725 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
683 } 726 }
684 727
685 bool PrefService::Preference::IsUserModifiable() const { 728 bool PrefService::Preference::IsUserModifiable() const {
686 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); 729 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
687 } 730 }
688 731
689 bool PrefService::Preference::IsExtensionModifiable() const { 732 bool PrefService::Preference::IsExtensionModifiable() const {
690 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); 733 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
691 } 734 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698