Chromium Code Reviews| 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 #if defined(DBUS_TYPE_UNIX_FD) | |
| 13 #include "base/platform_file.h" | |
| 14 #endif | |
| 12 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 13 #include "dbus/object_path.h" | 16 #include "dbus/object_path.h" |
| 14 #include "third_party/protobuf/src/google/protobuf/message_lite.h" | 17 #include "third_party/protobuf/src/google/protobuf/message_lite.h" |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 // Appends the header name and the value to |output|, if the value is | 21 // Appends the header name and the value to |output|, if the value is |
| 19 // not empty. | 22 // not empty. |
| 20 static void AppendStringHeader(const std::string& header_name, | 23 static void AppendStringHeader(const std::string& header_name, |
| 21 const std::string& header_value, | 24 const std::string& header_value, |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 break; | 195 break; |
| 193 } | 196 } |
| 194 case VARIANT: { | 197 case VARIANT: { |
| 195 MessageReader sub_reader(this); | 198 MessageReader sub_reader(this); |
| 196 if (!reader->PopVariant(&sub_reader)) | 199 if (!reader->PopVariant(&sub_reader)) |
| 197 return kBrokenMessage; | 200 return kBrokenMessage; |
| 198 output += indent + "variant "; | 201 output += indent + "variant "; |
| 199 output += ToStringInternal(indent + " ", &sub_reader); | 202 output += ToStringInternal(indent + " ", &sub_reader); |
| 200 break; | 203 break; |
| 201 } | 204 } |
| 205 #if defined(DBUS_TYPE_UNIX_FD) | |
|
satorux1
2012/03/28 00:32:43
Hmm, there seems to be many #ifdefs based on DBUS_
Sam Leffler
2012/03/28 17:28:09
You cannot do this unless you define UNIX_FD in th
satorux1
2012/03/28 17:41:54
My proposal was to define DBUS_TYPE_UNIX_FD for th
Sam Leffler
2012/03/28 19:25:27
Done.
| |
| 206 case UNIX_FD: { | |
| 207 FileDescriptor value; | |
| 208 if (!reader->PopFileDescriptor(&value)) | |
| 209 return kBrokenMessage; | |
| 210 output += indent + "fd#" + | |
| 211 base::StringPrintf("%u", value.value()) + "\n"; | |
| 212 break; | |
| 213 } | |
| 214 #endif | |
| 202 default: | 215 default: |
| 203 LOG(FATAL) << "Unknown type: " << type; | 216 LOG(FATAL) << "Unknown type: " << type; |
| 204 } | 217 } |
| 205 } | 218 } |
| 206 return output; | 219 return output; |
| 207 } | 220 } |
| 208 | 221 |
| 209 // The returned string consists of message headers such as | 222 // The returned string consists of message headers such as |
| 210 // destination if any, followed by a blank line, and the message | 223 // destination if any, followed by a blank line, and the message |
| 211 // payload. For example, a MethodCall's ToString() will look like: | 224 // payload. For example, a MethodCall's ToString() will look like: |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 670 } | 683 } |
| 671 | 684 |
| 672 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { | 685 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { |
| 673 const std::string signature = base::StringPrintf("%c", dbus_type); | 686 const std::string signature = base::StringPrintf("%c", dbus_type); |
| 674 MessageWriter variant_writer(message_); | 687 MessageWriter variant_writer(message_); |
| 675 OpenVariant(signature, &variant_writer); | 688 OpenVariant(signature, &variant_writer); |
| 676 variant_writer.AppendBasic(dbus_type, value); | 689 variant_writer.AppendBasic(dbus_type, value); |
| 677 CloseContainer(&variant_writer); | 690 CloseContainer(&variant_writer); |
| 678 } | 691 } |
| 679 | 692 |
| 693 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { | |
|
satorux1
2012/03/28 00:32:43
If we are to go with the idea described above, you
| |
| 694 #if defined(DBUS_TYPE_UNIX_FD) | |
| 695 base::PlatformFileInfo info; | |
| 696 int fd = value.value(); | |
| 697 bool ok = base::GetPlatformFileInfo(fd, &info); | |
| 698 if (!ok || info.is_directory) | |
| 699 LOG(FATAL) << "Attempt to pass invalid file descriptor"; | |
| 700 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); | |
| 701 #else | |
| 702 CHECK(false) << "File descriptor passing not supported"; | |
| 703 #endif | |
| 704 } | |
| 705 | |
| 680 // | 706 // |
| 681 // MessageReader implementation. | 707 // MessageReader implementation. |
| 682 // | 708 // |
| 683 | 709 |
| 684 MessageReader::MessageReader(Message* message) | 710 MessageReader::MessageReader(Message* message) |
| 685 : message_(message) { | 711 : message_(message) { |
| 686 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); | 712 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); |
| 687 if (message) | 713 if (message) |
| 688 dbus_message_iter_init(message_->raw_message(), &raw_message_iter_); | 714 dbus_message_iter_init(message_->raw_message(), &raw_message_iter_); |
| 689 } | 715 } |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 929 return true; | 955 return true; |
| 930 } | 956 } |
| 931 | 957 |
| 932 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { | 958 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { |
| 933 dbus::MessageReader variant_reader(message_); | 959 dbus::MessageReader variant_reader(message_); |
| 934 if (!PopVariant(&variant_reader)) | 960 if (!PopVariant(&variant_reader)) |
| 935 return false; | 961 return false; |
| 936 return variant_reader.PopBasic(dbus_type, value); | 962 return variant_reader.PopBasic(dbus_type, value); |
| 937 } | 963 } |
| 938 | 964 |
| 965 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { | |
| 966 #if defined(DBUS_TYPE_UNIX_FD) | |
| 967 int fd; | |
| 968 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); | |
| 969 if (!success) | |
| 970 return false; | |
| 971 | |
| 972 base::PlatformFileInfo info; | |
| 973 bool ok = base::GetPlatformFileInfo(fd, &info); | |
| 974 if (!ok || info.is_directory) { | |
| 975 LOG(FATAL) << "Attempt to receive invalid file descriptor"; | |
| 976 return false; // NB: not reached | |
| 977 } | |
|
keybuk
2012/03/27 22:36:35
doesn't close the file descriptor before returning
Sam Leffler
2012/03/27 23:20:40
Not sure where you're referring. fd is not closed
keybuk
2012/03/28 17:53:12
it does that now, it might not in future; likewise
Sam Leffler
2012/03/28 19:25:27
Done.
| |
| 978 value->PutValue(fd); | |
| 979 return true; | |
| 980 #else | |
| 981 CHECK(false) << "File descriptor passing not supported"; | |
| 982 return false; | |
| 983 #endif | |
| 984 } | |
| 985 | |
| 939 } // namespace dbus | 986 } // namespace dbus |
| OLD | NEW |