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