blob: c3105c61cac647c79a8e1e3f74b97b2d5354eccf [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
Kelvin Zhang1c86a922021-05-13 10:30:48 -040020#include <functional>
Alex Deymod6f60072015-10-12 12:22:27 -070021#include <map>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070022#include <string>
Kelvin Zhangcf4600e2020-10-27 15:50:33 -040023#include <string_view>
Alex Deymod6f60072015-10-12 12:22:27 -070024#include <vector>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070025
Alex Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/files/file_path.h>
Alex Deymod6f60072015-10-12 12:22:27 -070027
Darin Petkov30030592010-07-27 13:53:20 -070028#include "gtest/gtest_prod.h" // for FRIEND_TEST
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/prefs_interface.h"
Darin Petkov30030592010-07-27 13:53:20 -070030
31namespace chromeos_update_engine {
32
Alex Deymoa0284ac2016-07-22 12:51:41 -070033// Implements a preference store by storing the value associated with a key
34// in a given storage passed during construction.
35class PrefsBase : public PrefsInterface {
Darin Petkov30030592010-07-27 13:53:20 -070036 public:
Alex Deymoa0284ac2016-07-22 12:51:41 -070037 // Storage interface used to set and retrieve keys.
38 class StorageInterface {
39 public:
40 StorageInterface() = default;
41 virtual ~StorageInterface() = default;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070042
Alex Deymoa0284ac2016-07-22 12:51:41 -070043 // Get the key named |key| and store its value in the referenced |value|.
44 // Returns whether the operation succeeded.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040045 virtual bool GetKey(std::string_view key, std::string* value) const = 0;
Alex Deymoa0284ac2016-07-22 12:51:41 -070046
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070047 // Get the keys stored within the namespace. If there are no keys in the
48 // namespace, |keys| will be empty. Returns whether the operation succeeded.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040049 virtual bool GetSubKeys(std::string_view ns,
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070050 std::vector<std::string>* keys) const = 0;
51
Alex Deymoa0284ac2016-07-22 12:51:41 -070052 // Set the value of the key named |key| to |value| regardless of the
53 // previous value. Returns whether the operation succeeded.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040054 virtual bool SetKey(std::string_view key, std::string_view value) = 0;
Alex Deymoa0284ac2016-07-22 12:51:41 -070055
56 // Returns whether the key named |key| exists.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040057 virtual bool KeyExists(std::string_view key) const = 0;
Alex Deymoa0284ac2016-07-22 12:51:41 -070058
59 // Deletes the value associated with the key name |key|. Returns whether the
60 // key was deleted.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040061 virtual bool DeleteKey(std::string_view key) = 0;
Alex Deymoa0284ac2016-07-22 12:51:41 -070062
63 private:
64 DISALLOW_COPY_AND_ASSIGN(StorageInterface);
65 };
66
67 explicit PrefsBase(StorageInterface* storage) : storage_(storage) {}
Darin Petkov30030592010-07-27 13:53:20 -070068
69 // PrefsInterface methods.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040070 bool GetString(std::string_view key, std::string* value) const override;
71 bool SetString(std::string_view key, std::string_view value) override;
72 bool GetInt64(std::string_view key, int64_t* value) const override;
73 bool SetInt64(std::string_view key, const int64_t value) override;
74 bool GetBoolean(std::string_view key, bool* value) const override;
75 bool SetBoolean(std::string_view key, const bool value) override;
Darin Petkov30030592010-07-27 13:53:20 -070076
Kelvin Zhang1c86a922021-05-13 10:30:48 -040077 bool Exists(std::string_view key) const override;
78 bool Delete(std::string_view key) override;
79 bool Delete(std::string_view pref_key,
Vyshu Khota4c5413d2020-11-04 16:17:25 -080080 const std::vector<std::string>& nss) override;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070081
Kelvin Zhang1c86a922021-05-13 10:30:48 -040082 bool GetSubKeys(std::string_view ns,
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070083 std::vector<std::string>* keys) const override;
84
Kelvin Zhang1c86a922021-05-13 10:30:48 -040085 void AddObserver(std::string_view key, ObserverInterface* observer) override;
86 void RemoveObserver(std::string_view key,
Alex Deymod6f60072015-10-12 12:22:27 -070087 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.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040091 std::map<std::string, std::vector<ObserverInterface*>, std::less<>>
92 observers_;
Alex Deymoa0284ac2016-07-22 12:51:41 -070093
94 // The concrete implementation of the storage used for the keys.
95 StorageInterface* storage_;
96
97 DISALLOW_COPY_AND_ASSIGN(PrefsBase);
98};
99
100// Implements a preference store by storing the value associated with
101// a key in a separate file named after the key under a preference
102// store directory.
103
104class Prefs : public PrefsBase {
105 public:
106 Prefs() : PrefsBase(&file_storage_) {}
107
108 // Initializes the store by associating this object with |prefs_dir|
109 // as the preference store directory. Returns true on success, false
110 // otherwise.
111 bool Init(const base::FilePath& prefs_dir);
112
113 private:
Darin Petkov30030592010-07-27 13:53:20 -0700114 FRIEND_TEST(PrefsTest, GetFileNameForKey);
115 FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
116 FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
117
Alex Deymoa0284ac2016-07-22 12:51:41 -0700118 class FileStorage : public PrefsBase::StorageInterface {
119 public:
120 FileStorage() = default;
Darin Petkov30030592010-07-27 13:53:20 -0700121
Alex Deymoa0284ac2016-07-22 12:51:41 -0700122 bool Init(const base::FilePath& prefs_dir);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700123
Alex Deymoa0284ac2016-07-22 12:51:41 -0700124 // PrefsBase::StorageInterface overrides.
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400125 bool GetKey(std::string_view key, std::string* value) const override;
126 bool GetSubKeys(std::string_view ns,
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700127 std::vector<std::string>* keys) const override;
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400128 bool SetKey(std::string_view key, std::string_view value) override;
129 bool KeyExists(std::string_view key) const override;
130 bool DeleteKey(std::string_view key) override;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700131
132 private:
133 FRIEND_TEST(PrefsTest, GetFileNameForKey);
134 FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
135 FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
136
137 // Sets |filename| to the full path to the file containing the data
138 // associated with |key|. Returns true on success, false otherwise.
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400139 bool GetFileNameForKey(std::string_view key,
Alex Deymoa0284ac2016-07-22 12:51:41 -0700140 base::FilePath* filename) const;
141
142 // Preference store directory.
143 base::FilePath prefs_dir_;
144 };
145
146 // The concrete file storage implementation.
147 FileStorage file_storage_;
Alex Deymod6f60072015-10-12 12:22:27 -0700148
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700149 DISALLOW_COPY_AND_ASSIGN(Prefs);
Darin Petkov30030592010-07-27 13:53:20 -0700150};
151
Alex Deymoa0284ac2016-07-22 12:51:41 -0700152// Implements a preference store in memory. The stored values are lost when the
153// object is destroyed.
154
155class MemoryPrefs : public PrefsBase {
156 public:
157 MemoryPrefs() : PrefsBase(&mem_storage_) {}
158
159 private:
160 class MemoryStorage : public PrefsBase::StorageInterface {
161 public:
162 MemoryStorage() = default;
163
164 // PrefsBase::StorageInterface overrides.
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400165 bool GetKey(std::string_view, std::string* value) const override;
166 bool GetSubKeys(std::string_view ns,
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700167 std::vector<std::string>* keys) const override;
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400168 bool SetKey(std::string_view key, std::string_view value) override;
169 bool KeyExists(std::string_view key) const override;
170 bool DeleteKey(std::string_view key) override;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700171
172 private:
173 // The std::map holding the values in memory.
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400174 std::map<std::string, std::string, std::less<>> values_;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700175 };
176
177 // The concrete memory storage implementation.
178 MemoryStorage mem_storage_;
179
180 DISALLOW_COPY_AND_ASSIGN(MemoryPrefs);
181};
Darin Petkov30030592010-07-27 13:53:20 -0700182} // namespace chromeos_update_engine
183
Alex Deymo39910dc2015-11-09 17:04:30 -0800184#endif // UPDATE_ENGINE_COMMON_PREFS_H_