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