OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "dbus/object_path.h" | |
14 #include "third_party/protobuf/src/google/protobuf/message_lite.h" | 13 #include "third_party/protobuf/src/google/protobuf/message_lite.h" |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 // Appends the header name and the value to |output|, if the value is | 17 // Appends the header name and the value to |output|, if the value is |
19 // not empty. | 18 // not empty. |
20 static void AppendStringHeader(const std::string& header_name, | 19 static void AppendStringHeader(const std::string& header_name, |
21 const std::string& header_value, | 20 const std::string& header_value, |
22 std::string* output) { | 21 std::string* output) { |
23 if (!header_value.empty()) { | 22 if (!header_value.empty()) { |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 break; | 150 break; |
152 } | 151 } |
153 case STRING: { | 152 case STRING: { |
154 std::string value; | 153 std::string value; |
155 if (!reader->PopString(&value)) | 154 if (!reader->PopString(&value)) |
156 return kBrokenMessage; | 155 return kBrokenMessage; |
157 output += indent + "string \"" + value + "\"\n"; | 156 output += indent + "string \"" + value + "\"\n"; |
158 break; | 157 break; |
159 } | 158 } |
160 case OBJECT_PATH: { | 159 case OBJECT_PATH: { |
161 ObjectPath value; | 160 std::string value; |
162 if (!reader->PopObjectPath(&value)) | 161 if (!reader->PopObjectPath(&value)) |
163 return kBrokenMessage; | 162 return kBrokenMessage; |
164 output += indent + "object_path \"" + value.value() + "\"\n"; | 163 output += indent + "object_path \"" + value + "\"\n"; |
165 break; | 164 break; |
166 } | 165 } |
167 case ARRAY: { | 166 case ARRAY: { |
168 MessageReader sub_reader(this); | 167 MessageReader sub_reader(this); |
169 if (!reader->PopArray(&sub_reader)) | 168 if (!reader->PopArray(&sub_reader)) |
170 return kBrokenMessage; | 169 return kBrokenMessage; |
171 output += indent + "array [\n"; | 170 output += indent + "array [\n"; |
172 output += ToStringInternal(indent + " ", &sub_reader); | 171 output += ToStringInternal(indent + " ", &sub_reader); |
173 output += indent + "]\n"; | 172 output += indent + "]\n"; |
174 break; | 173 break; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 // string \"payload\" | 217 // string \"payload\" |
219 // ... | 218 // ... |
220 std::string Message::ToString() { | 219 std::string Message::ToString() { |
221 if (!raw_message_) | 220 if (!raw_message_) |
222 return ""; | 221 return ""; |
223 | 222 |
224 // Generate headers first. | 223 // Generate headers first. |
225 std::string headers; | 224 std::string headers; |
226 AppendStringHeader("message_type", GetMessageTypeAsString(), &headers); | 225 AppendStringHeader("message_type", GetMessageTypeAsString(), &headers); |
227 AppendStringHeader("destination", GetDestination(), &headers); | 226 AppendStringHeader("destination", GetDestination(), &headers); |
228 AppendStringHeader("path", GetPath().value(), &headers); | 227 AppendStringHeader("path", GetPath(), &headers); |
229 AppendStringHeader("interface", GetInterface(), &headers); | 228 AppendStringHeader("interface", GetInterface(), &headers); |
230 AppendStringHeader("member", GetMember(), &headers); | 229 AppendStringHeader("member", GetMember(), &headers); |
231 AppendStringHeader("error_name", GetErrorName(), &headers); | 230 AppendStringHeader("error_name", GetErrorName(), &headers); |
232 AppendStringHeader("sender", GetSender(), &headers); | 231 AppendStringHeader("sender", GetSender(), &headers); |
233 AppendStringHeader("signature", GetSignature(), &headers); | 232 AppendStringHeader("signature", GetSignature(), &headers); |
234 AppendUint32Header("serial", GetSerial(), &headers); | 233 AppendUint32Header("serial", GetSerial(), &headers); |
235 AppendUint32Header("reply_serial", GetReplySerial(), &headers); | 234 AppendUint32Header("reply_serial", GetReplySerial(), &headers); |
236 | 235 |
237 // Generate the payload. | 236 // Generate the payload. |
238 MessageReader reader(this); | 237 MessageReader reader(this); |
239 return headers + "\n" + ToStringInternal("", &reader); | 238 return headers + "\n" + ToStringInternal("", &reader); |
240 } | 239 } |
241 | 240 |
242 void Message::SetDestination(const std::string& destination) { | 241 void Message::SetDestination(const std::string& destination) { |
243 const bool success = dbus_message_set_destination(raw_message_, | 242 const bool success = dbus_message_set_destination(raw_message_, |
244 destination.c_str()); | 243 destination.c_str()); |
245 CHECK(success) << "Unable to allocate memory"; | 244 CHECK(success) << "Unable to allocate memory"; |
246 } | 245 } |
247 | 246 |
248 void Message::SetPath(const ObjectPath& path) { | 247 void Message::SetPath(const std::string& path) { |
249 const bool success = dbus_message_set_path(raw_message_, | 248 const bool success = dbus_message_set_path(raw_message_, |
250 path.value().c_str()); | 249 path.c_str()); |
251 CHECK(success) << "Unable to allocate memory"; | 250 CHECK(success) << "Unable to allocate memory"; |
252 } | 251 } |
253 | 252 |
254 void Message::SetInterface(const std::string& interface) { | 253 void Message::SetInterface(const std::string& interface) { |
255 const bool success = dbus_message_set_interface(raw_message_, | 254 const bool success = dbus_message_set_interface(raw_message_, |
256 interface.c_str()); | 255 interface.c_str()); |
257 CHECK(success) << "Unable to allocate memory"; | 256 CHECK(success) << "Unable to allocate memory"; |
258 } | 257 } |
259 | 258 |
260 void Message::SetMember(const std::string& member) { | 259 void Message::SetMember(const std::string& member) { |
(...skipping 20 matching lines...) Expand all Loading... |
281 | 280 |
282 void Message::SetReplySerial(uint32 reply_serial) { | 281 void Message::SetReplySerial(uint32 reply_serial) { |
283 dbus_message_set_reply_serial(raw_message_, reply_serial); | 282 dbus_message_set_reply_serial(raw_message_, reply_serial); |
284 } | 283 } |
285 | 284 |
286 std::string Message::GetDestination() { | 285 std::string Message::GetDestination() { |
287 const char* destination = dbus_message_get_destination(raw_message_); | 286 const char* destination = dbus_message_get_destination(raw_message_); |
288 return destination ? destination : ""; | 287 return destination ? destination : ""; |
289 } | 288 } |
290 | 289 |
291 ObjectPath Message::GetPath() { | 290 std::string Message::GetPath() { |
292 const char* path = dbus_message_get_path(raw_message_); | 291 const char* path = dbus_message_get_path(raw_message_); |
293 return ObjectPath(path ? path : ""); | 292 return path ? path : ""; |
294 } | 293 } |
295 | 294 |
296 std::string Message::GetInterface() { | 295 std::string Message::GetInterface() { |
297 const char* interface = dbus_message_get_interface(raw_message_); | 296 const char* interface = dbus_message_get_interface(raw_message_); |
298 return interface ? interface : ""; | 297 return interface ? interface : ""; |
299 } | 298 } |
300 | 299 |
301 std::string Message::GetMember() { | 300 std::string Message::GetMember() { |
302 const char* member = dbus_message_get_member(raw_message_); | 301 const char* member = dbus_message_get_member(raw_message_); |
303 return member ? member : ""; | 302 return member ? member : ""; |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 } | 483 } |
485 | 484 |
486 void MessageWriter::AppendString(const std::string& value) { | 485 void MessageWriter::AppendString(const std::string& value) { |
487 const char* pointer = value.c_str(); | 486 const char* pointer = value.c_str(); |
488 AppendBasic(DBUS_TYPE_STRING, &pointer); | 487 AppendBasic(DBUS_TYPE_STRING, &pointer); |
489 // TODO(satorux): It may make sense to return an error here, as the | 488 // TODO(satorux): It may make sense to return an error here, as the |
490 // input string can be large. If needed, we could add something like | 489 // input string can be large. If needed, we could add something like |
491 // bool AppendStringWithErrorChecking(). | 490 // bool AppendStringWithErrorChecking(). |
492 } | 491 } |
493 | 492 |
494 void MessageWriter::AppendObjectPath(const ObjectPath& value) { | 493 void MessageWriter::AppendObjectPath(const std::string& value) { |
495 const char* pointer = value.value().c_str(); | 494 const char* pointer = value.c_str(); |
496 AppendBasic(DBUS_TYPE_OBJECT_PATH, &pointer); | 495 AppendBasic(DBUS_TYPE_OBJECT_PATH, &pointer); |
497 } | 496 } |
498 | 497 |
499 // Ideally, client shouldn't need to supply the signature string, but | 498 // Ideally, client shouldn't need to supply the signature string, but |
500 // the underlying D-Bus library requires us to supply this before | 499 // the underlying D-Bus library requires us to supply this before |
501 // appending contents to array and variant. It's technically possible | 500 // appending contents to array and variant. It's technically possible |
502 // for us to design API that doesn't require the signature but it will | 501 // for us to design API that doesn't require the signature but it will |
503 // complicate the implementation so we decided to have the signature | 502 // complicate the implementation so we decided to have the signature |
504 // parameter. Hopefully, variants are less used in request messages from | 503 // parameter. Hopefully, variants are less used in request messages from |
505 // client side than response message from server side, so this should | 504 // client side than response message from server side, so this should |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 DCHECK(!container_is_open_); | 580 DCHECK(!container_is_open_); |
582 MessageWriter array_writer(message_); | 581 MessageWriter array_writer(message_); |
583 OpenArray("s", &array_writer); | 582 OpenArray("s", &array_writer); |
584 for (size_t i = 0; i < strings.size(); ++i) { | 583 for (size_t i = 0; i < strings.size(); ++i) { |
585 array_writer.AppendString(strings[i]); | 584 array_writer.AppendString(strings[i]); |
586 } | 585 } |
587 CloseContainer(&array_writer); | 586 CloseContainer(&array_writer); |
588 } | 587 } |
589 | 588 |
590 void MessageWriter::AppendArrayOfObjectPaths( | 589 void MessageWriter::AppendArrayOfObjectPaths( |
591 const std::vector<ObjectPath>& object_paths) { | 590 const std::vector<std::string>& object_paths) { |
592 DCHECK(!container_is_open_); | 591 DCHECK(!container_is_open_); |
593 MessageWriter array_writer(message_); | 592 MessageWriter array_writer(message_); |
594 OpenArray("o", &array_writer); | 593 OpenArray("o", &array_writer); |
595 for (size_t i = 0; i < object_paths.size(); ++i) { | 594 for (size_t i = 0; i < object_paths.size(); ++i) { |
596 array_writer.AppendObjectPath(object_paths[i]); | 595 array_writer.AppendObjectPath(object_paths[i]); |
597 } | 596 } |
598 CloseContainer(&array_writer); | 597 CloseContainer(&array_writer); |
599 } | 598 } |
600 | 599 |
601 bool MessageWriter::AppendProtoAsArrayOfBytes( | 600 bool MessageWriter::AppendProtoAsArrayOfBytes( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 | 645 |
647 void MessageWriter::AppendVariantOfDouble(double value) { | 646 void MessageWriter::AppendVariantOfDouble(double value) { |
648 AppendVariantOfBasic(DBUS_TYPE_DOUBLE, &value); | 647 AppendVariantOfBasic(DBUS_TYPE_DOUBLE, &value); |
649 } | 648 } |
650 | 649 |
651 void MessageWriter::AppendVariantOfString(const std::string& value) { | 650 void MessageWriter::AppendVariantOfString(const std::string& value) { |
652 const char* pointer = value.c_str(); | 651 const char* pointer = value.c_str(); |
653 AppendVariantOfBasic(DBUS_TYPE_STRING, &pointer); | 652 AppendVariantOfBasic(DBUS_TYPE_STRING, &pointer); |
654 } | 653 } |
655 | 654 |
656 void MessageWriter::AppendVariantOfObjectPath(const ObjectPath& value) { | 655 void MessageWriter::AppendVariantOfObjectPath(const std::string& value) { |
657 const char* pointer = value.value().c_str(); | 656 const char* pointer = value.c_str(); |
658 AppendVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &pointer); | 657 AppendVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &pointer); |
659 } | 658 } |
660 | 659 |
661 void MessageWriter::AppendBasic(int dbus_type, const void* value) { | 660 void MessageWriter::AppendBasic(int dbus_type, const void* value) { |
662 DCHECK(!container_is_open_); | 661 DCHECK(!container_is_open_); |
663 | 662 |
664 const bool success = dbus_message_iter_append_basic( | 663 const bool success = dbus_message_iter_append_basic( |
665 &raw_message_iter_, dbus_type, value); | 664 &raw_message_iter_, dbus_type, value); |
666 // dbus_message_iter_append_basic() fails only when there is not enough | 665 // dbus_message_iter_append_basic() fails only when there is not enough |
667 // memory. We don't return this error as there is nothing we can do when | 666 // memory. We don't return this error as there is nothing we can do when |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 } | 739 } |
741 | 740 |
742 bool MessageReader::PopString(std::string* value) { | 741 bool MessageReader::PopString(std::string* value) { |
743 char* tmp_value = NULL; | 742 char* tmp_value = NULL; |
744 const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value); | 743 const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value); |
745 if (success) | 744 if (success) |
746 value->assign(tmp_value); | 745 value->assign(tmp_value); |
747 return success; | 746 return success; |
748 } | 747 } |
749 | 748 |
750 bool MessageReader::PopObjectPath(ObjectPath* value) { | 749 bool MessageReader::PopObjectPath(std::string* value) { |
751 char* tmp_value = NULL; | 750 char* tmp_value = NULL; |
752 const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); | 751 const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); |
753 if (success) | 752 if (success) |
754 *value = ObjectPath(tmp_value); | 753 value->assign(tmp_value); |
755 return success; | 754 return success; |
756 } | 755 } |
757 | 756 |
758 bool MessageReader::PopArray(MessageReader* sub_reader) { | 757 bool MessageReader::PopArray(MessageReader* sub_reader) { |
759 return PopContainer(DBUS_TYPE_ARRAY, sub_reader); | 758 return PopContainer(DBUS_TYPE_ARRAY, sub_reader); |
760 } | 759 } |
761 | 760 |
762 bool MessageReader::PopStruct(MessageReader* sub_reader) { | 761 bool MessageReader::PopStruct(MessageReader* sub_reader) { |
763 return PopContainer(DBUS_TYPE_STRUCT, sub_reader); | 762 return PopContainer(DBUS_TYPE_STRUCT, sub_reader); |
764 } | 763 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
799 while (array_reader.HasMoreData()) { | 798 while (array_reader.HasMoreData()) { |
800 std::string string; | 799 std::string string; |
801 if (!array_reader.PopString(&string)) | 800 if (!array_reader.PopString(&string)) |
802 return false; | 801 return false; |
803 strings->push_back(string); | 802 strings->push_back(string); |
804 } | 803 } |
805 return true; | 804 return true; |
806 } | 805 } |
807 | 806 |
808 bool MessageReader::PopArrayOfObjectPaths( | 807 bool MessageReader::PopArrayOfObjectPaths( |
809 std::vector<ObjectPath> *object_paths) { | 808 std::vector<std::string> *object_paths) { |
810 MessageReader array_reader(message_); | 809 MessageReader array_reader(message_); |
811 if (!PopArray(&array_reader)) | 810 if (!PopArray(&array_reader)) |
812 return false; | 811 return false; |
813 while (array_reader.HasMoreData()) { | 812 while (array_reader.HasMoreData()) { |
814 ObjectPath object_path; | 813 std::string object_path; |
815 if (!array_reader.PopObjectPath(&object_path)) | 814 if (!array_reader.PopObjectPath(&object_path)) |
816 return false; | 815 return false; |
817 object_paths->push_back(object_path); | 816 object_paths->push_back(object_path); |
818 } | 817 } |
819 return true; | 818 return true; |
820 } | 819 } |
821 | 820 |
822 bool MessageReader::PopArrayOfBytesAsProto( | 821 bool MessageReader::PopArrayOfBytesAsProto( |
823 google::protobuf::MessageLite* protobuf) { | 822 google::protobuf::MessageLite* protobuf) { |
824 DCHECK(protobuf != NULL); | 823 DCHECK(protobuf != NULL); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 } | 875 } |
877 | 876 |
878 bool MessageReader::PopVariantOfString(std::string* value) { | 877 bool MessageReader::PopVariantOfString(std::string* value) { |
879 char* tmp_value = NULL; | 878 char* tmp_value = NULL; |
880 const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value); | 879 const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value); |
881 if (success) | 880 if (success) |
882 value->assign(tmp_value); | 881 value->assign(tmp_value); |
883 return success; | 882 return success; |
884 } | 883 } |
885 | 884 |
886 bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) { | 885 bool MessageReader::PopVariantOfObjectPath(std::string* value) { |
887 char* tmp_value = NULL; | 886 char* tmp_value = NULL; |
888 const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); | 887 const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); |
889 if (success) | 888 if (success) |
890 *value = ObjectPath(tmp_value); | 889 value->assign(tmp_value); |
891 return success; | 890 return success; |
892 } | 891 } |
893 | 892 |
894 Message::DataType MessageReader::GetDataType() { | 893 Message::DataType MessageReader::GetDataType() { |
895 const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_); | 894 const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_); |
896 return static_cast<Message::DataType>(dbus_type); | 895 return static_cast<Message::DataType>(dbus_type); |
897 } | 896 } |
898 | 897 |
899 bool MessageReader::CheckDataType(int dbus_type) { | 898 bool MessageReader::CheckDataType(int dbus_type) { |
900 const int actual_type = dbus_message_iter_get_arg_type(&raw_message_iter_); | 899 const int actual_type = dbus_message_iter_get_arg_type(&raw_message_iter_); |
(...skipping 29 matching lines...) Expand all Loading... |
930 } | 929 } |
931 | 930 |
932 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { | 931 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { |
933 dbus::MessageReader variant_reader(message_); | 932 dbus::MessageReader variant_reader(message_); |
934 if (!PopVariant(&variant_reader)) | 933 if (!PopVariant(&variant_reader)) |
935 return false; | 934 return false; |
936 return variant_reader.PopBasic(dbus_type, value); | 935 return variant_reader.PopBasic(dbus_type, value); |
937 } | 936 } |
938 | 937 |
939 } // namespace dbus | 938 } // namespace dbus |
OLD | NEW |