OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
17 #include "base/stl_util-inl.h" | 17 #include "base/stl_util-inl.h" |
18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/sys_string_conversions.h" | 20 #include "base/sys_string_conversions.h" |
21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
23 #include "chrome/browser/browser_thread.h" | 23 #include "chrome/browser/browser_thread.h" |
24 #include "chrome/browser/extensions/extension_pref_store.h" | |
24 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 25 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
25 #include "chrome/browser/prefs/command_line_pref_store.h" | 26 #include "chrome/browser/prefs/command_line_pref_store.h" |
26 #include "chrome/browser/prefs/default_pref_store.h" | 27 #include "chrome/browser/prefs/default_pref_store.h" |
28 #include "chrome/browser/prefs/overlay_persistent_pref_store.h" | |
27 #include "chrome/browser/prefs/pref_notifier_impl.h" | 29 #include "chrome/browser/prefs/pref_notifier_impl.h" |
28 #include "chrome/browser/prefs/pref_value_store.h" | 30 #include "chrome/browser/prefs/pref_value_store.h" |
29 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
30 #include "chrome/common/json_pref_store.h" | 32 #include "chrome/common/json_pref_store.h" |
31 #include "chrome/common/notification_service.h" | 33 #include "chrome/common/notification_service.h" |
32 #include "grit/chromium_strings.h" | 34 #include "grit/chromium_strings.h" |
33 #include "grit/generated_resources.h" | 35 #include "grit/generated_resources.h" |
34 | 36 |
35 namespace { | 37 namespace { |
36 | 38 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 // MessageLoop to run. | 80 // MessageLoop to run. |
79 void NotifyReadError(PrefService* pref, int message_id) { | 81 void NotifyReadError(PrefService* pref, int message_id) { |
80 Source<PrefService> source(pref); | 82 Source<PrefService> source(pref); |
81 NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, | 83 NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, |
82 source, Details<int>(&message_id)); | 84 source, Details<int>(&message_id)); |
83 } | 85 } |
84 | 86 |
85 } // namespace | 87 } // namespace |
86 | 88 |
87 // static | 89 // static |
88 PrefService* PrefService::CreatePrefService(const FilePath& pref_filename, | 90 PrefService* PrefService::CreatePrefService( |
89 PrefStore* extension_prefs, | 91 const FilePath& pref_filename, |
90 Profile* profile) { | 92 scoped_refptr<PrefStore> extension_prefs, |
93 Profile* profile) { | |
91 using policy::ConfigurationPolicyPrefStore; | 94 using policy::ConfigurationPolicyPrefStore; |
92 | 95 |
93 #if defined(OS_LINUX) | 96 #if defined(OS_LINUX) |
94 // We'd like to see what fraction of our users have the preferences | 97 // We'd like to see what fraction of our users have the preferences |
95 // stored on a network file system, as we've had no end of troubles | 98 // stored on a network file system, as we've had no end of troubles |
96 // with NFS/AFS. | 99 // with NFS/AFS. |
97 // TODO(evanm): remove this once we've collected state. | 100 // TODO(evanm): remove this once we've collected state. |
98 file_util::FileSystemType fstype; | 101 file_util::FileSystemType fstype; |
99 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { | 102 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { |
100 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", | 103 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", |
101 static_cast<int>(fstype), | 104 static_cast<int>(fstype), |
102 file_util::FILE_SYSTEM_TYPE_COUNT); | 105 file_util::FILE_SYSTEM_TYPE_COUNT); |
103 } | 106 } |
104 #endif | 107 #endif |
105 | 108 |
106 ConfigurationPolicyPrefStore* managed = | 109 scoped_refptr<ConfigurationPolicyPrefStore> managed = |
Mattias Nissler (ping if slow)
2010/12/20 14:50:02
scoped_refptr construction not needed here either.
battre
2010/12/21 18:51:59
Done.
| |
107 ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore(); | 110 ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore(); |
108 ConfigurationPolicyPrefStore* device_management = | 111 scoped_refptr<ConfigurationPolicyPrefStore> device_management = |
109 ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( | 112 ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( |
110 profile); | 113 profile); |
111 CommandLinePrefStore* command_line = | 114 scoped_refptr<CommandLinePrefStore> command_line = |
112 new CommandLinePrefStore(CommandLine::ForCurrentProcess()); | 115 new CommandLinePrefStore(CommandLine::ForCurrentProcess()); |
113 JsonPrefStore* user = new JsonPrefStore( | 116 scoped_refptr<JsonPrefStore> user = new JsonPrefStore( |
114 pref_filename, | 117 pref_filename, |
115 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 118 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
116 ConfigurationPolicyPrefStore* recommended = | 119 scoped_refptr<ConfigurationPolicyPrefStore> recommended = |
117 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); | 120 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); |
118 | 121 |
119 return new PrefService(managed, device_management, extension_prefs, | 122 return new PrefService(managed, device_management, extension_prefs, |
120 command_line, user, recommended, profile); | 123 command_line, user, recommended, profile); |
121 } | 124 } |
122 | 125 |
123 PrefService::PrefService(PrefStore* managed_platform_prefs, | 126 PrefService* PrefService::CreateIncognitoPrefService( |
124 PrefStore* device_management_prefs, | 127 scoped_refptr<PrefStore> incognito_extension_prefs) { |
125 PrefStore* extension_prefs, | 128 return new PrefService(pref_value_store_, |
126 PrefStore* command_line_prefs, | 129 user_pref_store_, |
127 PersistentPrefStore* user_prefs, | 130 incognito_extension_prefs, |
128 PrefStore* recommended_prefs, | 131 default_store_); |
132 } | |
133 | |
134 PrefService::PrefService(PrefValueStore* old_pref_value_store, | |
135 scoped_refptr<PersistentPrefStore> old_user_pref_store, | |
136 scoped_refptr<PrefStore> incognito_extension_prefs, | |
137 scoped_refptr<DefaultPrefStore> default_store) | |
138 : user_pref_store_(new OverlayPersistentPrefStore(old_user_pref_store)), | |
139 default_store_(default_store) { | |
140 pref_notifier_.reset(new PrefNotifierImpl(this)); | |
141 pref_value_store_ = | |
142 old_pref_value_store->Derive(incognito_extension_prefs, | |
143 user_pref_store_, | |
144 pref_notifier_.get()); | |
145 // We do not call InitFromStorage() here intentionally, as we do not read | |
146 // the user prefs from disk. | |
147 } | |
148 | |
149 PrefService::PrefService(scoped_refptr<PrefStore> managed_platform_prefs, | |
150 scoped_refptr<PrefStore> device_management_prefs, | |
151 scoped_refptr<PrefStore> extension_prefs, | |
152 scoped_refptr<PrefStore> command_line_prefs, | |
153 scoped_refptr<PersistentPrefStore> user_prefs, | |
154 scoped_refptr<PrefStore> recommended_prefs, | |
129 Profile* profile) | 155 Profile* profile) |
130 : user_pref_store_(user_prefs) { | 156 : user_pref_store_(user_prefs) { |
131 pref_notifier_.reset(new PrefNotifierImpl(this)); | 157 pref_notifier_.reset(new PrefNotifierImpl(this)); |
132 default_store_ = new DefaultPrefStore(); | 158 default_store_ = new DefaultPrefStore(); |
133 pref_value_store_ = | 159 pref_value_store_ = |
134 new PrefValueStore(managed_platform_prefs, | 160 new PrefValueStore(managed_platform_prefs, |
135 device_management_prefs, | 161 device_management_prefs, |
136 extension_prefs, | 162 extension_prefs, |
137 command_line_prefs, | 163 command_line_prefs, |
138 user_pref_store_, | 164 user_pref_store_, |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 bool rv = pref->GetValue()->GetAsString(&result); | 347 bool rv = pref->GetValue()->GetAsString(&result); |
322 DCHECK(rv); | 348 DCHECK(rv); |
323 #if defined(OS_POSIX) | 349 #if defined(OS_POSIX) |
324 // We store filepaths as UTF8, so convert it back to the system type. | 350 // We store filepaths as UTF8, so convert it back to the system type. |
325 result = base::SysWideToNativeMB(UTF8ToWide(result)); | 351 result = base::SysWideToNativeMB(UTF8ToWide(result)); |
326 #endif | 352 #endif |
327 return FilePath(result); | 353 return FilePath(result); |
328 } | 354 } |
329 | 355 |
330 bool PrefService::HasPrefPath(const char* path) const { | 356 bool PrefService::HasPrefPath(const char* path) const { |
331 return pref_value_store_->HasPrefPath(path); | 357 const Preference* pref = FindPreference(path); |
358 return pref && !pref->IsDefaultValue(); | |
332 } | 359 } |
333 | 360 |
334 const PrefService::Preference* PrefService::FindPreference( | 361 const PrefService::Preference* PrefService::FindPreference( |
335 const char* pref_name) const { | 362 const char* pref_name) const { |
336 DCHECK(CalledOnValidThread()); | 363 DCHECK(CalledOnValidThread()); |
337 Preference p(this, pref_name); | 364 Preference p(this, pref_name, Value::TYPE_NULL); |
338 PreferenceSet::const_iterator it = prefs_.find(&p); | 365 PreferenceSet::const_iterator it = prefs_.find(&p); |
339 return it == prefs_.end() ? NULL : *it; | 366 if (it != prefs_.end()) |
367 return *it; | |
368 const Value::ValueType type = default_store_->GetType(pref_name); | |
369 if (type == Value::TYPE_NULL) | |
370 return NULL; | |
371 Preference* new_pref = new Preference(this, pref_name, type); | |
372 prefs_.insert(new_pref); | |
373 return new_pref; | |
340 } | 374 } |
341 | 375 |
342 bool PrefService::ReadOnly() const { | 376 bool PrefService::ReadOnly() const { |
343 return user_pref_store_->ReadOnly(); | 377 return user_pref_store_->ReadOnly(); |
344 } | 378 } |
345 | 379 |
346 PrefNotifier* PrefService::pref_notifier() const { | 380 PrefNotifier* PrefService::pref_notifier() const { |
347 return pref_notifier_.get(); | 381 return pref_notifier_.get(); |
348 } | 382 } |
349 | 383 |
350 bool PrefService::IsManagedPreference(const char* pref_name) const { | 384 bool PrefService::IsManagedPreference(const char* pref_name) const { |
351 const Preference* pref = FindPreference(pref_name); | 385 const Preference* pref = FindPreference(pref_name); |
352 if (pref && pref->IsManaged()) { | 386 return pref && pref->IsManaged(); |
353 return true; | |
354 } | |
355 return false; | |
356 } | 387 } |
357 | 388 |
358 const DictionaryValue* PrefService::GetDictionary(const char* path) const { | 389 const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
359 DCHECK(CalledOnValidThread()); | 390 DCHECK(CalledOnValidThread()); |
360 | 391 |
361 const Preference* pref = FindPreference(path); | 392 const Preference* pref = FindPreference(path); |
362 if (!pref) { | 393 if (!pref) { |
363 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 394 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
364 return NULL; | 395 return NULL; |
365 } | 396 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 | 432 |
402 if (FindPreference(path)) { | 433 if (FindPreference(path)) { |
403 NOTREACHED() << "Tried to register duplicate pref " << path; | 434 NOTREACHED() << "Tried to register duplicate pref " << path; |
404 return; | 435 return; |
405 } | 436 } |
406 | 437 |
407 Value::ValueType orig_type = default_value->GetType(); | 438 Value::ValueType orig_type = default_value->GetType(); |
408 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 439 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
409 "invalid preference type: " << orig_type; | 440 "invalid preference type: " << orig_type; |
410 | 441 |
411 // We set the default value of dictionaries and lists to be null so it's | 442 // Hand off ownership. |
412 // easier for callers to check for empty dict/list prefs. The PrefValueStore | 443 default_store_->SetDefaultValue(path, scoped_value.release()); |
413 // accepts ownership of the value (null or default_value). | |
414 if (Value::TYPE_LIST == orig_type || Value::TYPE_DICTIONARY == orig_type) { | |
415 default_store_->SetDefaultValue(path, Value::CreateNullValue()); | |
416 } else { | |
417 // Hand off ownership. | |
418 default_store_->SetDefaultValue(path, scoped_value.release()); | |
419 } | |
420 | 444 |
421 pref_value_store_->RegisterPreferenceType(path, orig_type); | 445 prefs_.insert(new Preference(this, path, orig_type)); |
422 prefs_.insert(new Preference(this, path)); | |
423 } | 446 } |
424 | 447 |
425 void PrefService::ClearPref(const char* path) { | 448 void PrefService::ClearPref(const char* path) { |
426 DCHECK(CalledOnValidThread()); | 449 DCHECK(CalledOnValidThread()); |
427 | 450 |
428 const Preference* pref = FindPreference(path); | 451 const Preference* pref = FindPreference(path); |
429 if (!pref) { | 452 if (!pref) { |
430 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 453 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
431 return; | 454 return; |
432 } | 455 } |
433 user_pref_store_->RemoveValue(path); | 456 user_pref_store_->RemoveValue(path); |
434 } | 457 } |
435 | 458 |
436 void PrefService::Set(const char* path, const Value& value) { | 459 void PrefService::Set(const char* path, const Value& value) { |
437 DCHECK(CalledOnValidThread()); | 460 DCHECK(CalledOnValidThread()); |
438 | 461 |
439 const Preference* pref = FindPreference(path); | 462 const Preference* pref = FindPreference(path); |
440 if (!pref) { | 463 if (!pref) { |
441 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 464 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
442 return; | 465 return; |
443 } | 466 } |
444 | 467 |
445 // Allow dictionary and list types to be set to null, which removes their | 468 if (pref->GetType() != value.GetType()) { |
446 // user values. | |
447 if (value.GetType() == Value::TYPE_NULL && | |
448 (pref->GetType() == Value::TYPE_DICTIONARY || | |
449 pref->GetType() == Value::TYPE_LIST)) { | |
450 user_pref_store_->RemoveValue(path); | |
451 } else if (pref->GetType() != value.GetType()) { | |
452 NOTREACHED() << "Trying to set pref " << path | 469 NOTREACHED() << "Trying to set pref " << path |
453 << " of type " << pref->GetType() | 470 << " of type " << pref->GetType() |
454 << " to value of type " << value.GetType(); | 471 << " to value of type " << value.GetType(); |
455 } else { | 472 } else { |
456 user_pref_store_->SetValue(path, value.DeepCopy()); | 473 user_pref_store_->SetValue(path, value.DeepCopy()); |
457 } | 474 } |
458 } | 475 } |
459 | 476 |
460 void PrefService::SetBoolean(const char* path, bool value) { | 477 void PrefService::SetBoolean(const char* path, bool value) { |
461 SetUserPrefValue(path, Value::CreateBooleanValue(value)); | 478 SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
583 return; | 600 return; |
584 } | 601 } |
585 | 602 |
586 user_pref_store_->SetValue(path, new_value); | 603 user_pref_store_->SetValue(path, new_value); |
587 } | 604 } |
588 | 605 |
589 /////////////////////////////////////////////////////////////////////////////// | 606 /////////////////////////////////////////////////////////////////////////////// |
590 // PrefService::Preference | 607 // PrefService::Preference |
591 | 608 |
592 PrefService::Preference::Preference(const PrefService* service, | 609 PrefService::Preference::Preference(const PrefService* service, |
593 const char* name) | 610 const char* name, |
611 Value::ValueType type) | |
594 : name_(name), | 612 : name_(name), |
613 type_(type), | |
595 pref_service_(service) { | 614 pref_service_(service) { |
596 DCHECK(name); | 615 DCHECK(name); |
597 DCHECK(service); | 616 DCHECK(service); |
598 } | 617 } |
599 | 618 |
600 Value::ValueType PrefService::Preference::GetType() const { | 619 Value::ValueType PrefService::Preference::GetType() const { |
601 return pref_service_->pref_value_store_->GetRegisteredType(name_); | 620 return type_; |
602 } | 621 } |
603 | 622 |
604 const Value* PrefService::Preference::GetValue() const { | 623 const Value* PrefService::Preference::GetValue() const { |
605 DCHECK(pref_service_->FindPreference(name_.c_str())) << | 624 DCHECK(pref_service_->FindPreference(name_.c_str())) << |
606 "Must register pref before getting its value"; | 625 "Must register pref before getting its value"; |
607 | 626 |
608 Value* found_value = NULL; | 627 Value* found_value = NULL; |
609 if (pref_service_->pref_value_store_->GetValue(name_, &found_value)) | 628 if (pref_service_->pref_value_store_->GetValue(name_, type_, &found_value)) { |
629 DCHECK(found_value->IsType(type_)); | |
610 return found_value; | 630 return found_value; |
631 } | |
611 | 632 |
612 // Every registered preference has at least a default value. | 633 // Every registered preference has at least a default value. |
613 NOTREACHED() << "no valid value found for registered pref " << name_; | 634 NOTREACHED() << "no valid value found for registered pref " << name_; |
614 return NULL; | 635 return NULL; |
615 } | 636 } |
616 | 637 |
617 bool PrefService::Preference::IsManaged() const { | 638 bool PrefService::Preference::IsManaged() const { |
618 PrefValueStore* pref_value_store = pref_service_->pref_value_store_; | 639 PrefValueStore* pref_value_store = pref_service_->pref_value_store_; |
619 return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || | 640 return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || |
620 pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); | 641 pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); |
(...skipping 21 matching lines...) Expand all Loading... | |
642 | 663 |
643 bool PrefService::Preference::IsDefaultValue() const { | 664 bool PrefService::Preference::IsDefaultValue() const { |
644 return pref_service_->pref_value_store_-> | 665 return pref_service_->pref_value_store_-> |
645 PrefValueFromDefaultStore(name_.c_str()); | 666 PrefValueFromDefaultStore(name_.c_str()); |
646 } | 667 } |
647 | 668 |
648 bool PrefService::Preference::IsUserModifiable() const { | 669 bool PrefService::Preference::IsUserModifiable() const { |
649 return pref_service_->pref_value_store_-> | 670 return pref_service_->pref_value_store_-> |
650 PrefValueUserModifiable(name_.c_str()); | 671 PrefValueUserModifiable(name_.c_str()); |
651 } | 672 } |
OLD | NEW |