blob: 603c04093138f6b36a2f11cc810899c6bfa70a10 [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
5#ifndef SHILL_PROPERTY_STORE_
6#define SHILL_PROPERTY_STORE_
7
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;
37 bool GetInt16Property(const std::string &name, int16 *value,
38 Error *error) const;
39 bool GetInt32Property(const std::string &name, int32 *value,
40 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;
51 bool GetUint8Property(const std::string &name, uint8 *value,
52 Error *error) const;
53 bool GetUint16Property(const std::string &name, uint16 *value,
54 Error *error) const;
55 bool GetUint32Property(const std::string &name, uint32 *value,
56 Error *error) const;
57 bool GetUint64Property(const std::string &name, uint64 *value,
58 Error *error) const;
59 bool GetRpcIdentifierProperty(const std::string &name, RpcIdentifier *value,
60 Error *error) const;
61
Chris Masoneb925cc82011-06-22 15:39:57 -070062 // Methods to allow the setting, by name, of properties stored in this object.
63 // The property names are declared in chromeos/dbus/service_constants.h,
64 // so that they may be shared with libcros.
mukesh agrawalbebf1b82013-04-23 15:06:33 -070065 // If the property is successfully changed, these methods return true,
66 // and leave |error| untouched.
67 // If the property is unchanged because it already has the desired value,
68 // these methods return false, and leave |error| untouched.
69 // If the property change fails, these methods return false, and update
70 // |error|. However, updating |error| is skipped if |error| is NULL.
mukesh agrawal66b0aca2012-01-30 15:28:28 -080071 virtual bool SetBoolProperty(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070072 bool value,
73 Error *error);
74
mukesh agrawal66b0aca2012-01-30 15:28:28 -080075 virtual bool SetInt16Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070076 int16 value,
77 Error *error);
78
mukesh agrawal66b0aca2012-01-30 15:28:28 -080079 virtual bool SetInt32Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070080 int32 value,
81 Error *error);
82
mukesh agrawal66b0aca2012-01-30 15:28:28 -080083 virtual bool SetStringProperty(const std::string &name,
84 const std::string &value,
Chris Masoneb925cc82011-06-22 15:39:57 -070085 Error *error);
86
87 virtual bool SetStringmapProperty(
mukesh agrawal66b0aca2012-01-30 15:28:28 -080088 const std::string &name,
89 const std::map<std::string, std::string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -070090 Error *error);
91
mukesh agrawalbebf1b82013-04-23 15:06:33 -070092 virtual bool SetStringmapsProperty(
93 const std::string &name,
94 const std::vector<std::map<std::string, std::string> > &values,
95 Error *error);
96
mukesh agrawal66b0aca2012-01-30 15:28:28 -080097 virtual bool SetStringsProperty(const std::string &name,
98 const std::vector<std::string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -070099 Error *error);
100
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800101 virtual bool SetUint8Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700102 uint8 value,
103 Error *error);
104
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800105 virtual bool SetUint16Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700106 uint16 value,
107 Error *error);
108
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800109 virtual bool SetUint32Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700110 uint32 value,
111 Error *error);
112
Paul Stewarte18c33b2012-07-10 20:48:44 -0700113 virtual bool SetUint64Property(const std::string &name,
114 uint64 value,
115 Error *error);
116
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400117 virtual bool SetRpcIdentifierProperty(const std::string &name,
118 const RpcIdentifier &value,
119 Error *error);
120
mukesh agrawal4d260da2012-01-30 11:53:52 -0800121 // Clearing a property resets it to its "factory" value. This value
122 // is generally the value that it (the property) had when it was
123 // registered with PropertyStore.
124 //
125 // The exception to this rule is write-only derived properties. For
126 // such properties, the property owner explicitly provides a
127 // "factory" value at registration time. This is necessary because
128 // PropertyStore can't read the current value at registration time.
129 //
130 // |name| is the key used to access the property. If the property
131 // cannot be cleared, |error| is set, and the method returns false.
132 // Otherwrise, |error| is unchanged, and the method returns true.
133 virtual bool ClearProperty(const std::string &name, Error *error);
134
mukesh agrawalffa3d042011-10-06 15:26:10 -0700135 // Accessors for iterators over property maps. Useful for dumping all
136 // properties.
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800137 ReadablePropertyConstIterator<bool> GetBoolPropertiesIter() const;
138 ReadablePropertyConstIterator<int16> GetInt16PropertiesIter() const;
139 ReadablePropertyConstIterator<int32> GetInt32PropertiesIter() const;
Darin Petkov63138a92012-02-06 14:09:15 +0100140 ReadablePropertyConstIterator<KeyValueStore> GetKeyValueStorePropertiesIter(
141 ) const;
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400142 ReadablePropertyConstIterator<RpcIdentifier> GetRpcIdentifierPropertiesIter(
143 ) const;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700144 ReadablePropertyConstIterator<RpcIdentifiers> GetRpcIdentifiersPropertiesIter(
145 ) const;
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800146 ReadablePropertyConstIterator<std::string> GetStringPropertiesIter() const;
147 ReadablePropertyConstIterator<Stringmap> GetStringmapPropertiesIter() const;
148 ReadablePropertyConstIterator<Stringmaps> GetStringmapsPropertiesIter() const;
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800149 ReadablePropertyConstIterator<Strings> GetStringsPropertiesIter() const;
150 ReadablePropertyConstIterator<uint8> GetUint8PropertiesIter() const;
151 ReadablePropertyConstIterator<uint16> GetUint16PropertiesIter() const;
152 ReadablePropertyConstIterator<uint32> GetUint32PropertiesIter() const;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700153 ReadablePropertyConstIterator<uint64> GetUint64PropertiesIter() const;
Chris Masonea8a2c252011-06-27 22:16:30 -0700154
mukesh agrawal4d260da2012-01-30 11:53:52 -0800155 // Methods for registering a property.
156 //
157 // It is permitted to re-register a property (in which case the old
158 // binding is forgotten). However, the newly bound object must be of
159 // the same type.
160 //
161 // Note that types do not encode read-write permission. Hence, it
162 // is possible to change permissions by rebinding a property to the
163 // same object.
164 //
165 // (Corollary of the rebinding-to-same-type restriction: a
166 // PropertyStore cannot hold two properties of the same name, but
167 // differing types.)
Chris Masoneb925cc82011-06-22 15:39:57 -0700168 void RegisterBool(const std::string &name, bool *prop);
169 void RegisterConstBool(const std::string &name, const bool *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800170 void RegisterWriteOnlyBool(const std::string &name, bool *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700171 void RegisterInt16(const std::string &name, int16 *prop);
172 void RegisterConstInt16(const std::string &name, const int16 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800173 void RegisterWriteOnlyInt16(const std::string &name, int16 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700174 void RegisterInt32(const std::string &name, int32 *prop);
175 void RegisterConstInt32(const std::string &name, const int32 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800176 void RegisterWriteOnlyInt32(const std::string &name, int32 *prop);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800177 void RegisterUint32(const std::string &name, uint32 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700178 void RegisterString(const std::string &name, std::string *prop);
179 void RegisterConstString(const std::string &name, const std::string *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800180 void RegisterWriteOnlyString(const std::string &name, std::string *prop);
Chris Masone43b48a12011-07-01 13:37:07 -0700181 void RegisterStringmap(const std::string &name, Stringmap *prop);
182 void RegisterConstStringmap(const std::string &name, const Stringmap *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800183 void RegisterWriteOnlyStringmap(const std::string &name, Stringmap *prop);
Darin Petkovc0865312011-09-16 15:31:20 -0700184 void RegisterStringmaps(const std::string &name, Stringmaps *prop);
185 void RegisterConstStringmaps(const std::string &name, const Stringmaps *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800186 void RegisterWriteOnlyStringmaps(const std::string &name, Stringmaps *prop);
Chris Masone43b48a12011-07-01 13:37:07 -0700187 void RegisterStrings(const std::string &name, Strings *prop);
Chris Masone889666b2011-07-03 12:58:50 -0700188 void RegisterConstStrings(const std::string &name, const Strings *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800189 void RegisterWriteOnlyStrings(const std::string &name, Strings *prop);
Chris Masone889666b2011-07-03 12:58:50 -0700190 void RegisterUint8(const std::string &name, uint8 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700191 void RegisterConstUint8(const std::string &name, const uint8 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800192 void RegisterWriteOnlyUint8(const std::string &name, uint8 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700193 void RegisterUint16(const std::string &name, uint16 *prop);
194 void RegisterConstUint16(const std::string &name, const uint16 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800195 void RegisterWriteOnlyUint16(const std::string &name, uint16 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700196
Chris Masone27c4aa52011-07-02 13:10:14 -0700197 void RegisterDerivedBool(const std::string &name,
198 const BoolAccessor &accessor);
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800199 void RegisterDerivedInt32(const std::string &name,
200 const Int32Accessor &accessor);
Darin Petkov63138a92012-02-06 14:09:15 +0100201 void RegisterDerivedKeyValueStore(const std::string &name,
202 const KeyValueStoreAccessor &accessor);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400203 void RegisterDerivedRpcIdentifier(const std::string &name,
204 const RpcIdentifierAccessor &acc);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700205 void RegisterDerivedRpcIdentifiers(const std::string &name,
206 const RpcIdentifiersAccessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700207 void RegisterDerivedString(const std::string &name,
208 const StringAccessor &accessor);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400209 void RegisterDerivedStringmap(const std::string &name,
210 const StringmapAccessor &accessor);
Chris Masone889666b2011-07-03 12:58:50 -0700211 void RegisterDerivedStringmaps(const std::string &name,
212 const StringmapsAccessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700213 void RegisterDerivedStrings(const std::string &name,
214 const StringsAccessor &accessor);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800215 void RegisterDerivedUint16(const std::string &name,
216 const Uint16Accessor &accessor);
Paul Stewarte18c33b2012-07-10 20:48:44 -0700217 void RegisterDerivedUint64(const std::string &name,
218 const Uint64Accessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700219
220 private:
mukesh agrawalffa3d042011-10-06 15:26:10 -0700221 template <class V>
Paul Stewarte6e8e492013-01-17 11:00:50 -0800222 bool GetProperty(
223 const std::string &name,
224 V *value,
225 Error *error,
226 const std::map< std::string, std::tr1::shared_ptr<
227 AccessorInterface<V> > > &collection,
228 const std::string &value_type_english) const;
229
230 template <class V>
mukesh agrawalffa3d042011-10-06 15:26:10 -0700231 bool SetProperty(
232 const std::string &name,
233 const V &value,
234 Error *error,
235 std::map< std::string, std::tr1::shared_ptr< AccessorInterface<V> > > &,
236 const std::string &value_type_english);
237
Chris Masoneb925cc82011-06-22 15:39:57 -0700238 // These are std::maps instead of something cooler because the common
239 // operation is iterating through them and returning all properties.
240 std::map<std::string, BoolAccessor> bool_properties_;
241 std::map<std::string, Int16Accessor> int16_properties_;
242 std::map<std::string, Int32Accessor> int32_properties_;
Darin Petkov63138a92012-02-06 14:09:15 +0100243 std::map<std::string, KeyValueStoreAccessor> key_value_store_properties_;
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400244 std::map<std::string, RpcIdentifierAccessor> rpc_identifier_properties_;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700245 std::map<std::string, RpcIdentifiersAccessor> rpc_identifiers_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700246 std::map<std::string, StringAccessor> string_properties_;
247 std::map<std::string, StringmapAccessor> stringmap_properties_;
Chris Masone889666b2011-07-03 12:58:50 -0700248 std::map<std::string, StringmapsAccessor> stringmaps_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700249 std::map<std::string, StringsAccessor> strings_properties_;
250 std::map<std::string, Uint8Accessor> uint8_properties_;
251 std::map<std::string, Uint16Accessor> uint16_properties_;
252 std::map<std::string, Uint32Accessor> uint32_properties_;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700253 std::map<std::string, Uint64Accessor> uint64_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700254
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700255 PropertyChangeCallback property_changed_callback_;
256
Chris Masoneb925cc82011-06-22 15:39:57 -0700257 DISALLOW_COPY_AND_ASSIGN(PropertyStore);
258};
259
260} // namespace shill
261
262#endif // SHILL_PROPERTY_STORE_