| 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 Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 32 | bool Prefs::Init(const base::FilePath& prefs_dir) { | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 33 | prefs_dir_ = prefs_dir; | 
|  | 34 | return true; | 
|  | 35 | } | 
|  | 36 |  | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 37 | bool Prefs::GetString(const string& key, string* value) const { | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 38 | base::FilePath filename; | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 39 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 40 | if (!base::ReadFileToString(filename, value)) { | 
| Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 41 | LOG(INFO) << key << " not present in " << prefs_dir_.value(); | 
| Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 42 | return false; | 
|  | 43 | } | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 44 | return true; | 
|  | 45 | } | 
|  | 46 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 47 | bool Prefs::SetString(const string& key, const string& value) { | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 48 | base::FilePath filename; | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 49 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); | 
| Alex Deymo | 7454265 | 2015-10-15 16:23:44 -0700 | [diff] [blame] | 50 | if (!base::DirectoryExists(filename.DirName())) { | 
|  | 51 | // Only attempt to create the directory if it doesn't exist to avoid calls | 
|  | 52 | // to parent directories where we might not have permission to write to. | 
|  | 53 | TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName())); | 
|  | 54 | } | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 55 | TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) == | 
|  | 56 | static_cast<int>(value.size())); | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 57 | const auto observers_for_key = observers_.find(key); | 
|  | 58 | if (observers_for_key != observers_.end()) { | 
|  | 59 | std::vector<ObserverInterface*> copy_observers(observers_for_key->second); | 
|  | 60 | for (ObserverInterface* observer : copy_observers) | 
|  | 61 | observer->OnPrefSet(key); | 
|  | 62 | } | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 63 | return true; | 
|  | 64 | } | 
|  | 65 |  | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 66 | bool Prefs::GetInt64(const string& key, int64_t* value) const { | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 67 | string str_value; | 
| Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 68 | if (!GetString(key, &str_value)) | 
|  | 69 | return false; | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 70 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value); | 
| Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 71 | TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value)); | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 72 | return true; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | bool Prefs::SetInt64(const string& key, const int64_t value) { | 
| Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 76 | return SetString(key, base::Int64ToString(value)); | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 79 | bool Prefs::GetBoolean(const string& key, bool* value) const { | 
| Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 80 | string str_value; | 
|  | 81 | if (!GetString(key, &str_value)) | 
|  | 82 | return false; | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 83 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value); | 
| Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 84 | if (str_value == "false") { | 
|  | 85 | *value = false; | 
|  | 86 | return true; | 
|  | 87 | } | 
|  | 88 | if (str_value == "true") { | 
|  | 89 | *value = true; | 
|  | 90 | return true; | 
|  | 91 | } | 
|  | 92 | return false; | 
|  | 93 | } | 
|  | 94 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 95 | bool Prefs::SetBoolean(const string& key, const bool value) { | 
| Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 96 | return SetString(key, value ? "true" : "false"); | 
|  | 97 | } | 
|  | 98 |  | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 99 | bool Prefs::Exists(const string& key) const { | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 100 | base::FilePath filename; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 101 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 102 | return base::PathExists(filename); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | bool Prefs::Delete(const string& key) { | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 106 | base::FilePath filename; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 107 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 108 | TEST_AND_RETURN_FALSE(base::DeleteFile(filename, false)); | 
|  | 109 | const auto observers_for_key = observers_.find(key); | 
|  | 110 | if (observers_for_key != observers_.end()) { | 
|  | 111 | std::vector<ObserverInterface*> copy_observers(observers_for_key->second); | 
|  | 112 | for (ObserverInterface* observer : copy_observers) | 
|  | 113 | observer->OnPrefDeleted(key); | 
|  | 114 | } | 
|  | 115 | return true; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | void Prefs::AddObserver(const string& key, ObserverInterface* observer) { | 
|  | 119 | observers_[key].push_back(observer); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | void Prefs::RemoveObserver(const string& key, ObserverInterface* observer) { | 
|  | 123 | std::vector<ObserverInterface*>& observers_for_key = observers_[key]; | 
|  | 124 | auto observer_it = | 
|  | 125 | std::find(observers_for_key.begin(), observers_for_key.end(), observer); | 
|  | 126 | if (observer_it != observers_for_key.end()) | 
|  | 127 | observers_for_key.erase(observer_it); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 130 | bool Prefs::GetFileNameForKey(const string& key, | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 131 | base::FilePath* filename) const { | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 132 | // Allows only non-empty keys containing [A-Za-z0-9_-]. | 
|  | 133 | TEST_AND_RETURN_FALSE(!key.empty()); | 
|  | 134 | for (size_t i = 0; i < key.size(); ++i) { | 
|  | 135 | char c = key.at(i); | 
| Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 136 | TEST_AND_RETURN_FALSE(base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) || | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 137 | c == '_' || c == '-'); | 
|  | 138 | } | 
|  | 139 | *filename = prefs_dir_.Append(key); | 
|  | 140 | return true; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | }  // namespace chromeos_update_engine |