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
3 // found in the LICENSE file.
4
5 #include "chrome/service/service_process_prefs.h"
6
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "base/prefs/pref_filter.h"
9 #include "base/values.h"
10
ServiceProcessPrefs(const base::FilePath & pref_filename,base::SequencedTaskRunner * task_runner)11 ServiceProcessPrefs::ServiceProcessPrefs(
12 const base::FilePath& pref_filename,
13 base::SequencedTaskRunner* task_runner)
14 : prefs_(new JsonPrefStore(pref_filename,
15 task_runner,
16 scoped_ptr<PrefFilter>())) {
17 }
18
~ServiceProcessPrefs()19 ServiceProcessPrefs::~ServiceProcessPrefs() {}
20
ReadPrefs()21 void ServiceProcessPrefs::ReadPrefs() {
22 prefs_->ReadPrefs();
23 }
24
WritePrefs()25 void ServiceProcessPrefs::WritePrefs() {
26 prefs_->CommitPendingWrite();
27 }
28
GetString(const std::string & key,const std::string & default_value) const29 std::string ServiceProcessPrefs::GetString(
30 const std::string& key,
31 const std::string& default_value) const {
32 const base::Value* value;
33 std::string result;
34 if (!prefs_->GetValue(key, &value) || !value->GetAsString(&result))
35 return default_value;
36
37 return result;
38 }
39
SetString(const std::string & key,const std::string & value)40 void ServiceProcessPrefs::SetString(const std::string& key,
41 const std::string& value) {
42 prefs_->SetValue(key, new base::StringValue(value));
43 }
44
GetBoolean(const std::string & key,bool default_value) const45 bool ServiceProcessPrefs::GetBoolean(const std::string& key,
46 bool default_value) const {
47 const base::Value* value;
48 bool result = false;
49 if (!prefs_->GetValue(key, &value) || !value->GetAsBoolean(&result))
50 return default_value;
51
52 return result;
53 }
54
SetBoolean(const std::string & key,bool value)55 void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) {
56 prefs_->SetValue(key, new base::FundamentalValue(value));
57 }
58
GetInt(const std::string & key,int default_value) const59 int ServiceProcessPrefs::GetInt(const std::string& key,
60 int default_value) const {
61 const base::Value* value;
62 int result = default_value;
63 if (!prefs_->GetValue(key, &value) || !value->GetAsInteger(&result))
64 return default_value;
65
66 return result;
67 }
68
SetInt(const std::string & key,int value)69 void ServiceProcessPrefs::SetInt(const std::string& key, int value) {
70 prefs_->SetValue(key, new base::FundamentalValue(value));
71 }
72
GetDictionary(const std::string & key) const73 const base::DictionaryValue* ServiceProcessPrefs::GetDictionary(
74 const std::string& key) const {
75 const base::Value* value;
76 if (!prefs_->GetValue(key, &value) ||
77 !value->IsType(base::Value::TYPE_DICTIONARY)) {
78 return NULL;
79 }
80
81 return static_cast<const base::DictionaryValue*>(value);
82 }
83
GetList(const std::string & key) const84 const base::ListValue* ServiceProcessPrefs::GetList(
85 const std::string& key) const {
86 const base::Value* value;
87 if (!prefs_->GetValue(key, &value) || !value->IsType(base::Value::TYPE_LIST))
88 return NULL;
89
90 return static_cast<const base::ListValue*>(value);
91 }
92
SetValue(const std::string & key,base::Value * value)93 void ServiceProcessPrefs::SetValue(const std::string& key, base::Value* value) {
94 prefs_->SetValue(key, value);
95 }
96
RemovePref(const std::string & key)97 void ServiceProcessPrefs::RemovePref(const std::string& key) {
98 prefs_->RemoveValue(key);
99 }
100
101