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/pref_service.h" | 5 #include "chrome/browser/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" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
24 #include "chrome/browser/configuration_policy_provider_win.h" | 24 #include "chrome/browser/configuration_policy_provider_win.h" |
25 #elif defined(OS_MACOSX) | 25 #elif defined(OS_MACOSX) |
26 #include "chrome/browser/configuration_policy_provider_mac.h" | 26 #include "chrome/browser/configuration_policy_provider_mac.h" |
27 #elif defined(OS_POSIX) | 27 #elif defined(OS_POSIX) |
28 #include "chrome/browser/config_dir_policy_provider.h" | 28 #include "chrome/browser/config_dir_policy_provider.h" |
29 #endif | 29 #endif |
30 #include "chrome/browser/dummy_configuration_policy_provider.h" | 30 #include "chrome/browser/dummy_configuration_policy_provider.h" |
31 | 31 |
32 #include "chrome/browser/configuration_policy_pref_store.h" | 32 #include "chrome/browser/configuration_policy_pref_store.h" |
| 33 #include "chrome/browser/extensions/extension_pref_store.h" |
33 #include "chrome/common/chrome_paths.h" | 34 #include "chrome/common/chrome_paths.h" |
34 #include "chrome/common/json_pref_store.h" | 35 #include "chrome/common/json_pref_store.h" |
35 #include "chrome/common/notification_service.h" | 36 #include "chrome/common/notification_service.h" |
36 #include "grit/chromium_strings.h" | 37 #include "grit/chromium_strings.h" |
37 #include "grit/generated_resources.h" | 38 #include "grit/generated_resources.h" |
38 | 39 |
39 namespace { | 40 namespace { |
40 | 41 |
41 // A helper function for RegisterLocalized*Pref that creates a Value* based on | 42 // A helper function for RegisterLocalized*Pref that creates a Value* based on |
42 // the string value in the locale dll. Because we control the values in a | 43 // the string value in the locale dll. Because we control the values in a |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 // Forwards a notification after a PostMessage so that we can wait for the | 83 // Forwards a notification after a PostMessage so that we can wait for the |
83 // MessageLoop to run. | 84 // MessageLoop to run. |
84 void NotifyReadError(PrefService* pref, int message_id) { | 85 void NotifyReadError(PrefService* pref, int message_id) { |
85 Source<PrefService> source(pref); | 86 Source<PrefService> source(pref); |
86 NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, | 87 NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, |
87 source, Details<int>(&message_id)); | 88 source, Details<int>(&message_id)); |
88 } | 89 } |
89 | 90 |
90 } // namespace | 91 } // namespace |
91 | 92 |
| 93 // static |
92 PrefService* PrefService::CreatePrefService(const FilePath& pref_filename) { | 94 PrefService* PrefService::CreatePrefService(const FilePath& pref_filename) { |
93 PrefStore* managed_prefs = NULL; | 95 PrefStore* managed_prefs = NULL; |
| 96 ExtensionPrefStore* extension_prefs = new ExtensionPrefStore(NULL); |
94 PrefStore* local_prefs = new JsonPrefStore( | 97 PrefStore* local_prefs = new JsonPrefStore( |
95 pref_filename, | 98 pref_filename, |
96 ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)); | 99 ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)); |
97 PrefStore* recommended_prefs = NULL; | 100 PrefStore* recommended_prefs = NULL; |
98 | 101 |
99 ConfigurationPolicyProvider* managed_prefs_provider = NULL; | 102 ConfigurationPolicyProvider* managed_prefs_provider = NULL; |
100 ConfigurationPolicyProvider* recommended_prefs_provider = NULL; | 103 ConfigurationPolicyProvider* recommended_prefs_provider = NULL; |
101 | 104 |
102 #if defined(OS_WIN) | 105 #if defined(OS_WIN) |
103 managed_prefs_provider = new ConfigurationPolicyProviderWin(); | 106 managed_prefs_provider = new ConfigurationPolicyProviderWin(); |
(...skipping 16 matching lines...) Expand all Loading... |
120 | 123 |
121 // The ConfigurationPolicyPrefStore takes ownership of the passed | 124 // The ConfigurationPolicyPrefStore takes ownership of the passed |
122 // |provider|. | 125 // |provider|. |
123 managed_prefs = new ConfigurationPolicyPrefStore( | 126 managed_prefs = new ConfigurationPolicyPrefStore( |
124 CommandLine::ForCurrentProcess(), | 127 CommandLine::ForCurrentProcess(), |
125 managed_prefs_provider); | 128 managed_prefs_provider); |
126 recommended_prefs = new ConfigurationPolicyPrefStore( | 129 recommended_prefs = new ConfigurationPolicyPrefStore( |
127 CommandLine::ForCurrentProcess(), | 130 CommandLine::ForCurrentProcess(), |
128 recommended_prefs_provider); | 131 recommended_prefs_provider); |
129 | 132 |
130 // The PrefValueStore takes to ownership of the parameters. | 133 // The PrefValueStore takes ownership of the PrefStores. |
131 PrefValueStore* value_store = new PrefValueStore( | 134 PrefValueStore* value_store = new PrefValueStore( |
132 managed_prefs, | 135 managed_prefs, |
| 136 extension_prefs, |
133 local_prefs, | 137 local_prefs, |
134 recommended_prefs); | 138 recommended_prefs); |
| 139 |
| 140 PrefService* pref_service = new PrefService(value_store); |
| 141 extension_prefs->SetPrefService(pref_service); |
| 142 |
| 143 return pref_service; |
| 144 } |
| 145 |
| 146 // static |
| 147 PrefService* PrefService::CreateUserPrefService( |
| 148 const FilePath& pref_filename) { |
| 149 PrefValueStore* value_store = new PrefValueStore( |
| 150 NULL, /* no enforced prefs */ |
| 151 NULL, /* no extension prefs */ |
| 152 new JsonPrefStore( |
| 153 pref_filename, |
| 154 ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)), |
| 155 /* user prefs */ |
| 156 NULL /* no advised prefs */); |
135 return new PrefService(value_store); | 157 return new PrefService(value_store); |
136 } | 158 } |
137 | 159 |
138 PrefService::PrefService(PrefValueStore* pref_value_store) | 160 PrefService::PrefService(PrefValueStore* pref_value_store) |
139 : pref_value_store_(pref_value_store) { | 161 : pref_value_store_(pref_value_store) { |
140 InitFromStorage(); | 162 InitFromStorage(); |
141 } | 163 } |
142 | 164 |
143 PrefService::~PrefService() { | 165 PrefService::~PrefService() { |
144 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 } | 391 } |
370 | 392 |
371 const PrefService::Preference* PrefService::FindPreference( | 393 const PrefService::Preference* PrefService::FindPreference( |
372 const wchar_t* pref_name) const { | 394 const wchar_t* pref_name) const { |
373 DCHECK(CalledOnValidThread()); | 395 DCHECK(CalledOnValidThread()); |
374 Preference p(NULL, pref_name, NULL); | 396 Preference p(NULL, pref_name, NULL); |
375 PreferenceSet::const_iterator it = prefs_.find(&p); | 397 PreferenceSet::const_iterator it = prefs_.find(&p); |
376 return it == prefs_.end() ? NULL : *it; | 398 return it == prefs_.end() ? NULL : *it; |
377 } | 399 } |
378 | 400 |
| 401 void PrefService::FireObserversIfChanged(const wchar_t* path, |
| 402 const Value* old_value) { |
| 403 if (PrefIsChanged(path, old_value)) |
| 404 FireObservers(path); |
| 405 } |
| 406 |
| 407 bool PrefService::PrefIsChanged(const wchar_t* path, |
| 408 const Value* old_value) { |
| 409 Value* new_value = NULL; |
| 410 pref_value_store_->GetValue(path, &new_value); |
| 411 // Some unit tests have no values for certain prefs. |
| 412 return (!new_value || !old_value->Equals(new_value)); |
| 413 } |
| 414 |
379 const DictionaryValue* PrefService::GetDictionary(const wchar_t* path) const { | 415 const DictionaryValue* PrefService::GetDictionary(const wchar_t* path) const { |
380 DCHECK(CalledOnValidThread()); | 416 DCHECK(CalledOnValidThread()); |
381 | 417 |
382 const Preference* pref = FindPreference(path); | 418 const Preference* pref = FindPreference(path); |
383 if (!pref) { | 419 if (!pref) { |
384 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 420 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
385 return NULL; | 421 return NULL; |
386 } | 422 } |
387 const Value* value = pref->GetValue(); | 423 const Value* value = pref->GetValue(); |
388 if (value->GetType() == Value::TYPE_NULL) | 424 if (value->GetType() == Value::TYPE_NULL) |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 } | 767 } |
732 | 768 |
733 Value* PrefService::GetPrefCopy(const wchar_t* path) { | 769 Value* PrefService::GetPrefCopy(const wchar_t* path) { |
734 DCHECK(CalledOnValidThread()); | 770 DCHECK(CalledOnValidThread()); |
735 | 771 |
736 const Preference* pref = FindPreference(path); | 772 const Preference* pref = FindPreference(path); |
737 DCHECK(pref); | 773 DCHECK(pref); |
738 return pref->GetValue()->DeepCopy(); | 774 return pref->GetValue()->DeepCopy(); |
739 } | 775 } |
740 | 776 |
741 void PrefService::FireObserversIfChanged(const wchar_t* path, | |
742 const Value* old_value) { | |
743 Value* new_value = NULL; | |
744 pref_value_store_->GetValue(path, &new_value); | |
745 if (!old_value->Equals(new_value)) | |
746 FireObservers(path); | |
747 } | |
748 | |
749 void PrefService::FireObservers(const wchar_t* path) { | 777 void PrefService::FireObservers(const wchar_t* path) { |
750 DCHECK(CalledOnValidThread()); | 778 DCHECK(CalledOnValidThread()); |
751 | 779 |
752 // Convert path to a std::wstring because the Details constructor requires a | 780 // Convert path to a std::wstring because the Details constructor requires a |
753 // class. | 781 // class. |
754 std::wstring path_str(path); | 782 std::wstring path_str(path); |
755 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path_str); | 783 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path_str); |
756 if (observer_iterator == pref_observers_.end()) | 784 if (observer_iterator == pref_observers_.end()) |
757 return; | 785 return; |
758 | 786 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 | 833 |
806 bool PrefService::Preference::IsDefaultValue() const { | 834 bool PrefService::Preference::IsDefaultValue() const { |
807 DCHECK(default_value_.get()); | 835 DCHECK(default_value_.get()); |
808 return default_value_->Equals(GetValue()); | 836 return default_value_->Equals(GetValue()); |
809 } | 837 } |
810 | 838 |
811 bool PrefService::Preference::IsManaged() const { | 839 bool PrefService::Preference::IsManaged() const { |
812 return pref_value_store_->PrefValueIsManaged(name_.c_str()); | 840 return pref_value_store_->PrefValueIsManaged(name_.c_str()); |
813 } | 841 } |
814 | 842 |
OLD | NEW |