1 // Copyright 2019 The Chromium 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 #include "components/dbus/properties/types.h"
6 
7 #include "dbus/message.h"
8 #include "dbus/object_path.h"
9 
10 DbusType::~DbusType() = default;
11 
operator ==(const DbusType & other) const12 bool DbusType::operator==(const DbusType& other) const {
13   if (GetSignatureDynamic() != other.GetSignatureDynamic())
14     return false;
15   return IsEqual(other);
16 }
operator !=(const DbusType & other) const17 bool DbusType::operator!=(const DbusType& other) const {
18   return !(*this == other);
19 }
20 
DbusBoolean(bool value)21 DbusBoolean::DbusBoolean(bool value) : value_(value) {}
22 DbusBoolean::DbusBoolean(DbusBoolean&& other) = default;
23 DbusBoolean::~DbusBoolean() = default;
24 
Write(dbus::MessageWriter * writer) const25 void DbusBoolean::Write(dbus::MessageWriter* writer) const {
26   writer->AppendBool(value_);
27 }
28 
29 // static
GetSignature()30 std::string DbusBoolean::GetSignature() {
31   return "b";
32 }
33 
DbusInt32(int32_t value)34 DbusInt32::DbusInt32(int32_t value) : value_(value) {}
35 DbusInt32::DbusInt32(DbusInt32&& other) = default;
36 DbusInt32::~DbusInt32() = default;
37 
Write(dbus::MessageWriter * writer) const38 void DbusInt32::Write(dbus::MessageWriter* writer) const {
39   writer->AppendInt32(value_);
40 }
41 
42 // static
GetSignature()43 std::string DbusInt32::GetSignature() {
44   return "i";
45 }
46 
DbusUint32(uint32_t value)47 DbusUint32::DbusUint32(uint32_t value) : value_(value) {}
48 DbusUint32::DbusUint32(DbusUint32&& other) = default;
49 DbusUint32::~DbusUint32() = default;
50 
Write(dbus::MessageWriter * writer) const51 void DbusUint32::Write(dbus::MessageWriter* writer) const {
52   writer->AppendUint32(value_);
53 }
54 
55 // static
GetSignature()56 std::string DbusUint32::GetSignature() {
57   return "u";
58 }
59 
DbusInt64(int64_t value)60 DbusInt64::DbusInt64(int64_t value) : value_(value) {}
61 DbusInt64::DbusInt64(DbusInt64&& other) = default;
62 DbusInt64::~DbusInt64() = default;
63 
Write(dbus::MessageWriter * writer) const64 void DbusInt64::Write(dbus::MessageWriter* writer) const {
65   writer->AppendInt64(value_);
66 }
67 
68 // static
GetSignature()69 std::string DbusInt64::GetSignature() {
70   return "x";
71 }
72 
DbusDouble(double value)73 DbusDouble::DbusDouble(double value) : value_(value) {}
74 DbusDouble::DbusDouble(DbusDouble&& other) = default;
75 DbusDouble::~DbusDouble() = default;
76 
Write(dbus::MessageWriter * writer) const77 void DbusDouble::Write(dbus::MessageWriter* writer) const {
78   writer->AppendDouble(value_);
79 }
80 
81 // static
GetSignature()82 std::string DbusDouble::GetSignature() {
83   return "d";
84 }
85 
DbusString(const std::string & value)86 DbusString::DbusString(const std::string& value) : value_(value) {}
87 DbusString::DbusString(DbusString&& other) = default;
88 DbusString::~DbusString() = default;
89 
Write(dbus::MessageWriter * writer) const90 void DbusString::Write(dbus::MessageWriter* writer) const {
91   writer->AppendString(value_);
92 }
93 
94 // static
GetSignature()95 std::string DbusString::GetSignature() {
96   return "s";
97 }
98 
DbusObjectPath(const dbus::ObjectPath & value)99 DbusObjectPath::DbusObjectPath(const dbus::ObjectPath& value) : value_(value) {}
100 DbusObjectPath::DbusObjectPath(DbusObjectPath&& other) = default;
101 DbusObjectPath::~DbusObjectPath() = default;
102 
Write(dbus::MessageWriter * writer) const103 void DbusObjectPath::Write(dbus::MessageWriter* writer) const {
104   writer->AppendObjectPath(value_);
105 }
106 
107 // static
GetSignature()108 std::string DbusObjectPath::GetSignature() {
109   return "o";
110 }
111 
112 DbusVariant::DbusVariant() = default;
DbusVariant(std::unique_ptr<DbusType> value)113 DbusVariant::DbusVariant(std::unique_ptr<DbusType> value)
114     : value_(std::move(value)) {}
115 DbusVariant::DbusVariant(DbusVariant&& other) = default;
116 DbusVariant::~DbusVariant() = default;
117 DbusVariant& DbusVariant::operator=(DbusVariant&& other) = default;
118 
operator bool() const119 DbusVariant::operator bool() const {
120   return !!value_;
121 }
122 
IsEqual(const DbusType & other_type) const123 bool DbusVariant::IsEqual(const DbusType& other_type) const {
124   const DbusVariant* other = static_cast<const DbusVariant*>(&other_type);
125   return *value_ == *other->value_;
126 }
127 
Write(dbus::MessageWriter * writer) const128 void DbusVariant::Write(dbus::MessageWriter* writer) const {
129   dbus::MessageWriter variant_writer(nullptr);
130   writer->OpenVariant(value_->GetSignatureDynamic(), &variant_writer);
131   value_->Write(&variant_writer);
132   writer->CloseContainer(&variant_writer);
133 }
134 
135 // static
GetSignature()136 std::string DbusVariant::GetSignature() {
137   return "v";
138 }
139 
140 DbusByteArray::DbusByteArray() = default;
DbusByteArray(scoped_refptr<base::RefCountedMemory> value)141 DbusByteArray::DbusByteArray(scoped_refptr<base::RefCountedMemory> value)
142     : value_(value) {}
143 DbusByteArray::DbusByteArray(DbusByteArray&& other) = default;
144 DbusByteArray::~DbusByteArray() = default;
145 
IsEqual(const DbusType & other_type) const146 bool DbusByteArray::IsEqual(const DbusType& other_type) const {
147   const DbusByteArray* other = static_cast<const DbusByteArray*>(&other_type);
148   return value_->size() == other->value_->size() &&
149          !memcmp(value_->front(), other->value_->front(), value_->size());
150 }
151 
Write(dbus::MessageWriter * writer) const152 void DbusByteArray::Write(dbus::MessageWriter* writer) const {
153   writer->AppendArrayOfBytes(value_->front(), value_->size());
154 }
155 
156 // static
GetSignature()157 std::string DbusByteArray::GetSignature() {
158   return "ay";  // lmao
159 }
160 
161 DbusDictionary::DbusDictionary() = default;
162 DbusDictionary::DbusDictionary(DbusDictionary&& other) = default;
163 DbusDictionary::~DbusDictionary() = default;
164 
Put(const std::string & key,DbusVariant && value)165 bool DbusDictionary::Put(const std::string& key, DbusVariant&& value) {
166   auto it = value_.find(key);
167   const bool updated = it == value_.end() || it->second != value;
168   value_[key] = std::move(value);
169   return updated;
170 }
171 
Write(dbus::MessageWriter * writer) const172 void DbusDictionary::Write(dbus::MessageWriter* writer) const {
173   dbus::MessageWriter array_writer(nullptr);
174   writer->OpenArray("{sv}", &array_writer);
175   for (const auto& pair : value_) {
176     dbus::MessageWriter dict_entry_writer(nullptr);
177     array_writer.OpenDictEntry(&dict_entry_writer);
178     dict_entry_writer.AppendString(pair.first);
179     pair.second.Write(&dict_entry_writer);
180     array_writer.CloseContainer(&dict_entry_writer);
181   }
182   writer->CloseContainer(&array_writer);
183 }
184 
185 // static
GetSignature()186 std::string DbusDictionary::GetSignature() {
187   return "a{sv}";
188 }
189