Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #ifndef DBUS_MESSAGE_H_ | |
| 6 #define DBUS_MESSAGE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 #include <dbus/dbus.h> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 | |
| 15 namespace dbus { | |
| 16 | |
| 17 class MessageWriter; | |
| 18 class MessageReader; | |
| 19 | |
| 20 // Message is the base class of D-Bus message types. Client code should | |
| 21 // usually use sub classes such as MethodCall and Response instead. | |
| 22 // | |
| 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 // | |
| 34 // MessageType GetMessageType() vs. int GetMessageType() | |
| 35 // | |
| 36 // A new message type may be introduced in the future, but it should not | |
| 37 // affect our code, as long as we are only using MessageType members | |
| 38 // (i.e. don't try to cast random integers to this enum). | |
|
stevenjb
2011/07/27 22:08:52
I would remove the last paragraph, I think the fir
satorux1
2011/07/27 22:22:58
Done.
| |
| 39 enum MessageType { | |
| 40 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID, | |
| 41 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL, | |
| 42 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN, | |
| 43 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL, | |
| 44 MESSAGE_ERROR = DBUS_MESSAGE_TYPE_ERROR, | |
| 45 }; | |
| 46 | |
| 47 // The data type used in the D-Bus type system. See the comment at | |
| 48 // MessageType for why we are redefined data types here. | |
| 49 enum DataType { | |
| 50 INVALID_DATA = DBUS_TYPE_INVALID, | |
| 51 BYTE = DBUS_TYPE_BYTE, | |
| 52 BOOL = DBUS_TYPE_BOOLEAN, | |
| 53 INT16 = DBUS_TYPE_INT16, | |
| 54 UINT16 = DBUS_TYPE_UINT16, | |
| 55 INT32 = DBUS_TYPE_INT32, | |
| 56 UINT32 = DBUS_TYPE_UINT32, | |
| 57 INT64 = DBUS_TYPE_INT64, | |
| 58 UINT64 = DBUS_TYPE_UINT64, | |
| 59 DOUBLE = DBUS_TYPE_DOUBLE, | |
| 60 STRING = DBUS_TYPE_STRING, | |
| 61 OBJECT_PATH = DBUS_TYPE_OBJECT_PATH, | |
| 62 ARRAY = DBUS_TYPE_ARRAY, | |
| 63 STRUCT = DBUS_TYPE_STRUCT, | |
| 64 DICT_ENTRY = DBUS_TYPE_DICT_ENTRY, | |
| 65 VARIANT = DBUS_TYPE_VARIANT, | |
| 66 }; | |
| 67 | |
| 68 // Creates a Message. The internal raw message is NULL until it's set | |
| 69 // from outside by reset_raw_message(). | |
| 70 Message(); | |
| 71 virtual ~Message(); | |
| 72 | |
| 73 // Returns the type of the message. Returns MESSAGE_INVALID if | |
| 74 // raw_message_ is NULL. | |
| 75 MessageType GetMessageType(); | |
| 76 | |
| 77 DBusMessage* raw_message() { return raw_message_; } | |
| 78 | |
| 79 // Resets raw_message_ with the given raw message. Takes the ownership | |
| 80 // of raw_message. raw_message_ will be unref'ed in the destructor. | |
| 81 void reset_raw_message(DBusMessage* raw_message); | |
| 82 | |
| 83 // Returns the string representation of this message. Useful for | |
| 84 // debugging. The returned string is similar to dbus-send's output. | |
| 85 std::string ToString(); | |
| 86 | |
| 87 private: | |
| 88 // Helper function used in ToString(). | |
| 89 std::string ToStringInternal(const std::string& indent, | |
| 90 MessageReader* reader); | |
| 91 | |
| 92 DBusMessage* raw_message_; | |
| 93 DISALLOW_COPY_AND_ASSIGN(Message); | |
| 94 }; | |
| 95 | |
| 96 // MessageCall is a type of message used for calling a method via D-Bus. | |
| 97 class MethodCall : public Message { | |
| 98 public: | |
| 99 // Creates a method call message for the specified interface name and | |
| 100 // the method name. | |
| 101 // | |
| 102 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE | |
| 103 // interface ("org.freedesktop.DBus.Introspectable"), create a method | |
| 104 // call like this: | |
| 105 // | |
| 106 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get"); | |
| 107 // | |
| 108 // The constructor creates the internal raw_message_, so the client | |
| 109 // doesn't need to set this by reset_raw_message(). | |
| 110 MethodCall(const std::string& interface_name, | |
| 111 const std::string& method_name); | |
| 112 | |
| 113 const std::string& interface_name() { return interface_name_; } | |
| 114 const std::string& method_name() { return method_name_; } | |
| 115 | |
| 116 private: | |
| 117 friend class ObjectProxy; | |
| 118 | |
| 119 // Sets the service name. This will be handled by the object proxy. | |
| 120 void SetServiceName(const std::string& service_name); | |
| 121 // Sets the object path. This will be handled by the object proxy. | |
| 122 void SetObjectPath(const std::string& object_path); | |
| 123 | |
| 124 std::string interface_name_; | |
| 125 std::string method_name_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(MethodCall); | |
| 128 }; | |
| 129 | |
| 130 // Response is a type of message used for receiving a response from a | |
| 131 // method via D-Bus. | |
| 132 class Response : public Message { | |
| 133 public: | |
| 134 // Creates a Response message. The internal raw message is | |
| 135 // NULL. ObjectProxy will set it properly once it gets a response from | |
| 136 // the server. | |
| 137 Response(); | |
| 138 | |
| 139 private: | |
| 140 DISALLOW_COPY_AND_ASSIGN(Response); | |
| 141 }; | |
| 142 | |
| 143 // MessageWriter is used to write outgoing messages for calling methods | |
| 144 // and sending signals. | |
| 145 class MessageWriter { | |
| 146 public: | |
| 147 // The data will be written into the given message. | |
| 148 MessageWriter(Message* message); | |
| 149 ~MessageWriter(); | |
| 150 | |
| 151 // Appends a byte to the message. | |
| 152 void AppendByte(uint8 value); | |
| 153 void AppendBool(bool value); | |
| 154 void AppendInt16(int16 value); | |
| 155 void AppendUint16(uint16 value); | |
| 156 void AppendInt32(int32 value); | |
| 157 void AppendUint32(uint32 value); | |
| 158 void AppendInt64(int64 value); | |
| 159 void AppendUint64(uint64 value); | |
| 160 void AppendDouble(double value); | |
| 161 void AppendString(const std::string& value); | |
| 162 void AppendObjectPath(const std::string& value); | |
| 163 | |
| 164 // Opens an array. The array contents can be added to the array with | |
| 165 // |sub_writer|. The client code should close the array with | |
| 166 // CloseContainer(), once all contents are added. | |
| 167 // | |
| 168 // |signature| parameter is used to supply the D-Bus type signature of | |
| 169 // the array contents. For instance, if you want an array of strings, | |
| 170 // then you pass "s" as the signature. | |
| 171 // | |
| 172 // See the spec for details about the type signatures. | |
| 173 // http://dbus.freedesktop.org/doc/dbus-specification.html | |
| 174 // #message-protocol-signatures | |
| 175 // | |
| 176 // Ideally, client shouldn't need to supply the signature string, but | |
| 177 // the underlying D-Bus library requires us to supply this before | |
| 178 // appending contents to array and variant. It's technically possible | |
| 179 // for us to design API that doesn't require the signature but it will | |
| 180 // complicate the implementation so we decided to have the signature | |
| 181 // parameter. Hopefully, variants are less used in request messages from | |
| 182 // client side than response message from server side, so this should | |
| 183 // not be a big issue. | |
|
stevenjb
2011/07/27 22:08:52
We can probably omit the above paragraph. While it
satorux1
2011/07/27 22:22:58
Good point. Moved to .cc file so I don't forget ab
| |
| 184 // | |
| 185 void OpenArray(const std::string& signature, MessageWriter* sub_writer); | |
| 186 // Do the same for a variant. | |
| 187 void OpenVariant(const std::string& signature, MessageWriter* sub_writer); | |
| 188 // Do the same for Struct and dict entry. They don't need the signature. | |
| 189 void OpenStruct(MessageWriter* sub_writer); | |
| 190 void OpenDictEntry(MessageWriter* sub_writer); | |
| 191 | |
| 192 // Close the container for a array/variant/struct/dict entry. | |
| 193 void CloseContainer(MessageWriter* sub_writer); | |
| 194 | |
| 195 // Appends the array of bytes. Arrays of bytes are often used for | |
| 196 // exchanging binary blobs hence it's worth having a specialized | |
| 197 // function. | |
| 198 void AppendArrayOfBytes(const uint8* values, size_t length); | |
| 199 | |
| 200 // Appends the byte wrapped in a variant data container. Variants are | |
| 201 // widely used in D-Bus services so it's worth having a specialized | |
| 202 // function. For instance, The third parameter of | |
| 203 // "org.freedesktop.DBus.Properties.Set" is a variant. | |
| 204 void AppendVariantOfByte(uint8 value); | |
| 205 void AppendVariantOfBool(bool value); | |
| 206 void AppendVariantOfInt16(int16 value); | |
| 207 void AppendVariantOfUint16(uint16 value); | |
| 208 void AppendVariantOfInt32(int32 value); | |
| 209 void AppendVariantOfUint32(uint32 value); | |
| 210 void AppendVariantOfInt64(int64 value); | |
| 211 void AppendVariantOfUint64(uint64 value); | |
| 212 void AppendVariantOfDouble(double value); | |
| 213 void AppendVariantOfString(const std::string& value); | |
| 214 void AppendVariantOfObjectPath(const std::string& value); | |
| 215 | |
| 216 private: | |
| 217 // Helper function used to implement AppendByte etc. | |
| 218 void AppendBasic(int dbus_type, const void* value); | |
| 219 | |
| 220 // Helper function used to implement AppendVariantOfByte() etc. | |
| 221 void AppendVariantOfBasic(int dbus_type, const void* value); | |
| 222 | |
| 223 Message* message_; | |
| 224 DBusMessageIter raw_message_iter_; | |
| 225 bool container_is_open_; | |
| 226 | |
| 227 DISALLOW_COPY_AND_ASSIGN(MessageWriter); | |
| 228 }; | |
| 229 | |
| 230 // MessageReader is used to read incoming messages such as responses for | |
| 231 // method calls. | |
| 232 // | |
| 233 // MessageReader manages an internal iterator to read data. All functions | |
| 234 // starting with Pop advance the iterator on success. | |
| 235 // | |
| 236 // The main design goal of MessageReader and MessageWriter classes is to | |
| 237 // provide a type safe API. In the past, there was a Chrome OS blocker | |
| 238 // bug, that took days to fix, that would have been prevented if the API | |
| 239 // was type-safe. | |
| 240 // | |
| 241 // For instance, instead of doing something like: | |
| 242 // | |
| 243 // // We shouldn't add '&' to str here, but it compiles with '&' added. | |
| 244 // dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...) | |
| 245 // | |
| 246 // We want to do something like: | |
| 247 // | |
| 248 // writer.AppendString(str); | |
| 249 // | |
| 250 class MessageReader { | |
| 251 public: | |
| 252 // The data will be read from the given message. | |
| 253 MessageReader(Message* message); | |
| 254 ~MessageReader(); | |
| 255 | |
| 256 // Returns true if the reader has more data to read. The function is | |
| 257 // used to iterate contents in a container like: | |
| 258 // | |
| 259 // while (reader.HasMoreData()) | |
| 260 // reader.PopString(&value); | |
| 261 bool HasMoreData(); | |
| 262 | |
| 263 // Gets the byte at the current iterator position. On success, advances | |
| 264 // the iterator and returns true. It's an error if the actual data type | |
| 265 // is not a byte, so false will be returned in this case. | |
| 266 bool PopByte(uint8* value); | |
| 267 bool PopBool(bool* value); | |
| 268 bool PopInt16(int16* value); | |
| 269 bool PopUint16(uint16* value); | |
| 270 bool PopInt32(int32* value); | |
| 271 bool PopUint32(uint32* value); | |
| 272 bool PopInt64(int64* value); | |
| 273 bool PopUint64(uint64* value); | |
| 274 bool PopDouble(double* value); | |
| 275 bool PopString(std::string* value); | |
| 276 bool PopObjectPath(std::string* value); | |
| 277 | |
| 278 // Sets up the given message reader to read an array at the current | |
| 279 // iterator position. On success, advances the iterator and returns | |
| 280 // true. It's an error if the actual data type is not an array, so false | |
| 281 // will be returned in this case. | |
| 282 bool PopArray(MessageReader* sub_reader); | |
| 283 // The following functions do the same for other container types. | |
| 284 bool PopStruct(MessageReader* sub_reader); | |
| 285 bool PopDictEntry(MessageReader* sub_reader); | |
| 286 bool PopVariant(MessageReader* sub_reader); | |
| 287 | |
| 288 // Gets the array of bytes at the current iterator position. On success, | |
| 289 // advances the iterator and returns true. Arrays of bytes are often | |
| 290 // used for exchanging binary blobs hence it's worth having a | |
| 291 // specialized function. | |
| 292 bool PopArrayOfBytes(std::vector<uint8>* bytes); | |
| 293 | |
| 294 // Gets the array of object paths at the current iterator position. On | |
| 295 // success, advances the iterator and returns true. Arrays of object | |
| 296 // paths are often used to communicate with D-Bus services like | |
| 297 // NetworkManager, hence it's worth having a specialized function. | |
| 298 bool PopArrayOfObjectPaths(std::vector<std::string>* object_paths); | |
| 299 | |
| 300 // Gets the byte from the variant data container at the current iterator | |
| 301 // position. On success, returns true and advances the | |
| 302 // iterator. Variants are widely used in D-Bus services so it's worth | |
| 303 // having a specialized function. For instance, The return value type of | |
| 304 // "org.freedesktop.DBus.Properties.Get" is a variant. | |
| 305 bool PopVariantOfByte(uint8* value); | |
| 306 bool PopVariantOfBool(bool* value); | |
| 307 bool PopVariantOfInt16(int16* value); | |
| 308 bool PopVariantOfUint16(uint16* value); | |
| 309 bool PopVariantOfInt32(int32* value); | |
| 310 bool PopVariantOfUint32(uint32* value); | |
| 311 bool PopVariantOfInt64(int64* value); | |
| 312 bool PopVariantOfUint64(uint64* value); | |
| 313 bool PopVariantOfDouble(double* value); | |
| 314 bool PopVariantOfString(std::string* value); | |
| 315 bool PopVariantOfObjectPath(std::string* value); | |
| 316 | |
| 317 // Get the data type of the value at the current iterator | |
| 318 // position. INVALID_DATA will be returned if the iterator points to the | |
| 319 // end of the message. | |
| 320 Message::DataType GetDataType(); | |
| 321 | |
| 322 private: | |
| 323 // Returns true if the data type at the current iterator position | |
| 324 // matches the given D-Bus type, such as DBUS_TYPE_BYTE. | |
| 325 bool CheckDataType(int dbus_type); | |
| 326 | |
| 327 // Helper function used to implement PopByte() etc. | |
| 328 bool PopBasic(int dbus_type, void *value); | |
| 329 | |
| 330 // Helper function used to implement PopArray() etc. | |
| 331 bool PopContainer(int dbus_type, MessageReader* sub_reader); | |
| 332 | |
| 333 // Helper function used to implement PopVariantOfByte() etc. | |
| 334 bool PopVariantOfBasic(int dbus_type, void* value); | |
| 335 | |
| 336 Message* message_; | |
| 337 DBusMessageIter raw_message_iter_; | |
| 338 | |
| 339 DISALLOW_COPY_AND_ASSIGN(MessageReader); | |
| 340 }; | |
| 341 | |
| 342 } // namespace dbus | |
| 343 | |
| 344 #endif // DBUS_MESSAGE_H_ | |
| OLD | NEW |