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/policy/configuration_policy_pref_store.h" | 24 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
25 #include "chrome/browser/prefs/command_line_pref_store.h" | 25 #include "chrome/browser/prefs/command_line_pref_store.h" |
26 #include "chrome/browser/prefs/in_memory_pref_store.h" | 26 #include "chrome/browser/prefs/default_pref_store.h" |
27 #include "chrome/browser/prefs/pref_notifier_impl.h" | 27 #include "chrome/browser/prefs/pref_notifier_impl.h" |
28 #include "chrome/browser/prefs/pref_value_store.h" | 28 #include "chrome/browser/prefs/pref_value_store.h" |
29 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
30 #include "chrome/common/json_pref_store.h" | 30 #include "chrome/common/json_pref_store.h" |
31 #include "chrome/common/notification_service.h" | 31 #include "chrome/common/notification_service.h" |
32 #include "grit/chromium_strings.h" | 32 #include "grit/chromium_strings.h" |
33 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 void NotifyReadError(PrefService* pref, int message_id) { | 79 void NotifyReadError(PrefService* pref, int message_id) { |
80 Source<PrefService> source(pref); | 80 Source<PrefService> source(pref); |
81 NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, | 81 NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, |
82 source, Details<int>(&message_id)); | 82 source, Details<int>(&message_id)); |
83 } | 83 } |
84 | 84 |
85 } // namespace | 85 } // namespace |
86 | 86 |
87 // static | 87 // static |
88 PrefService* PrefService::CreatePrefService(const FilePath& pref_filename, | 88 PrefService* PrefService::CreatePrefService(const FilePath& pref_filename, |
| 89 PrefStore* extension_prefs, |
89 Profile* profile) { | 90 Profile* profile) { |
90 using policy::ConfigurationPolicyPrefStore; | 91 using policy::ConfigurationPolicyPrefStore; |
91 | 92 |
92 #if defined(OS_LINUX) | 93 #if defined(OS_LINUX) |
93 // 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 |
94 // 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 |
95 // with NFS/AFS. | 96 // with NFS/AFS. |
96 // TODO(evanm): remove this once we've collected state. | 97 // TODO(evanm): remove this once we've collected state. |
97 file_util::FileSystemType fstype; | 98 file_util::FileSystemType fstype; |
98 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { | 99 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { |
99 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", | 100 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", |
100 static_cast<int>(fstype), | 101 static_cast<int>(fstype), |
101 file_util::FILE_SYSTEM_TYPE_COUNT); | 102 file_util::FILE_SYSTEM_TYPE_COUNT); |
102 } | 103 } |
103 #endif | 104 #endif |
104 | 105 |
105 ConfigurationPolicyPrefStore* managed = | 106 ConfigurationPolicyPrefStore* managed = |
106 ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore(); | 107 ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore(); |
107 ConfigurationPolicyPrefStore* device_management = | 108 ConfigurationPolicyPrefStore* device_management = |
108 ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( | 109 ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( |
109 profile); | 110 profile); |
110 InMemoryPrefStore* extension = new InMemoryPrefStore(); | |
111 CommandLinePrefStore* command_line = | 111 CommandLinePrefStore* command_line = |
112 new CommandLinePrefStore(CommandLine::ForCurrentProcess()); | 112 new CommandLinePrefStore(CommandLine::ForCurrentProcess()); |
113 JsonPrefStore* user = new JsonPrefStore( | 113 JsonPrefStore* user = new JsonPrefStore( |
114 pref_filename, | 114 pref_filename, |
115 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 115 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
116 ConfigurationPolicyPrefStore* recommended = | 116 ConfigurationPolicyPrefStore* recommended = |
117 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); | 117 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); |
118 | 118 |
119 return new PrefService(managed, device_management, extension, command_line, | 119 return new PrefService(managed, device_management, extension_prefs, |
120 user, recommended, profile); | 120 command_line, user, recommended, profile); |
121 } | |
122 | |
123 // static | |
124 PrefService* PrefService::CreateUserPrefService(const FilePath& pref_filename) { | |
125 JsonPrefStore* user = new JsonPrefStore( | |
126 pref_filename, | |
127 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | |
128 InMemoryPrefStore* extension = new InMemoryPrefStore(); | |
129 | |
130 return new PrefService(NULL, NULL, extension, NULL, user, NULL, NULL); | |
131 } | 121 } |
132 | 122 |
133 PrefService::PrefService(PrefStore* managed_platform_prefs, | 123 PrefService::PrefService(PrefStore* managed_platform_prefs, |
134 PrefStore* device_management_prefs, | 124 PrefStore* device_management_prefs, |
135 PrefStore* extension_prefs, | 125 PrefStore* extension_prefs, |
136 PrefStore* command_line_prefs, | 126 PrefStore* command_line_prefs, |
137 PrefStore* user_prefs, | 127 PersistentPrefStore* user_prefs, |
138 PrefStore* recommended_prefs, | 128 PrefStore* recommended_prefs, |
139 Profile* profile) { | 129 Profile* profile) |
| 130 : user_pref_store_(user_prefs) { |
140 pref_notifier_.reset(new PrefNotifierImpl(this)); | 131 pref_notifier_.reset(new PrefNotifierImpl(this)); |
141 extension_store_ = extension_prefs; | 132 default_store_ = new DefaultPrefStore(); |
142 default_store_ = new InMemoryPrefStore(); | |
143 pref_value_store_ = | 133 pref_value_store_ = |
144 new PrefValueStore(managed_platform_prefs, | 134 new PrefValueStore(managed_platform_prefs, |
145 device_management_prefs, | 135 device_management_prefs, |
146 extension_store_, | 136 extension_prefs, |
147 command_line_prefs, | 137 command_line_prefs, |
148 user_prefs, | 138 user_pref_store_, |
149 recommended_prefs, | 139 recommended_prefs, |
150 default_store_, | 140 default_store_, |
151 pref_notifier_.get(), | 141 pref_notifier_.get(), |
152 profile); | 142 profile); |
153 InitFromStorage(); | 143 InitFromStorage(); |
154 } | 144 } |
155 | 145 |
156 PrefService::~PrefService() { | 146 PrefService::~PrefService() { |
157 DCHECK(CalledOnValidThread()); | 147 DCHECK(CalledOnValidThread()); |
158 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); | 148 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); |
159 prefs_.clear(); | 149 prefs_.clear(); |
160 } | 150 } |
161 | 151 |
162 void PrefService::InitFromStorage() { | 152 void PrefService::InitFromStorage() { |
163 PrefStore::PrefReadError error = LoadPersistentPrefs(); | 153 const PersistentPrefStore::PrefReadError error = |
164 if (error == PrefStore::PREF_READ_ERROR_NONE) | 154 user_pref_store_->ReadPrefs(); |
| 155 if (error == PersistentPrefStore::PREF_READ_ERROR_NONE) |
165 return; | 156 return; |
166 | 157 |
167 // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for | 158 // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for |
168 // an example problem that this can cause. | 159 // an example problem that this can cause. |
169 // Do some diagnosis and try to avoid losing data. | 160 // Do some diagnosis and try to avoid losing data. |
170 int message_id = 0; | 161 int message_id = 0; |
171 if (error <= PrefStore::PREF_READ_ERROR_JSON_TYPE) { | 162 if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) { |
172 message_id = IDS_PREFERENCES_CORRUPT_ERROR; | 163 message_id = IDS_PREFERENCES_CORRUPT_ERROR; |
173 } else if (error != PrefStore::PREF_READ_ERROR_NO_FILE) { | 164 } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) { |
174 message_id = IDS_PREFERENCES_UNREADABLE_ERROR; | 165 message_id = IDS_PREFERENCES_UNREADABLE_ERROR; |
175 } | 166 } |
176 | 167 |
177 if (message_id) { | 168 if (message_id) { |
178 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 169 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
179 NewRunnableFunction(&NotifyReadError, this, message_id)); | 170 NewRunnableFunction(&NotifyReadError, this, message_id)); |
180 } | 171 } |
181 UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, 20); | 172 UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, 20); |
182 } | 173 } |
183 | 174 |
184 bool PrefService::ReloadPersistentPrefs() { | 175 bool PrefService::ReloadPersistentPrefs() { |
185 return (LoadPersistentPrefs() == PrefStore::PREF_READ_ERROR_NONE); | 176 return user_pref_store_->ReadPrefs() == |
186 } | 177 PersistentPrefStore::PREF_READ_ERROR_NONE; |
187 | |
188 PrefStore::PrefReadError PrefService::LoadPersistentPrefs() { | |
189 DCHECK(CalledOnValidThread()); | |
190 | |
191 PrefStore::PrefReadError pref_error = pref_value_store_->ReadPrefs(); | |
192 | |
193 for (PreferenceSet::iterator it = prefs_.begin(); | |
194 it != prefs_.end(); ++it) { | |
195 (*it)->pref_service_ = this; | |
196 } | |
197 | |
198 return pref_error; | |
199 } | 178 } |
200 | 179 |
201 bool PrefService::SavePersistentPrefs() { | 180 bool PrefService::SavePersistentPrefs() { |
202 DCHECK(CalledOnValidThread()); | 181 DCHECK(CalledOnValidThread()); |
203 | 182 |
204 return pref_value_store_->WritePrefs(); | 183 return user_pref_store_->WritePrefs(); |
205 } | 184 } |
206 | 185 |
207 void PrefService::ScheduleSavePersistentPrefs() { | 186 void PrefService::ScheduleSavePersistentPrefs() { |
208 DCHECK(CalledOnValidThread()); | 187 DCHECK(CalledOnValidThread()); |
209 | 188 |
210 pref_value_store_->ScheduleWritePrefs(); | 189 user_pref_store_->ScheduleWritePrefs(); |
211 } | 190 } |
212 | 191 |
213 void PrefService::RegisterBooleanPref(const char* path, | 192 void PrefService::RegisterBooleanPref(const char* path, |
214 bool default_value) { | 193 bool default_value) { |
215 RegisterPreference(path, Value::CreateBooleanValue(default_value)); | 194 RegisterPreference(path, Value::CreateBooleanValue(default_value)); |
216 } | 195 } |
217 | 196 |
218 void PrefService::RegisterIntegerPref(const char* path, int default_value) { | 197 void PrefService::RegisterIntegerPref(const char* path, int default_value) { |
219 RegisterPreference(path, Value::CreateIntegerValue(default_value)); | 198 RegisterPreference(path, Value::CreateIntegerValue(default_value)); |
220 } | 199 } |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 | 333 |
355 const PrefService::Preference* PrefService::FindPreference( | 334 const PrefService::Preference* PrefService::FindPreference( |
356 const char* pref_name) const { | 335 const char* pref_name) const { |
357 DCHECK(CalledOnValidThread()); | 336 DCHECK(CalledOnValidThread()); |
358 Preference p(this, pref_name); | 337 Preference p(this, pref_name); |
359 PreferenceSet::const_iterator it = prefs_.find(&p); | 338 PreferenceSet::const_iterator it = prefs_.find(&p); |
360 return it == prefs_.end() ? NULL : *it; | 339 return it == prefs_.end() ? NULL : *it; |
361 } | 340 } |
362 | 341 |
363 bool PrefService::ReadOnly() const { | 342 bool PrefService::ReadOnly() const { |
364 return pref_value_store_->ReadOnly(); | 343 return user_pref_store_->ReadOnly(); |
365 } | 344 } |
366 | 345 |
367 PrefNotifier* PrefService::pref_notifier() const { | 346 PrefNotifier* PrefService::pref_notifier() const { |
368 return pref_notifier_.get(); | 347 return pref_notifier_.get(); |
369 } | 348 } |
370 | 349 |
371 PrefStore* PrefService::GetExtensionPrefStore() { | |
372 return extension_store_; | |
373 } | |
374 | |
375 bool PrefService::IsManagedPreference(const char* pref_name) const { | 350 bool PrefService::IsManagedPreference(const char* pref_name) const { |
376 const Preference* pref = FindPreference(pref_name); | 351 const Preference* pref = FindPreference(pref_name); |
377 if (pref && pref->IsManaged()) { | 352 if (pref && pref->IsManaged()) { |
378 return true; | 353 return true; |
379 } | 354 } |
380 return false; | 355 return false; |
381 } | 356 } |
382 | 357 |
383 const DictionaryValue* PrefService::GetDictionary(const char* path) const { | 358 const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
384 DCHECK(CalledOnValidThread()); | 359 DCHECK(CalledOnValidThread()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 } | 405 } |
431 | 406 |
432 Value::ValueType orig_type = default_value->GetType(); | 407 Value::ValueType orig_type = default_value->GetType(); |
433 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 408 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
434 "invalid preference type: " << orig_type; | 409 "invalid preference type: " << orig_type; |
435 | 410 |
436 // We set the default value of dictionaries and lists to be null so it's | 411 // We set the default value of dictionaries and lists to be null so it's |
437 // easier for callers to check for empty dict/list prefs. The PrefValueStore | 412 // easier for callers to check for empty dict/list prefs. The PrefValueStore |
438 // accepts ownership of the value (null or default_value). | 413 // accepts ownership of the value (null or default_value). |
439 if (Value::TYPE_LIST == orig_type || Value::TYPE_DICTIONARY == orig_type) { | 414 if (Value::TYPE_LIST == orig_type || Value::TYPE_DICTIONARY == orig_type) { |
440 default_store_->prefs()->Set(path, Value::CreateNullValue()); | 415 default_store_->SetDefaultValue(path, Value::CreateNullValue()); |
441 } else { | 416 } else { |
442 // Hand off ownership. | 417 // Hand off ownership. |
443 DCHECK(!PrefStore::IsUseDefaultSentinelValue(default_value)); | 418 default_store_->SetDefaultValue(path, scoped_value.release()); |
444 default_store_->prefs()->Set(path, scoped_value.release()); | |
445 } | 419 } |
446 | 420 |
447 pref_value_store_->RegisterPreferenceType(path, orig_type); | 421 pref_value_store_->RegisterPreferenceType(path, orig_type); |
448 prefs_.insert(new Preference(this, path)); | 422 prefs_.insert(new Preference(this, path)); |
449 } | 423 } |
450 | 424 |
451 void PrefService::ClearPref(const char* path) { | 425 void PrefService::ClearPref(const char* path) { |
452 DCHECK(CalledOnValidThread()); | 426 DCHECK(CalledOnValidThread()); |
453 | 427 |
454 const Preference* pref = FindPreference(path); | 428 const Preference* pref = FindPreference(path); |
455 if (!pref) { | 429 if (!pref) { |
456 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 430 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
457 return; | 431 return; |
458 } | 432 } |
459 pref_value_store_->RemoveUserPrefValue(path); | 433 user_pref_store_->RemoveValue(path); |
460 } | 434 } |
461 | 435 |
462 void PrefService::Set(const char* path, const Value& value) { | 436 void PrefService::Set(const char* path, const Value& value) { |
463 DCHECK(CalledOnValidThread()); | 437 DCHECK(CalledOnValidThread()); |
464 | 438 |
465 const Preference* pref = FindPreference(path); | 439 const Preference* pref = FindPreference(path); |
466 if (!pref) { | 440 if (!pref) { |
467 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 441 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
468 return; | 442 return; |
469 } | 443 } |
470 | 444 |
471 // Allow dictionary and list types to be set to null, which removes their | 445 // Allow dictionary and list types to be set to null, which removes their |
472 // user values. | 446 // user values. |
473 if (value.GetType() == Value::TYPE_NULL && | 447 if (value.GetType() == Value::TYPE_NULL && |
474 (pref->GetType() == Value::TYPE_DICTIONARY || | 448 (pref->GetType() == Value::TYPE_DICTIONARY || |
475 pref->GetType() == Value::TYPE_LIST)) { | 449 pref->GetType() == Value::TYPE_LIST)) { |
476 pref_value_store_->RemoveUserPrefValue(path); | 450 user_pref_store_->RemoveValue(path); |
477 } else if (pref->GetType() != value.GetType()) { | 451 } else if (pref->GetType() != value.GetType()) { |
478 NOTREACHED() << "Trying to set pref " << path | 452 NOTREACHED() << "Trying to set pref " << path |
479 << " of type " << pref->GetType() | 453 << " of type " << pref->GetType() |
480 << " to value of type " << value.GetType(); | 454 << " to value of type " << value.GetType(); |
481 } else { | 455 } else { |
482 pref_value_store_->SetUserPrefValue(path, value.DeepCopy()); | 456 user_pref_store_->SetValue(path, value.DeepCopy()); |
483 } | 457 } |
484 } | 458 } |
485 | 459 |
486 void PrefService::SetBoolean(const char* path, bool value) { | 460 void PrefService::SetBoolean(const char* path, bool value) { |
487 SetUserPrefValue(path, Value::CreateBooleanValue(value)); | 461 SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
488 } | 462 } |
489 | 463 |
490 void PrefService::SetInteger(const char* path, int value) { | 464 void PrefService::SetInteger(const char* path, int value) { |
491 SetUserPrefValue(path, Value::CreateIntegerValue(value)); | 465 SetUserPrefValue(path, Value::CreateIntegerValue(value)); |
492 } | 466 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 } | 522 } |
549 if (pref->GetType() != Value::TYPE_DICTIONARY) { | 523 if (pref->GetType() != Value::TYPE_DICTIONARY) { |
550 NOTREACHED() << "Wrong type for GetMutableDictionary: " << path; | 524 NOTREACHED() << "Wrong type for GetMutableDictionary: " << path; |
551 return NULL; | 525 return NULL; |
552 } | 526 } |
553 | 527 |
554 DictionaryValue* dict = NULL; | 528 DictionaryValue* dict = NULL; |
555 Value* tmp_value = NULL; | 529 Value* tmp_value = NULL; |
556 // Look for an existing preference in the user store. If it doesn't | 530 // Look for an existing preference in the user store. If it doesn't |
557 // exist or isn't the correct type, create a new user preference. | 531 // exist or isn't the correct type, create a new user preference. |
558 if (!pref_value_store_->GetUserValue(path, &tmp_value) || | 532 if (user_pref_store_->GetValue(path, &tmp_value) |
| 533 != PersistentPrefStore::READ_OK || |
559 !tmp_value->IsType(Value::TYPE_DICTIONARY)) { | 534 !tmp_value->IsType(Value::TYPE_DICTIONARY)) { |
560 dict = new DictionaryValue; | 535 dict = new DictionaryValue; |
561 pref_value_store_->SetUserPrefValueSilently(path, dict); | 536 user_pref_store_->SetValueSilently(path, dict); |
562 } else { | 537 } else { |
563 dict = static_cast<DictionaryValue*>(tmp_value); | 538 dict = static_cast<DictionaryValue*>(tmp_value); |
564 } | 539 } |
565 return dict; | 540 return dict; |
566 } | 541 } |
567 | 542 |
568 ListValue* PrefService::GetMutableList(const char* path) { | 543 ListValue* PrefService::GetMutableList(const char* path) { |
569 DCHECK(CalledOnValidThread()); | 544 DCHECK(CalledOnValidThread()); |
570 | 545 |
571 const Preference* pref = FindPreference(path); | 546 const Preference* pref = FindPreference(path); |
572 if (!pref) { | 547 if (!pref) { |
573 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 548 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
574 return NULL; | 549 return NULL; |
575 } | 550 } |
576 if (pref->GetType() != Value::TYPE_LIST) { | 551 if (pref->GetType() != Value::TYPE_LIST) { |
577 NOTREACHED() << "Wrong type for GetMutableList: " << path; | 552 NOTREACHED() << "Wrong type for GetMutableList: " << path; |
578 return NULL; | 553 return NULL; |
579 } | 554 } |
580 | 555 |
581 ListValue* list = NULL; | 556 ListValue* list = NULL; |
582 Value* tmp_value = NULL; | 557 Value* tmp_value = NULL; |
583 // Look for an existing preference in the user store. If it doesn't | 558 // Look for an existing preference in the user store. If it doesn't |
584 // exist or isn't the correct type, create a new user preference. | 559 // exist or isn't the correct type, create a new user preference. |
585 if (!pref_value_store_->GetUserValue(path, &tmp_value) || | 560 if (user_pref_store_->GetValue(path, &tmp_value) |
| 561 != PersistentPrefStore::READ_OK || |
586 !tmp_value->IsType(Value::TYPE_LIST)) { | 562 !tmp_value->IsType(Value::TYPE_LIST)) { |
587 list = new ListValue; | 563 list = new ListValue; |
588 pref_value_store_->SetUserPrefValueSilently(path, list); | 564 user_pref_store_->SetValueSilently(path, list); |
589 } else { | 565 } else { |
590 list = static_cast<ListValue*>(tmp_value); | 566 list = static_cast<ListValue*>(tmp_value); |
591 } | 567 } |
592 return list; | 568 return list; |
593 } | 569 } |
594 | 570 |
595 Value* PrefService::GetPrefCopy(const char* path) { | |
596 DCHECK(CalledOnValidThread()); | |
597 | |
598 const Preference* pref = FindPreference(path); | |
599 DCHECK(pref); | |
600 return pref->GetValue()->DeepCopy(); | |
601 } | |
602 | |
603 void PrefService::SetUserPrefValue(const char* path, Value* new_value) { | 571 void PrefService::SetUserPrefValue(const char* path, Value* new_value) { |
604 DCHECK(CalledOnValidThread()); | 572 DCHECK(CalledOnValidThread()); |
605 | 573 |
606 const Preference* pref = FindPreference(path); | 574 const Preference* pref = FindPreference(path); |
607 if (!pref) { | 575 if (!pref) { |
608 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 576 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
609 return; | 577 return; |
610 } | 578 } |
611 if (pref->IsManaged()) { | |
612 NOTREACHED() << "Preference is managed: " << path; | |
613 return; | |
614 } | |
615 if (pref->GetType() != new_value->GetType()) { | 579 if (pref->GetType() != new_value->GetType()) { |
616 NOTREACHED() << "Trying to set pref " << path | 580 NOTREACHED() << "Trying to set pref " << path |
617 << " of type " << pref->GetType() | 581 << " of type " << pref->GetType() |
618 << " to value of type " << new_value->GetType(); | 582 << " to value of type " << new_value->GetType(); |
619 return; | 583 return; |
620 } | 584 } |
621 | 585 |
622 pref_value_store_->SetUserPrefValue(path, new_value); | 586 user_pref_store_->SetValue(path, new_value); |
623 } | 587 } |
624 | 588 |
625 /////////////////////////////////////////////////////////////////////////////// | 589 /////////////////////////////////////////////////////////////////////////////// |
626 // PrefService::Preference | 590 // PrefService::Preference |
627 | 591 |
628 PrefService::Preference::Preference(const PrefService* service, | 592 PrefService::Preference::Preference(const PrefService* service, |
629 const char* name) | 593 const char* name) |
630 : name_(name), | 594 : name_(name), |
631 pref_service_(service) { | 595 pref_service_(service) { |
632 DCHECK(name); | 596 DCHECK(name); |
(...skipping 11 matching lines...) Expand all Loading... |
644 Value* found_value = NULL; | 608 Value* found_value = NULL; |
645 if (pref_service_->pref_value_store_->GetValue(name_, &found_value)) | 609 if (pref_service_->pref_value_store_->GetValue(name_, &found_value)) |
646 return found_value; | 610 return found_value; |
647 | 611 |
648 // Every registered preference has at least a default value. | 612 // Every registered preference has at least a default value. |
649 NOTREACHED() << "no valid value found for registered pref " << name_; | 613 NOTREACHED() << "no valid value found for registered pref " << name_; |
650 return NULL; | 614 return NULL; |
651 } | 615 } |
652 | 616 |
653 bool PrefService::Preference::IsManaged() const { | 617 bool PrefService::Preference::IsManaged() const { |
654 PrefValueStore* pref_value_store = | 618 PrefValueStore* pref_value_store = pref_service_->pref_value_store_; |
655 pref_service_->pref_value_store_; | |
656 return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || | 619 return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || |
657 pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); | 620 pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); |
658 } | 621 } |
659 | 622 |
660 bool PrefService::Preference::HasExtensionSetting() const { | 623 bool PrefService::Preference::HasExtensionSetting() const { |
661 return pref_service_->pref_value_store_-> | 624 return pref_service_->pref_value_store_-> |
662 PrefValueInExtensionStore(name_.c_str()); | 625 PrefValueInExtensionStore(name_.c_str()); |
663 } | 626 } |
664 | 627 |
665 bool PrefService::Preference::HasUserSetting() const { | 628 bool PrefService::Preference::HasUserSetting() const { |
(...skipping 13 matching lines...) Expand all Loading... |
679 | 642 |
680 bool PrefService::Preference::IsDefaultValue() const { | 643 bool PrefService::Preference::IsDefaultValue() const { |
681 return pref_service_->pref_value_store_-> | 644 return pref_service_->pref_value_store_-> |
682 PrefValueFromDefaultStore(name_.c_str()); | 645 PrefValueFromDefaultStore(name_.c_str()); |
683 } | 646 } |
684 | 647 |
685 bool PrefService::Preference::IsUserModifiable() const { | 648 bool PrefService::Preference::IsUserModifiable() const { |
686 return pref_service_->pref_value_store_-> | 649 return pref_service_->pref_value_store_-> |
687 PrefValueUserModifiable(name_.c_str()); | 650 PrefValueUserModifiable(name_.c_str()); |
688 } | 651 } |
OLD | NEW |