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 std::string ServiceProcessPrefs::GetString( | 25 std::string ServiceProcessPrefs::GetString( |
26 const std::string& key, | 26 const std::string& key, |
27 const std::string& default_value) const { | 27 const std::string& default_value) const { |
28 const Value* value; | 28 const Value* value; |
29 std::string result; | 29 std::string result; |
30 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || | 30 if (!prefs_->GetValue(key, &value) || !value->GetAsString(&result)) |
31 !value->GetAsString(&result)) { | |
32 return default_value; | 31 return default_value; |
33 } | 32 |
34 return result; | 33 return result; |
35 } | 34 } |
36 | 35 |
37 void ServiceProcessPrefs::SetString(const std::string& key, | 36 void ServiceProcessPrefs::SetString(const std::string& key, |
38 const std::string& value) { | 37 const std::string& value) { |
39 prefs_->SetValue(key, Value::CreateStringValue(value)); | 38 prefs_->SetValue(key, Value::CreateStringValue(value)); |
40 } | 39 } |
41 | 40 |
42 bool ServiceProcessPrefs::GetBoolean(const std::string& key, | 41 bool ServiceProcessPrefs::GetBoolean(const std::string& key, |
43 bool default_value) const { | 42 bool default_value) const { |
44 const Value* value; | 43 const Value* value; |
45 bool result = false; | 44 bool result = false; |
46 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || | 45 if (!prefs_->GetValue(key, &value) || !value->GetAsBoolean(&result)) |
47 !value->GetAsBoolean(&result)) { | |
48 return default_value; | 46 return default_value; |
49 } | 47 |
50 return result; | 48 return result; |
51 } | 49 } |
52 | 50 |
53 void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) { | 51 void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) { |
54 prefs_->SetValue(key, Value::CreateBooleanValue(value)); | 52 prefs_->SetValue(key, Value::CreateBooleanValue(value)); |
55 } | 53 } |
56 | 54 |
57 int ServiceProcessPrefs::GetInt(const std::string& key, | 55 int ServiceProcessPrefs::GetInt(const std::string& key, |
58 int default_value) const { | 56 int default_value) const { |
59 const Value* value; | 57 const Value* value; |
60 int result = default_value; | 58 int result = default_value; |
61 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || | 59 if (!prefs_->GetValue(key, &value) || !value->GetAsInteger(&result)) |
62 !value->GetAsInteger(&result)) { | |
63 return default_value; | 60 return default_value; |
64 } | 61 |
65 return result; | 62 return result; |
66 } | 63 } |
67 | 64 |
68 void ServiceProcessPrefs::SetInt(const std::string& key, int value) { | 65 void ServiceProcessPrefs::SetInt(const std::string& key, int value) { |
69 prefs_->SetValue(key, Value::CreateIntegerValue(value)); | 66 prefs_->SetValue(key, Value::CreateIntegerValue(value)); |
70 } | 67 } |
71 | 68 |
72 const DictionaryValue* ServiceProcessPrefs::GetDictionary( | 69 const DictionaryValue* ServiceProcessPrefs::GetDictionary( |
73 const std::string& key) const { | 70 const std::string& key) const { |
74 const Value* value; | 71 const Value* value; |
75 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || | 72 if (!prefs_->GetValue(key, &value) || |
76 !value->IsType(Value::TYPE_DICTIONARY)) { | 73 !value->IsType(Value::TYPE_DICTIONARY)) { |
77 return NULL; | 74 return NULL; |
78 } | 75 } |
79 | 76 |
80 return static_cast<const DictionaryValue*>(value); | 77 return static_cast<const DictionaryValue*>(value); |
81 } | 78 } |
82 | 79 |
83 const base::ListValue* ServiceProcessPrefs::GetList( | 80 const base::ListValue* ServiceProcessPrefs::GetList( |
84 const std::string& key) const { | 81 const std::string& key) const { |
85 const Value* value; | 82 const Value* value; |
86 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || | 83 if (!prefs_->GetValue(key, &value) || !value->IsType(Value::TYPE_LIST)) |
87 !value->IsType(Value::TYPE_LIST)) { | 84 return NULL; |
88 return NULL; | |
89 } | |
90 | 85 |
91 return static_cast<const ListValue*>(value); | 86 return static_cast<const ListValue*>(value); |
92 } | 87 } |
93 | 88 |
94 void ServiceProcessPrefs::SetValue(const std::string& key, base::Value* value) { | 89 void ServiceProcessPrefs::SetValue(const std::string& key, base::Value* value) { |
95 prefs_->SetValue(key, value); | 90 prefs_->SetValue(key, value); |
96 } | 91 } |
97 | 92 |
98 void ServiceProcessPrefs::RemovePref(const std::string& key) { | 93 void ServiceProcessPrefs::RemovePref(const std::string& key) { |
99 prefs_->RemoveValue(key); | 94 prefs_->RemoveValue(key); |
100 } | 95 } |
101 | 96 |
OLD | NEW |