[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/service/service_process_prefs.h" |
| 6 | |
| 7 | #include "base/values.h" |
| 8 | |
| 9 | ServiceProcessPrefs::ServiceProcessPrefs( |
| 10 | const FilePath& pref_filename, |
[email protected] | 370bc73 | 2012-05-07 21:25:20 | [diff] [blame] | 11 | base::MessageLoopProxy* file_message_loop_proxy) |
| 12 | : prefs_(new JsonPrefStore(pref_filename, file_message_loop_proxy)) { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 13 | } |
| 14 | |
[email protected] | 0a071a3 | 2011-02-08 00:18:24 | [diff] [blame] | 15 | ServiceProcessPrefs::~ServiceProcessPrefs() {} |
| 16 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 17 | void ServiceProcessPrefs::ReadPrefs() { |
[email protected] | c3498a1 | 2011-02-02 04:14:42 | [diff] [blame] | 18 | prefs_->ReadPrefs(); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void ServiceProcessPrefs::WritePrefs() { |
[email protected] | fbe17c8a | 2011-12-27 16:41:48 | [diff] [blame] | 22 | prefs_->CommitPendingWrite(); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 23 | } |
| 24 | |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame^] | 25 | std::string ServiceProcessPrefs::GetString( |
| 26 | const std::string& key, |
| 27 | const std::string& default_value) const { |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 28 | const Value* value; |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame^] | 29 | std::string result; |
| 30 | if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || |
| 31 | !value->GetAsString(&result)) { |
| 32 | return default_value; |
| 33 | } |
| 34 | return result; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void ServiceProcessPrefs::SetString(const std::string& key, |
| 38 | const std::string& value) { |
[email protected] | c3498a1 | 2011-02-02 04:14:42 | [diff] [blame] | 39 | prefs_->SetValue(key, Value::CreateStringValue(value)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame^] | 42 | bool ServiceProcessPrefs::GetBoolean(const std::string& key, |
| 43 | bool default_value) const { |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 44 | const Value* value; |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame^] | 45 | bool result = false; |
| 46 | if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || |
| 47 | !value->GetAsBoolean(&result)) { |
| 48 | return default_value; |
| 49 | } |
| 50 | return result; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) { |
[email protected] | c3498a1 | 2011-02-02 04:14:42 | [diff] [blame] | 54 | prefs_->SetValue(key, Value::CreateBooleanValue(value)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame^] | 57 | const DictionaryValue* ServiceProcessPrefs::GetDictionary( |
| 58 | const std::string& key) const { |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 59 | const Value* value; |
[email protected] | c3498a1 | 2011-02-02 04:14:42 | [diff] [blame] | 60 | if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 61 | !value->IsType(Value::TYPE_DICTIONARY)) { |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame^] | 62 | return NULL; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame^] | 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); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 77 | } |
[email protected] | bb07338 | 2011-05-06 23:22:21 | [diff] [blame] | 78 | |
| 79 | void ServiceProcessPrefs::RemovePref(const std::string& key) { |
| 80 | prefs_->RemoveValue(key); |
| 81 | } |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame^] | 82 | |