Chromium Code Reviews| Index: dbus/message.cc |
| diff --git a/dbus/message.cc b/dbus/message.cc |
| index 81182028120d466c61ab676ac6f93202078292dd..ba00a7d5d7e8c8cc05815ebd20b0b399b2cdf275 100644 |
| --- a/dbus/message.cc |
| +++ b/dbus/message.cc |
| @@ -178,31 +178,124 @@ std::string Message::ToString() { |
| // Generate headers first. |
| std::string headers; |
| + const std::string destination = GetDestination(); |
| + if (!destination.empty()) |
| + 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.
|
| + const std::string path = GetPath(); |
| + if (!path.empty()) |
| + headers += "path: " + path + "\n"; |
| + const std::string interface = GetInterface(); |
| + if (!interface.empty()) |
| + headers += "interface: " + interface + "\n"; |
| + const std::string member = GetMember(); |
| + if (!member.empty()) |
| + headers += "member: " + member + "\n"; |
| + const std::string error_name = GetErrorName(); |
| + if (!error_name.empty()) |
| + headers += "error_name: " + error_name + "\n"; |
| + const std::string sender = GetSender(); |
| + if (!sender.empty()) |
| + headers += "sender: " + sender + "\n"; |
| + const std::string signature = GetSignature(); |
| + if (!signature.empty()) |
| + headers += "signature: " + signature + "\n"; |
| + const uint32 serial = GetSerial(); |
| + if (serial != 0) |
| + headers += "serial: " + base::StringPrintf("%u", serial) + "\n"; |
| + const uint32 reply_serial = GetReplySerial(); |
| + if (reply_serial != 0) |
| + headers += "reply_serial: " + base::StringPrintf("%u", reply_serial) + "\n"; |
| + |
| + // Generate the payload. |
| + MessageReader reader(this); |
| + return headers + "\n" + ToStringInternal("", &reader); |
| +} |
| + |
| +void Message::SetDestination(const std::string& destination) { |
| + const bool success = dbus_message_set_destination(raw_message_, |
| + destination.c_str()); |
| + CHECK(success) << "Unable to allocate memory"; |
| +} |
| + |
| +void Message::SetPath(const std::string& path) { |
| + const bool success = dbus_message_set_path(raw_message_, |
| + path.c_str()); |
| + CHECK(success) << "Unable to allocate memory"; |
| +} |
| + |
| +void Message::SetInterface(const std::string& interface) { |
| + const bool success = dbus_message_set_interface(raw_message_, |
| + interface.c_str()); |
| + CHECK(success) << "Unable to allocate memory"; |
| +} |
| + |
| +void Message::SetMember(const std::string& member) { |
| + const bool success = dbus_message_set_member(raw_message_, |
| + member.c_str()); |
| + CHECK(success) << "Unable to allocate memory"; |
| +} |
| + |
| +void Message::SetErrorName(const std::string& error_name) { |
| + const bool success = dbus_message_set_error_name(raw_message_, |
| + error_name.c_str()); |
| + CHECK(success) << "Unable to allocate memory"; |
| +} |
| + |
| +void Message::SetSender(const std::string& sender) { |
| + const bool success = dbus_message_set_sender(raw_message_, |
| + sender.c_str()); |
| + CHECK(success) << "Unable to allocate memory"; |
| +} |
| + |
| +void Message::SetSerial(uint32 serial) { |
| + dbus_message_set_serial(raw_message_, serial); |
| +} |
| + |
| +void Message::SetReplySerial(uint32 reply_serial) { |
| + dbus_message_set_reply_serial(raw_message_, reply_serial); |
| +} |
| + |
| +std::string Message::GetDestination() { |
| const char* destination = dbus_message_get_destination(raw_message_); |
| - if (destination) |
| - headers += base::StringPrintf("destination: %s\n", destination); |
| + return destination ? destination : ""; |
| +} |
| + |
| +std::string Message::GetPath() { |
| const char* path = dbus_message_get_path(raw_message_); |
| - if (path) |
| - headers += base::StringPrintf("path: %s\n", path); |
| + return path ? path : ""; |
| +} |
| + |
| +std::string Message::GetInterface() { |
| const char* interface = dbus_message_get_interface(raw_message_); |
| - if (interface) |
| - headers += base::StringPrintf("interface: %s\n", interface); |
| + return interface ? interface : ""; |
| +} |
| + |
| +std::string Message::GetMember() { |
| const char* member = dbus_message_get_member(raw_message_); |
| - if (member) |
| - headers += base::StringPrintf("member: %s\n", member); |
| + return member ? member : ""; |
| +} |
| + |
| +std::string Message::GetErrorName() { |
| const char* error_name = dbus_message_get_error_name(raw_message_); |
| - if (error_name) |
| - headers += base::StringPrintf("error_name: %s\n", error_name); |
| + return error_name ? error_name : ""; |
| +} |
| + |
| +std::string Message::GetSender() { |
| const char* sender = dbus_message_get_sender(raw_message_); |
| - if (sender) |
| - headers += base::StringPrintf("sender: %s\n", sender); |
| + return sender ? sender : ""; |
| +} |
| + |
| +std::string Message::GetSignature() { |
| const char* signature = dbus_message_get_signature(raw_message_); |
| - if (signature) |
| - headers += base::StringPrintf("signature: %s\n", signature); |
| + return signature ? signature : ""; |
| +} |
| - // Generate the payload. |
| - MessageReader reader(this); |
| - return headers + "\n" + ToStringInternal("", &reader); |
| +uint32 Message::GetSerial() { |
| + return dbus_message_get_serial(raw_message_); |
| +} |
| + |
| +uint32 Message::GetReplySerial() { |
| + return dbus_message_get_reply_serial(raw_message_); |
| } |
| // |
| @@ -211,29 +304,26 @@ std::string Message::ToString() { |
| MethodCall::MethodCall(const std::string& interface_name, |
| const std::string& method_name) |
| - : Message(), |
| - interface_name_(interface_name), |
| - method_name_(method_name) { |
| + : Message() { |
| reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); |
| - bool success = dbus_message_set_interface(raw_message(), |
| - interface_name.c_str()); |
| - CHECK(success) << "Unable to allocate memory"; |
| - |
| - success = dbus_message_set_member(raw_message(), method_name.c_str()); |
| - CHECK(success) << "Unable to allocate memory"; |
| + SetInterface(interface_name); |
| + SetMember(method_name); |
| } |
| -void MethodCall::SetServiceName(const std::string& service_name) { |
| - const bool success = dbus_message_set_destination(raw_message(), |
| - service_name.c_str()); |
| - CHECK(success) << "Unable to allocate memory"; |
| -} |
| +MethodCall* MethodCall::FromRawMessage(DBusMessage* raw_message) { |
| + DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message)); |
| -void MethodCall::SetObjectPath(const std::string& object_path) { |
| - const bool success = dbus_message_set_path(raw_message(), |
| - object_path.c_str()); |
| - CHECK(success) << "Unable to allocate memory"; |
| + const char* interface = dbus_message_get_interface(raw_message); |
| + const char* member = dbus_message_get_member(raw_message); |
| + if (!interface) |
| + 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.
|
| + if (!member) |
| + member = ""; |
| + |
| + MethodCall* method_call = new MethodCall(interface, member); |
| + method_call->reset_raw_message(raw_message); |
| + return method_call; |
| } |
| // |
| @@ -243,6 +333,32 @@ void MethodCall::SetObjectPath(const std::string& object_path) { |
| Response::Response() : Message() { |
| } |
| +Response* Response::FromMethodCall(MethodCall* method_call) { |
| + Response* response = new Response; |
| + response->reset_raw_message( |
| + dbus_message_new_method_return(method_call->raw_message())); |
| + return response; |
| +} |
| + |
| +// |
| +// ErrorResponse implementation. |
| +// |
| + |
| +ErrorResponse::ErrorResponse() : Message() { |
| +} |
| + |
| +ErrorResponse* ErrorResponse::FromMethodCall( |
| + MethodCall* method_call, |
| + const std::string& error_name, |
| + const std::string& error_message) { |
| + ErrorResponse* response = new ErrorResponse; |
| + response->reset_raw_message( |
| + dbus_message_new_error(method_call->raw_message(), |
| + error_name.c_str(), |
| + error_message.c_str())); |
| + return response; |
| +} |
| + |
| // |
| // MessageWriter implementation. |
| // |