blob: b26af3e61cd975b405abab90d1c72c676628b913 [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
[email protected]56cbcb3a2013-12-23 21:24:467#include "base/prefs/pref_filter.h"
skyostil140473b2015-06-10 16:44:458#include "base/thread_task_runner_handle.h"
[email protected]f2d1f612010-12-09 15:10:179#include "base/values.h"
10
11ServiceProcessPrefs::ServiceProcessPrefs(
[email protected]3ea1b182013-02-08 22:38:4112 const base::FilePath& pref_filename,
[email protected]0de615a2012-11-08 04:40:5913 base::SequencedTaskRunner* task_runner)
[email protected]56cbcb3a2013-12-23 21:24:4614 : prefs_(new JsonPrefStore(pref_filename,
15 task_runner,
16 scoped_ptr<PrefFilter>())) {
[email protected]f2d1f612010-12-09 15:10:1717}
18
[email protected]0a071a32011-02-08 00:18:2419ServiceProcessPrefs::~ServiceProcessPrefs() {}
20
[email protected]f2d1f612010-12-09 15:10:1721void ServiceProcessPrefs::ReadPrefs() {
[email protected]c3498a12011-02-02 04:14:4222 prefs_->ReadPrefs();
[email protected]f2d1f612010-12-09 15:10:1723}
24
25void ServiceProcessPrefs::WritePrefs() {
[email protected]fbe17c8a2011-12-27 16:41:4826 prefs_->CommitPendingWrite();
[email protected]f2d1f612010-12-09 15:10:1727}
28
[email protected]6d1db74e2012-09-23 03:51:4129std::string ServiceProcessPrefs::GetString(
30 const std::string& key,
31 const std::string& default_value) const {
[email protected]378678f2013-02-15 11:50:5732 const base::Value* value;
[email protected]6d1db74e2012-09-23 03:51:4133 std::string result;
[email protected]892f1d62012-11-08 18:24:3434 if (!prefs_->GetValue(key, &value) || !value->GetAsString(&result))
[email protected]6d1db74e2012-09-23 03:51:4135 return default_value;
[email protected]892f1d62012-11-08 18:24:3436
[email protected]6d1db74e2012-09-23 03:51:4137 return result;
[email protected]f2d1f612010-12-09 15:10:1738}
39
40void ServiceProcessPrefs::SetString(const std::string& key,
41 const std::string& value) {
estade0bd407f2015-06-26 18:16:1842 prefs_->SetValue(key, make_scoped_ptr(new base::StringValue(value)),
raymes76de1af2015-05-06 03:22:2143 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]f2d1f612010-12-09 15:10:1744}
45
[email protected]6d1db74e2012-09-23 03:51:4146bool ServiceProcessPrefs::GetBoolean(const std::string& key,
47 bool default_value) const {
[email protected]378678f2013-02-15 11:50:5748 const base::Value* value;
[email protected]6d1db74e2012-09-23 03:51:4149 bool result = false;
[email protected]892f1d62012-11-08 18:24:3450 if (!prefs_->GetValue(key, &value) || !value->GetAsBoolean(&result))
[email protected]6d1db74e2012-09-23 03:51:4151 return default_value;
[email protected]892f1d62012-11-08 18:24:3452
[email protected]6d1db74e2012-09-23 03:51:4153 return result;
[email protected]f2d1f612010-12-09 15:10:1754}
55
56void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) {
estade0bd407f2015-06-26 18:16:1857 prefs_->SetValue(key, make_scoped_ptr(new base::FundamentalValue(value)),
raymes76de1af2015-05-06 03:22:2158 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]f2d1f612010-12-09 15:10:1759}
60
[email protected]05861d62012-10-26 19:59:4161int ServiceProcessPrefs::GetInt(const std::string& key,
62 int default_value) const {
[email protected]378678f2013-02-15 11:50:5763 const base::Value* value;
[email protected]05861d62012-10-26 19:59:4164 int result = default_value;
[email protected]892f1d62012-11-08 18:24:3465 if (!prefs_->GetValue(key, &value) || !value->GetAsInteger(&result))
[email protected]05861d62012-10-26 19:59:4166 return default_value;
[email protected]892f1d62012-11-08 18:24:3467
[email protected]05861d62012-10-26 19:59:4168 return result;
69}
70
71void ServiceProcessPrefs::SetInt(const std::string& key, int value) {
estade0bd407f2015-06-26 18:16:1872 prefs_->SetValue(key, make_scoped_ptr(new base::FundamentalValue(value)),
raymes76de1af2015-05-06 03:22:2173 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]05861d62012-10-26 19:59:4174}
75
[email protected]378678f2013-02-15 11:50:5776const base::DictionaryValue* ServiceProcessPrefs::GetDictionary(
[email protected]6d1db74e2012-09-23 03:51:4177 const std::string& key) const {
[email protected]378678f2013-02-15 11:50:5778 const base::Value* value;
[email protected]892f1d62012-11-08 18:24:3479 if (!prefs_->GetValue(key, &value) ||
[email protected]378678f2013-02-15 11:50:5780 !value->IsType(base::Value::TYPE_DICTIONARY)) {
[email protected]6d1db74e2012-09-23 03:51:4181 return NULL;
[email protected]f2d1f612010-12-09 15:10:1782 }
83
[email protected]378678f2013-02-15 11:50:5784 return static_cast<const base::DictionaryValue*>(value);
[email protected]6d1db74e2012-09-23 03:51:4185}
86
87const base::ListValue* ServiceProcessPrefs::GetList(
88 const std::string& key) const {
[email protected]378678f2013-02-15 11:50:5789 const base::Value* value;
90 if (!prefs_->GetValue(key, &value) || !value->IsType(base::Value::TYPE_LIST))
[email protected]892f1d62012-11-08 18:24:3491 return NULL;
[email protected]6d1db74e2012-09-23 03:51:4192
[email protected]378678f2013-02-15 11:50:5793 return static_cast<const base::ListValue*>(value);
[email protected]f2d1f612010-12-09 15:10:1794}
[email protected]bb073382011-05-06 23:22:2195
estade0bd407f2015-06-26 18:16:1896void ServiceProcessPrefs::SetValue(const std::string& key,
97 scoped_ptr<base::Value> value) {
98 prefs_->SetValue(key, value.Pass(),
99 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]e0832492012-10-08 22:55:55100}
101
[email protected]bb073382011-05-06 23:22:21102void ServiceProcessPrefs::RemovePref(const std::string& key) {
raymes76de1af2015-05-06 03:22:21103 prefs_->RemoveValue(key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]bb073382011-05-06 23:22:21104}
[email protected]6d1db74e2012-09-23 03:51:41105