Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(488)

Side by Side Diff: dbus/message.h

Issue 7492029: Implement classes used for manipulating D-Bus messages. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: improve ToString() Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « dbus/dbus.gyp ('k') | dbus/message.cc » ('j') | dbus/message.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 DBUS_MESSAGE_H_
6 #define DBUS_MESSAGE_H_
7 #pragma once
8
9 #include <string>
10 #include <vector>
11 #include <dbus/dbus.h>
12
13 #include "base/basictypes.h"
14
15 namespace dbus {
16
17 class MessageWriter;
18 class MessageReader;
19
20 // Message is the base class of D-Bus message types. Client code should
21 // usually use sub classes such as MethodCall and Response instead.
22 //
23 // The class name Message is very generic, but there should be no problem
24 // as the class is inside 'dbus' namespace. We chose to name this way, as
25 // libdbus defines lots of types starting with DBus, such as
26 // DBusMessage. We should avoid confusion and conflict with these types.
27 class Message {
28 public:
29 // The message type used in D-Bus. Redefined here so client code
30 // doesn't need to use raw D-Bus macros. DBUS_MESSAGE_TYPE_INVALID
31 // etc. are #define macros. Having an enum type here makes code a bit
32 // more type-safe:
33 //
34 // MessageType GetMessageType() vs. int GetMessageType()
35 //
36 // A new message type may be introduced in the future, but it should not
37 // affect our code, as long as we are only using MessageType members
38 // (i.e. don't try to cast random integers to this enum).
39 enum MessageType {
40 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID,
41 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL,
42 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN,
43 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL,
44 MESSAGE_ERROR = DBUS_MESSAGE_TYPE_ERROR,
45 };
46
47 // The data type used in the D-Bus type system. See the comment at
48 // MessageType for why we are redefined data types here.
49 enum DataType {
50 INVALID_DATA = DBUS_TYPE_INVALID,
51 BYTE = DBUS_TYPE_BYTE,
52 BOOL = DBUS_TYPE_BOOLEAN,
53 INT16 = DBUS_TYPE_INT16,
54 UINT16 = DBUS_TYPE_UINT16,
55 INT32 = DBUS_TYPE_INT32,
56 UINT32 = DBUS_TYPE_UINT32,
57 INT64 = DBUS_TYPE_INT64,
58 UINT64 = DBUS_TYPE_UINT64,
59 DOUBLE = DBUS_TYPE_DOUBLE,
60 STRING = DBUS_TYPE_STRING,
61 OBJECT_PATH = DBUS_TYPE_OBJECT_PATH,
62 ARRAY = DBUS_TYPE_ARRAY,
63 STRUCT = DBUS_TYPE_STRUCT,
64 DICT_ENTRY = DBUS_TYPE_DICT_ENTRY,
65 VARIANT = DBUS_TYPE_VARIANT,
66 };
67
68 // Creates a Message. The internal raw message is NULL until it's set
69 // from outside by reset_raw_message().
70 Message();
71 virtual ~Message();
72
73 // Returns the type of the message. Returns MESSAGE_INVALID if
74 // raw_message_ is NULL.
75 MessageType GetMessageType();
76
77 DBusMessage* raw_message() { return raw_message_; }
78
79 // Resets raw_message_ with the given raw message. Takes the ownership
80 // of raw_message. raw_message_ will be unref'ed in the destructor.
81 void reset_raw_message(DBusMessage* raw_message);
82
83 // Returns the string representation of this message. Useful for
84 // debugging. The returned string consists of message headers such as
85 // destination if any, followed by a blank line, and the message
86 // payload. For example, a MethodCall's ToString() will look like:
87 //
88 // destination: com.example.Service
89 // path: /com/example/Object
90 // interface: com.example.Interface
91 // member: SomeMethod
92 //
93 // string \"payload\"
94 // ...
95 std::string ToString();
96
97 private:
98 // Helper function used in ToString().
99 std::string ToStringInternal(const std::string& indent,
100 MessageReader* reader);
101
102 DBusMessage* raw_message_;
103 DISALLOW_COPY_AND_ASSIGN(Message);
104 };
105
106 // MessageCall is a type of message used for calling a method via D-Bus.
107 class MethodCall : public Message {
108 public:
109 // Creates a method call message for the specified interface name and
110 // the method name.
111 //
112 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
113 // interface ("org.freedesktop.DBus.Introspectable"), create a method
114 // call like this:
115 //
116 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
117 //
118 // The constructor creates the internal raw_message_, so the client
119 // doesn't need to set this by reset_raw_message().
120 MethodCall(const std::string& interface_name,
121 const std::string& method_name);
122
123 const std::string& interface_name() { return interface_name_; }
124 const std::string& method_name() { return method_name_; }
125
126 // Sets the service name. This will be handled by the object proxy.
127 void SetServiceName(const std::string& service_name);
128 // Sets the object path. This will be handled by the object proxy.
129 void SetObjectPath(const std::string& object_path);
130
131 std::string interface_name_;
132 std::string method_name_;
133
134 DISALLOW_COPY_AND_ASSIGN(MethodCall);
135 };
136
137 // Response is a type of message used for receiving a response from a
138 // method via D-Bus.
139 class Response : public Message {
140 public:
141 // Creates a Response message. The internal raw message is
142 // NULL. ObjectProxy will set it properly once it gets a response from
143 // the server.
144 Response();
145
146 private:
147 DISALLOW_COPY_AND_ASSIGN(Response);
148 };
149
150 // MessageWriter is used to write outgoing messages for calling methods
151 // and sending signals.
152 class MessageWriter {
153 public:
154 // The data will be written into the given message.
155 MessageWriter(Message* message);
156 ~MessageWriter();
157
158 // Appends a byte to the message.
159 void AppendByte(uint8 value);
160 void AppendBool(bool value);
161 void AppendInt16(int16 value);
162 void AppendUint16(uint16 value);
163 void AppendInt32(int32 value);
164 void AppendUint32(uint32 value);
165 void AppendInt64(int64 value);
166 void AppendUint64(uint64 value);
167 void AppendDouble(double value);
168 void AppendString(const std::string& value);
169 void AppendObjectPath(const std::string& value);
170
171 // Opens an array. The array contents can be added to the array with
172 // |sub_writer|. The client code should close the array with
173 // CloseContainer(), once all contents are added.
174 //
175 // |signature| parameter is used to supply the D-Bus type signature of
176 // the array contents. For instance, if you want an array of strings,
177 // then you pass "s" as the signature.
178 //
179 // See the spec for details about the type signatures.
180 // http://dbus.freedesktop.org/doc/dbus-specification.html
181 // #message-protocol-signatures
182 //
183 // Ideally, client shouldn't need to supply the signature string, but
184 // the underlying D-Bus library requires us to supply this before
185 // appending contents to array and variant. It's technically possible
186 // for us to design API that doesn't require the signature but it will
187 // complicate the implementation so we decided to have the signature
188 // parameter. Hopefully, variants are less used in request messages from
189 // client side than response message from server side, so this should
190 // not be a big issue.
191 //
192 void OpenArray(const std::string& signature, MessageWriter* sub_writer);
193 // Do the same for a variant.
194 void OpenVariant(const std::string& signature, MessageWriter* sub_writer);
195 // Do the same for Struct and dict entry. They don't need the signature.
196 void OpenStruct(MessageWriter* sub_writer);
197 void OpenDictEntry(MessageWriter* sub_writer);
198
199 // Close the container for a array/variant/struct/dict entry.
200 void CloseContainer(MessageWriter* sub_writer);
201
202 // Appends the array of bytes. Arrays of bytes are often used for
203 // exchanging binary blobs hence it's worth having a specialized
204 // function.
205 void AppendArrayOfBytes(const uint8* values, size_t length);
206
207 // Appends the byte wrapped in a variant data container. Variants are
208 // widely used in D-Bus services so it's worth having a specialized
209 // function. For instance, The third parameter of
210 // "org.freedesktop.DBus.Properties.Set" is a variant.
211 void AppendVariantOfByte(uint8 value);
212 void AppendVariantOfBool(bool value);
213 void AppendVariantOfInt16(int16 value);
214 void AppendVariantOfUint16(uint16 value);
215 void AppendVariantOfInt32(int32 value);
216 void AppendVariantOfUint32(uint32 value);
217 void AppendVariantOfInt64(int64 value);
218 void AppendVariantOfUint64(uint64 value);
219 void AppendVariantOfDouble(double value);
220 void AppendVariantOfString(const std::string& value);
221 void AppendVariantOfObjectPath(const std::string& value);
222
223 private:
224 // Helper function used to implement AppendByte etc.
225 void AppendBasic(int dbus_type, const void* value);
226
227 // Helper function used to implement AppendVariantOfByte() etc.
228 void AppendVariantOfBasic(int dbus_type, const void* value);
229
230 Message* message_;
231 DBusMessageIter raw_message_iter_;
232 bool container_is_open_;
233
234 DISALLOW_COPY_AND_ASSIGN(MessageWriter);
235 };
236
237 // MessageReader is used to read incoming messages such as responses for
238 // method calls.
239 //
240 // MessageReader manages an internal iterator to read data. All functions
241 // starting with Pop advance the iterator on success.
242 //
243 // The main design goal of MessageReader and MessageWriter classes is to
244 // provide a type safe API. In the past, there was a Chrome OS blocker
245 // bug, that took days to fix, that would have been prevented if the API
246 // was type-safe.
247 //
248 // For instance, instead of doing something like:
249 //
250 // // We shouldn't add '&' to str here, but it compiles with '&' added.
251 // dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...)
252 //
253 // We want to do something like:
254 //
255 // writer.AppendString(str);
256 //
257 class MessageReader {
258 public:
259 // The data will be read from the given message.
260 MessageReader(Message* message);
261 ~MessageReader();
262
263 // Returns true if the reader has more data to read. The function is
264 // used to iterate contents in a container like:
265 //
266 // while (reader.HasMoreData())
267 // reader.PopString(&value);
268 bool HasMoreData();
269
270 // Gets the byte at the current iterator position. On success, advances
271 // the iterator and returns true. It's an error if the actual data type
272 // is not a byte, so false will be returned in this case.
273 bool PopByte(uint8* value);
274 bool PopBool(bool* value);
275 bool PopInt16(int16* value);
276 bool PopUint16(uint16* value);
277 bool PopInt32(int32* value);
278 bool PopUint32(uint32* value);
279 bool PopInt64(int64* value);
280 bool PopUint64(uint64* value);
281 bool PopDouble(double* value);
282 bool PopString(std::string* value);
283 bool PopObjectPath(std::string* value);
284
285 // Sets up the given message reader to read an array at the current
286 // iterator position. On success, advances the iterator and returns
287 // true. It's an error if the actual data type is not an array, so false
288 // will be returned in this case.
289 bool PopArray(MessageReader* sub_reader);
290 // The following functions do the same for other container types.
291 bool PopStruct(MessageReader* sub_reader);
292 bool PopDictEntry(MessageReader* sub_reader);
293 bool PopVariant(MessageReader* sub_reader);
294
295 // Gets the array of bytes at the current iterator position. On success,
296 // advances the iterator and returns true. Arrays of bytes are often
297 // used for exchanging binary blobs hence it's worth having a
298 // specialized function.
299 bool PopArrayOfBytes(std::vector<uint8>* bytes);
300
301 // Gets the array of object paths at the current iterator position. On
302 // success, advances the iterator and returns true. Arrays of object
303 // paths are often used to communicate with D-Bus services like
304 // NetworkManager, hence it's worth having a specialized function.
305 bool PopArrayOfObjectPaths(std::vector<std::string>* object_paths);
306
307 // Gets the byte from the variant data container at the current iterator
308 // position. On success, returns true and advances the
309 // iterator. Variants are widely used in D-Bus services so it's worth
310 // having a specialized function. For instance, The return value type of
311 // "org.freedesktop.DBus.Properties.Get" is a variant.
312 bool PopVariantOfByte(uint8* value);
313 bool PopVariantOfBool(bool* value);
314 bool PopVariantOfInt16(int16* value);
315 bool PopVariantOfUint16(uint16* value);
316 bool PopVariantOfInt32(int32* value);
317 bool PopVariantOfUint32(uint32* value);
318 bool PopVariantOfInt64(int64* value);
319 bool PopVariantOfUint64(uint64* value);
320 bool PopVariantOfDouble(double* value);
321 bool PopVariantOfString(std::string* value);
322 bool PopVariantOfObjectPath(std::string* value);
323
324 // Get the data type of the value at the current iterator
325 // position. INVALID_DATA will be returned if the iterator points to the
326 // end of the message.
327 Message::DataType GetDataType();
328
329 private:
330 // Returns true if the data type at the current iterator position
331 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
332 bool CheckDataType(int dbus_type);
333
334 // Helper function used to implement PopByte() etc.
335 bool PopBasic(int dbus_type, void *value);
336
337 // Helper function used to implement PopArray() etc.
338 bool PopContainer(int dbus_type, MessageReader* sub_reader);
339
340 // Helper function used to implement PopVariantOfByte() etc.
341 bool PopVariantOfBasic(int dbus_type, void* value);
342
343 Message* message_;
344 DBusMessageIter raw_message_iter_;
345
346 DISALLOW_COPY_AND_ASSIGN(MessageReader);
347 };
348
349 } // namespace dbus
350
351 #endif // DBUS_MESSAGE_H_
OLDNEW
« no previous file with comments | « dbus/dbus.gyp ('k') | dbus/message.cc » ('j') | dbus/message.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698