OLD | NEW |
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" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 // MessageLoop to run. | 79 // MessageLoop to run. |
80 void NotifyReadError(PrefService* pref, int message_id) { | 80 void NotifyReadError(PrefService* pref, int message_id) { |
81 ShowProfileErrorDialog(message_id); | 81 ShowProfileErrorDialog(message_id); |
82 } | 82 } |
83 | 83 |
84 } // namespace | 84 } // namespace |
85 | 85 |
86 // static | 86 // static |
87 PrefService* PrefService::CreatePrefService(const FilePath& pref_filename, | 87 PrefService* PrefService::CreatePrefService(const FilePath& pref_filename, |
88 PrefStore* extension_prefs, | 88 PrefStore* extension_prefs, |
89 Profile* profile) { | 89 Profile* profile, |
90 return CreatePrefServiceAsync(pref_filename, extension_prefs, profile, NULL); | 90 bool async) { |
91 } | |
92 | |
93 // static | |
94 PrefService* PrefService::CreatePrefServiceAsync( | |
95 const FilePath& pref_filename, | |
96 PrefStore* extension_prefs, | |
97 Profile* profile, | |
98 PrefServiceDelegate* delegate) { | |
99 using policy::ConfigurationPolicyPrefStore; | 91 using policy::ConfigurationPolicyPrefStore; |
100 | 92 |
101 #if defined(OS_LINUX) | 93 #if defined(OS_LINUX) |
102 // We'd like to see what fraction of our users have the preferences | 94 // We'd like to see what fraction of our users have the preferences |
103 // stored on a network file system, as we've had no end of troubles | 95 // stored on a network file system, as we've had no end of troubles |
104 // with NFS/AFS. | 96 // with NFS/AFS. |
105 // TODO(evanm): remove this once we've collected state. | 97 // TODO(evanm): remove this once we've collected state. |
106 file_util::FileSystemType fstype; | 98 file_util::FileSystemType fstype; |
107 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { | 99 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { |
108 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", | 100 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", |
(...skipping 11 matching lines...) Expand all Loading... |
120 JsonPrefStore* user = new JsonPrefStore( | 112 JsonPrefStore* user = new JsonPrefStore( |
121 pref_filename, | 113 pref_filename, |
122 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
123 ConfigurationPolicyPrefStore* recommended_platform = | 115 ConfigurationPolicyPrefStore* recommended_platform = |
124 ConfigurationPolicyPrefStore::CreateRecommendedPlatformPolicyPrefStore(); | 116 ConfigurationPolicyPrefStore::CreateRecommendedPlatformPolicyPrefStore(); |
125 ConfigurationPolicyPrefStore* recommended_cloud = | 117 ConfigurationPolicyPrefStore* recommended_cloud = |
126 ConfigurationPolicyPrefStore::CreateRecommendedCloudPolicyPrefStore( | 118 ConfigurationPolicyPrefStore::CreateRecommendedCloudPolicyPrefStore( |
127 profile); | 119 profile); |
128 DefaultPrefStore* default_pref_store = new DefaultPrefStore(); | 120 DefaultPrefStore* default_pref_store = new DefaultPrefStore(); |
129 | 121 |
130 return new PrefService(managed_platform, managed_cloud, extension_prefs, | 122 PrefService* service = new PrefService( |
131 command_line, user, recommended_platform, | 123 managed_platform, managed_cloud, extension_prefs, |
132 recommended_cloud, default_pref_store, delegate); | 124 command_line, user, recommended_platform, |
| 125 recommended_cloud, default_pref_store); |
| 126 service->InitFromStorage(async); |
| 127 return service; |
133 } | 128 } |
134 | 129 |
135 PrefService* PrefService::CreateIncognitoPrefService( | 130 PrefService* PrefService::CreateIncognitoPrefService( |
136 PrefStore* incognito_extension_prefs) { | 131 PrefStore* incognito_extension_prefs) { |
137 return new PrefService(*this, incognito_extension_prefs); | 132 PrefService* service = new PrefService(*this, incognito_extension_prefs); |
| 133 service->InitFromStorage(false); |
| 134 return service; |
138 } | 135 } |
139 | 136 |
140 PrefService::PrefService(PrefStore* managed_platform_prefs, | 137 PrefService::PrefService(PrefStore* managed_platform_prefs, |
141 PrefStore* managed_cloud_prefs, | 138 PrefStore* managed_cloud_prefs, |
142 PrefStore* extension_prefs, | 139 PrefStore* extension_prefs, |
143 PrefStore* command_line_prefs, | 140 PrefStore* command_line_prefs, |
144 PersistentPrefStore* user_prefs, | 141 PersistentPrefStore* user_prefs, |
145 PrefStore* recommended_platform_prefs, | 142 PrefStore* recommended_platform_prefs, |
146 PrefStore* recommended_cloud_prefs, | 143 PrefStore* recommended_cloud_prefs, |
147 DefaultPrefStore* default_store, | 144 DefaultPrefStore* default_store) |
148 PrefServiceDelegate* delegate) | |
149 : user_pref_store_(user_prefs), | 145 : user_pref_store_(user_prefs), |
150 default_store_(default_store), | 146 default_store_(default_store) { |
151 delegate_(delegate) { | |
152 pref_notifier_.reset(new PrefNotifierImpl(this)); | 147 pref_notifier_.reset(new PrefNotifierImpl(this)); |
153 pref_value_store_.reset( | 148 pref_value_store_.reset( |
154 new PrefValueStore(managed_platform_prefs, | 149 new PrefValueStore(managed_platform_prefs, |
155 managed_cloud_prefs, | 150 managed_cloud_prefs, |
156 extension_prefs, | 151 extension_prefs, |
157 command_line_prefs, | 152 command_line_prefs, |
158 user_pref_store_, | 153 user_pref_store_, |
159 recommended_platform_prefs, | 154 recommended_platform_prefs, |
160 recommended_cloud_prefs, | 155 recommended_cloud_prefs, |
161 default_store, | 156 default_store, |
162 pref_notifier_.get())); | 157 pref_notifier_.get())); |
163 InitFromStorage(); | |
164 } | 158 } |
165 | 159 |
166 PrefService::PrefService(const PrefService& original, | 160 PrefService::PrefService(const PrefService& original, |
167 PrefStore* incognito_extension_prefs) | 161 PrefStore* incognito_extension_prefs) |
168 : user_pref_store_( | 162 : user_pref_store_( |
169 new OverlayPersistentPrefStore(original.user_pref_store_.get())), | 163 new OverlayPersistentPrefStore(original.user_pref_store_.get())), |
170 default_store_(original.default_store_.get()), | 164 default_store_(original.default_store_.get()) { |
171 delegate_(NULL) { | |
172 pref_notifier_.reset(new PrefNotifierImpl(this)); | 165 pref_notifier_.reset(new PrefNotifierImpl(this)); |
173 pref_value_store_.reset(original.pref_value_store_->CloneAndSpecialize( | 166 pref_value_store_.reset(original.pref_value_store_->CloneAndSpecialize( |
174 NULL, // managed_platform_prefs | 167 NULL, // managed_platform_prefs |
175 NULL, // managed_cloud_prefs | 168 NULL, // managed_cloud_prefs |
176 incognito_extension_prefs, | 169 incognito_extension_prefs, |
177 NULL, // command_line_prefs | 170 NULL, // command_line_prefs |
178 user_pref_store_.get(), | 171 user_pref_store_.get(), |
179 NULL, // recommended_platform_prefs | 172 NULL, // recommended_platform_prefs |
180 NULL, // recommended_cloud_prefs | 173 NULL, // recommended_cloud_prefs |
181 default_store_.get(), | 174 default_store_.get(), |
182 pref_notifier_.get())); | 175 pref_notifier_.get())); |
183 InitFromStorage(); | |
184 } | 176 } |
185 | 177 |
186 PrefService::~PrefService() { | 178 PrefService::~PrefService() { |
187 DCHECK(CalledOnValidThread()); | 179 DCHECK(CalledOnValidThread()); |
188 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); | 180 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); |
189 prefs_.clear(); | 181 prefs_.clear(); |
190 | 182 |
191 // Reset pointers so accesses after destruction reliably crash. | 183 // Reset pointers so accesses after destruction reliably crash. |
192 pref_value_store_.reset(); | 184 pref_value_store_.reset(); |
193 user_pref_store_ = NULL; | 185 user_pref_store_ = NULL; |
194 default_store_ = NULL; | 186 default_store_ = NULL; |
195 } | 187 } |
196 | 188 |
197 void PrefService::OnPrefsRead(PersistentPrefStore::PrefReadError error, | 189 void PrefService::OnInitializationCompleted() { |
198 bool no_dir) { | 190 // This is the only message PrefService is waiting for, so remove us from the |
199 if (no_dir) { | 191 // observers list. |
| 192 user_pref_store_->RemoveObserver(this); |
| 193 |
| 194 PersistentPrefStore::PrefReadError error; |
| 195 bool is_fatal; |
| 196 user_pref_store_->GetErrors(&error, &is_fatal); |
| 197 if (is_fatal) { |
200 // Bad news. When profile is created, the process that creates the directory | 198 // 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 | 199 // is explicitly started. So if directory is missing it probably means that |
202 // Chromium hasn't sufficient privileges. | 200 // Chromium hasn't sufficient privileges. |
203 CHECK(delegate_); | 201 pref_notifier_->OnPersistentPrefsRead(false); |
204 delegate_->OnPrefsLoaded(this, false); | |
205 return; | 202 return; |
206 } | 203 } |
207 | 204 |
208 if (error != PersistentPrefStore::PREF_READ_ERROR_NONE) { | 205 if (error != PersistentPrefStore::PREF_READ_ERROR_NONE) { |
209 // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for | 206 // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for |
210 // an example problem that this can cause. | 207 // an example problem that this can cause. |
211 // Do some diagnosis and try to avoid losing data. | 208 // Do some diagnosis and try to avoid losing data. |
212 int message_id = 0; | 209 int message_id = 0; |
213 if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) { | 210 if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) { |
214 message_id = IDS_PREFERENCES_CORRUPT_ERROR; | 211 message_id = IDS_PREFERENCES_CORRUPT_ERROR; |
215 } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) { | 212 } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) { |
216 message_id = IDS_PREFERENCES_UNREADABLE_ERROR; | 213 message_id = IDS_PREFERENCES_UNREADABLE_ERROR; |
217 } | 214 } |
218 | 215 |
219 if (message_id) { | 216 if (message_id) { |
220 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 217 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
221 NewRunnableFunction(&NotifyReadError, this, message_id)); | 218 NewRunnableFunction(&NotifyReadError, this, message_id)); |
222 } | 219 } |
223 UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, 20); | 220 UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, 20); |
224 } | 221 } |
225 | 222 pref_notifier_->OnPersistentPrefsRead(true); |
226 if (delegate_) | |
227 delegate_->OnPrefsLoaded(this, true); | |
228 } | 223 } |
229 | 224 |
230 void PrefService::InitFromStorage() { | 225 void PrefService::InitFromStorage(bool async) { |
231 if (!delegate_) { | 226 user_pref_store_->AddObserver(this); |
232 const PersistentPrefStore::PrefReadError error = | 227 if (!async) { |
233 user_pref_store_->ReadPrefs(); | 228 user_pref_store_->ReadPrefs(); |
234 OnPrefsRead(error, false); | |
235 } else { | 229 } else { |
236 // todo(altimofeev): move this method to PersistentPrefStore interface. | 230 user_pref_store_.get()->ReadPrefsAsync(); |
237 (static_cast<JsonPrefStore*>(user_pref_store_.get()))->ReadPrefs(this); | |
238 } | 231 } |
239 } | 232 } |
240 | 233 |
241 bool PrefService::ReloadPersistentPrefs() { | 234 bool PrefService::ReloadPersistentPrefs() { |
242 return user_pref_store_->ReadPrefs() == | 235 return user_pref_store_->ReadPrefs() == |
243 PersistentPrefStore::PREF_READ_ERROR_NONE; | 236 PersistentPrefStore::PREF_READ_ERROR_NONE; |
244 } | 237 } |
245 | 238 |
246 bool PrefService::SavePersistentPrefs() { | 239 bool PrefService::SavePersistentPrefs() { |
247 DCHECK(CalledOnValidThread()); | 240 DCHECK(CalledOnValidThread()); |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); | 692 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); |
700 } | 693 } |
701 | 694 |
702 bool PrefService::Preference::IsUserModifiable() const { | 695 bool PrefService::Preference::IsUserModifiable() const { |
703 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); | 696 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); |
704 } | 697 } |
705 | 698 |
706 bool PrefService::Preference::IsExtensionModifiable() const { | 699 bool PrefService::Preference::IsExtensionModifiable() const { |
707 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); | 700 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); |
708 } | 701 } |
OLD | NEW |