OLD | NEW |
---|---|
1 // Copyright (c) 2010 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 "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/common/json_pref_store.h" | 31 #include "chrome/common/json_pref_store.h" |
30 #include "chrome/common/notification_service.h" | 32 #include "chrome/common/notification_service.h" |
31 #include "grit/chromium_strings.h" | 33 #include "grit/chromium_strings.h" |
32 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
33 | 35 |
34 namespace { | 36 namespace { |
35 | 37 |
36 // A helper function for RegisterLocalized*Pref that creates a Value* based on | 38 // A helper function for RegisterLocalized*Pref that creates a Value* based on |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 ConfigurationPolicyPrefStore* device_management = | 109 ConfigurationPolicyPrefStore* device_management = |
108 ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( | 110 ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( |
109 profile); | 111 profile); |
110 CommandLinePrefStore* command_line = | 112 CommandLinePrefStore* command_line = |
111 new CommandLinePrefStore(CommandLine::ForCurrentProcess()); | 113 new CommandLinePrefStore(CommandLine::ForCurrentProcess()); |
112 JsonPrefStore* user = new JsonPrefStore( | 114 JsonPrefStore* user = new JsonPrefStore( |
113 pref_filename, | 115 pref_filename, |
114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 116 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
115 ConfigurationPolicyPrefStore* recommended = | 117 ConfigurationPolicyPrefStore* recommended = |
116 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); | 118 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); |
119 DefaultPrefStore* default_pref_store = new DefaultPrefStore(); | |
117 | 120 |
118 return new PrefService(managed, device_management, extension_prefs, | 121 return new PrefService(managed, device_management, extension_prefs, |
119 command_line, user, recommended); | 122 command_line, user, recommended, default_pref_store); |
123 } | |
124 | |
125 PrefService* PrefService::CreateIncognitoPrefService( | |
126 PrefStore* incognito_extension_prefs) { | |
127 return new PrefService( | |
128 managed_platform_prefs_, | |
129 device_management_prefs_, | |
130 incognito_extension_prefs, | |
131 command_line_prefs_, | |
132 new OverlayPersistentPrefStore(user_pref_store_.get()), | |
133 recommended_prefs_, | |
134 default_store_); | |
danno
2011/01/14 13:05:09
In previous versions, there was an effort to keep
battre
2011/01/20 17:59:29
Done.
| |
120 } | 135 } |
121 | 136 |
122 PrefService::PrefService(PrefStore* managed_platform_prefs, | 137 PrefService::PrefService(PrefStore* managed_platform_prefs, |
123 PrefStore* device_management_prefs, | 138 PrefStore* device_management_prefs, |
124 PrefStore* extension_prefs, | 139 PrefStore* extension_prefs, |
125 PrefStore* command_line_prefs, | 140 PrefStore* command_line_prefs, |
126 PersistentPrefStore* user_prefs, | 141 PersistentPrefStore* user_prefs, |
127 PrefStore* recommended_prefs) | 142 PrefStore* recommended_prefs, |
128 : user_pref_store_(user_prefs) { | 143 DefaultPrefStore* default_store) |
144 : managed_platform_prefs_(managed_platform_prefs), | |
145 device_management_prefs_(device_management_prefs), | |
146 extension_prefs_(extension_prefs), | |
147 command_line_prefs_(command_line_prefs), | |
148 recommended_prefs_(recommended_prefs), | |
149 user_pref_store_(user_prefs), | |
150 default_store_(default_store) { | |
129 pref_notifier_.reset(new PrefNotifierImpl(this)); | 151 pref_notifier_.reset(new PrefNotifierImpl(this)); |
130 default_store_ = new DefaultPrefStore(); | |
131 pref_value_store_ = | 152 pref_value_store_ = |
132 new PrefValueStore(managed_platform_prefs, | 153 new PrefValueStore(managed_platform_prefs_, |
133 device_management_prefs, | 154 device_management_prefs_, |
134 extension_prefs, | 155 extension_prefs_, |
135 command_line_prefs, | 156 command_line_prefs_, |
136 user_pref_store_, | 157 user_pref_store_, |
137 recommended_prefs, | 158 recommended_prefs_, |
138 default_store_, | 159 default_store_, |
139 pref_notifier_.get()); | 160 pref_notifier_.get()); |
140 InitFromStorage(); | 161 InitFromStorage(); |
141 } | 162 } |
142 | 163 |
143 PrefService::~PrefService() { | 164 PrefService::~PrefService() { |
144 DCHECK(CalledOnValidThread()); | 165 DCHECK(CalledOnValidThread()); |
145 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); | 166 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); |
146 prefs_.clear(); | 167 prefs_.clear(); |
147 } | 168 } |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 bool rv = pref->GetValue()->GetAsString(&result); | 339 bool rv = pref->GetValue()->GetAsString(&result); |
319 DCHECK(rv); | 340 DCHECK(rv); |
320 #if defined(OS_POSIX) | 341 #if defined(OS_POSIX) |
321 // We store filepaths as UTF8, so convert it back to the system type. | 342 // We store filepaths as UTF8, so convert it back to the system type. |
322 result = base::SysWideToNativeMB(UTF8ToWide(result)); | 343 result = base::SysWideToNativeMB(UTF8ToWide(result)); |
323 #endif | 344 #endif |
324 return FilePath(result); | 345 return FilePath(result); |
325 } | 346 } |
326 | 347 |
327 bool PrefService::HasPrefPath(const char* path) const { | 348 bool PrefService::HasPrefPath(const char* path) const { |
328 return pref_value_store_->HasPrefPath(path); | 349 const Preference* pref = FindPreference(path); |
350 return pref && !pref->IsDefaultValue(); | |
329 } | 351 } |
330 | 352 |
331 const PrefService::Preference* PrefService::FindPreference( | 353 const PrefService::Preference* PrefService::FindPreference( |
332 const char* pref_name) const { | 354 const char* pref_name) const { |
333 DCHECK(CalledOnValidThread()); | 355 DCHECK(CalledOnValidThread()); |
334 Preference p(this, pref_name); | 356 Preference p(this, pref_name, Value::TYPE_NULL); |
335 PreferenceSet::const_iterator it = prefs_.find(&p); | 357 PreferenceSet::const_iterator it = prefs_.find(&p); |
336 return it == prefs_.end() ? NULL : *it; | 358 if (it != prefs_.end()) |
359 return *it; | |
360 const Value::ValueType type = default_store_->GetType(pref_name); | |
361 if (type == Value::TYPE_NULL) | |
362 return NULL; | |
363 Preference* new_pref = new Preference(this, pref_name, type); | |
364 prefs_.insert(new_pref); | |
365 return new_pref; | |
337 } | 366 } |
338 | 367 |
339 bool PrefService::ReadOnly() const { | 368 bool PrefService::ReadOnly() const { |
340 return user_pref_store_->ReadOnly(); | 369 return user_pref_store_->ReadOnly(); |
341 } | 370 } |
342 | 371 |
343 PrefNotifier* PrefService::pref_notifier() const { | 372 PrefNotifier* PrefService::pref_notifier() const { |
344 return pref_notifier_.get(); | 373 return pref_notifier_.get(); |
345 } | 374 } |
346 | 375 |
347 bool PrefService::IsManagedPreference(const char* pref_name) const { | 376 bool PrefService::IsManagedPreference(const char* pref_name) const { |
348 const Preference* pref = FindPreference(pref_name); | 377 const Preference* pref = FindPreference(pref_name); |
349 if (pref && pref->IsManaged()) { | 378 return pref && pref->IsManaged(); |
350 return true; | |
351 } | |
352 return false; | |
353 } | 379 } |
354 | 380 |
355 const DictionaryValue* PrefService::GetDictionary(const char* path) const { | 381 const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
356 DCHECK(CalledOnValidThread()); | 382 DCHECK(CalledOnValidThread()); |
357 | 383 |
358 const Preference* pref = FindPreference(path); | 384 const Preference* pref = FindPreference(path); |
359 if (!pref) { | 385 if (!pref) { |
360 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 386 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
361 return NULL; | 387 return NULL; |
362 } | 388 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 | 424 |
399 if (FindPreference(path)) { | 425 if (FindPreference(path)) { |
400 NOTREACHED() << "Tried to register duplicate pref " << path; | 426 NOTREACHED() << "Tried to register duplicate pref " << path; |
401 return; | 427 return; |
402 } | 428 } |
403 | 429 |
404 Value::ValueType orig_type = default_value->GetType(); | 430 Value::ValueType orig_type = default_value->GetType(); |
405 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 431 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
406 "invalid preference type: " << orig_type; | 432 "invalid preference type: " << orig_type; |
407 | 433 |
408 // We set the default value of dictionaries and lists to be null so it's | 434 // Hand off ownership. |
409 // easier for callers to check for empty dict/list prefs. The PrefValueStore | 435 default_store_->SetDefaultValue(path, scoped_value.release()); |
410 // accepts ownership of the value (null or default_value). | |
411 if (Value::TYPE_LIST == orig_type || Value::TYPE_DICTIONARY == orig_type) { | |
412 default_store_->SetDefaultValue(path, Value::CreateNullValue()); | |
413 } else { | |
414 // Hand off ownership. | |
415 default_store_->SetDefaultValue(path, scoped_value.release()); | |
416 } | |
417 | |
418 pref_value_store_->RegisterPreferenceType(path, orig_type); | |
419 prefs_.insert(new Preference(this, path)); | |
420 } | 436 } |
421 | 437 |
422 void PrefService::ClearPref(const char* path) { | 438 void PrefService::ClearPref(const char* path) { |
423 DCHECK(CalledOnValidThread()); | 439 DCHECK(CalledOnValidThread()); |
424 | 440 |
425 const Preference* pref = FindPreference(path); | 441 const Preference* pref = FindPreference(path); |
426 if (!pref) { | 442 if (!pref) { |
427 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 443 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
428 return; | 444 return; |
429 } | 445 } |
430 user_pref_store_->RemoveValue(path); | 446 user_pref_store_->RemoveValue(path); |
431 } | 447 } |
432 | 448 |
433 void PrefService::Set(const char* path, const Value& value) { | 449 void PrefService::Set(const char* path, const Value& value) { |
434 DCHECK(CalledOnValidThread()); | 450 DCHECK(CalledOnValidThread()); |
435 | 451 |
436 const Preference* pref = FindPreference(path); | 452 const Preference* pref = FindPreference(path); |
437 if (!pref) { | 453 if (!pref) { |
438 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 454 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
439 return; | 455 return; |
440 } | 456 } |
441 | 457 |
442 // Allow dictionary and list types to be set to null, which removes their | 458 if (pref->GetType() != value.GetType()) { |
443 // user values. | |
444 if (value.GetType() == Value::TYPE_NULL && | |
445 (pref->GetType() == Value::TYPE_DICTIONARY || | |
446 pref->GetType() == Value::TYPE_LIST)) { | |
447 user_pref_store_->RemoveValue(path); | |
448 } else if (pref->GetType() != value.GetType()) { | |
449 NOTREACHED() << "Trying to set pref " << path | 459 NOTREACHED() << "Trying to set pref " << path |
450 << " of type " << pref->GetType() | 460 << " of type " << pref->GetType() |
451 << " to value of type " << value.GetType(); | 461 << " to value of type " << value.GetType(); |
452 } else { | 462 } else { |
453 user_pref_store_->SetValue(path, value.DeepCopy()); | 463 user_pref_store_->SetValue(path, value.DeepCopy()); |
454 } | 464 } |
455 } | 465 } |
456 | 466 |
457 void PrefService::SetBoolean(const char* path, bool value) { | 467 void PrefService::SetBoolean(const char* path, bool value) { |
458 SetUserPrefValue(path, Value::CreateBooleanValue(value)); | 468 SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
580 return; | 590 return; |
581 } | 591 } |
582 | 592 |
583 user_pref_store_->SetValue(path, new_value); | 593 user_pref_store_->SetValue(path, new_value); |
584 } | 594 } |
585 | 595 |
586 /////////////////////////////////////////////////////////////////////////////// | 596 /////////////////////////////////////////////////////////////////////////////// |
587 // PrefService::Preference | 597 // PrefService::Preference |
588 | 598 |
589 PrefService::Preference::Preference(const PrefService* service, | 599 PrefService::Preference::Preference(const PrefService* service, |
590 const char* name) | 600 const char* name, |
601 Value::ValueType type) | |
591 : name_(name), | 602 : name_(name), |
603 type_(type), | |
592 pref_service_(service) { | 604 pref_service_(service) { |
593 DCHECK(name); | 605 DCHECK(name); |
594 DCHECK(service); | 606 DCHECK(service); |
595 } | 607 } |
596 | 608 |
597 Value::ValueType PrefService::Preference::GetType() const { | 609 Value::ValueType PrefService::Preference::GetType() const { |
598 return pref_service_->pref_value_store_->GetRegisteredType(name_); | 610 return type_; |
599 } | 611 } |
600 | 612 |
601 const Value* PrefService::Preference::GetValue() const { | 613 const Value* PrefService::Preference::GetValue() const { |
602 DCHECK(pref_service_->FindPreference(name_.c_str())) << | 614 DCHECK(pref_service_->FindPreference(name_.c_str())) << |
603 "Must register pref before getting its value"; | 615 "Must register pref before getting its value"; |
604 | 616 |
605 Value* found_value = NULL; | 617 Value* found_value = NULL; |
606 if (pref_service_->pref_value_store_->GetValue(name_, &found_value)) | 618 if (pref_service_->pref_value_store_->GetValue(name_, type_, &found_value)) { |
619 DCHECK(found_value->IsType(type_)); | |
607 return found_value; | 620 return found_value; |
621 } | |
608 | 622 |
609 // Every registered preference has at least a default value. | 623 // Every registered preference has at least a default value. |
610 NOTREACHED() << "no valid value found for registered pref " << name_; | 624 NOTREACHED() << "no valid value found for registered pref " << name_; |
611 return NULL; | 625 return NULL; |
612 } | 626 } |
613 | 627 |
614 bool PrefService::Preference::IsManaged() const { | 628 bool PrefService::Preference::IsManaged() const { |
615 PrefValueStore* pref_value_store = pref_service_->pref_value_store_; | 629 PrefValueStore* pref_value_store = pref_service_->pref_value_store_; |
616 return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || | 630 return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || |
617 pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); | 631 pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); |
(...skipping 21 matching lines...) Expand all Loading... | |
639 | 653 |
640 bool PrefService::Preference::IsDefaultValue() const { | 654 bool PrefService::Preference::IsDefaultValue() const { |
641 return pref_service_->pref_value_store_-> | 655 return pref_service_->pref_value_store_-> |
642 PrefValueFromDefaultStore(name_.c_str()); | 656 PrefValueFromDefaultStore(name_.c_str()); |
643 } | 657 } |
644 | 658 |
645 bool PrefService::Preference::IsUserModifiable() const { | 659 bool PrefService::Preference::IsUserModifiable() const { |
646 return pref_service_->pref_value_store_-> | 660 return pref_service_->pref_value_store_-> |
647 PrefValueUserModifiable(name_.c_str()); | 661 PrefValueUserModifiable(name_.c_str()); |
648 } | 662 } |
OLD | NEW |