mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 2 | // 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> |
| 13 | |
| 14 | #include "shill/accessor_interface.h" |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 15 | #include "shill/property_iterator.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | class Error; |
| 20 | |
| 21 | class PropertyStore { |
| 22 | public: |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 23 | PropertyStore(); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 24 | virtual ~PropertyStore(); |
| 25 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 26 | virtual bool Contains(const std::string& property) const; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 27 | |
| 28 | // Methods to allow the setting, by name, of properties stored in this object. |
| 29 | // The property names are declared in chromeos/dbus/service_constants.h, |
| 30 | // so that they may be shared with libcros. |
| 31 | // Upon success, these methods return true and leave |error| untouched. |
| 32 | // Upon failure, they return false and set |error| appropriately, if it |
| 33 | // is non-NULL. |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 34 | virtual bool SetBoolProperty(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 35 | bool value, |
| 36 | Error *error); |
| 37 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 38 | virtual bool SetInt16Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 39 | int16 value, |
| 40 | Error *error); |
| 41 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 42 | virtual bool SetInt32Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 43 | int32 value, |
| 44 | Error *error); |
| 45 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 46 | virtual bool SetStringProperty(const std::string &name, |
| 47 | const std::string &value, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 48 | Error *error); |
| 49 | |
| 50 | virtual bool SetStringmapProperty( |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 51 | const std::string &name, |
| 52 | const std::map<std::string, std::string> &values, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 53 | Error *error); |
| 54 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 55 | virtual bool SetStringsProperty(const std::string &name, |
| 56 | const std::vector<std::string> &values, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 57 | Error *error); |
| 58 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 59 | virtual bool SetUint8Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 60 | uint8 value, |
| 61 | Error *error); |
| 62 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 63 | virtual bool SetUint16Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 64 | uint16 value, |
| 65 | Error *error); |
| 66 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 67 | virtual bool SetUint32Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 68 | uint32 value, |
| 69 | Error *error); |
| 70 | |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 71 | virtual bool SetUint64Property(const std::string &name, |
| 72 | uint64 value, |
| 73 | Error *error); |
| 74 | |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 75 | virtual bool SetRpcIdentifierProperty(const std::string &name, |
| 76 | const RpcIdentifier &value, |
| 77 | Error *error); |
| 78 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 79 | // Clearing a property resets it to its "factory" value. This value |
| 80 | // is generally the value that it (the property) had when it was |
| 81 | // registered with PropertyStore. |
| 82 | // |
| 83 | // The exception to this rule is write-only derived properties. For |
| 84 | // such properties, the property owner explicitly provides a |
| 85 | // "factory" value at registration time. This is necessary because |
| 86 | // PropertyStore can't read the current value at registration time. |
| 87 | // |
| 88 | // |name| is the key used to access the property. If the property |
| 89 | // cannot be cleared, |error| is set, and the method returns false. |
| 90 | // Otherwrise, |error| is unchanged, and the method returns true. |
| 91 | virtual bool ClearProperty(const std::string &name, Error *error); |
| 92 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 93 | // We do not provide methods for reading individual properties, |
| 94 | // because we don't need them to implement the flimflam API. (The flimflam |
| 95 | // API only allows fetching all properties at once -- not individual |
| 96 | // properties.) |
| 97 | |
| 98 | // Accessors for iterators over property maps. Useful for dumping all |
| 99 | // properties. |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 100 | ReadablePropertyConstIterator<bool> GetBoolPropertiesIter() const; |
| 101 | ReadablePropertyConstIterator<int16> GetInt16PropertiesIter() const; |
| 102 | ReadablePropertyConstIterator<int32> GetInt32PropertiesIter() const; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 103 | ReadablePropertyConstIterator<KeyValueStore> GetKeyValueStorePropertiesIter( |
| 104 | ) const; |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 105 | ReadablePropertyConstIterator<RpcIdentifier> GetRpcIdentifierPropertiesIter( |
| 106 | ) const; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 107 | ReadablePropertyConstIterator<RpcIdentifiers> GetRpcIdentifiersPropertiesIter( |
| 108 | ) const; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 109 | ReadablePropertyConstIterator<std::string> GetStringPropertiesIter() const; |
| 110 | ReadablePropertyConstIterator<Stringmap> GetStringmapPropertiesIter() const; |
| 111 | ReadablePropertyConstIterator<Stringmaps> GetStringmapsPropertiesIter() const; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 112 | ReadablePropertyConstIterator<Strings> GetStringsPropertiesIter() const; |
| 113 | ReadablePropertyConstIterator<uint8> GetUint8PropertiesIter() const; |
| 114 | ReadablePropertyConstIterator<uint16> GetUint16PropertiesIter() const; |
| 115 | ReadablePropertyConstIterator<uint32> GetUint32PropertiesIter() const; |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 116 | ReadablePropertyConstIterator<uint64> GetUint64PropertiesIter() const; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 117 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 118 | // Methods for registering a property. |
| 119 | // |
| 120 | // It is permitted to re-register a property (in which case the old |
| 121 | // binding is forgotten). However, the newly bound object must be of |
| 122 | // the same type. |
| 123 | // |
| 124 | // Note that types do not encode read-write permission. Hence, it |
| 125 | // is possible to change permissions by rebinding a property to the |
| 126 | // same object. |
| 127 | // |
| 128 | // (Corollary of the rebinding-to-same-type restriction: a |
| 129 | // PropertyStore cannot hold two properties of the same name, but |
| 130 | // differing types.) |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 131 | void RegisterBool(const std::string &name, bool *prop); |
| 132 | void RegisterConstBool(const std::string &name, const bool *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 133 | void RegisterWriteOnlyBool(const std::string &name, bool *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 134 | void RegisterInt16(const std::string &name, int16 *prop); |
| 135 | void RegisterConstInt16(const std::string &name, const int16 *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 136 | void RegisterWriteOnlyInt16(const std::string &name, int16 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 137 | void RegisterInt32(const std::string &name, int32 *prop); |
| 138 | void RegisterConstInt32(const std::string &name, const int32 *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 139 | void RegisterWriteOnlyInt32(const std::string &name, int32 *prop); |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 140 | void RegisterUint32(const std::string &name, uint32 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 141 | void RegisterString(const std::string &name, std::string *prop); |
| 142 | void RegisterConstString(const std::string &name, const std::string *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 143 | void RegisterWriteOnlyString(const std::string &name, std::string *prop); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 144 | void RegisterStringmap(const std::string &name, Stringmap *prop); |
| 145 | void RegisterConstStringmap(const std::string &name, const Stringmap *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 146 | void RegisterWriteOnlyStringmap(const std::string &name, Stringmap *prop); |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 147 | void RegisterStringmaps(const std::string &name, Stringmaps *prop); |
| 148 | void RegisterConstStringmaps(const std::string &name, const Stringmaps *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 149 | void RegisterWriteOnlyStringmaps(const std::string &name, Stringmaps *prop); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 150 | void RegisterStrings(const std::string &name, Strings *prop); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 151 | void RegisterConstStrings(const std::string &name, const Strings *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 152 | void RegisterWriteOnlyStrings(const std::string &name, Strings *prop); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 153 | void RegisterUint8(const std::string &name, uint8 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 154 | void RegisterConstUint8(const std::string &name, const uint8 *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 155 | void RegisterWriteOnlyUint8(const std::string &name, uint8 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 156 | void RegisterUint16(const std::string &name, uint16 *prop); |
| 157 | void RegisterConstUint16(const std::string &name, const uint16 *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 158 | void RegisterWriteOnlyUint16(const std::string &name, uint16 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 159 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 160 | void RegisterDerivedBool(const std::string &name, |
| 161 | const BoolAccessor &accessor); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 162 | void RegisterDerivedInt32(const std::string &name, |
| 163 | const Int32Accessor &accessor); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 164 | void RegisterDerivedKeyValueStore(const std::string &name, |
| 165 | const KeyValueStoreAccessor &accessor); |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 166 | void RegisterDerivedRpcIdentifier(const std::string &name, |
| 167 | const RpcIdentifierAccessor &acc); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 168 | void RegisterDerivedRpcIdentifiers(const std::string &name, |
| 169 | const RpcIdentifiersAccessor &accessor); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 170 | void RegisterDerivedString(const std::string &name, |
| 171 | const StringAccessor &accessor); |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 172 | void RegisterDerivedStringmap(const std::string &name, |
| 173 | const StringmapAccessor &accessor); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 174 | void RegisterDerivedStringmaps(const std::string &name, |
| 175 | const StringmapsAccessor &accessor); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 176 | void RegisterDerivedStrings(const std::string &name, |
| 177 | const StringsAccessor &accessor); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 178 | void RegisterDerivedUint16(const std::string &name, |
| 179 | const Uint16Accessor &accessor); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 180 | void RegisterDerivedUint64(const std::string &name, |
| 181 | const Uint64Accessor &accessor); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 182 | |
| 183 | private: |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 184 | template <class V> |
| 185 | bool SetProperty( |
| 186 | const std::string &name, |
| 187 | const V &value, |
| 188 | Error *error, |
| 189 | std::map< std::string, std::tr1::shared_ptr< AccessorInterface<V> > > &, |
| 190 | const std::string &value_type_english); |
| 191 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 192 | // These are std::maps instead of something cooler because the common |
| 193 | // operation is iterating through them and returning all properties. |
| 194 | std::map<std::string, BoolAccessor> bool_properties_; |
| 195 | std::map<std::string, Int16Accessor> int16_properties_; |
| 196 | std::map<std::string, Int32Accessor> int32_properties_; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 197 | std::map<std::string, KeyValueStoreAccessor> key_value_store_properties_; |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 198 | std::map<std::string, RpcIdentifierAccessor> rpc_identifier_properties_; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 199 | std::map<std::string, RpcIdentifiersAccessor> rpc_identifiers_properties_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 200 | std::map<std::string, StringAccessor> string_properties_; |
| 201 | std::map<std::string, StringmapAccessor> stringmap_properties_; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 202 | std::map<std::string, StringmapsAccessor> stringmaps_properties_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 203 | std::map<std::string, StringsAccessor> strings_properties_; |
| 204 | std::map<std::string, Uint8Accessor> uint8_properties_; |
| 205 | std::map<std::string, Uint16Accessor> uint16_properties_; |
| 206 | std::map<std::string, Uint32Accessor> uint32_properties_; |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 207 | std::map<std::string, Uint64Accessor> uint64_properties_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 208 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 209 | DISALLOW_COPY_AND_ASSIGN(PropertyStore); |
| 210 | }; |
| 211 | |
| 212 | } // namespace shill |
| 213 | |
| 214 | #endif // SHILL_PROPERTY_STORE_ |