1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "dbus/message.h"
9 #include "base/format_macros.h"
10 #include "base/logging.h"
11 #include "base/notreached.h"
12 #include "base/numerics/safe_conversions.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "dbus/object_path.h"
17 #include "third_party/protobuf/src/google/protobuf/message_lite.h"
21 // Appends the header name and the value to |output|, if the value is
23 void AppendStringHeader(const std::string& header_name,
24 const std::string& header_value,
25 std::string* output) {
26 if (!header_value.empty()) {
27 *output += header_name + ": " + header_value + "\n";
31 // Appends the header name and the value to |output|, if the value is
33 void AppendUint32Header(const std::string& header_name,
34 uint32_t header_value,
35 std::string* output) {
36 if (header_value != 0) {
37 *output += (header_name + ": " + base::NumberToString(header_value) + "\n");
45 bool IsDBusTypeUnixFdSupported() {
46 int major = 0, minor = 0, micro = 0;
47 dbus_get_version(&major, &minor, µ);
48 return major >= 1 && minor >= 4;
51 Message::Message() : raw_message_(nullptr) {}
55 dbus_message_unref(raw_message_);
58 void Message::Init(DBusMessage* raw_message) {
59 DCHECK(!raw_message_);
60 raw_message_ = raw_message;
63 Message::MessageType Message::GetMessageType() {
65 return MESSAGE_INVALID;
66 const int type = dbus_message_get_type(raw_message_);
67 return static_cast<Message::MessageType>(type);
70 std::string Message::GetMessageTypeAsString() {
71 switch (GetMessageType()) {
73 return "MESSAGE_INVALID";
74 case MESSAGE_METHOD_CALL:
75 return "MESSAGE_METHOD_CALL";
76 case MESSAGE_METHOD_RETURN:
77 return "MESSAGE_METHOD_RETURN";
79 return "MESSAGE_SIGNAL";
81 return "MESSAGE_ERROR";
87 std::string Message::ToStringInternal(const std::string& indent,
88 MessageReader* reader) {
89 const char* kBrokenMessage = "[broken message]";
91 while (reader->HasMoreData()) {
92 const DataType type = reader->GetDataType();
96 if (!reader->PopByte(&value))
97 return kBrokenMessage;
98 output += indent + "byte " + base::NumberToString(value) + "\n";
103 if (!reader->PopBool(&value))
104 return kBrokenMessage;
105 output += indent + "bool " + (value ? "true" : "false") + "\n";
110 if (!reader->PopInt16(&value))
111 return kBrokenMessage;
112 output += indent + "int16_t " + base::NumberToString(value) + "\n";
117 if (!reader->PopUint16(&value))
118 return kBrokenMessage;
119 output += indent + "uint16_t " + base::NumberToString(value) + "\n";
124 if (!reader->PopInt32(&value))
125 return kBrokenMessage;
126 output += indent + "int32_t " + base::NumberToString(value) + "\n";
131 if (!reader->PopUint32(&value))
132 return kBrokenMessage;
133 output += indent + "uint32_t " + base::NumberToString(value) + "\n";
138 if (!reader->PopInt64(&value))
139 return kBrokenMessage;
140 output += (indent + "int64_t " + base::NumberToString(value) + "\n");
145 if (!reader->PopUint64(&value))
146 return kBrokenMessage;
147 output += (indent + "uint64_t " + base::NumberToString(value) + "\n");
152 if (!reader->PopDouble(&value))
153 return kBrokenMessage;
154 output += indent + "double " + base::NumberToString(value) + "\n";
159 if (!reader->PopString(&value))
160 return kBrokenMessage;
161 // Truncate if the string is longer than the limit.
162 const size_t kTruncateLength = 100;
163 if (value.size() < kTruncateLength) {
164 output += indent + "string \"" + value + "\"\n";
166 std::string truncated;
167 base::TruncateUTF8ToByteSize(value, kTruncateLength, &truncated);
168 base::StringAppendF(&truncated, "... (%" PRIuS " bytes in total)",
170 output += indent + "string \"" + truncated + "\"\n";
176 if (!reader->PopObjectPath(&value))
177 return kBrokenMessage;
178 output += indent + "object_path \"" + value.value() + "\"\n";
182 MessageReader sub_reader(this);
183 if (!reader->PopArray(&sub_reader))
184 return kBrokenMessage;
185 output += indent + "array [\n";
186 output += ToStringInternal(indent + " ", &sub_reader);
187 output += indent + "]\n";
191 MessageReader sub_reader(this);
192 if (!reader->PopStruct(&sub_reader))
193 return kBrokenMessage;
194 output += indent + "struct {\n";
195 output += ToStringInternal(indent + " ", &sub_reader);
196 output += indent + "}\n";
200 MessageReader sub_reader(this);
201 if (!reader->PopDictEntry(&sub_reader))
202 return kBrokenMessage;
203 output += indent + "dict entry {\n";
204 output += ToStringInternal(indent + " ", &sub_reader);
205 output += indent + "}\n";
209 MessageReader sub_reader(this);
210 if (!reader->PopVariant(&sub_reader))
211 return kBrokenMessage;
212 output += indent + "variant ";
213 output += ToStringInternal(indent + " ", &sub_reader);
217 CHECK(IsDBusTypeUnixFdSupported());
219 base::ScopedFD file_descriptor;
220 if (!reader->PopFileDescriptor(&file_descriptor))
221 return kBrokenMessage;
223 indent + "fd#" + base::NumberToString(file_descriptor.get()) + "\n";
227 LOG(FATAL) << "Unknown type: " << type;
233 // The returned string consists of message headers such as
234 // destination if any, followed by a blank line, and the message
235 // payload. For example, a MethodCall's ToString() will look like:
237 // destination: com.example.Service
238 // path: /com/example/Object
239 // interface: com.example.Interface
240 // member: SomeMethod
242 // string \"payload\"
244 std::string Message::ToString() {
246 return std::string();
248 // Generate headers first.
250 AppendStringHeader("message_type", GetMessageTypeAsString(), &headers);
251 AppendStringHeader("destination", GetDestination(), &headers);
252 AppendStringHeader("path", GetPath().value(), &headers);
253 AppendStringHeader("interface", GetInterface(), &headers);
254 AppendStringHeader("member", GetMember(), &headers);
255 AppendStringHeader("error_name", GetErrorName(), &headers);
256 AppendStringHeader("sender", GetSender(), &headers);
257 AppendStringHeader("signature", GetSignature(), &headers);
258 AppendUint32Header("serial", GetSerial(), &headers);
259 AppendUint32Header("reply_serial", GetReplySerial(), &headers);
261 // Generate the payload.
262 MessageReader reader(this);
263 return headers + "\n" + ToStringInternal(std::string(), &reader);
266 bool Message::SetDestination(const std::string& destination) {
267 if (!dbus_validate_bus_name(destination.c_str(), nullptr)) {
268 LOG(ERROR) << "Invalid bus name: " << destination;
271 return dbus_message_set_destination(raw_message_, destination.c_str());
274 bool Message::SetPath(const ObjectPath& path) {
275 if (!path.IsValid()) {
276 LOG(ERROR) << "Invalid path: " << path.value();
279 return dbus_message_set_path(raw_message_, path.value().c_str());
282 bool Message::SetInterface(const std::string& interface) {
283 if (!dbus_validate_interface(interface.c_str(), nullptr)) {
284 LOG(ERROR) << "Invalid interface: " << interface;
287 return dbus_message_set_interface(raw_message_, interface.c_str());
290 bool Message::SetMember(const std::string& member) {
291 if (!dbus_validate_member(member.c_str(), nullptr)) {
292 LOG(ERROR) << "Invalid member: " << member;
295 return dbus_message_set_member(raw_message_, member.c_str());
298 bool Message::SetErrorName(const std::string& error_name) {
299 if (!dbus_validate_error_name(error_name.c_str(), nullptr)) {
300 LOG(ERROR) << "Invalid error name: " << error_name;
303 return dbus_message_set_error_name(raw_message_, error_name.c_str());
306 bool Message::SetSender(const std::string& sender) {
307 if (!dbus_validate_bus_name(sender.c_str(), nullptr)) {
308 LOG(ERROR) << "Invalid bus name: " << sender;
311 return dbus_message_set_sender(raw_message_, sender.c_str());
314 void Message::SetSerial(uint32_t serial) {
315 dbus_message_set_serial(raw_message_, serial);
318 void Message::SetReplySerial(uint32_t reply_serial) {
319 dbus_message_set_reply_serial(raw_message_, reply_serial);
322 std::string Message::GetDestination() {
323 const char* destination = dbus_message_get_destination(raw_message_);
324 return destination ? destination : "";
327 ObjectPath Message::GetPath() {
328 const char* path = dbus_message_get_path(raw_message_);
329 return ObjectPath(path ? path : "");
332 std::string Message::GetInterface() {
333 const char* interface = dbus_message_get_interface(raw_message_);
334 return interface ? interface : "";
337 std::string Message::GetMember() {
338 const char* member = dbus_message_get_member(raw_message_);
339 return member ? member : "";
342 std::string Message::GetErrorName() {
343 const char* error_name = dbus_message_get_error_name(raw_message_);
344 return error_name ? error_name : "";
347 std::string Message::GetSender() {
348 const char* sender = dbus_message_get_sender(raw_message_);
349 return sender ? sender : "";
352 std::string Message::GetSignature() {
353 const char* signature = dbus_message_get_signature(raw_message_);
354 return signature ? signature : "";
357 uint32_t Message::GetSerial() {
358 return dbus_message_get_serial(raw_message_);
361 uint32_t Message::GetReplySerial() {
362 return dbus_message_get_reply_serial(raw_message_);
366 // MethodCall implementation.
369 MethodCall::MethodCall(const std::string& interface_name,
370 const std::string& method_name) {
371 Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
373 CHECK(SetInterface(interface_name));
374 CHECK(SetMember(method_name));
377 MethodCall::MethodCall() = default;
379 std::unique_ptr<MethodCall> MethodCall::FromRawMessage(
380 DBusMessage* raw_message) {
381 DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message));
383 std::unique_ptr<MethodCall> method_call(new MethodCall());
384 method_call->Init(raw_message);
389 // Signal implementation.
391 Signal::Signal(const std::string& interface_name,
392 const std::string& method_name) {
393 Init(dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL));
395 CHECK(SetInterface(interface_name));
396 CHECK(SetMember(method_name));
399 Signal::Signal() = default;
401 std::unique_ptr<Signal> Signal::FromRawMessage(DBusMessage* raw_message) {
402 DCHECK_EQ(DBUS_MESSAGE_TYPE_SIGNAL, dbus_message_get_type(raw_message));
404 std::unique_ptr<Signal> signal(new Signal());
405 signal->Init(raw_message);
410 // Response implementation.
413 Response::Response() = default;
415 std::unique_ptr<Response> Response::FromRawMessage(DBusMessage* raw_message) {
416 DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_RETURN,
417 dbus_message_get_type(raw_message));
419 std::unique_ptr<Response> response(new Response());
420 response->Init(raw_message);
424 std::unique_ptr<Response> Response::FromMethodCall(MethodCall* method_call) {
425 std::unique_ptr<Response> response(new Response());
426 response->Init(dbus_message_new_method_return(method_call->raw_message()));
430 std::unique_ptr<Response> Response::CreateEmpty() {
431 std::unique_ptr<Response> response(new Response());
432 response->Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN));
437 // ErrorResponse implementation.
440 ErrorResponse::ErrorResponse() = default;
442 std::unique_ptr<ErrorResponse> ErrorResponse::FromRawMessage(
443 DBusMessage* raw_message) {
444 DCHECK_EQ(DBUS_MESSAGE_TYPE_ERROR, dbus_message_get_type(raw_message));
446 std::unique_ptr<ErrorResponse> response(new ErrorResponse());
447 response->Init(raw_message);
451 std::unique_ptr<ErrorResponse> ErrorResponse::FromMethodCall(
452 MethodCall* method_call,
453 const std::string& error_name,
454 const std::string& error_message) {
455 std::unique_ptr<ErrorResponse> response(new ErrorResponse());
456 response->Init(dbus_message_new_error(
457 method_call->raw_message(), error_name.c_str(), error_message.c_str()));
462 // MessageWriter implementation.
465 MessageWriter::MessageWriter(Message* message)
466 : message_(message), container_is_open_(false) {
467 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_));
469 dbus_message_iter_init_append(message_->raw_message(), &raw_message_iter_);
472 MessageWriter::~MessageWriter() = default;
474 void MessageWriter::AppendByte(uint8_t value) {
475 AppendBasic(DBUS_TYPE_BYTE, &value);
478 void MessageWriter::AppendBool(bool value) {
479 // The size of dbus_bool_t and the size of bool are different. The
480 // former is always 4 per dbus-types.h, whereas the latter is usually 1.
481 // dbus_message_iter_append_basic() used in AppendBasic() expects four
482 // bytes for DBUS_TYPE_BOOLEAN, so we must pass a dbus_bool_t, instead
483 // of a bool, to AppendBasic().
484 dbus_bool_t dbus_value = value ? 1 : 0;
485 AppendBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
488 void MessageWriter::AppendInt16(int16_t value) {
489 AppendBasic(DBUS_TYPE_INT16, &value);
492 void MessageWriter::AppendUint16(uint16_t value) {
493 AppendBasic(DBUS_TYPE_UINT16, &value);
496 void MessageWriter::AppendInt32(int32_t value) {
497 AppendBasic(DBUS_TYPE_INT32, &value);
500 void MessageWriter::AppendUint32(uint32_t value) {
501 AppendBasic(DBUS_TYPE_UINT32, &value);
504 void MessageWriter::AppendInt64(int64_t value) {
505 AppendBasic(DBUS_TYPE_INT64, &value);
508 void MessageWriter::AppendUint64(uint64_t value) {
509 AppendBasic(DBUS_TYPE_UINT64, &value);
512 void MessageWriter::AppendDouble(double value) {
513 AppendBasic(DBUS_TYPE_DOUBLE, &value);
516 void MessageWriter::AppendString(base::StringPiece value) {
517 // D-Bus Specification (0.19) says a string "must be valid UTF-8".
518 CHECK(base::IsStringUTF8(value));
519 const char* pointer = value.data() ? value.data() : "";
520 AppendBasic(DBUS_TYPE_STRING, &pointer);
521 // TODO(satorux): It may make sense to return an error here, as the
522 // input string can be large. If needed, we could add something like
523 // bool AppendStringWithErrorChecking().
526 void MessageWriter::AppendObjectPath(const ObjectPath& value) {
527 CHECK(value.IsValid());
528 const char* pointer = value.value().c_str();
529 AppendBasic(DBUS_TYPE_OBJECT_PATH, &pointer);
532 // Ideally, client shouldn't need to supply the signature string, but
533 // the underlying D-Bus library requires us to supply this before
534 // appending contents to array and variant. It's technically possible
535 // for us to design API that doesn't require the signature but it will
536 // complicate the implementation so we decided to have the signature
537 // parameter. Hopefully, variants are less used in request messages from
538 // client side than response message from server side, so this should
539 // not be a big issue.
540 void MessageWriter::OpenArray(const std::string& signature,
541 MessageWriter* writer) {
542 DCHECK(!container_is_open_);
544 const bool success = dbus_message_iter_open_container(
545 &raw_message_iter_, DBUS_TYPE_ARRAY, signature.c_str(),
546 &writer->raw_message_iter_);
547 CHECK(success) << "Unable to allocate memory";
548 container_is_open_ = true;
551 void MessageWriter::OpenVariant(const std::string& signature,
552 MessageWriter* writer) {
553 DCHECK(!container_is_open_);
555 const bool success = dbus_message_iter_open_container(
556 &raw_message_iter_, DBUS_TYPE_VARIANT, signature.c_str(),
557 &writer->raw_message_iter_);
558 CHECK(success) << "Unable to allocate memory";
559 container_is_open_ = true;
562 void MessageWriter::OpenStruct(MessageWriter* writer) {
563 DCHECK(!container_is_open_);
566 dbus_message_iter_open_container(&raw_message_iter_, DBUS_TYPE_STRUCT,
567 nullptr, // Signature should be nullptr.
568 &writer->raw_message_iter_);
569 CHECK(success) << "Unable to allocate memory";
570 container_is_open_ = true;
573 void MessageWriter::OpenDictEntry(MessageWriter* writer) {
574 DCHECK(!container_is_open_);
577 dbus_message_iter_open_container(&raw_message_iter_, DBUS_TYPE_DICT_ENTRY,
578 nullptr, // Signature should be nullptr.
579 &writer->raw_message_iter_);
580 CHECK(success) << "Unable to allocate memory";
581 container_is_open_ = true;
584 void MessageWriter::CloseContainer(MessageWriter* writer) {
585 DCHECK(container_is_open_);
587 const bool success = dbus_message_iter_close_container(
588 &raw_message_iter_, &writer->raw_message_iter_);
589 CHECK(success) << "Unable to allocate memory";
590 container_is_open_ = false;
593 void MessageWriter::AppendArrayOfBytes(const uint8_t* values, size_t length) {
594 DCHECK(!container_is_open_);
595 MessageWriter array_writer(message_);
596 OpenArray("y", &array_writer);
597 const bool success = dbus_message_iter_append_fixed_array(
598 &(array_writer.raw_message_iter_), DBUS_TYPE_BYTE, &values,
599 static_cast<int>(length));
600 CHECK(success) << "Unable to allocate memory";
601 CloseContainer(&array_writer);
604 void MessageWriter::AppendArrayOfInt32s(const int32_t* values, size_t length) {
605 DCHECK(!container_is_open_);
606 MessageWriter array_writer(message_);
607 OpenArray("i", &array_writer);
608 const bool success = dbus_message_iter_append_fixed_array(
609 &(array_writer.raw_message_iter_), DBUS_TYPE_INT32, &values,
610 static_cast<int>(length));
611 CHECK(success) << "Unable to allocate memory";
612 CloseContainer(&array_writer);
615 void MessageWriter::AppendArrayOfUint32s(const uint32_t* values,
617 DCHECK(!container_is_open_);
618 MessageWriter array_writer(message_);
619 OpenArray("u", &array_writer);
620 const bool success = dbus_message_iter_append_fixed_array(
621 &(array_writer.raw_message_iter_), DBUS_TYPE_UINT32, &values,
622 static_cast<int>(length));
623 CHECK(success) << "Unable to allocate memory";
624 CloseContainer(&array_writer);
627 void MessageWriter::AppendArrayOfDoubles(const double* values, size_t length) {
628 DCHECK(!container_is_open_);
629 MessageWriter array_writer(message_);
630 OpenArray("d", &array_writer);
631 const bool success = dbus_message_iter_append_fixed_array(
632 &(array_writer.raw_message_iter_), DBUS_TYPE_DOUBLE, &values,
633 static_cast<int>(length));
634 CHECK(success) << "Unable to allocate memory";
635 CloseContainer(&array_writer);
638 void MessageWriter::AppendArrayOfStrings(
639 const std::vector<std::string>& strings) {
640 DCHECK(!container_is_open_);
641 MessageWriter array_writer(message_);
642 OpenArray("s", &array_writer);
643 for (size_t i = 0; i < strings.size(); ++i) {
644 array_writer.AppendString(strings[i]);
646 CloseContainer(&array_writer);
649 void MessageWriter::AppendArrayOfObjectPaths(
650 const std::vector<ObjectPath>& object_paths) {
651 DCHECK(!container_is_open_);
652 MessageWriter array_writer(message_);
653 OpenArray("o", &array_writer);
654 for (size_t i = 0; i < object_paths.size(); ++i) {
655 array_writer.AppendObjectPath(object_paths[i]);
657 CloseContainer(&array_writer);
660 bool MessageWriter::AppendProtoAsArrayOfBytes(
661 const google::protobuf::MessageLite& protobuf) {
662 std::string serialized_proto;
663 if (!protobuf.SerializeToString(&serialized_proto)) {
664 LOG(ERROR) << "Unable to serialize supplied protocol buffer";
667 AppendArrayOfBytes(reinterpret_cast<const uint8_t*>(serialized_proto.data()),
668 serialized_proto.size());
672 void MessageWriter::AppendVariantOfByte(uint8_t value) {
673 AppendVariantOfBasic(DBUS_TYPE_BYTE, &value);
676 void MessageWriter::AppendVariantOfBool(bool value) {
677 // See the comment at MessageWriter::AppendBool().
678 dbus_bool_t dbus_value = value;
679 AppendVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
682 void MessageWriter::AppendVariantOfInt16(int16_t value) {
683 AppendVariantOfBasic(DBUS_TYPE_INT16, &value);
686 void MessageWriter::AppendVariantOfUint16(uint16_t value) {
687 AppendVariantOfBasic(DBUS_TYPE_UINT16, &value);
690 void MessageWriter::AppendVariantOfInt32(int32_t value) {
691 AppendVariantOfBasic(DBUS_TYPE_INT32, &value);
694 void MessageWriter::AppendVariantOfUint32(uint32_t value) {
695 AppendVariantOfBasic(DBUS_TYPE_UINT32, &value);
698 void MessageWriter::AppendVariantOfInt64(int64_t value) {
699 AppendVariantOfBasic(DBUS_TYPE_INT64, &value);
702 void MessageWriter::AppendVariantOfUint64(uint64_t value) {
703 AppendVariantOfBasic(DBUS_TYPE_UINT64, &value);
706 void MessageWriter::AppendVariantOfDouble(double value) {
707 AppendVariantOfBasic(DBUS_TYPE_DOUBLE, &value);
710 void MessageWriter::AppendVariantOfString(const std::string& value) {
711 const char* pointer = value.c_str();
712 AppendVariantOfBasic(DBUS_TYPE_STRING, &pointer);
715 void MessageWriter::AppendVariantOfObjectPath(const ObjectPath& value) {
716 const char* pointer = value.value().c_str();
717 AppendVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &pointer);
720 void MessageWriter::AppendBasic(int dbus_type, const void* value) {
721 DCHECK(!container_is_open_);
724 dbus_message_iter_append_basic(&raw_message_iter_, dbus_type, value);
725 // dbus_message_iter_append_basic() fails only when there is not enough
726 // memory. We don't return this error as there is nothing we can do when
727 // it fails to allocate memory for a byte etc.
728 CHECK(success) << "Unable to allocate memory";
731 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) {
732 const std::string signature(1u, // length
733 base::checked_cast<char>(dbus_type));
734 MessageWriter variant_writer(message_);
735 OpenVariant(signature, &variant_writer);
736 variant_writer.AppendBasic(dbus_type, value);
737 CloseContainer(&variant_writer);
740 void MessageWriter::AppendFileDescriptor(int value) {
741 CHECK(IsDBusTypeUnixFdSupported());
742 AppendBasic(DBUS_TYPE_UNIX_FD, &value); // This duplicates the FD.
746 // MessageReader implementation.
749 MessageReader::MessageReader(Message* message) : message_(message) {
750 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_));
752 dbus_message_iter_init(message_->raw_message(), &raw_message_iter_);
755 MessageReader::~MessageReader() = default;
757 bool MessageReader::HasMoreData() {
758 const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_);
759 return dbus_type != DBUS_TYPE_INVALID;
762 bool MessageReader::PopByte(uint8_t* value) {
763 return PopBasic(DBUS_TYPE_BYTE, value);
766 bool MessageReader::PopBool(bool* value) {
767 // Like MessageWriter::AppendBool(), we should copy |value| to
768 // dbus_bool_t, as dbus_message_iter_get_basic() used in PopBasic()
769 // expects four bytes for DBUS_TYPE_BOOLEAN.
770 dbus_bool_t dbus_value = FALSE;
771 const bool success = PopBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
772 *value = static_cast<bool>(dbus_value);
776 bool MessageReader::PopInt16(int16_t* value) {
777 return PopBasic(DBUS_TYPE_INT16, value);
780 bool MessageReader::PopUint16(uint16_t* value) {
781 return PopBasic(DBUS_TYPE_UINT16, value);
784 bool MessageReader::PopInt32(int32_t* value) {
785 return PopBasic(DBUS_TYPE_INT32, value);
788 bool MessageReader::PopUint32(uint32_t* value) {
789 return PopBasic(DBUS_TYPE_UINT32, value);
792 bool MessageReader::PopInt64(int64_t* value) {
793 return PopBasic(DBUS_TYPE_INT64, value);
796 bool MessageReader::PopUint64(uint64_t* value) {
797 return PopBasic(DBUS_TYPE_UINT64, value);
800 bool MessageReader::PopDouble(double* value) {
801 return PopBasic(DBUS_TYPE_DOUBLE, value);
804 bool MessageReader::PopString(std::string* value) {
805 char* tmp_value = nullptr;
806 const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value);
808 value->assign(tmp_value);
812 bool MessageReader::PopObjectPath(ObjectPath* value) {
813 char* tmp_value = nullptr;
814 const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
816 *value = ObjectPath(tmp_value);
820 bool MessageReader::PopArray(MessageReader* sub_reader) {
821 return PopContainer(DBUS_TYPE_ARRAY, sub_reader);
824 bool MessageReader::PopStruct(MessageReader* sub_reader) {
825 return PopContainer(DBUS_TYPE_STRUCT, sub_reader);
828 bool MessageReader::PopDictEntry(MessageReader* sub_reader) {
829 return PopContainer(DBUS_TYPE_DICT_ENTRY, sub_reader);
832 bool MessageReader::PopVariant(MessageReader* sub_reader) {
833 return PopContainer(DBUS_TYPE_VARIANT, sub_reader);
836 bool MessageReader::PopArrayOfBytes(const uint8_t** bytes, size_t* length) {
837 MessageReader array_reader(message_);
838 if (!PopArray(&array_reader))
840 // An empty array is allowed.
841 if (!array_reader.HasMoreData()) {
846 if (!array_reader.CheckDataType(DBUS_TYPE_BYTE))
849 dbus_message_iter_get_fixed_array(&array_reader.raw_message_iter_, bytes,
851 *length = static_cast<size_t>(int_length);
855 bool MessageReader::PopArrayOfInt32s(const int32_t** signed_ints,
857 MessageReader array_reader(message_);
858 if (!PopArray(&array_reader))
860 // An empty array is allowed.
861 if (!array_reader.HasMoreData()) {
863 *signed_ints = nullptr;
866 if (!array_reader.CheckDataType(DBUS_TYPE_INT32))
869 dbus_message_iter_get_fixed_array(&array_reader.raw_message_iter_,
870 signed_ints, &int_length);
871 *length = static_cast<size_t>(int_length);
875 bool MessageReader::PopArrayOfUint32s(const uint32_t** unsigned_ints,
877 MessageReader array_reader(message_);
878 if (!PopArray(&array_reader))
880 // An empty array is allowed.
881 if (!array_reader.HasMoreData()) {
883 *unsigned_ints = nullptr;
886 if (!array_reader.CheckDataType(DBUS_TYPE_UINT32))
889 dbus_message_iter_get_fixed_array(&array_reader.raw_message_iter_,
890 unsigned_ints, &int_length);
891 *length = static_cast<size_t>(int_length);
895 bool MessageReader::PopArrayOfDoubles(const double** doubles, size_t* length) {
896 MessageReader array_reader(message_);
897 if (!PopArray(&array_reader))
899 if (!array_reader.HasMoreData()) {
904 if (!array_reader.CheckDataType(DBUS_TYPE_DOUBLE))
907 dbus_message_iter_get_fixed_array(&array_reader.raw_message_iter_, doubles,
909 *length = static_cast<size_t>(int_length);
913 bool MessageReader::PopArrayOfStrings(std::vector<std::string>* strings) {
915 MessageReader array_reader(message_);
916 if (!PopArray(&array_reader))
918 while (array_reader.HasMoreData()) {
920 if (!array_reader.PopString(&string))
922 strings->push_back(string);
927 bool MessageReader::PopArrayOfObjectPaths(
928 std::vector<ObjectPath>* object_paths) {
929 object_paths->clear();
930 MessageReader array_reader(message_);
931 if (!PopArray(&array_reader))
933 while (array_reader.HasMoreData()) {
934 ObjectPath object_path;
935 if (!array_reader.PopObjectPath(&object_path))
937 object_paths->push_back(object_path);
942 bool MessageReader::PopArrayOfBytesAsProto(
943 google::protobuf::MessageLite* protobuf) {
945 const char* serialized_buf = nullptr;
947 if (!PopArrayOfBytes(reinterpret_cast<const uint8_t**>(&serialized_buf),
949 LOG(ERROR) << "Error reading array of bytes";
952 if (!protobuf->ParseFromArray(serialized_buf, buf_size)) {
953 LOG(ERROR) << "Failed to parse protocol buffer from array";
959 bool MessageReader::PopVariantOfByte(uint8_t* value) {
960 return PopVariantOfBasic(DBUS_TYPE_BYTE, value);
963 bool MessageReader::PopVariantOfBool(bool* value) {
964 // See the comment at MessageReader::PopBool().
965 dbus_bool_t dbus_value = FALSE;
966 const bool success = PopVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
967 *value = static_cast<bool>(dbus_value);
971 bool MessageReader::PopVariantOfInt16(int16_t* value) {
972 return PopVariantOfBasic(DBUS_TYPE_INT16, value);
975 bool MessageReader::PopVariantOfUint16(uint16_t* value) {
976 return PopVariantOfBasic(DBUS_TYPE_UINT16, value);
979 bool MessageReader::PopVariantOfInt32(int32_t* value) {
980 return PopVariantOfBasic(DBUS_TYPE_INT32, value);
983 bool MessageReader::PopVariantOfUint32(uint32_t* value) {
984 return PopVariantOfBasic(DBUS_TYPE_UINT32, value);
987 bool MessageReader::PopVariantOfInt64(int64_t* value) {
988 return PopVariantOfBasic(DBUS_TYPE_INT64, value);
991 bool MessageReader::PopVariantOfUint64(uint64_t* value) {
992 return PopVariantOfBasic(DBUS_TYPE_UINT64, value);
995 bool MessageReader::PopVariantOfDouble(double* value) {
996 return PopVariantOfBasic(DBUS_TYPE_DOUBLE, value);
999 bool MessageReader::PopVariantOfString(std::string* value) {
1000 char* tmp_value = nullptr;
1001 const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value);
1003 value->assign(tmp_value);
1007 bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) {
1008 char* tmp_value = nullptr;
1009 const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
1011 *value = ObjectPath(tmp_value);
1015 Message::DataType MessageReader::GetDataType() {
1016 const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_);
1017 return static_cast<Message::DataType>(dbus_type);
1020 std::string MessageReader::GetDataSignature() {
1021 std::string signature;
1022 char* raw_signature = dbus_message_iter_get_signature(&raw_message_iter_);
1023 if (raw_signature) {
1024 signature = raw_signature;
1025 dbus_free(raw_signature);
1030 bool MessageReader::CheckDataType(int dbus_type) {
1031 const int actual_type = dbus_message_iter_get_arg_type(&raw_message_iter_);
1032 if (actual_type != dbus_type) {
1033 VLOG(1) << "Type " << dbus_type << " is expected but got " << actual_type;
1039 bool MessageReader::PopBasic(int dbus_type, void* value) {
1040 if (!CheckDataType(dbus_type))
1042 // dbus_message_iter_get_basic() here should always work, as we have
1043 // already checked the next item's data type in CheckDataType(). Note
1044 // that dbus_message_iter_get_basic() is a void function.
1045 dbus_message_iter_get_basic(&raw_message_iter_, value);
1047 dbus_message_iter_next(&raw_message_iter_);
1051 bool MessageReader::PopContainer(int dbus_type, MessageReader* sub_reader) {
1052 DCHECK_NE(this, sub_reader);
1054 if (!CheckDataType(dbus_type))
1056 dbus_message_iter_recurse(&raw_message_iter_, &sub_reader->raw_message_iter_);
1057 dbus_message_iter_next(&raw_message_iter_);
1061 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) {
1062 MessageReader variant_reader(message_);
1063 if (!PopVariant(&variant_reader))
1065 return variant_reader.PopBasic(dbus_type, value);
1068 bool MessageReader::PopFileDescriptor(base::ScopedFD* value) {
1069 CHECK(IsDBusTypeUnixFdSupported());
1072 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd);
1076 *value = base::ScopedFD(fd);