blob: 38887f8c7bea03aa6a8dfb56310e7523f11e7a41 [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
[email protected]6df1b9592012-06-07 16:41:26117 // debugging. The output is truncated as needed (ex. strings are truncated
118 // if longer than a certain limit defined in the .cc file).
[email protected]a9e91492011-07-30 19:13:31119 std::string ToString();
120
[email protected]06ead872011-08-24 03:32:06121 protected:
122 // This class cannot be instantiated. Use sub classes instead.
123 Message();
124 virtual ~Message();
125
126 // Initializes the message with the given raw message.
127 void Init(DBusMessage* raw_message);
128
[email protected]a9e91492011-07-30 19:13:31129 private:
130 // Helper function used in ToString().
131 std::string ToStringInternal(const std::string& indent,
132 MessageReader* reader);
133
134 DBusMessage* raw_message_;
135 DISALLOW_COPY_AND_ASSIGN(Message);
136};
137
138// MessageCall is a type of message used for calling a method via D-Bus.
139class MethodCall : public Message {
140 public:
141 // Creates a method call message for the specified interface name and
142 // the method name.
143 //
144 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
145 // interface ("org.freedesktop.DBus.Introspectable"), create a method
146 // call like this:
147 //
148 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
149 //
[email protected]06ead872011-08-24 03:32:06150 // The constructor creates the internal raw message.
[email protected]a9e91492011-07-30 19:13:31151 MethodCall(const std::string& interface_name,
152 const std::string& method_name);
153
[email protected]9cce2d32011-08-10 22:34:02154 // Returns a newly created MethodCall from the given raw message of the
155 // type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the
156 // returned object. Takes the ownership of |raw_message|.
157 static MethodCall* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31158
[email protected]06ead872011-08-24 03:32:06159 private:
160 // Creates a method call message. The internal raw message is NULL.
161 // Only used internally.
162 MethodCall();
163
[email protected]a9e91492011-07-30 19:13:31164 DISALLOW_COPY_AND_ASSIGN(MethodCall);
165};
166
[email protected]3beaaa4e2011-08-23 07:29:21167// Signal is a type of message used to send a signal.
168class Signal : public Message {
169 public:
170 // Creates a signal message for the specified interface name and the
171 // method name.
172 //
173 // For instance, to send "PropertiesChanged" signal of
174 // DBUS_INTERFACE_INTROSPECTABLE interface
175 // ("org.freedesktop.DBus.Introspectable"), create a signal like this:
176 //
177 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged");
178 //
[email protected]06ead872011-08-24 03:32:06179 // The constructor creates the internal raw_message_.
[email protected]3beaaa4e2011-08-23 07:29:21180 Signal(const std::string& interface_name,
181 const std::string& method_name);
182
183 // Returns a newly created SIGNAL from the given raw message of the type
184 // DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned
185 // object. Takes the ownership of |raw_message|.
186 static Signal* FromRawMessage(DBusMessage* raw_message);
[email protected]06ead872011-08-24 03:32:06187
188 private:
189 // Creates a signal message. The internal raw message is NULL.
190 // Only used internally.
191 Signal();
192
193 DISALLOW_COPY_AND_ASSIGN(Signal);
[email protected]3beaaa4e2011-08-23 07:29:21194};
195
[email protected]a9e91492011-07-30 19:13:31196// Response is a type of message used for receiving a response from a
197// method via D-Bus.
198class Response : public Message {
199 public:
[email protected]06ead872011-08-24 03:32:06200 // Returns a newly created Response from the given raw message of the
201 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
202 // returned object. Takes the ownership of |raw_message|.
203 static Response* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31204
[email protected]9cce2d32011-08-10 22:34:02205 // Returns a newly created Response from the given method call. The
206 // caller must delete the returned object. Used for implementing
207 // exported methods.
208 static Response* FromMethodCall(MethodCall* method_call);
209
[email protected]829f0e4c2011-08-31 18:02:43210 // Returns a newly created Response with an empty payload. The caller
[email protected]06ead872011-08-24 03:32:06211 // must delete the returned object. Useful for testing.
212 static Response* CreateEmpty();
213
[email protected]7533518e2012-03-11 01:12:00214 protected:
[email protected]06ead872011-08-24 03:32:06215 // Creates a Response message. The internal raw message is NULL.
216 Response();
217
[email protected]7533518e2012-03-11 01:12:00218 private:
[email protected]a9e91492011-07-30 19:13:31219 DISALLOW_COPY_AND_ASSIGN(Response);
220};
221
[email protected]9cce2d32011-08-10 22:34:02222// ErrorResponse is a type of message used to return an error to the
223// caller of a method.
[email protected]7533518e2012-03-11 01:12:00224class ErrorResponse: public Response {
[email protected]9cce2d32011-08-10 22:34:02225 public:
[email protected]06ead872011-08-24 03:32:06226 // Returns a newly created Response from the given raw message of the
227 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
228 // returned object. Takes the ownership of |raw_message|.
229 static ErrorResponse* FromRawMessage(DBusMessage* raw_message);
[email protected]9cce2d32011-08-10 22:34:02230
231 // Returns a newly created ErrorResponse from the given method call, the
232 // error name, and the error message. The error name looks like
233 // "org.freedesktop.DBus.Error.Failed". Used for returning an error to a
234 // failed method call.
235 static ErrorResponse* FromMethodCall(MethodCall* method_call,
236 const std::string& error_name,
237 const std::string& error_message);
238
239 private:
[email protected]06ead872011-08-24 03:32:06240 // Creates an ErrorResponse message. The internal raw message is NULL.
241 ErrorResponse();
242
[email protected]9cce2d32011-08-10 22:34:02243 DISALLOW_COPY_AND_ASSIGN(ErrorResponse);
244};
245
[email protected]a9e91492011-07-30 19:13:31246// MessageWriter is used to write outgoing messages for calling methods
247// and sending signals.
248//
249// The main design goal of MessageReader and MessageWriter classes is to
250// provide a type safe API. In the past, there was a Chrome OS blocker
251// bug, that took days to fix, that would have been prevented if the API
252// was type-safe.
253//
254// For instance, instead of doing something like:
255//
256// // We shouldn't add '&' to str here, but it compiles with '&' added.
257// dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...)
258//
259// We want to do something like:
260//
261// writer.AppendString(str);
262//
263class MessageWriter {
264 public:
[email protected]9400ff82012-02-07 23:48:35265 // Data added with Append* will be written to |message|, which may be NULL
266 // to create a sub-writer for passing to OpenArray, etc.
267 explicit MessageWriter(Message* message);
[email protected]a9e91492011-07-30 19:13:31268 ~MessageWriter();
269
270 // Appends a byte to the message.
271 void AppendByte(uint8 value);
272 void AppendBool(bool value);
273 void AppendInt16(int16 value);
274 void AppendUint16(uint16 value);
275 void AppendInt32(int32 value);
276 void AppendUint32(uint32 value);
277 void AppendInt64(int64 value);
278 void AppendUint64(uint64 value);
279 void AppendDouble(double value);
280 void AppendString(const std::string& value);
[email protected]216ed0b2012-02-14 21:29:06281 void AppendObjectPath(const ObjectPath& value);
[email protected]e146bfc2012-03-30 06:46:20282 void AppendFileDescriptor(const FileDescriptor& value);
[email protected]a9e91492011-07-30 19:13:31283
284 // Opens an array. The array contents can be added to the array with
285 // |sub_writer|. The client code must close the array with
286 // CloseContainer(), once all contents are added.
287 //
288 // |signature| parameter is used to supply the D-Bus type signature of
289 // the array contents. For instance, if you want an array of strings,
290 // then you pass "s" as the signature.
291 //
292 // See the spec for details about the type signatures.
293 // http://dbus.freedesktop.org/doc/dbus-specification.html
294 // #message-protocol-signatures
295 //
296 void OpenArray(const std::string& signature, MessageWriter* sub_writer);
297 // Do the same for a variant.
298 void OpenVariant(const std::string& signature, MessageWriter* sub_writer);
299 // Do the same for Struct and dict entry. They don't need the signature.
300 void OpenStruct(MessageWriter* sub_writer);
301 void OpenDictEntry(MessageWriter* sub_writer);
302
303 // Close the container for a array/variant/struct/dict entry.
304 void CloseContainer(MessageWriter* sub_writer);
305
306 // Appends the array of bytes. Arrays of bytes are often used for
307 // exchanging binary blobs hence it's worth having a specialized
308 // function.
309 void AppendArrayOfBytes(const uint8* values, size_t length);
310
[email protected]8bc83fd2011-09-19 18:22:14311 // Appends the array of strings. Arrays of strings are often used for
312 // exchanging lists of names hence it's worth having a specialized
313 // function.
314 void AppendArrayOfStrings(const std::vector<std::string>& strings);
315
[email protected]090d8e512011-08-22 18:28:42316 // Appends the array of object paths. Arrays of object paths are often
[email protected]8bc83fd2011-09-19 18:22:14317 // used when exchanging object paths, hence it's worth having a
[email protected]090d8e512011-08-22 18:28:42318 // specialized function.
[email protected]216ed0b2012-02-14 21:29:06319 void AppendArrayOfObjectPaths(const std::vector<ObjectPath>& object_paths);
[email protected]090d8e512011-08-22 18:28:42320
[email protected]c033c5082012-02-09 18:14:08321 // Appends the protocol buffer as an array of bytes. The buffer is serialized
322 // into an array of bytes before communication, since protocol buffers are not
323 // a native dbus type. On the receiving size the array of bytes needs to be
324 // read and deserialized into a protocol buffer of the correct type. There are
325 // methods in MessageReader to assist in this. Return true on succes and fail
326 // when serialization is not successful.
327 bool AppendProtoAsArrayOfBytes(const google::protobuf::MessageLite& protobuf);
328
[email protected]a9e91492011-07-30 19:13:31329 // Appends the byte wrapped in a variant data container. Variants are
330 // widely used in D-Bus services so it's worth having a specialized
331 // function. For instance, The third parameter of
332 // "org.freedesktop.DBus.Properties.Set" is a variant.
333 void AppendVariantOfByte(uint8 value);
334 void AppendVariantOfBool(bool value);
335 void AppendVariantOfInt16(int16 value);
336 void AppendVariantOfUint16(uint16 value);
337 void AppendVariantOfInt32(int32 value);
338 void AppendVariantOfUint32(uint32 value);
339 void AppendVariantOfInt64(int64 value);
340 void AppendVariantOfUint64(uint64 value);
341 void AppendVariantOfDouble(double value);
342 void AppendVariantOfString(const std::string& value);
[email protected]216ed0b2012-02-14 21:29:06343 void AppendVariantOfObjectPath(const ObjectPath& value);
[email protected]a9e91492011-07-30 19:13:31344
345 private:
346 // Helper function used to implement AppendByte etc.
347 void AppendBasic(int dbus_type, const void* value);
348
349 // Helper function used to implement AppendVariantOfByte() etc.
350 void AppendVariantOfBasic(int dbus_type, const void* value);
351
352 Message* message_;
353 DBusMessageIter raw_message_iter_;
354 bool container_is_open_;
355
356 DISALLOW_COPY_AND_ASSIGN(MessageWriter);
357};
358
359// MessageReader is used to read incoming messages such as responses for
360// method calls.
361//
362// MessageReader manages an internal iterator to read data. All functions
363// starting with Pop advance the iterator on success.
364class MessageReader {
365 public:
[email protected]9400ff82012-02-07 23:48:35366 // The data will be read from the given |message|, which may be NULL to
367 // create a sub-reader for passing to PopArray, etc.
368 explicit MessageReader(Message* message);
[email protected]a9e91492011-07-30 19:13:31369 ~MessageReader();
370
371 // Returns true if the reader has more data to read. The function is
372 // used to iterate contents in a container like:
373 //
374 // while (reader.HasMoreData())
375 // reader.PopString(&value);
376 bool HasMoreData();
377
378 // Gets the byte at the current iterator position.
379 // Returns true and advances the iterator on success.
380 // Returns false if the data type is not a byte.
381 bool PopByte(uint8* value);
382 bool PopBool(bool* value);
383 bool PopInt16(int16* value);
384 bool PopUint16(uint16* value);
385 bool PopInt32(int32* value);
386 bool PopUint32(uint32* value);
387 bool PopInt64(int64* value);
388 bool PopUint64(uint64* value);
389 bool PopDouble(double* value);
390 bool PopString(std::string* value);
[email protected]216ed0b2012-02-14 21:29:06391 bool PopObjectPath(ObjectPath* value);
[email protected]e146bfc2012-03-30 06:46:20392 bool PopFileDescriptor(FileDescriptor* value);
[email protected]a9e91492011-07-30 19:13:31393
394 // Sets up the given message reader to read an array at the current
395 // iterator position.
396 // Returns true and advances the iterator on success.
397 // Returns false if the data type is not an array
398 bool PopArray(MessageReader* sub_reader);
399 bool PopStruct(MessageReader* sub_reader);
400 bool PopDictEntry(MessageReader* sub_reader);
401 bool PopVariant(MessageReader* sub_reader);
402
403 // Gets the array of bytes at the current iterator position.
404 // Returns true and advances the iterator on success.
405 //
406 // Arrays of bytes are often used for exchanging binary blobs hence it's
407 // worth having a specialized function.
408 //
409 // |bytes| must be copied if the contents will be referenced after the
[email protected]56d82c9c2011-09-06 20:03:24410 // MessageReader is destroyed.
[email protected]a9e91492011-07-30 19:13:31411 bool PopArrayOfBytes(uint8** bytes, size_t* length);
412
[email protected]8bc83fd2011-09-19 18:22:14413 // Gets the array of strings at the current iterator position.
414 // Returns true and advances the iterator on success.
415 //
416 // Arrays of strings are often used to communicate with D-Bus
417 // services like KWallet, hence it's worth having a specialized
418 // function.
419 bool PopArrayOfStrings(std::vector<std::string>* strings);
420
[email protected]a9e91492011-07-30 19:13:31421 // Gets the array of object paths at the current iterator position.
422 // Returns true and advances the iterator on success.
423 //
424 // Arrays of object paths are often used to communicate with D-Bus
425 // services like NetworkManager, hence it's worth having a specialized
426 // function.
[email protected]216ed0b2012-02-14 21:29:06427 bool PopArrayOfObjectPaths(std::vector<ObjectPath>* object_paths);
[email protected]a9e91492011-07-30 19:13:31428
[email protected]c033c5082012-02-09 18:14:08429 // Gets the array of bytes at the current iterator position. It then parses
430 // this binary blob into the protocol buffer supplied.
431 // Returns true and advances the iterator on success. On failure returns false
432 // and emits an error message on the source of the failure. The two most
433 // common errors come from the iterator not currently being at a byte array or
434 // the wrong type of protocol buffer is passed in and the parse fails.
435 bool PopArrayOfBytesAsProto(google::protobuf::MessageLite* protobuf);
436
[email protected]a9e91492011-07-30 19:13:31437 // Gets the byte from the variant data container at the current iterator
438 // position.
439 // Returns true and advances the iterator on success.
440 //
441 // Variants are widely used in D-Bus services so it's worth having a
442 // specialized function. For instance, The return value type of
443 // "org.freedesktop.DBus.Properties.Get" is a variant.
444 bool PopVariantOfByte(uint8* value);
445 bool PopVariantOfBool(bool* value);
446 bool PopVariantOfInt16(int16* value);
447 bool PopVariantOfUint16(uint16* value);
448 bool PopVariantOfInt32(int32* value);
449 bool PopVariantOfUint32(uint32* value);
450 bool PopVariantOfInt64(int64* value);
451 bool PopVariantOfUint64(uint64* value);
452 bool PopVariantOfDouble(double* value);
453 bool PopVariantOfString(std::string* value);
[email protected]216ed0b2012-02-14 21:29:06454 bool PopVariantOfObjectPath(ObjectPath* value);
[email protected]a9e91492011-07-30 19:13:31455
456 // Get the data type of the value at the current iterator
457 // position. INVALID_DATA will be returned if the iterator points to the
458 // end of the message.
459 Message::DataType GetDataType();
460
461 private:
462 // Returns true if the data type at the current iterator position
463 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
464 bool CheckDataType(int dbus_type);
465
466 // Helper function used to implement PopByte() etc.
467 bool PopBasic(int dbus_type, void *value);
468
469 // Helper function used to implement PopArray() etc.
470 bool PopContainer(int dbus_type, MessageReader* sub_reader);
471
472 // Helper function used to implement PopVariantOfByte() etc.
473 bool PopVariantOfBasic(int dbus_type, void* value);
474
475 Message* message_;
476 DBusMessageIter raw_message_iter_;
477
478 DISALLOW_COPY_AND_ASSIGN(MessageReader);
479};
480
481} // namespace dbus
482
483#endif // DBUS_MESSAGE_H_