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

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: Fix syncing of platform specific prefs 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.
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 pref_sync_associator_->DisassociateModels();
201 pref_sync_associaotr_ = NULL; // RefCounted.
195 } 202 }
196 203
197 void PrefService::OnPrefsRead(PersistentPrefStore::PrefReadError error, 204 void PrefService::OnPrefsRead(PersistentPrefStore::PrefReadError error,
198 bool no_dir) { 205 bool no_dir) {
199 if (no_dir) { 206 if (no_dir) {
200 // Bad news. When profile is created, the process that creates the directory 207 // 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 208 // is explicitly started. So if directory is missing it probably means that
202 // Chromium hasn't sufficient privileges. 209 // Chromium hasn't sufficient privileges.
203 CHECK(delegate_); 210 CHECK(delegate_);
204 delegate_->OnPrefsLoaded(this, false); 211 delegate_->OnPrefsLoaded(this, false);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 DCHECK(CalledOnValidThread()); 259 DCHECK(CalledOnValidThread());
253 user_pref_store_->ScheduleWritePrefs(); 260 user_pref_store_->ScheduleWritePrefs();
254 } 261 }
255 262
256 void PrefService::CommitPendingWrite() { 263 void PrefService::CommitPendingWrite() {
257 DCHECK(CalledOnValidThread()); 264 DCHECK(CalledOnValidThread());
258 user_pref_store_->CommitPendingWrite(); 265 user_pref_store_->CommitPendingWrite();
259 } 266 }
260 267
261 void PrefService::RegisterBooleanPref(const char* path, 268 void PrefService::RegisterBooleanPref(const char* path,
262 bool default_value) { 269 bool default_value,
263 RegisterPreference(path, Value::CreateBooleanValue(default_value)); 270 bool syncable) {
271 RegisterPreference(path, Value::CreateBooleanValue(default_value), syncable);
264 } 272 }
265 273
266 void PrefService::RegisterIntegerPref(const char* path, int default_value) { 274 void PrefService::RegisterIntegerPref(const char* path,
267 RegisterPreference(path, Value::CreateIntegerValue(default_value)); 275 int default_value,
276 bool syncable) {
277 RegisterPreference(path, Value::CreateIntegerValue(default_value), syncable);
268 } 278 }
269 279
270 void PrefService::RegisterDoublePref(const char* path, double default_value) { 280 void PrefService::RegisterDoublePref(const char* path,
271 RegisterPreference(path, Value::CreateDoubleValue(default_value)); 281 double default_value,
282 bool syncable) {
283 RegisterPreference(path, Value::CreateDoubleValue(default_value), syncable);
272 } 284 }
273 285
274 void PrefService::RegisterStringPref(const char* path, 286 void PrefService::RegisterStringPref(const char* path,
275 const std::string& default_value) { 287 const std::string& default_value,
276 RegisterPreference(path, Value::CreateStringValue(default_value)); 288 bool syncable) {
289 RegisterPreference(path, Value::CreateStringValue(default_value), syncable);
277 } 290 }
278 291
279 void PrefService::RegisterFilePathPref(const char* path, 292 void PrefService::RegisterFilePathPref(const char* path,
280 const FilePath& default_value) { 293 const FilePath& default_value,
281 RegisterPreference(path, Value::CreateStringValue(default_value.value())); 294 bool syncable) {
295 RegisterPreference(path,
296 Value::CreateStringValue(default_value.value()),
297 syncable);
282 } 298 }
283 299
284 void PrefService::RegisterListPref(const char* path) { 300 void PrefService::RegisterListPref(const char* path,
285 RegisterPreference(path, new ListValue()); 301 bool syncable) {
302 RegisterPreference(path, new ListValue(), syncable);
286 } 303 }
287 304
288 void PrefService::RegisterListPref(const char* path, ListValue* default_value) { 305 void PrefService::RegisterListPref(const char* path,
289 RegisterPreference(path, default_value); 306 ListValue* default_value,
290 } 307 bool syncable) {
291 308 RegisterPreference(path, default_value, syncable);
292 void PrefService::RegisterDictionaryPref(const char* path) {
293 RegisterPreference(path, new DictionaryValue());
294 } 309 }
295 310
296 void PrefService::RegisterDictionaryPref(const char* path, 311 void PrefService::RegisterDictionaryPref(const char* path,
297 DictionaryValue* default_value) { 312 bool syncable) {
298 RegisterPreference(path, default_value); 313 RegisterPreference(path, new DictionaryValue(), syncable);
314 }
315
316 void PrefService::RegisterDictionaryPref(const char* path,
317 DictionaryValue* default_value,
318 bool syncable) {
319 RegisterPreference(path, default_value, syncable);
299 } 320 }
300 321
301 void PrefService::RegisterLocalizedBooleanPref(const char* path, 322 void PrefService::RegisterLocalizedBooleanPref(const char* path,
302 int locale_default_message_id) { 323 int locale_default_message_id,
324 bool syncable) {
303 RegisterPreference( 325 RegisterPreference(
304 path, 326 path,
305 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); 327 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN,locale_default_message_id),
328 syncable);
306 } 329 }
307 330
308 void PrefService::RegisterLocalizedIntegerPref(const char* path, 331 void PrefService::RegisterLocalizedIntegerPref(const char* path,
309 int locale_default_message_id) { 332 int locale_default_message_id,
333 bool syncable) {
310 RegisterPreference( 334 RegisterPreference(
311 path, 335 path,
312 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); 336 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
337 syncable);
313 } 338 }
314 339
315 void PrefService::RegisterLocalizedDoublePref(const char* path, 340 void PrefService::RegisterLocalizedDoublePref(const char* path,
316 int locale_default_message_id) { 341 int locale_default_message_id,
342 bool syncable) {
317 RegisterPreference( 343 RegisterPreference(
318 path, 344 path,
319 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id)); 345 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
346 syncable);
320 } 347 }
321 348
322 void PrefService::RegisterLocalizedStringPref(const char* path, 349 void PrefService::RegisterLocalizedStringPref(const char* path,
323 int locale_default_message_id) { 350 int locale_default_message_id,
351 bool syncable) {
324 RegisterPreference( 352 RegisterPreference(
325 path, 353 path,
326 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); 354 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
355 syncable);
327 } 356 }
328 357
329 bool PrefService::GetBoolean(const char* path) const { 358 bool PrefService::GetBoolean(const char* path) const {
330 DCHECK(CalledOnValidThread()); 359 DCHECK(CalledOnValidThread());
331 360
332 bool result = false; 361 bool result = false;
333 362
334 const Preference* pref = FindPreference(path); 363 const Preference* pref = FindPreference(path);
335 if (!pref) { 364 if (!pref) {
336 NOTREACHED() << "Trying to read an unregistered pref: " << path; 365 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, 508 void PrefService::AddPrefObserver(const char* path,
480 NotificationObserver* obs) { 509 NotificationObserver* obs) {
481 pref_notifier_->AddPrefObserver(path, obs); 510 pref_notifier_->AddPrefObserver(path, obs);
482 } 511 }
483 512
484 void PrefService::RemovePrefObserver(const char* path, 513 void PrefService::RemovePrefObserver(const char* path,
485 NotificationObserver* obs) { 514 NotificationObserver* obs) {
486 pref_notifier_->RemovePrefObserver(path, obs); 515 pref_notifier_->RemovePrefObserver(path, obs);
487 } 516 }
488 517
489 void PrefService::RegisterPreference(const char* path, Value* default_value) { 518 void PrefService::RegisterPreference(const char* path,
519 Value* default_value,
520 bool syncable) {
490 DCHECK(CalledOnValidThread()); 521 DCHECK(CalledOnValidThread());
491 522
492 // The main code path takes ownership, but most don't. We'll be safe. 523 // The main code path takes ownership, but most don't. We'll be safe.
493 scoped_ptr<Value> scoped_value(default_value); 524 scoped_ptr<Value> scoped_value(default_value);
494 525
495 if (FindPreference(path)) { 526 if (FindPreference(path)) {
496 NOTREACHED() << "Tried to register duplicate pref " << path; 527 NOTREACHED() << "Tried to register duplicate pref " << path;
497 return; 528 return;
498 } 529 }
499 530
500 Value::ValueType orig_type = default_value->GetType(); 531 Value::ValueType orig_type = default_value->GetType();
501 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << 532 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) <<
502 "invalid preference type: " << orig_type; 533 "invalid preference type: " << orig_type;
503 534
504 // Hand off ownership. 535 // Hand off ownership.
505 default_store_->SetDefaultValue(path, scoped_value.release()); 536 default_store_->SetDefaultValue(path, scoped_value.release());
537 if (syncable && pref_sync_associator_.get())
538 pref_sync_associator_->RegisterPref(path);
506 } 539 }
507 540
508 void PrefService::ClearPref(const char* path) { 541 void PrefService::ClearPref(const char* path) {
509 DCHECK(CalledOnValidThread()); 542 DCHECK(CalledOnValidThread());
510 543
511 const Preference* pref = FindPreference(path); 544 const Preference* pref = FindPreference(path);
512 if (!pref) { 545 if (!pref) {
513 NOTREACHED() << "Trying to clear an unregistered pref: " << path; 546 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
514 return; 547 return;
515 } 548 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 } 605 }
573 std::string result("0"); 606 std::string result("0");
574 bool rv = pref->GetValue()->GetAsString(&result); 607 bool rv = pref->GetValue()->GetAsString(&result);
575 DCHECK(rv); 608 DCHECK(rv);
576 609
577 int64 val; 610 int64 val;
578 base::StringToInt64(result, &val); 611 base::StringToInt64(result, &val);
579 return val; 612 return val;
580 } 613 }
581 614
582 void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { 615 void PrefService::RegisterInt64Pref(
616 const char* path,
617 int64 default_value,
618 bool syncable) {
583 RegisterPreference( 619 RegisterPreference(
584 path, Value::CreateStringValue(base::Int64ToString(default_value))); 620 path,
621 Value::CreateStringValue(base::Int64ToString(default_value)),
622 syncable);
585 } 623 }
586 624
587 Value* PrefService::GetMutableUserPref(const char* path, 625 Value* PrefService::GetMutableUserPref(const char* path,
588 Value::ValueType type) { 626 Value::ValueType type) {
589 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); 627 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST);
590 DCHECK(CalledOnValidThread()); 628 DCHECK(CalledOnValidThread());
591 DLOG_IF(WARNING, IsManagedPreference(path)) << 629 DLOG_IF(WARNING, IsManagedPreference(path)) <<
592 "Attempt to change managed preference " << path; 630 "Attempt to change managed preference " << path;
593 631
594 const Preference* pref = FindPreference(path); 632 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()); 737 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
700 } 738 }
701 739
702 bool PrefService::Preference::IsUserModifiable() const { 740 bool PrefService::Preference::IsUserModifiable() const {
703 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); 741 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
704 } 742 }
705 743
706 bool PrefService::Preference::IsExtensionModifiable() const { 744 bool PrefService::Preference::IsExtensionModifiable() const {
707 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); 745 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
708 } 746 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698