| 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 #ifndef CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | 5 #ifndef CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | 
| 6 #define CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | 6 #define CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | 
| 7 | 7 | 
|  | 8 #include <map> | 
| 8 #include <string> | 9 #include <string> | 
|  | 10 | 
| 9 #include "base/basictypes.h" | 11 #include "base/basictypes.h" | 
| 10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" | 
| 11 #include "chromeos/chromeos_export.h" | 13 #include "chromeos/chromeos_export.h" | 
| 12 #include "chromeos/dbus/ibus/ibus_property.h" | 14 #include "chromeos/dbus/ibus/ibus_property.h" | 
| 13 #include "chromeos/dbus/ibus/ibus_text.h" | 15 #include "chromeos/dbus/ibus/ibus_text.h" | 
| 14 | 16 | 
|  | 17 namespace base { | 
|  | 18 class Value; | 
|  | 19 }  // namespace base | 
|  | 20 | 
| 15 namespace dbus { | 21 namespace dbus { | 
| 16 class MessageReader; | 22 class MessageReader; | 
| 17 class MessageWriter; | 23 class MessageWriter; | 
| 18 }  // dbus | 24 }  // namespace dbus | 
| 19 | 25 | 
| 20 namespace chromeos { | 26 namespace chromeos { | 
| 21 // TODO(nona): Remove ibus namespace after complete libibus removal. | 27 // TODO(nona): Remove ibus namespace after complete libibus removal. | 
| 22 namespace ibus { | 28 namespace ibus { | 
| 23 | 29 | 
| 24 // The data structure of IBusObject is represented as variant in "(sav...)" | 30 // The data structure of IBusObject is represented as variant in "(sav...)" | 
| 25 // signatur. The IBusObject is constructed with two sections, header and | 31 // signatur. The IBusObject is constructed with two sections, header and | 
| 26 // contents. The header section is represent as "sav" which contains type name | 32 // contents. The header section is represent as "sav" which contains type name | 
| 27 // and attachment array. The contents section is corresponding to "..." in | 33 // and attachment array. The contents section is corresponding to "..." in | 
| 28 // above signature, which can store arbitary type values including IBusObject. | 34 // above signature, which can store arbitary type values including IBusObject. | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 68 //   } | 74 //   } | 
| 69 // | 75 // | 
| 70 // The IBusObjectReader class provides reading IBusObject including attachment | 76 // The IBusObjectReader class provides reading IBusObject including attachment | 
| 71 // field from dbus message. This class checks the IBusObject header structure | 77 // field from dbus message. This class checks the IBusObject header structure | 
| 72 // and type name before reading contents. | 78 // and type name before reading contents. | 
| 73 // | 79 // | 
| 74 // EXAPMLE USAGE: | 80 // EXAPMLE USAGE: | 
| 75 //   // Craetes reader for IBusText | 81 //   // Craetes reader for IBusText | 
| 76 //   IBusObjectReader object_reader("IBusText", &reader); | 82 //   IBusObjectReader object_reader("IBusText", &reader); | 
| 77 // | 83 // | 
|  | 84 //   // Initialize for reading attachment field. | 
|  | 85 //   object_reader.Init(); | 
|  | 86 // | 
|  | 87 //   // Get attachment field. | 
|  | 88 //   base::Value* value = object_reader.GetAttachment("annotation"); | 
|  | 89 // | 
| 78 //   std::string text; | 90 //   std::string text; | 
| 79 //   reader.PopString(&text);  // Reading 1st value as string. | 91 //   reader.PopString(&text);  // Reading 1st value as string. | 
| 80 // | 92 // | 
| 81 //   // We can also read nested IBusObject. | 93 //   // We can also read nested IBusObject. | 
| 82 //   IBusObjectReader nested_object_reader("IBusAttrList", NULL); | 94 //   IBusObjectReader nested_object_reader("IBusAttrList", NULL); | 
| 83 //   reader.PopIBusObject(&nested_object_reader); | 95 //   reader.PopIBusObject(&nested_object_reader); | 
| 84 class CHROMEOS_EXPORT IBusObjectReader { | 96 class CHROMEOS_EXPORT IBusObjectReader { | 
| 85  public: | 97  public: | 
| 86   // |reader| must be released by caller. | 98   // |reader| must be released by caller. | 
| 87   IBusObjectReader(const std::string& type_name, | 99   IBusObjectReader(const std::string& type_name, | 
| 88                    dbus::MessageReader* reader); | 100                    dbus::MessageReader* reader); | 
| 89   virtual ~IBusObjectReader(); | 101   virtual ~IBusObjectReader(); | 
| 90 | 102 | 
| 91   // Reads IBusObject headers and checks if the type name is valid. | 103   // Reads IBusObject headers and checks if the type name is valid. | 
| 92   // Returns true on success. Uses InitWitAttachmentReader instead if you want | 104   // Returns true on success. Uses InitWitAttachmentReader instead if you want | 
| 93   // to read attachment field. | 105   // to read attachment field. | 
| 94   bool Init(); | 106   bool Init(); | 
| 95 | 107 | 
| 96   // Reads IBusObject headers and checks if the type name is valid. |  | 
| 97   // Returns true and sets up reader for attachment reading on success. |  | 
| 98   bool InitWithAttachmentReader(dbus::MessageReader* reader); |  | 
| 99 |  | 
| 100   // Reads IBusOBject with |reader| and checks if the type name is valid. | 108   // Reads IBusOBject with |reader| and checks if the type name is valid. | 
| 101   bool InitWithParentReader(dbus::MessageReader* reader); | 109   bool InitWithParentReader(dbus::MessageReader* reader); | 
| 102 | 110 | 
| 103   // Returns true if the IBusObject is valid. | 111   // Returns true if the IBusObject is valid. | 
| 104   bool IsValid() const; | 112   bool IsValid() const; | 
| 105 | 113 | 
| 106   // The following functions delegate dbus::MessageReader's functions. | 114   // The following functions delegate dbus::MessageReader's functions. | 
| 107   bool PopString(std::string* out); | 115   bool PopString(std::string* out); | 
| 108   bool PopUint32(uint32* out); | 116   bool PopUint32(uint32* out); | 
| 109   bool PopArray(dbus::MessageReader* reader); | 117   bool PopArray(dbus::MessageReader* reader); | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 122   // instead in the case of using any attribute entries in IBusText. | 130   // instead in the case of using any attribute entries in IBusText. | 
| 123   // Return true on success. | 131   // Return true on success. | 
| 124   bool PopStringFromIBusText(std::string* text); | 132   bool PopStringFromIBusText(std::string* text); | 
| 125 | 133 | 
| 126   // Pops a IBusProperty. | 134   // Pops a IBusProperty. | 
| 127   bool PopIBusProperty(ibus::IBusProperty* property); | 135   bool PopIBusProperty(ibus::IBusProperty* property); | 
| 128 | 136 | 
| 129   // Pops a IBusPropertyList. | 137   // Pops a IBusPropertyList. | 
| 130   bool PopIBusPropertyList(ibus::IBusPropertyList* property_list); | 138   bool PopIBusPropertyList(ibus::IBusPropertyList* property_list); | 
| 131 | 139 | 
|  | 140   // Gets attachment entry corresponding to |key|. Do not free returned value. | 
|  | 141   // Returns NULL if there is no entry. | 
|  | 142   const base::Value* GetAttachment(const std::string& key); | 
|  | 143 | 
| 132  private: | 144  private: | 
| 133   enum CheckResult { | 145   enum CheckResult { | 
| 134     IBUS_OBJECT_VALID,  // Already checked and valid type. | 146     IBUS_OBJECT_VALID,  // Already checked and valid type. | 
| 135     IBUS_OBJECT_INVALID,  // Already checked but invalid type. | 147     IBUS_OBJECT_INVALID,  // Already checked but invalid type. | 
| 136     IBUS_OBJECT_NOT_CHECKED,  // Not checked yet. | 148     IBUS_OBJECT_NOT_CHECKED,  // Not checked yet. | 
| 137   }; | 149   }; | 
| 138 | 150 | 
| 139   // Reads IBusObject headers without attachment and checks if the type name |  | 
| 140   // is valid. |  | 
| 141   bool InitWithoutAttachment(); |  | 
| 142 |  | 
| 143   std::string type_name_; | 151   std::string type_name_; | 
| 144   dbus::MessageReader* original_reader_; | 152   dbus::MessageReader* original_reader_; | 
| 145   scoped_ptr<dbus::MessageReader> top_variant_reader_; | 153   scoped_ptr<dbus::MessageReader> top_variant_reader_; | 
| 146   scoped_ptr<dbus::MessageReader> contents_reader_; | 154   scoped_ptr<dbus::MessageReader> contents_reader_; | 
| 147   CheckResult check_result_; | 155   CheckResult check_result_; | 
|  | 156   std::map<std::string, base::Value*> attachments_; | 
| 148 | 157 | 
| 149   DISALLOW_COPY_AND_ASSIGN(IBusObjectReader); | 158   DISALLOW_COPY_AND_ASSIGN(IBusObjectReader); | 
| 150 }; | 159 }; | 
| 151 | 160 | 
| 152 // IBusObjectWriter class provides writing IBusObject to dbus message. This | 161 // IBusObjectWriter class provides writing IBusObject to dbus message. This | 
| 153 // class appends header section before appending contents values. | 162 // class appends header section before appending contents values. | 
| 154 // IBusObjectWriter does not support writing attachment field because writing | 163 // IBusObjectWriter does not support writing attachment field because writing | 
| 155 // attachment field is not used in Chrome. | 164 // attachment field is not used in Chrome. | 
| 156 // | 165 // | 
| 157 // EXAMPLE USAGE: | 166 // EXAMPLE USAGE: | 
| 158 //   // Creates writer for IBusText | 167 //   // Creates writer for IBusText | 
| 159 //   IBusObjectWriter object_writer("IBusText", "sv", &writer); | 168 //   IBusObjectWriter object_writer("IBusText", "sv", &writer); | 
| 160 // | 169 // | 
|  | 170 //   // Add some attachments. | 
|  | 171 //   base::Value* value = base::Value::CreateStringValue("Noun"); | 
|  | 172 //   object_writer.AddAttachment("annotation", *value); | 
|  | 173 // | 
|  | 174 //   // Close header section. | 
|  | 175 //   object_writer.CloseHeader(); | 
|  | 176 // | 
| 161 //   const std::string text = "Sample Text"; | 177 //   const std::string text = "Sample Text"; | 
| 162 //   writer.AppendString(text); | 178 //   writer.AppendString(text); | 
| 163 // | 179 // | 
| 164 //   // We can also write nested IBusObject. | 180 //   // We can also write nested IBusObject. | 
| 165 //   IBusObjectWriter nested_object_writer("IBusAttrList", "av"); | 181 //   IBusObjectWriter nested_object_writer("IBusAttrList", "av"); | 
| 166 //   object_writer.AppendIBusObject(&nested_object_writer); | 182 //   object_writer.AppendIBusObject(&nested_object_writer); | 
| 167 //   ... appends values | 183 //   ... appends values | 
| 168 // | 184 // | 
| 169 //   nested_object_writer.CloseAll();  // To finish up, should call CloseAll. | 185 //   nested_object_writer.CloseAll();  // To finish up, should call CloseAll. | 
| 170 //   object_writer.CloseAll(); | 186 //   object_writer.CloseAll(); | 
| 171 class CHROMEOS_EXPORT IBusObjectWriter { | 187 class CHROMEOS_EXPORT IBusObjectWriter { | 
| 172  public: | 188  public: | 
|  | 189   enum WriterState { | 
|  | 190     NOT_INITIALZED,  // Created but not initialized. | 
|  | 191     HEADER_OPEN,  // Ready for writing attachment field. | 
|  | 192     INITIALIZED  // Ready for writing content values. | 
|  | 193   }; | 
|  | 194 | 
| 173   // |writer| must be released by caller. | 195   // |writer| must be released by caller. | 
| 174   IBusObjectWriter(const std::string& type_name, | 196   IBusObjectWriter(const std::string& type_name, | 
| 175                    const std::string& signature, | 197                    const std::string& signature, | 
| 176                    dbus::MessageWriter* writer); | 198                    dbus::MessageWriter* writer); | 
| 177   virtual ~IBusObjectWriter(); | 199   virtual ~IBusObjectWriter(); | 
| 178 | 200 | 
|  | 201   // Closes header to write content values. | 
|  | 202   void CloseHeader(); | 
|  | 203 | 
| 179   // Appends IBusObject headers with |writer|, should be called once. | 204   // Appends IBusObject headers with |writer|, should be called once. | 
| 180   void InitWithParentWriter(dbus::MessageWriter* writer); | 205   void InitWithParentWriter(dbus::MessageWriter* writer); | 
| 181 | 206 | 
|  | 207   // Adds an attachment, this function can be called only before CloseHeader | 
|  | 208   // function call. | 
|  | 209   bool AddAttachment(const std::string& key, const base::Value& value); | 
|  | 210 | 
| 182   // The following functions delegate dbus::MessageReader's functions. | 211   // The following functions delegate dbus::MessageReader's functions. | 
| 183   void AppendString(const std::string& input); | 212   void AppendString(const std::string& input); | 
| 184   void AppendUint32(uint32 value); | 213   void AppendUint32(uint32 value); | 
| 185   void AppendInt32(int32 value); | 214   void AppendInt32(int32 value); | 
| 186   void AppendBool(bool value); | 215   void AppendBool(bool value); | 
| 187   void OpenArray(const std::string& signature, | 216   void OpenArray(const std::string& signature, | 
| 188                  dbus::MessageWriter* writer); | 217                  dbus::MessageWriter* writer); | 
| 189   void CloseContainer(dbus::MessageWriter* writer); | 218   void CloseContainer(dbus::MessageWriter* writer); | 
| 190 | 219 | 
| 191   // Sets up |writer| for writing new IBusObject entry. | 220   // Sets up |writer| for writing new IBusObject entry. | 
| 192   void AppendIBusObject(IBusObjectWriter* writer); | 221   void AppendIBusObject(IBusObjectWriter* writer); | 
| 193 | 222 | 
| 194   // Closes all opened containers. | 223   // Closes all opened containers. | 
| 195   void CloseAll(); | 224   void CloseAll(); | 
| 196 | 225 | 
| 197   // Returns true if writer is initialized. |  | 
| 198   bool IsInitialized() const; |  | 
| 199 |  | 
| 200   // Appends a IBusText. | 226   // Appends a IBusText. | 
| 201   void AppendIBusText(const ibus::IBusText& text); | 227   void AppendIBusText(const ibus::IBusText& text); | 
| 202 | 228 | 
| 203   // Appends a string as IBusText without any attributes. Use AppendIBusText | 229   // Appends a string as IBusText without any attributes. Use AppendIBusText | 
| 204   // instead in the case of using any attribute entries. | 230   // instead in the case of using any attribute entries. | 
| 205   void AppendStringAsIBusText(const std::string& text); | 231   void AppendStringAsIBusText(const std::string& text); | 
| 206 | 232 | 
| 207   // Appends a IBusProperty. | 233   // Appends a IBusProperty. | 
| 208   void AppendIBusProperty(const ibus::IBusProperty& property); | 234   void AppendIBusProperty(const ibus::IBusProperty& property); | 
| 209 | 235 | 
| 210   // Appends a IBusPropertyList. | 236   // Appends a IBusPropertyList. | 
| 211   void AppendIBusPropertyList(const ibus::IBusPropertyList& property_list); | 237   void AppendIBusPropertyList(const ibus::IBusPropertyList& property_list); | 
| 212 | 238 | 
| 213  private: | 239  private: | 
| 214   // Appends IBusObject headers, should be called once. | 240   // Appends IBusObject headers, should be called once. | 
| 215   void Init(); | 241   void Init(); | 
| 216 | 242 | 
| 217   std::string type_name_; | 243   std::string type_name_; | 
| 218   std::string signature_; | 244   std::string signature_; | 
| 219   dbus::MessageWriter* original_writer_; | 245   dbus::MessageWriter* original_writer_; | 
|  | 246   WriterState state_; | 
| 220   scoped_ptr<dbus::MessageWriter> top_variant_writer_; | 247   scoped_ptr<dbus::MessageWriter> top_variant_writer_; | 
| 221   scoped_ptr<dbus::MessageWriter> contents_writer_; | 248   scoped_ptr<dbus::MessageWriter> contents_writer_; | 
|  | 249   scoped_ptr<dbus::MessageWriter> attachment_writer_; | 
| 222 | 250 | 
| 223   DISALLOW_COPY_AND_ASSIGN(IBusObjectWriter); | 251   DISALLOW_COPY_AND_ASSIGN(IBusObjectWriter); | 
| 224 }; | 252 }; | 
| 225 | 253 | 
| 226 }  // namespace ibus | 254 }  // namespace ibus | 
| 227 }  // namespace chromeos | 255 }  // namespace chromeos | 
| 228 | 256 | 
| 229 #endif  // CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | 257 #endif  // CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | 
| OLD | NEW | 
|---|