xref: /aosp_15_r20/external/libchrome/dbus/bus.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_BUS_H_
6*635a8641SAndroid Build Coastguard Worker #define DBUS_BUS_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <dbus/dbus.h>
9*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <map>
12*635a8641SAndroid Build Coastguard Worker #include <set>
13*635a8641SAndroid Build Coastguard Worker #include <string>
14*635a8641SAndroid Build Coastguard Worker #include <utility>
15*635a8641SAndroid Build Coastguard Worker #include <vector>
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/waitable_event.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h"
22*635a8641SAndroid Build Coastguard Worker #include "dbus/dbus_export.h"
23*635a8641SAndroid Build Coastguard Worker #include "dbus/object_path.h"
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker namespace base {
26*635a8641SAndroid Build Coastguard Worker class SequencedTaskRunner;
27*635a8641SAndroid Build Coastguard Worker class SingleThreadTaskRunner;
28*635a8641SAndroid Build Coastguard Worker class TaskRunner;
29*635a8641SAndroid Build Coastguard Worker }
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker namespace dbus {
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker class ExportedObject;
34*635a8641SAndroid Build Coastguard Worker class ObjectManager;
35*635a8641SAndroid Build Coastguard Worker class ObjectProxy;
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker // Bus is used to establish a connection with D-Bus, create object
38*635a8641SAndroid Build Coastguard Worker // proxies, and export objects.
39*635a8641SAndroid Build Coastguard Worker //
40*635a8641SAndroid Build Coastguard Worker // For asynchronous operations such as an asynchronous method call, the
41*635a8641SAndroid Build Coastguard Worker // bus object will use a task runner to monitor the underlying file
42*635a8641SAndroid Build Coastguard Worker // descriptor used for D-Bus communication. By default, the bus will use
43*635a8641SAndroid Build Coastguard Worker // the current thread's task runner. If |dbus_task_runner| option is
44*635a8641SAndroid Build Coastguard Worker // specified, the bus will use that task runner instead.
45*635a8641SAndroid Build Coastguard Worker //
46*635a8641SAndroid Build Coastguard Worker // THREADING
47*635a8641SAndroid Build Coastguard Worker //
48*635a8641SAndroid Build Coastguard Worker // In the D-Bus library, we use the two threads:
49*635a8641SAndroid Build Coastguard Worker //
50*635a8641SAndroid Build Coastguard Worker // - The origin thread: the thread that created the Bus object.
51*635a8641SAndroid Build Coastguard Worker // - The D-Bus thread: the thread servicing |dbus_task_runner|.
52*635a8641SAndroid Build Coastguard Worker //
53*635a8641SAndroid Build Coastguard Worker // The origin thread is usually Chrome's UI thread. The D-Bus thread is
54*635a8641SAndroid Build Coastguard Worker // usually a dedicated thread for the D-Bus library.
55*635a8641SAndroid Build Coastguard Worker //
56*635a8641SAndroid Build Coastguard Worker // BLOCKING CALLS
57*635a8641SAndroid Build Coastguard Worker //
58*635a8641SAndroid Build Coastguard Worker // Functions that issue blocking calls are marked "BLOCKING CALL" and
59*635a8641SAndroid Build Coastguard Worker // these functions should be called in the D-Bus thread (if
60*635a8641SAndroid Build Coastguard Worker // supplied). AssertOnDBusThread() is placed in these functions.
61*635a8641SAndroid Build Coastguard Worker //
62*635a8641SAndroid Build Coastguard Worker // Note that it's hard to tell if a libdbus function is actually blocking
63*635a8641SAndroid Build Coastguard Worker // or not (ex. dbus_bus_request_name() internally calls
64*635a8641SAndroid Build Coastguard Worker // dbus_connection_send_with_reply_and_block(), which is a blocking
65*635a8641SAndroid Build Coastguard Worker // call). To err on the safe side, we consider all libdbus functions that
66*635a8641SAndroid Build Coastguard Worker // deal with the connection to dbus-daemon to be blocking.
67*635a8641SAndroid Build Coastguard Worker //
68*635a8641SAndroid Build Coastguard Worker // SHUTDOWN
69*635a8641SAndroid Build Coastguard Worker //
70*635a8641SAndroid Build Coastguard Worker // The Bus object must be shut down manually by ShutdownAndBlock() and
71*635a8641SAndroid Build Coastguard Worker // friends. We require the manual shutdown to make the operation explicit
72*635a8641SAndroid Build Coastguard Worker // rather than doing it silently in the destructor.
73*635a8641SAndroid Build Coastguard Worker //
74*635a8641SAndroid Build Coastguard Worker // EXAMPLE USAGE:
75*635a8641SAndroid Build Coastguard Worker //
76*635a8641SAndroid Build Coastguard Worker // Synchronous method call:
77*635a8641SAndroid Build Coastguard Worker //
78*635a8641SAndroid Build Coastguard Worker //   dbus::Bus::Options options;
79*635a8641SAndroid Build Coastguard Worker //   // Set up the bus options here.
80*635a8641SAndroid Build Coastguard Worker //   ...
81*635a8641SAndroid Build Coastguard Worker //   dbus::Bus bus(options);
82*635a8641SAndroid Build Coastguard Worker //
83*635a8641SAndroid Build Coastguard Worker //   dbus::ObjectProxy* object_proxy =
84*635a8641SAndroid Build Coastguard Worker //       bus.GetObjectProxy(service_name, object_path);
85*635a8641SAndroid Build Coastguard Worker //
86*635a8641SAndroid Build Coastguard Worker //   dbus::MethodCall method_call(interface_name, method_name);
87*635a8641SAndroid Build Coastguard Worker //   std::unique_ptr<dbus::Response> response(
88*635a8641SAndroid Build Coastguard Worker //       object_proxy.CallMethodAndBlock(&method_call, timeout_ms));
89*635a8641SAndroid Build Coastguard Worker //   if (response.get() != nullptr) {  // Success.
90*635a8641SAndroid Build Coastguard Worker //     ...
91*635a8641SAndroid Build Coastguard Worker //   }
92*635a8641SAndroid Build Coastguard Worker //
93*635a8641SAndroid Build Coastguard Worker // Asynchronous method call:
94*635a8641SAndroid Build Coastguard Worker //
95*635a8641SAndroid Build Coastguard Worker //   void OnResponse(dbus::Response* response) {
96*635a8641SAndroid Build Coastguard Worker //     // response is NULL if the method call failed.
97*635a8641SAndroid Build Coastguard Worker //     if (!response)
98*635a8641SAndroid Build Coastguard Worker //       return;
99*635a8641SAndroid Build Coastguard Worker //   }
100*635a8641SAndroid Build Coastguard Worker //
101*635a8641SAndroid Build Coastguard Worker //   ...
102*635a8641SAndroid Build Coastguard Worker //   object_proxy.CallMethod(&method_call, timeout_ms,
103*635a8641SAndroid Build Coastguard Worker //                           base::Bind(&OnResponse));
104*635a8641SAndroid Build Coastguard Worker //
105*635a8641SAndroid Build Coastguard Worker // Exporting a method:
106*635a8641SAndroid Build Coastguard Worker //
107*635a8641SAndroid Build Coastguard Worker //   void Echo(dbus::MethodCall* method_call,
108*635a8641SAndroid Build Coastguard Worker //             dbus::ExportedObject::ResponseSender response_sender) {
109*635a8641SAndroid Build Coastguard Worker //     // Do something with method_call.
110*635a8641SAndroid Build Coastguard Worker //     Response* response = Response::FromMethodCall(method_call);
111*635a8641SAndroid Build Coastguard Worker //     // Build response here.
112*635a8641SAndroid Build Coastguard Worker //     // Can send an immediate response here to implement a synchronous service
113*635a8641SAndroid Build Coastguard Worker //     // or store the response_sender and send a response later to implement an
114*635a8641SAndroid Build Coastguard Worker //     // asynchronous service.
115*635a8641SAndroid Build Coastguard Worker //     response_sender.Run(response);
116*635a8641SAndroid Build Coastguard Worker //   }
117*635a8641SAndroid Build Coastguard Worker //
118*635a8641SAndroid Build Coastguard Worker //   void OnExported(const std::string& interface_name,
119*635a8641SAndroid Build Coastguard Worker //                   const ObjectPath& object_path,
120*635a8641SAndroid Build Coastguard Worker //                   bool success) {
121*635a8641SAndroid Build Coastguard Worker //     // success is true if the method was exported successfully.
122*635a8641SAndroid Build Coastguard Worker //   }
123*635a8641SAndroid Build Coastguard Worker //
124*635a8641SAndroid Build Coastguard Worker //   ...
125*635a8641SAndroid Build Coastguard Worker //   dbus::ExportedObject* exported_object =
126*635a8641SAndroid Build Coastguard Worker //       bus.GetExportedObject(service_name, object_path);
127*635a8641SAndroid Build Coastguard Worker //   exported_object.ExportMethod(interface_name, method_name,
128*635a8641SAndroid Build Coastguard Worker //                                base::Bind(&Echo),
129*635a8641SAndroid Build Coastguard Worker //                                base::Bind(&OnExported));
130*635a8641SAndroid Build Coastguard Worker //
131*635a8641SAndroid Build Coastguard Worker // WHY IS THIS A REF COUNTED OBJECT?
132*635a8641SAndroid Build Coastguard Worker //
133*635a8641SAndroid Build Coastguard Worker // Bus is a ref counted object, to ensure that |this| of the object is
134*635a8641SAndroid Build Coastguard Worker // alive when callbacks referencing |this| are called. However, after the
135*635a8641SAndroid Build Coastguard Worker // bus is shut down, |connection_| can be NULL. Hence, callbacks should
136*635a8641SAndroid Build Coastguard Worker // not rely on that |connection_| is alive.
137*635a8641SAndroid Build Coastguard Worker class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
138*635a8641SAndroid Build Coastguard Worker  public:
139*635a8641SAndroid Build Coastguard Worker   // Specifies the bus type. SESSION is used to communicate with per-user
140*635a8641SAndroid Build Coastguard Worker   // services like GNOME applications. SYSTEM is used to communicate with
141*635a8641SAndroid Build Coastguard Worker   // system-wide services like NetworkManager. CUSTOM_ADDRESS is used to
142*635a8641SAndroid Build Coastguard Worker   // communicate with an user specified address.
143*635a8641SAndroid Build Coastguard Worker   enum BusType {
144*635a8641SAndroid Build Coastguard Worker     SESSION = DBUS_BUS_SESSION,
145*635a8641SAndroid Build Coastguard Worker     SYSTEM = DBUS_BUS_SYSTEM,
146*635a8641SAndroid Build Coastguard Worker     CUSTOM_ADDRESS,
147*635a8641SAndroid Build Coastguard Worker   };
148*635a8641SAndroid Build Coastguard Worker 
149*635a8641SAndroid Build Coastguard Worker   // Specifies the connection type. PRIVATE should usually be used unless
150*635a8641SAndroid Build Coastguard Worker   // you are sure that SHARED is safe for you, which is unlikely the case
151*635a8641SAndroid Build Coastguard Worker   // in Chrome.
152*635a8641SAndroid Build Coastguard Worker   //
153*635a8641SAndroid Build Coastguard Worker   // PRIVATE gives you a private connection, that won't be shared with
154*635a8641SAndroid Build Coastguard Worker   // other Bus objects.
155*635a8641SAndroid Build Coastguard Worker   //
156*635a8641SAndroid Build Coastguard Worker   // SHARED gives you a connection shared among other Bus objects, which
157*635a8641SAndroid Build Coastguard Worker   // is unsafe if the connection is shared with multiple threads.
158*635a8641SAndroid Build Coastguard Worker   enum ConnectionType {
159*635a8641SAndroid Build Coastguard Worker     PRIVATE,
160*635a8641SAndroid Build Coastguard Worker     SHARED,
161*635a8641SAndroid Build Coastguard Worker   };
162*635a8641SAndroid Build Coastguard Worker 
163*635a8641SAndroid Build Coastguard Worker   // Specifies whether the GetServiceOwnerAndBlock call should report or
164*635a8641SAndroid Build Coastguard Worker   // suppress errors.
165*635a8641SAndroid Build Coastguard Worker   enum GetServiceOwnerOption {
166*635a8641SAndroid Build Coastguard Worker     REPORT_ERRORS,
167*635a8641SAndroid Build Coastguard Worker     SUPPRESS_ERRORS,
168*635a8641SAndroid Build Coastguard Worker   };
169*635a8641SAndroid Build Coastguard Worker 
170*635a8641SAndroid Build Coastguard Worker   // Specifies service ownership options.
171*635a8641SAndroid Build Coastguard Worker   //
172*635a8641SAndroid Build Coastguard Worker   // REQUIRE_PRIMARY indicates that you require primary ownership of the
173*635a8641SAndroid Build Coastguard Worker   // service name.
174*635a8641SAndroid Build Coastguard Worker   //
175*635a8641SAndroid Build Coastguard Worker   // ALLOW_REPLACEMENT indicates that you'll allow another connection to
176*635a8641SAndroid Build Coastguard Worker   // steal ownership of this service name from you.
177*635a8641SAndroid Build Coastguard Worker   //
178*635a8641SAndroid Build Coastguard Worker   // REQUIRE_PRIMARY_ALLOW_REPLACEMENT does the obvious.
179*635a8641SAndroid Build Coastguard Worker   enum ServiceOwnershipOptions {
180*635a8641SAndroid Build Coastguard Worker     REQUIRE_PRIMARY = (DBUS_NAME_FLAG_DO_NOT_QUEUE |
181*635a8641SAndroid Build Coastguard Worker                        DBUS_NAME_FLAG_REPLACE_EXISTING),
182*635a8641SAndroid Build Coastguard Worker     REQUIRE_PRIMARY_ALLOW_REPLACEMENT = (REQUIRE_PRIMARY |
183*635a8641SAndroid Build Coastguard Worker                                          DBUS_NAME_FLAG_ALLOW_REPLACEMENT),
184*635a8641SAndroid Build Coastguard Worker   };
185*635a8641SAndroid Build Coastguard Worker 
186*635a8641SAndroid Build Coastguard Worker   // Options used to create a Bus object.
187*635a8641SAndroid Build Coastguard Worker   struct CHROME_DBUS_EXPORT Options {
188*635a8641SAndroid Build Coastguard Worker     Options();
189*635a8641SAndroid Build Coastguard Worker     ~Options();
190*635a8641SAndroid Build Coastguard Worker 
191*635a8641SAndroid Build Coastguard Worker     BusType bus_type;  // SESSION by default.
192*635a8641SAndroid Build Coastguard Worker     ConnectionType connection_type;  // PRIVATE by default.
193*635a8641SAndroid Build Coastguard Worker     // If dbus_task_runner is set, the bus object will use that
194*635a8641SAndroid Build Coastguard Worker     // task runner to process asynchronous operations.
195*635a8641SAndroid Build Coastguard Worker     //
196*635a8641SAndroid Build Coastguard Worker     // The thread servicing the task runner should meet the following
197*635a8641SAndroid Build Coastguard Worker     // requirements:
198*635a8641SAndroid Build Coastguard Worker     // 1) Already running.
199*635a8641SAndroid Build Coastguard Worker     // 2) Has a MessageLoopForIO.
200*635a8641SAndroid Build Coastguard Worker     scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
201*635a8641SAndroid Build Coastguard Worker 
202*635a8641SAndroid Build Coastguard Worker     // Specifies the server addresses to be connected. If you want to
203*635a8641SAndroid Build Coastguard Worker     // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to
204*635a8641SAndroid Build Coastguard Worker     // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to
205*635a8641SAndroid Build Coastguard Worker     // connect to. The format of this address value is the dbus address style
206*635a8641SAndroid Build Coastguard Worker     // which is described in
207*635a8641SAndroid Build Coastguard Worker     // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
208*635a8641SAndroid Build Coastguard Worker     //
209*635a8641SAndroid Build Coastguard Worker     // EXAMPLE USAGE:
210*635a8641SAndroid Build Coastguard Worker     //   dbus::Bus::Options options;
211*635a8641SAndroid Build Coastguard Worker     //   options.bus_type = CUSTOM_ADDRESS;
212*635a8641SAndroid Build Coastguard Worker     //   options.address.assign("unix:path=/tmp/dbus-XXXXXXX");
213*635a8641SAndroid Build Coastguard Worker     //   // Set up other options
214*635a8641SAndroid Build Coastguard Worker     //   dbus::Bus bus(options);
215*635a8641SAndroid Build Coastguard Worker     //
216*635a8641SAndroid Build Coastguard Worker     //   // Do something.
217*635a8641SAndroid Build Coastguard Worker     //
218*635a8641SAndroid Build Coastguard Worker     std::string address;
219*635a8641SAndroid Build Coastguard Worker   };
220*635a8641SAndroid Build Coastguard Worker 
221*635a8641SAndroid Build Coastguard Worker   // Creates a Bus object. The actual connection will be established when
222*635a8641SAndroid Build Coastguard Worker   // Connect() is called.
223*635a8641SAndroid Build Coastguard Worker   explicit Bus(const Options& options);
224*635a8641SAndroid Build Coastguard Worker 
225*635a8641SAndroid Build Coastguard Worker   // Called when an ownership request is complete.
226*635a8641SAndroid Build Coastguard Worker   // Parameters:
227*635a8641SAndroid Build Coastguard Worker   // - the requested service name.
228*635a8641SAndroid Build Coastguard Worker   // - whether ownership has been obtained or not.
229*635a8641SAndroid Build Coastguard Worker   typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback;
230*635a8641SAndroid Build Coastguard Worker 
231*635a8641SAndroid Build Coastguard Worker   // Called when GetServiceOwner() completes.
232*635a8641SAndroid Build Coastguard Worker   // |service_owner| is the return value from GetServiceOwnerAndBlock().
233*635a8641SAndroid Build Coastguard Worker   typedef base::Callback<void (const std::string& service_owner)>
234*635a8641SAndroid Build Coastguard Worker       GetServiceOwnerCallback;
235*635a8641SAndroid Build Coastguard Worker 
236*635a8641SAndroid Build Coastguard Worker   // TODO(satorux): Remove the service name parameter as the caller of
237*635a8641SAndroid Build Coastguard Worker   // RequestOwnership() knows the service name.
238*635a8641SAndroid Build Coastguard Worker 
239*635a8641SAndroid Build Coastguard Worker   // Gets the object proxy for the given service name and the object path.
240*635a8641SAndroid Build Coastguard Worker   // The caller must not delete the returned object.
241*635a8641SAndroid Build Coastguard Worker   //
242*635a8641SAndroid Build Coastguard Worker   // Returns an existing object proxy if the bus object already owns the
243*635a8641SAndroid Build Coastguard Worker   // object proxy for the given service name and the object path.
244*635a8641SAndroid Build Coastguard Worker   // Never returns NULL.
245*635a8641SAndroid Build Coastguard Worker   //
246*635a8641SAndroid Build Coastguard Worker   // The bus will own all object proxies created by the bus, to ensure
247*635a8641SAndroid Build Coastguard Worker   // that the object proxies are detached from remote objects at the
248*635a8641SAndroid Build Coastguard Worker   // shutdown time of the bus.
249*635a8641SAndroid Build Coastguard Worker   //
250*635a8641SAndroid Build Coastguard Worker   // The object proxy is used to call methods of remote objects, and
251*635a8641SAndroid Build Coastguard Worker   // receive signals from them.
252*635a8641SAndroid Build Coastguard Worker   //
253*635a8641SAndroid Build Coastguard Worker   // |service_name| looks like "org.freedesktop.NetworkManager", and
254*635a8641SAndroid Build Coastguard Worker   // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
255*635a8641SAndroid Build Coastguard Worker   //
256*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
257*635a8641SAndroid Build Coastguard Worker   virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
258*635a8641SAndroid Build Coastguard Worker                                       const ObjectPath& object_path);
259*635a8641SAndroid Build Coastguard Worker 
260*635a8641SAndroid Build Coastguard Worker   // Same as above, but also takes a bitfield of ObjectProxy::Options.
261*635a8641SAndroid Build Coastguard Worker   // See object_proxy.h for available options.
262*635a8641SAndroid Build Coastguard Worker   virtual ObjectProxy* GetObjectProxyWithOptions(
263*635a8641SAndroid Build Coastguard Worker       const std::string& service_name,
264*635a8641SAndroid Build Coastguard Worker       const ObjectPath& object_path,
265*635a8641SAndroid Build Coastguard Worker       int options);
266*635a8641SAndroid Build Coastguard Worker 
267*635a8641SAndroid Build Coastguard Worker   // Removes the previously created object proxy for the given service
268*635a8641SAndroid Build Coastguard Worker   // name and the object path and releases its memory.
269*635a8641SAndroid Build Coastguard Worker   //
270*635a8641SAndroid Build Coastguard Worker   // If and object proxy for the given service name and object was
271*635a8641SAndroid Build Coastguard Worker   // created with GetObjectProxy, this function removes it from the
272*635a8641SAndroid Build Coastguard Worker   // bus object and detaches the ObjectProxy, invalidating any pointer
273*635a8641SAndroid Build Coastguard Worker   // previously acquired for it with GetObjectProxy. A subsequent call
274*635a8641SAndroid Build Coastguard Worker   // to GetObjectProxy will return a new object.
275*635a8641SAndroid Build Coastguard Worker   //
276*635a8641SAndroid Build Coastguard Worker   // All the object proxies are detached from remote objects at the
277*635a8641SAndroid Build Coastguard Worker   // shutdown time of the bus, but they can be detached early to reduce
278*635a8641SAndroid Build Coastguard Worker   // memory footprint and used match rules for the bus connection.
279*635a8641SAndroid Build Coastguard Worker   //
280*635a8641SAndroid Build Coastguard Worker   // |service_name| looks like "org.freedesktop.NetworkManager", and
281*635a8641SAndroid Build Coastguard Worker   // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
282*635a8641SAndroid Build Coastguard Worker   // |callback| is called when the object proxy is successfully removed and
283*635a8641SAndroid Build Coastguard Worker   // detached.
284*635a8641SAndroid Build Coastguard Worker   //
285*635a8641SAndroid Build Coastguard Worker   // The function returns true when there is an object proxy matching the
286*635a8641SAndroid Build Coastguard Worker   // |service_name| and |object_path| to remove, and calls |callback| when it
287*635a8641SAndroid Build Coastguard Worker   // is removed. Otherwise, it returns false and the |callback| function is
288*635a8641SAndroid Build Coastguard Worker   // never called. The |callback| argument must not be null.
289*635a8641SAndroid Build Coastguard Worker   //
290*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
291*635a8641SAndroid Build Coastguard Worker   virtual bool RemoveObjectProxy(const std::string& service_name,
292*635a8641SAndroid Build Coastguard Worker                                  const ObjectPath& object_path,
293*635a8641SAndroid Build Coastguard Worker                                  const base::Closure& callback);
294*635a8641SAndroid Build Coastguard Worker 
295*635a8641SAndroid Build Coastguard Worker   // Same as above, but also takes a bitfield of ObjectProxy::Options.
296*635a8641SAndroid Build Coastguard Worker   // See object_proxy.h for available options.
297*635a8641SAndroid Build Coastguard Worker   virtual bool RemoveObjectProxyWithOptions(
298*635a8641SAndroid Build Coastguard Worker       const std::string& service_name,
299*635a8641SAndroid Build Coastguard Worker       const ObjectPath& object_path,
300*635a8641SAndroid Build Coastguard Worker       int options,
301*635a8641SAndroid Build Coastguard Worker       const base::Closure& callback);
302*635a8641SAndroid Build Coastguard Worker 
303*635a8641SAndroid Build Coastguard Worker   // Gets the exported object for the given object path.
304*635a8641SAndroid Build Coastguard Worker   // The caller must not delete the returned object.
305*635a8641SAndroid Build Coastguard Worker   //
306*635a8641SAndroid Build Coastguard Worker   // Returns an existing exported object if the bus object already owns
307*635a8641SAndroid Build Coastguard Worker   // the exported object for the given object path. Never returns NULL.
308*635a8641SAndroid Build Coastguard Worker   //
309*635a8641SAndroid Build Coastguard Worker   // The bus will own all exported objects created by the bus, to ensure
310*635a8641SAndroid Build Coastguard Worker   // that the exported objects are unregistered at the shutdown time of
311*635a8641SAndroid Build Coastguard Worker   // the bus.
312*635a8641SAndroid Build Coastguard Worker   //
313*635a8641SAndroid Build Coastguard Worker   // The exported object is used to export methods of local objects, and
314*635a8641SAndroid Build Coastguard Worker   // send signal from them.
315*635a8641SAndroid Build Coastguard Worker   //
316*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
317*635a8641SAndroid Build Coastguard Worker   virtual ExportedObject* GetExportedObject(const ObjectPath& object_path);
318*635a8641SAndroid Build Coastguard Worker 
319*635a8641SAndroid Build Coastguard Worker   // Unregisters the exported object for the given object path |object_path|.
320*635a8641SAndroid Build Coastguard Worker   //
321*635a8641SAndroid Build Coastguard Worker   // Getting an exported object for the same object path after this call
322*635a8641SAndroid Build Coastguard Worker   // will return a new object, method calls on any remaining copies of the
323*635a8641SAndroid Build Coastguard Worker   // previous object will not be called.
324*635a8641SAndroid Build Coastguard Worker   //
325*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
326*635a8641SAndroid Build Coastguard Worker   virtual void UnregisterExportedObject(const ObjectPath& object_path);
327*635a8641SAndroid Build Coastguard Worker 
328*635a8641SAndroid Build Coastguard Worker 
329*635a8641SAndroid Build Coastguard Worker   // Gets an object manager for the given remote object path |object_path|
330*635a8641SAndroid Build Coastguard Worker   // exported by the service |service_name|.
331*635a8641SAndroid Build Coastguard Worker   //
332*635a8641SAndroid Build Coastguard Worker   // Returns an existing object manager if the bus object already owns a
333*635a8641SAndroid Build Coastguard Worker   // matching object manager, never returns NULL.
334*635a8641SAndroid Build Coastguard Worker   //
335*635a8641SAndroid Build Coastguard Worker   // The caller must not delete the returned object, the bus retains ownership
336*635a8641SAndroid Build Coastguard Worker   // of all object managers.
337*635a8641SAndroid Build Coastguard Worker   //
338*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
339*635a8641SAndroid Build Coastguard Worker   virtual ObjectManager* GetObjectManager(const std::string& service_name,
340*635a8641SAndroid Build Coastguard Worker                                           const ObjectPath& object_path);
341*635a8641SAndroid Build Coastguard Worker 
342*635a8641SAndroid Build Coastguard Worker   // Unregisters the object manager for the given remote object path
343*635a8641SAndroid Build Coastguard Worker   // |object_path| exported by the srevice |service_name|.
344*635a8641SAndroid Build Coastguard Worker   //
345*635a8641SAndroid Build Coastguard Worker   // Getting an object manager for the same remote object after this call
346*635a8641SAndroid Build Coastguard Worker   // will return a new object, method calls on any remaining copies of the
347*635a8641SAndroid Build Coastguard Worker   // previous object are not permitted.
348*635a8641SAndroid Build Coastguard Worker   //
349*635a8641SAndroid Build Coastguard Worker   // This method will asynchronously clean up any match rules that have been
350*635a8641SAndroid Build Coastguard Worker   // added for the object manager and invoke |callback| when the operation is
351*635a8641SAndroid Build Coastguard Worker   // complete. If this method returns false, then |callback| is never called.
352*635a8641SAndroid Build Coastguard Worker   // The |callback| argument must not be null.
353*635a8641SAndroid Build Coastguard Worker   //
354*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
355*635a8641SAndroid Build Coastguard Worker   virtual bool RemoveObjectManager(const std::string& service_name,
356*635a8641SAndroid Build Coastguard Worker                                    const ObjectPath& object_path,
357*635a8641SAndroid Build Coastguard Worker                                    const base::Closure& callback);
358*635a8641SAndroid Build Coastguard Worker 
359*635a8641SAndroid Build Coastguard Worker   // Shuts down the bus and blocks until it's done. More specifically, this
360*635a8641SAndroid Build Coastguard Worker   // function does the following:
361*635a8641SAndroid Build Coastguard Worker   //
362*635a8641SAndroid Build Coastguard Worker   // - Unregisters the object paths
363*635a8641SAndroid Build Coastguard Worker   // - Releases the service names
364*635a8641SAndroid Build Coastguard Worker   // - Closes the connection to dbus-daemon.
365*635a8641SAndroid Build Coastguard Worker   //
366*635a8641SAndroid Build Coastguard Worker   // This function can be called multiple times and it is no-op for the 2nd time
367*635a8641SAndroid Build Coastguard Worker   // calling.
368*635a8641SAndroid Build Coastguard Worker   //
369*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
370*635a8641SAndroid Build Coastguard Worker   virtual void ShutdownAndBlock();
371*635a8641SAndroid Build Coastguard Worker 
372*635a8641SAndroid Build Coastguard Worker   // Similar to ShutdownAndBlock(), but this function is used to
373*635a8641SAndroid Build Coastguard Worker   // synchronously shut down the bus that uses the D-Bus thread. This
374*635a8641SAndroid Build Coastguard Worker   // function is intended to be used at the very end of the browser
375*635a8641SAndroid Build Coastguard Worker   // shutdown, where it makes more sense to shut down the bus
376*635a8641SAndroid Build Coastguard Worker   // synchronously, than trying to make it asynchronous.
377*635a8641SAndroid Build Coastguard Worker   //
378*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL, but must be called in the origin thread.
379*635a8641SAndroid Build Coastguard Worker   virtual void ShutdownOnDBusThreadAndBlock();
380*635a8641SAndroid Build Coastguard Worker 
381*635a8641SAndroid Build Coastguard Worker   // Returns true if the shutdown has been completed.
shutdown_completed()382*635a8641SAndroid Build Coastguard Worker   bool shutdown_completed() { return shutdown_completed_; }
383*635a8641SAndroid Build Coastguard Worker 
384*635a8641SAndroid Build Coastguard Worker   //
385*635a8641SAndroid Build Coastguard Worker   // The public functions below are not intended to be used in client
386*635a8641SAndroid Build Coastguard Worker   // code. These are used to implement ObjectProxy and ExportedObject.
387*635a8641SAndroid Build Coastguard Worker   //
388*635a8641SAndroid Build Coastguard Worker 
389*635a8641SAndroid Build Coastguard Worker   // Connects the bus to the dbus-daemon.
390*635a8641SAndroid Build Coastguard Worker   // Returns true on success, or the bus is already connected.
391*635a8641SAndroid Build Coastguard Worker   //
392*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
393*635a8641SAndroid Build Coastguard Worker   virtual bool Connect();
394*635a8641SAndroid Build Coastguard Worker 
395*635a8641SAndroid Build Coastguard Worker   // Disconnects the bus from the dbus-daemon.
396*635a8641SAndroid Build Coastguard Worker   // Safe to call multiple times and no operation after the first call.
397*635a8641SAndroid Build Coastguard Worker   // Do not call for shared connection it will be released by libdbus.
398*635a8641SAndroid Build Coastguard Worker   //
399*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
400*635a8641SAndroid Build Coastguard Worker   virtual void ClosePrivateConnection();
401*635a8641SAndroid Build Coastguard Worker 
402*635a8641SAndroid Build Coastguard Worker   // Requests the ownership of the service name given by |service_name|.
403*635a8641SAndroid Build Coastguard Worker   // See also RequestOwnershipAndBlock().
404*635a8641SAndroid Build Coastguard Worker   //
405*635a8641SAndroid Build Coastguard Worker   // |on_ownership_callback| is called when the service name is obtained
406*635a8641SAndroid Build Coastguard Worker   // or failed to be obtained, in the origin thread.
407*635a8641SAndroid Build Coastguard Worker   //
408*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
409*635a8641SAndroid Build Coastguard Worker   virtual void RequestOwnership(const std::string& service_name,
410*635a8641SAndroid Build Coastguard Worker                                 ServiceOwnershipOptions options,
411*635a8641SAndroid Build Coastguard Worker                                 OnOwnershipCallback on_ownership_callback);
412*635a8641SAndroid Build Coastguard Worker 
413*635a8641SAndroid Build Coastguard Worker   // Requests the ownership of the given service name.
414*635a8641SAndroid Build Coastguard Worker   // Returns true on success, or the the service name is already obtained.
415*635a8641SAndroid Build Coastguard Worker   //
416*635a8641SAndroid Build Coastguard Worker   // Note that it's important to expose methods before requesting a service
417*635a8641SAndroid Build Coastguard Worker   // name with this method.  See also ExportedObject::ExportMethodAndBlock()
418*635a8641SAndroid Build Coastguard Worker   // for details.
419*635a8641SAndroid Build Coastguard Worker   //
420*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
421*635a8641SAndroid Build Coastguard Worker   virtual bool RequestOwnershipAndBlock(const std::string& service_name,
422*635a8641SAndroid Build Coastguard Worker                                         ServiceOwnershipOptions options);
423*635a8641SAndroid Build Coastguard Worker 
424*635a8641SAndroid Build Coastguard Worker   // Releases the ownership of the given service name.
425*635a8641SAndroid Build Coastguard Worker   // Returns true on success.
426*635a8641SAndroid Build Coastguard Worker   //
427*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
428*635a8641SAndroid Build Coastguard Worker   virtual bool ReleaseOwnership(const std::string& service_name);
429*635a8641SAndroid Build Coastguard Worker 
430*635a8641SAndroid Build Coastguard Worker   // Sets up async operations.
431*635a8641SAndroid Build Coastguard Worker   // Returns true on success, or it's already set up.
432*635a8641SAndroid Build Coastguard Worker   // This function needs to be called before starting async operations.
433*635a8641SAndroid Build Coastguard Worker   //
434*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
435*635a8641SAndroid Build Coastguard Worker   virtual bool SetUpAsyncOperations();
436*635a8641SAndroid Build Coastguard Worker 
437*635a8641SAndroid Build Coastguard Worker   // Sends a message to the bus and blocks until the response is
438*635a8641SAndroid Build Coastguard Worker   // received. Used to implement synchronous method calls.
439*635a8641SAndroid Build Coastguard Worker   //
440*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
441*635a8641SAndroid Build Coastguard Worker   virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
442*635a8641SAndroid Build Coastguard Worker                                              int timeout_ms,
443*635a8641SAndroid Build Coastguard Worker                                              DBusError* error);
444*635a8641SAndroid Build Coastguard Worker 
445*635a8641SAndroid Build Coastguard Worker   // Requests to send a message to the bus. The reply is handled with
446*635a8641SAndroid Build Coastguard Worker   // |pending_call| at a later time.
447*635a8641SAndroid Build Coastguard Worker   //
448*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
449*635a8641SAndroid Build Coastguard Worker   virtual void SendWithReply(DBusMessage* request,
450*635a8641SAndroid Build Coastguard Worker                              DBusPendingCall** pending_call,
451*635a8641SAndroid Build Coastguard Worker                              int timeout_ms);
452*635a8641SAndroid Build Coastguard Worker 
453*635a8641SAndroid Build Coastguard Worker   // Requests to send a message to the bus. The message serial number will
454*635a8641SAndroid Build Coastguard Worker   // be stored in |serial|.
455*635a8641SAndroid Build Coastguard Worker   //
456*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
457*635a8641SAndroid Build Coastguard Worker   virtual void Send(DBusMessage* request, uint32_t* serial);
458*635a8641SAndroid Build Coastguard Worker 
459*635a8641SAndroid Build Coastguard Worker   // Adds the message filter function. |filter_function| will be called
460*635a8641SAndroid Build Coastguard Worker   // when incoming messages are received.
461*635a8641SAndroid Build Coastguard Worker   //
462*635a8641SAndroid Build Coastguard Worker   // When a new incoming message arrives, filter functions are called in
463*635a8641SAndroid Build Coastguard Worker   // the order that they were added until the the incoming message is
464*635a8641SAndroid Build Coastguard Worker   // handled by a filter function.
465*635a8641SAndroid Build Coastguard Worker   //
466*635a8641SAndroid Build Coastguard Worker   // The same filter function associated with the same user data cannot be
467*635a8641SAndroid Build Coastguard Worker   // added more than once.
468*635a8641SAndroid Build Coastguard Worker   //
469*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
470*635a8641SAndroid Build Coastguard Worker   virtual void AddFilterFunction(DBusHandleMessageFunction filter_function,
471*635a8641SAndroid Build Coastguard Worker                                  void* user_data);
472*635a8641SAndroid Build Coastguard Worker 
473*635a8641SAndroid Build Coastguard Worker   // Removes the message filter previously added by AddFilterFunction().
474*635a8641SAndroid Build Coastguard Worker   //
475*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
476*635a8641SAndroid Build Coastguard Worker   virtual void RemoveFilterFunction(DBusHandleMessageFunction filter_function,
477*635a8641SAndroid Build Coastguard Worker                                     void* user_data);
478*635a8641SAndroid Build Coastguard Worker 
479*635a8641SAndroid Build Coastguard Worker   // Adds the match rule. Messages that match the rule will be processed
480*635a8641SAndroid Build Coastguard Worker   // by the filter functions added by AddFilterFunction().
481*635a8641SAndroid Build Coastguard Worker   //
482*635a8641SAndroid Build Coastguard Worker   // You cannot specify which filter function to use for a match rule.
483*635a8641SAndroid Build Coastguard Worker   // Instead, you should check if an incoming message is what you are
484*635a8641SAndroid Build Coastguard Worker   // interested in, in the filter functions.
485*635a8641SAndroid Build Coastguard Worker   //
486*635a8641SAndroid Build Coastguard Worker   // The same match rule can be added more than once and should be removed
487*635a8641SAndroid Build Coastguard Worker   // as many times as it was added.
488*635a8641SAndroid Build Coastguard Worker   //
489*635a8641SAndroid Build Coastguard Worker   // The match rule looks like:
490*635a8641SAndroid Build Coastguard Worker   // "type='signal', interface='org.chromium.SomeInterface'".
491*635a8641SAndroid Build Coastguard Worker   //
492*635a8641SAndroid Build Coastguard Worker   // See "Message Bus Message Routing" section in the D-Bus specification
493*635a8641SAndroid Build Coastguard Worker   // for details about match rules:
494*635a8641SAndroid Build Coastguard Worker   // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
495*635a8641SAndroid Build Coastguard Worker   //
496*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
497*635a8641SAndroid Build Coastguard Worker   virtual void AddMatch(const std::string& match_rule, DBusError* error);
498*635a8641SAndroid Build Coastguard Worker 
499*635a8641SAndroid Build Coastguard Worker   // Removes the match rule previously added by AddMatch().
500*635a8641SAndroid Build Coastguard Worker   // Returns false if the requested match rule is unknown or has already been
501*635a8641SAndroid Build Coastguard Worker   // removed. Otherwise, returns true and sets |error| accordingly.
502*635a8641SAndroid Build Coastguard Worker   //
503*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
504*635a8641SAndroid Build Coastguard Worker   virtual bool RemoveMatch(const std::string& match_rule, DBusError* error);
505*635a8641SAndroid Build Coastguard Worker 
506*635a8641SAndroid Build Coastguard Worker   // Tries to register the object path. Returns true on success.
507*635a8641SAndroid Build Coastguard Worker   // Returns false if the object path is already registered.
508*635a8641SAndroid Build Coastguard Worker   //
509*635a8641SAndroid Build Coastguard Worker   // |message_function| in |vtable| will be called every time when a new
510*635a8641SAndroid Build Coastguard Worker   // |message sent to the object path arrives.
511*635a8641SAndroid Build Coastguard Worker   //
512*635a8641SAndroid Build Coastguard Worker   // The same object path must not be added more than once.
513*635a8641SAndroid Build Coastguard Worker   //
514*635a8641SAndroid Build Coastguard Worker   // See also documentation of |dbus_connection_try_register_object_path| at
515*635a8641SAndroid Build Coastguard Worker   // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
516*635a8641SAndroid Build Coastguard Worker   //
517*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
518*635a8641SAndroid Build Coastguard Worker   virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
519*635a8641SAndroid Build Coastguard Worker                                      const DBusObjectPathVTable* vtable,
520*635a8641SAndroid Build Coastguard Worker                                      void* user_data,
521*635a8641SAndroid Build Coastguard Worker                                      DBusError* error);
522*635a8641SAndroid Build Coastguard Worker 
523*635a8641SAndroid Build Coastguard Worker   // Unregister the object path.
524*635a8641SAndroid Build Coastguard Worker   //
525*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
526*635a8641SAndroid Build Coastguard Worker   virtual void UnregisterObjectPath(const ObjectPath& object_path);
527*635a8641SAndroid Build Coastguard Worker 
528*635a8641SAndroid Build Coastguard Worker   // Returns the task runner of the D-Bus thread.
529*635a8641SAndroid Build Coastguard Worker   virtual base::TaskRunner* GetDBusTaskRunner();
530*635a8641SAndroid Build Coastguard Worker 
531*635a8641SAndroid Build Coastguard Worker   // Returns the task runner of the thread that created the bus.
532*635a8641SAndroid Build Coastguard Worker   virtual base::TaskRunner* GetOriginTaskRunner();
533*635a8641SAndroid Build Coastguard Worker 
534*635a8641SAndroid Build Coastguard Worker   // Returns true if the bus has the D-Bus thread.
535*635a8641SAndroid Build Coastguard Worker   virtual bool HasDBusThread();
536*635a8641SAndroid Build Coastguard Worker 
537*635a8641SAndroid Build Coastguard Worker   // Check whether the current thread is on the origin thread (the thread
538*635a8641SAndroid Build Coastguard Worker   // that created the bus). If not, DCHECK will fail.
539*635a8641SAndroid Build Coastguard Worker   virtual void AssertOnOriginThread();
540*635a8641SAndroid Build Coastguard Worker 
541*635a8641SAndroid Build Coastguard Worker   // Check whether the current thread is on the D-Bus thread. If not,
542*635a8641SAndroid Build Coastguard Worker   // DCHECK will fail. If the D-Bus thread is not supplied, it calls
543*635a8641SAndroid Build Coastguard Worker   // AssertOnOriginThread().
544*635a8641SAndroid Build Coastguard Worker   virtual void AssertOnDBusThread();
545*635a8641SAndroid Build Coastguard Worker 
546*635a8641SAndroid Build Coastguard Worker   // Gets the owner for |service_name| via org.freedesktop.DBus.GetNameOwner.
547*635a8641SAndroid Build Coastguard Worker   // Returns the owner name, if any, or an empty string on failure.
548*635a8641SAndroid Build Coastguard Worker   // |options| specifies where to printing error messages or not.
549*635a8641SAndroid Build Coastguard Worker   //
550*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
551*635a8641SAndroid Build Coastguard Worker   virtual std::string GetServiceOwnerAndBlock(const std::string& service_name,
552*635a8641SAndroid Build Coastguard Worker                                               GetServiceOwnerOption options);
553*635a8641SAndroid Build Coastguard Worker 
554*635a8641SAndroid Build Coastguard Worker   // A non-blocking version of GetServiceOwnerAndBlock().
555*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
556*635a8641SAndroid Build Coastguard Worker   virtual void GetServiceOwner(const std::string& service_name,
557*635a8641SAndroid Build Coastguard Worker                                const GetServiceOwnerCallback& callback);
558*635a8641SAndroid Build Coastguard Worker 
559*635a8641SAndroid Build Coastguard Worker   // Whenever the owner for |service_name| changes, run |callback| with the
560*635a8641SAndroid Build Coastguard Worker   // name of the new owner. If the owner goes away, then |callback| receives
561*635a8641SAndroid Build Coastguard Worker   // an empty string.
562*635a8641SAndroid Build Coastguard Worker   //
563*635a8641SAndroid Build Coastguard Worker   // Any unique (service_name, callback) can be used. Duplicate are ignored.
564*635a8641SAndroid Build Coastguard Worker   // |service_name| must not be empty and |callback| must not be null.
565*635a8641SAndroid Build Coastguard Worker   //
566*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
567*635a8641SAndroid Build Coastguard Worker   virtual void ListenForServiceOwnerChange(
568*635a8641SAndroid Build Coastguard Worker       const std::string& service_name,
569*635a8641SAndroid Build Coastguard Worker       const GetServiceOwnerCallback& callback);
570*635a8641SAndroid Build Coastguard Worker 
571*635a8641SAndroid Build Coastguard Worker   // Stop listening for |service_name| owner changes for |callback|.
572*635a8641SAndroid Build Coastguard Worker   // Any unique (service_name, callback) can be used. Non-registered callbacks
573*635a8641SAndroid Build Coastguard Worker   // for a given service name are ignored.
574*635a8641SAndroid Build Coastguard Worker   // |service_name| must not be empty and |callback| must not be null.
575*635a8641SAndroid Build Coastguard Worker   //
576*635a8641SAndroid Build Coastguard Worker   // Must be called in the origin thread.
577*635a8641SAndroid Build Coastguard Worker   virtual void UnlistenForServiceOwnerChange(
578*635a8641SAndroid Build Coastguard Worker       const std::string& service_name,
579*635a8641SAndroid Build Coastguard Worker       const GetServiceOwnerCallback& callback);
580*635a8641SAndroid Build Coastguard Worker 
581*635a8641SAndroid Build Coastguard Worker   // Return the unique name of the bus connnection if it is connected to
582*635a8641SAndroid Build Coastguard Worker   // D-BUS. Otherwise, return an empty string.
583*635a8641SAndroid Build Coastguard Worker   std::string GetConnectionName();
584*635a8641SAndroid Build Coastguard Worker 
585*635a8641SAndroid Build Coastguard Worker   // Returns true if the bus is connected to D-Bus.
is_connected()586*635a8641SAndroid Build Coastguard Worker   bool is_connected() { return connection_ != nullptr; }
587*635a8641SAndroid Build Coastguard Worker 
588*635a8641SAndroid Build Coastguard Worker  protected:
589*635a8641SAndroid Build Coastguard Worker   // This is protected, so we can define sub classes.
590*635a8641SAndroid Build Coastguard Worker   virtual ~Bus();
591*635a8641SAndroid Build Coastguard Worker 
592*635a8641SAndroid Build Coastguard Worker  private:
593*635a8641SAndroid Build Coastguard Worker   friend class base::RefCountedThreadSafe<Bus>;
594*635a8641SAndroid Build Coastguard Worker 
595*635a8641SAndroid Build Coastguard Worker   // Helper function used for RemoveObjectProxy().
596*635a8641SAndroid Build Coastguard Worker   void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
597*635a8641SAndroid Build Coastguard Worker                                  const base::Closure& callback);
598*635a8641SAndroid Build Coastguard Worker 
599*635a8641SAndroid Build Coastguard Worker   // Helper functions used for RemoveObjectManager().
600*635a8641SAndroid Build Coastguard Worker   void RemoveObjectManagerInternal(
601*635a8641SAndroid Build Coastguard Worker       scoped_refptr<dbus::ObjectManager> object_manager,
602*635a8641SAndroid Build Coastguard Worker       const base::Closure& callback);
603*635a8641SAndroid Build Coastguard Worker   void RemoveObjectManagerInternalHelper(
604*635a8641SAndroid Build Coastguard Worker       scoped_refptr<dbus::ObjectManager> object_manager,
605*635a8641SAndroid Build Coastguard Worker       const base::Closure& callback);
606*635a8641SAndroid Build Coastguard Worker 
607*635a8641SAndroid Build Coastguard Worker   // Helper function used for UnregisterExportedObject().
608*635a8641SAndroid Build Coastguard Worker   void UnregisterExportedObjectInternal(
609*635a8641SAndroid Build Coastguard Worker       scoped_refptr<dbus::ExportedObject> exported_object);
610*635a8641SAndroid Build Coastguard Worker 
611*635a8641SAndroid Build Coastguard Worker   // Helper function used for ShutdownOnDBusThreadAndBlock().
612*635a8641SAndroid Build Coastguard Worker   void ShutdownOnDBusThreadAndBlockInternal();
613*635a8641SAndroid Build Coastguard Worker 
614*635a8641SAndroid Build Coastguard Worker   // Helper function used for RequestOwnership().
615*635a8641SAndroid Build Coastguard Worker   void RequestOwnershipInternal(const std::string& service_name,
616*635a8641SAndroid Build Coastguard Worker                                 ServiceOwnershipOptions options,
617*635a8641SAndroid Build Coastguard Worker                                 OnOwnershipCallback on_ownership_callback);
618*635a8641SAndroid Build Coastguard Worker 
619*635a8641SAndroid Build Coastguard Worker   // Helper function used for GetServiceOwner().
620*635a8641SAndroid Build Coastguard Worker   void GetServiceOwnerInternal(const std::string& service_name,
621*635a8641SAndroid Build Coastguard Worker                                const GetServiceOwnerCallback& callback);
622*635a8641SAndroid Build Coastguard Worker 
623*635a8641SAndroid Build Coastguard Worker   // Helper function used for ListenForServiceOwnerChange().
624*635a8641SAndroid Build Coastguard Worker   void ListenForServiceOwnerChangeInternal(
625*635a8641SAndroid Build Coastguard Worker       const std::string& service_name,
626*635a8641SAndroid Build Coastguard Worker       const GetServiceOwnerCallback& callback);
627*635a8641SAndroid Build Coastguard Worker 
628*635a8641SAndroid Build Coastguard Worker   // Helper function used for UnListenForServiceOwnerChange().
629*635a8641SAndroid Build Coastguard Worker   void UnlistenForServiceOwnerChangeInternal(
630*635a8641SAndroid Build Coastguard Worker       const std::string& service_name,
631*635a8641SAndroid Build Coastguard Worker       const GetServiceOwnerCallback& callback);
632*635a8641SAndroid Build Coastguard Worker 
633*635a8641SAndroid Build Coastguard Worker   // Processes the all incoming data to the connection, if any.
634*635a8641SAndroid Build Coastguard Worker   //
635*635a8641SAndroid Build Coastguard Worker   // BLOCKING CALL.
636*635a8641SAndroid Build Coastguard Worker   void ProcessAllIncomingDataIfAny();
637*635a8641SAndroid Build Coastguard Worker 
638*635a8641SAndroid Build Coastguard Worker   // Called when a watch object is added. Used to start monitoring the
639*635a8641SAndroid Build Coastguard Worker   // file descriptor used for D-Bus communication.
640*635a8641SAndroid Build Coastguard Worker   dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
641*635a8641SAndroid Build Coastguard Worker 
642*635a8641SAndroid Build Coastguard Worker   // Called when a watch object is removed.
643*635a8641SAndroid Build Coastguard Worker   void OnRemoveWatch(DBusWatch* raw_watch);
644*635a8641SAndroid Build Coastguard Worker 
645*635a8641SAndroid Build Coastguard Worker   // Called when the "enabled" status of |raw_watch| is toggled.
646*635a8641SAndroid Build Coastguard Worker   void OnToggleWatch(DBusWatch* raw_watch);
647*635a8641SAndroid Build Coastguard Worker 
648*635a8641SAndroid Build Coastguard Worker   // Called when a timeout object is added. Used to start monitoring
649*635a8641SAndroid Build Coastguard Worker   // timeout for method calls.
650*635a8641SAndroid Build Coastguard Worker   dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
651*635a8641SAndroid Build Coastguard Worker 
652*635a8641SAndroid Build Coastguard Worker   // Called when a timeout object is removed.
653*635a8641SAndroid Build Coastguard Worker   void OnRemoveTimeout(DBusTimeout* raw_timeout);
654*635a8641SAndroid Build Coastguard Worker 
655*635a8641SAndroid Build Coastguard Worker   // Called when the "enabled" status of |raw_timeout| is toggled.
656*635a8641SAndroid Build Coastguard Worker   void OnToggleTimeout(DBusTimeout* raw_timeout);
657*635a8641SAndroid Build Coastguard Worker 
658*635a8641SAndroid Build Coastguard Worker   // Called when the dispatch status (i.e. if any incoming data is
659*635a8641SAndroid Build Coastguard Worker   // available) is changed.
660*635a8641SAndroid Build Coastguard Worker   void OnDispatchStatusChanged(DBusConnection* connection,
661*635a8641SAndroid Build Coastguard Worker                                DBusDispatchStatus status);
662*635a8641SAndroid Build Coastguard Worker 
663*635a8641SAndroid Build Coastguard Worker   // Called when a service owner change occurs.
664*635a8641SAndroid Build Coastguard Worker   void OnServiceOwnerChanged(DBusMessage* message);
665*635a8641SAndroid Build Coastguard Worker 
666*635a8641SAndroid Build Coastguard Worker   // Callback helper functions. Redirects to the corresponding member function.
667*635a8641SAndroid Build Coastguard Worker   static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
668*635a8641SAndroid Build Coastguard Worker   static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
669*635a8641SAndroid Build Coastguard Worker   static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
670*635a8641SAndroid Build Coastguard Worker   static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
671*635a8641SAndroid Build Coastguard Worker   static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
672*635a8641SAndroid Build Coastguard Worker   static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
673*635a8641SAndroid Build Coastguard Worker   static void OnDispatchStatusChangedThunk(DBusConnection* connection,
674*635a8641SAndroid Build Coastguard Worker                                            DBusDispatchStatus status,
675*635a8641SAndroid Build Coastguard Worker                                            void* data);
676*635a8641SAndroid Build Coastguard Worker 
677*635a8641SAndroid Build Coastguard Worker   // Calls OnConnectionDisconnected if the Disconnected signal is received.
678*635a8641SAndroid Build Coastguard Worker   static DBusHandlerResult OnConnectionDisconnectedFilter(
679*635a8641SAndroid Build Coastguard Worker       DBusConnection* connection,
680*635a8641SAndroid Build Coastguard Worker       DBusMessage* message,
681*635a8641SAndroid Build Coastguard Worker       void* user_data);
682*635a8641SAndroid Build Coastguard Worker 
683*635a8641SAndroid Build Coastguard Worker   // Calls OnServiceOwnerChanged for a NameOwnerChanged signal.
684*635a8641SAndroid Build Coastguard Worker   static DBusHandlerResult OnServiceOwnerChangedFilter(
685*635a8641SAndroid Build Coastguard Worker       DBusConnection* connection,
686*635a8641SAndroid Build Coastguard Worker       DBusMessage* message,
687*635a8641SAndroid Build Coastguard Worker       void* user_data);
688*635a8641SAndroid Build Coastguard Worker 
689*635a8641SAndroid Build Coastguard Worker   const BusType bus_type_;
690*635a8641SAndroid Build Coastguard Worker   const ConnectionType connection_type_;
691*635a8641SAndroid Build Coastguard Worker   scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
692*635a8641SAndroid Build Coastguard Worker   base::WaitableEvent on_shutdown_;
693*635a8641SAndroid Build Coastguard Worker   DBusConnection* connection_;
694*635a8641SAndroid Build Coastguard Worker 
695*635a8641SAndroid Build Coastguard Worker   scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
696*635a8641SAndroid Build Coastguard Worker   base::PlatformThreadId origin_thread_id_;
697*635a8641SAndroid Build Coastguard Worker 
698*635a8641SAndroid Build Coastguard Worker   std::set<std::string> owned_service_names_;
699*635a8641SAndroid Build Coastguard Worker   // The following sets are used to check if rules/object_paths/filters
700*635a8641SAndroid Build Coastguard Worker   // are properly cleaned up before destruction of the bus object.
701*635a8641SAndroid Build Coastguard Worker   // Since it's not an error to add the same match rule twice, the repeated
702*635a8641SAndroid Build Coastguard Worker   // match rules are counted in a map.
703*635a8641SAndroid Build Coastguard Worker   std::map<std::string, int> match_rules_added_;
704*635a8641SAndroid Build Coastguard Worker   std::set<ObjectPath> registered_object_paths_;
705*635a8641SAndroid Build Coastguard Worker   std::set<std::pair<DBusHandleMessageFunction, void*>> filter_functions_added_;
706*635a8641SAndroid Build Coastguard Worker 
707*635a8641SAndroid Build Coastguard Worker   // ObjectProxyTable is used to hold the object proxies created by the
708*635a8641SAndroid Build Coastguard Worker   // bus object. Key is a pair; the first part is a concatenated string of
709*635a8641SAndroid Build Coastguard Worker   // service name + object path, like
710*635a8641SAndroid Build Coastguard Worker   // "org.chromium.TestService/org/chromium/TestObject".
711*635a8641SAndroid Build Coastguard Worker   // The second part is the ObjectProxy::Options for the proxy.
712*635a8641SAndroid Build Coastguard Worker   typedef std::map<std::pair<std::string, int>,
713*635a8641SAndroid Build Coastguard Worker                    scoped_refptr<dbus::ObjectProxy>> ObjectProxyTable;
714*635a8641SAndroid Build Coastguard Worker   ObjectProxyTable object_proxy_table_;
715*635a8641SAndroid Build Coastguard Worker 
716*635a8641SAndroid Build Coastguard Worker   // ExportedObjectTable is used to hold the exported objects created by
717*635a8641SAndroid Build Coastguard Worker   // the bus object. Key is a concatenated string of service name +
718*635a8641SAndroid Build Coastguard Worker   // object path, like "org.chromium.TestService/org/chromium/TestObject".
719*635a8641SAndroid Build Coastguard Worker   typedef std::map<const dbus::ObjectPath,
720*635a8641SAndroid Build Coastguard Worker                    scoped_refptr<dbus::ExportedObject>> ExportedObjectTable;
721*635a8641SAndroid Build Coastguard Worker   ExportedObjectTable exported_object_table_;
722*635a8641SAndroid Build Coastguard Worker 
723*635a8641SAndroid Build Coastguard Worker   // ObjectManagerTable is used to hold the object managers created by the
724*635a8641SAndroid Build Coastguard Worker   // bus object. Key is a concatenated string of service name + object path,
725*635a8641SAndroid Build Coastguard Worker   // like "org.chromium.TestService/org/chromium/TestObject".
726*635a8641SAndroid Build Coastguard Worker   typedef std::map<std::string,
727*635a8641SAndroid Build Coastguard Worker                    scoped_refptr<dbus::ObjectManager>> ObjectManagerTable;
728*635a8641SAndroid Build Coastguard Worker   ObjectManagerTable object_manager_table_;
729*635a8641SAndroid Build Coastguard Worker 
730*635a8641SAndroid Build Coastguard Worker   // A map of NameOwnerChanged signals to listen for and the callbacks to run
731*635a8641SAndroid Build Coastguard Worker   // on the origin thread when the owner changes.
732*635a8641SAndroid Build Coastguard Worker   // Only accessed on the DBus thread.
733*635a8641SAndroid Build Coastguard Worker   // Key: Service name
734*635a8641SAndroid Build Coastguard Worker   // Value: Vector of callbacks. Unique and expected to be small. Not using
735*635a8641SAndroid Build Coastguard Worker   //        std::set here because base::Callbacks don't have a '<' operator.
736*635a8641SAndroid Build Coastguard Worker   typedef std::map<std::string, std::vector<GetServiceOwnerCallback>>
737*635a8641SAndroid Build Coastguard Worker       ServiceOwnerChangedListenerMap;
738*635a8641SAndroid Build Coastguard Worker   ServiceOwnerChangedListenerMap service_owner_changed_listener_map_;
739*635a8641SAndroid Build Coastguard Worker 
740*635a8641SAndroid Build Coastguard Worker   bool async_operations_set_up_;
741*635a8641SAndroid Build Coastguard Worker   bool shutdown_completed_;
742*635a8641SAndroid Build Coastguard Worker 
743*635a8641SAndroid Build Coastguard Worker   // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
744*635a8641SAndroid Build Coastguard Worker   // OnAddTimeout()/OnRemoveTimeou() are balanced.
745*635a8641SAndroid Build Coastguard Worker   int num_pending_watches_;
746*635a8641SAndroid Build Coastguard Worker   int num_pending_timeouts_;
747*635a8641SAndroid Build Coastguard Worker 
748*635a8641SAndroid Build Coastguard Worker   std::string address_;
749*635a8641SAndroid Build Coastguard Worker 
750*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(Bus);
751*635a8641SAndroid Build Coastguard Worker };
752*635a8641SAndroid Build Coastguard Worker 
753*635a8641SAndroid Build Coastguard Worker }  // namespace dbus
754*635a8641SAndroid Build Coastguard Worker 
755*635a8641SAndroid Build Coastguard Worker #endif  // DBUS_BUS_H_
756