blob: 93477ddc90dff736c898d280779cddcd59622cba [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#ifndef UPDATE_ENGINE_COMMON_PREFS_H_
18#define UPDATE_ENGINE_COMMON_PREFS_H_
Darin Petkov30030592010-07-27 13:53:20 -070019
Alex Deymod6f60072015-10-12 12:22:27 -070020#include <map>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070021#include <string>
Kelvin Zhangcf4600e2020-10-27 15:50:33 -040022#include <string_view>
Alex Deymod6f60072015-10-12 12:22:27 -070023#include <vector>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070024
Alex Vakulenko75039d72014-03-25 12:36:28 -070025#include <base/files/file_path.h>
Alex Deymod6f60072015-10-12 12:22:27 -070026
Darin Petkov30030592010-07-27 13:53:20 -070027#include "gtest/gtest_prod.h" // for FRIEND_TEST
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/prefs_interface.h"
Darin Petkov30030592010-07-27 13:53:20 -070029
30namespace chromeos_update_engine {
31
Alex Deymoa0284ac2016-07-22 12:51:41 -070032// Implements a preference store by storing the value associated with a key
33// in a given storage passed during construction.
34class PrefsBase : public PrefsInterface {
Darin Petkov30030592010-07-27 13:53:20 -070035 public:
Alex Deymoa0284ac2016-07-22 12:51:41 -070036 // Storage interface used to set and retrieve keys.
37 class StorageInterface {
38 public:
39 StorageInterface() = default;
40 virtual ~StorageInterface() = default;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070041
Alex Deymoa0284ac2016-07-22 12:51:41 -070042 // Get the key named |key| and store its value in the referenced |value|.
43 // Returns whether the operation succeeded.
44 virtual bool GetKey(const std::string& key, std::string* value) const = 0;
45
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070046 // Get the keys stored within the namespace. If there are no keys in the
47 // namespace, |keys| will be empty. Returns whether the operation succeeded.
48 virtual bool GetSubKeys(const std::string& ns,
49 std::vector<std::string>* keys) const = 0;
50
Alex Deymoa0284ac2016-07-22 12:51:41 -070051 // Set the value of the key named |key| to |value| regardless of the
52 // previous value. Returns whether the operation succeeded.
Kelvin Zhangcf4600e2020-10-27 15:50:33 -040053 virtual bool SetKey(const std::string& key, std::string_view value) = 0;
Alex Deymoa0284ac2016-07-22 12:51:41 -070054
55 // Returns whether the key named |key| exists.
56 virtual bool KeyExists(const std::string& key) const = 0;
57
58 // Deletes the value associated with the key name |key|. Returns whether the
59 // key was deleted.
60 virtual bool DeleteKey(const std::string& key) = 0;
61
62 private:
63 DISALLOW_COPY_AND_ASSIGN(StorageInterface);
64 };
65
66 explicit PrefsBase(StorageInterface* storage) : storage_(storage) {}
Darin Petkov30030592010-07-27 13:53:20 -070067
68 // PrefsInterface methods.
Alex Deymod6f60072015-10-12 12:22:27 -070069 bool GetString(const std::string& key, std::string* value) const override;
Kelvin Zhangcf4600e2020-10-27 15:50:33 -040070 bool SetString(const std::string& key, std::string_view value) override;
Alex Deymod6f60072015-10-12 12:22:27 -070071 bool GetInt64(const std::string& key, int64_t* value) const override;
Alex Deymo610277e2014-11-11 21:18:11 -080072 bool SetInt64(const std::string& key, const int64_t value) override;
Alex Deymod6f60072015-10-12 12:22:27 -070073 bool GetBoolean(const std::string& key, bool* value) const override;
Alex Deymo610277e2014-11-11 21:18:11 -080074 bool SetBoolean(const std::string& key, const bool value) override;
Darin Petkov30030592010-07-27 13:53:20 -070075
Alex Deymod6f60072015-10-12 12:22:27 -070076 bool Exists(const std::string& key) const override;
Alex Deymo610277e2014-11-11 21:18:11 -080077 bool Delete(const std::string& key) override;
Vyshu Khota4c5413d2020-11-04 16:17:25 -080078 bool Delete(const std::string& pref_key,
79 const std::vector<std::string>& nss) override;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070080
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070081 bool GetSubKeys(const std::string& ns,
82 std::vector<std::string>* keys) const override;
83
Alex Deymod6f60072015-10-12 12:22:27 -070084 void AddObserver(const std::string& key,
85 ObserverInterface* observer) override;
86 void RemoveObserver(const std::string& key,
87 ObserverInterface* observer) override;
88
Darin Petkov30030592010-07-27 13:53:20 -070089 private:
Alex Deymoa0284ac2016-07-22 12:51:41 -070090 // The registered observers watching for changes.
91 std::map<std::string, std::vector<ObserverInterface*>> observers_;
92
93 // The concrete implementation of the storage used for the keys.
94 StorageInterface* storage_;
95
96 DISALLOW_COPY_AND_ASSIGN(PrefsBase);
97};
98
99// Implements a preference store by storing the value associated with
100// a key in a separate file named after the key under a preference
101// store directory.
102
103class Prefs : public PrefsBase {
104 public:
105 Prefs() : PrefsBase(&file_storage_) {}
106
107 // Initializes the store by associating this object with |prefs_dir|
108 // as the preference store directory. Returns true on success, false
109 // otherwise.
110 bool Init(const base::FilePath& prefs_dir);
111
112 private:
Darin Petkov30030592010-07-27 13:53:20 -0700113 FRIEND_TEST(PrefsTest, GetFileNameForKey);
114 FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
115 FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
116
Alex Deymoa0284ac2016-07-22 12:51:41 -0700117 class FileStorage : public PrefsBase::StorageInterface {
118 public:
119 FileStorage() = default;
Darin Petkov30030592010-07-27 13:53:20 -0700120
Alex Deymoa0284ac2016-07-22 12:51:41 -0700121 bool Init(const base::FilePath& prefs_dir);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700122
Alex Deymoa0284ac2016-07-22 12:51:41 -0700123 // PrefsBase::StorageInterface overrides.
124 bool GetKey(const std::string& key, std::string* value) const override;
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700125 bool GetSubKeys(const std::string& ns,
126 std::vector<std::string>* keys) const override;
Kelvin Zhangcf4600e2020-10-27 15:50:33 -0400127 bool SetKey(const std::string& key, std::string_view value) override;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700128 bool KeyExists(const std::string& key) const override;
129 bool DeleteKey(const std::string& key) override;
130
131 private:
132 FRIEND_TEST(PrefsTest, GetFileNameForKey);
133 FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
134 FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
135
136 // Sets |filename| to the full path to the file containing the data
137 // associated with |key|. Returns true on success, false otherwise.
138 bool GetFileNameForKey(const std::string& key,
139 base::FilePath* filename) const;
140
141 // Preference store directory.
142 base::FilePath prefs_dir_;
143 };
144
145 // The concrete file storage implementation.
146 FileStorage file_storage_;
Alex Deymod6f60072015-10-12 12:22:27 -0700147
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700148 DISALLOW_COPY_AND_ASSIGN(Prefs);
Darin Petkov30030592010-07-27 13:53:20 -0700149};
150
Alex Deymoa0284ac2016-07-22 12:51:41 -0700151// Implements a preference store in memory. The stored values are lost when the
152// object is destroyed.
153
154class MemoryPrefs : public PrefsBase {
155 public:
156 MemoryPrefs() : PrefsBase(&mem_storage_) {}
157
158 private:
159 class MemoryStorage : public PrefsBase::StorageInterface {
160 public:
161 MemoryStorage() = default;
162
163 // PrefsBase::StorageInterface overrides.
164 bool GetKey(const std::string& key, std::string* value) const override;
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700165 bool GetSubKeys(const std::string& ns,
166 std::vector<std::string>* keys) const override;
Kelvin Zhangcf4600e2020-10-27 15:50:33 -0400167 bool SetKey(const std::string& key, std::string_view value) override;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700168 bool KeyExists(const std::string& key) const override;
169 bool DeleteKey(const std::string& key) override;
170
171 private:
172 // The std::map holding the values in memory.
173 std::map<std::string, std::string> values_;
174 };
175
176 // The concrete memory storage implementation.
177 MemoryStorage mem_storage_;
178
179 DISALLOW_COPY_AND_ASSIGN(MemoryPrefs);
180};
Darin Petkov30030592010-07-27 13:53:20 -0700181} // namespace chromeos_update_engine
182
Alex Deymo39910dc2015-11-09 17:04:30 -0800183#endif // UPDATE_ENGINE_COMMON_PREFS_H_