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