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