blob: 72200fc299bb8aa8a747be406859c46997a1ee9a [file] [log] [blame]
[email protected]a9e91492011-07-30 19:13:311// Copyright (c) 2011 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.
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
15namespace dbus {
16
17class MessageWriter;
18class MessageReader;
19
[email protected]06ead872011-08-24 03:32:0620// Message is the base class of D-Bus message types. Client code must use
21// sub classes such as MethodCall and Response instead.
[email protected]a9e91492011-07-30 19:13:3122//
23// The class name Message is very generic, but there should be no problem
24// as the class is inside 'dbus' namespace. We chose to name this way, as
25// libdbus defines lots of types starting with DBus, such as
26// DBusMessage. We should avoid confusion and conflict with these types.
27class Message {
28 public:
29 // The message type used in D-Bus. Redefined here so client code
30 // doesn't need to use raw D-Bus macros. DBUS_MESSAGE_TYPE_INVALID
31 // etc. are #define macros. Having an enum type here makes code a bit
32 // more type-safe.
33 enum MessageType {
34 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID,
35 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL,
36 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN,
37 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL,
38 MESSAGE_ERROR = DBUS_MESSAGE_TYPE_ERROR,
39 };
40
41 // The data type used in the D-Bus type system. See the comment at
42 // MessageType for why we are redefining data types here.
43 enum DataType {
44 INVALID_DATA = DBUS_TYPE_INVALID,
45 BYTE = DBUS_TYPE_BYTE,
46 BOOL = DBUS_TYPE_BOOLEAN,
47 INT16 = DBUS_TYPE_INT16,
48 UINT16 = DBUS_TYPE_UINT16,
49 INT32 = DBUS_TYPE_INT32,
50 UINT32 = DBUS_TYPE_UINT32,
51 INT64 = DBUS_TYPE_INT64,
52 UINT64 = DBUS_TYPE_UINT64,
53 DOUBLE = DBUS_TYPE_DOUBLE,
54 STRING = DBUS_TYPE_STRING,
55 OBJECT_PATH = DBUS_TYPE_OBJECT_PATH,
56 ARRAY = DBUS_TYPE_ARRAY,
57 STRUCT = DBUS_TYPE_STRUCT,
58 DICT_ENTRY = DBUS_TYPE_DICT_ENTRY,
59 VARIANT = DBUS_TYPE_VARIANT,
60 };
61
[email protected]a9e91492011-07-30 19:13:3162 // Returns the type of the message. Returns MESSAGE_INVALID if
63 // raw_message_ is NULL.
64 MessageType GetMessageType();
65
[email protected]06ead872011-08-24 03:32:0666 // Returns the type of the message as string like "MESSAGE_METHOD_CALL"
67 // for instance.
68 std::string GetMessageTypeAsString();
[email protected]a9e91492011-07-30 19:13:3169
[email protected]06ead872011-08-24 03:32:0670 DBusMessage* raw_message() { return raw_message_; }
[email protected]a9e91492011-07-30 19:13:3171
[email protected]9cce2d32011-08-10 22:34:0272 // Sets the destination, the path, the interface, the member, etc.
73 void SetDestination(const std::string& destination);
74 void SetPath(const std::string& path);
75 void SetInterface(const std::string& interface);
76 void SetMember(const std::string& member);
77 void SetErrorName(const std::string& error_name);
78 void SetSender(const std::string& sender);
79 void SetSerial(uint32 serial);
80 void SetReplySerial(uint32 reply_serial);
81 // SetSignature() does not exist as we cannot do it.
82
83 // Gets the destination, the path, the interface, the member, etc.
84 // If not set, an empty string is returned.
85 std::string GetDestination();
86 std::string GetPath();
87 std::string GetInterface();
88 std::string GetMember();
89 std::string GetErrorName();
90 std::string GetSender();
91 std::string GetSignature();
92 // Gets the serial and reply serial numbers. Returns 0 if not set.
93 uint32 GetSerial();
94 uint32 GetReplySerial();
95
[email protected]a9e91492011-07-30 19:13:3196 // Returns the string representation of this message. Useful for
97 // debugging.
98 std::string ToString();
99
[email protected]06ead872011-08-24 03:32:06100 protected:
101 // This class cannot be instantiated. Use sub classes instead.
102 Message();
103 virtual ~Message();
104
105 // Initializes the message with the given raw message.
106 void Init(DBusMessage* raw_message);
107
[email protected]a9e91492011-07-30 19:13:31108 private:
109 // Helper function used in ToString().
110 std::string ToStringInternal(const std::string& indent,
111 MessageReader* reader);
112
113 DBusMessage* raw_message_;
114 DISALLOW_COPY_AND_ASSIGN(Message);
115};
116
117// MessageCall is a type of message used for calling a method via D-Bus.
118class MethodCall : public Message {
119 public:
120 // Creates a method call message for the specified interface name and
121 // the method name.
122 //
123 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
124 // interface ("org.freedesktop.DBus.Introspectable"), create a method
125 // call like this:
126 //
127 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
128 //
[email protected]06ead872011-08-24 03:32:06129 // The constructor creates the internal raw message.
[email protected]a9e91492011-07-30 19:13:31130 MethodCall(const std::string& interface_name,
131 const std::string& method_name);
132
[email protected]9cce2d32011-08-10 22:34:02133 // Returns a newly created MethodCall from the given raw message of the
134 // type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the
135 // returned object. Takes the ownership of |raw_message|.
136 static MethodCall* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31137
[email protected]06ead872011-08-24 03:32:06138 private:
139 // Creates a method call message. The internal raw message is NULL.
140 // Only used internally.
141 MethodCall();
142
[email protected]a9e91492011-07-30 19:13:31143 DISALLOW_COPY_AND_ASSIGN(MethodCall);
144};
145
[email protected]3beaaa4e2011-08-23 07:29:21146// Signal is a type of message used to send a signal.
147class Signal : public Message {
148 public:
149 // Creates a signal message for the specified interface name and the
150 // method name.
151 //
152 // For instance, to send "PropertiesChanged" signal of
153 // DBUS_INTERFACE_INTROSPECTABLE interface
154 // ("org.freedesktop.DBus.Introspectable"), create a signal like this:
155 //
156 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged");
157 //
[email protected]06ead872011-08-24 03:32:06158 // The constructor creates the internal raw_message_.
[email protected]3beaaa4e2011-08-23 07:29:21159 Signal(const std::string& interface_name,
160 const std::string& method_name);
161
162 // Returns a newly created SIGNAL from the given raw message of the type
163 // DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned
164 // object. Takes the ownership of |raw_message|.
165 static Signal* FromRawMessage(DBusMessage* raw_message);
[email protected]06ead872011-08-24 03:32:06166
167 private:
168 // Creates a signal message. The internal raw message is NULL.
169 // Only used internally.
170 Signal();
171
172 DISALLOW_COPY_AND_ASSIGN(Signal);
[email protected]3beaaa4e2011-08-23 07:29:21173};
174
[email protected]a9e91492011-07-30 19:13:31175// Response is a type of message used for receiving a response from a
176// method via D-Bus.
177class Response : public Message {
178 public:
[email protected]06ead872011-08-24 03:32:06179 // Returns a newly created Response from the given raw message of the
180 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
181 // returned object. Takes the ownership of |raw_message|.
182 static Response* FromRawMessage(DBusMessage* raw_message);
[email protected]a9e91492011-07-30 19:13:31183
[email protected]9cce2d32011-08-10 22:34:02184 // Returns a newly created Response from the given method call. The
185 // caller must delete the returned object. Used for implementing
186 // exported methods.
187 static Response* FromMethodCall(MethodCall* method_call);
188
[email protected]829f0e4c2011-08-31 18:02:43189 // Returns a newly created Response with an empty payload. The caller
[email protected]06ead872011-08-24 03:32:06190 // must delete the returned object. Useful for testing.
191 static Response* CreateEmpty();
192
[email protected]a9e91492011-07-30 19:13:31193 private:
[email protected]06ead872011-08-24 03:32:06194 // Creates a Response message. The internal raw message is NULL.
195 Response();
196
[email protected]a9e91492011-07-30 19:13:31197 DISALLOW_COPY_AND_ASSIGN(Response);
198};
199
[email protected]9cce2d32011-08-10 22:34:02200// ErrorResponse is a type of message used to return an error to the
201// caller of a method.
202class ErrorResponse: public Message {
203 public:
[email protected]06ead872011-08-24 03:32:06204 // Returns a newly created Response from the given raw message of the
205 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the
206 // returned object. Takes the ownership of |raw_message|.
207 static ErrorResponse* FromRawMessage(DBusMessage* raw_message);
[email protected]9cce2d32011-08-10 22:34:02208
209 // Returns a newly created ErrorResponse from the given method call, the
210 // error name, and the error message. The error name looks like
211 // "org.freedesktop.DBus.Error.Failed". Used for returning an error to a
212 // failed method call.
213 static ErrorResponse* FromMethodCall(MethodCall* method_call,
214 const std::string& error_name,
215 const std::string& error_message);
216
217 private:
[email protected]06ead872011-08-24 03:32:06218 // Creates an ErrorResponse message. The internal raw message is NULL.
219 ErrorResponse();
220
[email protected]9cce2d32011-08-10 22:34:02221 DISALLOW_COPY_AND_ASSIGN(ErrorResponse);
222};
223
[email protected]a9e91492011-07-30 19:13:31224// MessageWriter is used to write outgoing messages for calling methods
225// and sending signals.
226//
227// The main design goal of MessageReader and MessageWriter classes is to
228// provide a type safe API. In the past, there was a Chrome OS blocker
229// bug, that took days to fix, that would have been prevented if the API
230// was type-safe.
231//
232// For instance, instead of doing something like:
233//
234// // We shouldn't add '&' to str here, but it compiles with '&' added.
235// dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...)
236//
237// We want to do something like:
238//
239// writer.AppendString(str);
240//
241class MessageWriter {
242 public:
243 // Data added with Append* will be written to |message|.
244 MessageWriter(Message* message);
245 ~MessageWriter();
246
247 // Appends a byte to the message.
248 void AppendByte(uint8 value);
249 void AppendBool(bool value);
250 void AppendInt16(int16 value);
251 void AppendUint16(uint16 value);
252 void AppendInt32(int32 value);
253 void AppendUint32(uint32 value);
254 void AppendInt64(int64 value);
255 void AppendUint64(uint64 value);
256 void AppendDouble(double value);
257 void AppendString(const std::string& value);
258 void AppendObjectPath(const std::string& value);
259
260 // Opens an array. The array contents can be added to the array with
261 // |sub_writer|. The client code must close the array with
262 // CloseContainer(), once all contents are added.
263 //
264 // |signature| parameter is used to supply the D-Bus type signature of
265 // the array contents. For instance, if you want an array of strings,
266 // then you pass "s" as the signature.
267 //
268 // See the spec for details about the type signatures.
269 // http://dbus.freedesktop.org/doc/dbus-specification.html
270 // #message-protocol-signatures
271 //
272 void OpenArray(const std::string& signature, MessageWriter* sub_writer);
273 // Do the same for a variant.
274 void OpenVariant(const std::string& signature, MessageWriter* sub_writer);
275 // Do the same for Struct and dict entry. They don't need the signature.
276 void OpenStruct(MessageWriter* sub_writer);
277 void OpenDictEntry(MessageWriter* sub_writer);
278
279 // Close the container for a array/variant/struct/dict entry.
280 void CloseContainer(MessageWriter* sub_writer);
281
282 // Appends the array of bytes. Arrays of bytes are often used for
283 // exchanging binary blobs hence it's worth having a specialized
284 // function.
285 void AppendArrayOfBytes(const uint8* values, size_t length);
286
[email protected]090d8e512011-08-22 18:28:42287 // Appends the array of object paths. Arrays of object paths are often
288 // used to exchanging object paths, hence it's worth having a
289 // specialized function.
290 void AppendArrayOfObjectPaths(const std::vector<std::string>& object_paths);
291
[email protected]a9e91492011-07-30 19:13:31292 // Appends the byte wrapped in a variant data container. Variants are
293 // widely used in D-Bus services so it's worth having a specialized
294 // function. For instance, The third parameter of
295 // "org.freedesktop.DBus.Properties.Set" is a variant.
296 void AppendVariantOfByte(uint8 value);
297 void AppendVariantOfBool(bool value);
298 void AppendVariantOfInt16(int16 value);
299 void AppendVariantOfUint16(uint16 value);
300 void AppendVariantOfInt32(int32 value);
301 void AppendVariantOfUint32(uint32 value);
302 void AppendVariantOfInt64(int64 value);
303 void AppendVariantOfUint64(uint64 value);
304 void AppendVariantOfDouble(double value);
305 void AppendVariantOfString(const std::string& value);
306 void AppendVariantOfObjectPath(const std::string& value);
307
308 private:
309 // Helper function used to implement AppendByte etc.
310 void AppendBasic(int dbus_type, const void* value);
311
312 // Helper function used to implement AppendVariantOfByte() etc.
313 void AppendVariantOfBasic(int dbus_type, const void* value);
314
315 Message* message_;
316 DBusMessageIter raw_message_iter_;
317 bool container_is_open_;
318
319 DISALLOW_COPY_AND_ASSIGN(MessageWriter);
320};
321
322// MessageReader is used to read incoming messages such as responses for
323// method calls.
324//
325// MessageReader manages an internal iterator to read data. All functions
326// starting with Pop advance the iterator on success.
327class MessageReader {
328 public:
329 // The data will be read from the given message.
330 MessageReader(Message* message);
331 ~MessageReader();
332
333 // Returns true if the reader has more data to read. The function is
334 // used to iterate contents in a container like:
335 //
336 // while (reader.HasMoreData())
337 // reader.PopString(&value);
338 bool HasMoreData();
339
340 // Gets the byte at the current iterator position.
341 // Returns true and advances the iterator on success.
342 // Returns false if the data type is not a byte.
343 bool PopByte(uint8* value);
344 bool PopBool(bool* value);
345 bool PopInt16(int16* value);
346 bool PopUint16(uint16* value);
347 bool PopInt32(int32* value);
348 bool PopUint32(uint32* value);
349 bool PopInt64(int64* value);
350 bool PopUint64(uint64* value);
351 bool PopDouble(double* value);
352 bool PopString(std::string* value);
353 bool PopObjectPath(std::string* value);
354
355 // Sets up the given message reader to read an array at the current
356 // iterator position.
357 // Returns true and advances the iterator on success.
358 // Returns false if the data type is not an array
359 bool PopArray(MessageReader* sub_reader);
360 bool PopStruct(MessageReader* sub_reader);
361 bool PopDictEntry(MessageReader* sub_reader);
362 bool PopVariant(MessageReader* sub_reader);
363
364 // Gets the array of bytes at the current iterator position.
365 // Returns true and advances the iterator on success.
366 //
367 // Arrays of bytes are often used for exchanging binary blobs hence it's
368 // worth having a specialized function.
369 //
370 // |bytes| must be copied if the contents will be referenced after the
[email protected]56d82c9c2011-09-06 20:03:24371 // MessageReader is destroyed.
[email protected]a9e91492011-07-30 19:13:31372 bool PopArrayOfBytes(uint8** bytes, size_t* length);
373
374 // Gets the array of object paths at the current iterator position.
375 // Returns true and advances the iterator on success.
376 //
377 // Arrays of object paths are often used to communicate with D-Bus
378 // services like NetworkManager, hence it's worth having a specialized
379 // function.
380 bool PopArrayOfObjectPaths(std::vector<std::string>* object_paths);
381
382 // Gets the byte from the variant data container at the current iterator
383 // position.
384 // Returns true and advances the iterator on success.
385 //
386 // Variants are widely used in D-Bus services so it's worth having a
387 // specialized function. For instance, The return value type of
388 // "org.freedesktop.DBus.Properties.Get" is a variant.
389 bool PopVariantOfByte(uint8* value);
390 bool PopVariantOfBool(bool* value);
391 bool PopVariantOfInt16(int16* value);
392 bool PopVariantOfUint16(uint16* value);
393 bool PopVariantOfInt32(int32* value);
394 bool PopVariantOfUint32(uint32* value);
395 bool PopVariantOfInt64(int64* value);
396 bool PopVariantOfUint64(uint64* value);
397 bool PopVariantOfDouble(double* value);
398 bool PopVariantOfString(std::string* value);
399 bool PopVariantOfObjectPath(std::string* value);
400
401 // Get the data type of the value at the current iterator
402 // position. INVALID_DATA will be returned if the iterator points to the
403 // end of the message.
404 Message::DataType GetDataType();
405
406 private:
407 // Returns true if the data type at the current iterator position
408 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
409 bool CheckDataType(int dbus_type);
410
411 // Helper function used to implement PopByte() etc.
412 bool PopBasic(int dbus_type, void *value);
413
414 // Helper function used to implement PopArray() etc.
415 bool PopContainer(int dbus_type, MessageReader* sub_reader);
416
417 // Helper function used to implement PopVariantOfByte() etc.
418 bool PopVariantOfBasic(int dbus_type, void* value);
419
420 Message* message_;
421 DBusMessageIter raw_message_iter_;
422
423 DISALLOW_COPY_AND_ASSIGN(MessageReader);
424};
425
426} // namespace dbus
427
428#endif // DBUS_MESSAGE_H_