| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chromeos/dbus/ibus/ibus_object.h" | 5 #include "chromeos/dbus/ibus/ibus_object.h" | 
| 6 | 6 | 
| 7 #include "base/logging.h" | 7 #include "base/logging.h" | 
|  | 8 #include "base/values.h" | 
| 8 #include "chromeos/dbus/ibus/ibus_property.h" | 9 #include "chromeos/dbus/ibus/ibus_property.h" | 
| 9 #include "chromeos/dbus/ibus/ibus_text.h" | 10 #include "chromeos/dbus/ibus/ibus_text.h" | 
| 10 #include "dbus/message.h" | 11 #include "dbus/message.h" | 
|  | 12 #include "dbus/values_util.h" | 
| 11 | 13 | 
| 12 namespace chromeos { | 14 namespace chromeos { | 
| 13 // TODO(nona): Remove ibus namespace after complete libibus removal. | 15 // TODO(nona): Remove ibus namespace after complete libibus removal. | 
| 14 namespace ibus { | 16 namespace ibus { | 
| 15 | 17 | 
| 16 /////////////////////////////////////////////////////////////////////////////// | 18 /////////////////////////////////////////////////////////////////////////////// | 
| 17 // IBusObjectReader | 19 // IBusObjectReader | 
| 18 IBusObjectReader::IBusObjectReader(const std::string& type_name, | 20 IBusObjectReader::IBusObjectReader(const std::string& type_name, | 
| 19                                    dbus::MessageReader* reader) | 21                                    dbus::MessageReader* reader) | 
| 20     : type_name_(type_name), | 22     : type_name_(type_name), | 
| 21       original_reader_(reader), | 23       original_reader_(reader), | 
| 22       top_variant_reader_(NULL), | 24       top_variant_reader_(NULL), | 
| 23       contents_reader_(NULL), | 25       contents_reader_(NULL), | 
| 24       check_result_(IBUS_OBJECT_NOT_CHECKED) { | 26       check_result_(IBUS_OBJECT_NOT_CHECKED) { | 
| 25 } | 27 } | 
| 26 | 28 | 
| 27 IBusObjectReader::~IBusObjectReader() { | 29 IBusObjectReader::~IBusObjectReader() { | 
|  | 30   for (std::map<std::string, base::Value*>::iterator ite = attachments_.begin(); | 
|  | 31        ite != attachments_.end(); ++ite) | 
|  | 32     delete ite->second; | 
| 28 } | 33 } | 
| 29 | 34 | 
| 30 bool IBusObjectReader::InitWithoutAttachment() { | 35 bool IBusObjectReader::Init() { | 
| 31   DCHECK(original_reader_); | 36   DCHECK(original_reader_); | 
| 32   DCHECK_EQ(IBUS_OBJECT_NOT_CHECKED, check_result_); | 37   DCHECK_EQ(IBUS_OBJECT_NOT_CHECKED, check_result_); | 
| 33 | 38 | 
| 34   top_variant_reader_.reset(new dbus::MessageReader(NULL)); | 39   top_variant_reader_.reset(new dbus::MessageReader(NULL)); | 
| 35   contents_reader_.reset(new dbus::MessageReader(NULL)); | 40   contents_reader_.reset(new dbus::MessageReader(NULL)); | 
| 36   check_result_ = IBUS_OBJECT_INVALID; | 41   check_result_ = IBUS_OBJECT_INVALID; | 
| 37 | 42 | 
| 38   // IBus object has a variant on top-level. | 43   // IBus object has a variant on top-level. | 
| 39   if (!original_reader_->PopVariant(top_variant_reader_.get())) { | 44   if (!original_reader_->PopVariant(top_variant_reader_.get())) { | 
| 40     LOG(ERROR) << "Invalid object structure[" << type_name_ << "]: " | 45     LOG(ERROR) << "Invalid object structure[" << type_name_ << "]: " | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
| 56                << "Can not get type name field."; | 61                << "Can not get type name field."; | 
| 57     return false; | 62     return false; | 
| 58   } | 63   } | 
| 59 | 64 | 
| 60   if (type_name != type_name_) { | 65   if (type_name != type_name_) { | 
| 61     LOG(ERROR) << "Type check failed: Given variant is not " << type_name_ | 66     LOG(ERROR) << "Type check failed: Given variant is not " << type_name_ | 
| 62                << " and actual type is " << type_name << "."; | 67                << " and actual type is " << type_name << "."; | 
| 63     return false; | 68     return false; | 
| 64   } | 69   } | 
| 65 | 70 | 
| 66   return true; | 71   dbus::MessageReader attachment_reader(NULL); | 
| 67 } |  | 
| 68 | 72 | 
| 69 bool IBusObjectReader::Init() { | 73   // IBus object has array object at the second element, which is used in | 
| 70   if (!InitWithoutAttachment()) | 74   // attaching additional information. | 
| 71     return false; | 75   if (!contents_reader_->PopArray(&attachment_reader)) { | 
| 72 |  | 
| 73   // Ignores attachment field. |  | 
| 74   dbus::MessageReader array_reader(NULL); |  | 
| 75   if (!contents_reader_->PopArray(&array_reader)) { |  | 
| 76     LOG(ERROR) << "Invalid object structure[" << type_name_ << "] " | 76     LOG(ERROR) << "Invalid object structure[" << type_name_ << "] " | 
| 77                << "can not find attachment array field."; | 77                << "can not find attachment array field."; | 
| 78     return false; | 78     return false; | 
| 79   } | 79   } | 
| 80   DLOG_IF(WARNING, array_reader.HasMoreData()) |  | 
| 81       << "Ignoring attachment field in " << type_name_ <<"."; |  | 
| 82   check_result_ = IBUS_OBJECT_VALID; |  | 
| 83   return true; |  | 
| 84 } |  | 
| 85 | 80 | 
| 86 bool IBusObjectReader::InitWithAttachmentReader(dbus::MessageReader* reader) { | 81   while (attachment_reader.HasMoreData()) { | 
| 87   DCHECK(reader); | 82     dbus::MessageReader dictionary_reader(NULL); | 
| 88   if (!InitWithoutAttachment()) | 83     if (!attachment_reader.PopDictEntry(&dictionary_reader)) { | 
| 89     return false; | 84       LOG(ERROR) << "Invalid attachment structure: " | 
|  | 85                  << "The attachment field is array of dictionary entry."; | 
|  | 86       return false; | 
|  | 87     } | 
| 90 | 88 | 
| 91   // IBus object has array object at the second element, which is used in | 89     std::string key; | 
| 92   // attaching additional information. | 90     if (!dictionary_reader.PopString(&key)) { | 
| 93   if (!contents_reader_->PopArray(reader)) { | 91       LOG(ERROR) << "Invalid attachement structure: " | 
| 94     LOG(ERROR) << "Invalid object structure[" << type_name_ << "] " | 92                  << "The 1st dictionary entry should be string."; | 
| 95                << "can not find attachment array field."; | 93       return false; | 
| 96     return false; | 94     } | 
|  | 95 | 
|  | 96     if (key.empty()) { | 
|  | 97       LOG(ERROR) << "Invalid attachement key: key is empty."; | 
|  | 98       return false; | 
|  | 99     } | 
|  | 100 | 
|  | 101     dbus::MessageReader variant_reader(NULL); | 
|  | 102     if (!dictionary_reader.PopVariant(&variant_reader)) { | 
|  | 103       LOG(ERROR) << "Invalid attachment structure: " | 
|  | 104                  << "The 2nd dictionary entry shuold be variant."; | 
|  | 105       return false; | 
|  | 106     } | 
|  | 107 | 
|  | 108     dbus::MessageReader sub_variant_reader(NULL); | 
|  | 109     if (!variant_reader.PopVariant(&sub_variant_reader)) { | 
|  | 110       LOG(ERROR) << "Invalid attachment structure: " | 
|  | 111                  << "The 2nd variant entry should contain variant."; | 
|  | 112       return false; | 
|  | 113     } | 
|  | 114 | 
|  | 115     attachments_[key] =  dbus::PopDataAsValue(&sub_variant_reader); | 
| 97   } | 116   } | 
| 98   check_result_ = IBUS_OBJECT_VALID; | 117   check_result_ = IBUS_OBJECT_VALID; | 
| 99   return true; | 118   return true; | 
| 100 } | 119 } | 
| 101 | 120 | 
| 102 bool IBusObjectReader::InitWithParentReader(dbus::MessageReader* reader) { | 121 bool IBusObjectReader::InitWithParentReader(dbus::MessageReader* reader) { | 
| 103   original_reader_ = reader; | 122   original_reader_ = reader; | 
| 104   return Init(); | 123   return Init(); | 
| 105 } | 124 } | 
| 106 | 125 | 
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 154                                                       property); | 173                                                       property); | 
| 155 } | 174 } | 
| 156 | 175 | 
| 157 bool IBusObjectReader::PopIBusPropertyList(IBusPropertyList* properties) { | 176 bool IBusObjectReader::PopIBusPropertyList(IBusPropertyList* properties) { | 
| 158   DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); | 177   DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); | 
| 159   DCHECK(contents_reader_.get()); | 178   DCHECK(contents_reader_.get()); | 
| 160   return IsValid() && chromeos::ibus::PopIBusPropertyList( | 179   return IsValid() && chromeos::ibus::PopIBusPropertyList( | 
| 161       contents_reader_.get(), properties); | 180       contents_reader_.get(), properties); | 
| 162 } | 181 } | 
| 163 | 182 | 
|  | 183 const base::Value* IBusObjectReader::GetAttachment(const std::string& key) { | 
|  | 184   DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); | 
|  | 185   DCHECK(contents_reader_.get()); | 
|  | 186   if (!IsValid()) | 
|  | 187     return NULL; | 
|  | 188   std::map<std::string, base::Value*>::iterator it = attachments_.find(key); | 
|  | 189   if (it == attachments_.end()) | 
|  | 190     return NULL; | 
|  | 191   return it->second; | 
|  | 192 } | 
|  | 193 | 
| 164 bool IBusObjectReader::HasMoreData() { | 194 bool IBusObjectReader::HasMoreData() { | 
| 165   DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); | 195   DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); | 
| 166   DCHECK(contents_reader_.get()); | 196   DCHECK(contents_reader_.get()); | 
| 167   return IsValid() && contents_reader_->HasMoreData(); | 197   return IsValid() && contents_reader_->HasMoreData(); | 
| 168 } | 198 } | 
| 169 | 199 | 
| 170 bool IBusObjectReader::PopIBusObject(IBusObjectReader* reader) { | 200 bool IBusObjectReader::PopIBusObject(IBusObjectReader* reader) { | 
| 171   DCHECK(contents_reader_.get()); | 201   DCHECK(contents_reader_.get()); | 
| 172   if (!IsValid()) | 202   if (!IsValid()) | 
| 173     return false; | 203     return false; | 
| 174   return reader->InitWithParentReader(contents_reader_.get()); | 204   return reader->InitWithParentReader(contents_reader_.get()); | 
| 175 } | 205 } | 
| 176 | 206 | 
| 177 bool IBusObjectReader::IsValid() const { | 207 bool IBusObjectReader::IsValid() const { | 
| 178   DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); | 208   DCHECK_NE(IBUS_OBJECT_NOT_CHECKED, check_result_); | 
| 179   return check_result_ == IBUS_OBJECT_VALID; | 209   return check_result_ == IBUS_OBJECT_VALID; | 
| 180 } | 210 } | 
| 181 | 211 | 
| 182 /////////////////////////////////////////////////////////////////////////////// | 212 /////////////////////////////////////////////////////////////////////////////// | 
| 183 //  IBusObjectWriter | 213 //  IBusObjectWriter | 
| 184 IBusObjectWriter::IBusObjectWriter(const std::string& type_name, | 214 IBusObjectWriter::IBusObjectWriter(const std::string& type_name, | 
| 185                                    const std::string& signature, | 215                                    const std::string& signature, | 
| 186                                    dbus::MessageWriter* writer) | 216                                    dbus::MessageWriter* writer) | 
| 187     : type_name_(type_name), | 217     : type_name_(type_name), | 
| 188       signature_(signature), | 218       signature_(signature), | 
| 189       original_writer_(writer) { | 219       original_writer_(writer), | 
|  | 220       state_(NOT_INITIALZED) { | 
| 190   if (original_writer_) | 221   if (original_writer_) | 
| 191     Init(); | 222     Init(); | 
| 192 } | 223 } | 
| 193 | 224 | 
| 194 IBusObjectWriter::~IBusObjectWriter() { | 225 IBusObjectWriter::~IBusObjectWriter() { | 
| 195 } | 226 } | 
| 196 | 227 | 
| 197 void IBusObjectWriter::AppendString(const std::string& input) { | 228 void IBusObjectWriter::AppendString(const std::string& input) { | 
| 198   DCHECK(IsInitialized()); | 229   DCHECK_EQ(state_, INITIALIZED); | 
| 199   contents_writer_->AppendString(input); | 230   contents_writer_->AppendString(input); | 
| 200 } | 231 } | 
| 201 | 232 | 
| 202 void IBusObjectWriter::AppendUint32(uint32 input) { | 233 void IBusObjectWriter::AppendUint32(uint32 input) { | 
| 203   DCHECK(IsInitialized()); | 234   DCHECK_EQ(state_, INITIALIZED); | 
| 204   contents_writer_->AppendUint32(input); | 235   contents_writer_->AppendUint32(input); | 
| 205 } | 236 } | 
| 206 | 237 | 
| 207 void IBusObjectWriter::AppendInt32(int32 input) { | 238 void IBusObjectWriter::AppendInt32(int32 input) { | 
| 208   DCHECK(IsInitialized()); | 239   DCHECK_EQ(state_, INITIALIZED); | 
| 209   contents_writer_->AppendInt32(input); | 240   contents_writer_->AppendInt32(input); | 
| 210 } | 241 } | 
| 211 | 242 | 
| 212 void IBusObjectWriter::AppendBool(bool input) { | 243 void IBusObjectWriter::AppendBool(bool input) { | 
| 213   DCHECK(IsInitialized()); | 244   DCHECK_EQ(state_, INITIALIZED); | 
| 214   contents_writer_->AppendBool(input); | 245   contents_writer_->AppendBool(input); | 
| 215 } | 246 } | 
| 216 | 247 | 
| 217 void IBusObjectWriter::OpenArray(const std::string& signature, | 248 void IBusObjectWriter::OpenArray(const std::string& signature, | 
| 218                                  dbus::MessageWriter* writer) { | 249                                  dbus::MessageWriter* writer) { | 
| 219   DCHECK(IsInitialized()); | 250   DCHECK_EQ(state_, INITIALIZED); | 
| 220   contents_writer_->OpenArray(signature, writer); | 251   contents_writer_->OpenArray(signature, writer); | 
| 221 } | 252 } | 
| 222 | 253 | 
| 223 void IBusObjectWriter::AppendIBusText(const IBusText& text) { | 254 void IBusObjectWriter::AppendIBusText(const IBusText& text) { | 
| 224   DCHECK(IsInitialized()); | 255   DCHECK_EQ(state_, INITIALIZED); | 
| 225   chromeos::ibus::AppendIBusText(text, contents_writer_.get()); | 256   chromeos::ibus::AppendIBusText(text, contents_writer_.get()); | 
| 226 } | 257 } | 
| 227 | 258 | 
| 228 void IBusObjectWriter::AppendStringAsIBusText(const std::string& text) { | 259 void IBusObjectWriter::AppendStringAsIBusText(const std::string& text) { | 
| 229   DCHECK(IsInitialized()); | 260   DCHECK_EQ(state_, INITIALIZED); | 
| 230   chromeos::ibus::AppendStringAsIBusText(text, contents_writer_.get()); | 261   chromeos::ibus::AppendStringAsIBusText(text, contents_writer_.get()); | 
| 231 } | 262 } | 
| 232 | 263 | 
| 233 void IBusObjectWriter::AppendIBusProperty(const IBusProperty& property) { | 264 void IBusObjectWriter::AppendIBusProperty(const IBusProperty& property) { | 
| 234   DCHECK(IsInitialized()); | 265   DCHECK_EQ(state_, INITIALIZED); | 
| 235   chromeos::ibus::AppendIBusProperty(property, contents_writer_.get()); | 266   chromeos::ibus::AppendIBusProperty(property, contents_writer_.get()); | 
| 236 } | 267 } | 
| 237 | 268 | 
| 238 void IBusObjectWriter::AppendIBusPropertyList( | 269 void IBusObjectWriter::AppendIBusPropertyList( | 
| 239     const IBusPropertyList& property_list) { | 270     const IBusPropertyList& property_list) { | 
| 240   DCHECK(IsInitialized()); | 271   DCHECK_EQ(state_, INITIALIZED); | 
| 241   chromeos::ibus::AppendIBusPropertyList(property_list, contents_writer_.get()); | 272   chromeos::ibus::AppendIBusPropertyList(property_list, contents_writer_.get()); | 
| 242 } | 273 } | 
| 243 | 274 | 
| 244 void IBusObjectWriter::CloseContainer(dbus::MessageWriter* writer) { | 275 void IBusObjectWriter::CloseContainer(dbus::MessageWriter* writer) { | 
| 245   DCHECK(IsInitialized()); | 276   DCHECK_EQ(state_, INITIALIZED); | 
| 246   contents_writer_->CloseContainer(writer); | 277   contents_writer_->CloseContainer(writer); | 
| 247 } | 278 } | 
| 248 | 279 | 
| 249 void IBusObjectWriter::AppendIBusObject(IBusObjectWriter* writer) { | 280 void IBusObjectWriter::AppendIBusObject(IBusObjectWriter* writer) { | 
| 250   DCHECK(IsInitialized()); | 281   DCHECK_EQ(state_, INITIALIZED); | 
| 251   DCHECK(!writer->IsInitialized()) << "Given writer is already initialized"; |  | 
| 252 |  | 
| 253   writer->InitWithParentWriter(contents_writer_.get()); | 282   writer->InitWithParentWriter(contents_writer_.get()); | 
| 254 } | 283 } | 
| 255 | 284 | 
| 256 void IBusObjectWriter::Init() { | 285 void IBusObjectWriter::Init() { | 
| 257   DCHECK(original_writer_); | 286   DCHECK(original_writer_); | 
| 258   DCHECK(!IsInitialized()); | 287   DCHECK_EQ(state_, NOT_INITIALZED); | 
| 259 | 288 | 
| 260   top_variant_writer_.reset(new dbus::MessageWriter(NULL)); | 289   top_variant_writer_.reset(new dbus::MessageWriter(NULL)); | 
| 261   contents_writer_.reset(new dbus::MessageWriter(NULL)); | 290   contents_writer_.reset(new dbus::MessageWriter(NULL)); | 
|  | 291   attachment_writer_.reset(new dbus::MessageWriter(NULL)); | 
| 262 | 292 | 
| 263   const std::string ibus_signature = "(sa{sv}" + signature_ + ")"; | 293   const std::string ibus_signature = "(sa{sv}" + signature_ + ")"; | 
| 264   original_writer_->OpenVariant(ibus_signature, top_variant_writer_.get()); | 294   original_writer_->OpenVariant(ibus_signature, top_variant_writer_.get()); | 
| 265   top_variant_writer_->OpenStruct(contents_writer_.get()); | 295   top_variant_writer_->OpenStruct(contents_writer_.get()); | 
| 266 | 296 | 
| 267   contents_writer_->AppendString(type_name_); | 297   contents_writer_->AppendString(type_name_); | 
| 268   dbus::MessageWriter header_array_writer(NULL); |  | 
| 269 | 298 | 
| 270   // There is no case setting any attachment in ChromeOS, so setting empty | 299   contents_writer_->OpenArray("{sv}", attachment_writer_.get()); | 
| 271   // array is enough. | 300   state_ = HEADER_OPEN; | 
| 272   contents_writer_->OpenArray("{sv}", &header_array_writer); |  | 
| 273   contents_writer_->CloseContainer(&header_array_writer); |  | 
| 274 } | 301 } | 
| 275 | 302 | 
| 276 void IBusObjectWriter::InitWithParentWriter(dbus::MessageWriter* writer) { | 303 void IBusObjectWriter::InitWithParentWriter(dbus::MessageWriter* writer) { | 
|  | 304   DCHECK_EQ(state_, NOT_INITIALZED) << "Already initialized."; | 
| 277   original_writer_ = writer; | 305   original_writer_ = writer; | 
| 278   Init(); | 306   Init(); | 
| 279 } | 307 } | 
| 280 | 308 | 
| 281 void IBusObjectWriter::CloseAll() { | 309 void IBusObjectWriter::CloseAll() { | 
| 282   DCHECK(original_writer_); | 310   DCHECK(original_writer_); | 
| 283   DCHECK(IsInitialized()); | 311   DCHECK_NE(state_, NOT_INITIALZED); | 
|  | 312   if (state_ == HEADER_OPEN) | 
|  | 313     CloseHeader(); | 
| 284 | 314 | 
| 285   top_variant_writer_->CloseContainer(contents_writer_.get()); | 315   top_variant_writer_->CloseContainer(contents_writer_.get()); | 
| 286   original_writer_->CloseContainer(top_variant_writer_.get()); | 316   original_writer_->CloseContainer(top_variant_writer_.get()); | 
| 287   top_variant_writer_.reset(); | 317   top_variant_writer_.reset(); | 
| 288   contents_writer_.reset(); | 318   contents_writer_.reset(); | 
| 289 } | 319 } | 
| 290 | 320 | 
| 291 bool IBusObjectWriter::IsInitialized() const { | 321 void IBusObjectWriter::CloseHeader() { | 
| 292   return contents_writer_.get() != NULL; | 322   DCHECK_EQ(state_, HEADER_OPEN) << "Header is already closed."; | 
|  | 323   contents_writer_->CloseContainer(attachment_writer_.get()); | 
|  | 324   state_ = INITIALIZED; | 
|  | 325 } | 
|  | 326 | 
|  | 327 bool IBusObjectWriter::AddAttachment(const std::string& key, | 
|  | 328                                      const base::Value& value) { | 
|  | 329   DCHECK_NE(state_, NOT_INITIALZED) << "Do not call before Init();"; | 
|  | 330   DCHECK_NE(state_, INITIALIZED) << "Do not call after CloseHeader()."; | 
|  | 331   DCHECK(attachment_writer_.get()); | 
|  | 332   DCHECK(!key.empty()); | 
|  | 333   DCHECK(!value.IsType(base::Value::TYPE_NULL)); | 
|  | 334 | 
|  | 335   dbus::MessageWriter dict_writer(NULL); | 
|  | 336   attachment_writer_->OpenDictEntry(&dict_writer); | 
|  | 337   dict_writer.AppendString(key); | 
|  | 338   dbus::MessageWriter variant_writer(NULL); | 
|  | 339   dict_writer.OpenVariant("v", &variant_writer); | 
|  | 340 | 
|  | 341   dbus::AppendBasicTypeValueDataAsVariant(&variant_writer, value); | 
|  | 342   dict_writer.CloseContainer(&variant_writer); | 
|  | 343   attachment_writer_->CloseContainer(&variant_writer); | 
|  | 344   return true; | 
| 293 } | 345 } | 
| 294 | 346 | 
| 295 }  // namespace ibus | 347 }  // namespace ibus | 
| 296 }  // namespace chromeos | 348 }  // namespace chromeos | 
| OLD | NEW | 
|---|