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