blob: 9d3a30f94a7d817f6a3645af0f707a95ac3a646e [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 Vakulenko75039d72014-03-25 12:36:28 -070032bool Prefs::Init(const base::FilePath& prefs_dir) {
Darin Petkov30030592010-07-27 13:53:20 -070033 prefs_dir_ = prefs_dir;
34 return true;
35}
36
Alex Deymod6f60072015-10-12 12:22:27 -070037bool Prefs::GetString(const string& key, string* value) const {
Alex Vakulenko75039d72014-03-25 12:36:28 -070038 base::FilePath filename;
Darin Petkov30030592010-07-27 13:53:20 -070039 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070040 if (!base::ReadFileToString(filename, value)) {
Jay Srinivasan08fce042012-06-07 16:31:01 -070041 LOG(INFO) << key << " not present in " << prefs_dir_.value();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070042 return false;
43 }
Darin Petkov30030592010-07-27 13:53:20 -070044 return true;
45}
46
Alex Deymof329b932014-10-30 01:37:48 -070047bool Prefs::SetString(const string& key, const string& value) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070048 base::FilePath filename;
Darin Petkov30030592010-07-27 13:53:20 -070049 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Deymo74542652015-10-15 16:23:44 -070050 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 Chan736fcb52014-05-21 18:28:22 -070055 TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) ==
56 static_cast<int>(value.size()));
Alex Deymod6f60072015-10-12 12:22:27 -070057 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 Petkov30030592010-07-27 13:53:20 -070063 return true;
64}
65
Alex Deymod6f60072015-10-12 12:22:27 -070066bool Prefs::GetInt64(const string& key, int64_t* value) const {
Darin Petkov30030592010-07-27 13:53:20 -070067 string str_value;
Jay Srinivasan08fce042012-06-07 16:31:01 -070068 if (!GetString(key, &str_value))
69 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070070 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Chris Masone790e62e2010-08-12 10:41:18 -070071 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
Darin Petkov30030592010-07-27 13:53:20 -070072 return true;
73}
74
75bool Prefs::SetInt64(const string& key, const int64_t value) {
Chris Masone790e62e2010-08-12 10:41:18 -070076 return SetString(key, base::Int64ToString(value));
Darin Petkov30030592010-07-27 13:53:20 -070077}
78
Alex Deymod6f60072015-10-12 12:22:27 -070079bool Prefs::GetBoolean(const string& key, bool* value) const {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070080 string str_value;
81 if (!GetString(key, &str_value))
82 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070083 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Alex Deymoefb7c4c2013-07-09 14:34:00 -070084 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 Deymof329b932014-10-30 01:37:48 -070095bool Prefs::SetBoolean(const string& key, const bool value) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070096 return SetString(key, value ? "true" : "false");
97}
98
Alex Deymod6f60072015-10-12 12:22:27 -070099bool Prefs::Exists(const string& key) const {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700100 base::FilePath filename;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700101 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700102 return base::PathExists(filename);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700103}
104
105bool Prefs::Delete(const string& key) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700106 base::FilePath filename;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700107 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Deymod6f60072015-10-12 12:22:27 -0700108 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
118void Prefs::AddObserver(const string& key, ObserverInterface* observer) {
119 observers_[key].push_back(observer);
120}
121
122void 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 Srinivasan480ddfa2012-06-01 19:15:26 -0700128}
129
Alex Deymof329b932014-10-30 01:37:48 -0700130bool Prefs::GetFileNameForKey(const string& key,
Alex Deymod6f60072015-10-12 12:22:27 -0700131 base::FilePath* filename) const {
Darin Petkov30030592010-07-27 13:53:20 -0700132 // 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);
136 TEST_AND_RETURN_FALSE(IsAsciiAlpha(c) || IsAsciiDigit(c) ||
137 c == '_' || c == '-');
138 }
139 *filename = prefs_dir_.Append(key);
140 return true;
141}
142
143} // namespace chromeos_update_engine