| 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/numerics/safe_conversions.h" |
| 12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "dbus/object_path.h" | 16 #include "dbus/object_path.h" |
| 16 | 17 |
| 17 #if defined(USE_SYSTEM_PROTOBUF) | 18 #if defined(USE_SYSTEM_PROTOBUF) |
| 18 #include <google/protobuf/message_lite.h> | 19 #include <google/protobuf/message_lite.h> |
| 19 #else | 20 #else |
| 20 #include "third_party/protobuf/src/google/protobuf/message_lite.h" | 21 #include "third_party/protobuf/src/google/protobuf/message_lite.h" |
| 21 #endif | 22 #endif |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Appends the header name and the value to |output|, if the value is | 26 // Appends the header name and the value to |output|, if the value is |
| 26 // not empty. | 27 // not empty. |
| 27 void AppendStringHeader(const std::string& header_name, | 28 void AppendStringHeader(const std::string& header_name, |
| 28 const std::string& header_value, | 29 const std::string& header_value, |
| 29 std::string* output) { | 30 std::string* output) { |
| 30 if (!header_value.empty()) { | 31 if (!header_value.empty()) { |
| 31 *output += header_name + ": " + header_value + "\n"; | 32 *output += header_name + ": " + header_value + "\n"; |
| 32 } | 33 } |
| 33 } | 34 } |
| 34 | 35 |
| 35 // Appends the header name and the value to |output|, if the value is | 36 // Appends the header name and the value to |output|, if the value is |
| 36 // nonzero. | 37 // nonzero. |
| 37 void AppendUint32Header(const std::string& header_name, | 38 void AppendUint32Header(const std::string& header_name, |
| 38 uint32 header_value, | 39 uint32 header_value, |
| 39 std::string* output) { | 40 std::string* output) { |
| 40 if (header_value != 0) { | 41 if (header_value != 0) { |
| 41 *output += (header_name + ": " + base::StringPrintf("%u", header_value) + | 42 *output += (header_name + ": " + base::UintToString(header_value) + "\n"); |
| 42 "\n"); | |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 | 45 |
| 46 } // namespace | 46 } // namespace |
| 47 | 47 |
| 48 namespace dbus { | 48 namespace dbus { |
| 49 | 49 |
| 50 bool IsDBusTypeUnixFdSupported() { | 50 bool IsDBusTypeUnixFdSupported() { |
| 51 int major = 0, minor = 0, micro = 0; | 51 int major = 0, minor = 0, micro = 0; |
| 52 dbus_get_version(&major, &minor, µ); | 52 dbus_get_version(&major, &minor, µ); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 MessageReader* reader) { | 95 MessageReader* reader) { |
| 96 const char* kBrokenMessage = "[broken message]"; | 96 const char* kBrokenMessage = "[broken message]"; |
| 97 std::string output; | 97 std::string output; |
| 98 while (reader->HasMoreData()) { | 98 while (reader->HasMoreData()) { |
| 99 const DataType type = reader->GetDataType(); | 99 const DataType type = reader->GetDataType(); |
| 100 switch (type) { | 100 switch (type) { |
| 101 case BYTE: { | 101 case BYTE: { |
| 102 uint8 value = 0; | 102 uint8 value = 0; |
| 103 if (!reader->PopByte(&value)) | 103 if (!reader->PopByte(&value)) |
| 104 return kBrokenMessage; | 104 return kBrokenMessage; |
| 105 output += indent + "byte " + base::IntToString(value) + "\n"; | 105 output += indent + "byte " + base::UintToString(value) + "\n"; |
| 106 break; | 106 break; |
| 107 } | 107 } |
| 108 case BOOL: { | 108 case BOOL: { |
| 109 bool value = false; | 109 bool value = false; |
| 110 if (!reader->PopBool(&value)) | 110 if (!reader->PopBool(&value)) |
| 111 return kBrokenMessage; | 111 return kBrokenMessage; |
| 112 output += indent + "bool " + (value ? "true" : "false") + "\n"; | 112 output += indent + "bool " + (value ? "true" : "false") + "\n"; |
| 113 break; | 113 break; |
| 114 } | 114 } |
| 115 case INT16: { | 115 case INT16: { |
| 116 int16 value = 0; | 116 int16 value = 0; |
| 117 if (!reader->PopInt16(&value)) | 117 if (!reader->PopInt16(&value)) |
| 118 return kBrokenMessage; | 118 return kBrokenMessage; |
| 119 output += indent + "int16 " + base::IntToString(value) + "\n"; | 119 output += indent + "int16 " + base::IntToString(value) + "\n"; |
| 120 break; | 120 break; |
| 121 } | 121 } |
| 122 case UINT16: { | 122 case UINT16: { |
| 123 uint16 value = 0; | 123 uint16 value = 0; |
| 124 if (!reader->PopUint16(&value)) | 124 if (!reader->PopUint16(&value)) |
| 125 return kBrokenMessage; | 125 return kBrokenMessage; |
| 126 output += indent + "uint16 " + base::IntToString(value) + "\n"; | 126 output += indent + "uint16 " + base::UintToString(value) + "\n"; |
| 127 break; | 127 break; |
| 128 } | 128 } |
| 129 case INT32: { | 129 case INT32: { |
| 130 int32 value = 0; | 130 int32 value = 0; |
| 131 if (!reader->PopInt32(&value)) | 131 if (!reader->PopInt32(&value)) |
| 132 return kBrokenMessage; | 132 return kBrokenMessage; |
| 133 output += indent + "int32 " + base::IntToString(value) + "\n"; | 133 output += indent + "int32 " + base::IntToString(value) + "\n"; |
| 134 break; | 134 break; |
| 135 } | 135 } |
| 136 case UINT32: { | 136 case UINT32: { |
| 137 uint32 value = 0; | 137 uint32 value = 0; |
| 138 if (!reader->PopUint32(&value)) | 138 if (!reader->PopUint32(&value)) |
| 139 return kBrokenMessage; | 139 return kBrokenMessage; |
| 140 output += indent + "uint32 " + base::StringPrintf("%u", value) + "\n"; | 140 output += indent + "uint32 " + base::UintToString(value) + "\n"; |
| 141 break; | 141 break; |
| 142 } | 142 } |
| 143 case INT64: { | 143 case INT64: { |
| 144 int64 value = 0; | 144 int64 value = 0; |
| 145 if (!reader->PopInt64(&value)) | 145 if (!reader->PopInt64(&value)) |
| 146 return kBrokenMessage; | 146 return kBrokenMessage; |
| 147 output += (indent + "int64 " + | 147 output += (indent + "int64 " + base::Int64ToString(value) + "\n"); |
| 148 base::StringPrintf("%" PRId64, value) + "\n"); | |
| 149 break; | 148 break; |
| 150 } | 149 } |
| 151 case UINT64: { | 150 case UINT64: { |
| 152 uint64 value = 0; | 151 uint64 value = 0; |
| 153 if (!reader->PopUint64(&value)) | 152 if (!reader->PopUint64(&value)) |
| 154 return kBrokenMessage; | 153 return kBrokenMessage; |
| 155 output += (indent + "uint64 " + | 154 output += (indent + "uint64 " + base::Uint64ToString(value) + "\n"); |
| 156 base::StringPrintf("%" PRIu64, value) + "\n"); | |
| 157 break; | 155 break; |
| 158 } | 156 } |
| 159 case DOUBLE: { | 157 case DOUBLE: { |
| 160 double value = 0; | 158 double value = 0; |
| 161 if (!reader->PopDouble(&value)) | 159 if (!reader->PopDouble(&value)) |
| 162 return kBrokenMessage; | 160 return kBrokenMessage; |
| 163 output += indent + "double " + base::StringPrintf("%f", value) + "\n"; | 161 output += indent + "double " + base::DoubleToString(value) + "\n"; |
| 164 break; | 162 break; |
| 165 } | 163 } |
| 166 case STRING: { | 164 case STRING: { |
| 167 std::string value; | 165 std::string value; |
| 168 if (!reader->PopString(&value)) | 166 if (!reader->PopString(&value)) |
| 169 return kBrokenMessage; | 167 return kBrokenMessage; |
| 170 // Truncate if the string is longer than the limit. | 168 // Truncate if the string is longer than the limit. |
| 171 const size_t kTruncateLength = 100; | 169 const size_t kTruncateLength = 100; |
| 172 if (value.size() < kTruncateLength) { | 170 if (value.size() < kTruncateLength) { |
| 173 output += indent + "string \"" + value + "\"\n"; | 171 output += indent + "string \"" + value + "\"\n"; |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 | 687 |
| 690 const bool success = dbus_message_iter_append_basic( | 688 const bool success = dbus_message_iter_append_basic( |
| 691 &raw_message_iter_, dbus_type, value); | 689 &raw_message_iter_, dbus_type, value); |
| 692 // dbus_message_iter_append_basic() fails only when there is not enough | 690 // dbus_message_iter_append_basic() fails only when there is not enough |
| 693 // memory. We don't return this error as there is nothing we can do when | 691 // memory. We don't return this error as there is nothing we can do when |
| 694 // it fails to allocate memory for a byte etc. | 692 // it fails to allocate memory for a byte etc. |
| 695 CHECK(success) << "Unable to allocate memory"; | 693 CHECK(success) << "Unable to allocate memory"; |
| 696 } | 694 } |
| 697 | 695 |
| 698 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { | 696 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { |
| 699 const std::string signature = base::StringPrintf("%c", dbus_type); | 697 const std::string signature(1u, // length |
| 698 base::checked_cast<char>(dbus_type)); |
| 700 MessageWriter variant_writer(message_); | 699 MessageWriter variant_writer(message_); |
| 701 OpenVariant(signature, &variant_writer); | 700 OpenVariant(signature, &variant_writer); |
| 702 variant_writer.AppendBasic(dbus_type, value); | 701 variant_writer.AppendBasic(dbus_type, value); |
| 703 CloseContainer(&variant_writer); | 702 CloseContainer(&variant_writer); |
| 704 } | 703 } |
| 705 | 704 |
| 706 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { | 705 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { |
| 707 CHECK(IsDBusTypeUnixFdSupported()); | 706 CHECK(IsDBusTypeUnixFdSupported()); |
| 708 | 707 |
| 709 if (!value.is_valid()) { | 708 if (!value.is_valid()) { |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 993 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); | 992 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); |
| 994 if (!success) | 993 if (!success) |
| 995 return false; | 994 return false; |
| 996 | 995 |
| 997 value->PutValue(fd); | 996 value->PutValue(fd); |
| 998 // NB: the caller must check validity before using the value | 997 // NB: the caller must check validity before using the value |
| 999 return true; | 998 return true; |
| 1000 } | 999 } |
| 1001 | 1000 |
| 1002 } // namespace dbus | 1001 } // namespace dbus |
| OLD | NEW |