OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/message.h" | 5 #include "dbus/message.h" |
6 | 6 |
7 #include <string> | |
8 | |
7 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
8 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 // Appends the header name and the value to |output|, if the value is | 16 // Appends the header name and the value to |output|, if the value is |
15 // not empty. | 17 // not empty. |
16 static void AppendStringHeader(const std::string& header_name, | 18 static void AppendStringHeader(const std::string& header_name, |
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
585 const std::vector<std::string>& object_paths) { | 587 const std::vector<std::string>& object_paths) { |
586 DCHECK(!container_is_open_); | 588 DCHECK(!container_is_open_); |
587 MessageWriter array_writer(message_); | 589 MessageWriter array_writer(message_); |
588 OpenArray("o", &array_writer); | 590 OpenArray("o", &array_writer); |
589 for (size_t i = 0; i < object_paths.size(); ++i) { | 591 for (size_t i = 0; i < object_paths.size(); ++i) { |
590 array_writer.AppendObjectPath(object_paths[i]); | 592 array_writer.AppendObjectPath(object_paths[i]); |
591 } | 593 } |
592 CloseContainer(&array_writer); | 594 CloseContainer(&array_writer); |
593 } | 595 } |
594 | 596 |
597 bool MessageWriter::AppendProtocolBuffer( | |
598 google::protobuf::MessageLite* protobuf) { | |
599 DCHECK(protobuf != NULL); | |
600 size_t buf_size = protobuf->ByteSize(); | |
601 char* serialized_proto = reinterpret_cast<char*>(malloc(buf_size)); | |
satorux1
2012/01/31 20:27:49
Cannot we just use std::string? We rarely use mall
rharrison
2012/01/31 21:05:16
Done.
| |
602 | |
603 if (!protobuf->SerializeToArray(serialized_proto, buf_size)) { | |
604 LOG(ERROR) << "Unable to serialize supplied protocol buffer"; | |
605 free(serialized_proto); | |
606 return false; | |
607 } | |
608 AppendArrayOfBytes(reinterpret_cast<uint8*>(serialized_proto), | |
609 buf_size); | |
610 free(serialized_proto); | |
611 return true; | |
612 } | |
613 | |
595 void MessageWriter::AppendVariantOfByte(uint8 value) { | 614 void MessageWriter::AppendVariantOfByte(uint8 value) { |
596 AppendVariantOfBasic(DBUS_TYPE_BYTE, &value); | 615 AppendVariantOfBasic(DBUS_TYPE_BYTE, &value); |
597 } | 616 } |
598 | 617 |
599 void MessageWriter::AppendVariantOfBool(bool value) { | 618 void MessageWriter::AppendVariantOfBool(bool value) { |
600 // See the comment at MessageWriter::AppendBool(). | 619 // See the comment at MessageWriter::AppendBool(). |
601 dbus_bool_t dbus_value = value; | 620 dbus_bool_t dbus_value = value; |
602 AppendVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value); | 621 AppendVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value); |
603 } | 622 } |
604 | 623 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
792 return false; | 811 return false; |
793 while (array_reader.HasMoreData()) { | 812 while (array_reader.HasMoreData()) { |
794 std::string object_path; | 813 std::string object_path; |
795 if (!array_reader.PopObjectPath(&object_path)) | 814 if (!array_reader.PopObjectPath(&object_path)) |
796 return false; | 815 return false; |
797 object_paths->push_back(object_path); | 816 object_paths->push_back(object_path); |
798 } | 817 } |
799 return true; | 818 return true; |
800 } | 819 } |
801 | 820 |
821 bool MessageReader::PopProtocolBuffer(google::protobuf::MessageLite* protobuf) { | |
822 DCHECK(protobuf != NULL); | |
823 char* serialized_buf; | |
824 size_t buf_size; | |
825 if (!PopArrayOfBytes(reinterpret_cast<uint8**>(&serialized_buf), &buf_size)) { | |
826 LOG(ERROR) << "Error reading array of bytes"; | |
827 return false; | |
828 } | |
829 if (!protobuf->ParseFromArray(serialized_buf, buf_size)) { | |
830 LOG(ERROR) << "Failed to parse protocol buffer from array"; | |
831 return false; | |
832 } | |
833 return true; | |
834 } | |
835 | |
802 bool MessageReader::PopVariantOfByte(uint8* value) { | 836 bool MessageReader::PopVariantOfByte(uint8* value) { |
803 return PopVariantOfBasic(DBUS_TYPE_BYTE, value); | 837 return PopVariantOfBasic(DBUS_TYPE_BYTE, value); |
804 } | 838 } |
805 | 839 |
806 bool MessageReader::PopVariantOfBool(bool* value) { | 840 bool MessageReader::PopVariantOfBool(bool* value) { |
807 // See the comment at MessageReader::PopBool(). | 841 // See the comment at MessageReader::PopBool(). |
808 dbus_bool_t dbus_value = FALSE; | 842 dbus_bool_t dbus_value = FALSE; |
809 const bool success = PopVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value); | 843 const bool success = PopVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value); |
810 *value = static_cast<bool>(dbus_value); | 844 *value = static_cast<bool>(dbus_value); |
811 return success; | 845 return success; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
894 } | 928 } |
895 | 929 |
896 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { | 930 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { |
897 dbus::MessageReader variant_reader(message_); | 931 dbus::MessageReader variant_reader(message_); |
898 if (!PopVariant(&variant_reader)) | 932 if (!PopVariant(&variant_reader)) |
899 return false; | 933 return false; |
900 return variant_reader.PopBasic(dbus_type, value); | 934 return variant_reader.PopBasic(dbus_type, value); |
901 } | 935 } |
902 | 936 |
903 } // namespace dbus | 937 } // namespace dbus |
OLD | NEW |