diff options
author | nona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-19 15:17:28 +0000 |
---|---|---|
committer | nona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-19 15:17:28 +0000 |
commit | a9026da8513cbf7fcbdb88a00aca4e8cf5b3a1a1 (patch) | |
tree | 6f194a506528a5086988d3a653cf8f970d47b932 /chromeos | |
parent | 020d85b2d2b19aa14c43d209129ec46be050e23d (diff) | |
download | chromium_src-a9026da8513cbf7fcbdb88a00aca4e8cf5b3a1a1.zip chromium_src-a9026da8513cbf7fcbdb88a00aca4e8cf5b3a1a1.tar.gz chromium_src-a9026da8513cbf7fcbdb88a00aca4e8cf5b3a1a1.tar.bz2 |
Extends IBusObject to handle attachment field.
With this CL, IBusObjectReader/IBusObjectWriter can read/write attachment field in IBusObject header field.
BUG=140186
TEST=ran chromeos_unittests
Review URL: https://chromiumcodereview.appspot.com/11361210
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@168522 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos')
-rw-r--r-- | chromeos/dbus/ibus/ibus_component.cc | 2 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_lookup_table.cc | 59 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_lookup_table.h | 4 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_object.cc | 150 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_object.h | 52 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_object_unittest.cc | 77 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_property.cc | 3 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_text.cc | 72 | ||||
-rw-r--r-- | chromeos/dbus/ibus/ibus_text_unittest.cc | 116 |
9 files changed, 229 insertions, 306 deletions
diff --git a/chromeos/dbus/ibus/ibus_component.cc b/chromeos/dbus/ibus/ibus_component.cc index 0a606e4..a7bedd6 100644 --- a/chromeos/dbus/ibus/ibus_component.cc +++ b/chromeos/dbus/ibus/ibus_component.cc @@ -89,6 +89,7 @@ void AppendIBusEngineDesc(const IBusComponent::EngineDescription& engine_desc, IBusObjectWriter ibus_object_writer("IBusEngineDesc", "ssssssssusss", writer); + ibus_object_writer.CloseHeader(); ibus_object_writer.AppendString(engine_desc.engine_id); ibus_object_writer.AppendString(engine_desc.display_name); ibus_object_writer.AppendString(engine_desc.description); @@ -193,6 +194,7 @@ bool CHROMEOS_EXPORT PopIBusComponent(dbus::MessageReader* reader, void CHROMEOS_EXPORT AppendIBusComponent(const IBusComponent& ibus_component, dbus::MessageWriter* writer) { IBusObjectWriter ibus_object_writer("IBusComponent", "ssssssssavav", writer); + ibus_object_writer.CloseHeader(); ibus_object_writer.AppendString(ibus_component.name()); ibus_object_writer.AppendString(ibus_component.description()); ibus_object_writer.AppendString(""); // The version string is not used. diff --git a/chromeos/dbus/ibus/ibus_lookup_table.cc b/chromeos/dbus/ibus/ibus_lookup_table.cc index 2a926f1..9a751b3 100644 --- a/chromeos/dbus/ibus/ibus_lookup_table.cc +++ b/chromeos/dbus/ibus/ibus_lookup_table.cc @@ -6,9 +6,10 @@ #include <string> #include "base/logging.h" -#include "dbus/message.h" -#include "chromeos/dbus/ibus/ibus_text.h" +#include "base/values.h" #include "chromeos/dbus/ibus/ibus_object.h" +#include "chromeos/dbus/ibus/ibus_text.h" +#include "dbus/message.h" namespace chromeos { // TODO(nona): Remove ibus namespace after complete libibus removal. @@ -17,6 +18,7 @@ namespace ibus { namespace { // The default entry number of a page in IBusLookupTable. const int kDefaultPageSize = 9; +const char kShowWindowAtCompositionKey[] = "show_window_at_composition"; } // namespace void AppendIBusLookupTable(const IBusLookupTable& table, @@ -24,6 +26,11 @@ void AppendIBusLookupTable(const IBusLookupTable& table, IBusObjectWriter ibus_lookup_table_writer("IBusLookupTable", "uubbiavav", writer); + scoped_ptr<base::Value> show_position( + base::Value::CreateBooleanValue(table.show_window_at_composition())); + ibus_lookup_table_writer.AddAttachment(kShowWindowAtCompositionKey, + *show_position.get()); + ibus_lookup_table_writer.CloseHeader(); ibus_lookup_table_writer.AppendUint32(table.page_size()); ibus_lookup_table_writer.AppendUint32(table.cursor_position()); ibus_lookup_table_writer.AppendBool(table.is_cursor_visible()); @@ -60,46 +67,15 @@ void AppendIBusLookupTable(const IBusLookupTable& table, bool PopIBusLookupTable(dbus::MessageReader* reader, IBusLookupTable* table) { IBusObjectReader ibus_object_reader("IBusLookupTable", reader); - dbus::MessageReader attachment_reader(NULL); - if (!ibus_object_reader.InitWithAttachmentReader(&attachment_reader)) + if (!ibus_object_reader.Init()) return false; - while (attachment_reader.HasMoreData()) { - dbus::MessageReader dictionary_reader(NULL); - if (!attachment_reader.PopDictEntry(&dictionary_reader)) { - LOG(ERROR) << "Invalid attachment structure: " - << "The attachment field is array of dictionary entry."; - return false; - } - - std::string key; - if (!dictionary_reader.PopString(&key)) { - LOG(ERROR) << "Invalid attachement structure: " - << "The 1st dictionary entry should be string."; - return false; - } - if (key != "show_window_at_composition") - continue; - - dbus::MessageReader variant_reader(NULL); - if (!dictionary_reader.PopVariant(&variant_reader)) { - LOG(ERROR) << "Invalid attachment structure: " - << "The 2nd dictionary entry shuold be variant."; - return false; - } - - dbus::MessageReader sub_variant_reader(NULL); - if (!variant_reader.PopVariant(&sub_variant_reader)) { - LOG(ERROR) << "Invalid attachment structure: " - << "The 2nd variant entry should contain variant."; - return false; - } - - bool show_window_at_composition = false; - if (!sub_variant_reader.PopBool(&show_window_at_composition)) - continue; // Ignores other field. - - table->set_show_window_at_composition(show_window_at_composition); + const base::Value* value = + ibus_object_reader.GetAttachment(kShowWindowAtCompositionKey); + if (value) { + bool show_window_at_composition; + if (value->GetAsBoolean(&show_window_at_composition)) + table->set_show_window_at_composition(show_window_at_composition); } uint32 page_size = 0; @@ -202,7 +178,8 @@ IBusLookupTable::IBusLookupTable() : page_size_(kDefaultPageSize), cursor_position_(0), is_cursor_visible_(true), - orientation_(IBUS_LOOKUP_TABLE_ORIENTATION_HORIZONTAL) { + orientation_(IBUS_LOOKUP_TABLE_ORIENTATION_HORIZONTAL), + show_window_at_composition_(false) { } IBusLookupTable::~IBusLookupTable() { diff --git a/chromeos/dbus/ibus/ibus_lookup_table.h b/chromeos/dbus/ibus/ibus_lookup_table.h index 73d11bd..0e1fe0f 100644 --- a/chromeos/dbus/ibus/ibus_lookup_table.h +++ b/chromeos/dbus/ibus/ibus_lookup_table.h @@ -126,7 +126,9 @@ class CHROMEOS_EXPORT IBusLookupTable { const std::vector<Entry>& candidates() const { return candidates_; } std::vector<Entry>* mutable_candidates() { return &candidates_; } - bool show_window_at_composition() { return show_window_at_composition_; } + bool show_window_at_composition() const { + return show_window_at_composition_; + } void set_show_window_at_composition(bool show_window_at_composition) { show_window_at_composition_ = show_window_at_composition; } diff --git a/chromeos/dbus/ibus/ibus_object.cc b/chromeos/dbus/ibus/ibus_object.cc index 641ed55..d4292f9 100644 --- a/chromeos/dbus/ibus/ibus_object.cc +++ b/chromeos/dbus/ibus/ibus_object.cc @@ -5,9 +5,11 @@ #include "chromeos/dbus/ibus/ibus_object.h" #include "base/logging.h" +#include "base/values.h" #include "chromeos/dbus/ibus/ibus_property.h" #include "chromeos/dbus/ibus/ibus_text.h" #include "dbus/message.h" +#include "dbus/values_util.h" namespace chromeos { // TODO(nona): Remove ibus namespace after complete libibus removal. @@ -25,9 +27,12 @@ IBusObjectReader::IBusObjectReader(const std::string& type_name, } IBusObjectReader::~IBusObjectReader() { + for (std::map<std::string, base::Value*>::iterator ite = attachments_.begin(); + ite != attachments_.end(); ++ite) + delete ite->second; } -bool IBusObjectReader::InitWithoutAttachment() { +bool IBusObjectReader::Init() { DCHECK(original_reader_); DCHECK_EQ(IBUS_OBJECT_NOT_CHECKED, check_result_); @@ -63,38 +68,52 @@ bool IBusObjectReader::InitWithoutAttachment() { return false; } - return true; -} - -bool IBusObjectReader::Init() { - if (!InitWithoutAttachment()) - return false; - - // Ignores attachment field. - dbus::MessageReader array_reader(NULL); - if (!contents_reader_->PopArray(&array_reader)) { - LOG(ERROR) << "Invalid object structure[" << type_name_ << "] " - << "can not find attachment array field."; - return false; - } - DLOG_IF(WARNING, array_reader.HasMoreData()) - << "Ignoring attachment field in " << type_name_ <<"."; - check_result_ = IBUS_OBJECT_VALID; - return true; -} - -bool IBusObjectReader::InitWithAttachmentReader(dbus::MessageReader* reader) { - DCHECK(reader); - if (!InitWithoutAttachment()) - return false; + dbus::MessageReader attachment_reader(NULL); // IBus object has array object at the second element, which is used in // attaching additional information. - if (!contents_reader_->PopArray(reader)) { + if (!contents_reader_->PopArray(&attachment_reader)) { LOG(ERROR) << "Invalid object structure[" << type_name_ << "] " << "can not find attachment array field."; return false; } + + while (attachment_reader.HasMoreData()) { + dbus::MessageReader dictionary_reader(NULL); + if (!attachment_reader.PopDictEntry(&dictionary_reader)) { + LOG(ERROR) << "Invalid attachment structure: " + << "The attachment field is array of dictionary entry."; + return false; + } + + std::string key; + if (!dictionary_reader.PopString(&key)) { + LOG(ERROR) << "Invalid attachement structure: " + << "The 1st dictionary entry should be string."; + return false; + } + + if (key.empty()) { + LOG(ERROR) << "Invalid attachement key: key is empty."; + return false; + } + + dbus::MessageReader variant_reader(NULL); + if (!dictionary_reader.PopVariant(&variant_reader)) { + LOG(ERROR) << "Invalid attachment structure: " + << "The 2nd dictionary entry shuold be variant."; + return false; + } + + dbus::MessageReader sub_variant_reader(NULL); + if (!variant_reader.PopVariant(&sub_variant_reader)) { + LOG(ERROR) << "Invalid attachment structure: " + << "The 2nd variant entry should contain variant."; + return false; + } + + attachments_[key] = dbus::PopDataAsValue(&sub_variant_reader); + } check_result_ = IBUS_OBJECT_VALID; return true; } @@ -161,6 +180,17 @@ bool IBusObjectReader::PopIBusPropertyList(IBusPropertyList* properties) { contents_reader_.get(), properties); } +const base::Value* IBusObjectReader::GetAttachment(const std::string& key) { + DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); + DCHECK(contents_reader_.get()); + if (!IsValid()) + return NULL; + std::map<std::string, base::Value*>::iterator it = attachments_.find(key); + if (it == attachments_.end()) + return NULL; + return it->second; +} + bool IBusObjectReader::HasMoreData() { DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); DCHECK(contents_reader_.get()); @@ -186,7 +216,8 @@ IBusObjectWriter::IBusObjectWriter(const std::string& type_name, dbus::MessageWriter* writer) : type_name_(type_name), signature_(signature), - original_writer_(writer) { + original_writer_(writer), + state_(NOT_INITIALZED) { if (original_writer_) Init(); } @@ -195,92 +226,91 @@ IBusObjectWriter::~IBusObjectWriter() { } void IBusObjectWriter::AppendString(const std::string& input) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); contents_writer_->AppendString(input); } void IBusObjectWriter::AppendUint32(uint32 input) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); contents_writer_->AppendUint32(input); } void IBusObjectWriter::AppendInt32(int32 input) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); contents_writer_->AppendInt32(input); } void IBusObjectWriter::AppendBool(bool input) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); contents_writer_->AppendBool(input); } void IBusObjectWriter::OpenArray(const std::string& signature, dbus::MessageWriter* writer) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); contents_writer_->OpenArray(signature, writer); } void IBusObjectWriter::AppendIBusText(const IBusText& text) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); chromeos::ibus::AppendIBusText(text, contents_writer_.get()); } void IBusObjectWriter::AppendStringAsIBusText(const std::string& text) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); chromeos::ibus::AppendStringAsIBusText(text, contents_writer_.get()); } void IBusObjectWriter::AppendIBusProperty(const IBusProperty& property) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); chromeos::ibus::AppendIBusProperty(property, contents_writer_.get()); } void IBusObjectWriter::AppendIBusPropertyList( const IBusPropertyList& property_list) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); chromeos::ibus::AppendIBusPropertyList(property_list, contents_writer_.get()); } void IBusObjectWriter::CloseContainer(dbus::MessageWriter* writer) { - DCHECK(IsInitialized()); + DCHECK_EQ(state_, INITIALIZED); contents_writer_->CloseContainer(writer); } void IBusObjectWriter::AppendIBusObject(IBusObjectWriter* writer) { - DCHECK(IsInitialized()); - DCHECK(!writer->IsInitialized()) << "Given writer is already initialized"; - + DCHECK_EQ(state_, INITIALIZED); writer->InitWithParentWriter(contents_writer_.get()); } void IBusObjectWriter::Init() { DCHECK(original_writer_); - DCHECK(!IsInitialized()); + DCHECK_EQ(state_, NOT_INITIALZED); top_variant_writer_.reset(new dbus::MessageWriter(NULL)); contents_writer_.reset(new dbus::MessageWriter(NULL)); + attachment_writer_.reset(new dbus::MessageWriter(NULL)); const std::string ibus_signature = "(sa{sv}" + signature_ + ")"; original_writer_->OpenVariant(ibus_signature, top_variant_writer_.get()); top_variant_writer_->OpenStruct(contents_writer_.get()); contents_writer_->AppendString(type_name_); - dbus::MessageWriter header_array_writer(NULL); - // There is no case setting any attachment in ChromeOS, so setting empty - // array is enough. - contents_writer_->OpenArray("{sv}", &header_array_writer); - contents_writer_->CloseContainer(&header_array_writer); + contents_writer_->OpenArray("{sv}", attachment_writer_.get()); + state_ = HEADER_OPEN; } void IBusObjectWriter::InitWithParentWriter(dbus::MessageWriter* writer) { + DCHECK_EQ(state_, NOT_INITIALZED) << "Already initialized."; original_writer_ = writer; Init(); } void IBusObjectWriter::CloseAll() { DCHECK(original_writer_); - DCHECK(IsInitialized()); + DCHECK_NE(state_, NOT_INITIALZED); + if (state_ == HEADER_OPEN) + CloseHeader(); top_variant_writer_->CloseContainer(contents_writer_.get()); original_writer_->CloseContainer(top_variant_writer_.get()); @@ -288,8 +318,30 @@ void IBusObjectWriter::CloseAll() { contents_writer_.reset(); } -bool IBusObjectWriter::IsInitialized() const { - return contents_writer_.get() != NULL; +void IBusObjectWriter::CloseHeader() { + DCHECK_EQ(state_, HEADER_OPEN) << "Header is already closed."; + contents_writer_->CloseContainer(attachment_writer_.get()); + state_ = INITIALIZED; +} + +bool IBusObjectWriter::AddAttachment(const std::string& key, + const base::Value& value) { + DCHECK_NE(state_, NOT_INITIALZED) << "Do not call before Init();"; + DCHECK_NE(state_, INITIALIZED) << "Do not call after CloseHeader()."; + DCHECK(attachment_writer_.get()); + DCHECK(!key.empty()); + DCHECK(!value.IsType(base::Value::TYPE_NULL)); + + dbus::MessageWriter dict_writer(NULL); + attachment_writer_->OpenDictEntry(&dict_writer); + dict_writer.AppendString(key); + dbus::MessageWriter variant_writer(NULL); + dict_writer.OpenVariant("v", &variant_writer); + + dbus::AppendBasicTypeValueDataAsVariant(&variant_writer, value); + dict_writer.CloseContainer(&variant_writer); + attachment_writer_->CloseContainer(&variant_writer); + return true; } } // namespace ibus diff --git a/chromeos/dbus/ibus/ibus_object.h b/chromeos/dbus/ibus/ibus_object.h index b6cf45c..66defa6 100644 --- a/chromeos/dbus/ibus/ibus_object.h +++ b/chromeos/dbus/ibus/ibus_object.h @@ -5,17 +5,23 @@ #ifndef CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ #define CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ +#include <map> #include <string> + #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" #include "chromeos/chromeos_export.h" #include "chromeos/dbus/ibus/ibus_property.h" #include "chromeos/dbus/ibus/ibus_text.h" +namespace base { +class Value; +} // namespace base + namespace dbus { class MessageReader; class MessageWriter; -} // dbus +} // namespace dbus namespace chromeos { // TODO(nona): Remove ibus namespace after complete libibus removal. @@ -75,6 +81,12 @@ namespace ibus { // // Craetes reader for IBusText // IBusObjectReader object_reader("IBusText", &reader); // +// // Initialize for reading attachment field. +// object_reader.Init(); +// +// // Get attachment field. +// base::Value* value = object_reader.GetAttachment("annotation"); +// // std::string text; // reader.PopString(&text); // Reading 1st value as string. // @@ -93,10 +105,6 @@ class CHROMEOS_EXPORT IBusObjectReader { // to read attachment field. bool Init(); - // Reads IBusObject headers and checks if the type name is valid. - // Returns true and sets up reader for attachment reading on success. - bool InitWithAttachmentReader(dbus::MessageReader* reader); - // Reads IBusOBject with |reader| and checks if the type name is valid. bool InitWithParentReader(dbus::MessageReader* reader); @@ -129,6 +137,10 @@ class CHROMEOS_EXPORT IBusObjectReader { // Pops a IBusPropertyList. bool PopIBusPropertyList(ibus::IBusPropertyList* property_list); + // Gets attachment entry corresponding to |key|. Do not free returned value. + // Returns NULL if there is no entry. + const base::Value* GetAttachment(const std::string& key); + private: enum CheckResult { IBUS_OBJECT_VALID, // Already checked and valid type. @@ -136,15 +148,12 @@ class CHROMEOS_EXPORT IBusObjectReader { IBUS_OBJECT_NOT_CHECKED, // Not checked yet. }; - // Reads IBusObject headers without attachment and checks if the type name - // is valid. - bool InitWithoutAttachment(); - std::string type_name_; dbus::MessageReader* original_reader_; scoped_ptr<dbus::MessageReader> top_variant_reader_; scoped_ptr<dbus::MessageReader> contents_reader_; CheckResult check_result_; + std::map<std::string, base::Value*> attachments_; DISALLOW_COPY_AND_ASSIGN(IBusObjectReader); }; @@ -158,6 +167,13 @@ class CHROMEOS_EXPORT IBusObjectReader { // // Creates writer for IBusText // IBusObjectWriter object_writer("IBusText", "sv", &writer); // +// // Add some attachments. +// base::Value* value = base::Value::CreateStringValue("Noun"); +// object_writer.AddAttachment("annotation", *value); +// +// // Close header section. +// object_writer.CloseHeader(); +// // const std::string text = "Sample Text"; // writer.AppendString(text); // @@ -170,15 +186,28 @@ class CHROMEOS_EXPORT IBusObjectReader { // object_writer.CloseAll(); class CHROMEOS_EXPORT IBusObjectWriter { public: + enum WriterState { + NOT_INITIALZED, // Created but not initialized. + HEADER_OPEN, // Ready for writing attachment field. + INITIALIZED // Ready for writing content values. + }; + // |writer| must be released by caller. IBusObjectWriter(const std::string& type_name, const std::string& signature, dbus::MessageWriter* writer); virtual ~IBusObjectWriter(); + // Closes header to write content values. + void CloseHeader(); + // Appends IBusObject headers with |writer|, should be called once. void InitWithParentWriter(dbus::MessageWriter* writer); + // Adds an attachment, this function can be called only before CloseHeader + // function call. + bool AddAttachment(const std::string& key, const base::Value& value); + // The following functions delegate dbus::MessageReader's functions. void AppendString(const std::string& input); void AppendUint32(uint32 value); @@ -194,9 +223,6 @@ class CHROMEOS_EXPORT IBusObjectWriter { // Closes all opened containers. void CloseAll(); - // Returns true if writer is initialized. - bool IsInitialized() const; - // Appends a IBusText. void AppendIBusText(const ibus::IBusText& text); @@ -217,8 +243,10 @@ class CHROMEOS_EXPORT IBusObjectWriter { std::string type_name_; std::string signature_; dbus::MessageWriter* original_writer_; + WriterState state_; scoped_ptr<dbus::MessageWriter> top_variant_writer_; scoped_ptr<dbus::MessageWriter> contents_writer_; + scoped_ptr<dbus::MessageWriter> attachment_writer_; DISALLOW_COPY_AND_ASSIGN(IBusObjectWriter); }; diff --git a/chromeos/dbus/ibus/ibus_object_unittest.cc b/chromeos/dbus/ibus/ibus_object_unittest.cc index 468e268..0d1a2e2 100644 --- a/chromeos/dbus/ibus/ibus_object_unittest.cc +++ b/chromeos/dbus/ibus/ibus_object_unittest.cc @@ -9,6 +9,7 @@ #include <vector> #include "base/logging.h" #include "base/memory/scoped_ptr.h" +#include "base/values.h" #include "dbus/message.h" #include "testing/gtest/include/gtest/gtest.h" @@ -28,9 +29,19 @@ TEST(IBusObjectTest, WriteReadTest) { const int32 kSampleInt32 = 54321; const bool kSampleBool = false; const uint32 kSampleArrayOfUint32Count = 10UL; + const char kAttachmentKey[] = "key"; + const char kStringAttachment[] = "Attachment String Value"; // Create ibus object. IBusObjectWriter ibus_object_writer(kSampleTypeName1, "suibauv", &writer); + + // Write attachment field. + scoped_ptr<base::Value> write_value( + base::Value::CreateStringValue(kStringAttachment)); + ibus_object_writer.AddAttachment(kAttachmentKey, *write_value.get()); + ibus_object_writer.CloseHeader(); + + // Write contents values. ibus_object_writer.AppendString(kSampleText1); ibus_object_writer.AppendUint32(kSampleUint32); ibus_object_writer.AppendInt32(kSampleInt32); @@ -42,6 +53,7 @@ TEST(IBusObjectTest, WriteReadTest) { ibus_object_writer.CloseContainer(&array_writer); IBusObjectWriter ibus_nested_object_writer(kSampleTypeName2, "s", NULL); ibus_object_writer.AppendIBusObject(&ibus_nested_object_writer); + ibus_nested_object_writer.CloseHeader(); ibus_nested_object_writer.AppendString(kSampleText2); ibus_object_writer.CloseAll(); @@ -49,6 +61,13 @@ TEST(IBusObjectTest, WriteReadTest) { dbus::MessageReader reader(message.get()); IBusObjectReader ibus_object_reader(kSampleTypeName1, &reader); ASSERT_TRUE(ibus_object_reader.Init()); + // Check the attachment value; + const base::Value* read_value = + ibus_object_reader.GetAttachment(kAttachmentKey); + ASSERT_TRUE(read_value); + std::string attachment_value; + ASSERT_TRUE(read_value->GetAsString(&attachment_value)); + EXPECT_EQ(kStringAttachment, attachment_value); // Check the first string value. std::string expected_string; ASSERT_TRUE(ibus_object_reader.PopString(&expected_string)); @@ -93,6 +112,7 @@ TEST(IBusObjectTest, EmptyEntryTest) { // Write empty IBusObject. dbus::MessageWriter writer(message.get()); IBusObjectWriter ibus_object_writer(kSampleTypeName, "", &writer); + ibus_object_writer.CloseHeader(); ibus_object_writer.CloseAll(); // Read empty IBusObject. @@ -113,6 +133,7 @@ TEST(IBusObjectTest, PopAppendIBusTextTest) { // Write IBusText. dbus::MessageWriter writer(message.get()); IBusObjectWriter ibus_object_writer(kSampleTypeName, "v", &writer); + ibus_object_writer.CloseHeader(); IBusText ibus_text; ibus_text.mutable_selection_attributes()->push_back(selection_attribute); ibus_text.set_text(kSampleString); @@ -144,6 +165,7 @@ TEST(IBusObjectTest, PopAppendStringAsIBusText) { // Write string as IBusText. dbus::MessageWriter writer(message.get()); IBusObjectWriter ibus_object_writer(kSampleTypeName, "v", &writer); + ibus_object_writer.CloseHeader(); ibus_object_writer.AppendStringAsIBusText(kSampleString); ibus_object_writer.CloseAll(); @@ -157,60 +179,6 @@ TEST(IBusObjectTest, PopAppendStringAsIBusText) { EXPECT_EQ(kSampleString, result_str); } -TEST(IBusObjectTest, AttachmentTest) { - // The IBusObjectWriter does not support attachment field writing, so crate - // IBusObject with attachment field manually. - const char kSampleTypeName[] = "IBusObject Name"; - const char kSampleDictKey[] = "Sample Key"; - const char kSampleText[] = "SampleText"; - scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); - dbus::MessageWriter writer(message.get()); - - // Create IBusObject header. - dbus::MessageWriter top_variant_writer(NULL); - writer.OpenVariant("(sa{sv})", &top_variant_writer); - dbus::MessageWriter contents_writer(NULL); - top_variant_writer.OpenStruct(&contents_writer); - contents_writer.AppendString(kSampleTypeName); - - // Write values into attachment field. - dbus::MessageWriter attachment_array_writer(NULL); - contents_writer.OpenArray("{sv}", &attachment_array_writer); - dbus::MessageWriter entry_writer(NULL); - attachment_array_writer.OpenDictEntry(&entry_writer); - entry_writer.AppendString(kSampleDictKey); - dbus::MessageWriter variant_writer(NULL); - entry_writer.OpenVariant("s",&variant_writer); - variant_writer.AppendString(kSampleText); - - // Close all containers. - entry_writer.CloseContainer(&variant_writer); - attachment_array_writer.CloseContainer(&entry_writer); - contents_writer.CloseContainer(&attachment_array_writer); - top_variant_writer.CloseContainer(&contents_writer); - writer.CloseContainer(&top_variant_writer); - - // Read with IBusObjectReader. - dbus::MessageReader reader(message.get()); - IBusObjectReader ibus_object_reader(kSampleTypeName, &reader); - dbus::MessageReader attr_reader(NULL); - ASSERT_TRUE(ibus_object_reader.InitWithAttachmentReader(&attr_reader)); - dbus::MessageReader dict_reader(NULL); - - // Check the values. - ASSERT_TRUE(attr_reader.PopDictEntry(&dict_reader)); - std::string key; - std::string value; - - ASSERT_TRUE(dict_reader.PopString(&key)); - EXPECT_EQ(kSampleDictKey, key); - - dbus::MessageReader variant_reader(NULL); - ASSERT_TRUE(dict_reader.PopVariant(&variant_reader)); - ASSERT_TRUE(variant_reader.PopString(&value)); - EXPECT_EQ(kSampleText, value); -} - TEST(IBusObjectTest, PopAppendIBusPropertyTest) { const char kSampleTypeName[] = "Empty IBusObject Name"; const char kSampleKey[] = "Key"; @@ -234,6 +202,7 @@ TEST(IBusObjectTest, PopAppendIBusPropertyTest) { // Write a IBusProperty. dbus::MessageWriter writer(response.get()); IBusObjectWriter ibus_object_writer(kSampleTypeName, "v", &writer); + ibus_object_writer.CloseHeader(); ibus_object_writer.AppendIBusProperty(property); ibus_object_writer.CloseAll(); diff --git a/chromeos/dbus/ibus/ibus_property.cc b/chromeos/dbus/ibus/ibus_property.cc index 649a81f..c4147f6 100644 --- a/chromeos/dbus/ibus/ibus_property.cc +++ b/chromeos/dbus/ibus/ibus_property.cc @@ -139,6 +139,8 @@ bool CHROMEOS_EXPORT PopIBusPropertyList(dbus::MessageReader* reader, void CHROMEOS_EXPORT AppendIBusProperty(const IBusProperty& property, dbus::MessageWriter* writer) { IBusObjectWriter ibus_property_writer("IBusProperty", "suvsvbbuv", writer); + ibus_property_writer.CloseHeader(); + ibus_property_writer.AppendString(property.key()); ibus_property_writer.AppendUint32(static_cast<uint32>(property.type())); ibus_property_writer.AppendStringAsIBusText(property.label()); @@ -156,6 +158,7 @@ void CHROMEOS_EXPORT AppendIBusPropertyList( const IBusPropertyList& property_list, dbus::MessageWriter* writer) { IBusObjectWriter ibus_property_list_writer("IBusPropList", "av", writer); + ibus_property_list_writer.CloseHeader(); dbus::MessageWriter property_list_writer(NULL); ibus_property_list_writer.OpenArray("v", &property_list_writer); for (size_t i = 0; i < property_list.size(); ++i) { diff --git a/chromeos/dbus/ibus/ibus_text.cc b/chromeos/dbus/ibus/ibus_text.cc index a809732..ea290cc 100644 --- a/chromeos/dbus/ibus/ibus_text.cc +++ b/chromeos/dbus/ibus/ibus_text.cc @@ -5,6 +5,7 @@ #include "chromeos/dbus/ibus/ibus_text.h" #include "base/logging.h" +#include "base/values.h" #include "chromeos/dbus/ibus/ibus_object.h" #include "dbus/message.h" @@ -15,6 +16,8 @@ namespace ibus { namespace { const uint32 kAttributeUnderline = 1; // Indicates underline attribute. const uint32 kAttributeSelection = 2; // Indicates background attribute. +const char kAnnotationKey[] = "annotation"; +const char kDescriptionKey[] = "description"; struct IBusAttribute { IBusAttribute() : type(0), value(0), start_index(0), end_index(0) {} @@ -46,6 +49,7 @@ bool PopIBusAttribute(dbus::MessageReader* reader, IBusAttribute* attribute) { void AppendIBusAttribute(dbus::MessageWriter* writer, const IBusAttribute& attribute) { IBusObjectWriter ibus_attribute_writer("IBusAttribute", "uuuu", writer); + ibus_attribute_writer.CloseHeader(); ibus_attribute_writer.AppendUint32(attribute.type); ibus_attribute_writer.AppendUint32(attribute.value); ibus_attribute_writer.AppendUint32(attribute.start_index); @@ -58,11 +62,25 @@ void AppendIBusAttribute(dbus::MessageWriter* writer, void AppendIBusText(const IBusText& ibus_text, dbus::MessageWriter* writer) { IBusObjectWriter ibus_text_writer("IBusText", "sv", writer); + if (!ibus_text.annotation().empty()) { + scoped_ptr<base::Value> annotation( + base::Value::CreateStringValue(ibus_text.annotation())); + ibus_text_writer.AddAttachment(kAnnotationKey, *annotation.get()); + } + + if (!ibus_text.description().empty()) { + scoped_ptr<base::Value> description( + base::Value::CreateStringValue(ibus_text.description())); + ibus_text_writer.AddAttachment(kDescriptionKey, *description.get()); + } + ibus_text_writer.CloseHeader(); + ibus_text_writer.AppendString(ibus_text.text()); // Start appending IBusAttrList into IBusText IBusObjectWriter ibus_attr_list_writer("IBusAttrList", "av", NULL); ibus_text_writer.AppendIBusObject(&ibus_attr_list_writer); + ibus_attr_list_writer.CloseHeader(); dbus::MessageWriter attribute_array_writer(NULL); ibus_attr_list_writer.OpenArray("v", &attribute_array_writer); @@ -104,49 +122,23 @@ void CHROMEOS_EXPORT AppendStringAsIBusText(const std::string& text, bool PopIBusText(dbus::MessageReader* reader, IBusText* ibus_text) { IBusObjectReader ibus_text_reader("IBusText", reader); - dbus::MessageReader attachment_reader(NULL); - if (!ibus_text_reader.InitWithAttachmentReader(&attachment_reader)) + if (!ibus_text_reader.Init()) return false; - while (attachment_reader.HasMoreData()) { - dbus::MessageReader dictionary_reader(NULL); - if (!attachment_reader.PopDictEntry(&dictionary_reader)) { - LOG(ERROR) << "Invalid attachment structure: " - << "The attachment field is array of dictionary entry."; - return false; - } - - std::string key; - if (!dictionary_reader.PopString(&key)) { - LOG(ERROR) << "Invalid attachement structure: " - << "The 1st dictionary entry should be string."; - return false; - } - - dbus::MessageReader variant_reader(NULL); - if (!dictionary_reader.PopVariant(&variant_reader)) { - LOG(ERROR) << "Invalid attachment structure: " - << "The 2nd dictionary entry shuold be variant."; - return false; - } - - dbus::MessageReader sub_variant_reader(NULL); - if (!variant_reader.PopVariant(&sub_variant_reader)) { - LOG(ERROR) << "Invalid attachment structure: " - << "The 2nd variant entry should contain variant."; - return false; - } - - std::string value; - if (!sub_variant_reader.PopString(&value)) - continue; // Ignore other attachment values. + const base::Value* annotation_value = + ibus_text_reader.GetAttachment(kAnnotationKey); + if (annotation_value) { + std::string annotation; + if (annotation_value->GetAsString(&annotation)) + ibus_text->set_annotation(annotation); + } - if (key == "annotation") - ibus_text->set_annotation(value); - else if (key == "description") - ibus_text->set_description(value); - else - continue; // Ignore other fields. + const base::Value* description_value = + ibus_text_reader.GetAttachment(kDescriptionKey); + if (description_value) { + std::string description; + if (description_value->GetAsString(&description)) + ibus_text->set_description(description); } std::string text; diff --git a/chromeos/dbus/ibus/ibus_text_unittest.cc b/chromeos/dbus/ibus/ibus_text_unittest.cc index b3df566..d87bf39 100644 --- a/chromeos/dbus/ibus/ibus_text_unittest.cc +++ b/chromeos/dbus/ibus/ibus_text_unittest.cc @@ -21,6 +21,8 @@ namespace ibus { TEST(IBusTextTest, WriteReadTest) { const char kSampleText[] = "Sample Text"; + const char kAnnotation[] = "Annotation"; + const char kDescription[] = "Description"; const IBusText::UnderlineAttribute kSampleUnderlineAttribute1 = { IBusText::IBUS_TEXT_UNDERLINE_SINGLE, 10, 20}; @@ -35,6 +37,8 @@ TEST(IBusTextTest, WriteReadTest) { // Make IBusText IBusText text; text.set_text(kSampleText); + text.set_annotation(kAnnotation); + text.set_description(kDescription); std::vector<IBusText::UnderlineAttribute>* underline_attributes = text.mutable_underline_attributes(); underline_attributes->push_back(kSampleUnderlineAttribute1); @@ -53,7 +57,9 @@ TEST(IBusTextTest, WriteReadTest) { dbus::MessageReader reader(response.get()); IBusText expected_text; ASSERT_TRUE(PopIBusText(&reader, &expected_text)); - EXPECT_EQ(expected_text.text(), kSampleText); + EXPECT_EQ(kSampleText, expected_text.text()); + EXPECT_EQ(kAnnotation, expected_text.annotation()); + EXPECT_EQ(kDescription, expected_text.description()); EXPECT_EQ(3U, expected_text.underline_attributes().size()); EXPECT_EQ(1U, expected_text.selection_attributes().size()); } @@ -90,113 +96,5 @@ TEST(IBusTextTest, PopStringFromIBusTextTest) { EXPECT_EQ(kSampleText, result); } -TEST(IBusTextTest, ReadAnnotationFieldTest) { - const char kSampleText[] = "Sample Text"; - const char kSampleAnnotationText[] = "Sample Annotation"; - - // Create IBusLookupTable. - scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); - dbus::MessageWriter writer(response.get()); - dbus::MessageWriter top_variant_writer(NULL); - writer.OpenVariant("(sa{sv}sv)", &top_variant_writer); - dbus::MessageWriter contents_writer(NULL); - top_variant_writer.OpenStruct(&contents_writer); - contents_writer.AppendString("IBusText"); - - // Write attachment field. - dbus::MessageWriter attachment_array_writer(NULL); - contents_writer.OpenArray("{sv}", &attachment_array_writer); - dbus::MessageWriter entry_writer(NULL); - attachment_array_writer.OpenDictEntry(&entry_writer); - entry_writer.AppendString("annotation"); - dbus::MessageWriter variant_writer(NULL); - entry_writer.OpenVariant("v", &variant_writer); - dbus::MessageWriter sub_variant_writer(NULL); - variant_writer.OpenVariant("s", &sub_variant_writer); - sub_variant_writer.AppendString(kSampleAnnotationText); - - // Close attachment field container. - variant_writer.CloseContainer(&sub_variant_writer); - entry_writer.CloseContainer(&variant_writer); - attachment_array_writer.CloseContainer(&entry_writer); - contents_writer.CloseContainer(&attachment_array_writer); - - // Write IBusText contents. - contents_writer.AppendString(kSampleText); - IBusObjectWriter ibus_attr_list_writer("IBusAttrList", "av", - &contents_writer); - dbus::MessageWriter attribute_array_writer(NULL); - ibus_attr_list_writer.OpenArray("v", &attribute_array_writer); - ibus_attr_list_writer.CloseContainer(&attribute_array_writer); - ibus_attr_list_writer.CloseAll(); - - // Close all containers. - top_variant_writer.CloseContainer(&contents_writer); - writer.CloseContainer(&top_variant_writer); - - // Read IBusText. - IBusText ibus_text; - dbus::MessageReader reader(response.get()); - PopIBusText(&reader, &ibus_text); - - // Check values. - EXPECT_EQ(kSampleText, ibus_text.text()); - EXPECT_EQ(kSampleAnnotationText, ibus_text.annotation()); -} - -TEST(IBusTextTest, ReadDescriptionFieldTest) { - const char kSampleText[] = "Sample Text"; - const char kSampleDescriptionText[] = "Sample Description"; - - // Create IBusLookupTable. - scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); - dbus::MessageWriter writer(response.get()); - dbus::MessageWriter top_variant_writer(NULL); - writer.OpenVariant("(sa{sv}sv)", &top_variant_writer); - dbus::MessageWriter contents_writer(NULL); - top_variant_writer.OpenStruct(&contents_writer); - contents_writer.AppendString("IBusText"); - - // Write attachment field. - dbus::MessageWriter attachment_array_writer(NULL); - contents_writer.OpenArray("{sv}", &attachment_array_writer); - dbus::MessageWriter entry_writer(NULL); - attachment_array_writer.OpenDictEntry(&entry_writer); - entry_writer.AppendString("description"); - dbus::MessageWriter variant_writer(NULL); - entry_writer.OpenVariant("v", &variant_writer); - dbus::MessageWriter sub_variant_writer(NULL); - variant_writer.OpenVariant("s", &sub_variant_writer); - sub_variant_writer.AppendString(kSampleDescriptionText); - - // Close attachment field container. - variant_writer.CloseContainer(&sub_variant_writer); - entry_writer.CloseContainer(&variant_writer); - attachment_array_writer.CloseContainer(&entry_writer); - contents_writer.CloseContainer(&attachment_array_writer); - - // Write IBusText contents. - contents_writer.AppendString(kSampleText); - IBusObjectWriter ibus_attr_list_writer("IBusAttrList", "av", - &contents_writer); - dbus::MessageWriter attribute_array_writer(NULL); - ibus_attr_list_writer.OpenArray("v", &attribute_array_writer); - ibus_attr_list_writer.CloseContainer(&attribute_array_writer); - ibus_attr_list_writer.CloseAll(); - - // Close all containers. - top_variant_writer.CloseContainer(&contents_writer); - writer.CloseContainer(&top_variant_writer); - - // Read IBusText. - IBusText ibus_text; - dbus::MessageReader reader(response.get()); - PopIBusText(&reader, &ibus_text); - - // Check values. - EXPECT_EQ(kSampleText, ibus_text.text()); - EXPECT_EQ(kSampleDescriptionText, ibus_text.description()); -} - } // namespace ibus } // namespace chromeos |