blob: 51576e109f4635276ba527b925ea52e3464c129b [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>
13
14#include "shill/accessor_interface.h"
Chris Masonea8a2c252011-06-27 22:16:30 -070015#include "shill/property_iterator.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070016
17namespace shill {
18
19class Error;
20
21class PropertyStore {
22 public:
Chris Masone27c4aa52011-07-02 13:10:14 -070023 PropertyStore();
Chris Masoneb925cc82011-06-22 15:39:57 -070024 virtual ~PropertyStore();
25
mukesh agrawalde29fa82011-09-16 16:16:36 -070026 virtual bool Contains(const std::string& property) const;
Chris Masoneb925cc82011-06-22 15:39:57 -070027
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 agrawal66b0aca2012-01-30 15:28:28 -080034 virtual bool SetBoolProperty(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070035 bool value,
36 Error *error);
37
mukesh agrawal66b0aca2012-01-30 15:28:28 -080038 virtual bool SetInt16Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070039 int16 value,
40 Error *error);
41
mukesh agrawal66b0aca2012-01-30 15:28:28 -080042 virtual bool SetInt32Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070043 int32 value,
44 Error *error);
45
mukesh agrawal66b0aca2012-01-30 15:28:28 -080046 virtual bool SetStringProperty(const std::string &name,
47 const std::string &value,
Chris Masoneb925cc82011-06-22 15:39:57 -070048 Error *error);
49
50 virtual bool SetStringmapProperty(
mukesh agrawal66b0aca2012-01-30 15:28:28 -080051 const std::string &name,
52 const std::map<std::string, std::string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -070053 Error *error);
54
mukesh agrawal66b0aca2012-01-30 15:28:28 -080055 virtual bool SetStringsProperty(const std::string &name,
56 const std::vector<std::string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -070057 Error *error);
58
mukesh agrawal66b0aca2012-01-30 15:28:28 -080059 virtual bool SetUint8Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070060 uint8 value,
61 Error *error);
62
mukesh agrawal66b0aca2012-01-30 15:28:28 -080063 virtual bool SetUint16Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070064 uint16 value,
65 Error *error);
66
mukesh agrawal66b0aca2012-01-30 15:28:28 -080067 virtual bool SetUint32Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070068 uint32 value,
69 Error *error);
70
Jason Glasgowacdc11f2012-03-30 14:12:22 -040071 virtual bool SetRpcIdentifierProperty(const std::string &name,
72 const RpcIdentifier &value,
73 Error *error);
74
mukesh agrawal4d260da2012-01-30 11:53:52 -080075 // Clearing a property resets it to its "factory" value. This value
76 // is generally the value that it (the property) had when it was
77 // registered with PropertyStore.
78 //
79 // The exception to this rule is write-only derived properties. For
80 // such properties, the property owner explicitly provides a
81 // "factory" value at registration time. This is necessary because
82 // PropertyStore can't read the current value at registration time.
83 //
84 // |name| is the key used to access the property. If the property
85 // cannot be cleared, |error| is set, and the method returns false.
86 // Otherwrise, |error| is unchanged, and the method returns true.
87 virtual bool ClearProperty(const std::string &name, Error *error);
88
mukesh agrawalffa3d042011-10-06 15:26:10 -070089 // We do not provide methods for reading individual properties,
90 // because we don't need them to implement the flimflam API. (The flimflam
91 // API only allows fetching all properties at once -- not individual
92 // properties.)
93
94 // Accessors for iterators over property maps. Useful for dumping all
95 // properties.
Gaurav Shah1b7a6162011-11-09 11:41:01 -080096 ReadablePropertyConstIterator<bool> GetBoolPropertiesIter() const;
97 ReadablePropertyConstIterator<int16> GetInt16PropertiesIter() const;
98 ReadablePropertyConstIterator<int32> GetInt32PropertiesIter() const;
Darin Petkov63138a92012-02-06 14:09:15 +010099 ReadablePropertyConstIterator<KeyValueStore> GetKeyValueStorePropertiesIter(
100 ) const;
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400101 ReadablePropertyConstIterator<RpcIdentifier> GetRpcIdentifierPropertiesIter(
102 ) const;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700103 ReadablePropertyConstIterator<RpcIdentifiers> GetRpcIdentifiersPropertiesIter(
104 ) const;
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800105 ReadablePropertyConstIterator<std::string> GetStringPropertiesIter() const;
106 ReadablePropertyConstIterator<Stringmap> GetStringmapPropertiesIter() const;
107 ReadablePropertyConstIterator<Stringmaps> GetStringmapsPropertiesIter() const;
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800108 ReadablePropertyConstIterator<Strings> GetStringsPropertiesIter() const;
109 ReadablePropertyConstIterator<uint8> GetUint8PropertiesIter() const;
110 ReadablePropertyConstIterator<uint16> GetUint16PropertiesIter() const;
111 ReadablePropertyConstIterator<uint32> GetUint32PropertiesIter() const;
Chris Masonea8a2c252011-06-27 22:16:30 -0700112
mukesh agrawal4d260da2012-01-30 11:53:52 -0800113 // Methods for registering a property.
114 //
115 // It is permitted to re-register a property (in which case the old
116 // binding is forgotten). However, the newly bound object must be of
117 // the same type.
118 //
119 // Note that types do not encode read-write permission. Hence, it
120 // is possible to change permissions by rebinding a property to the
121 // same object.
122 //
123 // (Corollary of the rebinding-to-same-type restriction: a
124 // PropertyStore cannot hold two properties of the same name, but
125 // differing types.)
Chris Masoneb925cc82011-06-22 15:39:57 -0700126 void RegisterBool(const std::string &name, bool *prop);
127 void RegisterConstBool(const std::string &name, const bool *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800128 void RegisterWriteOnlyBool(const std::string &name, bool *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700129 void RegisterInt16(const std::string &name, int16 *prop);
130 void RegisterConstInt16(const std::string &name, const int16 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800131 void RegisterWriteOnlyInt16(const std::string &name, int16 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700132 void RegisterInt32(const std::string &name, int32 *prop);
133 void RegisterConstInt32(const std::string &name, const int32 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800134 void RegisterWriteOnlyInt32(const std::string &name, int32 *prop);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800135 void RegisterUint32(const std::string &name, uint32 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700136 void RegisterString(const std::string &name, std::string *prop);
137 void RegisterConstString(const std::string &name, const std::string *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800138 void RegisterWriteOnlyString(const std::string &name, std::string *prop);
Chris Masone43b48a12011-07-01 13:37:07 -0700139 void RegisterStringmap(const std::string &name, Stringmap *prop);
140 void RegisterConstStringmap(const std::string &name, const Stringmap *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800141 void RegisterWriteOnlyStringmap(const std::string &name, Stringmap *prop);
Darin Petkovc0865312011-09-16 15:31:20 -0700142 void RegisterStringmaps(const std::string &name, Stringmaps *prop);
143 void RegisterConstStringmaps(const std::string &name, const Stringmaps *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800144 void RegisterWriteOnlyStringmaps(const std::string &name, Stringmaps *prop);
Chris Masone43b48a12011-07-01 13:37:07 -0700145 void RegisterStrings(const std::string &name, Strings *prop);
Chris Masone889666b2011-07-03 12:58:50 -0700146 void RegisterConstStrings(const std::string &name, const Strings *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800147 void RegisterWriteOnlyStrings(const std::string &name, Strings *prop);
Chris Masone889666b2011-07-03 12:58:50 -0700148 void RegisterUint8(const std::string &name, uint8 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700149 void RegisterConstUint8(const std::string &name, const uint8 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800150 void RegisterWriteOnlyUint8(const std::string &name, uint8 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700151 void RegisterUint16(const std::string &name, uint16 *prop);
152 void RegisterConstUint16(const std::string &name, const uint16 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800153 void RegisterWriteOnlyUint16(const std::string &name, uint16 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700154
Chris Masone27c4aa52011-07-02 13:10:14 -0700155 void RegisterDerivedBool(const std::string &name,
156 const BoolAccessor &accessor);
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800157 void RegisterDerivedInt32(const std::string &name,
158 const Int32Accessor &accessor);
Darin Petkov63138a92012-02-06 14:09:15 +0100159 void RegisterDerivedKeyValueStore(const std::string &name,
160 const KeyValueStoreAccessor &accessor);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400161 void RegisterDerivedRpcIdentifier(const std::string &name,
162 const RpcIdentifierAccessor &acc);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700163 void RegisterDerivedRpcIdentifiers(const std::string &name,
164 const RpcIdentifiersAccessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700165 void RegisterDerivedString(const std::string &name,
166 const StringAccessor &accessor);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400167 void RegisterDerivedStringmap(const std::string &name,
168 const StringmapAccessor &accessor);
Chris Masone889666b2011-07-03 12:58:50 -0700169 void RegisterDerivedStringmaps(const std::string &name,
170 const StringmapsAccessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700171 void RegisterDerivedStrings(const std::string &name,
172 const StringsAccessor &accessor);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800173 void RegisterDerivedUint16(const std::string &name,
174 const Uint16Accessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700175
176 private:
mukesh agrawalffa3d042011-10-06 15:26:10 -0700177 template <class V>
178 bool SetProperty(
179 const std::string &name,
180 const V &value,
181 Error *error,
182 std::map< std::string, std::tr1::shared_ptr< AccessorInterface<V> > > &,
183 const std::string &value_type_english);
184
Chris Masoneb925cc82011-06-22 15:39:57 -0700185 // These are std::maps instead of something cooler because the common
186 // operation is iterating through them and returning all properties.
187 std::map<std::string, BoolAccessor> bool_properties_;
188 std::map<std::string, Int16Accessor> int16_properties_;
189 std::map<std::string, Int32Accessor> int32_properties_;
Darin Petkov63138a92012-02-06 14:09:15 +0100190 std::map<std::string, KeyValueStoreAccessor> key_value_store_properties_;
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400191 std::map<std::string, RpcIdentifierAccessor> rpc_identifier_properties_;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700192 std::map<std::string, RpcIdentifiersAccessor> rpc_identifiers_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700193 std::map<std::string, StringAccessor> string_properties_;
194 std::map<std::string, StringmapAccessor> stringmap_properties_;
Chris Masone889666b2011-07-03 12:58:50 -0700195 std::map<std::string, StringmapsAccessor> stringmaps_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700196 std::map<std::string, StringsAccessor> strings_properties_;
197 std::map<std::string, Uint8Accessor> uint8_properties_;
198 std::map<std::string, Uint16Accessor> uint16_properties_;
199 std::map<std::string, Uint32Accessor> uint32_properties_;
200
Chris Masoneb925cc82011-06-22 15:39:57 -0700201 DISALLOW_COPY_AND_ASSIGN(PropertyStore);
202};
203
204} // namespace shill
205
206#endif // SHILL_PROPERTY_STORE_