blob: 85d045c2812b417dd3475d57c4921d885c514f49 [file] [log] [blame]
[email protected]9400ff82012-02-07 23:48:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a9e91492011-07-30 19:13:312// 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_
[email protected]a9e91492011-07-30 19:13:317
8#include <string>
9#include <vector>
10#include <dbus/dbus.h>
11
12#include "base/basictypes.h"
[email protected]e146bfc2012-03-30 06:46:2013#include "dbus/file_descriptor.h"
[email protected]216ed0b2012-02-14 21:29:0614#include "dbus/object_path.h"
[email protected]a9e91492011-07-30 19:13:3115
[email protected]c033c5082012-02-09 18:14:0816namespace google {
17namespace protobuf {
18
19class MessageLite;
20
21} // namespace protobuf
22} // namespace google
23
24
[email protected]a9e91492011-07-30 19:13:3125namespace dbus {
26
27class MessageWriter;
28class MessageReader;
29
[email protected]e146bfc2012-03-30 06:46:2030// DBUS_TYPE_UNIX_FD was added in D-Bus version 1.4
[email protected]73c71512012-07-25 19:29:5731#if !defined(DBUS_TYPE_UNIX_FD)
[email protected]e146bfc2012-03-30 06:46:2032#define DBUS_TYPE_UNIX_FD ((int) 'h')
33#endif
34
[email protected]73c71512012-07-25 19:29:5735// Returns true if Unix FD passing is supported in libdbus.
36// The check is done runtime rather than compile time as the libdbus
37// version used at runtime may be different from the one used at compile time.
38bool IsDBusTypeUnixFdSupported();
39
[email protected]06ead872011-08-24 03:32:0640// Message is the base class of D-Bus message types. Client code must use
41// sub classes such as MethodCall and Response instead.
[email protected]a9e91492011-07-30 19:13:3142//
43// The class name Message is very generic, but there should be no problem
44// as the class is inside 'dbus' namespace. We chose to name this way, as
45// libdbus defines lots of types starting with DBus, such as
46// DBusMessage. We should avoid confusion and conflict with these types.
47class Message {
48 public:
49 // The message type used in D-Bus. Redefined here so client code
50 // doesn't need to use raw D-Bus macros. DBUS_MESSAGE_TYPE_INVALID
51 // etc. are #define macros. Having an enum type here makes code a bit
52 // more type-safe.
53 enum MessageType {
54 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID,
55 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL,
56 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN,
57 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL,
58 MESSAGE_ERROR = DBUS_MESSAGE_TYPE_ERROR,
59 };
60
61 // The data type used in the D-Bus type system. See the comment at
62 // MessageType for why we are redefining data types here.
63 enum DataType {
64 INVALID_DATA = DBUS_TYPE_INVALID,
65 BYTE = DBUS_TYPE_BYTE,
66 BOOL = DBUS_TYPE_BOOLEAN,
67 INT16 = DBUS_TYPE_INT16,
68 UINT16 = DBUS_TYPE_UINT16,
69 INT32 = DBUS_TYPE_INT32,
70 UINT32 = DBUS_TYPE_UINT32,
71 INT64 = DBUS_TYPE_INT64,
72 UINT64 = DBUS_TYPE_UINT64,
73 DOUBLE = DBUS_TYPE_DOUBLE,
74 STRING = DBUS_TYPE_STRING,
75 OBJECT_PATH = DBUS_TYPE_OBJECT_PATH,
76 ARRAY = DBUS_TYPE_ARRAY,
77 STRUCT = DBUS_TYPE_STRUCT,
78 DICT_ENTRY = DBUS_TYPE_DICT_ENTRY,
79 VARIANT = DBUS_TYPE_VARIANT,
[email protected]e146bfc2012-03-30 06:46:2080 UNIX_FD = DBUS_TYPE_UNIX_FD,
[email protected]a9e91492011-07-30 19:13:3181 };
82
[email protected]a9e91492011-07-30 19:13:3183 // Returns the type of the message. Returns MESSAGE_INVALID if
84 // raw_message_ is NULL.
85 MessageType GetMessageType();
86
[email protected]06ead872011-08-24 03:32:0687 // Returns the type of the message as string like "MESSAGE_METHOD_CALL"
88 // for instance.
89 std::string GetMessageTypeAsString();
[email protected]a9e91492011-07-30 19:13:3190
[email protected]06ead872011-08-24 03:32:0691 DBusMessage* raw_message() { return raw_message_; }
[email protected]a9e91492011-07-30 19:13:3192
[email protected]9cce2d32011-08-10 22:34:0293 // Sets the destination, the path, the interface, the member, etc.
[email protected]ca72ff22012-05-23 06:55:2294 bool SetDestination(const std::string& destination);
95 bool SetPath(const ObjectPath& path);
96 bool SetInterface(const std::string& interface);
97 bool SetMember(const std::string& member);
98 bool SetErrorName(const std::string& error_name);
99 bool SetSender(const std::string& sender);
[email protected]9cce2d32011-08-10 22:34:02100 void SetSerial(uint32 serial);
101 void SetReplySerial(uint32 reply_serial);
102 // SetSignature() does not exist as we cannot do it.
103
104 // Gets the destination, the path, the interface, the member, etc.
105 // If not set, an empty string is returned.
106 std::string GetDestination();
[email protected]216ed0b2012-02-14 21:29:06107 ObjectPath GetPath();
[email protected]9cce2d32011-08-10 22:34:02108 std::string GetInterface();
109 std::string GetMember();
110 std::string GetErrorName();
111 std::string GetSender();
112 std::string GetSignature();
113 // Gets the serial and reply serial numbers. Returns 0 if not set.
114 uint32 GetSerial();
115 uint32 GetReplySerial();
116
[email protected]a9e91492011-07-30 19:13:31117 // Returns the string representation of this message. Useful for
[email protected]6df1b9592012-06-07 16:41:26118 // debugging. The output is truncated as needed (ex. strings are truncated
119 // if longer than a certain limit defined in the .cc file).
[email protected]a9e91492011-07-30 19:13:31120 std::string ToString();
121
[email protected]06ead872011-08-24 03:32:06122 protected:
123 // This class cannot be instantiated. Use sub classes instead.
124 Message();
125 virtual ~Message();
126
127 // Initializes the message with the given raw message.
128 void Init(DBusMessage* raw_message);
129
[email protected]a9e91492011-07-30 19:13:31130 private:
131 // Helper function used in ToString().
132 std::string ToStringInternal(const std::string& indent,
133 MessageReader* reader);
134
135 DBusMessage* raw_message_;
136 DISALLOW_COPY_AND_ASSIGN(Message);
137};
138
139// MessageCall is a type of message used for calling a method via D-Bus.
140class MethodCall : public Message {
141 public:
142 // Creates a method call message for the specified interface name and
143 // the method name.
144 //
145 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
146 // interface ("org.freedesktop.DBus.Introspectable"), create a method
147 // call like this:
148 //
149 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
150 //
[email protected]06ead872011-08-24 03:32:06151 // The constructor creates the internal raw message.
[email protected]a9e91492011-07-30 19:13:31152 MethodCall(const std::string& interface_name,
153 const std::string& method_name);
154
[email protected]9cce2d32011-08-10 22:34:02155 // Returns a newly created MethodCall from the given raw message of the
156 // type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the
157 // returned object. Takes the ownership of |raw_message|.
158 static MethodCall* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31159
[email protected]06ead872011-08-24 03:32:06160 private:
161 // Creates a method call message. The internal raw message is NULL.
162 // Only used internally.
163 MethodCall();
164
[email protected]a9e91492011-07-30 19:13:31165 DISALLOW_COPY_AND_ASSIGN(MethodCall);
166};
167
[email protected]3beaaa4e2011-08-23 07:29:21168// Signal is a type of message used to send a signal.
169class Signal : public Message {
170 public:
171 // Creates a signal message for the specified interface name and the
172 // method name.
173 //
174 // For instance, to send "PropertiesChanged" signal of
175 // DBUS_INTERFACE_INTROSPECTABLE interface
176 // ("org.freedesktop.DBus.Introspectable"), create a signal like this:
177 //
178 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged");
179 //
[email protected]06ead872011-08-24 03:32:06180 // The constructor creates the internal raw_message_.
[email protected]3beaaa4e2011-08-23 07:29:21181 Signal(const std::string& interface_name,
182 const std::string& method_name);
183
184 // Returns a newly created SIGNAL from the given raw message of the type
185 // DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned
186 // object. Takes the ownership of |raw_message|.
187 static Signal* FromRawMessage(DBusMessage* raw_message);
[email protected]06ead872011-08-24 03:32:06188
189 private:
190 // Creates a signal message. The internal raw message is NULL.
191 // Only used internally.
192 Signal();
193
194 DISALLOW_COPY_AND_ASSIGN(Signal);
[email protected]3beaaa4e2011-08-23 07:29:21195};
196
[email protected]a9e91492011-07-30 19:13:31197// Response is a type of message used for receiving a response from a
198// method via D-Bus.
199class Response : public Message {
200 public:
[email protected]06ead872011-08-24 03:32:06201 // Returns a newly created Response from the given raw message of the
202 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
203 // returned object. Takes the ownership of |raw_message|.
204 static Response* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31205
[email protected]9cce2d32011-08-10 22:34:02206 // Returns a newly created Response from the given method call. The
207 // caller must delete the returned object. Used for implementing
208 // exported methods.
209 static Response* FromMethodCall(MethodCall* method_call);
210
[email protected]829f0e4c2011-08-31 18:02:43211 // Returns a newly created Response with an empty payload. The caller
[email protected]06ead872011-08-24 03:32:06212 // must delete the returned object. Useful for testing.
213 static Response* CreateEmpty();
214
[email protected]7533518e2012-03-11 01:12:00215 protected:
[email protected]06ead872011-08-24 03:32:06216 // Creates a Response message. The internal raw message is NULL.
217 Response();
218
[email protected]7533518e2012-03-11 01:12:00219 private:
[email protected]a9e91492011-07-30 19:13:31220 DISALLOW_COPY_AND_ASSIGN(Response);
221};
222
[email protected]9cce2d32011-08-10 22:34:02223// ErrorResponse is a type of message used to return an error to the
224// caller of a method.
[email protected]7533518e2012-03-11 01:12:00225class ErrorResponse: public Response {
[email protected]9cce2d32011-08-10 22:34:02226 public:
[email protected]06ead872011-08-24 03:32:06227 // Returns a newly created Response from the given raw message of the
228 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
229 // returned object. Takes the ownership of |raw_message|.
230 static ErrorResponse* FromRawMessage(DBusMessage* raw_message);
[email protected]9cce2d32011-08-10 22:34:02231
232 // Returns a newly created ErrorResponse from the given method call, the
233 // error name, and the error message. The error name looks like
234 // "org.freedesktop.DBus.Error.Failed". Used for returning an error to a
235 // failed method call.
236 static ErrorResponse* FromMethodCall(MethodCall* method_call,
237 const std::string& error_name,
238 const std::string& error_message);
239
240 private:
[email protected]06ead872011-08-24 03:32:06241 // Creates an ErrorResponse message. The internal raw message is NULL.
242 ErrorResponse();
243
[email protected]9cce2d32011-08-10 22:34:02244 DISALLOW_COPY_AND_ASSIGN(ErrorResponse);
245};
246
[email protected]a9e91492011-07-30 19:13:31247// MessageWriter is used to write outgoing messages for calling methods
248// and sending signals.
249//
250// The main design goal of MessageReader and MessageWriter classes is to
251// provide a type safe API. In the past, there was a Chrome OS blocker
252// bug, that took days to fix, that would have been prevented if the API
253// was type-safe.
254//
255// For instance, instead of doing something like:
256//
257// // We shouldn't add '&' to str here, but it compiles with '&' added.
258// dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...)
259//
260// We want to do something like:
261//
262// writer.AppendString(str);
263//
264class MessageWriter {
265 public:
[email protected]9400ff82012-02-07 23:48:35266 // Data added with Append* will be written to |message|, which may be NULL
267 // to create a sub-writer for passing to OpenArray, etc.
268 explicit MessageWriter(Message* message);
[email protected]a9e91492011-07-30 19:13:31269 ~MessageWriter();
270
271 // Appends a byte to the message.
272 void AppendByte(uint8 value);
273 void AppendBool(bool value);
274 void AppendInt16(int16 value);
275 void AppendUint16(uint16 value);
276 void AppendInt32(int32 value);
277 void AppendUint32(uint32 value);
278 void AppendInt64(int64 value);
279 void AppendUint64(uint64 value);
280 void AppendDouble(double value);
281 void AppendString(const std::string& value);
[email protected]216ed0b2012-02-14 21:29:06282 void AppendObjectPath(const ObjectPath& value);
[email protected]e146bfc2012-03-30 06:46:20283 void AppendFileDescriptor(const FileDescriptor& value);
[email protected]a9e91492011-07-30 19:13:31284
285 // Opens an array. The array contents can be added to the array with
286 // |sub_writer|. The client code must close the array with
287 // CloseContainer(), once all contents are added.
288 //
289 // |signature| parameter is used to supply the D-Bus type signature of
290 // the array contents. For instance, if you want an array of strings,
291 // then you pass "s" as the signature.
292 //
293 // See the spec for details about the type signatures.
294 // http://dbus.freedesktop.org/doc/dbus-specification.html
295 // #message-protocol-signatures
296 //
297 void OpenArray(const std::string& signature, MessageWriter* sub_writer);
298 // Do the same for a variant.
299 void OpenVariant(const std::string& signature, MessageWriter* sub_writer);
300 // Do the same for Struct and dict entry. They don't need the signature.
301 void OpenStruct(MessageWriter* sub_writer);
302 void OpenDictEntry(MessageWriter* sub_writer);
303
304 // Close the container for a array/variant/struct/dict entry.
305 void CloseContainer(MessageWriter* sub_writer);
306
307 // Appends the array of bytes. Arrays of bytes are often used for
308 // exchanging binary blobs hence it's worth having a specialized
309 // function.
310 void AppendArrayOfBytes(const uint8* values, size_t length);
311
[email protected]8bc83fd2011-09-19 18:22:14312 // Appends the array of strings. Arrays of strings are often used for
313 // exchanging lists of names hence it's worth having a specialized
314 // function.
315 void AppendArrayOfStrings(const std::vector<std::string>& strings);
316
[email protected]090d8e512011-08-22 18:28:42317 // Appends the array of object paths. Arrays of object paths are often
[email protected]8bc83fd2011-09-19 18:22:14318 // used when exchanging object paths, hence it's worth having a
[email protected]090d8e512011-08-22 18:28:42319 // specialized function.
[email protected]216ed0b2012-02-14 21:29:06320 void AppendArrayOfObjectPaths(const std::vector<ObjectPath>& object_paths);
[email protected]090d8e512011-08-22 18:28:42321
[email protected]c033c5082012-02-09 18:14:08322 // Appends the protocol buffer as an array of bytes. The buffer is serialized
323 // into an array of bytes before communication, since protocol buffers are not
324 // a native dbus type. On the receiving size the array of bytes needs to be
325 // read and deserialized into a protocol buffer of the correct type. There are
326 // methods in MessageReader to assist in this. Return true on succes and fail
327 // when serialization is not successful.
328 bool AppendProtoAsArrayOfBytes(const google::protobuf::MessageLite& protobuf);
329
[email protected]a9e91492011-07-30 19:13:31330 // Appends the byte wrapped in a variant data container. Variants are
331 // widely used in D-Bus services so it's worth having a specialized
332 // function. For instance, The third parameter of
333 // "org.freedesktop.DBus.Properties.Set" is a variant.
334 void AppendVariantOfByte(uint8 value);
335 void AppendVariantOfBool(bool value);
336 void AppendVariantOfInt16(int16 value);
337 void AppendVariantOfUint16(uint16 value);
338 void AppendVariantOfInt32(int32 value);
339 void AppendVariantOfUint32(uint32 value);
340 void AppendVariantOfInt64(int64 value);
341 void AppendVariantOfUint64(uint64 value);
342 void AppendVariantOfDouble(double value);
343 void AppendVariantOfString(const std::string& value);
[email protected]216ed0b2012-02-14 21:29:06344 void AppendVariantOfObjectPath(const ObjectPath& value);
[email protected]a9e91492011-07-30 19:13:31345
346 private:
347 // Helper function used to implement AppendByte etc.
348 void AppendBasic(int dbus_type, const void* value);
349
350 // Helper function used to implement AppendVariantOfByte() etc.
351 void AppendVariantOfBasic(int dbus_type, const void* value);
352
353 Message* message_;
354 DBusMessageIter raw_message_iter_;
355 bool container_is_open_;
356
357 DISALLOW_COPY_AND_ASSIGN(MessageWriter);
358};
359
360// MessageReader is used to read incoming messages such as responses for
361// method calls.
362//
363// MessageReader manages an internal iterator to read data. All functions
364// starting with Pop advance the iterator on success.
365class MessageReader {
366 public:
[email protected]9400ff82012-02-07 23:48:35367 // The data will be read from the given |message|, which may be NULL to
368 // create a sub-reader for passing to PopArray, etc.
369 explicit MessageReader(Message* message);
[email protected]a9e91492011-07-30 19:13:31370 ~MessageReader();
371
372 // Returns true if the reader has more data to read. The function is
373 // used to iterate contents in a container like:
374 //
375 // while (reader.HasMoreData())
376 // reader.PopString(&value);
377 bool HasMoreData();
378
379 // Gets the byte at the current iterator position.
380 // Returns true and advances the iterator on success.
381 // Returns false if the data type is not a byte.
382 bool PopByte(uint8* value);
383 bool PopBool(bool* value);
384 bool PopInt16(int16* value);
385 bool PopUint16(uint16* value);
386 bool PopInt32(int32* value);
387 bool PopUint32(uint32* value);
388 bool PopInt64(int64* value);
389 bool PopUint64(uint64* value);
390 bool PopDouble(double* value);
391 bool PopString(std::string* value);
[email protected]216ed0b2012-02-14 21:29:06392 bool PopObjectPath(ObjectPath* value);
[email protected]e146bfc2012-03-30 06:46:20393 bool PopFileDescriptor(FileDescriptor* value);
[email protected]a9e91492011-07-30 19:13:31394
395 // Sets up the given message reader to read an array at the current
396 // iterator position.
397 // Returns true and advances the iterator on success.
398 // Returns false if the data type is not an array
399 bool PopArray(MessageReader* sub_reader);
400 bool PopStruct(MessageReader* sub_reader);
401 bool PopDictEntry(MessageReader* sub_reader);
402 bool PopVariant(MessageReader* sub_reader);
403
404 // Gets the array of bytes at the current iterator position.
405 // Returns true and advances the iterator on success.
406 //
407 // Arrays of bytes are often used for exchanging binary blobs hence it's
408 // worth having a specialized function.
409 //
410 // |bytes| must be copied if the contents will be referenced after the
[email protected]56d82c9c2011-09-06 20:03:24411 // MessageReader is destroyed.
[email protected]a9e91492011-07-30 19:13:31412 bool PopArrayOfBytes(uint8** bytes, size_t* length);
413
[email protected]8bc83fd2011-09-19 18:22:14414 // Gets the array of strings at the current iterator position.
415 // Returns true and advances the iterator on success.
416 //
417 // Arrays of strings are often used to communicate with D-Bus
418 // services like KWallet, hence it's worth having a specialized
419 // function.
420 bool PopArrayOfStrings(std::vector<std::string>* strings);
421
[email protected]a9e91492011-07-30 19:13:31422 // Gets the array of object paths at the current iterator position.
423 // Returns true and advances the iterator on success.
424 //
425 // Arrays of object paths are often used to communicate with D-Bus
426 // services like NetworkManager, hence it's worth having a specialized
427 // function.
[email protected]216ed0b2012-02-14 21:29:06428 bool PopArrayOfObjectPaths(std::vector<ObjectPath>* object_paths);
[email protected]a9e91492011-07-30 19:13:31429
[email protected]c033c5082012-02-09 18:14:08430 // Gets the array of bytes at the current iterator position. It then parses
431 // this binary blob into the protocol buffer supplied.
432 // Returns true and advances the iterator on success. On failure returns false
433 // and emits an error message on the source of the failure. The two most
434 // common errors come from the iterator not currently being at a byte array or
435 // the wrong type of protocol buffer is passed in and the parse fails.
436 bool PopArrayOfBytesAsProto(google::protobuf::MessageLite* protobuf);
437
[email protected]a9e91492011-07-30 19:13:31438 // Gets the byte from the variant data container at the current iterator
439 // position.
440 // Returns true and advances the iterator on success.
441 //
442 // Variants are widely used in D-Bus services so it's worth having a
443 // specialized function. For instance, The return value type of
444 // "org.freedesktop.DBus.Properties.Get" is a variant.
445 bool PopVariantOfByte(uint8* value);
446 bool PopVariantOfBool(bool* value);
447 bool PopVariantOfInt16(int16* value);
448 bool PopVariantOfUint16(uint16* value);
449 bool PopVariantOfInt32(int32* value);
450 bool PopVariantOfUint32(uint32* value);
451 bool PopVariantOfInt64(int64* value);
452 bool PopVariantOfUint64(uint64* value);
453 bool PopVariantOfDouble(double* value);
454 bool PopVariantOfString(std::string* value);
[email protected]216ed0b2012-02-14 21:29:06455 bool PopVariantOfObjectPath(ObjectPath* value);
[email protected]a9e91492011-07-30 19:13:31456
457 // Get the data type of the value at the current iterator
458 // position. INVALID_DATA will be returned if the iterator points to the
459 // end of the message.
460 Message::DataType GetDataType();
461
462 private:
463 // Returns true if the data type at the current iterator position
464 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
465 bool CheckDataType(int dbus_type);
466
467 // Helper function used to implement PopByte() etc.
468 bool PopBasic(int dbus_type, void *value);
469
470 // Helper function used to implement PopArray() etc.
471 bool PopContainer(int dbus_type, MessageReader* sub_reader);
472
473 // Helper function used to implement PopVariantOfByte() etc.
474 bool PopVariantOfBasic(int dbus_type, void* value);
475
476 Message* message_;
477 DBusMessageIter raw_message_iter_;
478
479 DISALLOW_COPY_AND_ASSIGN(MessageReader);
480};
481
482} // namespace dbus
483
484#endif // DBUS_MESSAGE_H_