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.
5 #ifndef DBUS_MESSAGE_H_
6 #define DBUS_MESSAGE_H_
11 #include <dbus/dbus.h>
13 #include "base/basictypes.h"
14 #include "dbus/object_path.h"
21 } // namespace protobuf
30 // Message is the base class of D-Bus message types. Client code must use
31 // sub classes such as MethodCall and Response instead.
33 // The class name Message is very generic, but there should be no problem
34 // as the class is inside 'dbus' namespace. We chose to name this way, as
35 // libdbus defines lots of types starting with DBus, such as
36 // DBusMessage. We should avoid confusion and conflict with these types.
39 // The message type used in D-Bus. Redefined here so client code
40 // doesn't need to use raw D-Bus macros. DBUS_MESSAGE_TYPE_INVALID
41 // etc. are #define macros. Having an enum type here makes code a bit
44 MESSAGE_INVALID
= DBUS_MESSAGE_TYPE_INVALID
,
45 MESSAGE_METHOD_CALL
= DBUS_MESSAGE_TYPE_METHOD_CALL
,
46 MESSAGE_METHOD_RETURN
= DBUS_MESSAGE_TYPE_METHOD_RETURN
,
47 MESSAGE_SIGNAL
= DBUS_MESSAGE_TYPE_SIGNAL
,
48 MESSAGE_ERROR
= DBUS_MESSAGE_TYPE_ERROR
,
51 // The data type used in the D-Bus type system. See the comment at
52 // MessageType for why we are redefining data types here.
54 INVALID_DATA
= DBUS_TYPE_INVALID
,
55 BYTE
= DBUS_TYPE_BYTE
,
56 BOOL
= DBUS_TYPE_BOOLEAN
,
57 INT16
= DBUS_TYPE_INT16
,
58 UINT16
= DBUS_TYPE_UINT16
,
59 INT32
= DBUS_TYPE_INT32
,
60 UINT32
= DBUS_TYPE_UINT32
,
61 INT64
= DBUS_TYPE_INT64
,
62 UINT64
= DBUS_TYPE_UINT64
,
63 DOUBLE
= DBUS_TYPE_DOUBLE
,
64 STRING
= DBUS_TYPE_STRING
,
65 OBJECT_PATH
= DBUS_TYPE_OBJECT_PATH
,
66 ARRAY
= DBUS_TYPE_ARRAY
,
67 STRUCT
= DBUS_TYPE_STRUCT
,
68 DICT_ENTRY
= DBUS_TYPE_DICT_ENTRY
,
69 VARIANT
= DBUS_TYPE_VARIANT
,
72 // Returns the type of the message. Returns MESSAGE_INVALID if
73 // raw_message_ is NULL.
74 MessageType
GetMessageType();
76 // Returns the type of the message as string like "MESSAGE_METHOD_CALL"
78 std::string
GetMessageTypeAsString();
80 DBusMessage
* raw_message() { return raw_message_
; }
82 // Sets the destination, the path, the interface, the member, etc.
83 void SetDestination(const std::string
& destination
);
84 void SetPath(const ObjectPath
& path
);
85 void SetInterface(const std::string
& interface
);
86 void SetMember(const std::string
& member
);
87 void SetErrorName(const std::string
& error_name
);
88 void SetSender(const std::string
& sender
);
89 void SetSerial(uint32 serial
);
90 void SetReplySerial(uint32 reply_serial
);
91 // SetSignature() does not exist as we cannot do it.
93 // Gets the destination, the path, the interface, the member, etc.
94 // If not set, an empty string is returned.
95 std::string
GetDestination();
97 std::string
GetInterface();
98 std::string
GetMember();
99 std::string
GetErrorName();
100 std::string
GetSender();
101 std::string
GetSignature();
102 // Gets the serial and reply serial numbers. Returns 0 if not set.
104 uint32
GetReplySerial();
106 // Returns the string representation of this message. Useful for
108 std::string
ToString();
111 // This class cannot be instantiated. Use sub classes instead.
115 // Initializes the message with the given raw message.
116 void Init(DBusMessage
* raw_message
);
119 // Helper function used in ToString().
120 std::string
ToStringInternal(const std::string
& indent
,
121 MessageReader
* reader
);
123 DBusMessage
* raw_message_
;
124 DISALLOW_COPY_AND_ASSIGN(Message
);
127 // MessageCall is a type of message used for calling a method via D-Bus.
128 class MethodCall
: public Message
{
130 // Creates a method call message for the specified interface name and
133 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
134 // interface ("org.freedesktop.DBus.Introspectable"), create a method
137 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
139 // The constructor creates the internal raw message.
140 MethodCall(const std::string
& interface_name
,
141 const std::string
& method_name
);
143 // Returns a newly created MethodCall from the given raw message of the
144 // type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the
145 // returned object. Takes the ownership of |raw_message|.
146 static MethodCall
* FromRawMessage(DBusMessage
* raw_message
);
149 // Creates a method call message. The internal raw message is NULL.
150 // Only used internally.
153 DISALLOW_COPY_AND_ASSIGN(MethodCall
);
156 // Signal is a type of message used to send a signal.
157 class Signal
: public Message
{
159 // Creates a signal message for the specified interface name and the
162 // For instance, to send "PropertiesChanged" signal of
163 // DBUS_INTERFACE_INTROSPECTABLE interface
164 // ("org.freedesktop.DBus.Introspectable"), create a signal like this:
166 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged");
168 // The constructor creates the internal raw_message_.
169 Signal(const std::string
& interface_name
,
170 const std::string
& method_name
);
172 // Returns a newly created SIGNAL from the given raw message of the type
173 // DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned
174 // object. Takes the ownership of |raw_message|.
175 static Signal
* FromRawMessage(DBusMessage
* raw_message
);
178 // Creates a signal message. The internal raw message is NULL.
179 // Only used internally.
182 DISALLOW_COPY_AND_ASSIGN(Signal
);
185 // Response is a type of message used for receiving a response from a
187 class Response
: public Message
{
189 // Returns a newly created Response from the given raw message of the
190 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
191 // returned object. Takes the ownership of |raw_message|.
192 static Response
* FromRawMessage(DBusMessage
* raw_message
);
194 // Returns a newly created Response from the given method call. The
195 // caller must delete the returned object. Used for implementing
197 static Response
* FromMethodCall(MethodCall
* method_call
);
199 // Returns a newly created Response with an empty payload. The caller
200 // must delete the returned object. Useful for testing.
201 static Response
* CreateEmpty();
204 // Creates a Response message. The internal raw message is NULL.
207 DISALLOW_COPY_AND_ASSIGN(Response
);
210 // ErrorResponse is a type of message used to return an error to the
211 // caller of a method.
212 class ErrorResponse
: public Message
{
214 // Returns a newly created Response from the given raw message of the
215 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
216 // returned object. Takes the ownership of |raw_message|.
217 static ErrorResponse
* FromRawMessage(DBusMessage
* raw_message
);
219 // Returns a newly created ErrorResponse from the given method call, the
220 // error name, and the error message. The error name looks like
221 // "org.freedesktop.DBus.Error.Failed". Used for returning an error to a
222 // failed method call.
223 static ErrorResponse
* FromMethodCall(MethodCall
* method_call
,
224 const std::string
& error_name
,
225 const std::string
& error_message
);
228 // Creates an ErrorResponse message. The internal raw message is NULL.
231 DISALLOW_COPY_AND_ASSIGN(ErrorResponse
);
234 // MessageWriter is used to write outgoing messages for calling methods
235 // and sending signals.
237 // The main design goal of MessageReader and MessageWriter classes is to
238 // provide a type safe API. In the past, there was a Chrome OS blocker
239 // bug, that took days to fix, that would have been prevented if the API
242 // For instance, instead of doing something like:
244 // // We shouldn't add '&' to str here, but it compiles with '&' added.
245 // dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...)
247 // We want to do something like:
249 // writer.AppendString(str);
251 class MessageWriter
{
253 // Data added with Append* will be written to |message|, which may be NULL
254 // to create a sub-writer for passing to OpenArray, etc.
255 explicit MessageWriter(Message
* message
);
258 // Appends a byte to the message.
259 void AppendByte(uint8 value
);
260 void AppendBool(bool value
);
261 void AppendInt16(int16 value
);
262 void AppendUint16(uint16 value
);
263 void AppendInt32(int32 value
);
264 void AppendUint32(uint32 value
);
265 void AppendInt64(int64 value
);
266 void AppendUint64(uint64 value
);
267 void AppendDouble(double value
);
268 void AppendString(const std::string
& value
);
269 void AppendObjectPath(const ObjectPath
& value
);
271 // Opens an array. The array contents can be added to the array with
272 // |sub_writer|. The client code must close the array with
273 // CloseContainer(), once all contents are added.
275 // |signature| parameter is used to supply the D-Bus type signature of
276 // the array contents. For instance, if you want an array of strings,
277 // then you pass "s" as the signature.
279 // See the spec for details about the type signatures.
280 // http://dbus.freedesktop.org/doc/dbus-specification.html
281 // #message-protocol-signatures
283 void OpenArray(const std::string
& signature
, MessageWriter
* sub_writer
);
284 // Do the same for a variant.
285 void OpenVariant(const std::string
& signature
, MessageWriter
* sub_writer
);
286 // Do the same for Struct and dict entry. They don't need the signature.
287 void OpenStruct(MessageWriter
* sub_writer
);
288 void OpenDictEntry(MessageWriter
* sub_writer
);
290 // Close the container for a array/variant/struct/dict entry.
291 void CloseContainer(MessageWriter
* sub_writer
);
293 // Appends the array of bytes. Arrays of bytes are often used for
294 // exchanging binary blobs hence it's worth having a specialized
296 void AppendArrayOfBytes(const uint8
* values
, size_t length
);
298 // Appends the array of strings. Arrays of strings are often used for
299 // exchanging lists of names hence it's worth having a specialized
301 void AppendArrayOfStrings(const std::vector
<std::string
>& strings
);
303 // Appends the array of object paths. Arrays of object paths are often
304 // used when exchanging object paths, hence it's worth having a
305 // specialized function.
306 void AppendArrayOfObjectPaths(const std::vector
<ObjectPath
>& object_paths
);
308 // Appends the protocol buffer as an array of bytes. The buffer is serialized
309 // into an array of bytes before communication, since protocol buffers are not
310 // a native dbus type. On the receiving size the array of bytes needs to be
311 // read and deserialized into a protocol buffer of the correct type. There are
312 // methods in MessageReader to assist in this. Return true on succes and fail
313 // when serialization is not successful.
314 bool AppendProtoAsArrayOfBytes(const google::protobuf::MessageLite
& protobuf
);
316 // Appends the byte wrapped in a variant data container. Variants are
317 // widely used in D-Bus services so it's worth having a specialized
318 // function. For instance, The third parameter of
319 // "org.freedesktop.DBus.Properties.Set" is a variant.
320 void AppendVariantOfByte(uint8 value
);
321 void AppendVariantOfBool(bool value
);
322 void AppendVariantOfInt16(int16 value
);
323 void AppendVariantOfUint16(uint16 value
);
324 void AppendVariantOfInt32(int32 value
);
325 void AppendVariantOfUint32(uint32 value
);
326 void AppendVariantOfInt64(int64 value
);
327 void AppendVariantOfUint64(uint64 value
);
328 void AppendVariantOfDouble(double value
);
329 void AppendVariantOfString(const std::string
& value
);
330 void AppendVariantOfObjectPath(const ObjectPath
& value
);
333 // Helper function used to implement AppendByte etc.
334 void AppendBasic(int dbus_type
, const void* value
);
336 // Helper function used to implement AppendVariantOfByte() etc.
337 void AppendVariantOfBasic(int dbus_type
, const void* value
);
340 DBusMessageIter raw_message_iter_
;
341 bool container_is_open_
;
343 DISALLOW_COPY_AND_ASSIGN(MessageWriter
);
346 // MessageReader is used to read incoming messages such as responses for
349 // MessageReader manages an internal iterator to read data. All functions
350 // starting with Pop advance the iterator on success.
351 class MessageReader
{
353 // The data will be read from the given |message|, which may be NULL to
354 // create a sub-reader for passing to PopArray, etc.
355 explicit MessageReader(Message
* message
);
358 // Returns true if the reader has more data to read. The function is
359 // used to iterate contents in a container like:
361 // while (reader.HasMoreData())
362 // reader.PopString(&value);
365 // Gets the byte at the current iterator position.
366 // Returns true and advances the iterator on success.
367 // Returns false if the data type is not a byte.
368 bool PopByte(uint8
* value
);
369 bool PopBool(bool* value
);
370 bool PopInt16(int16
* value
);
371 bool PopUint16(uint16
* value
);
372 bool PopInt32(int32
* value
);
373 bool PopUint32(uint32
* value
);
374 bool PopInt64(int64
* value
);
375 bool PopUint64(uint64
* value
);
376 bool PopDouble(double* value
);
377 bool PopString(std::string
* value
);
378 bool PopObjectPath(ObjectPath
* value
);
380 // Sets up the given message reader to read an array at the current
381 // iterator position.
382 // Returns true and advances the iterator on success.
383 // Returns false if the data type is not an array
384 bool PopArray(MessageReader
* sub_reader
);
385 bool PopStruct(MessageReader
* sub_reader
);
386 bool PopDictEntry(MessageReader
* sub_reader
);
387 bool PopVariant(MessageReader
* sub_reader
);
389 // Gets the array of bytes at the current iterator position.
390 // Returns true and advances the iterator on success.
392 // Arrays of bytes are often used for exchanging binary blobs hence it's
393 // worth having a specialized function.
395 // |bytes| must be copied if the contents will be referenced after the
396 // MessageReader is destroyed.
397 bool PopArrayOfBytes(uint8
** bytes
, size_t* length
);
399 // Gets the array of strings at the current iterator position.
400 // Returns true and advances the iterator on success.
402 // Arrays of strings are often used to communicate with D-Bus
403 // services like KWallet, hence it's worth having a specialized
405 bool PopArrayOfStrings(std::vector
<std::string
>* strings
);
407 // Gets the array of object paths at the current iterator position.
408 // Returns true and advances the iterator on success.
410 // Arrays of object paths are often used to communicate with D-Bus
411 // services like NetworkManager, hence it's worth having a specialized
413 bool PopArrayOfObjectPaths(std::vector
<ObjectPath
>* object_paths
);
415 // Gets the array of bytes at the current iterator position. It then parses
416 // this binary blob into the protocol buffer supplied.
417 // Returns true and advances the iterator on success. On failure returns false
418 // and emits an error message on the source of the failure. The two most
419 // common errors come from the iterator not currently being at a byte array or
420 // the wrong type of protocol buffer is passed in and the parse fails.
421 bool PopArrayOfBytesAsProto(google::protobuf::MessageLite
* protobuf
);
423 // Gets the byte from the variant data container at the current iterator
425 // Returns true and advances the iterator on success.
427 // Variants are widely used in D-Bus services so it's worth having a
428 // specialized function. For instance, The return value type of
429 // "org.freedesktop.DBus.Properties.Get" is a variant.
430 bool PopVariantOfByte(uint8
* value
);
431 bool PopVariantOfBool(bool* value
);
432 bool PopVariantOfInt16(int16
* value
);
433 bool PopVariantOfUint16(uint16
* value
);
434 bool PopVariantOfInt32(int32
* value
);
435 bool PopVariantOfUint32(uint32
* value
);
436 bool PopVariantOfInt64(int64
* value
);
437 bool PopVariantOfUint64(uint64
* value
);
438 bool PopVariantOfDouble(double* value
);
439 bool PopVariantOfString(std::string
* value
);
440 bool PopVariantOfObjectPath(ObjectPath
* value
);
442 // Get the data type of the value at the current iterator
443 // position. INVALID_DATA will be returned if the iterator points to the
444 // end of the message.
445 Message::DataType
GetDataType();
448 // Returns true if the data type at the current iterator position
449 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
450 bool CheckDataType(int dbus_type
);
452 // Helper function used to implement PopByte() etc.
453 bool PopBasic(int dbus_type
, void *value
);
455 // Helper function used to implement PopArray() etc.
456 bool PopContainer(int dbus_type
, MessageReader
* sub_reader
);
458 // Helper function used to implement PopVariantOfByte() etc.
459 bool PopVariantOfBasic(int dbus_type
, void* value
);
462 DBusMessageIter raw_message_iter_
;
464 DISALLOW_COPY_AND_ASSIGN(MessageReader
);
469 #endif // DBUS_MESSAGE_H_