chrome: bluetooth: hook up the AdapterAdded signal
[chromium-blink-merge.git] / dbus / message.h
blobceeb0edbf7a74f3417165f420c04539b5a8989a0
1 // 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.
5 #ifndef DBUS_MESSAGE_H_
6 #define DBUS_MESSAGE_H_
7 #pragma once
9 #include <string>
10 #include <vector>
11 #include <dbus/dbus.h>
13 #include "base/basictypes.h"
15 namespace dbus {
17 class MessageWriter;
18 class MessageReader;
20 // Message is the base class of D-Bus message types. Client code must use
21 // sub classes such as MethodCall and Response instead.
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.
27 class 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,
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,
62 // Returns the type of the message. Returns MESSAGE_INVALID if
63 // raw_message_ is NULL.
64 MessageType GetMessageType();
66 // Returns the type of the message as string like "MESSAGE_METHOD_CALL"
67 // for instance.
68 std::string GetMessageTypeAsString();
70 DBusMessage* raw_message() { return raw_message_; }
72 // 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.
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();
96 // Returns the string representation of this message. Useful for
97 // debugging.
98 std::string ToString();
100 protected:
101 // This class cannot be instantiated. Use sub classes instead.
102 Message();
103 virtual ~Message();
105 // Initializes the message with the given raw message.
106 void Init(DBusMessage* raw_message);
108 private:
109 // Helper function used in ToString().
110 std::string ToStringInternal(const std::string& indent,
111 MessageReader* reader);
113 DBusMessage* raw_message_;
114 DISALLOW_COPY_AND_ASSIGN(Message);
117 // MessageCall is a type of message used for calling a method via D-Bus.
118 class MethodCall : public Message {
119 public:
120 // Creates a method call message for the specified interface name and
121 // the method name.
123 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
124 // interface ("org.freedesktop.DBus.Introspectable"), create a method
125 // call like this:
127 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
129 // The constructor creates the internal raw message.
130 MethodCall(const std::string& interface_name,
131 const std::string& method_name);
133 // 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);
138 private:
139 // Creates a method call message. The internal raw message is NULL.
140 // Only used internally.
141 MethodCall();
143 DISALLOW_COPY_AND_ASSIGN(MethodCall);
146 // Signal is a type of message used to send a signal.
147 class Signal : public Message {
148 public:
149 // Creates a signal message for the specified interface name and the
150 // method name.
152 // For instance, to send "PropertiesChanged" signal of
153 // DBUS_INTERFACE_INTROSPECTABLE interface
154 // ("org.freedesktop.DBus.Introspectable"), create a signal like this:
156 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged");
158 // The constructor creates the internal raw_message_.
159 Signal(const std::string& interface_name,
160 const std::string& method_name);
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);
167 private:
168 // Creates a signal message. The internal raw message is NULL.
169 // Only used internally.
170 Signal();
172 DISALLOW_COPY_AND_ASSIGN(Signal);
175 // Response is a type of message used for receiving a response from a
176 // method via D-Bus.
177 class Response : public Message {
178 public:
179 // 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);
184 // 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);
189 // Returns a newly created Response with an empty payload. The caller
190 // must delete the returned object. Useful for testing.
191 static Response* CreateEmpty();
193 private:
194 // Creates a Response message. The internal raw message is NULL.
195 Response();
197 DISALLOW_COPY_AND_ASSIGN(Response);
200 // ErrorResponse is a type of message used to return an error to the
201 // caller of a method.
202 class ErrorResponse: public Message {
203 public:
204 // 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);
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);
217 private:
218 // Creates an ErrorResponse message. The internal raw message is NULL.
219 ErrorResponse();
221 DISALLOW_COPY_AND_ASSIGN(ErrorResponse);
224 // MessageWriter is used to write outgoing messages for calling methods
225 // and sending signals.
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.
232 // For instance, instead of doing something like:
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, ...)
237 // We want to do something like:
239 // writer.AppendString(str);
241 class MessageWriter {
242 public:
243 // Data added with Append* will be written to |message|.
244 MessageWriter(Message* message);
245 ~MessageWriter();
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);
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.
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.
268 // See the spec for details about the type signatures.
269 // http://dbus.freedesktop.org/doc/dbus-specification.html
270 // #message-protocol-signatures
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);
279 // Close the container for a array/variant/struct/dict entry.
280 void CloseContainer(MessageWriter* sub_writer);
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);
287 // Appends the array of strings. Arrays of strings are often used for
288 // exchanging lists of names hence it's worth having a specialized
289 // function.
290 void AppendArrayOfStrings(const std::vector<std::string>& strings);
292 // Appends the array of object paths. Arrays of object paths are often
293 // used when exchanging object paths, hence it's worth having a
294 // specialized function.
295 void AppendArrayOfObjectPaths(const std::vector<std::string>& object_paths);
297 // Appends the byte wrapped in a variant data container. Variants are
298 // widely used in D-Bus services so it's worth having a specialized
299 // function. For instance, The third parameter of
300 // "org.freedesktop.DBus.Properties.Set" is a variant.
301 void AppendVariantOfByte(uint8 value);
302 void AppendVariantOfBool(bool value);
303 void AppendVariantOfInt16(int16 value);
304 void AppendVariantOfUint16(uint16 value);
305 void AppendVariantOfInt32(int32 value);
306 void AppendVariantOfUint32(uint32 value);
307 void AppendVariantOfInt64(int64 value);
308 void AppendVariantOfUint64(uint64 value);
309 void AppendVariantOfDouble(double value);
310 void AppendVariantOfString(const std::string& value);
311 void AppendVariantOfObjectPath(const std::string& value);
313 private:
314 // Helper function used to implement AppendByte etc.
315 void AppendBasic(int dbus_type, const void* value);
317 // Helper function used to implement AppendVariantOfByte() etc.
318 void AppendVariantOfBasic(int dbus_type, const void* value);
320 Message* message_;
321 DBusMessageIter raw_message_iter_;
322 bool container_is_open_;
324 DISALLOW_COPY_AND_ASSIGN(MessageWriter);
327 // MessageReader is used to read incoming messages such as responses for
328 // method calls.
330 // MessageReader manages an internal iterator to read data. All functions
331 // starting with Pop advance the iterator on success.
332 class MessageReader {
333 public:
334 // The data will be read from the given message.
335 MessageReader(Message* message);
336 ~MessageReader();
338 // Returns true if the reader has more data to read. The function is
339 // used to iterate contents in a container like:
341 // while (reader.HasMoreData())
342 // reader.PopString(&value);
343 bool HasMoreData();
345 // Gets the byte at the current iterator position.
346 // Returns true and advances the iterator on success.
347 // Returns false if the data type is not a byte.
348 bool PopByte(uint8* value);
349 bool PopBool(bool* value);
350 bool PopInt16(int16* value);
351 bool PopUint16(uint16* value);
352 bool PopInt32(int32* value);
353 bool PopUint32(uint32* value);
354 bool PopInt64(int64* value);
355 bool PopUint64(uint64* value);
356 bool PopDouble(double* value);
357 bool PopString(std::string* value);
358 bool PopObjectPath(std::string* value);
360 // Sets up the given message reader to read an array at the current
361 // iterator position.
362 // Returns true and advances the iterator on success.
363 // Returns false if the data type is not an array
364 bool PopArray(MessageReader* sub_reader);
365 bool PopStruct(MessageReader* sub_reader);
366 bool PopDictEntry(MessageReader* sub_reader);
367 bool PopVariant(MessageReader* sub_reader);
369 // Gets the array of bytes at the current iterator position.
370 // Returns true and advances the iterator on success.
372 // Arrays of bytes are often used for exchanging binary blobs hence it's
373 // worth having a specialized function.
375 // |bytes| must be copied if the contents will be referenced after the
376 // MessageReader is destroyed.
377 bool PopArrayOfBytes(uint8** bytes, size_t* length);
379 // Gets the array of strings at the current iterator position.
380 // Returns true and advances the iterator on success.
382 // Arrays of strings are often used to communicate with D-Bus
383 // services like KWallet, hence it's worth having a specialized
384 // function.
385 bool PopArrayOfStrings(std::vector<std::string>* strings);
387 // Gets the array of object paths at the current iterator position.
388 // Returns true and advances the iterator on success.
390 // Arrays of object paths are often used to communicate with D-Bus
391 // services like NetworkManager, hence it's worth having a specialized
392 // function.
393 bool PopArrayOfObjectPaths(std::vector<std::string>* object_paths);
395 // Gets the byte from the variant data container at the current iterator
396 // position.
397 // Returns true and advances the iterator on success.
399 // Variants are widely used in D-Bus services so it's worth having a
400 // specialized function. For instance, The return value type of
401 // "org.freedesktop.DBus.Properties.Get" is a variant.
402 bool PopVariantOfByte(uint8* value);
403 bool PopVariantOfBool(bool* value);
404 bool PopVariantOfInt16(int16* value);
405 bool PopVariantOfUint16(uint16* value);
406 bool PopVariantOfInt32(int32* value);
407 bool PopVariantOfUint32(uint32* value);
408 bool PopVariantOfInt64(int64* value);
409 bool PopVariantOfUint64(uint64* value);
410 bool PopVariantOfDouble(double* value);
411 bool PopVariantOfString(std::string* value);
412 bool PopVariantOfObjectPath(std::string* value);
414 // Get the data type of the value at the current iterator
415 // position. INVALID_DATA will be returned if the iterator points to the
416 // end of the message.
417 Message::DataType GetDataType();
419 private:
420 // Returns true if the data type at the current iterator position
421 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
422 bool CheckDataType(int dbus_type);
424 // Helper function used to implement PopByte() etc.
425 bool PopBasic(int dbus_type, void *value);
427 // Helper function used to implement PopArray() etc.
428 bool PopContainer(int dbus_type, MessageReader* sub_reader);
430 // Helper function used to implement PopVariantOfByte() etc.
431 bool PopVariantOfBasic(int dbus_type, void* value);
433 Message* message_;
434 DBusMessageIter raw_message_iter_;
436 DISALLOW_COPY_AND_ASSIGN(MessageReader);
439 } // namespace dbus
441 #endif // DBUS_MESSAGE_H_