| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DBUS_MESSAGE_H_ | 5 #ifndef DBUS_MESSAGE_H_ |
| 6 #define DBUS_MESSAGE_H_ | 6 #define DBUS_MESSAGE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 #include <dbus/dbus.h> | 10 #include <dbus/dbus.h> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "dbus/dbus_export.h" |
| 13 #include "dbus/file_descriptor.h" | 14 #include "dbus/file_descriptor.h" |
| 14 #include "dbus/object_path.h" | 15 #include "dbus/object_path.h" |
| 15 | 16 |
| 16 namespace google { | 17 namespace google { |
| 17 namespace protobuf { | 18 namespace protobuf { |
| 18 | 19 |
| 19 class MessageLite; | 20 class MessageLite; |
| 20 | 21 |
| 21 } // namespace protobuf | 22 } // namespace protobuf |
| 22 } // namespace google | 23 } // namespace google |
| 23 | 24 |
| 24 | 25 |
| 25 namespace dbus { | 26 namespace dbus { |
| 26 | 27 |
| 27 class MessageWriter; | 28 class MessageWriter; |
| 28 class MessageReader; | 29 class MessageReader; |
| 29 | 30 |
| 30 // DBUS_TYPE_UNIX_FD was added in D-Bus version 1.4 | 31 // DBUS_TYPE_UNIX_FD was added in D-Bus version 1.4 |
| 31 #if !defined(DBUS_TYPE_UNIX_FD) | 32 #if !defined(DBUS_TYPE_UNIX_FD) |
| 32 #define DBUS_TYPE_UNIX_FD ((int) 'h') | 33 #define DBUS_TYPE_UNIX_FD ((int) 'h') |
| 33 #endif | 34 #endif |
| 34 | 35 |
| 35 // Returns true if Unix FD passing is supported in libdbus. | 36 // Returns true if Unix FD passing is supported in libdbus. |
| 36 // The check is done runtime rather than compile time as the libdbus | 37 // 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 // version used at runtime may be different from the one used at compile time. |
| 38 bool IsDBusTypeUnixFdSupported(); | 39 CHROME_DBUS_EXPORT bool IsDBusTypeUnixFdSupported(); |
| 39 | 40 |
| 40 // Message is the base class of D-Bus message types. Client code must use | 41 // Message is the base class of D-Bus message types. Client code must use |
| 41 // sub classes such as MethodCall and Response instead. | 42 // sub classes such as MethodCall and Response instead. |
| 42 // | 43 // |
| 43 // The class name Message is very generic, but there should be no problem | 44 // 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 // 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 // libdbus defines lots of types starting with DBus, such as |
| 46 // DBusMessage. We should avoid confusion and conflict with these types. | 47 // DBusMessage. We should avoid confusion and conflict with these types. |
| 47 class Message { | 48 class CHROME_DBUS_EXPORT Message { |
| 48 public: | 49 public: |
| 49 // The message type used in D-Bus. Redefined here so client code | 50 // 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 // 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 // etc. are #define macros. Having an enum type here makes code a bit |
| 52 // more type-safe. | 53 // more type-safe. |
| 53 enum MessageType { | 54 enum MessageType { |
| 54 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID, | 55 MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID, |
| 55 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL, | 56 MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL, |
| 56 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN, | 57 MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN, |
| 57 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL, | 58 MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL, |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 private: | 131 private: |
| 131 // Helper function used in ToString(). | 132 // Helper function used in ToString(). |
| 132 std::string ToStringInternal(const std::string& indent, | 133 std::string ToStringInternal(const std::string& indent, |
| 133 MessageReader* reader); | 134 MessageReader* reader); |
| 134 | 135 |
| 135 DBusMessage* raw_message_; | 136 DBusMessage* raw_message_; |
| 136 DISALLOW_COPY_AND_ASSIGN(Message); | 137 DISALLOW_COPY_AND_ASSIGN(Message); |
| 137 }; | 138 }; |
| 138 | 139 |
| 139 // MessageCall is a type of message used for calling a method via D-Bus. | 140 // MessageCall is a type of message used for calling a method via D-Bus. |
| 140 class MethodCall : public Message { | 141 class CHROME_DBUS_EXPORT MethodCall : public Message { |
| 141 public: | 142 public: |
| 142 // Creates a method call message for the specified interface name and | 143 // Creates a method call message for the specified interface name and |
| 143 // the method name. | 144 // the method name. |
| 144 // | 145 // |
| 145 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE | 146 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE |
| 146 // interface ("org.freedesktop.DBus.Introspectable"), create a method | 147 // interface ("org.freedesktop.DBus.Introspectable"), create a method |
| 147 // call like this: | 148 // call like this: |
| 148 // | 149 // |
| 149 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get"); | 150 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get"); |
| 150 // | 151 // |
| 151 // The constructor creates the internal raw message. | 152 // The constructor creates the internal raw message. |
| 152 MethodCall(const std::string& interface_name, | 153 MethodCall(const std::string& interface_name, |
| 153 const std::string& method_name); | 154 const std::string& method_name); |
| 154 | 155 |
| 155 // Returns a newly created MethodCall from the given raw message of the | 156 // 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 // type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the |
| 157 // returned object. Takes the ownership of |raw_message|. | 158 // returned object. Takes the ownership of |raw_message|. |
| 158 static MethodCall* FromRawMessage(DBusMessage* raw_message); | 159 static MethodCall* FromRawMessage(DBusMessage* raw_message); |
| 159 | 160 |
| 160 private: | 161 private: |
| 161 // Creates a method call message. The internal raw message is NULL. | 162 // Creates a method call message. The internal raw message is NULL. |
| 162 // Only used internally. | 163 // Only used internally. |
| 163 MethodCall(); | 164 MethodCall(); |
| 164 | 165 |
| 165 DISALLOW_COPY_AND_ASSIGN(MethodCall); | 166 DISALLOW_COPY_AND_ASSIGN(MethodCall); |
| 166 }; | 167 }; |
| 167 | 168 |
| 168 // Signal is a type of message used to send a signal. | 169 // Signal is a type of message used to send a signal. |
| 169 class Signal : public Message { | 170 class CHROME_DBUS_EXPORT Signal : public Message { |
| 170 public: | 171 public: |
| 171 // Creates a signal message for the specified interface name and the | 172 // Creates a signal message for the specified interface name and the |
| 172 // method name. | 173 // method name. |
| 173 // | 174 // |
| 174 // For instance, to send "PropertiesChanged" signal of | 175 // For instance, to send "PropertiesChanged" signal of |
| 175 // DBUS_INTERFACE_INTROSPECTABLE interface | 176 // DBUS_INTERFACE_INTROSPECTABLE interface |
| 176 // ("org.freedesktop.DBus.Introspectable"), create a signal like this: | 177 // ("org.freedesktop.DBus.Introspectable"), create a signal like this: |
| 177 // | 178 // |
| 178 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged"); | 179 // Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged"); |
| 179 // | 180 // |
| 180 // The constructor creates the internal raw_message_. | 181 // The constructor creates the internal raw_message_. |
| 181 Signal(const std::string& interface_name, | 182 Signal(const std::string& interface_name, |
| 182 const std::string& method_name); | 183 const std::string& method_name); |
| 183 | 184 |
| 184 // Returns a newly created SIGNAL from the given raw message of the type | 185 // 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 // DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned |
| 186 // object. Takes the ownership of |raw_message|. | 187 // object. Takes the ownership of |raw_message|. |
| 187 static Signal* FromRawMessage(DBusMessage* raw_message); | 188 static Signal* FromRawMessage(DBusMessage* raw_message); |
| 188 | 189 |
| 189 private: | 190 private: |
| 190 // Creates a signal message. The internal raw message is NULL. | 191 // Creates a signal message. The internal raw message is NULL. |
| 191 // Only used internally. | 192 // Only used internally. |
| 192 Signal(); | 193 Signal(); |
| 193 | 194 |
| 194 DISALLOW_COPY_AND_ASSIGN(Signal); | 195 DISALLOW_COPY_AND_ASSIGN(Signal); |
| 195 }; | 196 }; |
| 196 | 197 |
| 197 // Response is a type of message used for receiving a response from a | 198 // Response is a type of message used for receiving a response from a |
| 198 // method via D-Bus. | 199 // method via D-Bus. |
| 199 class Response : public Message { | 200 class CHROME_DBUS_EXPORT Response : public Message { |
| 200 public: | 201 public: |
| 201 // Returns a newly created Response from the given raw message of the | 202 // 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 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the |
| 203 // returned object. Takes the ownership of |raw_message|. | 204 // returned object. Takes the ownership of |raw_message|. |
| 204 static Response* FromRawMessage(DBusMessage* raw_message); | 205 static Response* FromRawMessage(DBusMessage* raw_message); |
| 205 | 206 |
| 206 // Returns a newly created Response from the given method call. The | 207 // Returns a newly created Response from the given method call. The |
| 207 // caller must delete the returned object. Used for implementing | 208 // caller must delete the returned object. Used for implementing |
| 208 // exported methods. | 209 // exported methods. |
| 209 static Response* FromMethodCall(MethodCall* method_call); | 210 static Response* FromMethodCall(MethodCall* method_call); |
| 210 | 211 |
| 211 // Returns a newly created Response with an empty payload. The caller | 212 // Returns a newly created Response with an empty payload. The caller |
| 212 // must delete the returned object. Useful for testing. | 213 // must delete the returned object. Useful for testing. |
| 213 static Response* CreateEmpty(); | 214 static Response* CreateEmpty(); |
| 214 | 215 |
| 215 protected: | 216 protected: |
| 216 // Creates a Response message. The internal raw message is NULL. | 217 // Creates a Response message. The internal raw message is NULL. |
| 217 Response(); | 218 Response(); |
| 218 | 219 |
| 219 private: | 220 private: |
| 220 DISALLOW_COPY_AND_ASSIGN(Response); | 221 DISALLOW_COPY_AND_ASSIGN(Response); |
| 221 }; | 222 }; |
| 222 | 223 |
| 223 // ErrorResponse is a type of message used to return an error to the | 224 // ErrorResponse is a type of message used to return an error to the |
| 224 // caller of a method. | 225 // caller of a method. |
| 225 class ErrorResponse: public Response { | 226 class CHROME_DBUS_EXPORT ErrorResponse: public Response { |
| 226 public: | 227 public: |
| 227 // Returns a newly created Response from the given raw message of the | 228 // 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 // type DBUS_MESSAGE_TYPE_METHOD_RETURN. The caller must delete the |
| 229 // returned object. Takes the ownership of |raw_message|. | 230 // returned object. Takes the ownership of |raw_message|. |
| 230 static ErrorResponse* FromRawMessage(DBusMessage* raw_message); | 231 static ErrorResponse* FromRawMessage(DBusMessage* raw_message); |
| 231 | 232 |
| 232 // Returns a newly created ErrorResponse from the given method call, the | 233 // Returns a newly created ErrorResponse from the given method call, the |
| 233 // error name, and the error message. The error name looks like | 234 // 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 // "org.freedesktop.DBus.Error.Failed". Used for returning an error to a |
| 235 // failed method call. | 236 // failed method call. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 254 // | 255 // |
| 255 // For instance, instead of doing something like: | 256 // For instance, instead of doing something like: |
| 256 // | 257 // |
| 257 // // We shouldn't add '&' to str here, but it compiles with '&' added. | 258 // // 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 // dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...) |
| 259 // | 260 // |
| 260 // We want to do something like: | 261 // We want to do something like: |
| 261 // | 262 // |
| 262 // writer.AppendString(str); | 263 // writer.AppendString(str); |
| 263 // | 264 // |
| 264 class MessageWriter { | 265 class CHROME_DBUS_EXPORT MessageWriter { |
| 265 public: | 266 public: |
| 266 // Data added with Append* will be written to |message|, which may be NULL | 267 // 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 // to create a sub-writer for passing to OpenArray, etc. |
| 268 explicit MessageWriter(Message* message); | 269 explicit MessageWriter(Message* message); |
| 269 ~MessageWriter(); | 270 ~MessageWriter(); |
| 270 | 271 |
| 271 // Appends a byte to the message. | 272 // Appends a byte to the message. |
| 272 void AppendByte(uint8 value); | 273 void AppendByte(uint8 value); |
| 273 void AppendBool(bool value); | 274 void AppendBool(bool value); |
| 274 void AppendInt16(int16 value); | 275 void AppendInt16(int16 value); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 bool container_is_open_; | 356 bool container_is_open_; |
| 356 | 357 |
| 357 DISALLOW_COPY_AND_ASSIGN(MessageWriter); | 358 DISALLOW_COPY_AND_ASSIGN(MessageWriter); |
| 358 }; | 359 }; |
| 359 | 360 |
| 360 // MessageReader is used to read incoming messages such as responses for | 361 // MessageReader is used to read incoming messages such as responses for |
| 361 // method calls. | 362 // method calls. |
| 362 // | 363 // |
| 363 // MessageReader manages an internal iterator to read data. All functions | 364 // MessageReader manages an internal iterator to read data. All functions |
| 364 // starting with Pop advance the iterator on success. | 365 // starting with Pop advance the iterator on success. |
| 365 class MessageReader { | 366 class CHROME_DBUS_EXPORT MessageReader { |
| 366 public: | 367 public: |
| 367 // The data will be read from the given |message|, which may be NULL to | 368 // 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 // create a sub-reader for passing to PopArray, etc. |
| 369 explicit MessageReader(Message* message); | 370 explicit MessageReader(Message* message); |
| 370 ~MessageReader(); | 371 ~MessageReader(); |
| 371 | 372 |
| 372 // Returns true if the reader has more data to read. The function is | 373 // Returns true if the reader has more data to read. The function is |
| 373 // used to iterate contents in a container like: | 374 // used to iterate contents in a container like: |
| 374 // | 375 // |
| 375 // while (reader.HasMoreData()) | 376 // while (reader.HasMoreData()) |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 | 476 |
| 476 Message* message_; | 477 Message* message_; |
| 477 DBusMessageIter raw_message_iter_; | 478 DBusMessageIter raw_message_iter_; |
| 478 | 479 |
| 479 DISALLOW_COPY_AND_ASSIGN(MessageReader); | 480 DISALLOW_COPY_AND_ASSIGN(MessageReader); |
| 480 }; | 481 }; |
| 481 | 482 |
| 482 } // namespace dbus | 483 } // namespace dbus |
| 483 | 484 |
| 484 #endif // DBUS_MESSAGE_H_ | 485 #endif // DBUS_MESSAGE_H_ |
| OLD | NEW |