Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/prefs.h" |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 18 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 21 | #include <base/files/file_util.h> |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 22 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 23 | #include <base/strings/string_number_conversions.h> |
| 24 | #include <base/strings/string_util.h> |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 25 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/utils.h" |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 27 | |
| 28 | using std::string; |
| 29 | |
| 30 | namespace chromeos_update_engine { |
| 31 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 32 | bool PrefsBase::GetString(const string& key, string* value) const { |
| 33 | return storage_->GetKey(key, value); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 36 | bool PrefsBase::SetString(const string& key, const string& value) { |
| 37 | TEST_AND_RETURN_FALSE(storage_->SetKey(key, value)); |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 38 | const auto observers_for_key = observers_.find(key); |
| 39 | if (observers_for_key != observers_.end()) { |
| 40 | std::vector<ObserverInterface*> copy_observers(observers_for_key->second); |
| 41 | for (ObserverInterface* observer : copy_observers) |
| 42 | observer->OnPrefSet(key); |
| 43 | } |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 47 | bool PrefsBase::GetInt64(const string& key, int64_t* value) const { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 48 | string str_value; |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 49 | if (!GetString(key, &str_value)) |
| 50 | return false; |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 51 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 52 | TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 56 | bool PrefsBase::SetInt64(const string& key, const int64_t value) { |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 57 | return SetString(key, base::Int64ToString(value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 60 | bool PrefsBase::GetBoolean(const string& key, bool* value) const { |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 61 | string str_value; |
| 62 | if (!GetString(key, &str_value)) |
| 63 | return false; |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 64 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 65 | if (str_value == "false") { |
| 66 | *value = false; |
| 67 | return true; |
| 68 | } |
| 69 | if (str_value == "true") { |
| 70 | *value = true; |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 76 | bool PrefsBase::SetBoolean(const string& key, const bool value) { |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 77 | return SetString(key, value ? "true" : "false"); |
| 78 | } |
| 79 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 80 | bool PrefsBase::Exists(const string& key) const { |
| 81 | return storage_->KeyExists(key); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 84 | bool PrefsBase::Delete(const string& key) { |
| 85 | TEST_AND_RETURN_FALSE(storage_->DeleteKey(key)); |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 86 | const auto observers_for_key = observers_.find(key); |
| 87 | if (observers_for_key != observers_.end()) { |
| 88 | std::vector<ObserverInterface*> copy_observers(observers_for_key->second); |
| 89 | for (ObserverInterface* observer : copy_observers) |
| 90 | observer->OnPrefDeleted(key); |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 95 | void PrefsBase::AddObserver(const string& key, ObserverInterface* observer) { |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 96 | observers_[key].push_back(observer); |
| 97 | } |
| 98 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 99 | void PrefsBase::RemoveObserver(const string& key, ObserverInterface* observer) { |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 100 | std::vector<ObserverInterface*>& observers_for_key = observers_[key]; |
| 101 | auto observer_it = |
| 102 | std::find(observers_for_key.begin(), observers_for_key.end(), observer); |
| 103 | if (observer_it != observers_for_key.end()) |
| 104 | observers_for_key.erase(observer_it); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 107 | // Prefs |
| 108 | |
| 109 | bool Prefs::Init(const base::FilePath& prefs_dir) { |
| 110 | return file_storage_.Init(prefs_dir); |
| 111 | } |
| 112 | |
| 113 | bool Prefs::FileStorage::Init(const base::FilePath& prefs_dir) { |
| 114 | prefs_dir_ = prefs_dir; |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool Prefs::FileStorage::GetKey(const string& key, string* value) const { |
| 119 | base::FilePath filename; |
| 120 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 121 | if (!base::ReadFileToString(filename, value)) { |
| 122 | LOG(INFO) << key << " not present in " << prefs_dir_.value(); |
| 123 | return false; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool Prefs::FileStorage::SetKey(const string& key, const string& value) { |
| 129 | base::FilePath filename; |
| 130 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 131 | if (!base::DirectoryExists(filename.DirName())) { |
| 132 | // Only attempt to create the directory if it doesn't exist to avoid calls |
| 133 | // to parent directories where we might not have permission to write to. |
| 134 | TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName())); |
| 135 | } |
| 136 | TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) == |
| 137 | static_cast<int>(value.size())); |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool Prefs::FileStorage::KeyExists(const string& key) const { |
| 142 | base::FilePath filename; |
| 143 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 144 | return base::PathExists(filename); |
| 145 | } |
| 146 | |
| 147 | bool Prefs::FileStorage::DeleteKey(const string& key) { |
| 148 | base::FilePath filename; |
| 149 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 150 | TEST_AND_RETURN_FALSE(base::DeleteFile(filename, false)); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool Prefs::FileStorage::GetFileNameForKey(const string& key, |
| 155 | base::FilePath* filename) const { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 156 | // Allows only non-empty keys containing [A-Za-z0-9_-]. |
| 157 | TEST_AND_RETURN_FALSE(!key.empty()); |
| 158 | for (size_t i = 0; i < key.size(); ++i) { |
| 159 | char c = key.at(i); |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 160 | TEST_AND_RETURN_FALSE(base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 161 | c == '_' || c == '-'); |
| 162 | } |
| 163 | *filename = prefs_dir_.Append(key); |
| 164 | return true; |
| 165 | } |
| 166 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 167 | // MemoryPrefs |
| 168 | |
| 169 | bool MemoryPrefs::MemoryStorage::GetKey(const string& key, |
| 170 | string* value) const { |
| 171 | auto it = values_.find(key); |
| 172 | if (it == values_.end()) |
| 173 | return false; |
| 174 | *value = it->second; |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | bool MemoryPrefs::MemoryStorage::SetKey(const string& key, |
| 179 | const string& value) { |
| 180 | values_[key] = value; |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | bool MemoryPrefs::MemoryStorage::KeyExists(const string& key) const { |
| 185 | return values_.find(key) != values_.end(); |
| 186 | } |
| 187 | |
| 188 | bool MemoryPrefs::MemoryStorage::DeleteKey(const string& key) { |
| 189 | auto it = values_.find(key); |
| 190 | if (it == values_.end()) |
| 191 | return false; |
| 192 | values_.erase(it); |
| 193 | return true; |
| 194 | } |
| 195 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 196 | } // namespace chromeos_update_engine |