blob: 54227a0185cb49714cb04287f2c48d29af7c0517 [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"
14
[email protected]c033c5082012-02-09 18:14:0815namespace google {
16namespace protobuf {
17
18class MessageLite;
19
20} // namespace protobuf
21} // namespace google
22
23
[email protected]a9e91492011-07-30 19:13:3124namespace dbus {
25
26class MessageWriter;
27class MessageReader;
28
[email protected]06ead872011-08-24 03:32:0629// Message is the base class of D-Bus message types. Client code must use
30// sub classes such as MethodCall and Response instead.
[email protected]a9e91492011-07-30 19:13:3131//
32// The class name Message is very generic, but there should be no problem
33// as the class is inside 'dbus' namespace. We chose to name this way, as
34// libdbus defines lots of types starting with DBus, such as
35// DBusMessage. We should avoid confusion and conflict with these types.
36class Message {
37 public:
38 // The message type used in D-Bus. Redefined here so client code
39 // doesn't need to use raw D-Bus macros. DBUS_MESSAGE_TYPE_INVALID
40 // etc. are #define macros. Having an enum type here makes code a bit
41 // more type-safe.
42 enum MessageType {
43 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID,
44 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL,
45 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN,
46 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL,
47 MESSAGE_ERROR = DBUS_MESSAGE_TYPE_ERROR,
48 };
49
50 // The data type used in the D-Bus type system. See the comment at
51 // MessageType for why we are redefining data types here.
52 enum DataType {
53 INVALID_DATA = DBUS_TYPE_INVALID,
54 BYTE = DBUS_TYPE_BYTE,
55 BOOL = DBUS_TYPE_BOOLEAN,
56 INT16 = DBUS_TYPE_INT16,
57 UINT16 = DBUS_TYPE_UINT16,
58 INT32 = DBUS_TYPE_INT32,
59 UINT32 = DBUS_TYPE_UINT32,
60 INT64 = DBUS_TYPE_INT64,
61 UINT64 = DBUS_TYPE_UINT64,
62 DOUBLE = DBUS_TYPE_DOUBLE,
63 STRING = DBUS_TYPE_STRING,
64 OBJECT_PATH = DBUS_TYPE_OBJECT_PATH,
65 ARRAY = DBUS_TYPE_ARRAY,
66 STRUCT = DBUS_TYPE_STRUCT,
67 DICT_ENTRY = DBUS_TYPE_DICT_ENTRY,
68 VARIANT = DBUS_TYPE_VARIANT,
69 };
70
[email protected]a9e91492011-07-30 19:13:3171 // Returns the type of the message. Returns MESSAGE_INVALID if
72 // raw_message_ is NULL.
73 MessageType GetMessageType();
74
[email protected]06ead872011-08-24 03:32:0675 // Returns the type of the message as string like "MESSAGE_METHOD_CALL"
76 // for instance.
77 std::string GetMessageTypeAsString();
[email protected]a9e91492011-07-30 19:13:3178
[email protected]06ead872011-08-24 03:32:0679 DBusMessage* raw_message() { return raw_message_; }
[email protected]a9e91492011-07-30 19:13:3180
[email protected]9cce2d32011-08-10 22:34:0281 // Sets the destination, the path, the interface, the member, etc.
82 void SetDestination(const std::string& destination);
83 void SetPath(const std::string& path);
84 void SetInterface(const std::string& interface);
85 void SetMember(const std::string& member);
86 void SetErrorName(const std::string& error_name);
87 void SetSender(const std::string& sender);
88 void SetSerial(uint32 serial);
89 void SetReplySerial(uint32 reply_serial);
90 // SetSignature() does not exist as we cannot do it.
91
92 // Gets the destination, the path, the interface, the member, etc.
93 // If not set, an empty string is returned.
94 std::string GetDestination();
95 std::string GetPath();
96 std::string GetInterface();
97 std::string GetMember();
98 std::string GetErrorName();
99 std::string GetSender();
100 std::string GetSignature();
101 // Gets the serial and reply serial numbers. Returns 0 if not set.
102 uint32 GetSerial();
103 uint32 GetReplySerial();
104
[email protected]a9e91492011-07-30 19:13:31105 // Returns the string representation of this message. Useful for
106 // debugging.
107 std::string ToString();
108
[email protected]06ead872011-08-24 03:32:06109 protected:
110 // This class cannot be instantiated. Use sub classes instead.
111 Message();
112 virtual ~Message();
113
114 // Initializes the message with the given raw message.
115 void Init(DBusMessage* raw_message);
116
[email protected]a9e91492011-07-30 19:13:31117 private:
118 // Helper function used in ToString().
119 std::string ToStringInternal(const std::string& indent,
120 MessageReader* reader);
121
122 DBusMessage* raw_message_;
123 DISALLOW_COPY_AND_ASSIGN(Message);
124};
125
126// MessageCall is a type of message used for calling a method via D-Bus.
127class MethodCall : public Message {
128 public:
129 // Creates a method call message for the specified interface name and
130 // the method name.
131 //
132 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
133 // interface ("org.freedesktop.DBus.Introspectable"), create a method
134 // call like this:
135 //
136 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
137 //
[email protected]06ead872011-08-24 03:32:06138 // The constructor creates the internal raw message.
[email protected]a9e91492011-07-30 19:13:31139 MethodCall(const std::string& interface_name,
140 const std::string& method_name);
141
[email protected]9cce2d32011-08-10 22:34:02142 // Returns a newly created MethodCall from the given raw message of the
143 // type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the
144 // returned object. Takes the ownership of |raw_message|.
145 static MethodCall* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31146
[email protected]06ead872011-08-24 03:32:06147 private:
148 // Creates a method call message. The internal raw message is NULL.
149 // Only used internally.
150 MethodCall();
151
[email protected]a9e91492011-07-30 19:13:31152 DISALLOW_COPY_AND_ASSIGN(MethodCall);
153};
154
[email protected]3beaaa4e2011-08-23 07:29:21155// Signal is a type of message used to send a signal.
156class Signal : public Message {
157 public:
158 // Creates a signal message for the specified interface name and the
159 // method name.
160 //
161 // For instance, to send "PropertiesChanged" signal of
162 // DBUS_INTERFACE_INTROSPECTABLE interface
163 // ("org.freedesktop.DBus.Introspectable"), create a signal like this:
164 //
165 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged");
166 //
[email protected]06ead872011-08-24 03:32:06167 // The constructor creates the internal raw_message_.
[email protected]3beaaa4e2011-08-23 07:29:21168 Signal(const std::string& interface_name,
169 const std::string& method_name);
170
171 // Returns a newly created SIGNAL from the given raw message of the type
172 // DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned
173 // object. Takes the ownership of |raw_message|.
174 static Signal* FromRawMessage(DBusMessage* raw_message);
[email protected]06ead872011-08-24 03:32:06175
176 private:
177 // Creates a signal message. The internal raw message is NULL.
178 // Only used internally.
179 Signal();
180
181 DISALLOW_COPY_AND_ASSIGN(Signal);
[email protected]3beaaa4e2011-08-23 07:29:21182};
183
[email protected]a9e91492011-07-30 19:13:31184// Response is a type of message used for receiving a response from a
185// method via D-Bus.
186class Response : public Message {
187 public:
[email protected]06ead872011-08-24 03:32:06188 // Returns a newly created Response from the given raw message of the
189 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
190 // returned object. Takes the ownership of |raw_message|.
191 static Response* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31192
[email protected]9cce2d32011-08-10 22:34:02193 // Returns a newly created Response from the given method call. The
194 // caller must delete the returned object. Used for implementing
195 // exported methods.
196 static Response* FromMethodCall(MethodCall* method_call);
197
[email protected]829f0e4c2011-08-31 18:02:43198 // Returns a newly created Response with an empty payload. The caller
[email protected]06ead872011-08-24 03:32:06199 // must delete the returned object. Useful for testing.
200 static Response* CreateEmpty();
201
[email protected]a9e91492011-07-30 19:13:31202 private:
[email protected]06ead872011-08-24 03:32:06203 // Creates a Response message. The internal raw message is NULL.
204 Response();
205
[email protected]a9e91492011-07-30 19:13:31206 DISALLOW_COPY_AND_ASSIGN(Response);
207};
208
[email protected]9cce2d32011-08-10 22:34:02209// ErrorResponse is a type of message used to return an error to the
210// caller of a method.
211class ErrorResponse: public Message {
212 public:
[email protected]06ead872011-08-24 03:32:06213 // Returns a newly created Response from the given raw message of the
214 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
215 // returned object. Takes the ownership of |raw_message|.
216 static ErrorResponse* FromRawMessage(DBusMessage* raw_message);
[email protected]9cce2d32011-08-10 22:34:02217
218 // Returns a newly created ErrorResponse from the given method call, the
219 // error name, and the error message. The error name looks like
220 // "org.freedesktop.DBus.Error.Failed". Used for returning an error to a
221 // failed method call.
222 static ErrorResponse* FromMethodCall(MethodCall* method_call,
223 const std::string& error_name,
224 const std::string& error_message);
225
226 private:
[email protected]06ead872011-08-24 03:32:06227 // Creates an ErrorResponse message. The internal raw message is NULL.
228 ErrorResponse();
229
[email protected]9cce2d32011-08-10 22:34:02230 DISALLOW_COPY_AND_ASSIGN(ErrorResponse);
231};
232
[email protected]a9e91492011-07-30 19:13:31233// MessageWriter is used to write outgoing messages for calling methods
234// and sending signals.
235//
236// The main design goal of MessageReader and MessageWriter classes is to
237// provide a type safe API. In the past, there was a Chrome OS blocker
238// bug, that took days to fix, that would have been prevented if the API
239// was type-safe.
240//
241// For instance, instead of doing something like:
242//
243// // We shouldn't add '&' to str here, but it compiles with '&' added.
244// dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...)
245//
246// We want to do something like:
247//
248// writer.AppendString(str);
249//
250class MessageWriter {
251 public:
[email protected]9400ff82012-02-07 23:48:35252 // Data added with Append* will be written to |message|, which may be NULL
253 // to create a sub-writer for passing to OpenArray, etc.
254 explicit MessageWriter(Message* message);
[email protected]a9e91492011-07-30 19:13:31255 ~MessageWriter();
256
257 // Appends a byte to the message.
258 void AppendByte(uint8 value);
259 void AppendBool(bool value);
260 void AppendInt16(int16 value);
261 void AppendUint16(uint16 value);
262 void AppendInt32(int32 value);
263 void AppendUint32(uint32 value);
264 void AppendInt64(int64 value);
265 void AppendUint64(uint64 value);
266 void AppendDouble(double value);
267 void AppendString(const std::string& value);
268 void AppendObjectPath(const std::string& value);
269
270 // Opens an array. The array contents can be added to the array with
271 // |sub_writer|. The client code must close the array with
272 // CloseContainer(), once all contents are added.
273 //
274 // |signature| parameter is used to supply the D-Bus type signature of
275 // the array contents. For instance, if you want an array of strings,
276 // then you pass "s" as the signature.
277 //
278 // See the spec for details about the type signatures.
279 // http://dbus.freedesktop.org/doc/dbus-specification.html
280 // #message-protocol-signatures
281 //
282 void OpenArray(const std::string& signature, MessageWriter* sub_writer);
283 // Do the same for a variant.
284 void OpenVariant(const std::string& signature, MessageWriter* sub_writer);
285 // Do the same for Struct and dict entry. They don't need the signature.
286 void OpenStruct(MessageWriter* sub_writer);
287 void OpenDictEntry(MessageWriter* sub_writer);
288
289 // Close the container for a array/variant/struct/dict entry.
290 void CloseContainer(MessageWriter* sub_writer);
291
292 // Appends the array of bytes. Arrays of bytes are often used for
293 // exchanging binary blobs hence it's worth having a specialized
294 // function.
295 void AppendArrayOfBytes(const uint8* values, size_t length);
296
[email protected]8bc83fd2011-09-19 18:22:14297 // Appends the array of strings. Arrays of strings are often used for
298 // exchanging lists of names hence it's worth having a specialized
299 // function.
300 void AppendArrayOfStrings(const std::vector<std::string>& strings);
301
[email protected]090d8e512011-08-22 18:28:42302 // Appends the array of object paths. Arrays of object paths are often
[email protected]8bc83fd2011-09-19 18:22:14303 // used when exchanging object paths, hence it's worth having a
[email protected]090d8e512011-08-22 18:28:42304 // specialized function.
305 void AppendArrayOfObjectPaths(const std::vector<std::string>& object_paths);
306
[email protected]c033c5082012-02-09 18:14:08307 // Appends the protocol buffer as an array of bytes. The buffer is serialized
308 // into an array of bytes before communication, since protocol buffers are not
309 // a native dbus type. On the receiving size the array of bytes needs to be
310 // read and deserialized into a protocol buffer of the correct type. There are
311 // methods in MessageReader to assist in this. Return true on succes and fail
312 // when serialization is not successful.
313 bool AppendProtoAsArrayOfBytes(const google::protobuf::MessageLite& protobuf);
314
[email protected]a9e91492011-07-30 19:13:31315 // Appends the byte wrapped in a variant data container. Variants are
316 // widely used in D-Bus services so it's worth having a specialized
317 // function. For instance, The third parameter of
318 // "org.freedesktop.DBus.Properties.Set" is a variant.
319 void AppendVariantOfByte(uint8 value);
320 void AppendVariantOfBool(bool value);
321 void AppendVariantOfInt16(int16 value);
322 void AppendVariantOfUint16(uint16 value);
323 void AppendVariantOfInt32(int32 value);
324 void AppendVariantOfUint32(uint32 value);
325 void AppendVariantOfInt64(int64 value);
326 void AppendVariantOfUint64(uint64 value);
327 void AppendVariantOfDouble(double value);
328 void AppendVariantOfString(const std::string& value);
329 void AppendVariantOfObjectPath(const std::string& value);
330
331 private:
332 // Helper function used to implement AppendByte etc.
333 void AppendBasic(int dbus_type, const void* value);
334
335 // Helper function used to implement AppendVariantOfByte() etc.
336 void AppendVariantOfBasic(int dbus_type, const void* value);
337
338 Message* message_;
339 DBusMessageIter raw_message_iter_;
340 bool container_is_open_;
341
342 DISALLOW_COPY_AND_ASSIGN(MessageWriter);
343};
344
345// MessageReader is used to read incoming messages such as responses for
346// method calls.
347//
348// MessageReader manages an internal iterator to read data. All functions
349// starting with Pop advance the iterator on success.
350class MessageReader {
351 public:
[email protected]9400ff82012-02-07 23:48:35352 // The data will be read from the given |message|, which may be NULL to
353 // create a sub-reader for passing to PopArray, etc.
354 explicit MessageReader(Message* message);
[email protected]a9e91492011-07-30 19:13:31355 ~MessageReader();
356
357 // Returns true if the reader has more data to read. The function is
358 // used to iterate contents in a container like:
359 //
360 // while (reader.HasMoreData())
361 // reader.PopString(&value);
362 bool HasMoreData();
363
364 // Gets the byte at the current iterator position.
365 // Returns true and advances the iterator on success.
366 // Returns false if the data type is not a byte.
367 bool PopByte(uint8* value);
368 bool PopBool(bool* value);
369 bool PopInt16(int16* value);
370 bool PopUint16(uint16* value);
371 bool PopInt32(int32* value);
372 bool PopUint32(uint32* value);
373 bool PopInt64(int64* value);
374 bool PopUint64(uint64* value);
375 bool PopDouble(double* value);
376 bool PopString(std::string* value);
377 bool PopObjectPath(std::string* value);
378
379 // Sets up the given message reader to read an array at the current
380 // iterator position.
381 // Returns true and advances the iterator on success.
382 // Returns false if the data type is not an array
383 bool PopArray(MessageReader* sub_reader);
384 bool PopStruct(MessageReader* sub_reader);
385 bool PopDictEntry(MessageReader* sub_reader);
386 bool PopVariant(MessageReader* sub_reader);
387
388 // Gets the array of bytes at the current iterator position.
389 // Returns true and advances the iterator on success.
390 //
391 // Arrays of bytes are often used for exchanging binary blobs hence it's
392 // worth having a specialized function.
393 //
394 // |bytes| must be copied if the contents will be referenced after the
[email protected]56d82c9c2011-09-06 20:03:24395 // MessageReader is destroyed.
[email protected]a9e91492011-07-30 19:13:31396 bool PopArrayOfBytes(uint8** bytes, size_t* length);
397
[email protected]8bc83fd2011-09-19 18:22:14398 // Gets the array of strings at the current iterator position.
399 // Returns true and advances the iterator on success.
400 //
401 // Arrays of strings are often used to communicate with D-Bus
402 // services like KWallet, hence it's worth having a specialized
403 // function.
404 bool PopArrayOfStrings(std::vector<std::string>* strings);
405
[email protected]a9e91492011-07-30 19:13:31406 // Gets the array of object paths at the current iterator position.
407 // Returns true and advances the iterator on success.
408 //
409 // Arrays of object paths are often used to communicate with D-Bus
410 // services like NetworkManager, hence it's worth having a specialized
411 // function.
412 bool PopArrayOfObjectPaths(std::vector<std::string>* object_paths);
413
[email protected]c033c5082012-02-09 18:14:08414 // Gets the array of bytes at the current iterator position. It then parses
415 // this binary blob into the protocol buffer supplied.
416 // Returns true and advances the iterator on success. On failure returns false
417 // and emits an error message on the source of the failure. The two most
418 // common errors come from the iterator not currently being at a byte array or
419 // the wrong type of protocol buffer is passed in and the parse fails.
420 bool PopArrayOfBytesAsProto(google::protobuf::MessageLite* protobuf);
421
[email protected]a9e91492011-07-30 19:13:31422 // Gets the byte from the variant data container at the current iterator
423 // position.
424 // Returns true and advances the iterator on success.
425 //
426 // Variants are widely used in D-Bus services so it's worth having a
427 // specialized function. For instance, The return value type of
428 // "org.freedesktop.DBus.Properties.Get" is a variant.
429 bool PopVariantOfByte(uint8* value);
430 bool PopVariantOfBool(bool* value);
431 bool PopVariantOfInt16(int16* value);
432 bool PopVariantOfUint16(uint16* value);
433 bool PopVariantOfInt32(int32* value);
434 bool PopVariantOfUint32(uint32* value);
435 bool PopVariantOfInt64(int64* value);
436 bool PopVariantOfUint64(uint64* value);
437 bool PopVariantOfDouble(double* value);
438 bool PopVariantOfString(std::string* value);
439 bool PopVariantOfObjectPath(std::string* value);
440
441 // Get the data type of the value at the current iterator
442 // position. INVALID_DATA will be returned if the iterator points to the
443 // end of the message.
444 Message::DataType GetDataType();
445
446 private:
447 // Returns true if the data type at the current iterator position
448 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
449 bool CheckDataType(int dbus_type);
450
451 // Helper function used to implement PopByte() etc.
452 bool PopBasic(int dbus_type, void *value);
453
454 // Helper function used to implement PopArray() etc.
455 bool PopContainer(int dbus_type, MessageReader* sub_reader);
456
457 // Helper function used to implement PopVariantOfByte() etc.
458 bool PopVariantOfBasic(int dbus_type, void* value);
459
460 Message* message_;
461 DBusMessageIter raw_message_iter_;
462
463 DISALLOW_COPY_AND_ASSIGN(MessageReader);
464};
465
466} // namespace dbus
467
468#endif // DBUS_MESSAGE_H_