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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/dbus/properties/types.h"
#include "base/memory/ref_counted_memory.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
DbusType::~DbusType() = default;
bool DbusType::operator==(const DbusType& other) const {
if (GetSignatureDynamic() != other.GetSignatureDynamic())
return false;
return IsEqual(other);
}
bool DbusType::operator!=(const DbusType& other) const {
return !(*this == other);
}
DbusBoolean::DbusBoolean(bool value) : value_(value) {}
DbusBoolean::DbusBoolean(DbusBoolean&& other) = default;
DbusBoolean::~DbusBoolean() = default;
void DbusBoolean::Write(dbus::MessageWriter* writer) const {
writer->AppendBool(value_);
}
// static
std::string DbusBoolean::GetSignature() {
return "b";
}
DbusInt32::DbusInt32(int32_t value) : value_(value) {}
DbusInt32::DbusInt32(DbusInt32&& other) = default;
DbusInt32::~DbusInt32() = default;
void DbusInt32::Write(dbus::MessageWriter* writer) const {
writer->AppendInt32(value_);
}
// static
std::string DbusInt32::GetSignature() {
return "i";
}
DbusUint32::DbusUint32(uint32_t value) : value_(value) {}
DbusUint32::DbusUint32(DbusUint32&& other) = default;
DbusUint32::~DbusUint32() = default;
void DbusUint32::Write(dbus::MessageWriter* writer) const {
writer->AppendUint32(value_);
}
// static
std::string DbusUint32::GetSignature() {
return "u";
}
DbusInt64::DbusInt64(int64_t value) : value_(value) {}
DbusInt64::DbusInt64(DbusInt64&& other) = default;
DbusInt64::~DbusInt64() = default;
void DbusInt64::Write(dbus::MessageWriter* writer) const {
writer->AppendInt64(value_);
}
// static
std::string DbusInt64::GetSignature() {
return "x";
}
DbusDouble::DbusDouble(double value) : value_(value) {}
DbusDouble::DbusDouble(DbusDouble&& other) = default;
DbusDouble::~DbusDouble() = default;
void DbusDouble::Write(dbus::MessageWriter* writer) const {
writer->AppendDouble(value_);
}
// static
std::string DbusDouble::GetSignature() {
return "d";
}
DbusString::DbusString(const std::string& value) : value_(value) {}
DbusString::DbusString(DbusString&& other) = default;
DbusString::~DbusString() = default;
void DbusString::Write(dbus::MessageWriter* writer) const {
writer->AppendString(value_);
}
// static
std::string DbusString::GetSignature() {
return "s";
}
DbusObjectPath::DbusObjectPath(const dbus::ObjectPath& value) : value_(value) {}
DbusObjectPath::DbusObjectPath(DbusObjectPath&& other) = default;
DbusObjectPath::~DbusObjectPath() = default;
void DbusObjectPath::Write(dbus::MessageWriter* writer) const {
writer->AppendObjectPath(value_);
}
// static
std::string DbusObjectPath::GetSignature() {
return "o";
}
DbusVariant::DbusVariant() = default;
DbusVariant::DbusVariant(std::unique_ptr<DbusType> value)
: value_(std::move(value)) {}
DbusVariant::DbusVariant(DbusVariant&& other) = default;
DbusVariant::~DbusVariant() = default;
DbusVariant& DbusVariant::operator=(DbusVariant&& other) = default;
DbusVariant::operator bool() const {
return !!value_;
}
bool DbusVariant::IsEqual(const DbusType& other_type) const {
const DbusVariant* other = static_cast<const DbusVariant*>(&other_type);
return *value_ == *other->value_;
}
void DbusVariant::Write(dbus::MessageWriter* writer) const {
dbus::MessageWriter variant_writer(nullptr);
writer->OpenVariant(value_->GetSignatureDynamic(), &variant_writer);
value_->Write(&variant_writer);
writer->CloseContainer(&variant_writer);
}
// static
std::string DbusVariant::GetSignature() {
return "v";
}
DbusByteArray::DbusByteArray() = default;
DbusByteArray::DbusByteArray(scoped_refptr<base::RefCountedMemory> value)
: value_(value) {}
DbusByteArray::DbusByteArray(DbusByteArray&& other) = default;
DbusByteArray::~DbusByteArray() = default;
bool DbusByteArray::IsEqual(const DbusType& other_type) const {
const DbusByteArray* other = static_cast<const DbusByteArray*>(&other_type);
return value_->size() == other->value_->size() &&
!memcmp(value_->front(), other->value_->front(), value_->size());
}
void DbusByteArray::Write(dbus::MessageWriter* writer) const {
writer->AppendArrayOfBytes(value_->front(), value_->size());
}
// static
std::string DbusByteArray::GetSignature() {
return "ay"; // lmao
}
DbusDictionary::DbusDictionary() = default;
DbusDictionary::DbusDictionary(DbusDictionary&& other) = default;
DbusDictionary::~DbusDictionary() = default;
bool DbusDictionary::Put(const std::string& key, DbusVariant&& value) {
auto it = value_.find(key);
const bool updated = it == value_.end() || it->second != value;
value_[key] = std::move(value);
return updated;
}
void DbusDictionary::Write(dbus::MessageWriter* writer) const {
dbus::MessageWriter array_writer(nullptr);
writer->OpenArray("{sv}", &array_writer);
for (const auto& pair : value_) {
dbus::MessageWriter dict_entry_writer(nullptr);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString(pair.first);
pair.second.Write(&dict_entry_writer);
array_writer.CloseContainer(&dict_entry_writer);
}
writer->CloseContainer(&array_writer);
}
// static
std::string DbusDictionary::GetSignature() {
return "a{sv}";
}
|