blob: 674f7c69d7a12b813371cd8af461f913f689f819 [file] [log] [blame]
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoneb925cc82011-06-22 15:39:57 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_PROPERTY_STORE_H_
6#define SHILL_PROPERTY_STORE_H_
Chris Masoneb925cc82011-06-22 15:39:57 -07007
8#include <map>
9#include <string>
10#include <vector>
11
12#include <base/basictypes.h>
mukesh agrawalbebf1b82013-04-23 15:06:33 -070013#include <base/callback.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070014
15#include "shill/accessor_interface.h"
Chris Masonea8a2c252011-06-27 22:16:30 -070016#include "shill/property_iterator.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070017
18namespace shill {
19
20class Error;
21
22class PropertyStore {
23 public:
mukesh agrawalbebf1b82013-04-23 15:06:33 -070024 typedef base::Callback<void(const std::string &)> PropertyChangeCallback;
Chris Masone27c4aa52011-07-02 13:10:14 -070025 PropertyStore();
mukesh agrawalbebf1b82013-04-23 15:06:33 -070026 explicit PropertyStore(PropertyChangeCallback property_change_callback);
Chris Masoneb925cc82011-06-22 15:39:57 -070027 virtual ~PropertyStore();
28
mukesh agrawalde29fa82011-09-16 16:16:36 -070029 virtual bool Contains(const std::string& property) const;
Chris Masoneb925cc82011-06-22 15:39:57 -070030
Paul Stewarte6e8e492013-01-17 11:00:50 -080031 // Methods to allow the getting of properties stored in the referenced
32 // |store_| by name. Upon success, these methods return true and return the
33 // property value in |value|. Upon failure, they return false and
34 // leave |value| untouched.
35 bool GetBoolProperty(const std::string &name, bool *value,
36 Error *error) const;
Ben Chan7fab8972014-08-10 17:14:46 -070037 bool GetInt16Property(const std::string &name, int16_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -080038 Error *error) const;
Ben Chan7fab8972014-08-10 17:14:46 -070039 bool GetInt32Property(const std::string &name, int32_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -080040 Error *error) const;
41 bool GetKeyValueStoreProperty(const std::string &name, KeyValueStore *value,
42 Error *error) const;
43 bool GetStringProperty(const std::string &name, std::string *value,
44 Error *error) const;
45 bool GetStringmapProperty(const std::string &name, Stringmap *values,
46 Error *error) const;
47 bool GetStringmapsProperty(const std::string &name, Stringmaps *values,
48 Error *error) const;
49 bool GetStringsProperty(const std::string &name, Strings *values,
50 Error *error) const;
Ben Chan7fab8972014-08-10 17:14:46 -070051 bool GetUint8Property(const std::string &name, uint8_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -080052 Error *error) const;
Ben Chan7fab8972014-08-10 17:14:46 -070053 bool GetUint16Property(const std::string &name, uint16_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -080054 Error *error) const;
mukesh agrawale7c7e652013-06-18 17:19:39 -070055 bool GetUint16sProperty(const std::string &name, Uint16s *value,
56 Error *error) const;
Ben Chan7fab8972014-08-10 17:14:46 -070057 bool GetUint32Property(const std::string &name, uint32_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -080058 Error *error) const;
Ben Chan7fab8972014-08-10 17:14:46 -070059 bool GetUint64Property(const std::string &name, uint64_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -080060 Error *error) const;
61 bool GetRpcIdentifierProperty(const std::string &name, RpcIdentifier *value,
62 Error *error) const;
63
Chris Masoneb925cc82011-06-22 15:39:57 -070064 // Methods to allow the setting, by name, of properties stored in this object.
65 // The property names are declared in chromeos/dbus/service_constants.h,
66 // so that they may be shared with libcros.
mukesh agrawalbebf1b82013-04-23 15:06:33 -070067 // If the property is successfully changed, these methods return true,
68 // and leave |error| untouched.
69 // If the property is unchanged because it already has the desired value,
70 // these methods return false, and leave |error| untouched.
71 // If the property change fails, these methods return false, and update
72 // |error|. However, updating |error| is skipped if |error| is NULL.
mukesh agrawal66b0aca2012-01-30 15:28:28 -080073 virtual bool SetBoolProperty(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070074 bool value,
75 Error *error);
76
mukesh agrawal66b0aca2012-01-30 15:28:28 -080077 virtual bool SetInt16Property(const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -070078 int16_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -070079 Error *error);
80
mukesh agrawal66b0aca2012-01-30 15:28:28 -080081 virtual bool SetInt32Property(const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -070082 int32_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -070083 Error *error);
84
mukesh agrawal66b0aca2012-01-30 15:28:28 -080085 virtual bool SetStringProperty(const std::string &name,
86 const std::string &value,
Chris Masoneb925cc82011-06-22 15:39:57 -070087 Error *error);
88
89 virtual bool SetStringmapProperty(
mukesh agrawal66b0aca2012-01-30 15:28:28 -080090 const std::string &name,
91 const std::map<std::string, std::string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -070092 Error *error);
93
mukesh agrawalbebf1b82013-04-23 15:06:33 -070094 virtual bool SetStringmapsProperty(
95 const std::string &name,
96 const std::vector<std::map<std::string, std::string> > &values,
97 Error *error);
98
mukesh agrawal66b0aca2012-01-30 15:28:28 -080099 virtual bool SetStringsProperty(const std::string &name,
100 const std::vector<std::string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -0700101 Error *error);
102
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800103 virtual bool SetUint8Property(const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700104 uint8_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700105 Error *error);
106
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800107 virtual bool SetUint16Property(const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700108 uint16_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700109 Error *error);
110
mukesh agrawale7c7e652013-06-18 17:19:39 -0700111 virtual bool SetUint16sProperty(const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700112 const std::vector<uint16_t> &value,
mukesh agrawale7c7e652013-06-18 17:19:39 -0700113 Error *error);
114
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800115 virtual bool SetUint32Property(const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700116 uint32_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700117 Error *error);
118
Paul Stewarte18c33b2012-07-10 20:48:44 -0700119 virtual bool SetUint64Property(const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700120 uint64_t value,
Paul Stewarte18c33b2012-07-10 20:48:44 -0700121 Error *error);
122
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400123 virtual bool SetRpcIdentifierProperty(const std::string &name,
124 const RpcIdentifier &value,
125 Error *error);
126
mukesh agrawal4d260da2012-01-30 11:53:52 -0800127 // Clearing a property resets it to its "factory" value. This value
128 // is generally the value that it (the property) had when it was
129 // registered with PropertyStore.
130 //
131 // The exception to this rule is write-only derived properties. For
132 // such properties, the property owner explicitly provides a
133 // "factory" value at registration time. This is necessary because
134 // PropertyStore can't read the current value at registration time.
135 //
136 // |name| is the key used to access the property. If the property
137 // cannot be cleared, |error| is set, and the method returns false.
Alex Vakulenko8a532292014-06-16 17:18:44 -0700138 // Otherwise, |error| is unchanged, and the method returns true.
mukesh agrawal4d260da2012-01-30 11:53:52 -0800139 virtual bool ClearProperty(const std::string &name, Error *error);
140
mukesh agrawalffa3d042011-10-06 15:26:10 -0700141 // Accessors for iterators over property maps. Useful for dumping all
142 // properties.
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800143 ReadablePropertyConstIterator<bool> GetBoolPropertiesIter() const;
Ben Chan7fab8972014-08-10 17:14:46 -0700144 ReadablePropertyConstIterator<int16_t> GetInt16PropertiesIter() const;
145 ReadablePropertyConstIterator<int32_t> GetInt32PropertiesIter() const;
Alex Vakulenko8a532292014-06-16 17:18:44 -0700146 ReadablePropertyConstIterator<KeyValueStore>
147 GetKeyValueStorePropertiesIter() const;
148 ReadablePropertyConstIterator<RpcIdentifier>
149 GetRpcIdentifierPropertiesIter() const;
150 ReadablePropertyConstIterator<RpcIdentifiers>
151 GetRpcIdentifiersPropertiesIter() const;
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800152 ReadablePropertyConstIterator<std::string> GetStringPropertiesIter() const;
153 ReadablePropertyConstIterator<Stringmap> GetStringmapPropertiesIter() const;
154 ReadablePropertyConstIterator<Stringmaps> GetStringmapsPropertiesIter() const;
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800155 ReadablePropertyConstIterator<Strings> GetStringsPropertiesIter() const;
Ben Chan7fab8972014-08-10 17:14:46 -0700156 ReadablePropertyConstIterator<uint8_t> GetUint8PropertiesIter() const;
157 ReadablePropertyConstIterator<uint16_t> GetUint16PropertiesIter() const;
mukesh agrawale7c7e652013-06-18 17:19:39 -0700158 ReadablePropertyConstIterator<Uint16s> GetUint16sPropertiesIter() const;
Ben Chan7fab8972014-08-10 17:14:46 -0700159 ReadablePropertyConstIterator<uint32_t> GetUint32PropertiesIter() const;
160 ReadablePropertyConstIterator<uint64_t> GetUint64PropertiesIter() const;
Chris Masonea8a2c252011-06-27 22:16:30 -0700161
mukesh agrawal4d260da2012-01-30 11:53:52 -0800162 // Methods for registering a property.
163 //
164 // It is permitted to re-register a property (in which case the old
165 // binding is forgotten). However, the newly bound object must be of
166 // the same type.
167 //
168 // Note that types do not encode read-write permission. Hence, it
169 // is possible to change permissions by rebinding a property to the
170 // same object.
171 //
172 // (Corollary of the rebinding-to-same-type restriction: a
173 // PropertyStore cannot hold two properties of the same name, but
174 // differing types.)
Chris Masoneb925cc82011-06-22 15:39:57 -0700175 void RegisterBool(const std::string &name, bool *prop);
176 void RegisterConstBool(const std::string &name, const bool *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800177 void RegisterWriteOnlyBool(const std::string &name, bool *prop);
Ben Chan7fab8972014-08-10 17:14:46 -0700178 void RegisterInt16(const std::string &name, int16_t *prop);
179 void RegisterConstInt16(const std::string &name, const int16_t *prop);
180 void RegisterWriteOnlyInt16(const std::string &name, int16_t *prop);
181 void RegisterInt32(const std::string &name, int32_t *prop);
182 void RegisterConstInt32(const std::string &name, const int32_t *prop);
183 void RegisterWriteOnlyInt32(const std::string &name, int32_t *prop);
184 void RegisterUint32(const std::string &name, uint32_t *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700185 void RegisterString(const std::string &name, std::string *prop);
186 void RegisterConstString(const std::string &name, const std::string *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800187 void RegisterWriteOnlyString(const std::string &name, std::string *prop);
Chris Masone43b48a12011-07-01 13:37:07 -0700188 void RegisterStringmap(const std::string &name, Stringmap *prop);
189 void RegisterConstStringmap(const std::string &name, const Stringmap *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800190 void RegisterWriteOnlyStringmap(const std::string &name, Stringmap *prop);
Darin Petkovc0865312011-09-16 15:31:20 -0700191 void RegisterStringmaps(const std::string &name, Stringmaps *prop);
192 void RegisterConstStringmaps(const std::string &name, const Stringmaps *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800193 void RegisterWriteOnlyStringmaps(const std::string &name, Stringmaps *prop);
Chris Masone43b48a12011-07-01 13:37:07 -0700194 void RegisterStrings(const std::string &name, Strings *prop);
Chris Masone889666b2011-07-03 12:58:50 -0700195 void RegisterConstStrings(const std::string &name, const Strings *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800196 void RegisterWriteOnlyStrings(const std::string &name, Strings *prop);
Ben Chan7fab8972014-08-10 17:14:46 -0700197 void RegisterUint8(const std::string &name, uint8_t *prop);
198 void RegisterConstUint8(const std::string &name, const uint8_t *prop);
199 void RegisterWriteOnlyUint8(const std::string &name, uint8_t *prop);
200 void RegisterUint16(const std::string &name, uint16_t *prop);
mukesh agrawale7c7e652013-06-18 17:19:39 -0700201 void RegisterUint16s(const std::string &name, Uint16s *prop);
Ben Chan7fab8972014-08-10 17:14:46 -0700202 void RegisterConstUint16(const std::string &name, const uint16_t *prop);
mukesh agrawale7c7e652013-06-18 17:19:39 -0700203 void RegisterConstUint16s(const std::string &name, const Uint16s *prop);
Ben Chan7fab8972014-08-10 17:14:46 -0700204 void RegisterWriteOnlyUint16(const std::string &name, uint16_t *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700205
Chris Masone27c4aa52011-07-02 13:10:14 -0700206 void RegisterDerivedBool(const std::string &name,
207 const BoolAccessor &accessor);
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800208 void RegisterDerivedInt32(const std::string &name,
209 const Int32Accessor &accessor);
Darin Petkov63138a92012-02-06 14:09:15 +0100210 void RegisterDerivedKeyValueStore(const std::string &name,
211 const KeyValueStoreAccessor &accessor);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400212 void RegisterDerivedRpcIdentifier(const std::string &name,
213 const RpcIdentifierAccessor &acc);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700214 void RegisterDerivedRpcIdentifiers(const std::string &name,
215 const RpcIdentifiersAccessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700216 void RegisterDerivedString(const std::string &name,
217 const StringAccessor &accessor);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400218 void RegisterDerivedStringmap(const std::string &name,
219 const StringmapAccessor &accessor);
Chris Masone889666b2011-07-03 12:58:50 -0700220 void RegisterDerivedStringmaps(const std::string &name,
221 const StringmapsAccessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700222 void RegisterDerivedStrings(const std::string &name,
223 const StringsAccessor &accessor);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800224 void RegisterDerivedUint16(const std::string &name,
225 const Uint16Accessor &accessor);
Paul Stewarte18c33b2012-07-10 20:48:44 -0700226 void RegisterDerivedUint64(const std::string &name,
227 const Uint64Accessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700228
229 private:
mukesh agrawalffa3d042011-10-06 15:26:10 -0700230 template <class V>
Paul Stewarte6e8e492013-01-17 11:00:50 -0800231 bool GetProperty(
232 const std::string &name,
233 V *value,
234 Error *error,
Alex Vakulenko8a532292014-06-16 17:18:44 -0700235 const std::map< std::string, std::shared_ptr<
Paul Stewarte6e8e492013-01-17 11:00:50 -0800236 AccessorInterface<V> > > &collection,
237 const std::string &value_type_english) const;
238
239 template <class V>
mukesh agrawalffa3d042011-10-06 15:26:10 -0700240 bool SetProperty(
241 const std::string &name,
242 const V &value,
243 Error *error,
Alex Vakulenko8a532292014-06-16 17:18:44 -0700244 std::map< std::string, std::shared_ptr< AccessorInterface<V> > > *,
mukesh agrawalffa3d042011-10-06 15:26:10 -0700245 const std::string &value_type_english);
246
Chris Masoneb925cc82011-06-22 15:39:57 -0700247 // These are std::maps instead of something cooler because the common
248 // operation is iterating through them and returning all properties.
249 std::map<std::string, BoolAccessor> bool_properties_;
250 std::map<std::string, Int16Accessor> int16_properties_;
251 std::map<std::string, Int32Accessor> int32_properties_;
Darin Petkov63138a92012-02-06 14:09:15 +0100252 std::map<std::string, KeyValueStoreAccessor> key_value_store_properties_;
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400253 std::map<std::string, RpcIdentifierAccessor> rpc_identifier_properties_;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700254 std::map<std::string, RpcIdentifiersAccessor> rpc_identifiers_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700255 std::map<std::string, StringAccessor> string_properties_;
256 std::map<std::string, StringmapAccessor> stringmap_properties_;
Chris Masone889666b2011-07-03 12:58:50 -0700257 std::map<std::string, StringmapsAccessor> stringmaps_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700258 std::map<std::string, StringsAccessor> strings_properties_;
259 std::map<std::string, Uint8Accessor> uint8_properties_;
260 std::map<std::string, Uint16Accessor> uint16_properties_;
mukesh agrawale7c7e652013-06-18 17:19:39 -0700261 std::map<std::string, Uint16sAccessor> uint16s_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700262 std::map<std::string, Uint32Accessor> uint32_properties_;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700263 std::map<std::string, Uint64Accessor> uint64_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700264
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700265 PropertyChangeCallback property_changed_callback_;
266
Chris Masoneb925cc82011-06-22 15:39:57 -0700267 DISALLOW_COPY_AND_ASSIGN(PropertyStore);
268};
269
270} // namespace shill
271
Ben Chanc45688b2014-07-02 23:50:45 -0700272#endif // SHILL_PROPERTY_STORE_H_