blob: 3fc1d8915eec971e8b30301a9bb3ef97f0866910 [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>
Alex Deymod6f60072015-10-12 12:22:27 -070022#include <vector>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070023
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/files/file_path.h>
Alex Deymod6f60072015-10-12 12:22:27 -070025
Darin Petkov30030592010-07-27 13:53:20 -070026#include "gtest/gtest_prod.h" // for FRIEND_TEST
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/prefs_interface.h"
Darin Petkov30030592010-07-27 13:53:20 -070028
29namespace chromeos_update_engine {
30
Alex Deymoa0284ac2016-07-22 12:51:41 -070031// Implements a preference store by storing the value associated with a key
32// in a given storage passed during construction.
33class PrefsBase : public PrefsInterface {
Darin Petkov30030592010-07-27 13:53:20 -070034 public:
Alex Deymoa0284ac2016-07-22 12:51:41 -070035 // Storage interface used to set and retrieve keys.
36 class StorageInterface {
37 public:
38 StorageInterface() = default;
39 virtual ~StorageInterface() = default;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070040
Alex Deymoa0284ac2016-07-22 12:51:41 -070041 // Get the key named |key| and store its value in the referenced |value|.
42 // Returns whether the operation succeeded.
43 virtual bool GetKey(const std::string& key, std::string* value) const = 0;
44
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070045 // Get the keys stored within the namespace. If there are no keys in the
46 // namespace, |keys| will be empty. Returns whether the operation succeeded.
47 virtual bool GetSubKeys(const std::string& ns,
48 std::vector<std::string>* keys) const = 0;
49
Alex Deymoa0284ac2016-07-22 12:51:41 -070050 // Set the value of the key named |key| to |value| regardless of the
51 // previous value. Returns whether the operation succeeded.
52 virtual bool SetKey(const std::string& key, const std::string& value) = 0;
53
54 // Returns whether the key named |key| exists.
55 virtual bool KeyExists(const std::string& key) const = 0;
56
57 // Deletes the value associated with the key name |key|. Returns whether the
58 // key was deleted.
59 virtual bool DeleteKey(const std::string& key) = 0;
60
61 private:
62 DISALLOW_COPY_AND_ASSIGN(StorageInterface);
63 };
64
65 explicit PrefsBase(StorageInterface* storage) : storage_(storage) {}
Darin Petkov30030592010-07-27 13:53:20 -070066
67 // PrefsInterface methods.
Alex Deymod6f60072015-10-12 12:22:27 -070068 bool GetString(const std::string& key, std::string* value) const override;
Alex Deymo610277e2014-11-11 21:18:11 -080069 bool SetString(const std::string& key, const std::string& value) override;
Alex Deymod6f60072015-10-12 12:22:27 -070070 bool GetInt64(const std::string& key, int64_t* value) const override;
Alex Deymo610277e2014-11-11 21:18:11 -080071 bool SetInt64(const std::string& key, const int64_t value) override;
Alex Deymod6f60072015-10-12 12:22:27 -070072 bool GetBoolean(const std::string& key, bool* value) const override;
Alex Deymo610277e2014-11-11 21:18:11 -080073 bool SetBoolean(const std::string& key, const bool value) override;
Darin Petkov30030592010-07-27 13:53:20 -070074
Alex Deymod6f60072015-10-12 12:22:27 -070075 bool Exists(const std::string& key) const override;
Alex Deymo610277e2014-11-11 21:18:11 -080076 bool Delete(const std::string& key) override;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070077
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070078 bool GetSubKeys(const std::string& ns,
79 std::vector<std::string>* keys) const override;
80
Alex Deymod6f60072015-10-12 12:22:27 -070081 void AddObserver(const std::string& key,
82 ObserverInterface* observer) override;
83 void RemoveObserver(const std::string& key,
84 ObserverInterface* observer) override;
85
Darin Petkov30030592010-07-27 13:53:20 -070086 private:
Alex Deymoa0284ac2016-07-22 12:51:41 -070087 // The registered observers watching for changes.
88 std::map<std::string, std::vector<ObserverInterface*>> observers_;
89
90 // The concrete implementation of the storage used for the keys.
91 StorageInterface* storage_;
92
93 DISALLOW_COPY_AND_ASSIGN(PrefsBase);
94};
95
96// Implements a preference store by storing the value associated with
97// a key in a separate file named after the key under a preference
98// store directory.
99
100class Prefs : public PrefsBase {
101 public:
102 Prefs() : PrefsBase(&file_storage_) {}
103
104 // Initializes the store by associating this object with |prefs_dir|
105 // as the preference store directory. Returns true on success, false
106 // otherwise.
107 bool Init(const base::FilePath& prefs_dir);
108
109 private:
Darin Petkov30030592010-07-27 13:53:20 -0700110 FRIEND_TEST(PrefsTest, GetFileNameForKey);
111 FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
112 FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
113
Alex Deymoa0284ac2016-07-22 12:51:41 -0700114 class FileStorage : public PrefsBase::StorageInterface {
115 public:
116 FileStorage() = default;
Darin Petkov30030592010-07-27 13:53:20 -0700117
Alex Deymoa0284ac2016-07-22 12:51:41 -0700118 bool Init(const base::FilePath& prefs_dir);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700119
Alex Deymoa0284ac2016-07-22 12:51:41 -0700120 // PrefsBase::StorageInterface overrides.
121 bool GetKey(const std::string& key, std::string* value) const override;
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700122 bool GetSubKeys(const std::string& ns,
123 std::vector<std::string>* keys) const override;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700124 bool SetKey(const std::string& key, const std::string& value) override;
125 bool KeyExists(const std::string& key) const override;
126 bool DeleteKey(const std::string& key) override;
127
128 private:
129 FRIEND_TEST(PrefsTest, GetFileNameForKey);
130 FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
131 FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
132
133 // Sets |filename| to the full path to the file containing the data
134 // associated with |key|. Returns true on success, false otherwise.
135 bool GetFileNameForKey(const std::string& key,
136 base::FilePath* filename) const;
137
138 // Preference store directory.
139 base::FilePath prefs_dir_;
140 };
141
142 // The concrete file storage implementation.
143 FileStorage file_storage_;
Alex Deymod6f60072015-10-12 12:22:27 -0700144
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700145 DISALLOW_COPY_AND_ASSIGN(Prefs);
Darin Petkov30030592010-07-27 13:53:20 -0700146};
147
Alex Deymoa0284ac2016-07-22 12:51:41 -0700148// Implements a preference store in memory. The stored values are lost when the
149// object is destroyed.
150
151class MemoryPrefs : public PrefsBase {
152 public:
153 MemoryPrefs() : PrefsBase(&mem_storage_) {}
154
155 private:
156 class MemoryStorage : public PrefsBase::StorageInterface {
157 public:
158 MemoryStorage() = default;
159
160 // PrefsBase::StorageInterface overrides.
161 bool GetKey(const std::string& key, std::string* value) const override;
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700162 bool GetSubKeys(const std::string& ns,
163 std::vector<std::string>* keys) const override;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700164 bool SetKey(const std::string& key, const std::string& value) override;
165 bool KeyExists(const std::string& key) const override;
166 bool DeleteKey(const std::string& key) override;
167
168 private:
169 // The std::map holding the values in memory.
170 std::map<std::string, std::string> values_;
171 };
172
173 // The concrete memory storage implementation.
174 MemoryStorage mem_storage_;
175
176 DISALLOW_COPY_AND_ASSIGN(MemoryPrefs);
177};
Darin Petkov30030592010-07-27 13:53:20 -0700178} // namespace chromeos_update_engine
179
Alex Deymo39910dc2015-11-09 17:04:30 -0800180#endif // UPDATE_ENGINE_COMMON_PREFS_H_