Skip to content

Commit

Permalink
Use more C++11 code in dbus/.
Browse files Browse the repository at this point in the history
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/658168
Reviewed-by: Ryo Hashimoto <hashimoto@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501171}
  • Loading branch information
Hidehiko Abe authored and Commit Bot committed Sep 12, 2017
1 parent 1c19443 commit 1156b45
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 54 deletions.
73 changes: 32 additions & 41 deletions dbus/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ bool IsDBusTypeUnixFdSupported() {
return major >= 1 && minor >= 4;
}

Message::Message()
: raw_message_(NULL) {
}
Message::Message() : raw_message_(nullptr) {}

Message::~Message() {
if (raw_message_)
Expand Down Expand Up @@ -344,21 +342,20 @@ uint32_t Message::GetReplySerial() {
//

MethodCall::MethodCall(const std::string& interface_name,
const std::string& method_name)
: Message() {
const std::string& method_name) {
Init(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));

CHECK(SetInterface(interface_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));

MethodCall* method_call = new MethodCall;
std::unique_ptr<MethodCall> method_call(new MethodCall());
method_call->Init(raw_message);
return method_call;
}
Expand All @@ -367,21 +364,19 @@ MethodCall* MethodCall::FromRawMessage(DBusMessage* raw_message) {
// Signal implementation.
//
Signal::Signal(const std::string& interface_name,
const std::string& method_name)
: Message() {
const std::string& method_name) {
Init(dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL));

CHECK(SetInterface(interface_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));

Signal* signal = new Signal;
std::unique_ptr<Signal> signal(new Signal());
signal->Init(raw_message);
return signal;
}
Expand All @@ -390,26 +385,25 @@ Signal* Signal::FromRawMessage(DBusMessage* raw_message) {
// Response implementation.
//

Response::Response() : Message() {
}
Response::Response() = default;

std::unique_ptr<Response> Response::FromRawMessage(DBusMessage* raw_message) {
DCHECK_EQ(DBUS_MESSAGE_TYPE_METHOD_RETURN,
dbus_message_get_type(raw_message));

std::unique_ptr<Response> response(new Response);
std::unique_ptr<Response> response(new Response());
response->Init(raw_message);
return response;
}

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()));
return response;
}

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));
return response;
}
Expand All @@ -418,14 +412,13 @@ std::unique_ptr<Response> Response::CreateEmpty() {
// ErrorResponse implementation.
//

ErrorResponse::ErrorResponse() : Response() {
}
ErrorResponse::ErrorResponse() = default;

std::unique_ptr<ErrorResponse> ErrorResponse::FromRawMessage(
DBusMessage* 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);
return response;
}
Expand All @@ -434,7 +427,7 @@ std::unique_ptr<ErrorResponse> ErrorResponse::FromMethodCall(
MethodCall* method_call,
const std::string& error_name,
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(),
error_name.c_str(),
error_message.c_str()));
Expand Down Expand Up @@ -551,23 +544,21 @@ void MessageWriter::OpenVariant(const std::string& signature,
void MessageWriter::OpenStruct(MessageWriter* writer) {
DCHECK(!container_is_open_);

const bool success = dbus_message_iter_open_container(
&raw_message_iter_,
DBUS_TYPE_STRUCT,
NULL, // Signature should be NULL.
&writer->raw_message_iter_);
const bool success =
dbus_message_iter_open_container(&raw_message_iter_, DBUS_TYPE_STRUCT,
nullptr, // Signature should be nullptr.
&writer->raw_message_iter_);
CHECK(success) << "Unable to allocate memory";
container_is_open_ = true;
}

void MessageWriter::OpenDictEntry(MessageWriter* writer) {
DCHECK(!container_is_open_);

const bool success = dbus_message_iter_open_container(
&raw_message_iter_,
DBUS_TYPE_DICT_ENTRY,
NULL, // Signature should be NULL.
&writer->raw_message_iter_);
const bool success =
dbus_message_iter_open_container(&raw_message_iter_, DBUS_TYPE_DICT_ENTRY,
nullptr, // Signature should be nullptr.
&writer->raw_message_iter_);
CHECK(success) << "Unable to allocate memory";
container_is_open_ = true;
}
Expand Down Expand Up @@ -777,15 +768,15 @@ bool MessageReader::PopDouble(double* value) {
}

bool MessageReader::PopString(std::string* value) {
char* tmp_value = NULL;
char* tmp_value = nullptr;
const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value);
if (success)
value->assign(tmp_value);
return success;
}

bool MessageReader::PopObjectPath(ObjectPath* value) {
char* tmp_value = NULL;
char* tmp_value = nullptr;
const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
if (success)
*value = ObjectPath(tmp_value);
Expand Down Expand Up @@ -815,7 +806,7 @@ bool MessageReader::PopArrayOfBytes(const uint8_t** bytes, size_t* length) {
// An empty array is allowed.
if (!array_reader.HasMoreData()) {
*length = 0;
*bytes = NULL;
*bytes = nullptr;
return true;
}
if (!array_reader.CheckDataType(DBUS_TYPE_BYTE))
Expand Down Expand Up @@ -879,8 +870,8 @@ bool MessageReader::PopArrayOfObjectPaths(

bool MessageReader::PopArrayOfBytesAsProto(
google::protobuf::MessageLite* protobuf) {
DCHECK(protobuf != NULL);
const char* serialized_buf = NULL;
DCHECK(protobuf);
const char* serialized_buf = nullptr;
size_t buf_size = 0;
if (!PopArrayOfBytes(reinterpret_cast<const uint8_t**>(&serialized_buf),
&buf_size)) {
Expand Down Expand Up @@ -935,15 +926,15 @@ bool MessageReader::PopVariantOfDouble(double* value) {
}

bool MessageReader::PopVariantOfString(std::string* value) {
char* tmp_value = NULL;
char* tmp_value = nullptr;
const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value);
if (success)
value->assign(tmp_value);
return success;
}

bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) {
char* tmp_value = NULL;
char* tmp_value = nullptr;
const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
if (success)
*value = ObjectPath(tmp_value);
Expand Down
10 changes: 4 additions & 6 deletions dbus/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ class CHROME_DBUS_EXPORT MethodCall : public Message {
const std::string& method_name);

// Returns a newly created MethodCall from the given raw message of the
// type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the
// returned object. Takes the ownership of |raw_message|.
static MethodCall* FromRawMessage(DBusMessage* raw_message);
// type DBUS_MESSAGE_TYPE_METHOD_CALL. Takes the ownership of |raw_message|.
static std::unique_ptr<MethodCall> FromRawMessage(DBusMessage* raw_message);

private:
// Creates a method call message. The internal raw message is NULL.
Expand All @@ -187,9 +186,8 @@ class CHROME_DBUS_EXPORT Signal : public Message {
const std::string& method_name);

// Returns a newly created SIGNAL from the given raw message of the type
// DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned
// object. Takes the ownership of |raw_message|.
static Signal* FromRawMessage(DBusMessage* raw_message);
// DBUS_MESSAGE_TYPE_SIGNAL. Takes the ownership of |raw_message|.
static std::unique_ptr<Signal> FromRawMessage(DBusMessage* raw_message);

private:
// Creates a signal message. The internal raw message is NULL.
Expand Down
16 changes: 9 additions & 7 deletions dbus/object_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@ namespace dbus {

namespace {

const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown";
const char kErrorObjectUnknown[] = "org.freedesktop.DBus.Error.UnknownObject";
constexpr char kErrorServiceUnknown[] =
"org.freedesktop.DBus.Error.ServiceUnknown";
constexpr char kErrorObjectUnknown[] =
"org.freedesktop.DBus.Error.UnknownObject";

// 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.
const char kDBusSystemObjectPath[] = "/org/freedesktop/DBus";
constexpr char kDBusSystemObjectPath[] = "/org/freedesktop/DBus";

// The D-Bus Object interface.
const char kDBusSystemObjectInterface[] = "org.freedesktop.DBus";
constexpr char kDBusSystemObjectInterface[] = "org.freedesktop.DBus";

// The D-Bus Object address.
const char kDBusSystemObjectAddress[] = "org.freedesktop.DBus";
constexpr char kDBusSystemObjectAddress[] = "org.freedesktop.DBus";

// The NameOwnerChanged member in |kDBusSystemObjectInterface|.
const char kNameOwnerChangedMember[] = "NameOwnerChanged";
constexpr char kNameOwnerChangedMember[] = "NameOwnerChanged";

// An empty function used for ObjectProxy::EmptyResponseCallback().
void EmptyResponseCallbackBody(Response* /*response*/) {
Expand Down

0 comments on commit 1156b45

Please sign in to comment.