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
11ServiceProcessPrefs::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
19ServiceProcessPrefs::~ServiceProcessPrefs() {}
20
21void ServiceProcessPrefs::ReadPrefs() {
22  prefs_->ReadPrefs();
23}
24
25void ServiceProcessPrefs::WritePrefs() {
26  prefs_->CommitPendingWrite();
27}
28
29std::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
40void ServiceProcessPrefs::SetString(const std::string& key,
41                                    const std::string& value) {
42  prefs_->SetValue(key, new base::StringValue(value));
43}
44
45bool 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
55void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) {
56  prefs_->SetValue(key, new base::FundamentalValue(value));
57}
58
59int 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
69void ServiceProcessPrefs::SetInt(const std::string& key, int value) {
70  prefs_->SetValue(key, new base::FundamentalValue(value));
71}
72
73const 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
84const 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
93void ServiceProcessPrefs::SetValue(const std::string& key, base::Value* value) {
94  prefs_->SetValue(key, value);
95}
96
97void ServiceProcessPrefs::RemovePref(const std::string& key) {
98  prefs_->RemoveValue(key);
99}
100
101