blob: 12d06c0ce53346fd422882ab6d9fa615eb3a2917 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Petkov30030592010-07-27 13:53:20 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/prefs.h"
Darin Petkov30030592010-07-27 13:53:20 -070018
Alex Deymod6f60072015-10-12 12:22:27 -070019#include <algorithm>
20
Ben Chan06c76a42014-09-05 08:21:06 -070021#include <base/files/file_util.h>
Darin Petkov36275772010-10-01 11:40:57 -070022#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070023#include <base/strings/string_number_conversions.h>
24#include <base/strings/string_util.h>
Darin Petkov36275772010-10-01 11:40:57 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/utils.h"
Darin Petkov30030592010-07-27 13:53:20 -070027
28using std::string;
29
30namespace chromeos_update_engine {
31
Alex Deymoa0284ac2016-07-22 12:51:41 -070032bool PrefsBase::GetString(const string& key, string* value) const {
33 return storage_->GetKey(key, value);
Darin Petkov30030592010-07-27 13:53:20 -070034}
35
Alex Deymoa0284ac2016-07-22 12:51:41 -070036bool PrefsBase::SetString(const string& key, const string& value) {
37 TEST_AND_RETURN_FALSE(storage_->SetKey(key, value));
Alex Deymod6f60072015-10-12 12:22:27 -070038 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 Petkov30030592010-07-27 13:53:20 -070044 return true;
45}
46
Alex Deymoa0284ac2016-07-22 12:51:41 -070047bool PrefsBase::GetInt64(const string& key, int64_t* value) const {
Darin Petkov30030592010-07-27 13:53:20 -070048 string str_value;
Jay Srinivasan08fce042012-06-07 16:31:01 -070049 if (!GetString(key, &str_value))
50 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070051 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Chris Masone790e62e2010-08-12 10:41:18 -070052 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
Darin Petkov30030592010-07-27 13:53:20 -070053 return true;
54}
55
Alex Deymoa0284ac2016-07-22 12:51:41 -070056bool PrefsBase::SetInt64(const string& key, const int64_t value) {
Chris Masone790e62e2010-08-12 10:41:18 -070057 return SetString(key, base::Int64ToString(value));
Darin Petkov30030592010-07-27 13:53:20 -070058}
59
Alex Deymoa0284ac2016-07-22 12:51:41 -070060bool PrefsBase::GetBoolean(const string& key, bool* value) const {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070061 string str_value;
62 if (!GetString(key, &str_value))
63 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070064 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Alex Deymoefb7c4c2013-07-09 14:34:00 -070065 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 Deymoa0284ac2016-07-22 12:51:41 -070076bool PrefsBase::SetBoolean(const string& key, const bool value) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070077 return SetString(key, value ? "true" : "false");
78}
79
Alex Deymoa0284ac2016-07-22 12:51:41 -070080bool PrefsBase::Exists(const string& key) const {
81 return storage_->KeyExists(key);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070082}
83
Alex Deymoa0284ac2016-07-22 12:51:41 -070084bool PrefsBase::Delete(const string& key) {
85 TEST_AND_RETURN_FALSE(storage_->DeleteKey(key));
Alex Deymod6f60072015-10-12 12:22:27 -070086 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 Deymoa0284ac2016-07-22 12:51:41 -070095void PrefsBase::AddObserver(const string& key, ObserverInterface* observer) {
Alex Deymod6f60072015-10-12 12:22:27 -070096 observers_[key].push_back(observer);
97}
98
Alex Deymoa0284ac2016-07-22 12:51:41 -070099void PrefsBase::RemoveObserver(const string& key, ObserverInterface* observer) {
Alex Deymod6f60072015-10-12 12:22:27 -0700100 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 Srinivasan480ddfa2012-06-01 19:15:26 -0700105}
106
Alex Deymoa0284ac2016-07-22 12:51:41 -0700107// Prefs
108
109bool Prefs::Init(const base::FilePath& prefs_dir) {
110 return file_storage_.Init(prefs_dir);
111}
112
113bool Prefs::FileStorage::Init(const base::FilePath& prefs_dir) {
114 prefs_dir_ = prefs_dir;
115 return true;
116}
117
118bool 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
128bool 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
141bool 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
147bool 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
154bool Prefs::FileStorage::GetFileNameForKey(const string& key,
155 base::FilePath* filename) const {
Darin Petkov30030592010-07-27 13:53:20 -0700156 // 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 Vakulenko0103c362016-01-20 07:56:15 -0800160 TEST_AND_RETURN_FALSE(base::IsAsciiAlpha(c) || base::IsAsciiDigit(c) ||
Darin Petkov30030592010-07-27 13:53:20 -0700161 c == '_' || c == '-');
162 }
163 *filename = prefs_dir_.Append(key);
164 return true;
165}
166
Alex Deymoa0284ac2016-07-22 12:51:41 -0700167// MemoryPrefs
168
169bool 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
178bool MemoryPrefs::MemoryStorage::SetKey(const string& key,
179 const string& value) {
180 values_[key] = value;
181 return true;
182}
183
184bool MemoryPrefs::MemoryStorage::KeyExists(const string& key) const {
185 return values_.find(key) != values_.end();
186}
187
188bool 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 Petkov30030592010-07-27 13:53:20 -0700196} // namespace chromeos_update_engine