15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef DBUS_MESSAGE_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DBUS_MESSAGE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <dbus/dbus.h>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dbus/dbus_export.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dbus/file_descriptor.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dbus/object_path.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace google {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace protobuf {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MessageLite;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace protobuf
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace google
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace dbus {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MessageWriter;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MessageReader;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DBUS_TYPE_UNIX_FD was added in D-Bus version 1.4
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(DBUS_TYPE_UNIX_FD)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DBUS_TYPE_UNIX_FD      ((int) 'h')
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if Unix FD passing is supported in libdbus.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The check is done runtime rather than compile time as the libdbus
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// version used at runtime may be different from the one used at compile time.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CHROME_DBUS_EXPORT bool IsDBusTypeUnixFdSupported();
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Message is the base class of D-Bus message types. Client code must use
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// sub classes such as MethodCall and Response instead.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The class name Message is very generic, but there should be no problem
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as the class is inside 'dbus' namespace. We chose to name this way, as
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// libdbus defines lots of types starting with DBus, such as
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DBusMessage. We should avoid confusion and conflict with these types.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CHROME_DBUS_EXPORT Message {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The message type used in D-Bus.  Redefined here so client code
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // doesn't need to use raw D-Bus macros. DBUS_MESSAGE_TYPE_INVALID
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // etc. are #define macros. Having an enum type here makes code a bit
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // more type-safe.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum MessageType {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MESSAGE_INVALID = DBUS_MESSAGE_TYPE_INVALID,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MESSAGE_METHOD_CALL = DBUS_MESSAGE_TYPE_METHOD_CALL,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MESSAGE_METHOD_RETURN = DBUS_MESSAGE_TYPE_METHOD_RETURN,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MESSAGE_SIGNAL = DBUS_MESSAGE_TYPE_SIGNAL,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MESSAGE_ERROR = DBUS_MESSAGE_TYPE_ERROR,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) };
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The data type used in the D-Bus type system.  See the comment at
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // MessageType for why we are redefining data types here.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum DataType {
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INVALID_DATA = DBUS_TYPE_INVALID,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BYTE = DBUS_TYPE_BYTE,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BOOL = DBUS_TYPE_BOOLEAN,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INT16 = DBUS_TYPE_INT16,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UINT16 = DBUS_TYPE_UINT16,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INT32 = DBUS_TYPE_INT32,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UINT32 = DBUS_TYPE_UINT32,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INT64 = DBUS_TYPE_INT64,
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UINT64 = DBUS_TYPE_UINT64,
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DOUBLE = DBUS_TYPE_DOUBLE,
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STRING = DBUS_TYPE_STRING,
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OBJECT_PATH = DBUS_TYPE_OBJECT_PATH,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ARRAY = DBUS_TYPE_ARRAY,
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STRUCT = DBUS_TYPE_STRUCT,
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DICT_ENTRY = DBUS_TYPE_DICT_ENTRY,
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VARIANT = DBUS_TYPE_VARIANT,
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UNIX_FD = DBUS_TYPE_UNIX_FD,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the type of the message. Returns MESSAGE_INVALID if
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // raw_message_ is NULL.
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MessageType GetMessageType();
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the type of the message as string like "MESSAGE_METHOD_CALL"
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for instance.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetMessageTypeAsString();
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DBusMessage* raw_message() { return raw_message_; }
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the destination, the path, the interface, the member, etc.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetDestination(const std::string& destination);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetPath(const ObjectPath& path);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetInterface(const std::string& interface);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetMember(const std::string& member);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetErrorName(const std::string& error_name);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetSender(const std::string& sender);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetSerial(uint32 serial);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetReplySerial(uint32 reply_serial);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SetSignature() does not exist as we cannot do it.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the destination, the path, the interface, the member, etc.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If not set, an empty string is returned.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetDestination();
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ObjectPath GetPath();
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetInterface();
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetMember();
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetErrorName();
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetSender();
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetSignature();
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the serial and reply serial numbers. Returns 0 if not set.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 GetSerial();
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 GetReplySerial();
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the string representation of this message. Useful for
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // debugging. The output is truncated as needed (ex. strings are truncated
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if longer than a certain limit defined in the .cc file).
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ToString();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This class cannot be instantiated. Use sub classes instead.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Message();
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~Message();
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the message with the given raw message.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Init(DBusMessage* raw_message);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used in ToString().
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ToStringInternal(const std::string& indent,
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               MessageReader* reader);
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DBusMessage* raw_message_;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(Message);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MessageCall is a type of message used for calling a method via D-Bus.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CHROME_DBUS_EXPORT MethodCall : public Message {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a method call message for the specified interface name and
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the method name.
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // interface ("org.freedesktop.DBus.Introspectable"), create a method
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // call like this:
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The constructor creates the internal raw message.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MethodCall(const std::string& interface_name,
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             const std::string& method_name);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a newly created MethodCall from the given raw message of the
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // type DBUS_MESSAGE_TYPE_METHOD_CALL. The caller must delete the
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned object. Takes the ownership of |raw_message|.
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static MethodCall* FromRawMessage(DBusMessage* raw_message);
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a method call message. The internal raw message is NULL.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Only used internally.
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MethodCall();
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MethodCall);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Signal is a type of message used to send a signal.
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CHROME_DBUS_EXPORT Signal : public Message {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a signal message for the specified interface name and the
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // method name.
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For instance, to send "PropertiesChanged" signal of
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DBUS_INTERFACE_INTROSPECTABLE interface
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ("org.freedesktop.DBus.Introspectable"), create a signal like this:
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   Signal signal(DBUS_INTERFACE_INTROSPECTABLE, "PropertiesChanged");
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The constructor creates the internal raw_message_.
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Signal(const std::string& interface_name,
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         const std::string& method_name);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a newly created SIGNAL from the given raw message of the type
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DBUS_MESSAGE_TYPE_SIGNAL. The caller must delete the returned
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object. Takes the ownership of |raw_message|.
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Signal* FromRawMessage(DBusMessage* raw_message);
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a signal message. The internal raw message is NULL.
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Only used internally.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Signal();
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(Signal);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Response is a type of message used for receiving a response from a
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// method via D-Bus.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CHROME_DBUS_EXPORT Response : public Message {
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a newly created Response from the given raw message of the
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // type DBUS_MESSAGE_TYPE_METHOD_RETURN. Takes the ownership of |raw_message|.
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static scoped_ptr<Response> FromRawMessage(DBusMessage* raw_message);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a newly created Response from the given method call.
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Used for implementing exported methods. Does NOT take the ownership of
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |method_call|.
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static scoped_ptr<Response> FromMethodCall(MethodCall* method_call);
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a newly created Response with an empty payload.
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Useful for testing.
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static scoped_ptr<Response> CreateEmpty();
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a Response message. The internal raw message is NULL.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Response();
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(Response);
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ErrorResponse is a type of message used to return an error to the
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// caller of a method.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CHROME_DBUS_EXPORT ErrorResponse: public Response {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a newly created Response from the given raw message of the
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // type DBUS_MESSAGE_TYPE_METHOD_RETURN. Takes the ownership of |raw_message|.
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static scoped_ptr<ErrorResponse> FromRawMessage(DBusMessage* raw_message);
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a newly created ErrorResponse from the given method call, the
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // error name, and the error message.  The error name looks like
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "org.freedesktop.DBus.Error.Failed". Used for returning an error to a
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // failed method call. Does NOT take the ownership of |method_call|.
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static scoped_ptr<ErrorResponse> FromMethodCall(
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      MethodCall* method_call,
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::string& error_name,
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::string& error_message);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates an ErrorResponse message. The internal raw message is NULL.
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ErrorResponse();
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ErrorResponse);
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MessageWriter is used to write outgoing messages for calling methods
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and sending signals.
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The main design goal of MessageReader and MessageWriter classes is to
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// provide a type safe API. In the past, there was a Chrome OS blocker
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// bug, that took days to fix, that would have been prevented if the API
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// was type-safe.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For instance, instead of doing something like:
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   // We shouldn't add '&' to str here, but it compiles with '&' added.
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   dbus_g_proxy_call(..., G_TYPE_STRING, str, G_TYPE_INVALID, ...)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We want to do something like:
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   writer.AppendString(str);
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CHROME_DBUS_EXPORT MessageWriter {
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Data added with Append* will be written to |message|, which may be NULL
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to create a sub-writer for passing to OpenArray, etc.
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit MessageWriter(Message* message);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~MessageWriter();
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends a byte to the message.
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendByte(uint8 value);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendBool(bool value);
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendInt16(int16 value);
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendUint16(uint16 value);
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendInt32(int32 value);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendUint32(uint32 value);
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendInt64(int64 value);
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendUint64(uint64 value);
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendDouble(double value);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendString(const std::string& value);
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendObjectPath(const ObjectPath& value);
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendFileDescriptor(const FileDescriptor& value);
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Opens an array. The array contents can be added to the array with
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |sub_writer|. The client code must close the array with
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // CloseContainer(), once all contents are added.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |signature| parameter is used to supply the D-Bus type signature of
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the array contents. For instance, if you want an array of strings,
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // then you pass "s" as the signature.
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See the spec for details about the type signatures.
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://dbus.freedesktop.org/doc/dbus-specification.html
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // #message-protocol-signatures
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OpenArray(const std::string& signature, MessageWriter* sub_writer);
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do the same for a variant.
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OpenVariant(const std::string& signature, MessageWriter* sub_writer);
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do the same for Struct and dict entry. They don't need the signature.
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OpenStruct(MessageWriter* sub_writer);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OpenDictEntry(MessageWriter* sub_writer);
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Close the container for a array/variant/struct/dict entry.
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CloseContainer(MessageWriter* sub_writer);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends the array of bytes. Arrays of bytes are often used for
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // exchanging binary blobs hence it's worth having a specialized
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function.
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendArrayOfBytes(const uint8* values, size_t length);
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends the array of strings. Arrays of strings are often used for
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // exchanging lists of names hence it's worth having a specialized
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function.
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendArrayOfStrings(const std::vector<std::string>& strings);
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends the array of object paths. Arrays of object paths are often
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // used when exchanging object paths, hence it's worth having a
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // specialized function.
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendArrayOfObjectPaths(const std::vector<ObjectPath>& object_paths);
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends the protocol buffer as an array of bytes. The buffer is serialized
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // into an array of bytes before communication, since protocol buffers are not
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a native dbus type. On the receiving size the array of bytes needs to be
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // read and deserialized into a protocol buffer of the correct type. There are
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // methods in MessageReader to assist in this.  Return true on succes and fail
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when serialization is not successful.
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AppendProtoAsArrayOfBytes(const google::protobuf::MessageLite& protobuf);
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends the byte wrapped in a variant data container. Variants are
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // widely used in D-Bus services so it's worth having a specialized
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function. For instance, The third parameter of
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "org.freedesktop.DBus.Properties.Set" is a variant.
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfByte(uint8 value);
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfBool(bool value);
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfInt16(int16 value);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfUint16(uint16 value);
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfInt32(int32 value);
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfUint32(uint32 value);
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfInt64(int64 value);
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfUint64(uint64 value);
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfDouble(double value);
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfString(const std::string& value);
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfObjectPath(const ObjectPath& value);
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used to implement AppendByte etc.
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendBasic(int dbus_type, const void* value);
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used to implement AppendVariantOfByte() etc.
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendVariantOfBasic(int dbus_type, const void* value);
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Message* message_;
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DBusMessageIter raw_message_iter_;
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool container_is_open_;
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MessageWriter);
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MessageReader is used to read incoming messages such as responses for
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// method calls.
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MessageReader manages an internal iterator to read data. All functions
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// starting with Pop advance the iterator on success.
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CHROME_DBUS_EXPORT MessageReader {
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The data will be read from the given |message|, which may be NULL to
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // create a sub-reader for passing to PopArray, etc.
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit MessageReader(Message* message);
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~MessageReader();
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the reader has more data to read. The function is
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // used to iterate contents in a container like:
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   while (reader.HasMoreData())
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //     reader.PopString(&value);
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasMoreData();
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the byte at the current iterator position.
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true and advances the iterator on success.
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the data type is not a byte.
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopByte(uint8* value);
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopBool(bool* value);
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopInt16(int16* value);
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopUint16(uint16* value);
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopInt32(int32* value);
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopUint32(uint32* value);
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopInt64(int64* value);
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopUint64(uint64* value);
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopDouble(double* value);
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopString(std::string* value);
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopObjectPath(ObjectPath* value);
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopFileDescriptor(FileDescriptor* value);
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets up the given message reader to read an array at the current
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // iterator position.
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true and advances the iterator on success.
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the data type is not an array
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopArray(MessageReader* sub_reader);
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopStruct(MessageReader* sub_reader);
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopDictEntry(MessageReader* sub_reader);
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariant(MessageReader* sub_reader);
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the array of bytes at the current iterator position.
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true and advances the iterator on success.
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Arrays of bytes are often used for exchanging binary blobs hence it's
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // worth having a specialized function.
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Ownership of the memory pointed to by |bytes| remains with the
412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // MessageReader; |bytes| must be copied if the contents will be referenced
413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // after the MessageReader is destroyed.
414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool PopArrayOfBytes(const uint8** bytes, size_t* length);
415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Gets the array of strings at the current iterator position. |strings| is
417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // cleared before being modified. Returns true and advances the iterator on
418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // success.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Arrays of strings are often used to communicate with D-Bus
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // services like KWallet, hence it's worth having a specialized
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function.
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopArrayOfStrings(std::vector<std::string>* strings);
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the array of object paths at the current iterator position.
426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // |object_paths| is cleared before being modified. Returns true and advances
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // the iterator on success.
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Arrays of object paths are often used to communicate with D-Bus
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // services like NetworkManager, hence it's worth having a specialized
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function.
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopArrayOfObjectPaths(std::vector<ObjectPath>* object_paths);
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the array of bytes at the current iterator position. It then parses
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this binary blob into the protocol buffer supplied.
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true and advances the iterator on success. On failure returns false
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and emits an error message on the source of the failure. The two most
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // common errors come from the iterator not currently being at a byte array or
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the wrong type of protocol buffer is passed in and the parse fails.
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopArrayOfBytesAsProto(google::protobuf::MessageLite* protobuf);
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the byte from the variant data container at the current iterator
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // position.
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true and advances the iterator on success.
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Variants are widely used in D-Bus services so it's worth having a
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // specialized function. For instance, The return value type of
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "org.freedesktop.DBus.Properties.Get" is a variant.
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfByte(uint8* value);
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfBool(bool* value);
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfInt16(int16* value);
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfUint16(uint16* value);
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfInt32(int32* value);
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfUint32(uint32* value);
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfInt64(int64* value);
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfUint64(uint64* value);
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfDouble(double* value);
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfString(std::string* value);
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfObjectPath(ObjectPath* value);
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the data type of the value at the current iterator
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // position. INVALID_DATA will be returned if the iterator points to the
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // end of the message.
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Message::DataType GetDataType();
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Get the DBus signature of the value at the current iterator position.
4671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // An empty string will be returned if the iterator points to the end of
4681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // the message.
4691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string GetDataSignature();
4701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the data type at the current iterator position
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CheckDataType(int dbus_type);
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used to implement PopByte() etc.
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopBasic(int dbus_type, void *value);
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used to implement PopArray() etc.
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopContainer(int dbus_type, MessageReader* sub_reader);
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used to implement PopVariantOfByte() etc.
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool PopVariantOfBasic(int dbus_type, void* value);
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Message* message_;
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DBusMessageIter raw_message_iter_;
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MessageReader);
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace dbus
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // DBUS_MESSAGE_H_
494