Commit 1156b457 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Use more C++11 code in dbus/.

Use std::unique_ptr for MethodCall::FromRawMessage()
and Signal::FromRawMessage().
Use nullptr instead of NULL.
Use constexpr for constant in anonymous namespace in
ObjectProxy.

BUG=739622
TEST=Ran trybots.

Change-Id: I84fb7bc0318b09a77a44540575a97db6e5992db0
Reviewed-on: https://chromium-review.googlesource.com/658168Reviewed-by: default avatarRyo Hashimoto <hashimoto@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501171}
parent 1c19443a
...@@ -47,9 +47,7 @@ bool IsDBusTypeUnixFdSupported() { ...@@ -47,9 +47,7 @@ bool IsDBusTypeUnixFdSupported() {
return major >= 1 && minor >= 4; return major >= 1 && minor >= 4;
} }
Message::Message() Message::Message() : raw_message_(nullptr) {}
: raw_message_(NULL) {
}
Message::~Message() { Message::~Message() {
if (raw_message_) if (raw_message_)
...@@ -344,21 +342,20 @@ uint32_t Message::GetReplySerial() { ...@@ -344,21 +342,20 @@ uint32_t Message::GetReplySerial() {
// //
MethodCall::MethodCall(const std::string& interface_name, MethodCall::MethodCall(const std::string& interface_name,
const std::string& method_name) const std::string& method_name) {
: Message() {
Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
CHECK(SetInterface(interface_name)); CHECK(SetInterface(interface_name));
CHECK(SetMember(method_name)); CHECK(SetMember(method_name));
} }
MethodCall::MethodCall() : Message() { MethodCall::MethodCall() = default;
}
MethodCall* MethodCall::FromRawMessage(DBusMessage* raw_message) { std::unique_ptr<MethodCall> MethodCall::FromRawMessage(
DBusMessage* raw_message) {
DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message)); DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_get_type(raw_message));
MethodCall* method_call = new MethodCall; std::unique_ptr<MethodCall> method_call(new MethodCall());
method_call->Init(raw_message); method_call->Init(raw_message);
return method_call; return method_call;
} }
...@@ -367,21 +364,19 @@ MethodCall* MethodCall::FromRawMessage(DBusMessage* raw_message) { ...@@ -367,21 +364,19 @@ MethodCall* MethodCall::FromRawMessage(DBusMessage* raw_message) {
// Signal implementation. // Signal implementation.
// //
Signal::Signal(const std::string& interface_name, Signal::Signal(const std::string& interface_name,
const std::string& method_name) const std::string& method_name) {
: Message() {
Init(dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL)); Init(dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL));
CHECK(SetInterface(interface_name)); CHECK(SetInterface(interface_name));
CHECK(SetMember(method_name)); CHECK(SetMember(method_name));
} }
Signal::Signal() : Message() { Signal::Signal() = default;
}
Signal* Signal::FromRawMessage(DBusMessage* raw_message) { std::unique_ptr<Signal> Signal::FromRawMessage(DBusMessage* raw_message) {
DCHECK_EQ(DBUS_MESSAGE_TYPE_SIGNAL, dbus_message_get_type(raw_message)); DCHECK_EQ(DBUS_MESSAGE_TYPE_SIGNAL, dbus_message_get_type(raw_message));
Signal* signal = new Signal; std::unique_ptr<Signal> signal(new Signal());
signal->Init(raw_message); signal->Init(raw_message);
return signal; return signal;
} }
...@@ -390,26 +385,25 @@ Signal* Signal::FromRawMessage(DBusMessage* raw_message) { ...@@ -390,26 +385,25 @@ Signal* Signal::FromRawMessage(DBusMessage* raw_message) {
// Response implementation. // Response implementation.
// //
Response::Response() : Message() { Response::Response() = default;
}
std::unique_ptr<Response> Response::FromRawMessage(DBusMessage* raw_message) { std::unique_ptr<Response> Response::FromRawMessage(DBusMessage* raw_message) {
DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_RETURN, DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_RETURN,
dbus_message_get_type(raw_message)); dbus_message_get_type(raw_message));
std::unique_ptr<Response> response(new Response); std::unique_ptr<Response> response(new Response());
response->Init(raw_message); response->Init(raw_message);
return response; return response;
} }
std::unique_ptr<Response> Response::FromMethodCall(MethodCall* method_call) { std::unique_ptr<Response> Response::FromMethodCall(MethodCall* method_call) {
std::unique_ptr<Response> response(new Response); std::unique_ptr<Response> response(new Response());
response->Init(dbus_message_new_method_return(method_call->raw_message())); response->Init(dbus_message_new_method_return(method_call->raw_message()));
return response; return response;
} }
std::unique_ptr<Response> Response::CreateEmpty() { std::unique_ptr<Response> Response::CreateEmpty() {
std::unique_ptr<Response> response(new Response); std::unique_ptr<Response> response(new Response());
response->Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN)); response->Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN));
return response; return response;
} }
...@@ -418,14 +412,13 @@ std::unique_ptr<Response> Response::CreateEmpty() { ...@@ -418,14 +412,13 @@ std::unique_ptr<Response> Response::CreateEmpty() {
// ErrorResponse implementation. // ErrorResponse implementation.
// //
ErrorResponse::ErrorResponse() : Response() { ErrorResponse::ErrorResponse() = default;
}
std::unique_ptr<ErrorResponse> ErrorResponse::FromRawMessage( std::unique_ptr<ErrorResponse> ErrorResponse::FromRawMessage(
DBusMessage* raw_message) { DBusMessage* raw_message) {
DCHECK_EQ(DBUS_MESSAGE_TYPE_ERROR, dbus_message_get_type(raw_message)); DCHECK_EQ(DBUS_MESSAGE_TYPE_ERROR, dbus_message_get_type(raw_message));
std::unique_ptr<ErrorResponse> response(new ErrorResponse); std::unique_ptr<ErrorResponse> response(new ErrorResponse());
response->Init(raw_message); response->Init(raw_message);
return response; return response;
} }
...@@ -434,7 +427,7 @@ std::unique_ptr<ErrorResponse> ErrorResponse::FromMethodCall( ...@@ -434,7 +427,7 @@ std::unique_ptr<ErrorResponse> ErrorResponse::FromMethodCall(
MethodCall* method_call, MethodCall* method_call,
const std::string& error_name, const std::string& error_name,
const std::string& error_message) { const std::string& error_message) {
std::unique_ptr<ErrorResponse> response(new ErrorResponse); std::unique_ptr<ErrorResponse> response(new ErrorResponse());
response->Init(dbus_message_new_error(method_call->raw_message(), response->Init(dbus_message_new_error(method_call->raw_message(),
error_name.c_str(), error_name.c_str(),
error_message.c_str())); error_message.c_str()));
...@@ -551,11 +544,10 @@ void MessageWriter::OpenVariant(const std::string& signature, ...@@ -551,11 +544,10 @@ void MessageWriter::OpenVariant(const std::string& signature,
void MessageWriter::OpenStruct(MessageWriter* writer) { void MessageWriter::OpenStruct(MessageWriter* writer) {
DCHECK(!container_is_open_); DCHECK(!container_is_open_);
const bool success = dbus_message_iter_open_container( const bool success =
&raw_message_iter_, dbus_message_iter_open_container(&raw_message_iter_, DBUS_TYPE_STRUCT,
DBUS_TYPE_STRUCT, nullptr, // Signature should be nullptr.
NULL, // Signature should be NULL. &writer->raw_message_iter_);
&writer->raw_message_iter_);
CHECK(success) << "Unable to allocate memory"; CHECK(success) << "Unable to allocate memory";
container_is_open_ = true; container_is_open_ = true;
} }
...@@ -563,11 +555,10 @@ void MessageWriter::OpenStruct(MessageWriter* writer) { ...@@ -563,11 +555,10 @@ void MessageWriter::OpenStruct(MessageWriter* writer) {
void MessageWriter::OpenDictEntry(MessageWriter* writer) { void MessageWriter::OpenDictEntry(MessageWriter* writer) {
DCHECK(!container_is_open_); DCHECK(!container_is_open_);
const bool success = dbus_message_iter_open_container( const bool success =
&raw_message_iter_, dbus_message_iter_open_container(&raw_message_iter_, DBUS_TYPE_DICT_ENTRY,
DBUS_TYPE_DICT_ENTRY, nullptr, // Signature should be nullptr.
NULL, // Signature should be NULL. &writer->raw_message_iter_);
&writer->raw_message_iter_);
CHECK(success) << "Unable to allocate memory"; CHECK(success) << "Unable to allocate memory";
container_is_open_ = true; container_is_open_ = true;
} }
...@@ -777,7 +768,7 @@ bool MessageReader::PopDouble(double* value) { ...@@ -777,7 +768,7 @@ bool MessageReader::PopDouble(double* value) {
} }
bool MessageReader::PopString(std::string* value) { bool MessageReader::PopString(std::string* value) {
char* tmp_value = NULL; char* tmp_value = nullptr;
const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value); const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value);
if (success) if (success)
value->assign(tmp_value); value->assign(tmp_value);
...@@ -785,7 +776,7 @@ bool MessageReader::PopString(std::string* value) { ...@@ -785,7 +776,7 @@ bool MessageReader::PopString(std::string* value) {
} }
bool MessageReader::PopObjectPath(ObjectPath* value) { bool MessageReader::PopObjectPath(ObjectPath* value) {
char* tmp_value = NULL; char* tmp_value = nullptr;
const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
if (success) if (success)
*value = ObjectPath(tmp_value); *value = ObjectPath(tmp_value);
...@@ -815,7 +806,7 @@ bool MessageReader::PopArrayOfBytes(const uint8_t** bytes, size_t* length) { ...@@ -815,7 +806,7 @@ bool MessageReader::PopArrayOfBytes(const uint8_t** bytes, size_t* length) {
// An empty array is allowed. // An empty array is allowed.
if (!array_reader.HasMoreData()) { if (!array_reader.HasMoreData()) {
*length = 0; *length = 0;
*bytes = NULL; *bytes = nullptr;
return true; return true;
} }
if (!array_reader.CheckDataType(DBUS_TYPE_BYTE)) if (!array_reader.CheckDataType(DBUS_TYPE_BYTE))
...@@ -879,8 +870,8 @@ bool MessageReader::PopArrayOfObjectPaths( ...@@ -879,8 +870,8 @@ bool MessageReader::PopArrayOfObjectPaths(
bool MessageReader::PopArrayOfBytesAsProto( bool MessageReader::PopArrayOfBytesAsProto(
google::protobuf::MessageLite* protobuf) { google::protobuf::MessageLite* protobuf) {
DCHECK(protobuf != NULL); DCHECK(protobuf);
const char* serialized_buf = NULL; const char* serialized_buf = nullptr;
size_t buf_size = 0; size_t buf_size = 0;
if (!PopArrayOfBytes(reinterpret_cast<const uint8_t**>(&serialized_buf), if (!PopArrayOfBytes(reinterpret_cast<const uint8_t**>(&serialized_buf),
&buf_size)) { &buf_size)) {
...@@ -935,7 +926,7 @@ bool MessageReader::PopVariantOfDouble(double* value) { ...@@ -935,7 +926,7 @@ bool MessageReader::PopVariantOfDouble(double* value) {
} }
bool MessageReader::PopVariantOfString(std::string* value) { bool MessageReader::PopVariantOfString(std::string* value) {
char* tmp_value = NULL; char* tmp_value = nullptr;
const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value); const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value);
if (success) if (success)
value->assign(tmp_value); value->assign(tmp_value);
...@@ -943,7 +934,7 @@ bool MessageReader::PopVariantOfString(std::string* value) { ...@@ -943,7 +934,7 @@ bool MessageReader::PopVariantOfString(std::string* value) {
} }
bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) { bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) {
char* tmp_value = NULL; char* tmp_value = nullptr;
const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
if (success) if (success)
*value = ObjectPath(tmp_value); *value = ObjectPath(tmp_value);
......
...@@ -158,9 +158,8 @@ class CHROME_DBUS_EXPORT MethodCall : public Message { ...@@ -158,9 +158,8 @@ class CHROME_DBUS_EXPORT MethodCall : public Message {
const std::string& method_name); const std::string& method_name);
// Returns a newly created MethodCall from the given raw message of the // Returns a newly created MethodCall from the given raw message of the
// type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the // type DBUS_MESSAGE_TYPE_METHOD_CALL. Takes the ownership of |raw_message|.
// returned object. Takes the ownership of |raw_message|. static std::unique_ptr<MethodCall> FromRawMessage(DBusMessage* raw_message);
static MethodCall* FromRawMessage(DBusMessage* raw_message);
private: private:
// Creates a method call message. The internal raw message is NULL. // Creates a method call message. The internal raw message is NULL.
...@@ -187,9 +186,8 @@ class CHROME_DBUS_EXPORT Signal : public Message { ...@@ -187,9 +186,8 @@ class CHROME_DBUS_EXPORT Signal : public Message {
const std::string& method_name); const std::string& method_name);
// Returns a newly created SIGNAL from the given raw message of the type // Returns a newly created SIGNAL from the given raw message of the type
// DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned // DBUS_MESSAGE_TYPE_SIGNAL. Takes the ownership of |raw_message|.
// object. Takes the ownership of |raw_message|. static std::unique_ptr<Signal> FromRawMessage(DBusMessage* raw_message);
static Signal* FromRawMessage(DBusMessage* raw_message);
private: private:
// Creates a signal message. The internal raw message is NULL. // Creates a signal message. The internal raw message is NULL.
......
...@@ -27,23 +27,25 @@ namespace dbus { ...@@ -27,23 +27,25 @@ namespace dbus {
namespace { namespace {
const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown"; constexpr char kErrorServiceUnknown[] =
const char kErrorObjectUnknown[] = "org.freedesktop.DBus.Error.UnknownObject"; "org.freedesktop.DBus.Error.ServiceUnknown";
constexpr char kErrorObjectUnknown[] =
"org.freedesktop.DBus.Error.UnknownObject";
// Used for success ratio histograms. 1 for success, 0 for failure. // Used for success ratio histograms. 1 for success, 0 for failure.
const int kSuccessRatioHistogramMaxValue = 2; constexpr int kSuccessRatioHistogramMaxValue = 2;
// The path of D-Bus Object sending NameOwnerChanged signal. // The path of D-Bus Object sending NameOwnerChanged signal.
const char kDBusSystemObjectPath[] = "/org/freedesktop/DBus"; constexpr char kDBusSystemObjectPath[] = "/org/freedesktop/DBus";
// The D-Bus Object interface. // The D-Bus Object interface.
const char kDBusSystemObjectInterface[] = "org.freedesktop.DBus"; constexpr char kDBusSystemObjectInterface[] = "org.freedesktop.DBus";
// The D-Bus Object address. // The D-Bus Object address.
const char kDBusSystemObjectAddress[] = "org.freedesktop.DBus"; constexpr char kDBusSystemObjectAddress[] = "org.freedesktop.DBus";
// The NameOwnerChanged member in |kDBusSystemObjectInterface|. // The NameOwnerChanged member in |kDBusSystemObjectInterface|.
const char kNameOwnerChangedMember[] = "NameOwnerChanged"; constexpr char kNameOwnerChangedMember[] = "NameOwnerChanged";
// An empty function used for ObjectProxy::EmptyResponseCallback(). // An empty function used for ObjectProxy::EmptyResponseCallback().
void EmptyResponseCallbackBody(Response* /*response*/) { void EmptyResponseCallbackBody(Response* /*response*/) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment