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