Check for NULL extension_service and extension_process_map for Android platform
[chromium-blink-merge.git] / dbus / message.cc
blobaf3c1c575550ad6f64e1eb1e6cfa9164d2925d0c
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/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"
17 namespace {
19 // Appends the header name and the value to |output|, if the value is
20 // not empty.
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
30 // nonzero.
31 void AppendUint32Header(const std::string& header_name,
32 uint32 header_value,
33 std::string* output) {
34 if (header_value != 0) {
35 *output += (header_name + ": " + base::StringPrintf("%u", header_value) +
36 "\n");
40 } // namespace
42 namespace dbus {
44 bool IsDBusTypeUnixFdSupported() {
45 int major = 0, minor = 0, micro = 0;
46 dbus_get_version(&major, &minor, &micro);
47 return major >= 1 && minor >= 4;
50 Message::Message()
51 : raw_message_(NULL) {
54 Message::~Message() {
55 if (raw_message_)
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() {
65 if (!raw_message_)
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()) {
73 case MESSAGE_INVALID:
74 return "MESSAGE_INVALID";
75 case MESSAGE_METHOD_CALL:
76 return "MESSAGE_METHOD_CALL";
77 case MESSAGE_METHOD_RETURN:
78 return "MESSAGE_METHOD_RETURN";
79 case MESSAGE_SIGNAL:
80 return "MESSAGE_SIGNAL";
81 case MESSAGE_ERROR:
82 return "MESSAGE_ERROR";
84 NOTREACHED();
85 return "";
88 std::string Message::ToStringInternal(const std::string& indent,
89 MessageReader* reader) {
90 const char* kBrokenMessage = "[broken message]";
91 std::string output;
92 while (reader->HasMoreData()) {
93 const DataType type = reader->GetDataType();
94 switch (type) {
95 case BYTE: {
96 uint8 value = 0;
97 if (!reader->PopByte(&value))
98 return kBrokenMessage;
99 output += indent + "byte " + base::StringPrintf("%d", value) + "\n";
100 break;
102 case BOOL: {
103 bool value = false;
104 if (!reader->PopBool(&value))
105 return kBrokenMessage;
106 output += indent + "bool " + (value ? "true" : "false") + "\n";
107 break;
109 case INT16: {
110 int16 value = 0;
111 if (!reader->PopInt16(&value))
112 return kBrokenMessage;
113 output += indent + "int16 " + base::StringPrintf("%d", value) + "\n";
114 break;
116 case UINT16: {
117 uint16 value = 0;
118 if (!reader->PopUint16(&value))
119 return kBrokenMessage;
120 output += indent + "uint16 " + base::StringPrintf("%d", value) + "\n";
121 break;
123 case INT32: {
124 int32 value = 0;
125 if (!reader->PopInt32(&value))
126 return kBrokenMessage;
127 output += indent + "int32 " + base::StringPrintf("%d", value) + "\n";
128 break;
130 case UINT32: {
131 uint32 value = 0;
132 if (!reader->PopUint32(&value))
133 return kBrokenMessage;
134 output += indent + "uint32 " + base::StringPrintf("%u", value) + "\n";
135 break;
137 case INT64: {
138 int64 value = 0;
139 if (!reader->PopInt64(&value))
140 return kBrokenMessage;
141 output += (indent + "int64 " +
142 base::StringPrintf("%" PRId64, value) + "\n");
143 break;
145 case UINT64: {
146 uint64 value = 0;
147 if (!reader->PopUint64(&value))
148 return kBrokenMessage;
149 output += (indent + "uint64 " +
150 base::StringPrintf("%" PRIu64, value) + "\n");
151 break;
153 case DOUBLE: {
154 double value = 0;
155 if (!reader->PopDouble(&value))
156 return kBrokenMessage;
157 output += indent + "double " + base::StringPrintf("%f", value) + "\n";
158 break;
160 case STRING: {
161 std::string value;
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";
168 } else {
169 std::string truncated;
170 TruncateUTF8ToByteSize(value, kTruncateLength, &truncated);
171 base::StringAppendF(&truncated, "... (%"PRIuS" bytes in total)",
172 value.size());
173 output += indent + "string \"" + truncated + "\"\n";
175 break;
177 case OBJECT_PATH: {
178 ObjectPath value;
179 if (!reader->PopObjectPath(&value))
180 return kBrokenMessage;
181 output += indent + "object_path \"" + value.value() + "\"\n";
182 break;
184 case ARRAY: {
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";
191 break;
193 case STRUCT: {
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";
200 break;
202 case DICT_ENTRY: {
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";
209 break;
211 case VARIANT: {
212 MessageReader sub_reader(this);
213 if (!reader->PopVariant(&sub_reader))
214 return kBrokenMessage;
215 output += indent + "variant ";
216 output += ToStringInternal(indent + " ", &sub_reader);
217 break;
219 case UNIX_FD: {
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";
227 break;
229 default:
230 LOG(FATAL) << "Unknown type: " << type;
233 return output;
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\"
246 // ...
247 std::string Message::ToString() {
248 if (!raw_message_)
249 return "";
251 // Generate headers first.
252 std::string headers;
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)
350 : Message() {
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);
365 return method_call;
369 // Signal implementation.
371 Signal::Signal(const std::string& interface_name,
372 const std::string& method_name)
373 : Message() {
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);
388 return signal;
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);
404 return response;
407 Response* Response::FromMethodCall(MethodCall* method_call) {
408 Response* response = new Response;
409 response->Init(dbus_message_new_method_return(method_call->raw_message()));
410 return response;
413 Response* Response::CreateEmpty() {
414 Response* response = new Response;
415 response->Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN));
416 return response;
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);
430 return response;
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(),
439 error_name.c_str(),
440 error_message.c_str()));
441 return response;
445 // MessageWriter implementation.
448 MessageWriter::MessageWriter(Message* message) :
449 message_(message),
450 container_is_open_(false) {
451 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_));
452 if (message)
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(
530 &raw_message_iter_,
531 DBUS_TYPE_ARRAY,
532 signature.c_str(),
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(
543 &raw_message_iter_,
544 DBUS_TYPE_VARIANT,
545 signature.c_str(),
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(
555 &raw_message_iter_,
556 DBUS_TYPE_STRUCT,
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(
567 &raw_message_iter_,
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_),
590 DBUS_TYPE_BYTE,
591 &values,
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";
624 return false;
626 AppendArrayOfBytes(reinterpret_cast<const uint8*>(serialized_proto.data()),
627 serialized_proto.size());
628 return true;
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_));
716 if (message)
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);
740 return success;
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);
774 if (success)
775 value->assign(tmp_value);
776 return success;
779 bool MessageReader::PopObjectPath(ObjectPath* value) {
780 char* tmp_value = NULL;
781 const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
782 if (success)
783 *value = ObjectPath(tmp_value);
784 return success;
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))
806 return false;
807 // An empty array is allowed.
808 if (!array_reader.HasMoreData()) {
809 *length = 0;
810 *bytes = NULL;
811 return true;
813 if (!array_reader.CheckDataType(DBUS_TYPE_BYTE))
814 return false;
815 int int_length = 0;
816 dbus_message_iter_get_fixed_array(&array_reader.raw_message_iter_,
817 bytes,
818 &int_length);
819 *length = static_cast<int>(int_length);
820 return true;
823 bool MessageReader::PopArrayOfStrings(
824 std::vector<std::string> *strings) {
825 MessageReader array_reader(message_);
826 if (!PopArray(&array_reader))
827 return false;
828 while (array_reader.HasMoreData()) {
829 std::string string;
830 if (!array_reader.PopString(&string))
831 return false;
832 strings->push_back(string);
834 return true;
837 bool MessageReader::PopArrayOfObjectPaths(
838 std::vector<ObjectPath> *object_paths) {
839 MessageReader array_reader(message_);
840 if (!PopArray(&array_reader))
841 return false;
842 while (array_reader.HasMoreData()) {
843 ObjectPath object_path;
844 if (!array_reader.PopObjectPath(&object_path))
845 return false;
846 object_paths->push_back(object_path);
848 return true;
851 bool MessageReader::PopArrayOfBytesAsProto(
852 google::protobuf::MessageLite* protobuf) {
853 DCHECK(protobuf != NULL);
854 char* serialized_buf = NULL;
855 size_t buf_size = 0;
856 if (!PopArrayOfBytes(reinterpret_cast<uint8**>(&serialized_buf), &buf_size)) {
857 LOG(ERROR) << "Error reading array of bytes";
858 return false;
860 if (!protobuf->ParseFromArray(serialized_buf, buf_size)) {
861 LOG(ERROR) << "Failed to parse protocol buffer from array";
862 return false;
864 return true;
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);
876 return success;
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);
910 if (success)
911 value->assign(tmp_value);
912 return success;
915 bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) {
916 char* tmp_value = NULL;
917 const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
918 if (success)
919 *value = ObjectPath(tmp_value);
920 return success;
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 "
932 << actual_type;
933 return false;
935 return true;
938 bool MessageReader::PopBasic(int dbus_type, void* value) {
939 if (!CheckDataType(dbus_type))
940 return false;
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);
945 DCHECK(value);
946 dbus_message_iter_next(&raw_message_iter_);
947 return true;
950 bool MessageReader::PopContainer(int dbus_type, MessageReader* sub_reader) {
951 DCHECK_NE(this, sub_reader);
953 if (!CheckDataType(dbus_type))
954 return false;
955 dbus_message_iter_recurse(&raw_message_iter_,
956 &sub_reader->raw_message_iter_);
957 dbus_message_iter_next(&raw_message_iter_);
958 return true;
961 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) {
962 dbus::MessageReader variant_reader(message_);
963 if (!PopVariant(&variant_reader))
964 return false;
965 return variant_reader.PopBasic(dbus_type, value);
968 bool MessageReader::PopFileDescriptor(FileDescriptor* value) {
969 CHECK(IsDBusTypeUnixFdSupported());
971 int fd = -1;
972 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd);
973 if (!success)
974 return false;
976 value->PutValue(fd);
977 // NB: the caller must check validity before using the value
978 return true;
981 } // namespace dbus