keybuk@chromium.org | e34b29e | 2012-02-08 08:48:35 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "dbus/message.h" |
| 6 | |
rharrison@chromium.org | 3d53035 | 2012-02-10 03:14:08 +0900 | [diff] [blame] | 7 | #include <string> |
| 8 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 9 | #include "base/format_macros.h" |
| 10 | #include "base/logging.h" |
ricea | 1bd48b9 | 2015-09-19 07:26:10 +0900 | [diff] [blame] | 11 | #include "base/numerics/safe_conversions.h" |
ricea | 6d265ec | 2015-07-02 00:56:50 +0900 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
avi@chromium.org | ffcdb95 | 2013-06-11 16:27:01 +0900 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
| 14 | #include "base/strings/stringprintf.h" |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 15 | #include "dbus/object_path.h" |
rharrison@chromium.org | 3d53035 | 2012-02-10 03:14:08 +0900 | [diff] [blame] | 16 | #include "third_party/protobuf/src/google/protobuf/message_lite.h" |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 17 | |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | // Appends the header name and the value to |output|, if the value is |
| 21 | // not empty. |
davemoore@chromium.org | b8ebdc6 | 2012-07-26 04:29:57 +0900 | [diff] [blame] | 22 | void AppendStringHeader(const std::string& header_name, |
| 23 | const std::string& header_value, |
| 24 | std::string* output) { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 25 | if (!header_value.empty()) { |
| 26 | *output += header_name + ": " + header_value + "\n"; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Appends the header name and the value to |output|, if the value is |
| 31 | // nonzero. |
davemoore@chromium.org | b8ebdc6 | 2012-07-26 04:29:57 +0900 | [diff] [blame] | 32 | void AppendUint32Header(const std::string& header_name, |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 33 | uint32_t header_value, |
davemoore@chromium.org | b8ebdc6 | 2012-07-26 04:29:57 +0900 | [diff] [blame] | 34 | std::string* output) { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 35 | if (header_value != 0) { |
ricea | 1bd48b9 | 2015-09-19 07:26:10 +0900 | [diff] [blame] | 36 | *output += (header_name + ": " + base::UintToString(header_value) + "\n"); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | } // namespace |
| 41 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 42 | namespace dbus { |
| 43 | |
davemoore@chromium.org | b8ebdc6 | 2012-07-26 04:29:57 +0900 | [diff] [blame] | 44 | bool IsDBusTypeUnixFdSupported() { |
| 45 | int major = 0, minor = 0, micro = 0; |
| 46 | dbus_get_version(&major, &minor, µ); |
| 47 | return major >= 1 && minor >= 4; |
| 48 | } |
| 49 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 50 | Message::Message() |
| 51 | : raw_message_(NULL) { |
| 52 | } |
| 53 | |
| 54 | Message::~Message() { |
| 55 | if (raw_message_) |
| 56 | dbus_message_unref(raw_message_); |
| 57 | } |
| 58 | |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 59 | void Message::Init(DBusMessage* raw_message) { |
| 60 | DCHECK(!raw_message_); |
| 61 | raw_message_ = raw_message; |
| 62 | } |
| 63 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 64 | Message::MessageType Message::GetMessageType() { |
| 65 | if (!raw_message_) |
| 66 | return MESSAGE_INVALID; |
| 67 | const int type = dbus_message_get_type(raw_message_); |
| 68 | return static_cast<Message::MessageType>(type); |
| 69 | } |
| 70 | |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 71 | std::string Message::GetMessageTypeAsString() { |
| 72 | switch (GetMessageType()) { |
| 73 | case MESSAGE_INVALID: |
| 74 | return "MESSAGE_INVALID"; |
| 75 | case MESSAGE_METHOD_CALL: |
| 76 | return "MESSAGE_METHOD_CALL"; |
| 77 | case MESSAGE_METHOD_RETURN: |
| 78 | return "MESSAGE_METHOD_RETURN"; |
| 79 | case MESSAGE_SIGNAL: |
| 80 | return "MESSAGE_SIGNAL"; |
| 81 | case MESSAGE_ERROR: |
| 82 | return "MESSAGE_ERROR"; |
| 83 | } |
| 84 | NOTREACHED(); |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 85 | return std::string(); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | std::string Message::ToStringInternal(const std::string& indent, |
| 89 | MessageReader* reader) { |
| 90 | const char* kBrokenMessage = "[broken message]"; |
| 91 | std::string output; |
| 92 | while (reader->HasMoreData()) { |
| 93 | const DataType type = reader->GetDataType(); |
| 94 | switch (type) { |
| 95 | case BYTE: { |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 96 | uint8_t value = 0; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 97 | if (!reader->PopByte(&value)) |
| 98 | return kBrokenMessage; |
ricea | 1bd48b9 | 2015-09-19 07:26:10 +0900 | [diff] [blame] | 99 | output += indent + "byte " + base::UintToString(value) + "\n"; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 100 | break; |
| 101 | } |
| 102 | case BOOL: { |
| 103 | bool value = false; |
| 104 | if (!reader->PopBool(&value)) |
| 105 | return kBrokenMessage; |
| 106 | output += indent + "bool " + (value ? "true" : "false") + "\n"; |
| 107 | break; |
| 108 | } |
| 109 | case INT16: { |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 110 | int16_t value = 0; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 111 | if (!reader->PopInt16(&value)) |
| 112 | return kBrokenMessage; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 113 | output += indent + "int16_t " + base::IntToString(value) + "\n"; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | case UINT16: { |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 117 | uint16_t value = 0; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 118 | if (!reader->PopUint16(&value)) |
| 119 | return kBrokenMessage; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 120 | output += indent + "uint16_t " + base::UintToString(value) + "\n"; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | case INT32: { |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 124 | int32_t value = 0; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 125 | if (!reader->PopInt32(&value)) |
| 126 | return kBrokenMessage; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 127 | output += indent + "int32_t " + base::IntToString(value) + "\n"; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | case UINT32: { |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 131 | uint32_t value = 0; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 132 | if (!reader->PopUint32(&value)) |
| 133 | return kBrokenMessage; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 134 | output += indent + "uint32_t " + base::UintToString(value) + "\n"; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | case INT64: { |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 138 | int64_t value = 0; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 139 | if (!reader->PopInt64(&value)) |
| 140 | return kBrokenMessage; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 141 | output += (indent + "int64_t " + base::Int64ToString(value) + "\n"); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | case UINT64: { |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 145 | uint64_t value = 0; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 146 | if (!reader->PopUint64(&value)) |
| 147 | return kBrokenMessage; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 148 | output += (indent + "uint64_t " + base::Uint64ToString(value) + "\n"); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | case DOUBLE: { |
| 152 | double value = 0; |
| 153 | if (!reader->PopDouble(&value)) |
| 154 | return kBrokenMessage; |
ricea | 1bd48b9 | 2015-09-19 07:26:10 +0900 | [diff] [blame] | 155 | output += indent + "double " + base::DoubleToString(value) + "\n"; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | case STRING: { |
| 159 | std::string value; |
| 160 | if (!reader->PopString(&value)) |
| 161 | return kBrokenMessage; |
satorux@chromium.org | 6a7aa37 | 2012-06-08 01:41:26 +0900 | [diff] [blame] | 162 | // Truncate if the string is longer than the limit. |
| 163 | const size_t kTruncateLength = 100; |
| 164 | if (value.size() < kTruncateLength) { |
| 165 | output += indent + "string \"" + value + "\"\n"; |
| 166 | } else { |
| 167 | std::string truncated; |
brettw@chromium.org | 1f044f3 | 2013-12-04 07:05:28 +0900 | [diff] [blame] | 168 | base::TruncateUTF8ToByteSize(value, kTruncateLength, &truncated); |
thakis@chromium.org | f3b469a | 2013-07-31 11:29:01 +0900 | [diff] [blame] | 169 | base::StringAppendF(&truncated, "... (%" PRIuS " bytes in total)", |
satorux@chromium.org | 6a7aa37 | 2012-06-08 01:41:26 +0900 | [diff] [blame] | 170 | value.size()); |
| 171 | output += indent + "string \"" + truncated + "\"\n"; |
| 172 | } |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | case OBJECT_PATH: { |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 176 | ObjectPath value; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 177 | if (!reader->PopObjectPath(&value)) |
| 178 | return kBrokenMessage; |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 179 | output += indent + "object_path \"" + value.value() + "\"\n"; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | case ARRAY: { |
| 183 | MessageReader sub_reader(this); |
| 184 | if (!reader->PopArray(&sub_reader)) |
| 185 | return kBrokenMessage; |
| 186 | output += indent + "array [\n"; |
| 187 | output += ToStringInternal(indent + " ", &sub_reader); |
| 188 | output += indent + "]\n"; |
| 189 | break; |
| 190 | } |
| 191 | case STRUCT: { |
| 192 | MessageReader sub_reader(this); |
| 193 | if (!reader->PopStruct(&sub_reader)) |
| 194 | return kBrokenMessage; |
| 195 | output += indent + "struct {\n"; |
| 196 | output += ToStringInternal(indent + " ", &sub_reader); |
| 197 | output += indent + "}\n"; |
| 198 | break; |
| 199 | } |
| 200 | case DICT_ENTRY: { |
| 201 | MessageReader sub_reader(this); |
| 202 | if (!reader->PopDictEntry(&sub_reader)) |
| 203 | return kBrokenMessage; |
| 204 | output += indent + "dict entry {\n"; |
| 205 | output += ToStringInternal(indent + " ", &sub_reader); |
| 206 | output += indent + "}\n"; |
| 207 | break; |
| 208 | } |
| 209 | case VARIANT: { |
| 210 | MessageReader sub_reader(this); |
| 211 | if (!reader->PopVariant(&sub_reader)) |
| 212 | return kBrokenMessage; |
| 213 | output += indent + "variant "; |
| 214 | output += ToStringInternal(indent + " ", &sub_reader); |
| 215 | break; |
| 216 | } |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 217 | case UNIX_FD: { |
davemoore@chromium.org | b8ebdc6 | 2012-07-26 04:29:57 +0900 | [diff] [blame] | 218 | CHECK(IsDBusTypeUnixFdSupported()); |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 219 | |
hashimoto | 6550fcf | 2016-09-14 13:05:02 +0900 | [diff] [blame] | 220 | base::ScopedFD file_descriptor; |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 221 | if (!reader->PopFileDescriptor(&file_descriptor)) |
| 222 | return kBrokenMessage; |
| 223 | output += indent + "fd#" + |
hashimoto | 6550fcf | 2016-09-14 13:05:02 +0900 | [diff] [blame] | 224 | base::IntToString(file_descriptor.get()) + "\n"; |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 225 | break; |
| 226 | } |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 227 | default: |
| 228 | LOG(FATAL) << "Unknown type: " << type; |
| 229 | } |
| 230 | } |
| 231 | return output; |
| 232 | } |
| 233 | |
| 234 | // The returned string consists of message headers such as |
| 235 | // destination if any, followed by a blank line, and the message |
| 236 | // payload. For example, a MethodCall's ToString() will look like: |
| 237 | // |
| 238 | // destination: com.example.Service |
| 239 | // path: /com/example/Object |
| 240 | // interface: com.example.Interface |
| 241 | // member: SomeMethod |
| 242 | // |
| 243 | // string \"payload\" |
| 244 | // ... |
| 245 | std::string Message::ToString() { |
| 246 | if (!raw_message_) |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 247 | return std::string(); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 248 | |
| 249 | // Generate headers first. |
| 250 | std::string headers; |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 251 | AppendStringHeader("message_type", GetMessageTypeAsString(), &headers); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 252 | AppendStringHeader("destination", GetDestination(), &headers); |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 253 | AppendStringHeader("path", GetPath().value(), &headers); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 254 | AppendStringHeader("interface", GetInterface(), &headers); |
| 255 | AppendStringHeader("member", GetMember(), &headers); |
| 256 | AppendStringHeader("error_name", GetErrorName(), &headers); |
| 257 | AppendStringHeader("sender", GetSender(), &headers); |
benchan | befafc8 | 2014-08-25 20:16:56 +0900 | [diff] [blame] | 258 | AppendStringHeader("signature", GetSignature(), &headers); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 259 | AppendUint32Header("serial", GetSerial(), &headers); |
| 260 | AppendUint32Header("reply_serial", GetReplySerial(), &headers); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 261 | |
| 262 | // Generate the payload. |
| 263 | MessageReader reader(this); |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 264 | return headers + "\n" + ToStringInternal(std::string(), &reader); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 265 | } |
| 266 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 267 | bool Message::SetDestination(const std::string& destination) { |
| 268 | return dbus_message_set_destination(raw_message_, destination.c_str()); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 269 | } |
| 270 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 271 | bool Message::SetPath(const ObjectPath& path) { |
| 272 | return dbus_message_set_path(raw_message_, path.value().c_str()); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 273 | } |
| 274 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 275 | bool Message::SetInterface(const std::string& interface) { |
| 276 | return dbus_message_set_interface(raw_message_, interface.c_str()); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 277 | } |
| 278 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 279 | bool Message::SetMember(const std::string& member) { |
| 280 | return dbus_message_set_member(raw_message_, member.c_str()); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 281 | } |
| 282 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 283 | bool Message::SetErrorName(const std::string& error_name) { |
| 284 | return dbus_message_set_error_name(raw_message_, error_name.c_str()); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 285 | } |
| 286 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 287 | bool Message::SetSender(const std::string& sender) { |
| 288 | return dbus_message_set_sender(raw_message_, sender.c_str()); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 289 | } |
| 290 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 291 | void Message::SetSerial(uint32_t serial) { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 292 | dbus_message_set_serial(raw_message_, serial); |
| 293 | } |
| 294 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 295 | void Message::SetReplySerial(uint32_t reply_serial) { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 296 | dbus_message_set_reply_serial(raw_message_, reply_serial); |
| 297 | } |
| 298 | |
| 299 | std::string Message::GetDestination() { |
| 300 | const char* destination = dbus_message_get_destination(raw_message_); |
| 301 | return destination ? destination : ""; |
| 302 | } |
| 303 | |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 304 | ObjectPath Message::GetPath() { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 305 | const char* path = dbus_message_get_path(raw_message_); |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 306 | return ObjectPath(path ? path : ""); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | std::string Message::GetInterface() { |
| 310 | const char* interface = dbus_message_get_interface(raw_message_); |
| 311 | return interface ? interface : ""; |
| 312 | } |
| 313 | |
| 314 | std::string Message::GetMember() { |
| 315 | const char* member = dbus_message_get_member(raw_message_); |
| 316 | return member ? member : ""; |
| 317 | } |
| 318 | |
| 319 | std::string Message::GetErrorName() { |
| 320 | const char* error_name = dbus_message_get_error_name(raw_message_); |
| 321 | return error_name ? error_name : ""; |
| 322 | } |
| 323 | |
| 324 | std::string Message::GetSender() { |
| 325 | const char* sender = dbus_message_get_sender(raw_message_); |
| 326 | return sender ? sender : ""; |
| 327 | } |
| 328 | |
benchan | befafc8 | 2014-08-25 20:16:56 +0900 | [diff] [blame] | 329 | std::string Message::GetSignature() { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 330 | const char* signature = dbus_message_get_signature(raw_message_); |
| 331 | return signature ? signature : ""; |
| 332 | } |
| 333 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 334 | uint32_t Message::GetSerial() { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 335 | return dbus_message_get_serial(raw_message_); |
| 336 | } |
| 337 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 338 | uint32_t Message::GetReplySerial() { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 339 | return dbus_message_get_reply_serial(raw_message_); |
| 340 | } |
| 341 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 342 | // |
| 343 | // MethodCall implementation. |
| 344 | // |
| 345 | |
| 346 | MethodCall::MethodCall(const std::string& interface_name, |
| 347 | const std::string& method_name) |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 348 | : Message() { |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 349 | Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 350 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 351 | CHECK(SetInterface(interface_name)); |
| 352 | CHECK(SetMember(method_name)); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 353 | } |
| 354 | |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 355 | MethodCall::MethodCall() : Message() { |
| 356 | } |
| 357 | |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 358 | MethodCall* MethodCall::FromRawMessage(DBusMessage* raw_message) { |
| 359 | DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message)); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 360 | |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 361 | MethodCall* method_call = new MethodCall; |
| 362 | method_call->Init(raw_message); |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 363 | return method_call; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 367 | // Signal implementation. |
| 368 | // |
| 369 | Signal::Signal(const std::string& interface_name, |
| 370 | const std::string& method_name) |
| 371 | : Message() { |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 372 | Init(dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL)); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 373 | |
hashimoto@chromium.org | b030551 | 2012-05-23 15:55:22 +0900 | [diff] [blame] | 374 | CHECK(SetInterface(interface_name)); |
| 375 | CHECK(SetMember(method_name)); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 376 | } |
| 377 | |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 378 | Signal::Signal() : Message() { |
| 379 | } |
| 380 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 381 | Signal* Signal::FromRawMessage(DBusMessage* raw_message) { |
| 382 | DCHECK_EQ(DBUS_MESSAGE_TYPE_SIGNAL, dbus_message_get_type(raw_message)); |
| 383 | |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 384 | Signal* signal = new Signal; |
| 385 | signal->Init(raw_message); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 386 | return signal; |
| 387 | } |
| 388 | |
| 389 | // |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 390 | // Response implementation. |
| 391 | // |
| 392 | |
| 393 | Response::Response() : Message() { |
| 394 | } |
| 395 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 396 | std::unique_ptr<Response> Response::FromRawMessage(DBusMessage* raw_message) { |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 397 | DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_RETURN, |
| 398 | dbus_message_get_type(raw_message)); |
| 399 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 400 | std::unique_ptr<Response> response(new Response); |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 401 | response->Init(raw_message); |
dcheng | 6c1ffcf | 2015-12-28 11:24:50 +0900 | [diff] [blame] | 402 | return response; |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 403 | } |
| 404 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 405 | std::unique_ptr<Response> Response::FromMethodCall(MethodCall* method_call) { |
| 406 | std::unique_ptr<Response> response(new Response); |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 407 | response->Init(dbus_message_new_method_return(method_call->raw_message())); |
dcheng | 6c1ffcf | 2015-12-28 11:24:50 +0900 | [diff] [blame] | 408 | return response; |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 409 | } |
| 410 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 411 | std::unique_ptr<Response> Response::CreateEmpty() { |
| 412 | std::unique_ptr<Response> response(new Response); |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 413 | response->Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN)); |
dcheng | 6c1ffcf | 2015-12-28 11:24:50 +0900 | [diff] [blame] | 414 | return response; |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 415 | } |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 416 | |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 417 | // |
| 418 | // ErrorResponse implementation. |
| 419 | // |
| 420 | |
keybuk@chromium.org | b21d2b7 | 2012-03-11 10:12:00 +0900 | [diff] [blame] | 421 | ErrorResponse::ErrorResponse() : Response() { |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 422 | } |
| 423 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 424 | std::unique_ptr<ErrorResponse> ErrorResponse::FromRawMessage( |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 425 | DBusMessage* raw_message) { |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 426 | DCHECK_EQ(DBUS_MESSAGE_TYPE_ERROR, dbus_message_get_type(raw_message)); |
| 427 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 428 | std::unique_ptr<ErrorResponse> response(new ErrorResponse); |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 429 | response->Init(raw_message); |
dcheng | 6c1ffcf | 2015-12-28 11:24:50 +0900 | [diff] [blame] | 430 | return response; |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 431 | } |
| 432 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 433 | std::unique_ptr<ErrorResponse> ErrorResponse::FromMethodCall( |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 434 | MethodCall* method_call, |
| 435 | const std::string& error_name, |
| 436 | const std::string& error_message) { |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 437 | std::unique_ptr<ErrorResponse> response(new ErrorResponse); |
satorux@chromium.org | ffa83a9 | 2011-08-24 12:32:06 +0900 | [diff] [blame] | 438 | response->Init(dbus_message_new_error(method_call->raw_message(), |
| 439 | error_name.c_str(), |
| 440 | error_message.c_str())); |
dcheng | 6c1ffcf | 2015-12-28 11:24:50 +0900 | [diff] [blame] | 441 | return response; |
satorux@chromium.org | 8facb24 | 2011-08-11 07:34:02 +0900 | [diff] [blame] | 442 | } |
| 443 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 444 | // |
| 445 | // MessageWriter implementation. |
| 446 | // |
| 447 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 448 | MessageWriter::MessageWriter(Message* message) |
| 449 | : message_(message), |
| 450 | container_is_open_(false) { |
jhawkins@chromium.org | 69bde42 | 2012-02-13 13:12:40 +0900 | [diff] [blame] | 451 | memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); |
keybuk@chromium.org | e34b29e | 2012-02-08 08:48:35 +0900 | [diff] [blame] | 452 | if (message) |
| 453 | dbus_message_iter_init_append(message_->raw_message(), &raw_message_iter_); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | MessageWriter::~MessageWriter() { |
| 457 | } |
| 458 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 459 | void MessageWriter::AppendByte(uint8_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 460 | AppendBasic(DBUS_TYPE_BYTE, &value); |
| 461 | } |
| 462 | |
| 463 | void MessageWriter::AppendBool(bool value) { |
satorux@chromium.org | a566a2c | 2011-08-05 06:57:21 +0900 | [diff] [blame] | 464 | // The size of dbus_bool_t and the size of bool are different. The |
| 465 | // former is always 4 per dbus-types.h, whereas the latter is usually 1. |
| 466 | // dbus_message_iter_append_basic() used in AppendBasic() expects four |
| 467 | // bytes for DBUS_TYPE_BOOLEAN, so we must pass a dbus_bool_t, instead |
| 468 | // of a bool, to AppendBasic(). |
| 469 | dbus_bool_t dbus_value = value; |
| 470 | AppendBasic(DBUS_TYPE_BOOLEAN, &dbus_value); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 471 | } |
| 472 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 473 | void MessageWriter::AppendInt16(int16_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 474 | AppendBasic(DBUS_TYPE_INT16, &value); |
| 475 | } |
| 476 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 477 | void MessageWriter::AppendUint16(uint16_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 478 | AppendBasic(DBUS_TYPE_UINT16, &value); |
| 479 | } |
| 480 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 481 | void MessageWriter::AppendInt32(int32_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 482 | AppendBasic(DBUS_TYPE_INT32, &value); |
| 483 | } |
| 484 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 485 | void MessageWriter::AppendUint32(uint32_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 486 | AppendBasic(DBUS_TYPE_UINT32, &value); |
| 487 | } |
| 488 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 489 | void MessageWriter::AppendInt64(int64_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 490 | AppendBasic(DBUS_TYPE_INT64, &value); |
| 491 | } |
| 492 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 493 | void MessageWriter::AppendUint64(uint64_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 494 | AppendBasic(DBUS_TYPE_UINT64, &value); |
| 495 | } |
| 496 | |
| 497 | void MessageWriter::AppendDouble(double value) { |
| 498 | AppendBasic(DBUS_TYPE_DOUBLE, &value); |
| 499 | } |
| 500 | |
| 501 | void MessageWriter::AppendString(const std::string& value) { |
hashimoto@chromium.org | 5fdbcf7 | 2012-06-05 13:15:50 +0900 | [diff] [blame] | 502 | // D-Bus Specification (0.19) says a string "must be valid UTF-8". |
brettw@chromium.org | 89f697b | 2014-05-07 23:38:26 +0900 | [diff] [blame] | 503 | CHECK(base::IsStringUTF8(value)); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 504 | const char* pointer = value.c_str(); |
| 505 | AppendBasic(DBUS_TYPE_STRING, &pointer); |
| 506 | // TODO(satorux): It may make sense to return an error here, as the |
| 507 | // input string can be large. If needed, we could add something like |
| 508 | // bool AppendStringWithErrorChecking(). |
| 509 | } |
| 510 | |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 511 | void MessageWriter::AppendObjectPath(const ObjectPath& value) { |
hashimoto@chromium.org | 5fdbcf7 | 2012-06-05 13:15:50 +0900 | [diff] [blame] | 512 | CHECK(value.IsValid()); |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 513 | const char* pointer = value.value().c_str(); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 514 | AppendBasic(DBUS_TYPE_OBJECT_PATH, &pointer); |
| 515 | } |
| 516 | |
| 517 | // Ideally, client shouldn't need to supply the signature string, but |
| 518 | // the underlying D-Bus library requires us to supply this before |
| 519 | // appending contents to array and variant. It's technically possible |
| 520 | // for us to design API that doesn't require the signature but it will |
| 521 | // complicate the implementation so we decided to have the signature |
| 522 | // parameter. Hopefully, variants are less used in request messages from |
| 523 | // client side than response message from server side, so this should |
| 524 | // not be a big issue. |
| 525 | void MessageWriter::OpenArray(const std::string& signature, |
| 526 | MessageWriter* writer) { |
| 527 | DCHECK(!container_is_open_); |
| 528 | |
| 529 | const bool success = dbus_message_iter_open_container( |
| 530 | &raw_message_iter_, |
| 531 | DBUS_TYPE_ARRAY, |
| 532 | signature.c_str(), |
| 533 | &writer->raw_message_iter_); |
| 534 | CHECK(success) << "Unable to allocate memory"; |
| 535 | container_is_open_ = true; |
| 536 | } |
| 537 | |
| 538 | void MessageWriter::OpenVariant(const std::string& signature, |
| 539 | MessageWriter* writer) { |
| 540 | DCHECK(!container_is_open_); |
| 541 | |
| 542 | const bool success = dbus_message_iter_open_container( |
| 543 | &raw_message_iter_, |
| 544 | DBUS_TYPE_VARIANT, |
| 545 | signature.c_str(), |
| 546 | &writer->raw_message_iter_); |
| 547 | CHECK(success) << "Unable to allocate memory"; |
| 548 | container_is_open_ = true; |
| 549 | } |
| 550 | |
| 551 | void MessageWriter::OpenStruct(MessageWriter* writer) { |
| 552 | DCHECK(!container_is_open_); |
| 553 | |
| 554 | const bool success = dbus_message_iter_open_container( |
| 555 | &raw_message_iter_, |
| 556 | DBUS_TYPE_STRUCT, |
| 557 | NULL, // Signature should be NULL. |
| 558 | &writer->raw_message_iter_); |
| 559 | CHECK(success) << "Unable to allocate memory"; |
| 560 | container_is_open_ = true; |
| 561 | } |
| 562 | |
| 563 | void MessageWriter::OpenDictEntry(MessageWriter* writer) { |
| 564 | DCHECK(!container_is_open_); |
| 565 | |
| 566 | const bool success = dbus_message_iter_open_container( |
| 567 | &raw_message_iter_, |
| 568 | DBUS_TYPE_DICT_ENTRY, |
| 569 | NULL, // Signature should be NULL. |
| 570 | &writer->raw_message_iter_); |
| 571 | CHECK(success) << "Unable to allocate memory"; |
| 572 | container_is_open_ = true; |
| 573 | } |
| 574 | |
| 575 | void MessageWriter::CloseContainer(MessageWriter* writer) { |
| 576 | DCHECK(container_is_open_); |
| 577 | |
| 578 | const bool success = dbus_message_iter_close_container( |
| 579 | &raw_message_iter_, &writer->raw_message_iter_); |
| 580 | CHECK(success) << "Unable to allocate memory"; |
| 581 | container_is_open_ = false; |
| 582 | } |
| 583 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 584 | void MessageWriter::AppendArrayOfBytes(const uint8_t* values, size_t length) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 585 | DCHECK(!container_is_open_); |
| 586 | MessageWriter array_writer(message_); |
| 587 | OpenArray("y", &array_writer); |
| 588 | const bool success = dbus_message_iter_append_fixed_array( |
| 589 | &(array_writer.raw_message_iter_), |
| 590 | DBUS_TYPE_BYTE, |
| 591 | &values, |
| 592 | static_cast<int>(length)); |
| 593 | CHECK(success) << "Unable to allocate memory"; |
| 594 | CloseContainer(&array_writer); |
| 595 | } |
| 596 | |
warx | ef0f837 | 2016-03-31 06:27:41 +0900 | [diff] [blame] | 597 | void MessageWriter::AppendArrayOfDoubles(const double* values, size_t length) { |
| 598 | DCHECK(!container_is_open_); |
| 599 | MessageWriter array_writer(message_); |
| 600 | OpenArray("d", &array_writer); |
| 601 | const bool success = dbus_message_iter_append_fixed_array( |
| 602 | &(array_writer.raw_message_iter_), |
| 603 | DBUS_TYPE_DOUBLE, |
| 604 | &values, |
| 605 | static_cast<int>(length)); |
| 606 | CHECK(success) << "Unable to allocate memory"; |
| 607 | CloseContainer(&array_writer); |
| 608 | } |
| 609 | |
mdm@chromium.org | 3d2baf0 | 2011-09-20 03:22:14 +0900 | [diff] [blame] | 610 | void MessageWriter::AppendArrayOfStrings( |
| 611 | const std::vector<std::string>& strings) { |
| 612 | DCHECK(!container_is_open_); |
| 613 | MessageWriter array_writer(message_); |
| 614 | OpenArray("s", &array_writer); |
| 615 | for (size_t i = 0; i < strings.size(); ++i) { |
| 616 | array_writer.AppendString(strings[i]); |
| 617 | } |
| 618 | CloseContainer(&array_writer); |
| 619 | } |
| 620 | |
satorux@chromium.org | f0ceb48 | 2011-08-23 03:28:42 +0900 | [diff] [blame] | 621 | void MessageWriter::AppendArrayOfObjectPaths( |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 622 | const std::vector<ObjectPath>& object_paths) { |
satorux@chromium.org | f0ceb48 | 2011-08-23 03:28:42 +0900 | [diff] [blame] | 623 | DCHECK(!container_is_open_); |
| 624 | MessageWriter array_writer(message_); |
| 625 | OpenArray("o", &array_writer); |
| 626 | for (size_t i = 0; i < object_paths.size(); ++i) { |
| 627 | array_writer.AppendObjectPath(object_paths[i]); |
| 628 | } |
| 629 | CloseContainer(&array_writer); |
| 630 | } |
| 631 | |
rharrison@chromium.org | 3d53035 | 2012-02-10 03:14:08 +0900 | [diff] [blame] | 632 | bool MessageWriter::AppendProtoAsArrayOfBytes( |
| 633 | const google::protobuf::MessageLite& protobuf) { |
| 634 | std::string serialized_proto; |
| 635 | if (!protobuf.SerializeToString(&serialized_proto)) { |
| 636 | LOG(ERROR) << "Unable to serialize supplied protocol buffer"; |
| 637 | return false; |
| 638 | } |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 639 | AppendArrayOfBytes(reinterpret_cast<const uint8_t*>(serialized_proto.data()), |
rharrison@chromium.org | 3d53035 | 2012-02-10 03:14:08 +0900 | [diff] [blame] | 640 | serialized_proto.size()); |
| 641 | return true; |
| 642 | } |
| 643 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 644 | void MessageWriter::AppendVariantOfByte(uint8_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 645 | AppendVariantOfBasic(DBUS_TYPE_BYTE, &value); |
| 646 | } |
| 647 | |
| 648 | void MessageWriter::AppendVariantOfBool(bool value) { |
satorux@chromium.org | a566a2c | 2011-08-05 06:57:21 +0900 | [diff] [blame] | 649 | // See the comment at MessageWriter::AppendBool(). |
| 650 | dbus_bool_t dbus_value = value; |
| 651 | AppendVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 652 | } |
| 653 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 654 | void MessageWriter::AppendVariantOfInt16(int16_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 655 | AppendVariantOfBasic(DBUS_TYPE_INT16, &value); |
| 656 | } |
| 657 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 658 | void MessageWriter::AppendVariantOfUint16(uint16_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 659 | AppendVariantOfBasic(DBUS_TYPE_UINT16, &value); |
| 660 | } |
| 661 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 662 | void MessageWriter::AppendVariantOfInt32(int32_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 663 | AppendVariantOfBasic(DBUS_TYPE_INT32, &value); |
| 664 | } |
| 665 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 666 | void MessageWriter::AppendVariantOfUint32(uint32_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 667 | AppendVariantOfBasic(DBUS_TYPE_UINT32, &value); |
| 668 | } |
| 669 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 670 | void MessageWriter::AppendVariantOfInt64(int64_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 671 | AppendVariantOfBasic(DBUS_TYPE_INT64, &value); |
| 672 | } |
| 673 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 674 | void MessageWriter::AppendVariantOfUint64(uint64_t value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 675 | AppendVariantOfBasic(DBUS_TYPE_UINT64, &value); |
| 676 | } |
| 677 | |
| 678 | void MessageWriter::AppendVariantOfDouble(double value) { |
| 679 | AppendVariantOfBasic(DBUS_TYPE_DOUBLE, &value); |
| 680 | } |
| 681 | |
| 682 | void MessageWriter::AppendVariantOfString(const std::string& value) { |
| 683 | const char* pointer = value.c_str(); |
| 684 | AppendVariantOfBasic(DBUS_TYPE_STRING, &pointer); |
| 685 | } |
| 686 | |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 687 | void MessageWriter::AppendVariantOfObjectPath(const ObjectPath& value) { |
| 688 | const char* pointer = value.value().c_str(); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 689 | AppendVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &pointer); |
| 690 | } |
| 691 | |
| 692 | void MessageWriter::AppendBasic(int dbus_type, const void* value) { |
| 693 | DCHECK(!container_is_open_); |
| 694 | |
| 695 | const bool success = dbus_message_iter_append_basic( |
| 696 | &raw_message_iter_, dbus_type, value); |
| 697 | // dbus_message_iter_append_basic() fails only when there is not enough |
| 698 | // memory. We don't return this error as there is nothing we can do when |
| 699 | // it fails to allocate memory for a byte etc. |
| 700 | CHECK(success) << "Unable to allocate memory"; |
| 701 | } |
| 702 | |
| 703 | void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { |
ricea | 1bd48b9 | 2015-09-19 07:26:10 +0900 | [diff] [blame] | 704 | const std::string signature(1u, // length |
| 705 | base::checked_cast<char>(dbus_type)); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 706 | MessageWriter variant_writer(message_); |
| 707 | OpenVariant(signature, &variant_writer); |
| 708 | variant_writer.AppendBasic(dbus_type, value); |
| 709 | CloseContainer(&variant_writer); |
| 710 | } |
| 711 | |
hashimoto | dac22fb | 2016-08-25 01:51:38 +0900 | [diff] [blame] | 712 | void MessageWriter::AppendFileDescriptor(int value) { |
| 713 | CHECK(IsDBusTypeUnixFdSupported()); |
| 714 | AppendBasic(DBUS_TYPE_UNIX_FD, &value); // This duplicates the FD. |
| 715 | } |
| 716 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 717 | // |
| 718 | // MessageReader implementation. |
| 719 | // |
| 720 | |
| 721 | MessageReader::MessageReader(Message* message) |
| 722 | : message_(message) { |
jhawkins@chromium.org | 69bde42 | 2012-02-13 13:12:40 +0900 | [diff] [blame] | 723 | memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); |
keybuk@chromium.org | e34b29e | 2012-02-08 08:48:35 +0900 | [diff] [blame] | 724 | if (message) |
| 725 | dbus_message_iter_init(message_->raw_message(), &raw_message_iter_); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | |
| 729 | MessageReader::~MessageReader() { |
| 730 | } |
| 731 | |
| 732 | bool MessageReader::HasMoreData() { |
| 733 | const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_); |
| 734 | return dbus_type != DBUS_TYPE_INVALID; |
| 735 | } |
| 736 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 737 | bool MessageReader::PopByte(uint8_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 738 | return PopBasic(DBUS_TYPE_BYTE, value); |
| 739 | } |
| 740 | |
| 741 | bool MessageReader::PopBool(bool* value) { |
satorux@chromium.org | a566a2c | 2011-08-05 06:57:21 +0900 | [diff] [blame] | 742 | // Like MessageWriter::AppendBool(), we should copy |value| to |
| 743 | // dbus_bool_t, as dbus_message_iter_get_basic() used in PopBasic() |
| 744 | // expects four bytes for DBUS_TYPE_BOOLEAN. |
| 745 | dbus_bool_t dbus_value = FALSE; |
| 746 | const bool success = PopBasic(DBUS_TYPE_BOOLEAN, &dbus_value); |
| 747 | *value = static_cast<bool>(dbus_value); |
| 748 | return success; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 749 | } |
| 750 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 751 | bool MessageReader::PopInt16(int16_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 752 | return PopBasic(DBUS_TYPE_INT16, value); |
| 753 | } |
| 754 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 755 | bool MessageReader::PopUint16(uint16_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 756 | return PopBasic(DBUS_TYPE_UINT16, value); |
| 757 | } |
| 758 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 759 | bool MessageReader::PopInt32(int32_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 760 | return PopBasic(DBUS_TYPE_INT32, value); |
| 761 | } |
| 762 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 763 | bool MessageReader::PopUint32(uint32_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 764 | return PopBasic(DBUS_TYPE_UINT32, value); |
| 765 | } |
| 766 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 767 | bool MessageReader::PopInt64(int64_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 768 | return PopBasic(DBUS_TYPE_INT64, value); |
| 769 | } |
| 770 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 771 | bool MessageReader::PopUint64(uint64_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 772 | return PopBasic(DBUS_TYPE_UINT64, value); |
| 773 | } |
| 774 | |
| 775 | bool MessageReader::PopDouble(double* value) { |
| 776 | return PopBasic(DBUS_TYPE_DOUBLE, value); |
| 777 | } |
| 778 | |
| 779 | bool MessageReader::PopString(std::string* value) { |
| 780 | char* tmp_value = NULL; |
| 781 | const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value); |
| 782 | if (success) |
| 783 | value->assign(tmp_value); |
| 784 | return success; |
| 785 | } |
| 786 | |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 787 | bool MessageReader::PopObjectPath(ObjectPath* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 788 | char* tmp_value = NULL; |
| 789 | const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); |
| 790 | if (success) |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 791 | *value = ObjectPath(tmp_value); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 792 | return success; |
| 793 | } |
| 794 | |
| 795 | bool MessageReader::PopArray(MessageReader* sub_reader) { |
| 796 | return PopContainer(DBUS_TYPE_ARRAY, sub_reader); |
| 797 | } |
| 798 | |
| 799 | bool MessageReader::PopStruct(MessageReader* sub_reader) { |
| 800 | return PopContainer(DBUS_TYPE_STRUCT, sub_reader); |
| 801 | } |
| 802 | |
| 803 | bool MessageReader::PopDictEntry(MessageReader* sub_reader) { |
| 804 | return PopContainer(DBUS_TYPE_DICT_ENTRY, sub_reader); |
| 805 | } |
| 806 | |
| 807 | bool MessageReader::PopVariant(MessageReader* sub_reader) { |
| 808 | return PopContainer(DBUS_TYPE_VARIANT, sub_reader); |
| 809 | } |
| 810 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 811 | bool MessageReader::PopArrayOfBytes(const uint8_t** bytes, size_t* length) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 812 | MessageReader array_reader(message_); |
| 813 | if (!PopArray(&array_reader)) |
| 814 | return false; |
satorux@chromium.org | 39a0d9b | 2011-11-11 11:13:56 +0900 | [diff] [blame] | 815 | // An empty array is allowed. |
| 816 | if (!array_reader.HasMoreData()) { |
| 817 | *length = 0; |
| 818 | *bytes = NULL; |
| 819 | return true; |
| 820 | } |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 821 | if (!array_reader.CheckDataType(DBUS_TYPE_BYTE)) |
| 822 | return false; |
| 823 | int int_length = 0; |
| 824 | dbus_message_iter_get_fixed_array(&array_reader.raw_message_iter_, |
| 825 | bytes, |
| 826 | &int_length); |
warx | ef0f837 | 2016-03-31 06:27:41 +0900 | [diff] [blame] | 827 | *length = static_cast<size_t>(int_length); |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | bool MessageReader::PopArrayOfDoubles(const double** doubles, size_t* length) { |
| 832 | MessageReader array_reader(message_); |
| 833 | if (!PopArray(&array_reader)) |
| 834 | return false; |
| 835 | if (!array_reader.HasMoreData()) { |
| 836 | *length = 0; |
| 837 | *doubles = nullptr; |
| 838 | return true; |
| 839 | } |
| 840 | if (!array_reader.CheckDataType(DBUS_TYPE_DOUBLE)) |
| 841 | return false; |
| 842 | int int_length = 0; |
| 843 | dbus_message_iter_get_fixed_array(&array_reader.raw_message_iter_, |
| 844 | doubles, |
| 845 | &int_length); |
| 846 | *length = static_cast<size_t>(int_length); |
satorux@chromium.org | 39a0d9b | 2011-11-11 11:13:56 +0900 | [diff] [blame] | 847 | return true; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 848 | } |
| 849 | |
mdm@chromium.org | 3d2baf0 | 2011-09-20 03:22:14 +0900 | [diff] [blame] | 850 | bool MessageReader::PopArrayOfStrings( |
| 851 | std::vector<std::string> *strings) { |
derat@chromium.org | 2bf9e5c | 2014-02-25 00:59:40 +0900 | [diff] [blame] | 852 | strings->clear(); |
mdm@chromium.org | 3d2baf0 | 2011-09-20 03:22:14 +0900 | [diff] [blame] | 853 | MessageReader array_reader(message_); |
| 854 | if (!PopArray(&array_reader)) |
derat@chromium.org | 2bf9e5c | 2014-02-25 00:59:40 +0900 | [diff] [blame] | 855 | return false; |
mdm@chromium.org | 3d2baf0 | 2011-09-20 03:22:14 +0900 | [diff] [blame] | 856 | while (array_reader.HasMoreData()) { |
| 857 | std::string string; |
| 858 | if (!array_reader.PopString(&string)) |
| 859 | return false; |
| 860 | strings->push_back(string); |
| 861 | } |
| 862 | return true; |
| 863 | } |
| 864 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 865 | bool MessageReader::PopArrayOfObjectPaths( |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 866 | std::vector<ObjectPath> *object_paths) { |
derat@chromium.org | 2bf9e5c | 2014-02-25 00:59:40 +0900 | [diff] [blame] | 867 | object_paths->clear(); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 868 | MessageReader array_reader(message_); |
| 869 | if (!PopArray(&array_reader)) |
derat@chromium.org | 2bf9e5c | 2014-02-25 00:59:40 +0900 | [diff] [blame] | 870 | return false; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 871 | while (array_reader.HasMoreData()) { |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 872 | ObjectPath object_path; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 873 | if (!array_reader.PopObjectPath(&object_path)) |
| 874 | return false; |
| 875 | object_paths->push_back(object_path); |
| 876 | } |
| 877 | return true; |
| 878 | } |
| 879 | |
rharrison@chromium.org | 3d53035 | 2012-02-10 03:14:08 +0900 | [diff] [blame] | 880 | bool MessageReader::PopArrayOfBytesAsProto( |
| 881 | google::protobuf::MessageLite* protobuf) { |
| 882 | DCHECK(protobuf != NULL); |
derat@chromium.org | 2bf9e5c | 2014-02-25 00:59:40 +0900 | [diff] [blame] | 883 | const char* serialized_buf = NULL; |
rharrison@chromium.org | 3d53035 | 2012-02-10 03:14:08 +0900 | [diff] [blame] | 884 | size_t buf_size = 0; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 885 | if (!PopArrayOfBytes(reinterpret_cast<const uint8_t**>(&serialized_buf), |
| 886 | &buf_size)) { |
rharrison@chromium.org | 3d53035 | 2012-02-10 03:14:08 +0900 | [diff] [blame] | 887 | LOG(ERROR) << "Error reading array of bytes"; |
| 888 | return false; |
| 889 | } |
| 890 | if (!protobuf->ParseFromArray(serialized_buf, buf_size)) { |
| 891 | LOG(ERROR) << "Failed to parse protocol buffer from array"; |
| 892 | return false; |
| 893 | } |
| 894 | return true; |
| 895 | } |
| 896 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 897 | bool MessageReader::PopVariantOfByte(uint8_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 898 | return PopVariantOfBasic(DBUS_TYPE_BYTE, value); |
| 899 | } |
| 900 | |
| 901 | bool MessageReader::PopVariantOfBool(bool* value) { |
satorux@chromium.org | a566a2c | 2011-08-05 06:57:21 +0900 | [diff] [blame] | 902 | // See the comment at MessageReader::PopBool(). |
| 903 | dbus_bool_t dbus_value = FALSE; |
| 904 | const bool success = PopVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value); |
| 905 | *value = static_cast<bool>(dbus_value); |
| 906 | return success; |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 907 | } |
| 908 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 909 | bool MessageReader::PopVariantOfInt16(int16_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 910 | return PopVariantOfBasic(DBUS_TYPE_INT16, value); |
| 911 | } |
| 912 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 913 | bool MessageReader::PopVariantOfUint16(uint16_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 914 | return PopVariantOfBasic(DBUS_TYPE_UINT16, value); |
| 915 | } |
| 916 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 917 | bool MessageReader::PopVariantOfInt32(int32_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 918 | return PopVariantOfBasic(DBUS_TYPE_INT32, value); |
| 919 | } |
| 920 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 921 | bool MessageReader::PopVariantOfUint32(uint32_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 922 | return PopVariantOfBasic(DBUS_TYPE_UINT32, value); |
| 923 | } |
| 924 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 925 | bool MessageReader::PopVariantOfInt64(int64_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 926 | return PopVariantOfBasic(DBUS_TYPE_INT64, value); |
| 927 | } |
| 928 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 929 | bool MessageReader::PopVariantOfUint64(uint64_t* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 930 | return PopVariantOfBasic(DBUS_TYPE_UINT64, value); |
| 931 | } |
| 932 | |
| 933 | bool MessageReader::PopVariantOfDouble(double* value) { |
| 934 | return PopVariantOfBasic(DBUS_TYPE_DOUBLE, value); |
| 935 | } |
| 936 | |
| 937 | bool MessageReader::PopVariantOfString(std::string* value) { |
| 938 | char* tmp_value = NULL; |
| 939 | const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value); |
| 940 | if (success) |
| 941 | value->assign(tmp_value); |
| 942 | return success; |
| 943 | } |
| 944 | |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 945 | bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) { |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 946 | char* tmp_value = NULL; |
| 947 | const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); |
| 948 | if (success) |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 949 | *value = ObjectPath(tmp_value); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 950 | return success; |
| 951 | } |
| 952 | |
| 953 | Message::DataType MessageReader::GetDataType() { |
| 954 | const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_); |
| 955 | return static_cast<Message::DataType>(dbus_type); |
| 956 | } |
| 957 | |
benchan | 464afff | 2014-08-25 15:50:29 +0900 | [diff] [blame] | 958 | std::string MessageReader::GetDataSignature() { |
| 959 | std::string signature; |
| 960 | char* raw_signature = dbus_message_iter_get_signature(&raw_message_iter_); |
| 961 | if (raw_signature) { |
| 962 | signature = raw_signature; |
| 963 | dbus_free(raw_signature); |
| 964 | } |
| 965 | return signature; |
| 966 | } |
| 967 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 968 | bool MessageReader::CheckDataType(int dbus_type) { |
| 969 | const int actual_type = dbus_message_iter_get_arg_type(&raw_message_iter_); |
| 970 | if (actual_type != dbus_type) { |
| 971 | VLOG(1) << "Type " << dbus_type << " is expected but got " |
| 972 | << actual_type; |
| 973 | return false; |
| 974 | } |
| 975 | return true; |
| 976 | } |
| 977 | |
| 978 | bool MessageReader::PopBasic(int dbus_type, void* value) { |
| 979 | if (!CheckDataType(dbus_type)) |
| 980 | return false; |
| 981 | // dbus_message_iter_get_basic() here should always work, as we have |
| 982 | // already checked the next item's data type in CheckDataType(). Note |
| 983 | // that dbus_message_iter_get_basic() is a void function. |
| 984 | dbus_message_iter_get_basic(&raw_message_iter_, value); |
| 985 | DCHECK(value); |
| 986 | dbus_message_iter_next(&raw_message_iter_); |
| 987 | return true; |
| 988 | } |
| 989 | |
| 990 | bool MessageReader::PopContainer(int dbus_type, MessageReader* sub_reader) { |
| 991 | DCHECK_NE(this, sub_reader); |
| 992 | |
| 993 | if (!CheckDataType(dbus_type)) |
| 994 | return false; |
| 995 | dbus_message_iter_recurse(&raw_message_iter_, |
| 996 | &sub_reader->raw_message_iter_); |
| 997 | dbus_message_iter_next(&raw_message_iter_); |
| 998 | return true; |
| 999 | } |
| 1000 | |
| 1001 | bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 1002 | MessageReader variant_reader(message_); |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 1003 | if (!PopVariant(&variant_reader)) |
| 1004 | return false; |
| 1005 | return variant_reader.PopBasic(dbus_type, value); |
| 1006 | } |
| 1007 | |
hashimoto | dac22fb | 2016-08-25 01:51:38 +0900 | [diff] [blame] | 1008 | bool MessageReader::PopFileDescriptor(base::ScopedFD* value) { |
| 1009 | CHECK(IsDBusTypeUnixFdSupported()); |
| 1010 | |
| 1011 | int fd = -1; |
| 1012 | const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); |
| 1013 | if (!success) |
| 1014 | return false; |
| 1015 | |
| 1016 | *value = base::ScopedFD(fd); |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
satorux@chromium.org | b684e27 | 2011-07-31 04:13:31 +0900 | [diff] [blame] | 1020 | } // namespace dbus |