OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | |
6 #define CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "chromeos/chromeos_export.h" | |
14 #include "chromeos/dbus/ibus/ibus_text.h" | |
15 | |
16 namespace base { | |
17 class Value; | |
18 } // namespace base | |
19 | |
20 namespace dbus { | |
21 class MessageReader; | |
22 class MessageWriter; | |
23 } // namespace dbus | |
24 | |
25 namespace chromeos { | |
26 | |
27 // The data structure of IBusObject is represented as variant in "(sav...)" | |
28 // signature. The IBusObject is constructed with two sections, header and | |
29 // contents. The header section is represent as "sav" which contains type name | |
30 // and attachment array. The contents section is corresponding to "..." in | |
31 // above signature, which can store arbitrary type values including IBusObject. | |
32 // | |
33 // DATA STRUCTURE OVERVIEW: | |
34 // | |
35 // variant // Handle with top_variant_writer_/top_variant_reader_. | |
36 // struct { // Handle with contents_writer_/contents_reader_. | |
37 // // Header section | |
38 // string typename // The type name of object, like "IBusText" | |
39 // array [ // attachment array. | |
40 // dict_entry ( | |
41 // string "mozc.candidates" // The key in the dictionary entry. | |
42 // variant ... // The value in the dictionary entry. | |
43 // ) | |
44 // ... | |
45 // ] | |
46 // | |
47 // // Contents section | |
48 // ... // The contents area. | |
49 // } | |
50 // | |
51 // EXAMPLE: IBusText | |
52 // | |
53 // variant struct { | |
54 // string "IBusText" // Header of IBusText | |
55 // array[] | |
56 // string "A" // The 1st value of IBusText | |
57 // variant struct { // THe 2nd value of IBusText | |
58 // string "IBusAttrList" // Header of IBusAttrList | |
59 // array[] | |
60 // array[ // The 1st value of IBusAttrList | |
61 // variant struct{ | |
62 // string "IBusAttribute" // Header of IBusAttribute | |
63 // array[] | |
64 // uint32 1 // The 1st value of IBusAttribute | |
65 // uint32 1 // The 2nd value of IBusAttribute | |
66 // uint32 0 // The 3rd value of IBusAttribute | |
67 // uint32 1 // The 4th value of IBusAttribute | |
68 // } | |
69 // ] | |
70 // } | |
71 // } | |
72 // | |
73 // The IBusObjectReader class provides reading IBusObject including attachment | |
74 // field from dbus message. This class checks the IBusObject header structure | |
75 // and type name before reading contents. | |
76 // | |
77 // EXAPMLE USAGE: | |
78 // // Creates reader for IBusText | |
79 // IBusObjectReader object_reader("IBusText", &reader); | |
80 // | |
81 // // Initialize for reading attachment field. | |
82 // object_reader.Init(); | |
83 // | |
84 // // Get attachment field. | |
85 // base::Value* value = object_reader.GetAttachment("annotation"); | |
86 // | |
87 // std::string text; | |
88 // reader.PopString(&text); // Reading 1st value as string. | |
89 // | |
90 // // We can also read nested IBusObject. | |
91 // IBusObjectReader nested_object_reader("IBusAttrList", NULL); | |
92 // reader.PopIBusObject(&nested_object_reader); | |
93 class CHROMEOS_EXPORT IBusObjectReader { | |
94 public: | |
95 // |reader| must be released by caller. | |
96 IBusObjectReader(const std::string& type_name, | |
97 dbus::MessageReader* reader); | |
98 virtual ~IBusObjectReader(); | |
99 | |
100 // Reads IBusObject headers and checks if the type name is valid. | |
101 // Returns true on success. Uses InitWitAttachmentReader instead if you want | |
102 // to read attachment field. | |
103 bool Init(); | |
104 | |
105 // Reads IBusOBject with |reader| and checks if the type name is valid. | |
106 bool InitWithParentReader(dbus::MessageReader* reader); | |
107 | |
108 // Returns true if the IBusObject is valid. | |
109 bool IsValid() const; | |
110 | |
111 // The following functions delegate dbus::MessageReader's functions. | |
112 bool PopString(std::string* out); | |
113 bool PopUint32(uint32* out); | |
114 bool PopArray(dbus::MessageReader* reader); | |
115 bool PopBool(bool* out); | |
116 bool PopInt32(int32* out); | |
117 bool HasMoreData(); | |
118 | |
119 // Sets up |reader| for reading an IBusObject entry. | |
120 bool PopIBusObject(IBusObjectReader* reader); | |
121 | |
122 // Pops a IBusText. | |
123 // Returns true on success. | |
124 bool PopIBusText(IBusText* text); | |
125 | |
126 // Pops a IBusText and store it's text field into |text|. Use PopIBusText | |
127 // instead in the case of using any attribute entries in IBusText. | |
128 // Return true on success. | |
129 bool PopStringFromIBusText(std::string* text); | |
130 | |
131 // Gets attachment entry corresponding to |key|. Do not free returned value. | |
132 // Returns NULL if there is no entry. | |
133 const base::Value* GetAttachment(const std::string& key); | |
134 | |
135 private: | |
136 enum CheckResult { | |
137 IBUS_OBJECT_VALID, // Already checked and valid type. | |
138 IBUS_OBJECT_INVALID, // Already checked but invalid type. | |
139 IBUS_OBJECT_NOT_CHECKED, // Not checked yet. | |
140 }; | |
141 | |
142 std::string type_name_; | |
143 dbus::MessageReader* original_reader_; | |
144 scoped_ptr<dbus::MessageReader> top_variant_reader_; | |
145 scoped_ptr<dbus::MessageReader> contents_reader_; | |
146 CheckResult check_result_; | |
147 std::map<std::string, base::Value*> attachments_; | |
148 | |
149 DISALLOW_COPY_AND_ASSIGN(IBusObjectReader); | |
150 }; | |
151 | |
152 // IBusObjectWriter class provides writing IBusObject to dbus message. This | |
153 // class appends header section before appending contents values. | |
154 // IBusObjectWriter does not support writing attachment field because writing | |
155 // attachment field is not used in Chrome. | |
156 // | |
157 // EXAMPLE USAGE: | |
158 // // Creates writer for IBusText | |
159 // IBusObjectWriter object_writer("IBusText", "sv", &writer); | |
160 // | |
161 // // Add some attachments. | |
162 // base::Value* value = base::Value::CreateStringValue("Noun"); | |
163 // object_writer.AddAttachment("annotation", *value); | |
164 // | |
165 // // Close header section. | |
166 // object_writer.CloseHeader(); | |
167 // | |
168 // const std::string text = "Sample Text"; | |
169 // writer.AppendString(text); | |
170 // | |
171 // // We can also write nested IBusObject. | |
172 // IBusObjectWriter nested_object_writer("IBusAttrList", "av"); | |
173 // object_writer.AppendIBusObject(&nested_object_writer); | |
174 // ... appends values | |
175 // | |
176 // nested_object_writer.CloseAll(); // To finish up, should call CloseAll. | |
177 // object_writer.CloseAll(); | |
178 class CHROMEOS_EXPORT IBusObjectWriter { | |
179 public: | |
180 enum WriterState { | |
181 NOT_INITIALZED, // Created but not initialized. | |
182 HEADER_OPEN, // Ready for writing attachment field. | |
183 INITIALIZED // Ready for writing content values. | |
184 }; | |
185 | |
186 // |writer| must be released by caller. | |
187 IBusObjectWriter(const std::string& type_name, | |
188 const std::string& signature, | |
189 dbus::MessageWriter* writer); | |
190 virtual ~IBusObjectWriter(); | |
191 | |
192 // Closes header to write content values. | |
193 void CloseHeader(); | |
194 | |
195 // Appends IBusObject headers with |writer|, should be called once. | |
196 void InitWithParentWriter(dbus::MessageWriter* writer); | |
197 | |
198 // Adds an attachment, this function can be called only before CloseHeader | |
199 // function call. | |
200 bool AddAttachment(const std::string& key, const base::Value& value); | |
201 | |
202 // The following functions delegate dbus::MessageReader's functions. | |
203 void AppendString(const std::string& input); | |
204 void AppendUint32(uint32 value); | |
205 void AppendInt32(int32 value); | |
206 void AppendBool(bool value); | |
207 void OpenArray(const std::string& signature, | |
208 dbus::MessageWriter* writer); | |
209 void CloseContainer(dbus::MessageWriter* writer); | |
210 | |
211 // Sets up |writer| for writing new IBusObject entry. | |
212 void AppendIBusObject(IBusObjectWriter* writer); | |
213 | |
214 // Closes all opened containers. | |
215 void CloseAll(); | |
216 | |
217 // Appends a IBusText. | |
218 void AppendIBusText(const IBusText& text); | |
219 | |
220 // Appends a string as IBusText without any attributes. Use AppendIBusText | |
221 // instead in the case of using any attribute entries. | |
222 void AppendStringAsIBusText(const std::string& text); | |
223 | |
224 private: | |
225 // Appends IBusObject headers, should be called once. | |
226 void Init(); | |
227 | |
228 std::string type_name_; | |
229 std::string signature_; | |
230 dbus::MessageWriter* original_writer_; | |
231 WriterState state_; | |
232 scoped_ptr<dbus::MessageWriter> top_variant_writer_; | |
233 scoped_ptr<dbus::MessageWriter> contents_writer_; | |
234 scoped_ptr<dbus::MessageWriter> attachment_writer_; | |
235 | |
236 DISALLOW_COPY_AND_ASSIGN(IBusObjectWriter); | |
237 }; | |
238 | |
239 } // namespace chromeos | |
240 | |
241 #endif // CHROMEOS_DBUS_IBUS_IBUS_OBJECT_H_ | |
OLD | NEW |