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