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