[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 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 | |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame] | 9 | #include "base/memory/ptr_util.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 10 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 11 | #include "base/values.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 12 | #include "components/prefs/pref_filter.h" |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 13 | |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame] | 14 | ServiceProcessPrefs::ServiceProcessPrefs(const base::FilePath& pref_filename, |
| 15 | base::SequencedTaskRunner* task_runner) |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 16 | : prefs_(new JsonPrefStore(pref_filename, |
| 17 | task_runner, |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame] | 18 | std::unique_ptr<PrefFilter>())) {} |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 19 | |
[email protected] | 0a071a3 | 2011-02-08 00:18:24 | [diff] [blame] | 20 | ServiceProcessPrefs::~ServiceProcessPrefs() {} |
| 21 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 22 | void ServiceProcessPrefs::ReadPrefs() { |
[email protected] | c3498a1 | 2011-02-02 04:14:42 | [diff] [blame] | 23 | prefs_->ReadPrefs(); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void ServiceProcessPrefs::WritePrefs() { |
[email protected] | fbe17c8a | 2011-12-27 16:41:48 | [diff] [blame] | 27 | prefs_->CommitPendingWrite(); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 28 | } |
| 29 | |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 30 | std::string ServiceProcessPrefs::GetString( |
| 31 | const std::string& key, |
| 32 | const std::string& default_value) const { |
[email protected] | 378678f | 2013-02-15 11:50:57 | [diff] [blame] | 33 | const base::Value* value; |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 34 | std::string result; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 35 | if (!prefs_->GetValue(key, &value) || !value->GetAsString(&result)) |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 36 | return default_value; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 37 | |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 38 | return result; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void ServiceProcessPrefs::SetString(const std::string& key, |
| 42 | const std::string& value) { |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame] | 43 | prefs_->SetValue(key, base::WrapUnique(new base::StringValue(value)), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 44 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 47 | bool ServiceProcessPrefs::GetBoolean(const std::string& key, |
| 48 | bool default_value) const { |
[email protected] | 378678f | 2013-02-15 11:50:57 | [diff] [blame] | 49 | const base::Value* value; |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 50 | bool result = false; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 51 | if (!prefs_->GetValue(key, &value) || !value->GetAsBoolean(&result)) |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 52 | return default_value; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 53 | |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 54 | return result; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) { |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame] | 58 | prefs_->SetValue(key, base::WrapUnique(new base::FundamentalValue(value)), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 59 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 05861d6 | 2012-10-26 19:59:41 | [diff] [blame] | 62 | int ServiceProcessPrefs::GetInt(const std::string& key, |
| 63 | int default_value) const { |
[email protected] | 378678f | 2013-02-15 11:50:57 | [diff] [blame] | 64 | const base::Value* value; |
[email protected] | 05861d6 | 2012-10-26 19:59:41 | [diff] [blame] | 65 | int result = default_value; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 66 | if (!prefs_->GetValue(key, &value) || !value->GetAsInteger(&result)) |
[email protected] | 05861d6 | 2012-10-26 19:59:41 | [diff] [blame] | 67 | return default_value; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 68 | |
[email protected] | 05861d6 | 2012-10-26 19:59:41 | [diff] [blame] | 69 | return result; |
| 70 | } |
| 71 | |
| 72 | void ServiceProcessPrefs::SetInt(const std::string& key, int value) { |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame] | 73 | prefs_->SetValue(key, base::WrapUnique(new base::FundamentalValue(value)), |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 74 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | 05861d6 | 2012-10-26 19:59:41 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 378678f | 2013-02-15 11:50:57 | [diff] [blame] | 77 | const base::DictionaryValue* ServiceProcessPrefs::GetDictionary( |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 78 | const std::string& key) const { |
[email protected] | 378678f | 2013-02-15 11:50:57 | [diff] [blame] | 79 | const base::Value* value; |
thestig | 892e687 | 2016-04-21 03:34:08 | [diff] [blame] | 80 | if (!prefs_->GetValue(key, &value)) |
| 81 | return nullptr; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 82 | |
thestig | 892e687 | 2016-04-21 03:34:08 | [diff] [blame] | 83 | const base::DictionaryValue* dict_value = nullptr; |
| 84 | value->GetAsDictionary(&dict_value); |
| 85 | return dict_value; |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | const base::ListValue* ServiceProcessPrefs::GetList( |
| 89 | const std::string& key) const { |
[email protected] | 378678f | 2013-02-15 11:50:57 | [diff] [blame] | 90 | const base::Value* value; |
thestig | 892e687 | 2016-04-21 03:34:08 | [diff] [blame] | 91 | if (!prefs_->GetValue(key, &value)) |
| 92 | return nullptr; |
[email protected] | 6d1db74e | 2012-09-23 03:51:41 | [diff] [blame] | 93 | |
thestig | 892e687 | 2016-04-21 03:34:08 | [diff] [blame] | 94 | const base::ListValue* list_value = nullptr; |
| 95 | value->GetAsList(&list_value); |
| 96 | return list_value; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 97 | } |
[email protected] | bb07338 | 2011-05-06 23:22:21 | [diff] [blame] | 98 | |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 99 | void ServiceProcessPrefs::SetValue(const std::string& key, |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame] | 100 | std::unique_ptr<base::Value> value) { |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 101 | prefs_->SetValue(key, std::move(value), |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 102 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | e083249 | 2012-10-08 22:55:55 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | bb07338 | 2011-05-06 23:22:21 | [diff] [blame] | 105 | void ServiceProcessPrefs::RemovePref(const std::string& key) { |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 106 | prefs_->RemoveValue(key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | bb07338 | 2011-05-06 23:22:21 | [diff] [blame] | 107 | } |