Skip to content

Commit

Permalink
Linux: add two new DBus client library utility functions, to be used …
Browse files Browse the repository at this point in the history
…by KWallet.

The new functions are AppendArrayOfStrings and PopArrayOfStrings, which have
self-explanatory names. Currently the KWallet code does this looping itself.
Review URL: http://codereview.chromium.org/7941009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@101781 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mdm@chromium.org committed Sep 19, 2011
1 parent 9e0ce17 commit 8bc83fd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
25 changes: 25 additions & 0 deletions dbus/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,17 @@ void MessageWriter::AppendArrayOfBytes(const uint8* values, size_t length) {
CloseContainer(&array_writer);
}

void MessageWriter::AppendArrayOfStrings(
const std::vector<std::string>& strings) {
DCHECK(!container_is_open_);
MessageWriter array_writer(message_);
OpenArray("s", &array_writer);
for (size_t i = 0; i < strings.size(); ++i) {
array_writer.AppendString(strings[i]);
}
CloseContainer(&array_writer);
}

void MessageWriter::AppendArrayOfObjectPaths(
const std::vector<std::string>& object_paths) {
DCHECK(!container_is_open_);
Expand Down Expand Up @@ -754,6 +765,20 @@ bool MessageReader::PopArrayOfBytes(uint8** bytes, size_t* length) {
return bytes != NULL;
}

bool MessageReader::PopArrayOfStrings(
std::vector<std::string> *strings) {
MessageReader array_reader(message_);
if (!PopArray(&array_reader))
return false;
while (array_reader.HasMoreData()) {
std::string string;
if (!array_reader.PopString(&string))
return false;
strings->push_back(string);
}
return true;
}

bool MessageReader::PopArrayOfObjectPaths(
std::vector<std::string> *object_paths) {
MessageReader array_reader(message_);
Expand Down
15 changes: 14 additions & 1 deletion dbus/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,13 @@ class MessageWriter {
// function.
void AppendArrayOfBytes(const uint8* values, size_t length);

// Appends the array of strings. Arrays of strings are often used for
// exchanging lists of names hence it's worth having a specialized
// function.
void AppendArrayOfStrings(const std::vector<std::string>& strings);

// Appends the array of object paths. Arrays of object paths are often
// used to exchanging object paths, hence it's worth having a
// used when exchanging object paths, hence it's worth having a
// specialized function.
void AppendArrayOfObjectPaths(const std::vector<std::string>& object_paths);

Expand Down Expand Up @@ -371,6 +376,14 @@ class MessageReader {
// MessageReader is destroyed.
bool PopArrayOfBytes(uint8** bytes, size_t* length);

// Gets the array of strings at the current iterator position.
// Returns true and advances the iterator on success.
//
// Arrays of strings are often used to communicate with D-Bus
// services like KWallet, hence it's worth having a specialized
// function.
bool PopArrayOfStrings(std::vector<std::string>* strings);

// Gets the array of object paths at the current iterator position.
// Returns true and advances the iterator on success.
//
Expand Down
33 changes: 27 additions & 6 deletions dbus/message_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,30 @@ TEST(MessageTest, ArrayOfBytes) {
ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
ASSERT_FALSE(reader.HasMoreData());
ASSERT_EQ(3U, length);
ASSERT_EQ(1, output_bytes[0]);
ASSERT_EQ(2, output_bytes[1]);
ASSERT_EQ(3, output_bytes[2]);
EXPECT_EQ(1, output_bytes[0]);
EXPECT_EQ(2, output_bytes[1]);
EXPECT_EQ(3, output_bytes[2]);
}

TEST(MessageTest, ArrayOfStrings) {
scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
dbus::MessageWriter writer(message.get());
std::vector<std::string> strings;
strings.push_back("fee");
strings.push_back("fie");
strings.push_back("foe");
strings.push_back("fum");
writer.AppendArrayOfStrings(strings);

dbus::MessageReader reader(message.get());
std::vector<std::string> output_strings;
ASSERT_TRUE(reader.PopArrayOfStrings(&output_strings));
ASSERT_FALSE(reader.HasMoreData());
ASSERT_EQ(4U, output_strings.size());
EXPECT_EQ("fee", output_strings[0]);
EXPECT_EQ("fie", output_strings[1]);
EXPECT_EQ("foe", output_strings[2]);
EXPECT_EQ("fum", output_strings[3]);
}

TEST(MessageTest, ArrayOfObjectPaths) {
Expand All @@ -185,9 +206,9 @@ TEST(MessageTest, ArrayOfObjectPaths) {
ASSERT_TRUE(reader.PopArrayOfObjectPaths(&output_object_paths));
ASSERT_FALSE(reader.HasMoreData());
ASSERT_EQ(3U, output_object_paths.size());
ASSERT_EQ("/object/path/1", output_object_paths[0]);
ASSERT_EQ("/object/path/2", output_object_paths[1]);
ASSERT_EQ("/object/path/3", output_object_paths[2]);
EXPECT_EQ("/object/path/1", output_object_paths[0]);
EXPECT_EQ("/object/path/2", output_object_paths[1]);
EXPECT_EQ("/object/path/3", output_object_paths[2]);
}

// Test that an array can be properly written and read. We only have this
Expand Down

0 comments on commit 8bc83fd

Please sign in to comment.