| 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/service/service_process_prefs.h" | 5 #include "chrome/service/service_process_prefs.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 | 8 |
| 9 ServiceProcessPrefs::ServiceProcessPrefs( | 9 ServiceProcessPrefs::ServiceProcessPrefs( |
| 10 const FilePath& pref_filename, | 10 const FilePath& pref_filename, |
| 11 base::MessageLoopProxy* file_message_loop_proxy) | 11 base::MessageLoopProxy* file_message_loop_proxy) |
| 12 : prefs_(new JsonPrefStore(pref_filename, file_message_loop_proxy)) { | 12 : prefs_(new JsonPrefStore(pref_filename, file_message_loop_proxy)) { |
| 13 } | 13 } |
| 14 | 14 |
| 15 ServiceProcessPrefs::~ServiceProcessPrefs() {} | 15 ServiceProcessPrefs::~ServiceProcessPrefs() {} |
| 16 | 16 |
| 17 void ServiceProcessPrefs::ReadPrefs() { | 17 void ServiceProcessPrefs::ReadPrefs() { |
| 18 prefs_->ReadPrefs(); | 18 prefs_->ReadPrefs(); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void ServiceProcessPrefs::WritePrefs() { | 21 void ServiceProcessPrefs::WritePrefs() { |
| 22 prefs_->CommitPendingWrite(); | 22 prefs_->CommitPendingWrite(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void ServiceProcessPrefs::GetString(const std::string& key, | 25 std::string ServiceProcessPrefs::GetString( |
| 26 std::string* result) const { | 26 const std::string& key, |
| 27 const std::string& default_value) const { |
| 27 const Value* value; | 28 const Value* value; |
| 28 if (prefs_->GetValue(key, &value) == PersistentPrefStore::READ_OK) | 29 std::string result; |
| 29 value->GetAsString(result); | 30 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || |
| 31 !value->GetAsString(&result)) { |
| 32 return default_value; |
| 33 } |
| 34 return result; |
| 30 } | 35 } |
| 31 | 36 |
| 32 void ServiceProcessPrefs::SetString(const std::string& key, | 37 void ServiceProcessPrefs::SetString(const std::string& key, |
| 33 const std::string& value) { | 38 const std::string& value) { |
| 34 prefs_->SetValue(key, Value::CreateStringValue(value)); | 39 prefs_->SetValue(key, Value::CreateStringValue(value)); |
| 35 } | 40 } |
| 36 | 41 |
| 37 void ServiceProcessPrefs::GetBoolean(const std::string& key, | 42 bool ServiceProcessPrefs::GetBoolean(const std::string& key, |
| 38 bool* result) const { | 43 bool default_value) const { |
| 39 const Value* value; | 44 const Value* value; |
| 40 if (prefs_->GetValue(key, &value) == PersistentPrefStore::READ_OK) | 45 bool result = false; |
| 41 value->GetAsBoolean(result); | 46 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || |
| 47 !value->GetAsBoolean(&result)) { |
| 48 return default_value; |
| 49 } |
| 50 return result; |
| 42 } | 51 } |
| 43 | 52 |
| 44 void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) { | 53 void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) { |
| 45 prefs_->SetValue(key, Value::CreateBooleanValue(value)); | 54 prefs_->SetValue(key, Value::CreateBooleanValue(value)); |
| 46 } | 55 } |
| 47 | 56 |
| 48 void ServiceProcessPrefs::GetDictionary(const std::string& key, | 57 const DictionaryValue* ServiceProcessPrefs::GetDictionary( |
| 49 const DictionaryValue** result) const { | 58 const std::string& key) const { |
| 50 const Value* value; | 59 const Value* value; |
| 51 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || | 60 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || |
| 52 !value->IsType(Value::TYPE_DICTIONARY)) { | 61 !value->IsType(Value::TYPE_DICTIONARY)) { |
| 53 return; | 62 return NULL; |
| 54 } | 63 } |
| 55 | 64 |
| 56 *result = static_cast<const DictionaryValue*>(value); | 65 return static_cast<const DictionaryValue*>(value); |
| 66 } |
| 67 |
| 68 const base::ListValue* ServiceProcessPrefs::GetList( |
| 69 const std::string& key) const { |
| 70 const Value* value; |
| 71 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || |
| 72 !value->IsType(Value::TYPE_LIST)) { |
| 73 return NULL; |
| 74 } |
| 75 |
| 76 return static_cast<const ListValue*>(value); |
| 57 } | 77 } |
| 58 | 78 |
| 59 void ServiceProcessPrefs::RemovePref(const std::string& key) { | 79 void ServiceProcessPrefs::RemovePref(const std::string& key) { |
| 60 prefs_->RemoveValue(key); | 80 prefs_->RemoveValue(key); |
| 61 } | 81 } |
| 82 |
| OLD | NEW |