blob: ceded3471c6119abd77757519d4b6768ad530f8b [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
dchenge73d8520c2015-12-27 01:19:097#include <utility>
8
dchengc0e39d572016-04-19 06:15:179#include "base/memory/ptr_util.h"
gabb15e19072016-05-11 20:45:4110#include "base/threading/thread_task_runner_handle.h"
[email protected]f2d1f612010-12-09 15:10:1711#include "base/values.h"
brettwb1fc1b82016-02-02 00:19:0812#include "components/prefs/pref_filter.h"
[email protected]f2d1f612010-12-09 15:10:1713
dchengc0e39d572016-04-19 06:15:1714ServiceProcessPrefs::ServiceProcessPrefs(const base::FilePath& pref_filename,
15 base::SequencedTaskRunner* task_runner)
[email protected]56cbcb3a2013-12-23 21:24:4616 : prefs_(new JsonPrefStore(pref_filename,
17 task_runner,
dchengc0e39d572016-04-19 06:15:1718 std::unique_ptr<PrefFilter>())) {}
[email protected]f2d1f612010-12-09 15:10:1719
[email protected]0a071a32011-02-08 00:18:2420ServiceProcessPrefs::~ServiceProcessPrefs() {}
21
[email protected]f2d1f612010-12-09 15:10:1722void ServiceProcessPrefs::ReadPrefs() {
[email protected]c3498a12011-02-02 04:14:4223 prefs_->ReadPrefs();
[email protected]f2d1f612010-12-09 15:10:1724}
25
26void ServiceProcessPrefs::WritePrefs() {
[email protected]fbe17c8a2011-12-27 16:41:4827 prefs_->CommitPendingWrite();
[email protected]f2d1f612010-12-09 15:10:1728}
29
[email protected]6d1db74e2012-09-23 03:51:4130std::string ServiceProcessPrefs::GetString(
31 const std::string& key,
32 const std::string& default_value) const {
[email protected]378678f2013-02-15 11:50:5733 const base::Value* value;
[email protected]6d1db74e2012-09-23 03:51:4134 std::string result;
[email protected]892f1d62012-11-08 18:24:3435 if (!prefs_->GetValue(key, &value) || !value->GetAsString(&result))
[email protected]6d1db74e2012-09-23 03:51:4136 return default_value;
[email protected]892f1d62012-11-08 18:24:3437
[email protected]6d1db74e2012-09-23 03:51:4138 return result;
[email protected]f2d1f612010-12-09 15:10:1739}
40
41void ServiceProcessPrefs::SetString(const std::string& key,
42 const std::string& value) {
dchengc0e39d572016-04-19 06:15:1743 prefs_->SetValue(key, base::WrapUnique(new base::StringValue(value)),
raymes76de1af2015-05-06 03:22:2144 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]f2d1f612010-12-09 15:10:1745}
46
[email protected]6d1db74e2012-09-23 03:51:4147bool ServiceProcessPrefs::GetBoolean(const std::string& key,
48 bool default_value) const {
[email protected]378678f2013-02-15 11:50:5749 const base::Value* value;
[email protected]6d1db74e2012-09-23 03:51:4150 bool result = false;
[email protected]892f1d62012-11-08 18:24:3451 if (!prefs_->GetValue(key, &value) || !value->GetAsBoolean(&result))
[email protected]6d1db74e2012-09-23 03:51:4152 return default_value;
[email protected]892f1d62012-11-08 18:24:3453
[email protected]6d1db74e2012-09-23 03:51:4154 return result;
[email protected]f2d1f612010-12-09 15:10:1755}
56
57void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) {
dchengc0e39d572016-04-19 06:15:1758 prefs_->SetValue(key, base::WrapUnique(new base::FundamentalValue(value)),
raymes76de1af2015-05-06 03:22:2159 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]f2d1f612010-12-09 15:10:1760}
61
[email protected]05861d62012-10-26 19:59:4162int ServiceProcessPrefs::GetInt(const std::string& key,
63 int default_value) const {
[email protected]378678f2013-02-15 11:50:5764 const base::Value* value;
[email protected]05861d62012-10-26 19:59:4165 int result = default_value;
[email protected]892f1d62012-11-08 18:24:3466 if (!prefs_->GetValue(key, &value) || !value->GetAsInteger(&result))
[email protected]05861d62012-10-26 19:59:4167 return default_value;
[email protected]892f1d62012-11-08 18:24:3468
[email protected]05861d62012-10-26 19:59:4169 return result;
70}
71
72void ServiceProcessPrefs::SetInt(const std::string& key, int value) {
dchengc0e39d572016-04-19 06:15:1773 prefs_->SetValue(key, base::WrapUnique(new base::FundamentalValue(value)),
raymes76de1af2015-05-06 03:22:2174 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]05861d62012-10-26 19:59:4175}
76
[email protected]378678f2013-02-15 11:50:5777const base::DictionaryValue* ServiceProcessPrefs::GetDictionary(
[email protected]6d1db74e2012-09-23 03:51:4178 const std::string& key) const {
[email protected]378678f2013-02-15 11:50:5779 const base::Value* value;
thestig892e6872016-04-21 03:34:0880 if (!prefs_->GetValue(key, &value))
81 return nullptr;
[email protected]f2d1f612010-12-09 15:10:1782
thestig892e6872016-04-21 03:34:0883 const base::DictionaryValue* dict_value = nullptr;
84 value->GetAsDictionary(&dict_value);
85 return dict_value;
[email protected]6d1db74e2012-09-23 03:51:4186}
87
88const base::ListValue* ServiceProcessPrefs::GetList(
89 const std::string& key) const {
[email protected]378678f2013-02-15 11:50:5790 const base::Value* value;
thestig892e6872016-04-21 03:34:0891 if (!prefs_->GetValue(key, &value))
92 return nullptr;
[email protected]6d1db74e2012-09-23 03:51:4193
thestig892e6872016-04-21 03:34:0894 const base::ListValue* list_value = nullptr;
95 value->GetAsList(&list_value);
96 return list_value;
[email protected]f2d1f612010-12-09 15:10:1797}
[email protected]bb073382011-05-06 23:22:2198
estade0bd407f2015-06-26 18:16:1899void ServiceProcessPrefs::SetValue(const std::string& key,
dchengc0e39d572016-04-19 06:15:17100 std::unique_ptr<base::Value> value) {
dchenge73d8520c2015-12-27 01:19:09101 prefs_->SetValue(key, std::move(value),
estade0bd407f2015-06-26 18:16:18102 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]e0832492012-10-08 22:55:55103}
104
[email protected]bb073382011-05-06 23:22:21105void ServiceProcessPrefs::RemovePref(const std::string& key) {
raymes76de1af2015-05-06 03:22:21106 prefs_->RemoveValue(key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
[email protected]bb073382011-05-06 23:22:21107}