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