xref: /aosp_15_r20/external/libchrome/dbus/object_proxy.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_OBJECT_PROXY_H_
6*635a8641SAndroid Build Coastguard Worker #define DBUS_OBJECT_PROXY_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <dbus/dbus.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <map>
11*635a8641SAndroid Build Coastguard Worker #include <memory>
12*635a8641SAndroid Build Coastguard Worker #include <set>
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/callback.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
21*635a8641SAndroid Build Coastguard Worker #include "dbus/dbus_export.h"
22*635a8641SAndroid Build Coastguard Worker #include "dbus/object_path.h"
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker namespace base {
25*635a8641SAndroid Build Coastguard Worker class TaskRunner;
26*635a8641SAndroid Build Coastguard Worker }  // namespace base
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker namespace dbus {
29*635a8641SAndroid Build Coastguard Worker 
30*635a8641SAndroid Build Coastguard Worker class Bus;
31*635a8641SAndroid Build Coastguard Worker class ErrorResponse;
32*635a8641SAndroid Build Coastguard Worker class MethodCall;
33*635a8641SAndroid Build Coastguard Worker class Response;
34*635a8641SAndroid Build Coastguard Worker class ScopedDBusError;
35*635a8641SAndroid Build Coastguard Worker class Signal;
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker // ObjectProxy is used to communicate with remote objects, mainly for
38*635a8641SAndroid Build Coastguard Worker // calling methods of these objects.
39*635a8641SAndroid Build Coastguard Worker //
40*635a8641SAndroid Build Coastguard Worker // ObjectProxy is a ref counted object, to ensure that |this| of the
41*635a8641SAndroid Build Coastguard Worker // object is alive when callbacks referencing |this| are called; the
42*635a8641SAndroid Build Coastguard Worker // bus always holds at least one of those references so object proxies
43*635a8641SAndroid Build Coastguard Worker // always last as long as the bus that created them.
44*635a8641SAndroid Build Coastguard Worker class CHROME_DBUS_EXPORT ObjectProxy
45*635a8641SAndroid Build Coastguard Worker     : public base::RefCountedThreadSafe<ObjectProxy> {
46*635a8641SAndroid Build Coastguard Worker  public:
47*635a8641SAndroid Build Coastguard Worker   // Client code should use Bus::GetObjectProxy() or
48*635a8641SAndroid Build Coastguard Worker   // Bus::GetObjectProxyWithOptions() instead of this constructor.
49*635a8641SAndroid Build Coastguard Worker   ObjectProxy(Bus* bus,
50*635a8641SAndroid Build Coastguard Worker               const std::string& service_name,
51*635a8641SAndroid Build Coastguard Worker               const ObjectPath& object_path,
52*635a8641SAndroid Build Coastguard Worker               int options);
53*635a8641SAndroid Build Coastguard Worker 
54*635a8641SAndroid Build Coastguard Worker   // Options to be OR-ed together when calling Bus::GetObjectProxyWithOptions().
55*635a8641SAndroid Build Coastguard Worker   // Set the IGNORE_SERVICE_UNKNOWN_ERRORS option to silence logging of
56*635a8641SAndroid Build Coastguard Worker   // org.freedesktop.DBus.Error.ServiceUnknown errors and
57*635a8641SAndroid Build Coastguard Worker   // org.freedesktop.DBus.Error.ObjectUnknown errors.
58*635a8641SAndroid Build Coastguard Worker   enum Options {
59*635a8641SAndroid Build Coastguard Worker     DEFAULT_OPTIONS = 0,
60*635a8641SAndroid Build Coastguard Worker     IGNORE_SERVICE_UNKNOWN_ERRORS = 1 << 0
61*635a8641SAndroid Build Coastguard Worker   };
62*635a8641SAndroid Build Coastguard Worker 
63*635a8641SAndroid Build Coastguard Worker   // Special timeout constants.
64*635a8641SAndroid Build Coastguard Worker   //
65*635a8641SAndroid Build Coastguard Worker   // The constants correspond to DBUS_TIMEOUT_USE_DEFAULT and
66*635a8641SAndroid Build Coastguard Worker   // DBUS_TIMEOUT_INFINITE. Here we use literal numbers instead of these
67*635a8641SAndroid Build Coastguard Worker   // macros as these aren't defined with D-Bus earlier than 1.4.12.
68*635a8641SAndroid Build Coastguard Worker   enum {
69*635a8641SAndroid Build Coastguard Worker     TIMEOUT_USE_DEFAULT = -1,
70*635a8641SAndroid Build Coastguard Worker     TIMEOUT_INFINITE = 0x7fffffff,
71*635a8641SAndroid Build Coastguard Worker   };
72*635a8641SAndroid Build Coastguard Worker 
73*635a8641SAndroid Build Coastguard Worker   // Called when an error response is returned or no response is returned.
74*635a8641SAndroid Build Coastguard Worker   // Used for CallMethodWithErrorCallback().
75*635a8641SAndroid Build Coastguard Worker   using ErrorCallback = base::OnceCallback<void(ErrorResponse*)>;
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker   // Called when the response is returned. Used for CallMethod().
78*635a8641SAndroid Build Coastguard Worker   using ResponseCallback = base::OnceCallback<void(Response*)>;
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker   // Called when the response is returned or an error occurs. Used for
81*635a8641SAndroid Build Coastguard Worker   // CallMethodWithErrorResponse().
82*635a8641SAndroid Build Coastguard Worker   // Note that even in error case, ErrorResponse* may be nullptr.
83*635a8641SAndroid Build Coastguard Worker   // E.g. out-of-memory error is found in libdbus, or the connection of
84*635a8641SAndroid Build Coastguard Worker   // |bus_| is not yet established.
85*635a8641SAndroid Build Coastguard Worker   using ResponseOrErrorCallback =
86*635a8641SAndroid Build Coastguard Worker       base::OnceCallback<void(Response*, ErrorResponse*)>;
87*635a8641SAndroid Build Coastguard Worker 
88*635a8641SAndroid Build Coastguard Worker   // Called when a signal is received. Signal* is the incoming signal.
89*635a8641SAndroid Build Coastguard Worker   using SignalCallback = base::Callback<void(Signal*)>;
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker   // Called when NameOwnerChanged signal is received.
92*635a8641SAndroid Build Coastguard Worker   using NameOwnerChangedCallback =
93*635a8641SAndroid Build Coastguard Worker       base::Callback<void(const std::string& old_owner,
94*635a8641SAndroid Build Coastguard Worker                           const std::string& new_owner)>;
95*635a8641SAndroid Build Coastguard Worker 
96*635a8641SAndroid Build Coastguard Worker   // Called when the service becomes available.
97*635a8641SAndroid Build Coastguard Worker   using WaitForServiceToBeAvailableCallback =
98*635a8641SAndroid Build Coastguard Worker       base::OnceCallback<void(bool service_is_available)>;
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker   // Called when the object proxy is connected to the signal.
101*635a8641SAndroid Build Coastguard Worker   // Parameters:
102*635a8641SAndroid Build Coastguard Worker   // - the interface name.
103*635a8641SAndroid Build Coastguard Worker   // - the signal name.
104*635a8641SAndroid Build Coastguard Worker   // - whether it was successful or not.
105*635a8641SAndroid Build Coastguard Worker   using OnConnectedCallback =
106*635a8641SAndroid Build Coastguard Worker       base::OnceCallback<void(const std::string&, const std::string&, bool)>;
107*635a8641SAndroid Build Coastguard Worker 
108*635a8641SAndroid Build Coastguard Worker   // Calls the method of the remote object and blocks until the response
109*635a8641SAndroid Build Coastguard Worker   // is returned. Returns NULL on error with the error details specified
110*635a8641SAndroid Build Coastguard Worker   // in the |error| object.
111*635a8641SAndroid Build Coastguard Worker   //
112*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
113*635a8641SAndroid Build Coastguard Worker   virtual std::unique_ptr<Response> CallMethodAndBlockWithErrorDetails(
114*635a8641SAndroid Build Coastguard Worker       MethodCall* method_call,
115*635a8641SAndroid Build Coastguard Worker       int timeout_ms,
116*635a8641SAndroid Build Coastguard Worker       ScopedDBusError* error);
117*635a8641SAndroid Build Coastguard Worker 
118*635a8641SAndroid Build Coastguard Worker   // Calls the method of the remote object and blocks until the response
119*635a8641SAndroid Build Coastguard Worker   // is returned. Returns NULL on error.
120*635a8641SAndroid Build Coastguard Worker   //
121*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
122*635a8641SAndroid Build Coastguard Worker   virtual std::unique_ptr<Response> CallMethodAndBlock(MethodCall* method_call,
123*635a8641SAndroid Build Coastguard Worker                                                        int timeout_ms);
124*635a8641SAndroid Build Coastguard Worker 
125*635a8641SAndroid Build Coastguard Worker   // Requests to call the method of the remote object.
126*635a8641SAndroid Build Coastguard Worker   //
127*635a8641SAndroid Build Coastguard Worker   // |callback| will be called in the origin thread, once the method call
128*635a8641SAndroid Build Coastguard Worker   // is complete. As it's called in the origin thread, |callback| can
129*635a8641SAndroid Build Coastguard Worker   // safely reference objects in the origin thread (i.e. UI thread in most
130*635a8641SAndroid Build Coastguard Worker   // cases).
131*635a8641SAndroid Build Coastguard Worker   //
132*635a8641SAndroid Build Coastguard Worker   // If the method call is successful, a pointer to Response object will
133*635a8641SAndroid Build Coastguard Worker   // be passed to the callback. If unsuccessful, nullptr will be passed to
134*635a8641SAndroid Build Coastguard Worker   // the callback.
135*635a8641SAndroid Build Coastguard Worker   //
136*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
137*635a8641SAndroid Build Coastguard Worker   virtual void CallMethod(MethodCall* method_call,
138*635a8641SAndroid Build Coastguard Worker                           int timeout_ms,
139*635a8641SAndroid Build Coastguard Worker                           ResponseCallback callback);
140*635a8641SAndroid Build Coastguard Worker 
141*635a8641SAndroid Build Coastguard Worker   // Requests to call the method of the remote object.
142*635a8641SAndroid Build Coastguard Worker   //
143*635a8641SAndroid Build Coastguard Worker   // This is almost as same as CallMethod() defined above.
144*635a8641SAndroid Build Coastguard Worker   // The difference is that, the |callback| can take ErrorResponse.
145*635a8641SAndroid Build Coastguard Worker   // In case of error, ErrorResponse object is passed to the |callback|
146*635a8641SAndroid Build Coastguard Worker   // if the remote object returned an error, or nullptr if a response was not
147*635a8641SAndroid Build Coastguard Worker   // received at all (e.g., D-Bus connection is not established). In either
148*635a8641SAndroid Build Coastguard Worker   // error case, Response* should be nullptr.
149*635a8641SAndroid Build Coastguard Worker   virtual void CallMethodWithErrorResponse(MethodCall* method_call,
150*635a8641SAndroid Build Coastguard Worker                                            int timeout_ms,
151*635a8641SAndroid Build Coastguard Worker                                            ResponseOrErrorCallback callback);
152*635a8641SAndroid Build Coastguard Worker 
153*635a8641SAndroid Build Coastguard Worker   // DEPRECATED. Please use CallMethodWithErrorResponse() instead.
154*635a8641SAndroid Build Coastguard Worker   // TODO(hidehiko): Remove this when migration is done.
155*635a8641SAndroid Build Coastguard Worker   // Requests to call the method of the remote object.
156*635a8641SAndroid Build Coastguard Worker   //
157*635a8641SAndroid Build Coastguard Worker   // |callback| and |error_callback| will be called in the origin thread, once
158*635a8641SAndroid Build Coastguard Worker   // the method call is complete. As it's called in the origin thread,
159*635a8641SAndroid Build Coastguard Worker   // |callback| can safely reference objects in the origin thread (i.e.
160*635a8641SAndroid Build Coastguard Worker   // UI thread in most cases).
161*635a8641SAndroid Build Coastguard Worker   //
162*635a8641SAndroid Build Coastguard Worker   // If the method call is successful, |callback| will be invoked with a
163*635a8641SAndroid Build Coastguard Worker   // Response object. If unsuccessful, |error_callback| will be invoked with an
164*635a8641SAndroid Build Coastguard Worker   // ErrorResponse object (if the remote object returned an error) or nullptr
165*635a8641SAndroid Build Coastguard Worker   // (if a response was not received at all).
166*635a8641SAndroid Build Coastguard Worker   //
167*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
168*635a8641SAndroid Build Coastguard Worker   virtual void CallMethodWithErrorCallback(MethodCall* method_call,
169*635a8641SAndroid Build Coastguard Worker                                            int timeout_ms,
170*635a8641SAndroid Build Coastguard Worker                                            ResponseCallback callback,
171*635a8641SAndroid Build Coastguard Worker                                            ErrorCallback error_callback);
172*635a8641SAndroid Build Coastguard Worker 
173*635a8641SAndroid Build Coastguard Worker   // Requests to connect to the signal from the remote object.
174*635a8641SAndroid Build Coastguard Worker   //
175*635a8641SAndroid Build Coastguard Worker   // |signal_callback| will be called in the origin thread, when the
176*635a8641SAndroid Build Coastguard Worker   // signal is received from the remote object. As it's called in the
177*635a8641SAndroid Build Coastguard Worker   // origin thread, |signal_callback| can safely reference objects in the
178*635a8641SAndroid Build Coastguard Worker   // origin thread (i.e. UI thread in most cases).
179*635a8641SAndroid Build Coastguard Worker   //
180*635a8641SAndroid Build Coastguard Worker   // |on_connected_callback| is called when the object proxy is connected
181*635a8641SAndroid Build Coastguard Worker   // to the signal, or failed to be connected, in the origin thread.
182*635a8641SAndroid Build Coastguard Worker   //
183*635a8641SAndroid Build Coastguard Worker   // If a SignalCallback has already been registered for the given
184*635a8641SAndroid Build Coastguard Worker   // |interface_name| and |signal_name|, |signal_callback| will be
185*635a8641SAndroid Build Coastguard Worker   // added to the list of callbacks for |interface_name| and
186*635a8641SAndroid Build Coastguard Worker   // |signal_name|.
187*635a8641SAndroid Build Coastguard Worker   //
188*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
189*635a8641SAndroid Build Coastguard Worker   virtual void ConnectToSignal(const std::string& interface_name,
190*635a8641SAndroid Build Coastguard Worker                                const std::string& signal_name,
191*635a8641SAndroid Build Coastguard Worker                                SignalCallback signal_callback,
192*635a8641SAndroid Build Coastguard Worker                                OnConnectedCallback on_connected_callback);
193*635a8641SAndroid Build Coastguard Worker 
194*635a8641SAndroid Build Coastguard Worker   // Sets a callback for "NameOwnerChanged" signal. The callback is called on
195*635a8641SAndroid Build Coastguard Worker   // the origin thread when D-Bus system sends "NameOwnerChanged" for the name
196*635a8641SAndroid Build Coastguard Worker   // represented by |service_name_|.
197*635a8641SAndroid Build Coastguard Worker   virtual void SetNameOwnerChangedCallback(NameOwnerChangedCallback callback);
198*635a8641SAndroid Build Coastguard Worker 
199*635a8641SAndroid Build Coastguard Worker   // Registers |callback| to run when the service becomes available. If the
200*635a8641SAndroid Build Coastguard Worker   // service is already available, or if connecting to the name-owner-changed
201*635a8641SAndroid Build Coastguard Worker   // signal fails, |callback| will be run once asynchronously. Otherwise,
202*635a8641SAndroid Build Coastguard Worker   // |callback| will be run once in the future after the service becomes
203*635a8641SAndroid Build Coastguard Worker   // available.
204*635a8641SAndroid Build Coastguard Worker   virtual void WaitForServiceToBeAvailable(
205*635a8641SAndroid Build Coastguard Worker       WaitForServiceToBeAvailableCallback callback);
206*635a8641SAndroid Build Coastguard Worker 
207*635a8641SAndroid Build Coastguard Worker   // Detaches from the remote object. The Bus object will take care of
208*635a8641SAndroid Build Coastguard Worker   // detaching so you don't have to do this manually.
209*635a8641SAndroid Build Coastguard Worker   //
210*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
211*635a8641SAndroid Build Coastguard Worker   virtual void Detach();
212*635a8641SAndroid Build Coastguard Worker 
object_path()213*635a8641SAndroid Build Coastguard Worker   const ObjectPath& object_path() const { return object_path_; }
214*635a8641SAndroid Build Coastguard Worker 
215*635a8641SAndroid Build Coastguard Worker  protected:
216*635a8641SAndroid Build Coastguard Worker   // This is protected, so we can define sub classes.
217*635a8641SAndroid Build Coastguard Worker   virtual ~ObjectProxy();
218*635a8641SAndroid Build Coastguard Worker 
219*635a8641SAndroid Build Coastguard Worker  private:
220*635a8641SAndroid Build Coastguard Worker   friend class base::RefCountedThreadSafe<ObjectProxy>;
221*635a8641SAndroid Build Coastguard Worker 
222*635a8641SAndroid Build Coastguard Worker   // Callback passed to CallMethod and its family should be deleted on the
223*635a8641SAndroid Build Coastguard Worker   // origin thread in any cases. This class manages the work.
224*635a8641SAndroid Build Coastguard Worker   class ReplyCallbackHolder {
225*635a8641SAndroid Build Coastguard Worker    public:
226*635a8641SAndroid Build Coastguard Worker     // Designed to be created on the origin thread.
227*635a8641SAndroid Build Coastguard Worker     // Both |origin_task_runner| and |callback| must not be null.
228*635a8641SAndroid Build Coastguard Worker     ReplyCallbackHolder(scoped_refptr<base::TaskRunner> origin_task_runner,
229*635a8641SAndroid Build Coastguard Worker                         ResponseOrErrorCallback callback);
230*635a8641SAndroid Build Coastguard Worker 
231*635a8641SAndroid Build Coastguard Worker     // This is movable to be bound to an OnceCallback.
232*635a8641SAndroid Build Coastguard Worker     ReplyCallbackHolder(ReplyCallbackHolder&& other);
233*635a8641SAndroid Build Coastguard Worker 
234*635a8641SAndroid Build Coastguard Worker     // |callback_| needs to be destroyed on the origin thread.
235*635a8641SAndroid Build Coastguard Worker     // If this is not destroyed on non-origin thread, it PostTask()s the
236*635a8641SAndroid Build Coastguard Worker     // callback to the origin thread for destroying.
237*635a8641SAndroid Build Coastguard Worker     ~ReplyCallbackHolder();
238*635a8641SAndroid Build Coastguard Worker 
239*635a8641SAndroid Build Coastguard Worker     // Returns |callback_| with releasing its ownership.
240*635a8641SAndroid Build Coastguard Worker     // This must be called on the origin thread.
241*635a8641SAndroid Build Coastguard Worker     ResponseOrErrorCallback ReleaseCallback();
242*635a8641SAndroid Build Coastguard Worker 
243*635a8641SAndroid Build Coastguard Worker    private:
244*635a8641SAndroid Build Coastguard Worker     scoped_refptr<base::TaskRunner> origin_task_runner_;
245*635a8641SAndroid Build Coastguard Worker     ResponseOrErrorCallback callback_;
246*635a8641SAndroid Build Coastguard Worker     DISALLOW_COPY_AND_ASSIGN(ReplyCallbackHolder);
247*635a8641SAndroid Build Coastguard Worker   };
248*635a8641SAndroid Build Coastguard Worker 
249*635a8641SAndroid Build Coastguard Worker   // Starts the async method call. This is a helper function to implement
250*635a8641SAndroid Build Coastguard Worker   // CallMethod().
251*635a8641SAndroid Build Coastguard Worker   void StartAsyncMethodCall(int timeout_ms,
252*635a8641SAndroid Build Coastguard Worker                             DBusMessage* request_message,
253*635a8641SAndroid Build Coastguard Worker                             ReplyCallbackHolder callback_holder,
254*635a8641SAndroid Build Coastguard Worker                             base::TimeTicks start_time);
255*635a8641SAndroid Build Coastguard Worker 
256*635a8641SAndroid Build Coastguard Worker   // Called when the pending call is complete.
257*635a8641SAndroid Build Coastguard Worker   void OnPendingCallIsComplete(ReplyCallbackHolder callback_holder,
258*635a8641SAndroid Build Coastguard Worker                                base::TimeTicks start_time,
259*635a8641SAndroid Build Coastguard Worker                                DBusPendingCall* pending_call);
260*635a8641SAndroid Build Coastguard Worker 
261*635a8641SAndroid Build Coastguard Worker   // Runs the ResponseOrErrorCallback with the given response object.
262*635a8641SAndroid Build Coastguard Worker   void RunResponseOrErrorCallback(ReplyCallbackHolder callback_holderk,
263*635a8641SAndroid Build Coastguard Worker                                   base::TimeTicks start_time,
264*635a8641SAndroid Build Coastguard Worker                                   Response* response,
265*635a8641SAndroid Build Coastguard Worker                                   ErrorResponse* error_response);
266*635a8641SAndroid Build Coastguard Worker 
267*635a8641SAndroid Build Coastguard Worker   // Connects to NameOwnerChanged signal.
268*635a8641SAndroid Build Coastguard Worker   bool ConnectToNameOwnerChangedSignal();
269*635a8641SAndroid Build Coastguard Worker 
270*635a8641SAndroid Build Coastguard Worker   // Helper function for ConnectToSignal().
271*635a8641SAndroid Build Coastguard Worker   bool ConnectToSignalInternal(const std::string& interface_name,
272*635a8641SAndroid Build Coastguard Worker                                const std::string& signal_name,
273*635a8641SAndroid Build Coastguard Worker                                SignalCallback signal_callback);
274*635a8641SAndroid Build Coastguard Worker 
275*635a8641SAndroid Build Coastguard Worker   // Helper function for WaitForServiceToBeAvailable().
276*635a8641SAndroid Build Coastguard Worker   void WaitForServiceToBeAvailableInternal();
277*635a8641SAndroid Build Coastguard Worker 
278*635a8641SAndroid Build Coastguard Worker   // Handles the incoming request messages and dispatches to the signal
279*635a8641SAndroid Build Coastguard Worker   // callbacks.
280*635a8641SAndroid Build Coastguard Worker   DBusHandlerResult HandleMessage(DBusConnection* connection,
281*635a8641SAndroid Build Coastguard Worker                                   DBusMessage* raw_message);
282*635a8641SAndroid Build Coastguard Worker 
283*635a8641SAndroid Build Coastguard Worker   // Runs the method. Helper function for HandleMessage().
284*635a8641SAndroid Build Coastguard Worker   void RunMethod(base::TimeTicks start_time,
285*635a8641SAndroid Build Coastguard Worker                  std::vector<SignalCallback> signal_callbacks,
286*635a8641SAndroid Build Coastguard Worker                  Signal* signal);
287*635a8641SAndroid Build Coastguard Worker 
288*635a8641SAndroid Build Coastguard Worker   // Redirects the function call to HandleMessage().
289*635a8641SAndroid Build Coastguard Worker   static DBusHandlerResult HandleMessageThunk(DBusConnection* connection,
290*635a8641SAndroid Build Coastguard Worker                                               DBusMessage* raw_message,
291*635a8641SAndroid Build Coastguard Worker                                               void* user_data);
292*635a8641SAndroid Build Coastguard Worker 
293*635a8641SAndroid Build Coastguard Worker   // Helper method for logging response errors appropriately.
294*635a8641SAndroid Build Coastguard Worker   void LogMethodCallFailure(const base::StringPiece& interface_name,
295*635a8641SAndroid Build Coastguard Worker                             const base::StringPiece& method_name,
296*635a8641SAndroid Build Coastguard Worker                             const base::StringPiece& error_name,
297*635a8641SAndroid Build Coastguard Worker                             const base::StringPiece& error_message) const;
298*635a8641SAndroid Build Coastguard Worker 
299*635a8641SAndroid Build Coastguard Worker   // Used as ResponseOrErrorCallback by CallMethod().
300*635a8641SAndroid Build Coastguard Worker   // Logs error message, and drops |error_response| from the arguments to pass
301*635a8641SAndroid Build Coastguard Worker   // |response_callback|.
302*635a8641SAndroid Build Coastguard Worker   void OnCallMethod(const std::string& interface_name,
303*635a8641SAndroid Build Coastguard Worker                     const std::string& method_name,
304*635a8641SAndroid Build Coastguard Worker                     ResponseCallback response_callback,
305*635a8641SAndroid Build Coastguard Worker                     Response* response,
306*635a8641SAndroid Build Coastguard Worker                     ErrorResponse* error_response);
307*635a8641SAndroid Build Coastguard Worker 
308*635a8641SAndroid Build Coastguard Worker   // Adds the match rule to the bus and associate the callback with the signal.
309*635a8641SAndroid Build Coastguard Worker   bool AddMatchRuleWithCallback(const std::string& match_rule,
310*635a8641SAndroid Build Coastguard Worker                                 const std::string& absolute_signal_name,
311*635a8641SAndroid Build Coastguard Worker                                 SignalCallback signal_callback);
312*635a8641SAndroid Build Coastguard Worker 
313*635a8641SAndroid Build Coastguard Worker   // Adds the match rule to the bus so that HandleMessage can see the signal.
314*635a8641SAndroid Build Coastguard Worker   bool AddMatchRuleWithoutCallback(const std::string& match_rule,
315*635a8641SAndroid Build Coastguard Worker                                    const std::string& absolute_signal_name);
316*635a8641SAndroid Build Coastguard Worker 
317*635a8641SAndroid Build Coastguard Worker   // Calls D-Bus's GetNameOwner method synchronously to update
318*635a8641SAndroid Build Coastguard Worker   // |service_name_owner_| with the current owner of |service_name_|.
319*635a8641SAndroid Build Coastguard Worker   //
320*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
321*635a8641SAndroid Build Coastguard Worker   void UpdateNameOwnerAndBlock();
322*635a8641SAndroid Build Coastguard Worker 
323*635a8641SAndroid Build Coastguard Worker   // Handles NameOwnerChanged signal from D-Bus's special message bus.
324*635a8641SAndroid Build Coastguard Worker   DBusHandlerResult HandleNameOwnerChanged(
325*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<dbus::Signal> signal);
326*635a8641SAndroid Build Coastguard Worker 
327*635a8641SAndroid Build Coastguard Worker   // Runs |name_owner_changed_callback_|.
328*635a8641SAndroid Build Coastguard Worker   void RunNameOwnerChangedCallback(const std::string& old_owner,
329*635a8641SAndroid Build Coastguard Worker                                    const std::string& new_owner);
330*635a8641SAndroid Build Coastguard Worker 
331*635a8641SAndroid Build Coastguard Worker   // Runs |wait_for_service_to_be_available_callbacks_|.
332*635a8641SAndroid Build Coastguard Worker   void RunWaitForServiceToBeAvailableCallbacks(bool service_is_available);
333*635a8641SAndroid Build Coastguard Worker 
334*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus_;
335*635a8641SAndroid Build Coastguard Worker   std::string service_name_;
336*635a8641SAndroid Build Coastguard Worker   ObjectPath object_path_;
337*635a8641SAndroid Build Coastguard Worker 
338*635a8641SAndroid Build Coastguard Worker   // The method table where keys are absolute signal names (i.e. interface
339*635a8641SAndroid Build Coastguard Worker   // name + signal name), and values are lists of the corresponding callbacks.
340*635a8641SAndroid Build Coastguard Worker   using MethodTable = std::map<std::string, std::vector<SignalCallback>>;
341*635a8641SAndroid Build Coastguard Worker   MethodTable method_table_;
342*635a8641SAndroid Build Coastguard Worker 
343*635a8641SAndroid Build Coastguard Worker   // The callback called when NameOwnerChanged signal is received.
344*635a8641SAndroid Build Coastguard Worker   NameOwnerChangedCallback name_owner_changed_callback_;
345*635a8641SAndroid Build Coastguard Worker 
346*635a8641SAndroid Build Coastguard Worker   // Called when the service becomes available.
347*635a8641SAndroid Build Coastguard Worker   std::vector<WaitForServiceToBeAvailableCallback>
348*635a8641SAndroid Build Coastguard Worker       wait_for_service_to_be_available_callbacks_;
349*635a8641SAndroid Build Coastguard Worker 
350*635a8641SAndroid Build Coastguard Worker   std::set<std::string> match_rules_;
351*635a8641SAndroid Build Coastguard Worker 
352*635a8641SAndroid Build Coastguard Worker   const bool ignore_service_unknown_errors_;
353*635a8641SAndroid Build Coastguard Worker 
354*635a8641SAndroid Build Coastguard Worker   // Known name owner of the well-known bus name represented by |service_name_|.
355*635a8641SAndroid Build Coastguard Worker   std::string service_name_owner_;
356*635a8641SAndroid Build Coastguard Worker 
357*635a8641SAndroid Build Coastguard Worker   std::set<DBusPendingCall*> pending_calls_;
358*635a8641SAndroid Build Coastguard Worker 
359*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ObjectProxy);
360*635a8641SAndroid Build Coastguard Worker };
361*635a8641SAndroid Build Coastguard Worker 
362*635a8641SAndroid Build Coastguard Worker }  // namespace dbus
363*635a8641SAndroid Build Coastguard Worker 
364*635a8641SAndroid Build Coastguard Worker #endif  // DBUS_OBJECT_PROXY_H_
365