blob: 0b9834dd2834b7a4f4289a50ab0de2cfab48f462 [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_
7#pragma once
8
9#include <string>
10#include <vector>
11#include <dbus/dbus.h>
12
13#include "base/basictypes.h"
[email protected]e146bfc2012-03-30 06:46:2014#include "dbus/file_descriptor.h"
[email protected]216ed0b2012-02-14 21:29:0615#include "dbus/object_path.h"
[email protected]a9e91492011-07-30 19:13:3116
[email protected]c033c5082012-02-09 18:14:0817namespace google {
18namespace protobuf {
19
20class MessageLite;
21
22} // namespace protobuf
23} // namespace google
24
25
[email protected]a9e91492011-07-30 19:13:3126namespace dbus {
27
28class MessageWriter;
29class MessageReader;
30
[email protected]e146bfc2012-03-30 06:46:2031// DBUS_TYPE_UNIX_FD was added in D-Bus version 1.4
32#if defined(DBUS_TYPE_UNIX_FD)
33const bool kDBusTypeUnixFdIsSupported = true;
34#else
35const bool kDBusTypeUnixFdIsSupported = false;
36#define DBUS_TYPE_UNIX_FD ((int) 'h')
37#endif
38
[email protected]06ead872011-08-24 03:32:0639// Message is the base class of D-Bus message types. Client code must use
40// sub classes such as MethodCall and Response instead.
[email protected]a9e91492011-07-30 19:13:3141//
42// The class name Message is very generic, but there should be no problem
43// as the class is inside 'dbus' namespace. We chose to name this way, as
44// libdbus defines lots of types starting with DBus, such as
45// DBusMessage. We should avoid confusion and conflict with these types.
46class Message {
47 public:
48 // The message type used in D-Bus. Redefined here so client code
49 // doesn't need to use raw D-Bus macros. DBUS_MESSAGE_TYPE_INVALID
50 // etc. are #define macros. Having an enum type here makes code a bit
51 // more type-safe.
52 enum MessageType {
53 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID,
54 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL,
55 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN,
56 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL,
57 MESSAGE_ERROR = DBUS_MESSAGE_TYPE_ERROR,
58 };
59
60 // The data type used in the D-Bus type system. See the comment at
61 // MessageType for why we are redefining data types here.
62 enum DataType {
63 INVALID_DATA = DBUS_TYPE_INVALID,
64 BYTE = DBUS_TYPE_BYTE,
65 BOOL = DBUS_TYPE_BOOLEAN,
66 INT16 = DBUS_TYPE_INT16,
67 UINT16 = DBUS_TYPE_UINT16,
68 INT32 = DBUS_TYPE_INT32,
69 UINT32 = DBUS_TYPE_UINT32,
70 INT64 = DBUS_TYPE_INT64,
71 UINT64 = DBUS_TYPE_UINT64,
72 DOUBLE = DBUS_TYPE_DOUBLE,
73 STRING = DBUS_TYPE_STRING,
74 OBJECT_PATH = DBUS_TYPE_OBJECT_PATH,
75 ARRAY = DBUS_TYPE_ARRAY,
76 STRUCT = DBUS_TYPE_STRUCT,
77 DICT_ENTRY = DBUS_TYPE_DICT_ENTRY,
78 VARIANT = DBUS_TYPE_VARIANT,
[email protected]e146bfc2012-03-30 06:46:2079 UNIX_FD = DBUS_TYPE_UNIX_FD,
[email protected]a9e91492011-07-30 19:13:3180 };
81
[email protected]a9e91492011-07-30 19:13:3182 // Returns the type of the message. Returns MESSAGE_INVALID if
83 // raw_message_ is NULL.
84 MessageType GetMessageType();
85
[email protected]06ead872011-08-24 03:32:0686 // Returns the type of the message as string like "MESSAGE_METHOD_CALL"
87 // for instance.
88 std::string GetMessageTypeAsString();
[email protected]a9e91492011-07-30 19:13:3189
[email protected]06ead872011-08-24 03:32:0690 DBusMessage* raw_message() { return raw_message_; }
[email protected]a9e91492011-07-30 19:13:3191
[email protected]9cce2d32011-08-10 22:34:0292 // Sets the destination, the path, the interface, the member, etc.
[email protected]ca72ff22012-05-23 06:55:2293 bool SetDestination(const std::string& destination);
94 bool SetPath(const ObjectPath& path);
95 bool SetInterface(const std::string& interface);
96 bool SetMember(const std::string& member);
97 bool SetErrorName(const std::string& error_name);
98 bool SetSender(const std::string& sender);
[email protected]9cce2d32011-08-10 22:34:0299 void SetSerial(uint32 serial);
100 void SetReplySerial(uint32 reply_serial);
101 // SetSignature() does not exist as we cannot do it.
102
103 // Gets the destination, the path, the interface, the member, etc.
104 // If not set, an empty string is returned.
105 std::string GetDestination();
[email protected]216ed0b2012-02-14 21:29:06106 ObjectPath GetPath();
[email protected]9cce2d32011-08-10 22:34:02107 std::string GetInterface();
108 std::string GetMember();
109 std::string GetErrorName();
110 std::string GetSender();
111 std::string GetSignature();
112 // Gets the serial and reply serial numbers. Returns 0 if not set.
113 uint32 GetSerial();
114 uint32 GetReplySerial();
115
[email protected]a9e91492011-07-30 19:13:31116 // Returns the string representation of this message. Useful for
117 // debugging.
118 std::string ToString();
119
[email protected]06ead872011-08-24 03:32:06120 protected:
121 // This class cannot be instantiated. Use sub classes instead.
122 Message();
123 virtual ~Message();
124
125 // Initializes the message with the given raw message.
126 void Init(DBusMessage* raw_message);
127
[email protected]a9e91492011-07-30 19:13:31128 private:
129 // Helper function used in ToString().
130 std::string ToStringInternal(const std::string& indent,
131 MessageReader* reader);
132
133 DBusMessage* raw_message_;
134 DISALLOW_COPY_AND_ASSIGN(Message);
135};
136
137// MessageCall is a type of message used for calling a method via D-Bus.
138class MethodCall : public Message {
139 public:
140 // Creates a method call message for the specified interface name and
141 // the method name.
142 //
143 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
144 // interface ("org.freedesktop.DBus.Introspectable"), create a method
145 // call like this:
146 //
147 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
148 //
[email protected]06ead872011-08-24 03:32:06149 // The constructor creates the internal raw message.
[email protected]a9e91492011-07-30 19:13:31150 MethodCall(const std::string& interface_name,
151 const std::string& method_name);
152
[email protected]9cce2d32011-08-10 22:34:02153 // Returns a newly created MethodCall from the given raw message of the
154 // type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the
155 // returned object. Takes the ownership of |raw_message|.
156 static MethodCall* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31157
[email protected]06ead872011-08-24 03:32:06158 private:
159 // Creates a method call message. The internal raw message is NULL.
160 // Only used internally.
161 MethodCall();
162
[email protected]a9e91492011-07-30 19:13:31163 DISALLOW_COPY_AND_ASSIGN(MethodCall);
164};
165
[email protected]3beaaa4e2011-08-23 07:29:21166// Signal is a type of message used to send a signal.
167class Signal : public Message {
168 public:
169 // Creates a signal message for the specified interface name and the
170 // method name.
171 //
172 // For instance, to send "PropertiesChanged" signal of
173 // DBUS_INTERFACE_INTROSPECTABLE interface
174 // ("org.freedesktop.DBus.Introspectable"), create a signal like this:
175 //
176 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged");
177 //
[email protected]06ead872011-08-24 03:32:06178 // The constructor creates the internal raw_message_.
[email protected]3beaaa4e2011-08-23 07:29:21179 Signal(const std::string& interface_name,
180 const std::string& method_name);
181
182 // Returns a newly created SIGNAL from the given raw message of the type
183 // DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned
184 // object. Takes the ownership of |raw_message|.
185 static Signal* FromRawMessage(DBusMessage* raw_message);
[email protected]06ead872011-08-24 03:32:06186
187 private:
188 // Creates a signal message. The internal raw message is NULL.
189 // Only used internally.
190 Signal();
191
192 DISALLOW_COPY_AND_ASSIGN(Signal);
[email protected]3beaaa4e2011-08-23 07:29:21193};
194
[email protected]a9e91492011-07-30 19:13:31195// Response is a type of message used for receiving a response from a
196// method via D-Bus.
197class Response : public Message {
198 public:
[email protected]06ead872011-08-24 03:32:06199 // Returns a newly created Response from the given raw message of the
200 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
201 // returned object. Takes the ownership of |raw_message|.
202 static Response* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31203
[email protected]9cce2d32011-08-10 22:34:02204 // Returns a newly created Response from the given method call. The
205 // caller must delete the returned object. Used for implementing
206 // exported methods.
207 static Response* FromMethodCall(MethodCall* method_call);
208
[email protected]829f0e4c2011-08-31 18:02:43209 // Returns a newly created Response with an empty payload. The caller
[email protected]06ead872011-08-24 03:32:06210 // must delete the returned object. Useful for testing.
211 static Response* CreateEmpty();
212
[email protected]7533518e2012-03-11 01:12:00213 protected:
[email protected]06ead872011-08-24 03:32:06214 // Creates a Response message. The internal raw message is NULL.
215 Response();
216
[email protected]7533518e2012-03-11 01:12:00217 private:
[email protected]a9e91492011-07-30 19:13:31218 DISALLOW_COPY_AND_ASSIGN(Response);
219};
220
[email protected]9cce2d32011-08-10 22:34:02221// ErrorResponse is a type of message used to return an error to the
222// caller of a method.
[email protected]7533518e2012-03-11 01:12:00223class ErrorResponse: public Response {
[email protected]9cce2d32011-08-10 22:34:02224 public:
[email protected]06ead872011-08-24 03:32:06225 // Returns a newly created Response from the given raw message of the
226 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
227 // returned object. Takes the ownership of |raw_message|.
228 static ErrorResponse* FromRawMessage(DBusMessage* raw_message);
[email protected]9cce2d32011-08-10 22:34:02229
230 // Returns a newly created ErrorResponse from the given method call, the
231 // error name, and the error message. The error name looks like
232 // "org.freedesktop.DBus.Error.Failed". Used for returning an error to a
233 // failed method call.
234 static ErrorResponse* FromMethodCall(MethodCall* method_call,
235 const std::string& error_name,
236 const std::string& error_message);
237
238 private:
[email protected]06ead872011-08-24 03:32:06239 // Creates an ErrorResponse message. The internal raw message is NULL.
240 ErrorResponse();
241
[email protected]9cce2d32011-08-10 22:34:02242 DISALLOW_COPY_AND_ASSIGN(ErrorResponse);
243};
244
[email protected]a9e91492011-07-30 19:13:31245// MessageWriter is used to write outgoing messages for calling methods
246// and sending signals.
247//
248// The main design goal of MessageReader and MessageWriter classes is to
249// provide a type safe API. In the past, there was a Chrome OS blocker
250// bug, that took days to fix, that would have been prevented if the API
251// was type-safe.
252//
253// For instance, instead of doing something like:
254//
255// // We shouldn't add '&' to str here, but it compiles with '&' added.
256// dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...)
257//
258// We want to do something like:
259//
260// writer.AppendString(str);
261//
262class MessageWriter {
263 public:
[email protected]9400ff82012-02-07 23:48:35264 // Data added with Append* will be written to |message|, which may be NULL
265 // to create a sub-writer for passing to OpenArray, etc.
266 explicit MessageWriter(Message* message);
[email protected]a9e91492011-07-30 19:13:31267 ~MessageWriter();
268
269 // Appends a byte to the message.
270 void AppendByte(uint8 value);
271 void AppendBool(bool value);
272 void AppendInt16(int16 value);
273 void AppendUint16(uint16 value);
274 void AppendInt32(int32 value);
275 void AppendUint32(uint32 value);
276 void AppendInt64(int64 value);
277 void AppendUint64(uint64 value);
278 void AppendDouble(double value);
279 void AppendString(const std::string& value);
[email protected]216ed0b2012-02-14 21:29:06280 void AppendObjectPath(const ObjectPath& value);
[email protected]e146bfc2012-03-30 06:46:20281 void AppendFileDescriptor(const FileDescriptor& value);
[email protected]a9e91492011-07-30 19:13:31282
283 // Opens an array. The array contents can be added to the array with
284 // |sub_writer|. The client code must close the array with
285 // CloseContainer(), once all contents are added.
286 //
287 // |signature| parameter is used to supply the D-Bus type signature of
288 // the array contents. For instance, if you want an array of strings,
289 // then you pass "s" as the signature.
290 //
291 // See the spec for details about the type signatures.
292 // http://dbus.freedesktop.org/doc/dbus-specification.html
293 // #message-protocol-signatures
294 //
295 void OpenArray(const std::string& signature, MessageWriter* sub_writer);
296 // Do the same for a variant.
297 void OpenVariant(const std::string& signature, MessageWriter* sub_writer);
298 // Do the same for Struct and dict entry. They don't need the signature.
299 void OpenStruct(MessageWriter* sub_writer);
300 void OpenDictEntry(MessageWriter* sub_writer);
301
302 // Close the container for a array/variant/struct/dict entry.
303 void CloseContainer(MessageWriter* sub_writer);
304
305 // Appends the array of bytes. Arrays of bytes are often used for
306 // exchanging binary blobs hence it's worth having a specialized
307 // function.
308 void AppendArrayOfBytes(const uint8* values, size_t length);
309
[email protected]8bc83fd2011-09-19 18:22:14310 // Appends the array of strings. Arrays of strings are often used for
311 // exchanging lists of names hence it's worth having a specialized
312 // function.
313 void AppendArrayOfStrings(const std::vector<std::string>& strings);
314
[email protected]090d8e512011-08-22 18:28:42315 // Appends the array of object paths. Arrays of object paths are often
[email protected]8bc83fd2011-09-19 18:22:14316 // used when exchanging object paths, hence it's worth having a
[email protected]090d8e512011-08-22 18:28:42317 // specialized function.
[email protected]216ed0b2012-02-14 21:29:06318 void AppendArrayOfObjectPaths(const std::vector<ObjectPath>& object_paths);
[email protected]090d8e512011-08-22 18:28:42319
[email protected]c033c5082012-02-09 18:14:08320 // Appends the protocol buffer as an array of bytes. The buffer is serialized
321 // into an array of bytes before communication, since protocol buffers are not
322 // a native dbus type. On the receiving size the array of bytes needs to be
323 // read and deserialized into a protocol buffer of the correct type. There are
324 // methods in MessageReader to assist in this. Return true on succes and fail
325 // when serialization is not successful.
326 bool AppendProtoAsArrayOfBytes(const google::protobuf::MessageLite& protobuf);
327
[email protected]a9e91492011-07-30 19:13:31328 // Appends the byte wrapped in a variant data container. Variants are
329 // widely used in D-Bus services so it's worth having a specialized
330 // function. For instance, The third parameter of
331 // "org.freedesktop.DBus.Properties.Set" is a variant.
332 void AppendVariantOfByte(uint8 value);
333 void AppendVariantOfBool(bool value);
334 void AppendVariantOfInt16(int16 value);
335 void AppendVariantOfUint16(uint16 value);
336 void AppendVariantOfInt32(int32 value);
337 void AppendVariantOfUint32(uint32 value);
338 void AppendVariantOfInt64(int64 value);
339 void AppendVariantOfUint64(uint64 value);
340 void AppendVariantOfDouble(double value);
341 void AppendVariantOfString(const std::string& value);
[email protected]216ed0b2012-02-14 21:29:06342 void AppendVariantOfObjectPath(const ObjectPath& value);
[email protected]a9e91492011-07-30 19:13:31343
344 private:
345 // Helper function used to implement AppendByte etc.
346 void AppendBasic(int dbus_type, const void* value);
347
348 // Helper function used to implement AppendVariantOfByte() etc.
349 void AppendVariantOfBasic(int dbus_type, const void* value);
350
351 Message* message_;
352 DBusMessageIter raw_message_iter_;
353 bool container_is_open_;
354
355 DISALLOW_COPY_AND_ASSIGN(MessageWriter);
356};
357
358// MessageReader is used to read incoming messages such as responses for
359// method calls.
360//
361// MessageReader manages an internal iterator to read data. All functions
362// starting with Pop advance the iterator on success.
363class MessageReader {
364 public:
[email protected]9400ff82012-02-07 23:48:35365 // The data will be read from the given |message|, which may be NULL to
366 // create a sub-reader for passing to PopArray, etc.
367 explicit MessageReader(Message* message);
[email protected]a9e91492011-07-30 19:13:31368 ~MessageReader();
369
370 // Returns true if the reader has more data to read. The function is
371 // used to iterate contents in a container like:
372 //
373 // while (reader.HasMoreData())
374 // reader.PopString(&value);
375 bool HasMoreData();
376
377 // Gets the byte at the current iterator position.
378 // Returns true and advances the iterator on success.
379 // Returns false if the data type is not a byte.
380 bool PopByte(uint8* value);
381 bool PopBool(bool* value);
382 bool PopInt16(int16* value);
383 bool PopUint16(uint16* value);
384 bool PopInt32(int32* value);
385 bool PopUint32(uint32* value);
386 bool PopInt64(int64* value);
387 bool PopUint64(uint64* value);
388 bool PopDouble(double* value);
389 bool PopString(std::string* value);
[email protected]216ed0b2012-02-14 21:29:06390 bool PopObjectPath(ObjectPath* value);
[email protected]e146bfc2012-03-30 06:46:20391 bool PopFileDescriptor(FileDescriptor* value);
[email protected]a9e91492011-07-30 19:13:31392
393 // Sets up the given message reader to read an array at the current
394 // iterator position.
395 // Returns true and advances the iterator on success.
396 // Returns false if the data type is not an array
397 bool PopArray(MessageReader* sub_reader);
398 bool PopStruct(MessageReader* sub_reader);
399 bool PopDictEntry(MessageReader* sub_reader);
400 bool PopVariant(MessageReader* sub_reader);
401
402 // Gets the array of bytes at the current iterator position.
403 // Returns true and advances the iterator on success.
404 //
405 // Arrays of bytes are often used for exchanging binary blobs hence it's
406 // worth having a specialized function.
407 //
408 // |bytes| must be copied if the contents will be referenced after the
[email protected]56d82c9c2011-09-06 20:03:24409 // MessageReader is destroyed.
[email protected]a9e91492011-07-30 19:13:31410 bool PopArrayOfBytes(uint8** bytes, size_t* length);
411
[email protected]8bc83fd2011-09-19 18:22:14412 // Gets the array of strings at the current iterator position.
413 // Returns true and advances the iterator on success.
414 //
415 // Arrays of strings are often used to communicate with D-Bus
416 // services like KWallet, hence it's worth having a specialized
417 // function.
418 bool PopArrayOfStrings(std::vector<std::string>* strings);
419
[email protected]a9e91492011-07-30 19:13:31420 // Gets the array of object paths at the current iterator position.
421 // Returns true and advances the iterator on success.
422 //
423 // Arrays of object paths are often used to communicate with D-Bus
424 // services like NetworkManager, hence it's worth having a specialized
425 // function.
[email protected]216ed0b2012-02-14 21:29:06426 bool PopArrayOfObjectPaths(std::vector<ObjectPath>* object_paths);
[email protected]a9e91492011-07-30 19:13:31427
[email protected]c033c5082012-02-09 18:14:08428 // Gets the array of bytes at the current iterator position. It then parses
429 // this binary blob into the protocol buffer supplied.
430 // Returns true and advances the iterator on success. On failure returns false
431 // and emits an error message on the source of the failure. The two most
432 // common errors come from the iterator not currently being at a byte array or
433 // the wrong type of protocol buffer is passed in and the parse fails.
434 bool PopArrayOfBytesAsProto(google::protobuf::MessageLite* protobuf);
435
[email protected]a9e91492011-07-30 19:13:31436 // Gets the byte from the variant data container at the current iterator
437 // position.
438 // Returns true and advances the iterator on success.
439 //
440 // Variants are widely used in D-Bus services so it's worth having a
441 // specialized function. For instance, The return value type of
442 // "org.freedesktop.DBus.Properties.Get" is a variant.
443 bool PopVariantOfByte(uint8* value);
444 bool PopVariantOfBool(bool* value);
445 bool PopVariantOfInt16(int16* value);
446 bool PopVariantOfUint16(uint16* value);
447 bool PopVariantOfInt32(int32* value);
448 bool PopVariantOfUint32(uint32* value);
449 bool PopVariantOfInt64(int64* value);
450 bool PopVariantOfUint64(uint64* value);
451 bool PopVariantOfDouble(double* value);
452 bool PopVariantOfString(std::string* value);
[email protected]216ed0b2012-02-14 21:29:06453 bool PopVariantOfObjectPath(ObjectPath* value);
[email protected]a9e91492011-07-30 19:13:31454
455 // Get the data type of the value at the current iterator
456 // position. INVALID_DATA will be returned if the iterator points to the
457 // end of the message.
458 Message::DataType GetDataType();
459
460 private:
461 // Returns true if the data type at the current iterator position
462 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
463 bool CheckDataType(int dbus_type);
464
465 // Helper function used to implement PopByte() etc.
466 bool PopBasic(int dbus_type, void *value);
467
468 // Helper function used to implement PopArray() etc.
469 bool PopContainer(int dbus_type, MessageReader* sub_reader);
470
471 // Helper function used to implement PopVariantOfByte() etc.
472 bool PopVariantOfBasic(int dbus_type, void* value);
473
474 Message* message_;
475 DBusMessageIter raw_message_iter_;
476
477 DISALLOW_COPY_AND_ASSIGN(MessageReader);
478};
479
480} // namespace dbus
481
482#endif // DBUS_MESSAGE_H_