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