diff options
author | mdm@chromium.org <mdm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-19 18:22:14 +0000 |
---|---|---|
committer | mdm@chromium.org <mdm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-19 18:22:14 +0000 |
commit | 8bc83fd407b76025b411004554fb410789512fc4 (patch) | |
tree | 005f411f7aa09b324ae535af8eb7b5bc7fd9b031 /dbus | |
parent | 9e0ce17c8308d897b7fadf7b1164c4c5234cfe98 (diff) | |
download | chromium_src-8bc83fd407b76025b411004554fb410789512fc4.zip chromium_src-8bc83fd407b76025b411004554fb410789512fc4.tar.gz chromium_src-8bc83fd407b76025b411004554fb410789512fc4.tar.bz2 |
Linux: add two new DBus client library utility functions, to be used 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
Diffstat (limited to 'dbus')
-rw-r--r-- | dbus/message.cc | 25 | ||||
-rw-r--r-- | dbus/message.h | 15 | ||||
-rw-r--r-- | dbus/message_unittest.cc | 33 |
3 files changed, 66 insertions, 7 deletions
diff --git a/dbus/message.cc b/dbus/message.cc index 2837ce5..aa9cc55 100644 --- a/dbus/message.cc +++ b/dbus/message.cc @@ -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_); @@ -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_); diff --git a/dbus/message.h b/dbus/message.h index 72200fc..ceeb0ed 100644 --- a/dbus/message.h +++ b/dbus/message.h @@ -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); @@ -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. // diff --git a/dbus/message_unittest.cc b/dbus/message_unittest.cc index 473a6ef..29fe888 100644 --- a/dbus/message_unittest.cc +++ b/dbus/message_unittest.cc @@ -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) { @@ -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 |