1 // Copyright 2014 The Chromium OS Authors. All rights reserved. 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 LIBBRILLO_BRILLO_DBUS_DBUS_PROPERTY_H_ 6 #define LIBBRILLO_BRILLO_DBUS_DBUS_PROPERTY_H_ 7 8 #include <brillo/dbus/data_serialization.h> 9 #include <dbus/property.h> 10 11 namespace brillo { 12 namespace dbus_utils { 13 14 // Re-implementation of dbus::Property<T> that can handle any type supported by 15 // D-Bus data serialization layer, such as vectors, maps, tuples, etc. 16 // This class is pretty much a copy of dbus::Property<T> from dbus/property.h 17 // except that it provides the implementations for PopValueFromReader and 18 // AppendSetValueToWriter. 19 template<class T> 20 class Property : public dbus::PropertyBase { 21 public: 22 Property() = default; 23 24 // Retrieves the cached value. value()25 const T& value() const { return value_; } 26 27 // Requests an updated value from the remote object incurring a 28 // round-trip. |callback| will be called when the new value is available. 29 // This may not be implemented by some interfaces. Get(dbus::PropertySet::GetCallback callback)30 void Get(dbus::PropertySet::GetCallback callback) { 31 property_set()->Get(this, callback); 32 } 33 34 // Synchronous vesion of Get(). GetAndBlock()35 bool GetAndBlock() { 36 return property_set()->GetAndBlock(this); 37 } 38 39 // Requests that the remote object change the property value to |value|, 40 // |callback| will be called to indicate the success or failure of the 41 // request, however the new value may not be available depending on the 42 // remote object. Set(const T & value,dbus::PropertySet::SetCallback callback)43 void Set(const T& value, dbus::PropertySet::SetCallback callback) { 44 set_value_ = value; 45 property_set()->Set(this, callback); 46 } 47 48 // Synchronous version of Set(). SetAndBlock(const T & value)49 bool SetAndBlock(const T& value) { 50 set_value_ = value; 51 return property_set()->SetAndBlock(this); 52 } 53 54 // Method used by PropertySet to retrieve the value from a MessageReader, 55 // no knowledge of the contained type is required, this method returns 56 // true if its expected type was found, false if not. PopValueFromReader(dbus::MessageReader * reader)57 bool PopValueFromReader(dbus::MessageReader* reader) override { 58 return PopVariantValueFromReader(reader, &value_); 59 } 60 61 // Method used by PropertySet to append the set value to a MessageWriter, 62 // no knowledge of the contained type is required. 63 // Implementation provided by specialization. AppendSetValueToWriter(dbus::MessageWriter * writer)64 void AppendSetValueToWriter(dbus::MessageWriter* writer) override { 65 AppendValueToWriterAsVariant(writer, set_value_); 66 } 67 68 // Method used by test and stub implementations of dbus::PropertySet::Set 69 // to replace the property value with the set value without using a 70 // dbus::MessageReader. ReplaceValueWithSetValue()71 void ReplaceValueWithSetValue() override { 72 value_ = set_value_; 73 property_set()->NotifyPropertyChanged(name()); 74 } 75 76 // Method used by test and stub implementations to directly set the 77 // value of a property. ReplaceValue(const T & value)78 void ReplaceValue(const T& value) { 79 value_ = value; 80 property_set()->NotifyPropertyChanged(name()); 81 } 82 83 private: 84 // Current cached value of the property. 85 T value_; 86 87 // Replacement value of the property. 88 T set_value_; 89 }; 90 91 } // namespace dbus_utils 92 } // namespace brillo 93 94 #endif // LIBBRILLO_BRILLO_DBUS_DBUS_PROPERTY_H_ 95