| 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/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "dbus/object_path.h" | 14 #include "dbus/object_path.h" |
| 15 #include "third_party/protobuf/src/google/protobuf/message_lite.h" | 15 #include "third_party/protobuf/src/google/protobuf/message_lite.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Appends the header name and the value to |output|, if the value is | 19 // Appends the header name and the value to |output|, if the value is |
| 20 // not empty. | 20 // not empty. |
| 21 void AppendStringHeader(const std::string& header_name, | 21 static void AppendStringHeader(const std::string& header_name, |
| 22 const std::string& header_value, | 22 const std::string& header_value, |
| 23 std::string* output) { | 23 std::string* output) { |
| 24 if (!header_value.empty()) { | 24 if (!header_value.empty()) { |
| 25 *output += header_name + ": " + header_value + "\n"; | 25 *output += header_name + ": " + header_value + "\n"; |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Appends the header name and the value to |output|, if the value is | 29 // Appends the header name and the value to |output|, if the value is |
| 30 // nonzero. | 30 // nonzero. |
| 31 void AppendUint32Header(const std::string& header_name, | 31 static void AppendUint32Header(const std::string& header_name, |
| 32 uint32 header_value, | 32 uint32 header_value, |
| 33 std::string* output) { | 33 std::string* output) { |
| 34 if (header_value != 0) { | 34 if (header_value != 0) { |
| 35 *output += (header_name + ": " + base::StringPrintf("%u", header_value) + | 35 *output += (header_name + ": " + base::StringPrintf("%u", header_value) + |
| 36 "\n"); | 36 "\n"); |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 // Returns true if Unix FD passing is supported in libdbus. | |
| 41 // The check is done runtime rather than compile time as the libdbus | |
| 42 // version used at runtime may be different from the one used at compile time. | |
| 43 bool IsDBusTypeUnixFdSupported() { | |
| 44 int major = 0, minor = 0, micro = 0; | |
| 45 dbus_get_version(&major, &minor, µ); | |
| 46 return major >= 1 && minor >= 4; | |
| 47 } | |
| 48 | |
| 49 } // namespace | 40 } // namespace |
| 50 | 41 |
| 51 namespace dbus { | 42 namespace dbus { |
| 52 | 43 |
| 53 Message::Message() | 44 Message::Message() |
| 54 : raw_message_(NULL) { | 45 : raw_message_(NULL) { |
| 55 } | 46 } |
| 56 | 47 |
| 57 Message::~Message() { | 48 Message::~Message() { |
| 58 if (raw_message_) | 49 if (raw_message_) |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 } | 204 } |
| 214 case VARIANT: { | 205 case VARIANT: { |
| 215 MessageReader sub_reader(this); | 206 MessageReader sub_reader(this); |
| 216 if (!reader->PopVariant(&sub_reader)) | 207 if (!reader->PopVariant(&sub_reader)) |
| 217 return kBrokenMessage; | 208 return kBrokenMessage; |
| 218 output += indent + "variant "; | 209 output += indent + "variant "; |
| 219 output += ToStringInternal(indent + " ", &sub_reader); | 210 output += ToStringInternal(indent + " ", &sub_reader); |
| 220 break; | 211 break; |
| 221 } | 212 } |
| 222 case UNIX_FD: { | 213 case UNIX_FD: { |
| 223 CHECK(IsDBusTypeUnixFdSupported()); | 214 CHECK(kDBusTypeUnixFdIsSupported); |
| 224 | 215 |
| 225 FileDescriptor file_descriptor; | 216 FileDescriptor file_descriptor; |
| 226 if (!reader->PopFileDescriptor(&file_descriptor)) | 217 if (!reader->PopFileDescriptor(&file_descriptor)) |
| 227 return kBrokenMessage; | 218 return kBrokenMessage; |
| 228 output += indent + "fd#" + | 219 output += indent + "fd#" + |
| 229 base::StringPrintf("%d", file_descriptor.value()) + "\n"; | 220 base::StringPrintf("%d", file_descriptor.value()) + "\n"; |
| 230 break; | 221 break; |
| 231 } | 222 } |
| 232 default: | 223 default: |
| 233 LOG(FATAL) << "Unknown type: " << type; | 224 LOG(FATAL) << "Unknown type: " << type; |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 | 683 |
| 693 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { | 684 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { |
| 694 const std::string signature = base::StringPrintf("%c", dbus_type); | 685 const std::string signature = base::StringPrintf("%c", dbus_type); |
| 695 MessageWriter variant_writer(message_); | 686 MessageWriter variant_writer(message_); |
| 696 OpenVariant(signature, &variant_writer); | 687 OpenVariant(signature, &variant_writer); |
| 697 variant_writer.AppendBasic(dbus_type, value); | 688 variant_writer.AppendBasic(dbus_type, value); |
| 698 CloseContainer(&variant_writer); | 689 CloseContainer(&variant_writer); |
| 699 } | 690 } |
| 700 | 691 |
| 701 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { | 692 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { |
| 702 CHECK(IsDBusTypeUnixFdSupported()); | 693 CHECK(kDBusTypeUnixFdIsSupported); |
| 703 | 694 |
| 704 if (!value.is_valid()) { | 695 if (!value.is_valid()) { |
| 705 // NB: sending a directory potentially enables sandbox escape | 696 // NB: sending a directory potentially enables sandbox escape |
| 706 LOG(FATAL) << "Attempt to pass invalid file descriptor"; | 697 LOG(FATAL) << "Attempt to pass invalid file descriptor"; |
| 707 } | 698 } |
| 708 int fd = value.value(); | 699 int fd = value.value(); |
| 709 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); | 700 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); |
| 710 } | 701 } |
| 711 | 702 |
| 712 // | 703 // |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 962 } | 953 } |
| 963 | 954 |
| 964 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { | 955 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { |
| 965 dbus::MessageReader variant_reader(message_); | 956 dbus::MessageReader variant_reader(message_); |
| 966 if (!PopVariant(&variant_reader)) | 957 if (!PopVariant(&variant_reader)) |
| 967 return false; | 958 return false; |
| 968 return variant_reader.PopBasic(dbus_type, value); | 959 return variant_reader.PopBasic(dbus_type, value); |
| 969 } | 960 } |
| 970 | 961 |
| 971 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { | 962 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { |
| 972 CHECK(IsDBusTypeUnixFdSupported()); | 963 CHECK(kDBusTypeUnixFdIsSupported); |
| 973 | 964 |
| 974 int fd = -1; | 965 int fd = -1; |
| 975 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); | 966 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); |
| 976 if (!success) | 967 if (!success) |
| 977 return false; | 968 return false; |
| 978 | 969 |
| 979 value->PutValue(fd); | 970 value->PutValue(fd); |
| 980 // NB: the caller must check validity before using the value | 971 // NB: the caller must check validity before using the value |
| 981 return true; | 972 return true; |
| 982 } | 973 } |
| 983 | 974 |
| 984 } // namespace dbus | 975 } // namespace dbus |
| OLD | NEW |