Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "dbus/message.h" | 5 #include "dbus/message.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 // member: SomeMethod | 171 // member: SomeMethod |
| 172 // | 172 // |
| 173 // string \"payload\" | 173 // string \"payload\" |
| 174 // ... | 174 // ... |
| 175 std::string Message::ToString() { | 175 std::string Message::ToString() { |
| 176 if (!raw_message_) | 176 if (!raw_message_) |
| 177 return ""; | 177 return ""; |
| 178 | 178 |
| 179 // Generate headers first. | 179 // Generate headers first. |
| 180 std::string headers; | 180 std::string headers; |
| 181 const char* destination = dbus_message_get_destination(raw_message_); | 181 const std::string destination = GetDestination(); |
| 182 if (destination) | 182 if (!destination.empty()) |
| 183 headers += base::StringPrintf("destination: %s\n", destination); | 183 headers += "destination: " + destination + "\n"; |
|
stevenjb
2011/08/09 18:30:41
nit: We could simplify this code by creating a loc
satorux1
2011/08/09 20:39:08
Good idea. Done.
| |
| 184 const char* path = dbus_message_get_path(raw_message_); | 184 const std::string path = GetPath(); |
| 185 if (path) | 185 if (!path.empty()) |
| 186 headers += base::StringPrintf("path: %s\n", path); | 186 headers += "path: " + path + "\n"; |
| 187 const char* interface = dbus_message_get_interface(raw_message_); | 187 const std::string interface = GetInterface(); |
| 188 if (interface) | 188 if (!interface.empty()) |
| 189 headers += base::StringPrintf("interface: %s\n", interface); | 189 headers += "interface: " + interface + "\n"; |
| 190 const char* member = dbus_message_get_member(raw_message_); | 190 const std::string member = GetMember(); |
| 191 if (member) | 191 if (!member.empty()) |
| 192 headers += base::StringPrintf("member: %s\n", member); | 192 headers += "member: " + member + "\n"; |
| 193 const char* error_name = dbus_message_get_error_name(raw_message_); | 193 const std::string error_name = GetErrorName(); |
| 194 if (error_name) | 194 if (!error_name.empty()) |
| 195 headers += base::StringPrintf("error_name: %s\n", error_name); | 195 headers += "error_name: " + error_name + "\n"; |
| 196 const char* sender = dbus_message_get_sender(raw_message_); | 196 const std::string sender = GetSender(); |
| 197 if (sender) | 197 if (!sender.empty()) |
| 198 headers += base::StringPrintf("sender: %s\n", sender); | 198 headers += "sender: " + sender + "\n"; |
| 199 const char* signature = dbus_message_get_signature(raw_message_); | 199 const std::string signature = GetSignature(); |
| 200 if (signature) | 200 if (!signature.empty()) |
| 201 headers += base::StringPrintf("signature: %s\n", signature); | 201 headers += "signature: " + signature + "\n"; |
| 202 const uint32 serial = GetSerial(); | |
| 203 if (serial != 0) | |
| 204 headers += "serial: " + base::StringPrintf("%u", serial) + "\n"; | |
| 205 const uint32 reply_serial = GetReplySerial(); | |
| 206 if (reply_serial != 0) | |
| 207 headers += "reply_serial: " + base::StringPrintf("%u", reply_serial) + "\n"; | |
| 202 | 208 |
| 203 // Generate the payload. | 209 // Generate the payload. |
| 204 MessageReader reader(this); | 210 MessageReader reader(this); |
| 205 return headers + "\n" + ToStringInternal("", &reader); | 211 return headers + "\n" + ToStringInternal("", &reader); |
| 206 } | 212 } |
| 207 | 213 |
| 214 void Message::SetDestination(const std::string& destination) { | |
| 215 const bool success = dbus_message_set_destination(raw_message_, | |
| 216 destination.c_str()); | |
| 217 CHECK(success) << "Unable to allocate memory"; | |
| 218 } | |
| 219 | |
| 220 void Message::SetPath(const std::string& path) { | |
| 221 const bool success = dbus_message_set_path(raw_message_, | |
| 222 path.c_str()); | |
| 223 CHECK(success) << "Unable to allocate memory"; | |
| 224 } | |
| 225 | |
| 226 void Message::SetInterface(const std::string& interface) { | |
| 227 const bool success = dbus_message_set_interface(raw_message_, | |
| 228 interface.c_str()); | |
| 229 CHECK(success) << "Unable to allocate memory"; | |
| 230 } | |
| 231 | |
| 232 void Message::SetMember(const std::string& member) { | |
| 233 const bool success = dbus_message_set_member(raw_message_, | |
| 234 member.c_str()); | |
| 235 CHECK(success) << "Unable to allocate memory"; | |
| 236 } | |
| 237 | |
| 238 void Message::SetErrorName(const std::string& error_name) { | |
| 239 const bool success = dbus_message_set_error_name(raw_message_, | |
| 240 error_name.c_str()); | |
| 241 CHECK(success) << "Unable to allocate memory"; | |
| 242 } | |
| 243 | |
| 244 void Message::SetSender(const std::string& sender) { | |
| 245 const bool success = dbus_message_set_sender(raw_message_, | |
| 246 sender.c_str()); | |
| 247 CHECK(success) << "Unable to allocate memory"; | |
| 248 } | |
| 249 | |
| 250 void Message::SetSerial(uint32 serial) { | |
| 251 dbus_message_set_serial(raw_message_, serial); | |
| 252 } | |
| 253 | |
| 254 void Message::SetReplySerial(uint32 reply_serial) { | |
| 255 dbus_message_set_reply_serial(raw_message_, reply_serial); | |
| 256 } | |
| 257 | |
| 258 std::string Message::GetDestination() { | |
| 259 const char* destination = dbus_message_get_destination(raw_message_); | |
| 260 return destination ? destination : ""; | |
| 261 } | |
| 262 | |
| 263 std::string Message::GetPath() { | |
| 264 const char* path = dbus_message_get_path(raw_message_); | |
| 265 return path ? path : ""; | |
| 266 } | |
| 267 | |
| 268 std::string Message::GetInterface() { | |
| 269 const char* interface = dbus_message_get_interface(raw_message_); | |
| 270 return interface ? interface : ""; | |
| 271 } | |
| 272 | |
| 273 std::string Message::GetMember() { | |
| 274 const char* member = dbus_message_get_member(raw_message_); | |
| 275 return member ? member : ""; | |
| 276 } | |
| 277 | |
| 278 std::string Message::GetErrorName() { | |
| 279 const char* error_name = dbus_message_get_error_name(raw_message_); | |
| 280 return error_name ? error_name : ""; | |
| 281 } | |
| 282 | |
| 283 std::string Message::GetSender() { | |
| 284 const char* sender = dbus_message_get_sender(raw_message_); | |
| 285 return sender ? sender : ""; | |
| 286 } | |
| 287 | |
| 288 std::string Message::GetSignature() { | |
| 289 const char* signature = dbus_message_get_signature(raw_message_); | |
| 290 return signature ? signature : ""; | |
| 291 } | |
| 292 | |
| 293 uint32 Message::GetSerial() { | |
| 294 return dbus_message_get_serial(raw_message_); | |
| 295 } | |
| 296 | |
| 297 uint32 Message::GetReplySerial() { | |
| 298 return dbus_message_get_reply_serial(raw_message_); | |
| 299 } | |
| 300 | |
| 208 // | 301 // |
| 209 // MethodCall implementation. | 302 // MethodCall implementation. |
| 210 // | 303 // |
| 211 | 304 |
| 212 MethodCall::MethodCall(const std::string& interface_name, | 305 MethodCall::MethodCall(const std::string& interface_name, |
| 213 const std::string& method_name) | 306 const std::string& method_name) |
| 214 : Message(), | 307 : Message() { |
| 215 interface_name_(interface_name), | |
| 216 method_name_(method_name) { | |
| 217 reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); | 308 reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); |
| 218 | 309 |
| 219 bool success = dbus_message_set_interface(raw_message(), | 310 SetInterface(interface_name); |
| 220 interface_name.c_str()); | 311 SetMember(method_name); |
| 221 CHECK(success) << "Unable to allocate memory"; | |
| 222 | |
| 223 success = dbus_message_set_member(raw_message(), method_name.c_str()); | |
| 224 CHECK(success) << "Unable to allocate memory"; | |
| 225 } | 312 } |
| 226 | 313 |
| 227 void MethodCall::SetServiceName(const std::string& service_name) { | 314 MethodCall* MethodCall::FromRawMessage(DBusMessage* raw_message) { |
| 228 const bool success = dbus_message_set_destination(raw_message(), | 315 DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message)); |
| 229 service_name.c_str()); | |
| 230 CHECK(success) << "Unable to allocate memory"; | |
| 231 } | |
| 232 | 316 |
| 233 void MethodCall::SetObjectPath(const std::string& object_path) { | 317 const char* interface = dbus_message_get_interface(raw_message); |
| 234 const bool success = dbus_message_set_path(raw_message(), | 318 const char* member = dbus_message_get_member(raw_message); |
| 235 object_path.c_str()); | 319 if (!interface) |
| 236 CHECK(success) << "Unable to allocate memory"; | 320 interface = ""; |
|
stevenjb
2011/08/09 18:30:41
nit: might be slightly more readable as:
std::stri
satorux1
2011/08/09 20:39:08
Good idea. Done.
| |
| 321 if (!member) | |
| 322 member = ""; | |
| 323 | |
| 324 MethodCall* method_call = new MethodCall(interface, member); | |
| 325 method_call->reset_raw_message(raw_message); | |
| 326 return method_call; | |
| 237 } | 327 } |
| 238 | 328 |
| 239 // | 329 // |
| 240 // Response implementation. | 330 // Response implementation. |
| 241 // | 331 // |
| 242 | 332 |
| 243 Response::Response() : Message() { | 333 Response::Response() : Message() { |
| 244 } | 334 } |
| 245 | 335 |
| 336 Response* Response::FromMethodCall(MethodCall* method_call) { | |
| 337 Response* response = new Response; | |
| 338 response->reset_raw_message( | |
| 339 dbus_message_new_method_return(method_call->raw_message())); | |
| 340 return response; | |
| 341 } | |
| 342 | |
| 343 // | |
| 344 // ErrorResponse implementation. | |
| 345 // | |
| 346 | |
| 347 ErrorResponse::ErrorResponse() : Message() { | |
| 348 } | |
| 349 | |
| 350 ErrorResponse* ErrorResponse::FromMethodCall( | |
| 351 MethodCall* method_call, | |
| 352 const std::string& error_name, | |
| 353 const std::string& error_message) { | |
| 354 ErrorResponse* response = new ErrorResponse; | |
| 355 response->reset_raw_message( | |
| 356 dbus_message_new_error(method_call->raw_message(), | |
| 357 error_name.c_str(), | |
| 358 error_message.c_str())); | |
| 359 return response; | |
| 360 } | |
| 361 | |
| 246 // | 362 // |
| 247 // MessageWriter implementation. | 363 // MessageWriter implementation. |
| 248 // | 364 // |
| 249 | 365 |
| 250 MessageWriter::MessageWriter(Message* message) : | 366 MessageWriter::MessageWriter(Message* message) : |
| 251 message_(message), | 367 message_(message), |
| 252 container_is_open_(false) { | 368 container_is_open_(false) { |
| 253 dbus_message_iter_init_append(message_->raw_message(), &raw_message_iter_); | 369 dbus_message_iter_init_append(message_->raw_message(), &raw_message_iter_); |
| 254 } | 370 } |
| 255 | 371 |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 673 } | 789 } |
| 674 | 790 |
| 675 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { | 791 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { |
| 676 dbus::MessageReader variant_reader(message_); | 792 dbus::MessageReader variant_reader(message_); |
| 677 if (!PopVariant(&variant_reader)) | 793 if (!PopVariant(&variant_reader)) |
| 678 return false; | 794 return false; |
| 679 return variant_reader.PopBasic(dbus_type, value); | 795 return variant_reader.PopBasic(dbus_type, value); |
| 680 } | 796 } |
| 681 | 797 |
| 682 } // namespace dbus | 798 } // namespace dbus |
| OLD | NEW |