1 // Copyright (c) 2012 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.
5 #include "dbus/message.h"
9 #include "base/basictypes.h"
10 #include "base/format_macros.h"
11 #include "base/logging.h"
12 #include "base/string_util.h"
13 #include "base/stringprintf.h"
14 #include "dbus/object_path.h"
15 #include "third_party/protobuf/src/google/protobuf/message_lite.h"
19 // Appends the header name and the value to |output|, if the value is
21 void AppendStringHeader(const std::string
& header_name
,
22 const std::string
& header_value
,
23 std::string
* output
) {
24 if (!header_value
.empty()) {
25 *output
+= header_name
+ ": " + header_value
+ "\n";
29 // Appends the header name and the value to |output|, if the value is
31 void AppendUint32Header(const std::string
& header_name
,
33 std::string
* output
) {
34 if (header_value
!= 0) {
35 *output
+= (header_name
+ ": " + base::StringPrintf("%u", header_value
) +
44 bool IsDBusTypeUnixFdSupported() {
45 int major
= 0, minor
= 0, micro
= 0;
46 dbus_get_version(&major
, &minor
, µ
);
47 return major
>= 1 && minor
>= 4;
51 : raw_message_(NULL
) {
56 dbus_message_unref(raw_message_
);
59 void Message::Init(DBusMessage
* raw_message
) {
60 DCHECK(!raw_message_
);
61 raw_message_
= raw_message
;
64 Message::MessageType
Message::GetMessageType() {
66 return MESSAGE_INVALID
;
67 const int type
= dbus_message_get_type(raw_message_
);
68 return static_cast<Message::MessageType
>(type
);
71 std::string
Message::GetMessageTypeAsString() {
72 switch (GetMessageType()) {
74 return "MESSAGE_INVALID";
75 case MESSAGE_METHOD_CALL
:
76 return "MESSAGE_METHOD_CALL";
77 case MESSAGE_METHOD_RETURN
:
78 return "MESSAGE_METHOD_RETURN";
80 return "MESSAGE_SIGNAL";
82 return "MESSAGE_ERROR";
88 std::string
Message::ToStringInternal(const std::string
& indent
,
89 MessageReader
* reader
) {
90 const char* kBrokenMessage
= "[broken message]";
92 while (reader
->HasMoreData()) {
93 const DataType type
= reader
->GetDataType();
97 if (!reader
->PopByte(&value
))
98 return kBrokenMessage
;
99 output
+= indent
+ "byte " + base::StringPrintf("%d", value
) + "\n";
104 if (!reader
->PopBool(&value
))
105 return kBrokenMessage
;
106 output
+= indent
+ "bool " + (value
? "true" : "false") + "\n";
111 if (!reader
->PopInt16(&value
))
112 return kBrokenMessage
;
113 output
+= indent
+ "int16 " + base::StringPrintf("%d", value
) + "\n";
118 if (!reader
->PopUint16(&value
))
119 return kBrokenMessage
;
120 output
+= indent
+ "uint16 " + base::StringPrintf("%d", value
) + "\n";
125 if (!reader
->PopInt32(&value
))
126 return kBrokenMessage
;
127 output
+= indent
+ "int32 " + base::StringPrintf("%d", value
) + "\n";
132 if (!reader
->PopUint32(&value
))
133 return kBrokenMessage
;
134 output
+= indent
+ "uint32 " + base::StringPrintf("%u", value
) + "\n";
139 if (!reader
->PopInt64(&value
))
140 return kBrokenMessage
;
141 output
+= (indent
+ "int64 " +
142 base::StringPrintf("%" PRId64
, value
) + "\n");
147 if (!reader
->PopUint64(&value
))
148 return kBrokenMessage
;
149 output
+= (indent
+ "uint64 " +
150 base::StringPrintf("%" PRIu64
, value
) + "\n");
155 if (!reader
->PopDouble(&value
))
156 return kBrokenMessage
;
157 output
+= indent
+ "double " + base::StringPrintf("%f", value
) + "\n";
162 if (!reader
->PopString(&value
))
163 return kBrokenMessage
;
164 // Truncate if the string is longer than the limit.
165 const size_t kTruncateLength
= 100;
166 if (value
.size() < kTruncateLength
) {
167 output
+= indent
+ "string \"" + value
+ "\"\n";
169 std::string truncated
;
170 TruncateUTF8ToByteSize(value
, kTruncateLength
, &truncated
);
171 base::StringAppendF(&truncated
, "... (%"PRIuS
" bytes in total)",
173 output
+= indent
+ "string \"" + truncated
+ "\"\n";
179 if (!reader
->PopObjectPath(&value
))
180 return kBrokenMessage
;
181 output
+= indent
+ "object_path \"" + value
.value() + "\"\n";
185 MessageReader
sub_reader(this);
186 if (!reader
->PopArray(&sub_reader
))
187 return kBrokenMessage
;
188 output
+= indent
+ "array [\n";
189 output
+= ToStringInternal(indent
+ " ", &sub_reader
);
190 output
+= indent
+ "]\n";
194 MessageReader
sub_reader(this);
195 if (!reader
->PopStruct(&sub_reader
))
196 return kBrokenMessage
;
197 output
+= indent
+ "struct {\n";
198 output
+= ToStringInternal(indent
+ " ", &sub_reader
);
199 output
+= indent
+ "}\n";
203 MessageReader
sub_reader(this);
204 if (!reader
->PopDictEntry(&sub_reader
))
205 return kBrokenMessage
;
206 output
+= indent
+ "dict entry {\n";
207 output
+= ToStringInternal(indent
+ " ", &sub_reader
);
208 output
+= indent
+ "}\n";
212 MessageReader
sub_reader(this);
213 if (!reader
->PopVariant(&sub_reader
))
214 return kBrokenMessage
;
215 output
+= indent
+ "variant ";
216 output
+= ToStringInternal(indent
+ " ", &sub_reader
);
220 CHECK(IsDBusTypeUnixFdSupported());
222 FileDescriptor file_descriptor
;
223 if (!reader
->PopFileDescriptor(&file_descriptor
))
224 return kBrokenMessage
;
225 output
+= indent
+ "fd#" +
226 base::StringPrintf("%d", file_descriptor
.value()) + "\n";
230 LOG(FATAL
) << "Unknown type: " << type
;
236 // The returned string consists of message headers such as
237 // destination if any, followed by a blank line, and the message
238 // payload. For example, a MethodCall's ToString() will look like:
240 // destination: com.example.Service
241 // path: /com/example/Object
242 // interface: com.example.Interface
243 // member: SomeMethod
245 // string \"payload\"
247 std::string
Message::ToString() {
251 // Generate headers first.
253 AppendStringHeader("message_type", GetMessageTypeAsString(), &headers
);
254 AppendStringHeader("destination", GetDestination(), &headers
);
255 AppendStringHeader("path", GetPath().value(), &headers
);
256 AppendStringHeader("interface", GetInterface(), &headers
);
257 AppendStringHeader("member", GetMember(), &headers
);
258 AppendStringHeader("error_name", GetErrorName(), &headers
);
259 AppendStringHeader("sender", GetSender(), &headers
);
260 AppendStringHeader("signature", GetSignature(), &headers
);
261 AppendUint32Header("serial", GetSerial(), &headers
);
262 AppendUint32Header("reply_serial", GetReplySerial(), &headers
);
264 // Generate the payload.
265 MessageReader
reader(this);
266 return headers
+ "\n" + ToStringInternal("", &reader
);
269 bool Message::SetDestination(const std::string
& destination
) {
270 return dbus_message_set_destination(raw_message_
, destination
.c_str());
273 bool Message::SetPath(const ObjectPath
& path
) {
274 return dbus_message_set_path(raw_message_
, path
.value().c_str());
277 bool Message::SetInterface(const std::string
& interface
) {
278 return dbus_message_set_interface(raw_message_
, interface
.c_str());
281 bool Message::SetMember(const std::string
& member
) {
282 return dbus_message_set_member(raw_message_
, member
.c_str());
285 bool Message::SetErrorName(const std::string
& error_name
) {
286 return dbus_message_set_error_name(raw_message_
, error_name
.c_str());
289 bool Message::SetSender(const std::string
& sender
) {
290 return dbus_message_set_sender(raw_message_
, sender
.c_str());
293 void Message::SetSerial(uint32 serial
) {
294 dbus_message_set_serial(raw_message_
, serial
);
297 void Message::SetReplySerial(uint32 reply_serial
) {
298 dbus_message_set_reply_serial(raw_message_
, reply_serial
);
301 std::string
Message::GetDestination() {
302 const char* destination
= dbus_message_get_destination(raw_message_
);
303 return destination
? destination
: "";
306 ObjectPath
Message::GetPath() {
307 const char* path
= dbus_message_get_path(raw_message_
);
308 return ObjectPath(path
? path
: "");
311 std::string
Message::GetInterface() {
312 const char* interface
= dbus_message_get_interface(raw_message_
);
313 return interface
? interface
: "";
316 std::string
Message::GetMember() {
317 const char* member
= dbus_message_get_member(raw_message_
);
318 return member
? member
: "";
321 std::string
Message::GetErrorName() {
322 const char* error_name
= dbus_message_get_error_name(raw_message_
);
323 return error_name
? error_name
: "";
326 std::string
Message::GetSender() {
327 const char* sender
= dbus_message_get_sender(raw_message_
);
328 return sender
? sender
: "";
331 std::string
Message::GetSignature() {
332 const char* signature
= dbus_message_get_signature(raw_message_
);
333 return signature
? signature
: "";
336 uint32
Message::GetSerial() {
337 return dbus_message_get_serial(raw_message_
);
340 uint32
Message::GetReplySerial() {
341 return dbus_message_get_reply_serial(raw_message_
);
345 // MethodCall implementation.
348 MethodCall::MethodCall(const std::string
& interface_name
,
349 const std::string
& method_name
)
351 Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL
));
353 CHECK(SetInterface(interface_name
));
354 CHECK(SetMember(method_name
));
357 MethodCall::MethodCall() : Message() {
360 MethodCall
* MethodCall::FromRawMessage(DBusMessage
* raw_message
) {
361 DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL
, dbus_message_get_type(raw_message
));
363 MethodCall
* method_call
= new MethodCall
;
364 method_call
->Init(raw_message
);
369 // Signal implementation.
371 Signal::Signal(const std::string
& interface_name
,
372 const std::string
& method_name
)
374 Init(dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL
));
376 CHECK(SetInterface(interface_name
));
377 CHECK(SetMember(method_name
));
380 Signal::Signal() : Message() {
383 Signal
* Signal::FromRawMessage(DBusMessage
* raw_message
) {
384 DCHECK_EQ(DBUS_MESSAGE_TYPE_SIGNAL
, dbus_message_get_type(raw_message
));
386 Signal
* signal
= new Signal
;
387 signal
->Init(raw_message
);
392 // Response implementation.
395 Response::Response() : Message() {
398 Response
* Response::FromRawMessage(DBusMessage
* raw_message
) {
399 DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_RETURN
,
400 dbus_message_get_type(raw_message
));
402 Response
* response
= new Response
;
403 response
->Init(raw_message
);
407 Response
* Response::FromMethodCall(MethodCall
* method_call
) {
408 Response
* response
= new Response
;
409 response
->Init(dbus_message_new_method_return(method_call
->raw_message()));
413 Response
* Response::CreateEmpty() {
414 Response
* response
= new Response
;
415 response
->Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN
));
419 // ErrorResponse implementation.
422 ErrorResponse::ErrorResponse() : Response() {
425 ErrorResponse
* ErrorResponse::FromRawMessage(DBusMessage
* raw_message
) {
426 DCHECK_EQ(DBUS_MESSAGE_TYPE_ERROR
, dbus_message_get_type(raw_message
));
428 ErrorResponse
* response
= new ErrorResponse
;
429 response
->Init(raw_message
);
433 ErrorResponse
* ErrorResponse::FromMethodCall(
434 MethodCall
* method_call
,
435 const std::string
& error_name
,
436 const std::string
& error_message
) {
437 ErrorResponse
* response
= new ErrorResponse
;
438 response
->Init(dbus_message_new_error(method_call
->raw_message(),
440 error_message
.c_str()));
445 // MessageWriter implementation.
448 MessageWriter::MessageWriter(Message
* message
) :
450 container_is_open_(false) {
451 memset(&raw_message_iter_
, 0, sizeof(raw_message_iter_
));
453 dbus_message_iter_init_append(message_
->raw_message(), &raw_message_iter_
);
456 MessageWriter::~MessageWriter() {
459 void MessageWriter::AppendByte(uint8 value
) {
460 AppendBasic(DBUS_TYPE_BYTE
, &value
);
463 void MessageWriter::AppendBool(bool value
) {
464 // The size of dbus_bool_t and the size of bool are different. The
465 // former is always 4 per dbus-types.h, whereas the latter is usually 1.
466 // dbus_message_iter_append_basic() used in AppendBasic() expects four
467 // bytes for DBUS_TYPE_BOOLEAN, so we must pass a dbus_bool_t, instead
468 // of a bool, to AppendBasic().
469 dbus_bool_t dbus_value
= value
;
470 AppendBasic(DBUS_TYPE_BOOLEAN
, &dbus_value
);
473 void MessageWriter::AppendInt16(int16 value
) {
474 AppendBasic(DBUS_TYPE_INT16
, &value
);
477 void MessageWriter::AppendUint16(uint16 value
) {
478 AppendBasic(DBUS_TYPE_UINT16
, &value
);
481 void MessageWriter::AppendInt32(int32 value
) {
482 AppendBasic(DBUS_TYPE_INT32
, &value
);
485 void MessageWriter::AppendUint32(uint32 value
) {
486 AppendBasic(DBUS_TYPE_UINT32
, &value
);
489 void MessageWriter::AppendInt64(int64 value
) {
490 AppendBasic(DBUS_TYPE_INT64
, &value
);
493 void MessageWriter::AppendUint64(uint64 value
) {
494 AppendBasic(DBUS_TYPE_UINT64
, &value
);
497 void MessageWriter::AppendDouble(double value
) {
498 AppendBasic(DBUS_TYPE_DOUBLE
, &value
);
501 void MessageWriter::AppendString(const std::string
& value
) {
502 // D-Bus Specification (0.19) says a string "must be valid UTF-8".
503 CHECK(IsStringUTF8(value
));
504 const char* pointer
= value
.c_str();
505 AppendBasic(DBUS_TYPE_STRING
, &pointer
);
506 // TODO(satorux): It may make sense to return an error here, as the
507 // input string can be large. If needed, we could add something like
508 // bool AppendStringWithErrorChecking().
511 void MessageWriter::AppendObjectPath(const ObjectPath
& value
) {
512 CHECK(value
.IsValid());
513 const char* pointer
= value
.value().c_str();
514 AppendBasic(DBUS_TYPE_OBJECT_PATH
, &pointer
);
517 // Ideally, client shouldn't need to supply the signature string, but
518 // the underlying D-Bus library requires us to supply this before
519 // appending contents to array and variant. It's technically possible
520 // for us to design API that doesn't require the signature but it will
521 // complicate the implementation so we decided to have the signature
522 // parameter. Hopefully, variants are less used in request messages from
523 // client side than response message from server side, so this should
524 // not be a big issue.
525 void MessageWriter::OpenArray(const std::string
& signature
,
526 MessageWriter
* writer
) {
527 DCHECK(!container_is_open_
);
529 const bool success
= dbus_message_iter_open_container(
533 &writer
->raw_message_iter_
);
534 CHECK(success
) << "Unable to allocate memory";
535 container_is_open_
= true;
538 void MessageWriter::OpenVariant(const std::string
& signature
,
539 MessageWriter
* writer
) {
540 DCHECK(!container_is_open_
);
542 const bool success
= dbus_message_iter_open_container(
546 &writer
->raw_message_iter_
);
547 CHECK(success
) << "Unable to allocate memory";
548 container_is_open_
= true;
551 void MessageWriter::OpenStruct(MessageWriter
* writer
) {
552 DCHECK(!container_is_open_
);
554 const bool success
= dbus_message_iter_open_container(
557 NULL
, // Signature should be NULL.
558 &writer
->raw_message_iter_
);
559 CHECK(success
) << "Unable to allocate memory";
560 container_is_open_
= true;
563 void MessageWriter::OpenDictEntry(MessageWriter
* writer
) {
564 DCHECK(!container_is_open_
);
566 const bool success
= dbus_message_iter_open_container(
568 DBUS_TYPE_DICT_ENTRY
,
569 NULL
, // Signature should be NULL.
570 &writer
->raw_message_iter_
);
571 CHECK(success
) << "Unable to allocate memory";
572 container_is_open_
= true;
575 void MessageWriter::CloseContainer(MessageWriter
* writer
) {
576 DCHECK(container_is_open_
);
578 const bool success
= dbus_message_iter_close_container(
579 &raw_message_iter_
, &writer
->raw_message_iter_
);
580 CHECK(success
) << "Unable to allocate memory";
581 container_is_open_
= false;
584 void MessageWriter::AppendArrayOfBytes(const uint8
* values
, size_t length
) {
585 DCHECK(!container_is_open_
);
586 MessageWriter
array_writer(message_
);
587 OpenArray("y", &array_writer
);
588 const bool success
= dbus_message_iter_append_fixed_array(
589 &(array_writer
.raw_message_iter_
),
592 static_cast<int>(length
));
593 CHECK(success
) << "Unable to allocate memory";
594 CloseContainer(&array_writer
);
597 void MessageWriter::AppendArrayOfStrings(
598 const std::vector
<std::string
>& strings
) {
599 DCHECK(!container_is_open_
);
600 MessageWriter
array_writer(message_
);
601 OpenArray("s", &array_writer
);
602 for (size_t i
= 0; i
< strings
.size(); ++i
) {
603 array_writer
.AppendString(strings
[i
]);
605 CloseContainer(&array_writer
);
608 void MessageWriter::AppendArrayOfObjectPaths(
609 const std::vector
<ObjectPath
>& object_paths
) {
610 DCHECK(!container_is_open_
);
611 MessageWriter
array_writer(message_
);
612 OpenArray("o", &array_writer
);
613 for (size_t i
= 0; i
< object_paths
.size(); ++i
) {
614 array_writer
.AppendObjectPath(object_paths
[i
]);
616 CloseContainer(&array_writer
);
619 bool MessageWriter::AppendProtoAsArrayOfBytes(
620 const google::protobuf::MessageLite
& protobuf
) {
621 std::string serialized_proto
;
622 if (!protobuf
.SerializeToString(&serialized_proto
)) {
623 LOG(ERROR
) << "Unable to serialize supplied protocol buffer";
626 AppendArrayOfBytes(reinterpret_cast<const uint8
*>(serialized_proto
.data()),
627 serialized_proto
.size());
631 void MessageWriter::AppendVariantOfByte(uint8 value
) {
632 AppendVariantOfBasic(DBUS_TYPE_BYTE
, &value
);
635 void MessageWriter::AppendVariantOfBool(bool value
) {
636 // See the comment at MessageWriter::AppendBool().
637 dbus_bool_t dbus_value
= value
;
638 AppendVariantOfBasic(DBUS_TYPE_BOOLEAN
, &dbus_value
);
641 void MessageWriter::AppendVariantOfInt16(int16 value
) {
642 AppendVariantOfBasic(DBUS_TYPE_INT16
, &value
);
645 void MessageWriter::AppendVariantOfUint16(uint16 value
) {
646 AppendVariantOfBasic(DBUS_TYPE_UINT16
, &value
);
649 void MessageWriter::AppendVariantOfInt32(int32 value
) {
650 AppendVariantOfBasic(DBUS_TYPE_INT32
, &value
);
653 void MessageWriter::AppendVariantOfUint32(uint32 value
) {
654 AppendVariantOfBasic(DBUS_TYPE_UINT32
, &value
);
657 void MessageWriter::AppendVariantOfInt64(int64 value
) {
658 AppendVariantOfBasic(DBUS_TYPE_INT64
, &value
);
661 void MessageWriter::AppendVariantOfUint64(uint64 value
) {
662 AppendVariantOfBasic(DBUS_TYPE_UINT64
, &value
);
665 void MessageWriter::AppendVariantOfDouble(double value
) {
666 AppendVariantOfBasic(DBUS_TYPE_DOUBLE
, &value
);
669 void MessageWriter::AppendVariantOfString(const std::string
& value
) {
670 const char* pointer
= value
.c_str();
671 AppendVariantOfBasic(DBUS_TYPE_STRING
, &pointer
);
674 void MessageWriter::AppendVariantOfObjectPath(const ObjectPath
& value
) {
675 const char* pointer
= value
.value().c_str();
676 AppendVariantOfBasic(DBUS_TYPE_OBJECT_PATH
, &pointer
);
679 void MessageWriter::AppendBasic(int dbus_type
, const void* value
) {
680 DCHECK(!container_is_open_
);
682 const bool success
= dbus_message_iter_append_basic(
683 &raw_message_iter_
, dbus_type
, value
);
684 // dbus_message_iter_append_basic() fails only when there is not enough
685 // memory. We don't return this error as there is nothing we can do when
686 // it fails to allocate memory for a byte etc.
687 CHECK(success
) << "Unable to allocate memory";
690 void MessageWriter::AppendVariantOfBasic(int dbus_type
, const void* value
) {
691 const std::string signature
= base::StringPrintf("%c", dbus_type
);
692 MessageWriter
variant_writer(message_
);
693 OpenVariant(signature
, &variant_writer
);
694 variant_writer
.AppendBasic(dbus_type
, value
);
695 CloseContainer(&variant_writer
);
698 void MessageWriter::AppendFileDescriptor(const FileDescriptor
& value
) {
699 CHECK(IsDBusTypeUnixFdSupported());
701 if (!value
.is_valid()) {
702 // NB: sending a directory potentially enables sandbox escape
703 LOG(FATAL
) << "Attempt to pass invalid file descriptor";
705 int fd
= value
.value();
706 AppendBasic(DBUS_TYPE_UNIX_FD
, &fd
);
710 // MessageReader implementation.
713 MessageReader::MessageReader(Message
* message
)
714 : message_(message
) {
715 memset(&raw_message_iter_
, 0, sizeof(raw_message_iter_
));
717 dbus_message_iter_init(message_
->raw_message(), &raw_message_iter_
);
721 MessageReader::~MessageReader() {
724 bool MessageReader::HasMoreData() {
725 const int dbus_type
= dbus_message_iter_get_arg_type(&raw_message_iter_
);
726 return dbus_type
!= DBUS_TYPE_INVALID
;
729 bool MessageReader::PopByte(uint8
* value
) {
730 return PopBasic(DBUS_TYPE_BYTE
, value
);
733 bool MessageReader::PopBool(bool* value
) {
734 // Like MessageWriter::AppendBool(), we should copy |value| to
735 // dbus_bool_t, as dbus_message_iter_get_basic() used in PopBasic()
736 // expects four bytes for DBUS_TYPE_BOOLEAN.
737 dbus_bool_t dbus_value
= FALSE
;
738 const bool success
= PopBasic(DBUS_TYPE_BOOLEAN
, &dbus_value
);
739 *value
= static_cast<bool>(dbus_value
);
743 bool MessageReader::PopInt16(int16
* value
) {
744 return PopBasic(DBUS_TYPE_INT16
, value
);
747 bool MessageReader::PopUint16(uint16
* value
) {
748 return PopBasic(DBUS_TYPE_UINT16
, value
);
751 bool MessageReader::PopInt32(int32
* value
) {
752 return PopBasic(DBUS_TYPE_INT32
, value
);
755 bool MessageReader::PopUint32(uint32
* value
) {
756 return PopBasic(DBUS_TYPE_UINT32
, value
);
759 bool MessageReader::PopInt64(int64
* value
) {
760 return PopBasic(DBUS_TYPE_INT64
, value
);
763 bool MessageReader::PopUint64(uint64
* value
) {
764 return PopBasic(DBUS_TYPE_UINT64
, value
);
767 bool MessageReader::PopDouble(double* value
) {
768 return PopBasic(DBUS_TYPE_DOUBLE
, value
);
771 bool MessageReader::PopString(std::string
* value
) {
772 char* tmp_value
= NULL
;
773 const bool success
= PopBasic(DBUS_TYPE_STRING
, &tmp_value
);
775 value
->assign(tmp_value
);
779 bool MessageReader::PopObjectPath(ObjectPath
* value
) {
780 char* tmp_value
= NULL
;
781 const bool success
= PopBasic(DBUS_TYPE_OBJECT_PATH
, &tmp_value
);
783 *value
= ObjectPath(tmp_value
);
787 bool MessageReader::PopArray(MessageReader
* sub_reader
) {
788 return PopContainer(DBUS_TYPE_ARRAY
, sub_reader
);
791 bool MessageReader::PopStruct(MessageReader
* sub_reader
) {
792 return PopContainer(DBUS_TYPE_STRUCT
, sub_reader
);
795 bool MessageReader::PopDictEntry(MessageReader
* sub_reader
) {
796 return PopContainer(DBUS_TYPE_DICT_ENTRY
, sub_reader
);
799 bool MessageReader::PopVariant(MessageReader
* sub_reader
) {
800 return PopContainer(DBUS_TYPE_VARIANT
, sub_reader
);
803 bool MessageReader::PopArrayOfBytes(uint8
** bytes
, size_t* length
) {
804 MessageReader
array_reader(message_
);
805 if (!PopArray(&array_reader
))
807 // An empty array is allowed.
808 if (!array_reader
.HasMoreData()) {
813 if (!array_reader
.CheckDataType(DBUS_TYPE_BYTE
))
816 dbus_message_iter_get_fixed_array(&array_reader
.raw_message_iter_
,
819 *length
= static_cast<int>(int_length
);
823 bool MessageReader::PopArrayOfStrings(
824 std::vector
<std::string
> *strings
) {
825 MessageReader
array_reader(message_
);
826 if (!PopArray(&array_reader
))
828 while (array_reader
.HasMoreData()) {
830 if (!array_reader
.PopString(&string
))
832 strings
->push_back(string
);
837 bool MessageReader::PopArrayOfObjectPaths(
838 std::vector
<ObjectPath
> *object_paths
) {
839 MessageReader
array_reader(message_
);
840 if (!PopArray(&array_reader
))
842 while (array_reader
.HasMoreData()) {
843 ObjectPath object_path
;
844 if (!array_reader
.PopObjectPath(&object_path
))
846 object_paths
->push_back(object_path
);
851 bool MessageReader::PopArrayOfBytesAsProto(
852 google::protobuf::MessageLite
* protobuf
) {
853 DCHECK(protobuf
!= NULL
);
854 char* serialized_buf
= NULL
;
856 if (!PopArrayOfBytes(reinterpret_cast<uint8
**>(&serialized_buf
), &buf_size
)) {
857 LOG(ERROR
) << "Error reading array of bytes";
860 if (!protobuf
->ParseFromArray(serialized_buf
, buf_size
)) {
861 LOG(ERROR
) << "Failed to parse protocol buffer from array";
867 bool MessageReader::PopVariantOfByte(uint8
* value
) {
868 return PopVariantOfBasic(DBUS_TYPE_BYTE
, value
);
871 bool MessageReader::PopVariantOfBool(bool* value
) {
872 // See the comment at MessageReader::PopBool().
873 dbus_bool_t dbus_value
= FALSE
;
874 const bool success
= PopVariantOfBasic(DBUS_TYPE_BOOLEAN
, &dbus_value
);
875 *value
= static_cast<bool>(dbus_value
);
879 bool MessageReader::PopVariantOfInt16(int16
* value
) {
880 return PopVariantOfBasic(DBUS_TYPE_INT16
, value
);
883 bool MessageReader::PopVariantOfUint16(uint16
* value
) {
884 return PopVariantOfBasic(DBUS_TYPE_UINT16
, value
);
887 bool MessageReader::PopVariantOfInt32(int32
* value
) {
888 return PopVariantOfBasic(DBUS_TYPE_INT32
, value
);
891 bool MessageReader::PopVariantOfUint32(uint32
* value
) {
892 return PopVariantOfBasic(DBUS_TYPE_UINT32
, value
);
895 bool MessageReader::PopVariantOfInt64(int64
* value
) {
896 return PopVariantOfBasic(DBUS_TYPE_INT64
, value
);
899 bool MessageReader::PopVariantOfUint64(uint64
* value
) {
900 return PopVariantOfBasic(DBUS_TYPE_UINT64
, value
);
903 bool MessageReader::PopVariantOfDouble(double* value
) {
904 return PopVariantOfBasic(DBUS_TYPE_DOUBLE
, value
);
907 bool MessageReader::PopVariantOfString(std::string
* value
) {
908 char* tmp_value
= NULL
;
909 const bool success
= PopVariantOfBasic(DBUS_TYPE_STRING
, &tmp_value
);
911 value
->assign(tmp_value
);
915 bool MessageReader::PopVariantOfObjectPath(ObjectPath
* value
) {
916 char* tmp_value
= NULL
;
917 const bool success
= PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH
, &tmp_value
);
919 *value
= ObjectPath(tmp_value
);
923 Message::DataType
MessageReader::GetDataType() {
924 const int dbus_type
= dbus_message_iter_get_arg_type(&raw_message_iter_
);
925 return static_cast<Message::DataType
>(dbus_type
);
928 bool MessageReader::CheckDataType(int dbus_type
) {
929 const int actual_type
= dbus_message_iter_get_arg_type(&raw_message_iter_
);
930 if (actual_type
!= dbus_type
) {
931 VLOG(1) << "Type " << dbus_type
<< " is expected but got "
938 bool MessageReader::PopBasic(int dbus_type
, void* value
) {
939 if (!CheckDataType(dbus_type
))
941 // dbus_message_iter_get_basic() here should always work, as we have
942 // already checked the next item's data type in CheckDataType(). Note
943 // that dbus_message_iter_get_basic() is a void function.
944 dbus_message_iter_get_basic(&raw_message_iter_
, value
);
946 dbus_message_iter_next(&raw_message_iter_
);
950 bool MessageReader::PopContainer(int dbus_type
, MessageReader
* sub_reader
) {
951 DCHECK_NE(this, sub_reader
);
953 if (!CheckDataType(dbus_type
))
955 dbus_message_iter_recurse(&raw_message_iter_
,
956 &sub_reader
->raw_message_iter_
);
957 dbus_message_iter_next(&raw_message_iter_
);
961 bool MessageReader::PopVariantOfBasic(int dbus_type
, void* value
) {
962 dbus::MessageReader
variant_reader(message_
);
963 if (!PopVariant(&variant_reader
))
965 return variant_reader
.PopBasic(dbus_type
, value
);
968 bool MessageReader::PopFileDescriptor(FileDescriptor
* value
) {
969 CHECK(IsDBusTypeUnixFdSupported());
972 const bool success
= PopBasic(DBUS_TYPE_UNIX_FD
, &fd
);
977 // NB: the caller must check validity before using the value