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, so but there should be no |
| 24 // problem as the class is inside 'dbus' namespace. We chose to name this |
| 25 // way, as 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. |
| 30 // Redefined here so client code doesn't need to use raw D-Bus macros. |
| 31 enum MessageType { |
| 32 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID, |
| 33 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL, |
| 34 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN, |
| 35 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL, |
| 36 MESSAGE_ERROR = DBUS_MESSAGE_TYPE_ERROR, |
| 37 }; |
| 38 |
| 39 // The data type used in the D-Bus type system. |
| 40 // Redefined here so client code doesn't need to use raw D-Bus macros. |
| 41 enum DataType { |
| 42 INVALID_DATA = DBUS_TYPE_INVALID, |
| 43 BYTE = DBUS_TYPE_BYTE, |
| 44 BOOL = DBUS_TYPE_BOOLEAN, |
| 45 INT16 = DBUS_TYPE_INT16, |
| 46 UINT16 = DBUS_TYPE_UINT16, |
| 47 INT32 = DBUS_TYPE_INT32, |
| 48 UINT32 = DBUS_TYPE_UINT32, |
| 49 INT64 = DBUS_TYPE_INT64, |
| 50 UINT64 = DBUS_TYPE_UINT64, |
| 51 DOUBLE = DBUS_TYPE_DOUBLE, |
| 52 STRING = DBUS_TYPE_STRING, |
| 53 OBJECT_PATH = DBUS_TYPE_OBJECT_PATH, |
| 54 ARRAY = DBUS_TYPE_ARRAY, |
| 55 STRUCT = DBUS_TYPE_STRUCT, |
| 56 DICT_ENTRY = DBUS_TYPE_DICT_ENTRY, |
| 57 VARIANT = DBUS_TYPE_VARIANT, |
| 58 }; |
| 59 |
| 60 // Creates a Message. The internal raw message is NULL until it's set |
| 61 // from outside by reset_raw_message(). |
| 62 Message(); |
| 63 virtual ~Message(); |
| 64 |
| 65 // Returns the type of the message. |
| 66 MessageType GetMessageType(); |
| 67 |
| 68 DBusMessage* raw_message() { return raw_message_; } |
| 69 |
| 70 // Resets raw_message_ with the given raw message. Takes the ownership |
| 71 // of raw_message. raw_message_ will be unref'ed in the destructor. |
| 72 void reset_raw_message(DBusMessage* raw_message); |
| 73 |
| 74 // Returns the string representation of this message. Useful for |
| 75 // debuggin. The returned string is similar to dbus-send's output. |
| 76 std::string ToString(); |
| 77 |
| 78 private: |
| 79 // Helper function used in ToString(). |
| 80 std::string ToStringInternal(const std::string& indent, |
| 81 MessageReader* reader); |
| 82 |
| 83 DBusMessage* raw_message_; |
| 84 DISALLOW_COPY_AND_ASSIGN(Message); |
| 85 }; |
| 86 |
| 87 // MessageCall is a type of message used for calling a method via D-Bus. |
| 88 class MethodCall : public Message { |
| 89 public: |
| 90 // Creates a method call message for the specified interface name and |
| 91 // the method name. |
| 92 // |
| 93 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE |
| 94 // interface ("org.freedesktop.DBus.Introspectable"), create a method |
| 95 // call like this: |
| 96 // |
| 97 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get"); |
| 98 // |
| 99 MethodCall(const std::string& interface_name, |
| 100 const std::string& method_name); |
| 101 |
| 102 const std::string& interface_name() { return interface_name_; } |
| 103 const std::string& method_name() { return method_name_; } |
| 104 |
| 105 private: |
| 106 friend class ObjectProxy; |
| 107 |
| 108 // Sets the service name. This will be handled by the object proxy. |
| 109 void SetServiceName(const std::string& service_name); |
| 110 // Sets the object path. This will be handled by the object proxy. |
| 111 void SetObjectPath(const std::string& object_path); |
| 112 |
| 113 std::string interface_name_; |
| 114 std::string method_name_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(MethodCall); |
| 117 }; |
| 118 |
| 119 // Response is a type of message used for receiving a response from a |
| 120 // method via D-Bus. |
| 121 class Response : public Message { |
| 122 public: |
| 123 Response(); |
| 124 |
| 125 private: |
| 126 DISALLOW_COPY_AND_ASSIGN(Response); |
| 127 }; |
| 128 |
| 129 // MessageWriter is used to write outgoing messages for calling methods |
| 130 // and sending signals. |
| 131 class MessageWriter { |
| 132 public: |
| 133 // The data will be written into the given message. |
| 134 MessageWriter(Message* message); |
| 135 ~MessageWriter(); |
| 136 |
| 137 // Appends a byte to the message. |
| 138 void AppendByte(uint8 value); |
| 139 // The following functions do the same for other basic types. |
| 140 void AppendBool(bool value); |
| 141 void AppendInt16(int16 value); |
| 142 void AppendUint16(uint16 value); |
| 143 void AppendInt32(int32 value); |
| 144 void AppendUint32(uint32 value); |
| 145 void AppendInt64(int64 value); |
| 146 void AppendUint64(uint64 value); |
| 147 void AppendDouble(double value); |
| 148 void AppendString(const std::string& value); |
| 149 void AppendObjectPath(const std::string& value); |
| 150 |
| 151 // Opens an array. The array contents can be added to the array with |
| 152 // sub_writer. The client code should close the array by CloseContainer, |
| 153 // once all contents are added. |
| 154 // |
| 155 // |signature| parameter is used to supply the D-Bus type signature of |
| 156 // the array contents. For instance, if you want an array of strings, |
| 157 // then you pass "s" as the signature. need to take |
| 158 // |
| 159 // See the spec for details about the type signatures. |
| 160 // http://dbus.freedesktop.org/doc/dbus-specification.html |
| 161 // #message-protocol-signatures |
| 162 // |
| 163 // Ideally, client shouldn't need to supply the signature string, but |
| 164 // the underlying D-Bus library requires us to supply this before |
| 165 // appending contents to array and variant. It's technically possible |
| 166 // for us to design API that doesn't require the signature but it'll |
| 167 // complicate the implementation so we decided to have the signature |
| 168 // parameter. |
| 169 // |
| 170 void OpenArray(const std::string& signature, MessageWriter* sub_writer); |
| 171 // Do the same for a variant. |
| 172 void OpenVariant(const std::string& signature, MessageWriter* sub_writer); |
| 173 // Do the same for Struct and dict entry. They don't need the signature. |
| 174 void OpenStruct(MessageWriter* sub_writer); |
| 175 void OpenDictEntry(MessageWriter* sub_writer); |
| 176 |
| 177 // Close the container for a array/variant/struct/dict entry. |
| 178 void CloseContainer(MessageWriter* sub_writer); |
| 179 |
| 180 private: |
| 181 Message* message_; |
| 182 DBusMessageIter raw_message_iter_; |
| 183 bool container_is_open_; |
| 184 |
| 185 DISALLOW_COPY_AND_ASSIGN(MessageWriter); |
| 186 }; |
| 187 |
| 188 // MessageReader is used to read incoming messages such as responses for |
| 189 // method calls. |
| 190 // |
| 191 // MessageReader manages an internal iterator to read data. All functions |
| 192 // starting with Pop advance the iterator on success. |
| 193 // |
| 194 // The main design goal of MessageReader and MessageWriter classes is to |
| 195 // provide type safe API. In the past, there was a Chrome OS blocker bug, |
| 196 // that took days to fix, that would have been prevented if the API was |
| 197 // type-safe. |
| 198 // |
| 199 // For instance, instead of doing something like: |
| 200 // |
| 201 // // We shouldn't add '&' to str here, but it compiles with '&' added. |
| 202 // dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...) |
| 203 // |
| 204 // We want to do something like: |
| 205 // |
| 206 // writer.AppendString(str); |
| 207 // |
| 208 // See crosbug.com/7619 for details about the bug in question. |
| 209 class MessageReader { |
| 210 public: |
| 211 // The data will be read from the given message. |
| 212 MessageReader(Message* message); |
| 213 ~MessageReader(); |
| 214 |
| 215 // Returns true if the reader has more data to read. The function is |
| 216 // used to iterate contents in a container like: |
| 217 // |
| 218 // while (reader.HasMore()) |
| 219 // reader.PopString(&value); |
| 220 bool HasMore(); |
| 221 |
| 222 // Gets the byte at the current iterator position. On success, advances |
| 223 // the iterator and returns true. It's an error if the actual data type |
| 224 // is not a byte, so false will be returned in this case. |
| 225 bool PopByte(uint8* value); |
| 226 // The following functions do the same for other basic types. |
| 227 bool PopBool(bool* value); |
| 228 bool PopInt16(int16* value); |
| 229 bool PopUint16(uint16* value); |
| 230 bool PopInt32(int32* value); |
| 231 bool PopUint32(uint32* value); |
| 232 bool PopInt64(int64* value); |
| 233 bool PopUint64(uint64* value); |
| 234 bool PopDouble(double* value); |
| 235 bool PopString(std::string* value); |
| 236 bool PopObjectPath(std::string* value); |
| 237 |
| 238 // Sets up the given message reader to read an array at the current |
| 239 // iterator position. On success, advances the iterator and returns |
| 240 // true. It's an error if the actual data type is not an array, so false |
| 241 // will be returned in this case. |
| 242 bool PopArray(MessageReader* sub_reader); |
| 243 // The following functions do the same for other container types. |
| 244 bool PopStruct(MessageReader* sub_reader); |
| 245 bool PopDictEntry(MessageReader* sub_reader); |
| 246 bool PopVariant(MessageReader* sub_reader); |
| 247 |
| 248 // Gets the array of bytes at the current iterator positio. On success, |
| 249 // advances the iterator and returns true. Arrays of bytes are often |
| 250 // used for exchanging binary blobs hence it's worth having a |
| 251 // specialized function. |
| 252 bool PopArrayOfBytes(std::vector<uint8>* bytes); |
| 253 |
| 254 // Gets the array of object paths at the current iterator positio. On |
| 255 // success, advances the iterator and returns true. Arrays of object |
| 256 // paths are often used to communicate with D-Bus services like |
| 257 // NetworkManager, hence it's worth having a specialized function. |
| 258 bool PopArrayOfObjectPaths(std::vector<std::string>* object_paths); |
| 259 |
| 260 // Gets the byte from the variant data container at the current iterator |
| 261 // position. On success, returns true and advances the |
| 262 // iterator. Variants are widely used in D-Bus services so it's worth |
| 263 // having a specialized function. For instance, The return value type of |
| 264 // "org.freedesktop.DBus.Introspectable.Get" is a variant. |
| 265 bool PopVariantOfByte(uint8* value); |
| 266 // The following functions do the same for other basic types. |
| 267 bool PopVariantOfBool(bool* value); |
| 268 bool PopVariantOfInt16(int16* value); |
| 269 bool PopVariantOfUint16(uint16* value); |
| 270 bool PopVariantOfInt32(int32* value); |
| 271 bool PopVariantOfUint32(uint32* value); |
| 272 bool PopVariantOfInt64(int64* value); |
| 273 bool PopVariantOfUint64(uint64* value); |
| 274 bool PopVariantOfDouble(double* value); |
| 275 bool PopVariantOfString(std::string* value); |
| 276 bool PopVariantOfObjectPath(std::string* value); |
| 277 |
| 278 // Get the data type of the value at the current iterator |
| 279 // position. INVALID_DATA will be returned if the iterator points to the |
| 280 // end of the message. |
| 281 Message::DataType GetDataType(); |
| 282 |
| 283 private: |
| 284 // Returns true if the data type at the current iterator position |
| 285 // matches the given D-Bus type, such as DBUS_TYPE_BYTE. |
| 286 bool CheckDataType(int dbus_type); |
| 287 |
| 288 // Helper function used to implement PopByte() etc. |
| 289 bool PopBasic(int dbus_type, void *value); |
| 290 |
| 291 // Helper function used to implement PopArray() etc. |
| 292 bool PopContainer(int dbus_type, MessageReader* sub_reader); |
| 293 |
| 294 // Helper function used to implement PopVariantOfByte() etc. |
| 295 bool PopVariantOfBasic(int dbus_type, void* value); |
| 296 |
| 297 Message* message_; |
| 298 DBusMessageIter raw_message_iter_; |
| 299 |
| 300 DISALLOW_COPY_AND_ASSIGN(MessageReader); |
| 301 }; |
| 302 |
| 303 } // namespace dbus |
| 304 |
| 305 #endif // DBUS_MESSAGE_H_ |
OLD | NEW |