blob: e1aca1a1f7abe99d8f503be362b5858fb41cc902 [file] [log] [blame]
[email protected]68bf41a2011-03-25 16:38:311// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f2d1f612010-12-09 15:10:172// 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/values.h"
8
9ServiceProcessPrefs::ServiceProcessPrefs(
10 const FilePath& pref_filename,
[email protected]370bc732012-05-07 21:25:2011 base::MessageLoopProxy* file_message_loop_proxy)
12 : prefs_(new JsonPrefStore(pref_filename, file_message_loop_proxy)) {
[email protected]f2d1f612010-12-09 15:10:1713}
14
[email protected]0a071a32011-02-08 00:18:2415ServiceProcessPrefs::~ServiceProcessPrefs() {}
16
[email protected]f2d1f612010-12-09 15:10:1717void ServiceProcessPrefs::ReadPrefs() {
[email protected]c3498a12011-02-02 04:14:4218 prefs_->ReadPrefs();
[email protected]f2d1f612010-12-09 15:10:1719}
20
21void ServiceProcessPrefs::WritePrefs() {
[email protected]fbe17c8a2011-12-27 16:41:4822 prefs_->CommitPendingWrite();
[email protected]f2d1f612010-12-09 15:10:1723}
24
[email protected]6d1db74e2012-09-23 03:51:4125std::string ServiceProcessPrefs::GetString(
26 const std::string& key,
27 const std::string& default_value) const {
[email protected]68bf41a2011-03-25 16:38:3128 const Value* value;
[email protected]6d1db74e2012-09-23 03:51:4129 std::string result;
30 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK ||
31 !value->GetAsString(&result)) {
32 return default_value;
33 }
34 return result;
[email protected]f2d1f612010-12-09 15:10:1735}
36
37void ServiceProcessPrefs::SetString(const std::string& key,
38 const std::string& value) {
[email protected]c3498a12011-02-02 04:14:4239 prefs_->SetValue(key, Value::CreateStringValue(value));
[email protected]f2d1f612010-12-09 15:10:1740}
41
[email protected]6d1db74e2012-09-23 03:51:4142bool ServiceProcessPrefs::GetBoolean(const std::string& key,
43 bool default_value) const {
[email protected]68bf41a2011-03-25 16:38:3144 const Value* value;
[email protected]6d1db74e2012-09-23 03:51:4145 bool result = false;
46 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK ||
47 !value->GetAsBoolean(&result)) {
48 return default_value;
49 }
50 return result;
[email protected]f2d1f612010-12-09 15:10:1751}
52
53void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) {
[email protected]c3498a12011-02-02 04:14:4254 prefs_->SetValue(key, Value::CreateBooleanValue(value));
[email protected]f2d1f612010-12-09 15:10:1755}
56
[email protected]6d1db74e2012-09-23 03:51:4157const DictionaryValue* ServiceProcessPrefs::GetDictionary(
58 const std::string& key) const {
[email protected]68bf41a2011-03-25 16:38:3159 const Value* value;
[email protected]c3498a12011-02-02 04:14:4260 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK ||
[email protected]f2d1f612010-12-09 15:10:1761 !value->IsType(Value::TYPE_DICTIONARY)) {
[email protected]6d1db74e2012-09-23 03:51:4162 return NULL;
[email protected]f2d1f612010-12-09 15:10:1763 }
64
[email protected]6d1db74e2012-09-23 03:51:4165 return static_cast<const DictionaryValue*>(value);
66}
67
68const base::ListValue* ServiceProcessPrefs::GetList(
69 const std::string& key) const {
70 const Value* value;
71 if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK ||
72 !value->IsType(Value::TYPE_LIST)) {
73 return NULL;
74 }
75
76 return static_cast<const ListValue*>(value);
[email protected]f2d1f612010-12-09 15:10:1777}
[email protected]bb073382011-05-06 23:22:2178
79void ServiceProcessPrefs::RemovePref(const std::string& key) {
80 prefs_->RemoveValue(key);
81}
[email protected]6d1db74e2012-09-23 03:51:4182