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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
// 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/compiler_specific.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "dbus/message.h"
namespace {
bool PopContainer(dbus::MessageReader* reader,
dbus::MessageReader* element_reader) {
switch (reader->GetDataType()) {
case dbus::Message::DataType::ARRAY:
return reader->PopArray(element_reader);
case dbus::Message::DataType::STRUCT:
return reader->PopStruct(element_reader);
case dbus::Message::DataType::DICT_ENTRY:
return reader->PopDictEntry(element_reader);
default:
NOTREACHED();
}
}
template <typename T>
std::unique_ptr<DbusType> CreateDbusType(dbus::MessageReader* reader) {
auto value = std::make_unique<T>();
if (!value->Read(reader)) {
return nullptr;
}
return value;
}
std::unique_ptr<DbusType> CreateDynamicDbusType(dbus::MessageReader* reader) {
switch (reader->GetDataType()) {
case dbus::Message::DataType::BYTE:
return CreateDbusType<DbusByte>(reader);
case dbus::Message::DataType::BOOL:
return CreateDbusType<DbusBoolean>(reader);
case dbus::Message::DataType::INT16:
return CreateDbusType<DbusInt16>(reader);
case dbus::Message::DataType::UINT16:
return CreateDbusType<DbusUint16>(reader);
case dbus::Message::DataType::INT32:
return CreateDbusType<DbusInt32>(reader);
case dbus::Message::DataType::UINT32:
return CreateDbusType<DbusUint32>(reader);
case dbus::Message::DataType::INT64:
return CreateDbusType<DbusInt64>(reader);
case dbus::Message::DataType::UINT64:
return CreateDbusType<DbusUint64>(reader);
case dbus::Message::DataType::DOUBLE:
return CreateDbusType<DbusDouble>(reader);
case dbus::Message::DataType::STRING:
return CreateDbusType<DbusString>(reader);
case dbus::Message::DataType::OBJECT_PATH:
return CreateDbusType<DbusObjectPath>(reader);
case dbus::Message::DataType::UNIX_FD:
return CreateDbusType<DbusUnixFd>(reader);
case dbus::Message::DataType::VARIANT:
return CreateDbusType<DbusVariant>(reader);
case dbus::Message::DataType::ARRAY:
// Special case for byte arrays to avoid creating a bunch of virtual
// objects for each byte of binary data.
if (reader->GetDataSignature() == "ay") {
return CreateDbusType<DbusByteArray>(reader);
}
// Special case for string-variant dictionaries.
if (reader->GetDataSignature() == "a{sv}") {
return CreateDbusType<DbusDictionary>(reader);
}
[[fallthrough]];
case dbus::Message::DataType::STRUCT:
case dbus::Message::DataType::DICT_ENTRY:
// For templated types (array, struct dict entry), an untyped container is
// required.
return CreateDbusType<detail::UntypedDbusContainer>(reader);
case dbus::Message::DataType::INVALID_DATA:
return nullptr;
}
}
} // namespace
DbusType::~DbusType() = default;
bool DbusType::operator==(const DbusType& other) const {
if (!TypeMatches(other)) {
return false;
}
// Dynamic types should be casted to static types before comparison.
CHECK(!IsUntyped());
CHECK(!other.IsUntyped());
return IsEqual(other);
}
bool DbusType::Move(DbusType&& object) {
if (GetSignatureDynamic() != object.GetSignatureDynamic()) {
return false;
}
// Allow moving from UntypedDbusContainer to DbusParameters.
if (IsParameters() != object.IsParameters() &&
(!IsParameters() || !object.IsUntyped())) {
return false;
}
MoveImpl(std::move(object));
return true;
}
bool DbusType::IsUntyped() const {
return false;
}
bool DbusType::IsParameters() const {
return false;
}
bool DbusType::TypeMatches(const DbusType& other) const {
// An explicit check for IsParameters() is necessary since
// DbusParameters<DbusInt32> has the same signature as DbusInt32.
return IsParameters() == other.IsParameters() &&
GetSignatureDynamic() == other.GetSignatureDynamic();
}
namespace detail {
UntypedDbusContainer::UntypedDbusContainer() = default;
UntypedDbusContainer::UntypedDbusContainer(
std::vector<std::unique_ptr<DbusType>> value,
std::string signature)
: value_(std::move(value)), signature_(std::move(signature)) {}
UntypedDbusContainer::UntypedDbusContainer(
UntypedDbusContainer&& other) noexcept = default;
UntypedDbusContainer& UntypedDbusContainer::operator=(
UntypedDbusContainer&& other) noexcept = default;
UntypedDbusContainer::~UntypedDbusContainer() = default;
void UntypedDbusContainer::Write(dbus::MessageWriter* writer) const {
// Dynamic types are read from variants. Only static types should be written.
NOTREACHED();
}
bool UntypedDbusContainer::Read(dbus::MessageReader* reader) {
dbus::MessageReader element_reader(nullptr);
signature_ = reader->GetDataSignature();
if (!PopContainer(reader, &element_reader)) {
return false;
}
value_.clear();
while (element_reader.HasMoreData()) {
std::unique_ptr<DbusType> element = CreateDynamicDbusType(&element_reader);
if (!element) {
return false;
}
value_.push_back(std::move(element));
}
return true;
}
std::string UntypedDbusContainer::GetSignatureDynamic() const {
return signature_;
}
bool UntypedDbusContainer::IsEqual(const DbusType& other_type) const {
// Dynamic types should be casted to static types before comparison.
NOTREACHED();
}
void UntypedDbusContainer::MoveImpl(DbusType&& object) {
if (!object.IsUntyped()) {
// Can't currently move from a typed container back to an untyped one.
// It's possible to implement, but there's no point.
NOTIMPLEMENTED();
return;
}
CHECK(object.IsUntyped());
auto* other = static_cast<UntypedDbusContainer*>(&object);
value_ = std::move(other->value_);
signature_ = std::move(other->signature_);
}
bool UntypedDbusContainer::IsUntyped() const {
return true;
}
} // namespace detail
DbusUnixFd::DbusUnixFd() = default;
DbusUnixFd::DbusUnixFd(base::ScopedFD fd) : value_(std::move(fd)) {}
DbusUnixFd::DbusUnixFd(DbusUnixFd&& other) noexcept = default;
DbusUnixFd& DbusUnixFd::operator=(DbusUnixFd&& other) noexcept = default;
DbusUnixFd::~DbusUnixFd() = default;
void DbusUnixFd::Write(dbus::MessageWriter* writer) const {
// The fd will be duplicated.
writer->AppendFileDescriptor(value_.get());
}
bool DbusUnixFd::Read(dbus::MessageReader* reader) {
return reader->PopFileDescriptor(&value_);
}
std::string DbusUnixFd::GetSignatureDynamic() const {
return "h";
}
bool DbusUnixFd::IsEqual(const DbusType& other_type) const {
// FDs can't be compared, other than by value. However, since ScopedFD has
// unique ownership over the FD, values will always compare false.
return false;
}
void DbusUnixFd::MoveImpl(DbusType&& object) {
value_ = std::move(static_cast<DbusUnixFd*>(&object)->value_);
}
bool DbusUnixFd::IsUntyped() const {
return false;
}
// static
std::string DbusUnixFd::GetSignature() {
return "h";
}
DbusVariant::DbusVariant() = default;
DbusVariant::DbusVariant(std::unique_ptr<DbusType> value)
: value_(std::move(value)) {}
DbusVariant::DbusVariant(DbusVariant&& other) noexcept = default;
DbusVariant& DbusVariant::operator=(DbusVariant&& other) noexcept = default;
DbusVariant::~DbusVariant() = default;
DbusVariant::operator bool() const {
return !!value_;
}
bool DbusVariant::IsEqual(const DbusType& other_type) const {
const DbusVariant* other = static_cast<const DbusVariant*>(&other_type);
if (value_ && other->value_) {
return *value_ == *other->value_;
}
return value_ == other->value_;
}
void DbusVariant::Write(dbus::MessageWriter* writer) const {
CHECK(value_);
dbus::MessageWriter variant_writer(nullptr);
writer->OpenVariant(value_->GetSignatureDynamic(), &variant_writer);
value_->Write(&variant_writer);
writer->CloseContainer(&variant_writer);
}
bool DbusVariant::Read(dbus::MessageReader* reader) {
dbus::MessageReader variant_reader(nullptr);
if (!reader->PopVariant(&variant_reader)) {
return false;
}
value_ = CreateDynamicDbusType(&variant_reader);
return value_ != nullptr;
}
// static
std::string DbusVariant::GetSignature() {
return "v";
}
DbusByteArray::DbusByteArray() = default;
DbusByteArray::DbusByteArray(scoped_refptr<base::RefCountedMemory> value)
: value_(std::move(value)) {}
DbusByteArray::DbusByteArray(DbusByteArray&& other) noexcept = default;
DbusByteArray& DbusByteArray::operator=(DbusByteArray&& other) noexcept =
default;
DbusByteArray::~DbusByteArray() = default;
bool DbusByteArray::IsEqual(const DbusType& other_type) const {
const DbusByteArray* other = static_cast<const DbusByteArray*>(&other_type);
return value_->Equals(other->value_);
}
void DbusByteArray::Write(dbus::MessageWriter* writer) const {
writer->AppendArrayOfBytes(*value_);
}
bool DbusByteArray::Read(dbus::MessageReader* reader) {
const uint8_t* bytes = nullptr;
size_t length = 0;
if (!reader->PopArrayOfBytes(&bytes, &length)) {
return false;
}
// SAFETY: the span adapts the pointer-length return value of
// PopArrayOfBytes() without any pointer arithmetic.
auto data = UNSAFE_BUFFERS(base::span(bytes, length));
value_ = base::MakeRefCounted<base::RefCountedBytes>(data);
return true;
}
// static
std::string DbusByteArray::GetSignature() {
return "ay";
}
DbusDictionary::DbusDictionary() = default;
DbusDictionary::DbusDictionary(DbusDictionary&& other) noexcept = default;
DbusDictionary& DbusDictionary::operator=(DbusDictionary&& other) noexcept =
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;
}
DbusVariant* DbusDictionary::Get(const std::string& key) {
auto it = value_.find(key);
return it != value_.end() ? &it->second : nullptr;
}
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);
}
bool DbusDictionary::Read(dbus::MessageReader* reader) {
dbus::MessageReader array_reader(nullptr);
if (!reader->PopArray(&array_reader)) {
return false;
}
while (array_reader.HasMoreData()) {
dbus::MessageReader dict_entry_reader(nullptr);
if (!array_reader.PopDictEntry(&dict_entry_reader)) {
return false;
}
std::string key;
if (!dict_entry_reader.PopString(&key)) {
return false;
}
DbusVariant value;
if (!value.Read(&dict_entry_reader)) {
return false;
}
value_[key] = std::move(value);
}
return true;
}
// static
std::string DbusDictionary::GetSignature() {
return "a{sv}";
}
DbusDictionary MakeDbusDictionary() {
return DbusDictionary();
}
DbusVariant ReadDbusMessage(dbus::MessageReader* reader) {
std::string signature;
std::vector<std::unique_ptr<DbusType>> data;
while (reader->HasMoreData()) {
data.push_back(CreateDynamicDbusType(reader));
if (!data.back()) {
return DbusVariant();
}
signature += data.back()->GetSignatureDynamic();
}
if (data.size() == 1U) {
return DbusVariant(std::move(data[0]));
}
return DbusVariant(std::make_unique<detail::UntypedDbusContainer>(
std::move(data), std::move(signature)));
}
|