| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/common/pref_service.h" | 5 #include "chrome/common/pref_service.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 11 #include "base/task.h" | 12 #include "base/task.h" |
| 12 #include "base/thread.h" | 13 #include "base/thread.h" |
| 13 #include "chrome/common/json_value_serializer.h" | 14 #include "chrome/common/json_value_serializer.h" |
| 14 #include "chrome/common/l10n_util.h" | 15 #include "chrome/common/l10n_util.h" |
| 15 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/stl_util-inl.h" | 17 #include "chrome/common/stl_util-inl.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 33 data_(data) { | 34 data_(data) { |
| 34 } | 35 } |
| 35 | 36 |
| 36 void Run() { | 37 void Run() { |
| 37 // Write the data to a temp file then rename to avoid data loss if we crash | 38 // Write the data to a temp file then rename to avoid data loss if we crash |
| 38 // while writing the file. | 39 // while writing the file. |
| 39 std::wstring tmp_file_name = file_name_ + L".tmp"; | 40 std::wstring tmp_file_name = file_name_ + L".tmp"; |
| 40 int bytes_written = file_util::WriteFile(tmp_file_name, data_.c_str(), | 41 int bytes_written = file_util::WriteFile(tmp_file_name, data_.c_str(), |
| 41 static_cast<int>(data_.length())); | 42 static_cast<int>(data_.length())); |
| 42 if (bytes_written != -1) { | 43 if (bytes_written != -1) { |
| 43 if (!MoveFileEx(tmp_file_name.c_str(), file_name_.c_str(), | 44 if (!file_util::Move(tmp_file_name, file_name_)) { |
| 44 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) { | |
| 45 // Rename failed. Try again on the off chance someone has locked either | 45 // Rename failed. Try again on the off chance someone has locked either |
| 46 // file and hope we're successful the second time through. | 46 // file and hope we're successful the second time through. |
| 47 BOOL move_result = | 47 bool move_result = file_util::Move(tmp_file_name, file_name_); |
| 48 MoveFileEx(tmp_file_name.c_str(), file_name_.c_str(), | |
| 49 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING); | |
| 50 DCHECK(move_result); | 48 DCHECK(move_result); |
| 51 } | 49 } |
| 52 } | 50 } |
| 53 } | 51 } |
| 54 | 52 |
| 55 private: | 53 private: |
| 56 std::wstring file_name_; | 54 std::wstring file_name_; |
| 57 std::string data_; | 55 std::string data_; |
| 58 | 56 |
| 59 DISALLOW_EVIL_CONSTRUCTORS(SaveLaterTask); | 57 DISALLOW_COPY_AND_ASSIGN(SaveLaterTask); |
| 60 }; | 58 }; |
| 61 | 59 |
| 62 // A helper function for RegisterLocalized*Pref that creates a Value* based on | 60 // A helper function for RegisterLocalized*Pref that creates a Value* based on |
| 63 // the string value in the locale dll. Because we control the values in a | 61 // the string value in the locale dll. Because we control the values in a |
| 64 // locale dll, this should always return a Value of the appropriate type. | 62 // locale dll, this should always return a Value of the appropriate type. |
| 65 Value* CreateLocaleDefaultValue(Value::ValueType type, int message_id) { | 63 Value* CreateLocaleDefaultValue(Value::ValueType type, int message_id) { |
| 66 std::wstring resource_string = l10n_util::GetString(message_id); | 64 std::wstring resource_string = l10n_util::GetString(message_id); |
| 67 DCHECK(!resource_string.empty()); | 65 DCHECK(!resource_string.empty()); |
| 68 switch (type) { | 66 switch (type) { |
| 69 case Value::TYPE_BOOLEAN: { | 67 case Value::TYPE_BOOLEAN: { |
| 70 if (L"true" == resource_string) | 68 if (L"true" == resource_string) |
| 71 return Value::CreateBooleanValue(true); | 69 return Value::CreateBooleanValue(true); |
| 72 if (L"false" == resource_string) | 70 if (L"false" == resource_string) |
| 73 return Value::CreateBooleanValue(false); | 71 return Value::CreateBooleanValue(false); |
| 74 break; | 72 break; |
| 75 } | 73 } |
| 76 | 74 |
| 77 case Value::TYPE_INTEGER: { | 75 case Value::TYPE_INTEGER: { |
| 78 int num_int = 0; | 76 return Value::CreateIntegerValue(StringToInt(resource_string)); |
| 79 int parsed_values = swscanf_s(resource_string.c_str(), L"%d", &num_int); | |
| 80 // This is a trusted value (comes from our locale dll), so it should | |
| 81 // successfully parse. | |
| 82 DCHECK(parsed_values == 1); | |
| 83 return Value::CreateIntegerValue(num_int); | |
| 84 break; | 77 break; |
| 85 } | 78 } |
| 86 | 79 |
| 87 case Value::TYPE_REAL: { | 80 case Value::TYPE_REAL: { |
| 88 double num_double = 0.0; | 81 return Value::CreateRealValue(StringToDouble(resource_string)); |
| 89 int parsed_values = swscanf_s(resource_string.c_str(), L"%lf", | |
| 90 &num_double); | |
| 91 // This is a trusted value (comes from our locale dll), so it should | |
| 92 // successfully parse. | |
| 93 DCHECK(parsed_values == 1); | |
| 94 return Value::CreateRealValue(num_double); | |
| 95 break; | 82 break; |
| 96 } | 83 } |
| 97 | 84 |
| 98 case Value::TYPE_STRING: { | 85 case Value::TYPE_STRING: { |
| 99 return Value::CreateStringValue(resource_string); | 86 return Value::CreateStringValue(resource_string); |
| 100 break; | 87 break; |
| 101 } | 88 } |
| 102 | 89 |
| 103 default: { | 90 default: { |
| 104 DCHECK(false) << | 91 DCHECK(false) << |
| 105 "list and dictionary types can not have default locale values"; | 92 "list and dictionary types can not have default locale values"; |
| 106 } | 93 } |
| 107 } | 94 } |
| 108 NOTREACHED(); | 95 NOTREACHED(); |
| 109 return Value::CreateNullValue(); | 96 return Value::CreateNullValue(); |
| 110 } | 97 } |
| 111 | 98 |
| 112 } // namespace | 99 } // namespace |
| 113 | 100 |
| 114 PrefService::PrefService() | 101 PrefService::PrefService() |
| 115 : persistent_(new DictionaryValue), | 102 : persistent_(new DictionaryValue), |
| 116 transient_(new DictionaryValue), | 103 transient_(new DictionaryValue), |
| 117 save_preferences_factory_(NULL) { | 104 save_preferences_factory_(NULL) { |
| 118 } | 105 } |
| 119 | 106 |
| 120 PrefService::PrefService(const std::wstring& pref_filename) | 107 PrefService::PrefService(const std::wstring& pref_filename) |
| 121 : persistent_(new DictionaryValue), | 108 : persistent_(new DictionaryValue), |
| 122 transient_(new DictionaryValue), | 109 transient_(new DictionaryValue), |
| 123 pref_filename_(pref_filename), | 110 pref_filename_(pref_filename), |
| 124 #pragma warning(suppress: 4355) // Okay to pass "this" here. | 111 ALLOW_THIS_IN_INITIALIZER_LIST(save_preferences_factory_(this)) { |
| 125 save_preferences_factory_(this) { | |
| 126 LoadPersistentPrefs(pref_filename_); | 112 LoadPersistentPrefs(pref_filename_); |
| 127 } | 113 } |
| 128 | 114 |
| 129 PrefService::~PrefService() { | 115 PrefService::~PrefService() { |
| 130 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
| 131 | 117 |
| 132 // Verify that there are no pref observers when we shut down. | 118 // Verify that there are no pref observers when we shut down. |
| 133 for (PrefObserverMap::iterator it = pref_observers_.begin(); | 119 for (PrefObserverMap::iterator it = pref_observers_.begin(); |
| 134 it != pref_observers_.end(); ++it) { | 120 it != pref_observers_.end(); ++it) { |
| 135 NotificationObserverList::Iterator obs_iterator(*(it->second)); | 121 NotificationObserverList::Iterator obs_iterator(*(it->second)); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 if (observer_iterator == pref_observers_.end()) { | 413 if (observer_iterator == pref_observers_.end()) { |
| 428 observer_list = new NotificationObserverList; | 414 observer_list = new NotificationObserverList; |
| 429 pref_observers_[path] = observer_list; | 415 pref_observers_[path] = observer_list; |
| 430 } else { | 416 } else { |
| 431 observer_list = observer_iterator->second; | 417 observer_list = observer_iterator->second; |
| 432 } | 418 } |
| 433 | 419 |
| 434 // Verify that this observer doesn't already exist. | 420 // Verify that this observer doesn't already exist. |
| 435 NotificationObserverList::Iterator it(*observer_list); | 421 NotificationObserverList::Iterator it(*observer_list); |
| 436 NotificationObserver* existing_obs; | 422 NotificationObserver* existing_obs; |
| 437 while (existing_obs = it.GetNext()) { | 423 while ((existing_obs = it.GetNext()) != NULL) { |
| 438 DCHECK(existing_obs != obs) << path << " observer already registered"; | 424 DCHECK(existing_obs != obs) << path << " observer already registered"; |
| 439 if (existing_obs == obs) | 425 if (existing_obs == obs) |
| 440 return; | 426 return; |
| 441 } | 427 } |
| 442 | 428 |
| 443 // Ok, safe to add the pref observer. | 429 // Ok, safe to add the pref observer. |
| 444 observer_list->AddObserver(obs); | 430 observer_list->AddObserver(obs); |
| 445 } | 431 } |
| 446 | 432 |
| 447 void PrefService::RemovePrefObserver(const wchar_t* path, | 433 void PrefService::RemovePrefObserver(const wchar_t* path, |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 | 619 |
| 634 // Convert path to a std::wstring because the Details constructor requires a | 620 // Convert path to a std::wstring because the Details constructor requires a |
| 635 // class. | 621 // class. |
| 636 std::wstring path_str(path); | 622 std::wstring path_str(path); |
| 637 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path_str); | 623 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path_str); |
| 638 if (observer_iterator == pref_observers_.end()) | 624 if (observer_iterator == pref_observers_.end()) |
| 639 return; | 625 return; |
| 640 | 626 |
| 641 NotificationObserverList::Iterator it(*(observer_iterator->second)); | 627 NotificationObserverList::Iterator it(*(observer_iterator->second)); |
| 642 NotificationObserver* observer; | 628 NotificationObserver* observer; |
| 643 while (observer = it.GetNext()) { | 629 while ((observer = it.GetNext()) != NULL) { |
| 644 observer->Observe(NOTIFY_PREF_CHANGED, | 630 observer->Observe(NOTIFY_PREF_CHANGED, |
| 645 Source<PrefService>(this), | 631 Source<PrefService>(this), |
| 646 Details<std::wstring>(&path_str)); | 632 Details<std::wstring>(&path_str)); |
| 647 } | 633 } |
| 648 } | 634 } |
| 649 | 635 |
| 650 /////////////////////////////////////////////////////////////////////////////// | 636 /////////////////////////////////////////////////////////////////////////////// |
| 651 // PrefService::Preference | 637 // PrefService::Preference |
| 652 | 638 |
| 653 PrefService::Preference::Preference(DictionaryValue* root_pref, | 639 PrefService::Preference::Preference(DictionaryValue* root_pref, |
| 654 const wchar_t* name, | 640 const wchar_t* name, |
| 655 Value* default_value) | 641 Value* default_value) |
| 656 : root_pref_(root_pref), | 642 : type_(Value::TYPE_NULL), |
| 657 name_(name), | 643 name_(name), |
| 658 default_value_(default_value), | 644 default_value_(default_value), |
| 659 type_(Value::TYPE_NULL) { | 645 root_pref_(root_pref) { |
| 660 DCHECK(name); | 646 DCHECK(name); |
| 661 | 647 |
| 662 if (default_value) { | 648 if (default_value) { |
| 663 type_ = default_value->GetType(); | 649 type_ = default_value->GetType(); |
| 664 DCHECK(type_ != Value::TYPE_NULL && type_ != Value::TYPE_BINARY) << | 650 DCHECK(type_ != Value::TYPE_NULL && type_ != Value::TYPE_BINARY) << |
| 665 "invalid preference type: " << type_; | 651 "invalid preference type: " << type_; |
| 666 } | 652 } |
| 667 | 653 |
| 668 // We set the default value of lists and dictionaries to be null so it's | 654 // We set the default value of lists and dictionaries to be null so it's |
| 669 // easier for callers to check for empty list/dict prefs. | 655 // easier for callers to check for empty list/dict prefs. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 683 | 669 |
| 684 // Pref not found, just return the app default. | 670 // Pref not found, just return the app default. |
| 685 return default_value_.get(); | 671 return default_value_.get(); |
| 686 } | 672 } |
| 687 | 673 |
| 688 bool PrefService::Preference::IsDefaultValue() const { | 674 bool PrefService::Preference::IsDefaultValue() const { |
| 689 DCHECK(default_value_.get()); | 675 DCHECK(default_value_.get()); |
| 690 return default_value_->Equals(GetValue()); | 676 return default_value_->Equals(GetValue()); |
| 691 } | 677 } |
| 692 | 678 |
| OLD | NEW |