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 <utility>
8
9 #include "base/threading/thread_task_runner_handle.h"
10 #include "base/values.h"
11 #include "components/prefs/pref_filter.h"
12
ServiceProcessPrefs(const base::FilePath & pref_filename,base::SequencedTaskRunner * task_runner)13 ServiceProcessPrefs::ServiceProcessPrefs(const base::FilePath& pref_filename,
14 base::SequencedTaskRunner* task_runner)
15 : prefs_(new JsonPrefStore(pref_filename,
16 std::unique_ptr<PrefFilter>(),
17 task_runner)) {}
18
~ServiceProcessPrefs()19 ServiceProcessPrefs::~ServiceProcessPrefs() {}
20
ReadPrefs()21 void ServiceProcessPrefs::ReadPrefs() {
22 prefs_->ReadPrefs();
23 }
24
WritePrefs()25 void ServiceProcessPrefs::WritePrefs() {
26 prefs_->CommitPendingWrite(base::OnceClosure());
27 }
28
GetString(const std::string & key,const std::string & default_value) const29 std::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
SetString(const std::string & key,const std::string & value)40 void ServiceProcessPrefs::SetString(const std::string& key,
41 const std::string& value) {
42 prefs_->SetValue(key, std::make_unique<base::Value>(value),
43 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
44 }
45
GetBoolean(const std::string & key,bool default_value) const46 bool ServiceProcessPrefs::GetBoolean(const std::string& key,
47 bool default_value) const {
48 const base::Value* value;
49 bool result = false;
50 if (!prefs_->GetValue(key, &value) || !value->GetAsBoolean(&result))
51 return default_value;
52
53 return result;
54 }
55
SetBoolean(const std::string & key,bool value)56 void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) {
57 prefs_->SetValue(key, std::make_unique<base::Value>(value),
58 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
59 }
60
GetInt(const std::string & key,int default_value) const61 int ServiceProcessPrefs::GetInt(const std::string& key,
62 int default_value) const {
63 const base::Value* value;
64 int result = default_value;
65 if (!prefs_->GetValue(key, &value) || !value->GetAsInteger(&result))
66 return default_value;
67
68 return result;
69 }
70
SetInt(const std::string & key,int value)71 void ServiceProcessPrefs::SetInt(const std::string& key, int value) {
72 prefs_->SetValue(key, std::make_unique<base::Value>(value),
73 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
74 }
75
GetDictionary(const std::string & key) const76 const base::DictionaryValue* ServiceProcessPrefs::GetDictionary(
77 const std::string& key) const {
78 const base::Value* value;
79 if (!prefs_->GetValue(key, &value))
80 return nullptr;
81
82 const base::DictionaryValue* dict_value = nullptr;
83 value->GetAsDictionary(&dict_value);
84 return dict_value;
85 }
86
GetList(const std::string & key) const87 const base::ListValue* ServiceProcessPrefs::GetList(
88 const std::string& key) const {
89 const base::Value* value;
90 if (!prefs_->GetValue(key, &value))
91 return nullptr;
92
93 const base::ListValue* list_value = nullptr;
94 value->GetAsList(&list_value);
95 return list_value;
96 }
97
SetValue(const std::string & key,std::unique_ptr<base::Value> value)98 void ServiceProcessPrefs::SetValue(const std::string& key,
99 std::unique_ptr<base::Value> value) {
100 prefs_->SetValue(key, std::move(value),
101 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
102 }
103
RemovePref(const std::string & key)104 void ServiceProcessPrefs::RemovePref(const std::string& key) {
105 prefs_->RemoveValue(key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
106 }
107