aboutsummaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/dbus_property.h
blob: 0f76a13cd89afa17f07a76dd56ab399c410cc63a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBCHROMEOS_CHROMEOS_DBUS_DBUS_PROPERTY_H_
#define LIBCHROMEOS_CHROMEOS_DBUS_DBUS_PROPERTY_H_

#include <chromeos/dbus/data_serialization.h>
#include <dbus/property.h>

namespace chromeos {
namespace dbus_utils {

// Re-implementation of dbus::Property<T> that can handle any type supported by
// D-Bus data serialization layer, such as vectors, maps, tuples, etc.
// This class is pretty much a copy of dbus::Property<T> from dbus/property.h
// except that it provides the implementations for PopValueFromReader and
// AppendSetValueToWriter.
template<class T>
class Property : public dbus::PropertyBase {
 public:
  Property() = default;

  // Retrieves the cached value.
  const T& value() const { return value_; }

  // Requests an updated value from the remote object incurring a
  // round-trip. |callback| will be called when the new value is available.
  // This may not be implemented by some interfaces.
  void Get(dbus::PropertySet::GetCallback callback) {
    property_set()->Get(this, callback);
  }

  // Synchronous vesion of Get().
  bool GetAndBlock() {
    return property_set()->GetAndBlock(this);
  }

  // Requests that the remote object change the property value to |value|,
  // |callback| will be called to indicate the success or failure of the
  // request, however the new value may not be available depending on the
  // remote object.
  void Set(const T& value, dbus::PropertySet::SetCallback callback) {
    set_value_ = value;
    property_set()->Set(this, callback);
  }

  // Synchronous version of Set().
  bool SetAndBlock(const T& value) {
    set_value_ = value;
    return property_set()->SetAndBlock(this);
  }

  // Method used by PropertySet to retrieve the value from a MessageReader,
  // no knowledge of the contained type is required, this method returns
  // true if its expected type was found, false if not.
  bool PopValueFromReader(dbus::MessageReader* reader) override {
    return PopVariantValueFromReader(reader, &value_);
  }

  // Method used by PropertySet to append the set value to a MessageWriter,
  // no knowledge of the contained type is required.
  // Implementation provided by specialization.
  void AppendSetValueToWriter(dbus::MessageWriter* writer) override {
    AppendValueToWriterAsVariant(writer, set_value_);
  }

  // Method used by test and stub implementations of dbus::PropertySet::Set
  // to replace the property value with the set value without using a
  // dbus::MessageReader.
  void ReplaceValueWithSetValue() override {
    value_ = set_value_;
    property_set()->NotifyPropertyChanged(name());
  }

  // Method used by test and stub implementations to directly set the
  // value of a property.
  void ReplaceValue(const T& value) {
    value_ = value;
    property_set()->NotifyPropertyChanged(name());
  }

 private:
  // Current cached value of the property.
  T value_;

  // Replacement value of the property.
  T set_value_;
};

}  // namespace dbus_utils
}  // namespace chromeos

#endif  // LIBCHROMEOS_CHROMEOS_DBUS_DBUS_PROPERTY_H_