summaryrefslogtreecommitdiffstats
path: root/chromeos
diff options
context:
space:
mode:
authornona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-23 23:27:16 +0000
committernona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-23 23:27:16 +0000
commit200d88f50350f0fb867ff07a7e4c4b8396373bda (patch)
tree2ebfa5cf2ee08b75ca05fcbc08952f03aac5bcb9 /chromeos
parent46220c22ece79f1f1c534427093f4521fe2766c6 (diff)
downloadchromium_src-200d88f50350f0fb867ff07a7e4c4b8396373bda.zip
chromium_src-200d88f50350f0fb867ff07a7e4c4b8396373bda.tar.gz
chromium_src-200d88f50350f0fb867ff07a7e4c4b8396373bda.tar.bz2
Implement reading attachment field in IBusObject.
IBusObjectReader reads attachment field in IBusObject header, which is constructed of array of dictionary entries. This field is used in Japanese input method to show usage information. IBusObjectWriter does not support writing attachment field because it is only sent by ibus-mozc engine to send usage information. BUG=chromium-os:30653 TEST=unit_tests,chromeos_unittests,dbus_unittests Review URL: https://chromiumcodereview.appspot.com/10417012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@138664 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos')
-rw-r--r--chromeos/dbus/ibus/ibus_object.cc42
-rw-r--r--chromeos/dbus/ibus/ibus_object.h29
-rw-r--r--chromeos/dbus/ibus/ibus_object_unittest.cc54
3 files changed, 108 insertions, 17 deletions
diff --git a/chromeos/dbus/ibus/ibus_object.cc b/chromeos/dbus/ibus/ibus_object.cc
index ebba2b3..faa0249 100644
--- a/chromeos/dbus/ibus/ibus_object.cc
+++ b/chromeos/dbus/ibus/ibus_object.cc
@@ -26,7 +26,7 @@ IBusObjectReader::IBusObjectReader(const std::string& type_name,
IBusObjectReader::~IBusObjectReader() {
}
-bool IBusObjectReader::Init() {
+bool IBusObjectReader::InitWithoutAttachment() {
DCHECK(original_reader_);
DCHECK_EQ(IBUS_OBJECT_NOT_CHECKED, check_result_);
@@ -62,17 +62,37 @@ bool IBusObjectReader::Init() {
return false;
}
- // IBus object has array object at the second element, which is used in
- // attaching additional information.
- // TODO(nona): Read mozc.candidates in attachement array.
+ 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 attachement array field.";
+ LOG(ERROR) << "Invalid object structure[" << type_name_ << "] "
+ << "can not find attachment array field.";
return false;
}
- if (array_reader.HasMoreData()) {
- LOG(ERROR) << "Reading attachement field is not implemented.";
+ 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;
+
+ // IBus object has array object at the second element, which is used in
+ // attaching additional information.
+ if (!contents_reader_->PopArray(reader)) {
+ LOG(ERROR) << "Invalid object structure[" << type_name_ << "] "
+ << "can not find attachment array field.";
+ return false;
}
check_result_ = IBUS_OBJECT_VALID;
return true;
@@ -214,16 +234,16 @@ void IBusObjectWriter::Init() {
top_variant_writer_.reset(new dbus::MessageWriter(NULL));
contents_writer_.reset(new dbus::MessageWriter(NULL));
- const std::string ibus_signature = "(sav" + signature_ + ")";
+ 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 attachement in ChromeOS, so setting empty
+ // There is no case setting any attachment in ChromeOS, so setting empty
// array is enough.
- contents_writer_->OpenArray("v", &header_array_writer);
+ contents_writer_->OpenArray("{sv}", &header_array_writer);
contents_writer_->CloseContainer(&header_array_writer);
}
diff --git a/chromeos/dbus/ibus/ibus_object.h b/chromeos/dbus/ibus/ibus_object.h
index d56b556..275fc71 100644
--- a/chromeos/dbus/ibus/ibus_object.h
+++ b/chromeos/dbus/ibus/ibus_object.h
@@ -23,7 +23,7 @@ namespace ibus {
// The data structure of IBusObject is represented as variant in "(sav...)"
// signatur. The IBusObject is constructed with two sections, header and
// contents. The header section is represent as "sav" which contains type name
-// and attachement array. The contents section is corresponding to "..." in
+// and attachment array. The contents section is corresponding to "..." in
// above signature, which can store arbitary type values including IBusObject.
//
// DATA STRUCTURE OVERVIEW:
@@ -32,7 +32,13 @@ namespace ibus {
// struct { // Handle with contents_writer_/contents_reader_.
// // Header section
// string typename // The type name of object, like "IBusText"
-// array [] // attachement array.
+// array [ // attachment array.
+// dict_entry (
+// string "mozc.candidates" // The key in the dictionary entry.
+// variant ... // The value in the dictionary entry.
+// )
+// ...
+// ]
//
// // Contents section
// ... // The contents area.
@@ -60,9 +66,9 @@ namespace ibus {
// }
// }
//
-// The IBusObjectReader class provides reading IBusObject from dbus message.
-// This class checks the IBusObject header structure and type name before
-// reading contents.
+// The IBusObjectReader class provides reading IBusObject including attachment
+// field from dbus message. This class checks the IBusObject header structure
+// and type name before reading contents.
//
// EXAPMLE USAGE:
// // Craetes reader for IBusText
@@ -82,9 +88,14 @@ class CHROMEOS_EXPORT IBusObjectReader {
virtual ~IBusObjectReader();
// Reads IBusObject headers and checks if the type name is valid.
- // Returns true on success.
+ // Returns true on success. Uses InitWitAttachmentReader instead if you want
+ // 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);
@@ -118,6 +129,10 @@ 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_;
@@ -129,6 +144,8 @@ class CHROMEOS_EXPORT IBusObjectReader {
// IBusObjectWriter class provides writing IBusObject to dbus message. This
// class appends header section before appending contents values.
+// IBusObjectWriter does not support writing attachment field because writing
+// attachment field is not used in Chrome.
//
// EXAMPLE USAGE:
// // Creates writer for IBusText
diff --git a/chromeos/dbus/ibus/ibus_object_unittest.cc b/chromeos/dbus/ibus/ibus_object_unittest.cc
index 574b61d..dd543ea 100644
--- a/chromeos/dbus/ibus/ibus_object_unittest.cc
+++ b/chromeos/dbus/ibus/ibus_object_unittest.cc
@@ -156,5 +156,59 @@ 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);
+}
+
} // namespace ibus
} // namespace chromeos