Update V8 to version 4.6.72.
[chromium-blink-merge.git] / dbus / message.cc
blob3b021e50195d31a470adc9a9ae776a6ed927d8b1
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"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/format_macros.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "dbus/object_path.h"
17 #if defined(USE_SYSTEM_PROTOBUF)
18 #include <google/protobuf/message_lite.h>
19 #else
20 #include "third_party/protobuf/src/google/protobuf/message_lite.h"
21 #endif
23 namespace {
25 // Appends the header name and the value to |output|, if the value is
26 // not empty.
27 void AppendStringHeader(const std::string& header_name,
28 const std::string& header_value,
29 std::string* output) {
30 if (!header_value.empty()) {
31 *output += header_name + ": " + header_value + "\n";
35 // Appends the header name and the value to |output|, if the value is
36 // nonzero.
37 void AppendUint32Header(const std::string& header_name,
38 uint32 header_value,
39 std::string* output) {
40 if (header_value != 0) {
41 *output += (header_name + ": " + base::StringPrintf("%u", header_value) +
42 "\n");
46 } // namespace
48 namespace dbus {
50 bool IsDBusTypeUnixFdSupported() {
51 int major = 0, minor = 0, micro = 0;
52 dbus_get_version(&major, &minor, &micro);
53 return major >= 1 && minor >= 4;
56 Message::Message()
57 : raw_message_(NULL) {
60 Message::~Message() {
61 if (raw_message_)
62 dbus_message_unref(raw_message_);
65 void Message::Init(DBusMessage* raw_message) {
66 DCHECK(!raw_message_);
67 raw_message_ = raw_message;
70 Message::MessageType Message::GetMessageType() {
71 if (!raw_message_)
72 return MESSAGE_INVALID;
73 const int type = dbus_message_get_type(raw_message_);
74 return static_cast<Message::MessageType>(type);
77 std::string Message::GetMessageTypeAsString() {
78 switch (GetMessageType()) {
79 case MESSAGE_INVALID:
80 return "MESSAGE_INVALID";
81 case MESSAGE_METHOD_CALL:
82 return "MESSAGE_METHOD_CALL";
83 case MESSAGE_METHOD_RETURN:
84 return "MESSAGE_METHOD_RETURN";
85 case MESSAGE_SIGNAL:
86 return "MESSAGE_SIGNAL";
87 case MESSAGE_ERROR:
88 return "MESSAGE_ERROR";
90 NOTREACHED();
91 return std::string();
94 std::string Message::ToStringInternal(const std::string& indent,
95 MessageReader* reader) {
96 const char* kBrokenMessage = "[broken message]";
97 std::string output;
98 while (reader->HasMoreData()) {
99 const DataType type = reader->GetDataType();
100 switch (type) {
101 case BYTE: {
102 uint8 value = 0;
103 if (!reader->PopByte(&value))
104 return kBrokenMessage;
105 output += indent + "byte " + base::IntToString(value) + "\n";
106 break;
108 case BOOL: {
109 bool value = false;
110 if (!reader->PopBool(&value))
111 return kBrokenMessage;
112 output += indent + "bool " + (value ? "true" : "false") + "\n";
113 break;
115 case INT16: {
116 int16 value = 0;
117 if (!reader->PopInt16(&value))
118 return kBrokenMessage;
119 output += indent + "int16 " + base::IntToString(value) + "\n";
120 break;
122 case UINT16: {
123 uint16 value = 0;
124 if (!reader->PopUint16(&value))
125 return kBrokenMessage;
126 output += indent + "uint16 " + base::IntToString(value) + "\n";
127 break;
129 case INT32: {
130 int32 value = 0;
131 if (!reader->PopInt32(&value))
132 return kBrokenMessage;
133 output += indent + "int32 " + base::IntToString(value) + "\n";
134 break;
136 case UINT32: {
137 uint32 value = 0;
138 if (!reader->PopUint32(&value))
139 return kBrokenMessage;
140 output += indent + "uint32 " + base::StringPrintf("%u", value) + "\n";
141 break;
143 case INT64: {
144 int64 value = 0;
145 if (!reader->PopInt64(&value))
146 return kBrokenMessage;
147 output += (indent + "int64 " +
148 base::StringPrintf("%" PRId64, value) + "\n");
149 break;
151 case UINT64: {
152 uint64 value = 0;
153 if (!reader->PopUint64(&value))
154 return kBrokenMessage;
155 output += (indent + "uint64 " +
156 base::StringPrintf("%" PRIu64, value) + "\n");
157 break;
159 case DOUBLE: {
160 double value = 0;
161 if (!reader->PopDouble(&value))
162 return kBrokenMessage;
163 output += indent + "double " + base::StringPrintf("%f", value) + "\n";
164 break;
166 case STRING: {
167 std::string value;
168 if (!reader->PopString(&value))
169 return kBrokenMessage;
170 // Truncate if the string is longer than the limit.
171 const size_t kTruncateLength = 100;
172 if (value.size() < kTruncateLength) {
173 output += indent + "string \"" + value + "\"\n";
174 } else {
175 std::string truncated;
176 base::TruncateUTF8ToByteSize(value, kTruncateLength, &truncated);
177 base::StringAppendF(&truncated, "... (%" PRIuS " bytes in total)",
178 value.size());
179 output += indent + "string \"" + truncated + "\"\n";
181 break;
183 case OBJECT_PATH: {
184 ObjectPath value;
185 if (!reader->PopObjectPath(&value))
186 return kBrokenMessage;
187 output += indent + "object_path \"" + value.value() + "\"\n";
188 break;
190 case ARRAY: {
191 MessageReader sub_reader(this);
192 if (!reader->PopArray(&sub_reader))
193 return kBrokenMessage;
194 output += indent + "array [\n";
195 output += ToStringInternal(indent + " ", &sub_reader);
196 output += indent + "]\n";
197 break;
199 case STRUCT: {
200 MessageReader sub_reader(this);
201 if (!reader->PopStruct(&sub_reader))
202 return kBrokenMessage;
203 output += indent + "struct {\n";
204 output += ToStringInternal(indent + " ", &sub_reader);
205 output += indent + "}\n";
206 break;
208 case DICT_ENTRY: {
209 MessageReader sub_reader(this);
210 if (!reader->PopDictEntry(&sub_reader))
211 return kBrokenMessage;
212 output += indent + "dict entry {\n";
213 output += ToStringInternal(indent + " ", &sub_reader);
214 output += indent + "}\n";
215 break;
217 case VARIANT: {
218 MessageReader sub_reader(this);
219 if (!reader->PopVariant(&sub_reader))
220 return kBrokenMessage;
221 output += indent + "variant ";
222 output += ToStringInternal(indent + " ", &sub_reader);
223 break;
225 case UNIX_FD: {
226 CHECK(IsDBusTypeUnixFdSupported());
228 FileDescriptor file_descriptor;
229 if (!reader->PopFileDescriptor(&file_descriptor))
230 return kBrokenMessage;
231 output += indent + "fd#" +
232 base::IntToString(file_descriptor.value()) + "\n";
233 break;
235 default:
236 LOG(FATAL) << "Unknown type: " << type;
239 return output;
242 // The returned string consists of message headers such as
243 // destination if any, followed by a blank line, and the message
244 // payload. For example, a MethodCall's ToString() will look like:
246 // destination: com.example.Service
247 // path: /com/example/Object
248 // interface: com.example.Interface
249 // member: SomeMethod
251 // string \"payload\"
252 // ...
253 std::string Message::ToString() {
254 if (!raw_message_)
255 return std::string();
257 // Generate headers first.
258 std::string headers;
259 AppendStringHeader("message_type", GetMessageTypeAsString(), &headers);
260 AppendStringHeader("destination", GetDestination(), &headers);
261 AppendStringHeader("path", GetPath().value(), &headers);
262 AppendStringHeader("interface", GetInterface(), &headers);
263 AppendStringHeader("member", GetMember(), &headers);
264 AppendStringHeader("error_name", GetErrorName(), &headers);
265 AppendStringHeader("sender", GetSender(), &headers);
266 AppendStringHeader("signature", GetSignature(), &headers);
267 AppendUint32Header("serial", GetSerial(), &headers);
268 AppendUint32Header("reply_serial", GetReplySerial(), &headers);
270 // Generate the payload.
271 MessageReader reader(this);
272 return headers + "\n" + ToStringInternal(std::string(), &reader);
275 bool Message::SetDestination(const std::string& destination) {
276 return dbus_message_set_destination(raw_message_, destination.c_str());
279 bool Message::SetPath(const ObjectPath& path) {
280 return dbus_message_set_path(raw_message_, path.value().c_str());
283 bool Message::SetInterface(const std::string& interface) {
284 return dbus_message_set_interface(raw_message_, interface.c_str());
287 bool Message::SetMember(const std::string& member) {
288 return dbus_message_set_member(raw_message_, member.c_str());
291 bool Message::SetErrorName(const std::string& error_name) {
292 return dbus_message_set_error_name(raw_message_, error_name.c_str());
295 bool Message::SetSender(const std::string& sender) {
296 return dbus_message_set_sender(raw_message_, sender.c_str());
299 void Message::SetSerial(uint32 serial) {
300 dbus_message_set_serial(raw_message_, serial);
303 void Message::SetReplySerial(uint32 reply_serial) {
304 dbus_message_set_reply_serial(raw_message_, reply_serial);
307 std::string Message::GetDestination() {
308 const char* destination = dbus_message_get_destination(raw_message_);
309 return destination ? destination : "";
312 ObjectPath Message::GetPath() {
313 const char* path = dbus_message_get_path(raw_message_);
314 return ObjectPath(path ? path : "");
317 std::string Message::GetInterface() {
318 const char* interface = dbus_message_get_interface(raw_message_);
319 return interface ? interface : "";
322 std::string Message::GetMember() {
323 const char* member = dbus_message_get_member(raw_message_);
324 return member ? member : "";
327 std::string Message::GetErrorName() {
328 const char* error_name = dbus_message_get_error_name(raw_message_);
329 return error_name ? error_name : "";
332 std::string Message::GetSender() {
333 const char* sender = dbus_message_get_sender(raw_message_);
334 return sender ? sender : "";
337 std::string Message::GetSignature() {
338 const char* signature = dbus_message_get_signature(raw_message_);
339 return signature ? signature : "";
342 uint32 Message::GetSerial() {
343 return dbus_message_get_serial(raw_message_);
346 uint32 Message::GetReplySerial() {
347 return dbus_message_get_reply_serial(raw_message_);
351 // MethodCall implementation.
354 MethodCall::MethodCall(const std::string& interface_name,
355 const std::string& method_name)
356 : Message() {
357 Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
359 CHECK(SetInterface(interface_name));
360 CHECK(SetMember(method_name));
363 MethodCall::MethodCall() : Message() {
366 MethodCall* MethodCall::FromRawMessage(DBusMessage* raw_message) {
367 DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message));
369 MethodCall* method_call = new MethodCall;
370 method_call->Init(raw_message);
371 return method_call;
375 // Signal implementation.
377 Signal::Signal(const std::string& interface_name,
378 const std::string& method_name)
379 : Message() {
380 Init(dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL));
382 CHECK(SetInterface(interface_name));
383 CHECK(SetMember(method_name));
386 Signal::Signal() : Message() {
389 Signal* Signal::FromRawMessage(DBusMessage* raw_message) {
390 DCHECK_EQ(DBUS_MESSAGE_TYPE_SIGNAL, dbus_message_get_type(raw_message));
392 Signal* signal = new Signal;
393 signal->Init(raw_message);
394 return signal;
398 // Response implementation.
401 Response::Response() : Message() {
404 scoped_ptr<Response> Response::FromRawMessage(DBusMessage* raw_message) {
405 DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_RETURN,
406 dbus_message_get_type(raw_message));
408 scoped_ptr<Response> response(new Response);
409 response->Init(raw_message);
410 return response.Pass();
413 scoped_ptr<Response> Response::FromMethodCall(MethodCall* method_call) {
414 scoped_ptr<Response> response(new Response);
415 response->Init(dbus_message_new_method_return(method_call->raw_message()));
416 return response.Pass();
419 scoped_ptr<Response> Response::CreateEmpty() {
420 scoped_ptr<Response> response(new Response);
421 response->Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN));
422 return response.Pass();
426 // ErrorResponse implementation.
429 ErrorResponse::ErrorResponse() : Response() {
432 scoped_ptr<ErrorResponse> ErrorResponse::FromRawMessage(
433 DBusMessage* raw_message) {
434 DCHECK_EQ(DBUS_MESSAGE_TYPE_ERROR, dbus_message_get_type(raw_message));
436 scoped_ptr<ErrorResponse> response(new ErrorResponse);
437 response->Init(raw_message);
438 return response.Pass();
441 scoped_ptr<ErrorResponse> ErrorResponse::FromMethodCall(
442 MethodCall* method_call,
443 const std::string& error_name,
444 const std::string& error_message) {
445 scoped_ptr<ErrorResponse> response(new ErrorResponse);
446 response->Init(dbus_message_new_error(method_call->raw_message(),
447 error_name.c_str(),
448 error_message.c_str()));
449 return response.Pass();
453 // MessageWriter implementation.
456 MessageWriter::MessageWriter(Message* message)
457 : message_(message),
458 container_is_open_(false) {
459 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_));
460 if (message)
461 dbus_message_iter_init_append(message_->raw_message(), &raw_message_iter_);
464 MessageWriter::~MessageWriter() {
467 void MessageWriter::AppendByte(uint8 value) {
468 AppendBasic(DBUS_TYPE_BYTE, &value);
471 void MessageWriter::AppendBool(bool value) {
472 // The size of dbus_bool_t and the size of bool are different. The
473 // former is always 4 per dbus-types.h, whereas the latter is usually 1.
474 // dbus_message_iter_append_basic() used in AppendBasic() expects four
475 // bytes for DBUS_TYPE_BOOLEAN, so we must pass a dbus_bool_t, instead
476 // of a bool, to AppendBasic().
477 dbus_bool_t dbus_value = value;
478 AppendBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
481 void MessageWriter::AppendInt16(int16 value) {
482 AppendBasic(DBUS_TYPE_INT16, &value);
485 void MessageWriter::AppendUint16(uint16 value) {
486 AppendBasic(DBUS_TYPE_UINT16, &value);
489 void MessageWriter::AppendInt32(int32 value) {
490 AppendBasic(DBUS_TYPE_INT32, &value);
493 void MessageWriter::AppendUint32(uint32 value) {
494 AppendBasic(DBUS_TYPE_UINT32, &value);
497 void MessageWriter::AppendInt64(int64 value) {
498 AppendBasic(DBUS_TYPE_INT64, &value);
501 void MessageWriter::AppendUint64(uint64 value) {
502 AppendBasic(DBUS_TYPE_UINT64, &value);
505 void MessageWriter::AppendDouble(double value) {
506 AppendBasic(DBUS_TYPE_DOUBLE, &value);
509 void MessageWriter::AppendString(const std::string& value) {
510 // D-Bus Specification (0.19) says a string "must be valid UTF-8".
511 CHECK(base::IsStringUTF8(value));
512 const char* pointer = value.c_str();
513 AppendBasic(DBUS_TYPE_STRING, &pointer);
514 // TODO(satorux): It may make sense to return an error here, as the
515 // input string can be large. If needed, we could add something like
516 // bool AppendStringWithErrorChecking().
519 void MessageWriter::AppendObjectPath(const ObjectPath& value) {
520 CHECK(value.IsValid());
521 const char* pointer = value.value().c_str();
522 AppendBasic(DBUS_TYPE_OBJECT_PATH, &pointer);
525 // Ideally, client shouldn't need to supply the signature string, but
526 // the underlying D-Bus library requires us to supply this before
527 // appending contents to array and variant. It's technically possible
528 // for us to design API that doesn't require the signature but it will
529 // complicate the implementation so we decided to have the signature
530 // parameter. Hopefully, variants are less used in request messages from
531 // client side than response message from server side, so this should
532 // not be a big issue.
533 void MessageWriter::OpenArray(const std::string& signature,
534 MessageWriter* writer) {
535 DCHECK(!container_is_open_);
537 const bool success = dbus_message_iter_open_container(
538 &raw_message_iter_,
539 DBUS_TYPE_ARRAY,
540 signature.c_str(),
541 &writer->raw_message_iter_);
542 CHECK(success) << "Unable to allocate memory";
543 container_is_open_ = true;
546 void MessageWriter::OpenVariant(const std::string& signature,
547 MessageWriter* writer) {
548 DCHECK(!container_is_open_);
550 const bool success = dbus_message_iter_open_container(
551 &raw_message_iter_,
552 DBUS_TYPE_VARIANT,
553 signature.c_str(),
554 &writer->raw_message_iter_);
555 CHECK(success) << "Unable to allocate memory";
556 container_is_open_ = true;
559 void MessageWriter::OpenStruct(MessageWriter* writer) {
560 DCHECK(!container_is_open_);
562 const bool success = dbus_message_iter_open_container(
563 &raw_message_iter_,
564 DBUS_TYPE_STRUCT,
565 NULL, // Signature should be NULL.
566 &writer->raw_message_iter_);
567 CHECK(success) << "Unable to allocate memory";
568 container_is_open_ = true;
571 void MessageWriter::OpenDictEntry(MessageWriter* writer) {
572 DCHECK(!container_is_open_);
574 const bool success = dbus_message_iter_open_container(
575 &raw_message_iter_,
576 DBUS_TYPE_DICT_ENTRY,
577 NULL, // Signature should be NULL.
578 &writer->raw_message_iter_);
579 CHECK(success) << "Unable to allocate memory";
580 container_is_open_ = true;
583 void MessageWriter::CloseContainer(MessageWriter* writer) {
584 DCHECK(container_is_open_);
586 const bool success = dbus_message_iter_close_container(
587 &raw_message_iter_, &writer->raw_message_iter_);
588 CHECK(success) << "Unable to allocate memory";
589 container_is_open_ = false;
592 void MessageWriter::AppendArrayOfBytes(const uint8* values, size_t length) {
593 DCHECK(!container_is_open_);
594 MessageWriter array_writer(message_);
595 OpenArray("y", &array_writer);
596 const bool success = dbus_message_iter_append_fixed_array(
597 &(array_writer.raw_message_iter_),
598 DBUS_TYPE_BYTE,
599 &values,
600 static_cast<int>(length));
601 CHECK(success) << "Unable to allocate memory";
602 CloseContainer(&array_writer);
605 void MessageWriter::AppendArrayOfStrings(
606 const std::vector<std::string>& strings) {
607 DCHECK(!container_is_open_);
608 MessageWriter array_writer(message_);
609 OpenArray("s", &array_writer);
610 for (size_t i = 0; i < strings.size(); ++i) {
611 array_writer.AppendString(strings[i]);
613 CloseContainer(&array_writer);
616 void MessageWriter::AppendArrayOfObjectPaths(
617 const std::vector<ObjectPath>& object_paths) {
618 DCHECK(!container_is_open_);
619 MessageWriter array_writer(message_);
620 OpenArray("o", &array_writer);
621 for (size_t i = 0; i < object_paths.size(); ++i) {
622 array_writer.AppendObjectPath(object_paths[i]);
624 CloseContainer(&array_writer);
627 bool MessageWriter::AppendProtoAsArrayOfBytes(
628 const google::protobuf::MessageLite& protobuf) {
629 std::string serialized_proto;
630 if (!protobuf.SerializeToString(&serialized_proto)) {
631 LOG(ERROR) << "Unable to serialize supplied protocol buffer";
632 return false;
634 AppendArrayOfBytes(reinterpret_cast<const uint8*>(serialized_proto.data()),
635 serialized_proto.size());
636 return true;
639 void MessageWriter::AppendVariantOfByte(uint8 value) {
640 AppendVariantOfBasic(DBUS_TYPE_BYTE, &value);
643 void MessageWriter::AppendVariantOfBool(bool value) {
644 // See the comment at MessageWriter::AppendBool().
645 dbus_bool_t dbus_value = value;
646 AppendVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
649 void MessageWriter::AppendVariantOfInt16(int16 value) {
650 AppendVariantOfBasic(DBUS_TYPE_INT16, &value);
653 void MessageWriter::AppendVariantOfUint16(uint16 value) {
654 AppendVariantOfBasic(DBUS_TYPE_UINT16, &value);
657 void MessageWriter::AppendVariantOfInt32(int32 value) {
658 AppendVariantOfBasic(DBUS_TYPE_INT32, &value);
661 void MessageWriter::AppendVariantOfUint32(uint32 value) {
662 AppendVariantOfBasic(DBUS_TYPE_UINT32, &value);
665 void MessageWriter::AppendVariantOfInt64(int64 value) {
666 AppendVariantOfBasic(DBUS_TYPE_INT64, &value);
669 void MessageWriter::AppendVariantOfUint64(uint64 value) {
670 AppendVariantOfBasic(DBUS_TYPE_UINT64, &value);
673 void MessageWriter::AppendVariantOfDouble(double value) {
674 AppendVariantOfBasic(DBUS_TYPE_DOUBLE, &value);
677 void MessageWriter::AppendVariantOfString(const std::string& value) {
678 const char* pointer = value.c_str();
679 AppendVariantOfBasic(DBUS_TYPE_STRING, &pointer);
682 void MessageWriter::AppendVariantOfObjectPath(const ObjectPath& value) {
683 const char* pointer = value.value().c_str();
684 AppendVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &pointer);
687 void MessageWriter::AppendBasic(int dbus_type, const void* value) {
688 DCHECK(!container_is_open_);
690 const bool success = dbus_message_iter_append_basic(
691 &raw_message_iter_, dbus_type, value);
692 // dbus_message_iter_append_basic() fails only when there is not enough
693 // memory. We don't return this error as there is nothing we can do when
694 // it fails to allocate memory for a byte etc.
695 CHECK(success) << "Unable to allocate memory";
698 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) {
699 const std::string signature = base::StringPrintf("%c", dbus_type);
700 MessageWriter variant_writer(message_);
701 OpenVariant(signature, &variant_writer);
702 variant_writer.AppendBasic(dbus_type, value);
703 CloseContainer(&variant_writer);
706 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) {
707 CHECK(IsDBusTypeUnixFdSupported());
709 if (!value.is_valid()) {
710 // NB: sending a directory potentially enables sandbox escape
711 LOG(FATAL) << "Attempt to pass invalid file descriptor";
713 int fd = value.value();
714 AppendBasic(DBUS_TYPE_UNIX_FD, &fd);
718 // MessageReader implementation.
721 MessageReader::MessageReader(Message* message)
722 : message_(message) {
723 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_));
724 if (message)
725 dbus_message_iter_init(message_->raw_message(), &raw_message_iter_);
729 MessageReader::~MessageReader() {
732 bool MessageReader::HasMoreData() {
733 const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_);
734 return dbus_type != DBUS_TYPE_INVALID;
737 bool MessageReader::PopByte(uint8* value) {
738 return PopBasic(DBUS_TYPE_BYTE, value);
741 bool MessageReader::PopBool(bool* value) {
742 // Like MessageWriter::AppendBool(), we should copy |value| to
743 // dbus_bool_t, as dbus_message_iter_get_basic() used in PopBasic()
744 // expects four bytes for DBUS_TYPE_BOOLEAN.
745 dbus_bool_t dbus_value = FALSE;
746 const bool success = PopBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
747 *value = static_cast<bool>(dbus_value);
748 return success;
751 bool MessageReader::PopInt16(int16* value) {
752 return PopBasic(DBUS_TYPE_INT16, value);
755 bool MessageReader::PopUint16(uint16* value) {
756 return PopBasic(DBUS_TYPE_UINT16, value);
759 bool MessageReader::PopInt32(int32* value) {
760 return PopBasic(DBUS_TYPE_INT32, value);
763 bool MessageReader::PopUint32(uint32* value) {
764 return PopBasic(DBUS_TYPE_UINT32, value);
767 bool MessageReader::PopInt64(int64* value) {
768 return PopBasic(DBUS_TYPE_INT64, value);
771 bool MessageReader::PopUint64(uint64* value) {
772 return PopBasic(DBUS_TYPE_UINT64, value);
775 bool MessageReader::PopDouble(double* value) {
776 return PopBasic(DBUS_TYPE_DOUBLE, value);
779 bool MessageReader::PopString(std::string* value) {
780 char* tmp_value = NULL;
781 const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value);
782 if (success)
783 value->assign(tmp_value);
784 return success;
787 bool MessageReader::PopObjectPath(ObjectPath* value) {
788 char* tmp_value = NULL;
789 const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
790 if (success)
791 *value = ObjectPath(tmp_value);
792 return success;
795 bool MessageReader::PopArray(MessageReader* sub_reader) {
796 return PopContainer(DBUS_TYPE_ARRAY, sub_reader);
799 bool MessageReader::PopStruct(MessageReader* sub_reader) {
800 return PopContainer(DBUS_TYPE_STRUCT, sub_reader);
803 bool MessageReader::PopDictEntry(MessageReader* sub_reader) {
804 return PopContainer(DBUS_TYPE_DICT_ENTRY, sub_reader);
807 bool MessageReader::PopVariant(MessageReader* sub_reader) {
808 return PopContainer(DBUS_TYPE_VARIANT, sub_reader);
811 bool MessageReader::PopArrayOfBytes(const uint8** bytes, size_t* length) {
812 MessageReader array_reader(message_);
813 if (!PopArray(&array_reader))
814 return false;
815 // An empty array is allowed.
816 if (!array_reader.HasMoreData()) {
817 *length = 0;
818 *bytes = NULL;
819 return true;
821 if (!array_reader.CheckDataType(DBUS_TYPE_BYTE))
822 return false;
823 int int_length = 0;
824 dbus_message_iter_get_fixed_array(&array_reader.raw_message_iter_,
825 bytes,
826 &int_length);
827 *length = static_cast<int>(int_length);
828 return true;
831 bool MessageReader::PopArrayOfStrings(
832 std::vector<std::string> *strings) {
833 strings->clear();
834 MessageReader array_reader(message_);
835 if (!PopArray(&array_reader))
836 return false;
837 while (array_reader.HasMoreData()) {
838 std::string string;
839 if (!array_reader.PopString(&string))
840 return false;
841 strings->push_back(string);
843 return true;
846 bool MessageReader::PopArrayOfObjectPaths(
847 std::vector<ObjectPath> *object_paths) {
848 object_paths->clear();
849 MessageReader array_reader(message_);
850 if (!PopArray(&array_reader))
851 return false;
852 while (array_reader.HasMoreData()) {
853 ObjectPath object_path;
854 if (!array_reader.PopObjectPath(&object_path))
855 return false;
856 object_paths->push_back(object_path);
858 return true;
861 bool MessageReader::PopArrayOfBytesAsProto(
862 google::protobuf::MessageLite* protobuf) {
863 DCHECK(protobuf != NULL);
864 const char* serialized_buf = NULL;
865 size_t buf_size = 0;
866 if (!PopArrayOfBytes(
867 reinterpret_cast<const uint8**>(&serialized_buf), &buf_size)) {
868 LOG(ERROR) << "Error reading array of bytes";
869 return false;
871 if (!protobuf->ParseFromArray(serialized_buf, buf_size)) {
872 LOG(ERROR) << "Failed to parse protocol buffer from array";
873 return false;
875 return true;
878 bool MessageReader::PopVariantOfByte(uint8* value) {
879 return PopVariantOfBasic(DBUS_TYPE_BYTE, value);
882 bool MessageReader::PopVariantOfBool(bool* value) {
883 // See the comment at MessageReader::PopBool().
884 dbus_bool_t dbus_value = FALSE;
885 const bool success = PopVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
886 *value = static_cast<bool>(dbus_value);
887 return success;
890 bool MessageReader::PopVariantOfInt16(int16* value) {
891 return PopVariantOfBasic(DBUS_TYPE_INT16, value);
894 bool MessageReader::PopVariantOfUint16(uint16* value) {
895 return PopVariantOfBasic(DBUS_TYPE_UINT16, value);
898 bool MessageReader::PopVariantOfInt32(int32* value) {
899 return PopVariantOfBasic(DBUS_TYPE_INT32, value);
902 bool MessageReader::PopVariantOfUint32(uint32* value) {
903 return PopVariantOfBasic(DBUS_TYPE_UINT32, value);
906 bool MessageReader::PopVariantOfInt64(int64* value) {
907 return PopVariantOfBasic(DBUS_TYPE_INT64, value);
910 bool MessageReader::PopVariantOfUint64(uint64* value) {
911 return PopVariantOfBasic(DBUS_TYPE_UINT64, value);
914 bool MessageReader::PopVariantOfDouble(double* value) {
915 return PopVariantOfBasic(DBUS_TYPE_DOUBLE, value);
918 bool MessageReader::PopVariantOfString(std::string* value) {
919 char* tmp_value = NULL;
920 const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value);
921 if (success)
922 value->assign(tmp_value);
923 return success;
926 bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) {
927 char* tmp_value = NULL;
928 const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
929 if (success)
930 *value = ObjectPath(tmp_value);
931 return success;
934 Message::DataType MessageReader::GetDataType() {
935 const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_);
936 return static_cast<Message::DataType>(dbus_type);
939 std::string MessageReader::GetDataSignature() {
940 std::string signature;
941 char* raw_signature = dbus_message_iter_get_signature(&raw_message_iter_);
942 if (raw_signature) {
943 signature = raw_signature;
944 dbus_free(raw_signature);
946 return signature;
949 bool MessageReader::CheckDataType(int dbus_type) {
950 const int actual_type = dbus_message_iter_get_arg_type(&raw_message_iter_);
951 if (actual_type != dbus_type) {
952 VLOG(1) << "Type " << dbus_type << " is expected but got "
953 << actual_type;
954 return false;
956 return true;
959 bool MessageReader::PopBasic(int dbus_type, void* value) {
960 if (!CheckDataType(dbus_type))
961 return false;
962 // dbus_message_iter_get_basic() here should always work, as we have
963 // already checked the next item's data type in CheckDataType(). Note
964 // that dbus_message_iter_get_basic() is a void function.
965 dbus_message_iter_get_basic(&raw_message_iter_, value);
966 DCHECK(value);
967 dbus_message_iter_next(&raw_message_iter_);
968 return true;
971 bool MessageReader::PopContainer(int dbus_type, MessageReader* sub_reader) {
972 DCHECK_NE(this, sub_reader);
974 if (!CheckDataType(dbus_type))
975 return false;
976 dbus_message_iter_recurse(&raw_message_iter_,
977 &sub_reader->raw_message_iter_);
978 dbus_message_iter_next(&raw_message_iter_);
979 return true;
982 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) {
983 MessageReader variant_reader(message_);
984 if (!PopVariant(&variant_reader))
985 return false;
986 return variant_reader.PopBasic(dbus_type, value);
989 bool MessageReader::PopFileDescriptor(FileDescriptor* value) {
990 CHECK(IsDBusTypeUnixFdSupported());
992 int fd = -1;
993 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd);
994 if (!success)
995 return false;
997 value->PutValue(fd);
998 // NB: the caller must check validity before using the value
999 return true;
1002 } // namespace dbus