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.
5 #include "chrome/service/service_process_prefs.h"
7 #include "base/prefs/pref_filter.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "base/values.h"
11 ServiceProcessPrefs::ServiceProcessPrefs(
12 const base::FilePath
& pref_filename
,
13 base::SequencedTaskRunner
* task_runner
)
14 : prefs_(new JsonPrefStore(pref_filename
,
16 scoped_ptr
<PrefFilter
>())) {
19 ServiceProcessPrefs::~ServiceProcessPrefs() {}
21 void ServiceProcessPrefs::ReadPrefs() {
25 void ServiceProcessPrefs::WritePrefs() {
26 prefs_
->CommitPendingWrite();
29 std::string
ServiceProcessPrefs::GetString(
30 const std::string
& key
,
31 const std::string
& default_value
) const {
32 const base::Value
* value
;
34 if (!prefs_
->GetValue(key
, &value
) || !value
->GetAsString(&result
))
40 void ServiceProcessPrefs::SetString(const std::string
& key
,
41 const std::string
& value
) {
42 prefs_
->SetValue(key
, make_scoped_ptr(new base::StringValue(value
)),
43 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
46 bool ServiceProcessPrefs::GetBoolean(const std::string
& key
,
47 bool default_value
) const {
48 const base::Value
* value
;
50 if (!prefs_
->GetValue(key
, &value
) || !value
->GetAsBoolean(&result
))
56 void ServiceProcessPrefs::SetBoolean(const std::string
& key
, bool value
) {
57 prefs_
->SetValue(key
, make_scoped_ptr(new base::FundamentalValue(value
)),
58 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
61 int ServiceProcessPrefs::GetInt(const std::string
& key
,
62 int default_value
) const {
63 const base::Value
* value
;
64 int result
= default_value
;
65 if (!prefs_
->GetValue(key
, &value
) || !value
->GetAsInteger(&result
))
71 void ServiceProcessPrefs::SetInt(const std::string
& key
, int value
) {
72 prefs_
->SetValue(key
, make_scoped_ptr(new base::FundamentalValue(value
)),
73 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
76 const base::DictionaryValue
* ServiceProcessPrefs::GetDictionary(
77 const std::string
& key
) const {
78 const base::Value
* value
;
79 if (!prefs_
->GetValue(key
, &value
) ||
80 !value
->IsType(base::Value::TYPE_DICTIONARY
)) {
84 return static_cast<const base::DictionaryValue
*>(value
);
87 const base::ListValue
* ServiceProcessPrefs::GetList(
88 const std::string
& key
) const {
89 const base::Value
* value
;
90 if (!prefs_
->GetValue(key
, &value
) || !value
->IsType(base::Value::TYPE_LIST
))
93 return static_cast<const base::ListValue
*>(value
);
96 void ServiceProcessPrefs::SetValue(const std::string
& key
,
97 scoped_ptr
<base::Value
> value
) {
98 prefs_
->SetValue(key
, value
.Pass(),
99 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
102 void ServiceProcessPrefs::RemovePref(const std::string
& key
) {
103 prefs_
->RemoveValue(key
, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);