blob: 7d3915909b9fb42792c328522ae0ee1b9f2e0af9 [file] [log] [blame]
adamk@chromium.org35c0eef2012-02-11 06:45:23 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef DBUS_BUS_H_
6#define DBUS_BUS_H_
satorux@chromium.org163f1cb2011-08-18 05:58:12 +09007
thestig@chromium.org074b1db2013-02-20 10:36:53 +09008#include <dbus/dbus.h>
avi0ad0ce02015-12-23 03:12:45 +09009#include <stdint.h>
thestig@chromium.org074b1db2013-02-20 10:36:53 +090010
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +090011#include <map>
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090012#include <set>
13#include <string>
satorux@chromium.org66bc4c22011-10-06 09:20:53 +090014#include <utility>
thestig@chromium.orgc2482f12013-06-11 07:52:34 +090015#include <vector>
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090016
17#include "base/callback.h"
avi0ad0ce02015-12-23 03:12:45 +090018#include "base/macros.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090019#include "base/memory/ref_counted.h"
satorux@chromium.orgd336d452011-09-02 15:56:23 +090020#include "base/synchronization/waitable_event.h"
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +090021#include "base/threading/platform_thread.h"
tfarina@chromium.org7928ea22012-11-05 10:56:14 +090022#include "dbus/dbus_export.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090023#include "dbus/object_path.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090024
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090025namespace base {
thestig@chromium.org074b1db2013-02-20 10:36:53 +090026class SequencedTaskRunner;
27class SingleThreadTaskRunner;
hashimoto@chromium.org955f6482013-09-26 13:32:29 +090028class TaskRunner;
thestig@chromium.org074b1db2013-02-20 10:36:53 +090029}
30
31namespace tracked_objects {
32class Location;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090033}
34
35namespace dbus {
36
37class ExportedObject;
keybuk@chromium.org09715012013-03-26 03:20:08 +090038class ObjectManager;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090039class ObjectProxy;
40
41// Bus is used to establish a connection with D-Bus, create object
42// proxies, and export objects.
43//
44// For asynchronous operations such as an asynchronous method call, the
thestig@chromium.org074b1db2013-02-20 10:36:53 +090045// bus object will use a task runner to monitor the underlying file
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090046// descriptor used for D-Bus communication. By default, the bus will use
thestig@chromium.org074b1db2013-02-20 10:36:53 +090047// the current thread's task runner. If |dbus_task_runner| option is
48// specified, the bus will use that task runner instead.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090049//
50// THREADING
51//
52// In the D-Bus library, we use the two threads:
53//
54// - The origin thread: the thread that created the Bus object.
thestig@chromium.org074b1db2013-02-20 10:36:53 +090055// - The D-Bus thread: the thread servicing |dbus_task_runner|.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090056//
57// The origin thread is usually Chrome's UI thread. The D-Bus thread is
58// usually a dedicated thread for the D-Bus library.
59//
60// BLOCKING CALLS
61//
62// Functions that issue blocking calls are marked "BLOCKING CALL" and
63// these functions should be called in the D-Bus thread (if
64// supplied). AssertOnDBusThread() is placed in these functions.
65//
66// Note that it's hard to tell if a libdbus function is actually blocking
67// or not (ex. dbus_bus_request_name() internally calls
68// dbus_connection_send_with_reply_and_block(), which is a blocking
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +090069// call). To err on the safe side, we consider all libdbus functions that
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +090070// deal with the connection to dbus-daemon to be blocking.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090071//
satorux@chromium.org326a6f82011-08-27 16:26:34 +090072// SHUTDOWN
73//
satorux@chromium.orgd336d452011-09-02 15:56:23 +090074// The Bus object must be shut down manually by ShutdownAndBlock() and
75// friends. We require the manual shutdown to make the operation explicit
76// rather than doing it silently in the destructor.
satorux@chromium.org326a6f82011-08-27 16:26:34 +090077//
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090078// EXAMPLE USAGE:
79//
80// Synchronous method call:
81//
82// dbus::Bus::Options options;
83// // Set up the bus options here.
84// ...
85// dbus::Bus bus(options);
86//
87// dbus::ObjectProxy* object_proxy =
88// bus.GetObjectProxy(service_name, object_path);
89//
90// dbus::MethodCall method_call(interface_name, method_name);
dcheng30c5a172016-04-09 07:55:04 +090091// std::unique_ptr<dbus::Response> response(
mdm@chromium.org45f2c6a2011-09-07 05:03:24 +090092// object_proxy.CallMethodAndBlock(&method_call, timeout_ms));
93// if (response.get() != NULL) { // Success.
94// ...
95// }
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090096//
97// Asynchronous method call:
98//
99// void OnResponse(dbus::Response* response) {
100// // response is NULL if the method call failed.
101// if (!response)
102// return;
103// }
104//
105// ...
106// object_proxy.CallMethod(&method_call, timeout_ms,
107// base::Bind(&OnResponse));
108//
109// Exporting a method:
110//
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900111// void Echo(dbus::MethodCall* method_call,
112// dbus::ExportedObject::ResponseSender response_sender) {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900113// // Do something with method_call.
114// Response* response = Response::FromMethodCall(method_call);
115// // Build response here.
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900116// // Can send an immediate response here to implement a synchronous service
117// // or store the response_sender and send a response later to implement an
118// // asynchronous service.
119// response_sender.Run(response);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900120// }
121//
122// void OnExported(const std::string& interface_name,
keybuk@google.combf4649a2012-02-15 06:29:06 +0900123// const ObjectPath& object_path,
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900124// bool success) {
125// // success is true if the method was exported successfully.
126// }
127//
128// ...
129// dbus::ExportedObject* exported_object =
130// bus.GetExportedObject(service_name, object_path);
131// exported_object.ExportMethod(interface_name, method_name,
132// base::Bind(&Echo),
133// base::Bind(&OnExported));
134//
135// WHY IS THIS A REF COUNTED OBJECT?
136//
137// Bus is a ref counted object, to ensure that |this| of the object is
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900138// alive when callbacks referencing |this| are called. However, after the
139// bus is shut down, |connection_| can be NULL. Hence, callbacks should
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900140// not rely on that |connection_| is alive.
tfarina@chromium.org7928ea22012-11-05 10:56:14 +0900141class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900142 public:
143 // Specifies the bus type. SESSION is used to communicate with per-user
144 // services like GNOME applications. SYSTEM is used to communicate with
nona@chromium.org9f638e02012-04-19 12:20:03 +0900145 // system-wide services like NetworkManager. CUSTOM_ADDRESS is used to
146 // communicate with an user specified address.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900147 enum BusType {
148 SESSION = DBUS_BUS_SESSION,
nona@chromium.org9f638e02012-04-19 12:20:03 +0900149 SYSTEM = DBUS_BUS_SYSTEM,
150 CUSTOM_ADDRESS,
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900151 };
152
153 // Specifies the connection type. PRIVATE should usually be used unless
154 // you are sure that SHARED is safe for you, which is unlikely the case
155 // in Chrome.
156 //
157 // PRIVATE gives you a private connection, that won't be shared with
158 // other Bus objects.
159 //
160 // SHARED gives you a connection shared among other Bus objects, which
161 // is unsafe if the connection is shared with multiple threads.
162 enum ConnectionType {
163 PRIVATE,
164 SHARED,
165 };
166
thestig@chromium.org56057f22013-05-05 00:48:37 +0900167 // Specifies whether the GetServiceOwnerAndBlock call should report or
168 // suppress errors.
169 enum GetServiceOwnerOption {
170 REPORT_ERRORS,
171 SUPPRESS_ERRORS,
172 };
173
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900174 // Specifies service ownership options.
175 //
176 // REQUIRE_PRIMARY indicates that you require primary ownership of the
177 // service name.
178 //
179 // ALLOW_REPLACEMENT indicates that you'll allow another connection to
180 // steal ownership of this service name from you.
181 //
182 // REQUIRE_PRIMARY_ALLOW_REPLACEMENT does the obvious.
183 enum ServiceOwnershipOptions {
184 REQUIRE_PRIMARY = (DBUS_NAME_FLAG_DO_NOT_QUEUE |
185 DBUS_NAME_FLAG_REPLACE_EXISTING),
186 REQUIRE_PRIMARY_ALLOW_REPLACEMENT = (REQUIRE_PRIMARY |
187 DBUS_NAME_FLAG_ALLOW_REPLACEMENT),
188 };
189
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900190 // Options used to create a Bus object.
tfarina@chromium.org7928ea22012-11-05 10:56:14 +0900191 struct CHROME_DBUS_EXPORT Options {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900192 Options();
193 ~Options();
194
195 BusType bus_type; // SESSION by default.
196 ConnectionType connection_type; // PRIVATE by default.
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900197 // If dbus_task_runner is set, the bus object will use that
198 // task runner to process asynchronous operations.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900199 //
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900200 // The thread servicing the task runner should meet the following
mdm@chromium.org45f2c6a2011-09-07 05:03:24 +0900201 // requirements:
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900202 // 1) Already running.
203 // 2) Has a MessageLoopForIO.
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900204 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
nona@chromium.org9f638e02012-04-19 12:20:03 +0900205
206 // Specifies the server addresses to be connected. If you want to
207 // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to
208 // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to
209 // connect to. The format of this address value is the dbus address style
210 // which is described in
211 // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
212 //
213 // EXAMPLE USAGE:
214 // dbus::Bus::Options options;
215 // options.bus_type = CUSTOM_ADDRESS;
216 // options.address.assign("unix:path=/tmp/dbus-XXXXXXX");
217 // // Set up other options
218 // dbus::Bus bus(options);
219 //
220 // // Do something.
221 //
222 std::string address;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900223 };
224
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900225 // Creates a Bus object. The actual connection will be established when
226 // Connect() is called.
227 explicit Bus(const Options& options);
228
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900229 // Called when an ownership request is complete.
230 // Parameters:
231 // - the requested service name.
232 // - whether ownership has been obtained or not.
233 typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback;
thestig@chromium.org56057f22013-05-05 00:48:37 +0900234
235 // Called when GetServiceOwner() completes.
236 // |service_owner| is the return value from GetServiceOwnerAndBlock().
237 typedef base::Callback<void (const std::string& service_owner)>
238 GetServiceOwnerCallback;
239
deymo@chromium.org6d168a72013-01-30 05:29:12 +0900240 // TODO(satorux): Remove the service name parameter as the caller of
241 // RequestOwnership() knows the service name.
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900242
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900243 // Gets the object proxy for the given service name and the object path.
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900244 // The caller must not delete the returned object.
245 //
246 // Returns an existing object proxy if the bus object already owns the
247 // object proxy for the given service name and the object path.
248 // Never returns NULL.
249 //
250 // The bus will own all object proxies created by the bus, to ensure
251 // that the object proxies are detached from remote objects at the
252 // shutdown time of the bus.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900253 //
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900254 // The object proxy is used to call methods of remote objects, and
255 // receive signals from them.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900256 //
257 // |service_name| looks like "org.freedesktop.NetworkManager", and
258 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
259 //
260 // Must be called in the origin thread.
261 virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
keybuk@google.combf4649a2012-02-15 06:29:06 +0900262 const ObjectPath& object_path);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900263
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900264 // Same as above, but also takes a bitfield of ObjectProxy::Options.
265 // See object_proxy.h for available options.
266 virtual ObjectProxy* GetObjectProxyWithOptions(
267 const std::string& service_name,
keybuk@google.combf4649a2012-02-15 06:29:06 +0900268 const ObjectPath& object_path,
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900269 int options);
270
deymo@chromium.org6d168a72013-01-30 05:29:12 +0900271 // Removes the previously created object proxy for the given service
272 // name and the object path and releases its memory.
273 //
274 // If and object proxy for the given service name and object was
275 // created with GetObjectProxy, this function removes it from the
276 // bus object and detaches the ObjectProxy, invalidating any pointer
277 // previously acquired for it with GetObjectProxy. A subsequent call
278 // to GetObjectProxy will return a new object.
279 //
280 // All the object proxies are detached from remote objects at the
281 // shutdown time of the bus, but they can be detached early to reduce
282 // memory footprint and used match rules for the bus connection.
283 //
284 // |service_name| looks like "org.freedesktop.NetworkManager", and
285 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
286 // |callback| is called when the object proxy is successfully removed and
287 // detached.
288 //
289 // The function returns true when there is an object proxy matching the
290 // |service_name| and |object_path| to remove, and calls |callback| when it
291 // is removed. Otherwise, it returns false and the |callback| function is
292 // never called. The |callback| argument must not be null.
293 //
294 // Must be called in the origin thread.
295 virtual bool RemoveObjectProxy(const std::string& service_name,
296 const ObjectPath& object_path,
297 const base::Closure& callback);
298
299 // Same as above, but also takes a bitfield of ObjectProxy::Options.
300 // See object_proxy.h for available options.
301 virtual bool RemoveObjectProxyWithOptions(
302 const std::string& service_name,
303 const ObjectPath& object_path,
304 int options,
305 const base::Closure& callback);
306
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900307 // Gets the exported object for the given object path.
308 // The caller must not delete the returned object.
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900309 //
310 // Returns an existing exported object if the bus object already owns
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900311 // the exported object for the given object path. Never returns NULL.
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900312 //
313 // The bus will own all exported objects created by the bus, to ensure
314 // that the exported objects are unregistered at the shutdown time of
315 // the bus.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900316 //
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900317 // The exported object is used to export methods of local objects, and
318 // send signal from them.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900319 //
320 // Must be called in the origin thread.
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900321 virtual ExportedObject* GetExportedObject(const ObjectPath& object_path);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900322
keybuk@chromium.orgd2ca8f32012-03-14 10:18:35 +0900323 // Unregisters the exported object for the given object path |object_path|.
324 //
325 // Getting an exported object for the same object path after this call
326 // will return a new object, method calls on any remaining copies of the
327 // previous object will not be called.
328 //
329 // Must be called in the origin thread.
330 virtual void UnregisterExportedObject(const ObjectPath& object_path);
331
keybuk@chromium.org09715012013-03-26 03:20:08 +0900332
333 // Gets an object manager for the given remote object path |object_path|
334 // exported by the service |service_name|.
335 //
336 // Returns an existing object manager if the bus object already owns a
337 // matching object manager, never returns NULL.
338 //
339 // The caller must not delete the returned object, the bus retains ownership
340 // of all object managers.
341 //
342 // Must be called in the origin thread.
343 virtual ObjectManager* GetObjectManager(const std::string& service_name,
344 const ObjectPath& object_path);
345
346 // Unregisters the object manager for the given remote object path
347 // |object_path| exported by the srevice |service_name|.
348 //
349 // Getting an object manager for the same remote object after this call
350 // will return a new object, method calls on any remaining copies of the
351 // previous object are not permitted.
352 //
armansitof4364642014-09-06 02:49:34 +0900353 // This method will asynchronously clean up any match rules that have been
354 // added for the object manager and invoke |callback| when the operation is
355 // complete. If this method returns false, then |callback| is never called.
356 // The |callback| argument must not be null.
357 //
keybuk@chromium.org09715012013-03-26 03:20:08 +0900358 // Must be called in the origin thread.
armansitof4364642014-09-06 02:49:34 +0900359 virtual bool RemoveObjectManager(const std::string& service_name,
360 const ObjectPath& object_path,
361 const base::Closure& callback);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900362
363 // Instructs all registered object managers to retrieve their set of managed
364 // objects from their respective remote objects. There is no need to call this
365 // manually, this is called automatically by the D-Bus thread manager once
366 // implementation classes are registered.
367 virtual void GetManagedObjects();
368
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900369 // Shuts down the bus and blocks until it's done. More specifically, this
370 // function does the following:
371 //
372 // - Unregisters the object paths
373 // - Releases the service names
374 // - Closes the connection to dbus-daemon.
375 //
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900376 // This function can be called multiple times and it is no-op for the 2nd time
377 // calling.
378 //
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900379 // BLOCKING CALL.
380 virtual void ShutdownAndBlock();
381
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900382 // Similar to ShutdownAndBlock(), but this function is used to
383 // synchronously shut down the bus that uses the D-Bus thread. This
384 // function is intended to be used at the very end of the browser
385 // shutdown, where it makes more sense to shut down the bus
386 // synchronously, than trying to make it asynchronous.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900387 //
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900388 // BLOCKING CALL, but must be called in the origin thread.
389 virtual void ShutdownOnDBusThreadAndBlock();
390
391 // Returns true if the shutdown has been completed.
392 bool shutdown_completed() { return shutdown_completed_; }
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900393
394 //
395 // The public functions below are not intended to be used in client
396 // code. These are used to implement ObjectProxy and ExportedObject.
397 //
398
399 // Connects the bus to the dbus-daemon.
400 // Returns true on success, or the bus is already connected.
401 //
402 // BLOCKING CALL.
403 virtual bool Connect();
404
nona@chromium.org1de76fd2013-02-16 01:44:40 +0900405 // Disconnects the bus from the dbus-daemon.
406 // Safe to call multiple times and no operation after the first call.
407 // Do not call for shared connection it will be released by libdbus.
408 //
409 // BLOCKING CALL.
410 virtual void ClosePrivateConnection();
411
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900412 // Requests the ownership of the service name given by |service_name|.
413 // See also RequestOwnershipAndBlock().
414 //
415 // |on_ownership_callback| is called when the service name is obtained
416 // or failed to be obtained, in the origin thread.
417 //
418 // Must be called in the origin thread.
419 virtual void RequestOwnership(const std::string& service_name,
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900420 ServiceOwnershipOptions options,
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900421 OnOwnershipCallback on_ownership_callback);
422
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900423 // Requests the ownership of the given service name.
424 // Returns true on success, or the the service name is already obtained.
425 //
satorux@chromium.org49e0bc72014-01-09 13:39:17 +0900426 // Note that it's important to expose methods before requesting a service
427 // name with this method. See also ExportedObject::ExportMethodAndBlock()
428 // for details.
429 //
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900430 // BLOCKING CALL.
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900431 virtual bool RequestOwnershipAndBlock(const std::string& service_name,
432 ServiceOwnershipOptions options);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900433
434 // Releases the ownership of the given service name.
435 // Returns true on success.
436 //
437 // BLOCKING CALL.
438 virtual bool ReleaseOwnership(const std::string& service_name);
439
440 // Sets up async operations.
441 // Returns true on success, or it's already set up.
442 // This function needs to be called before starting async operations.
443 //
444 // BLOCKING CALL.
445 virtual bool SetUpAsyncOperations();
446
447 // Sends a message to the bus and blocks until the response is
448 // received. Used to implement synchronous method calls.
449 //
450 // BLOCKING CALL.
451 virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
452 int timeout_ms,
453 DBusError* error);
454
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900455 // Requests to send a message to the bus. The reply is handled with
456 // |pending_call| at a later time.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900457 //
458 // BLOCKING CALL.
459 virtual void SendWithReply(DBusMessage* request,
460 DBusPendingCall** pending_call,
461 int timeout_ms);
462
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900463 // Requests to send a message to the bus. The message serial number will
464 // be stored in |serial|.
465 //
466 // BLOCKING CALL.
avi0ad0ce02015-12-23 03:12:45 +0900467 virtual void Send(DBusMessage* request, uint32_t* serial);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900468
469 // Adds the message filter function. |filter_function| will be called
hashimoto46be6e92014-12-04 16:41:55 +0900470 // when incoming messages are received.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900471 //
472 // When a new incoming message arrives, filter functions are called in
473 // the order that they were added until the the incoming message is
474 // handled by a filter function.
475 //
satorux@chromium.org66bc4c22011-10-06 09:20:53 +0900476 // The same filter function associated with the same user data cannot be
hashimoto46be6e92014-12-04 16:41:55 +0900477 // added more than once.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900478 //
479 // BLOCKING CALL.
hashimoto46be6e92014-12-04 16:41:55 +0900480 virtual void AddFilterFunction(DBusHandleMessageFunction filter_function,
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900481 void* user_data);
482
483 // Removes the message filter previously added by AddFilterFunction().
484 //
485 // BLOCKING CALL.
hashimoto46be6e92014-12-04 16:41:55 +0900486 virtual void RemoveFilterFunction(DBusHandleMessageFunction filter_function,
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900487 void* user_data);
488
489 // Adds the match rule. Messages that match the rule will be processed
490 // by the filter functions added by AddFilterFunction().
491 //
492 // You cannot specify which filter function to use for a match rule.
493 // Instead, you should check if an incoming message is what you are
494 // interested in, in the filter functions.
495 //
deymo@chromium.org7894ebf2013-01-31 15:08:02 +0900496 // The same match rule can be added more than once and should be removed
497 // as many times as it was added.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900498 //
499 // The match rule looks like:
500 // "type='signal', interface='org.chromium.SomeInterface'".
501 //
502 // See "Message Bus Message Routing" section in the D-Bus specification
503 // for details about match rules:
504 // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
505 //
506 // BLOCKING CALL.
507 virtual void AddMatch(const std::string& match_rule, DBusError* error);
508
509 // Removes the match rule previously added by AddMatch().
deymo@chromium.org7894ebf2013-01-31 15:08:02 +0900510 // Returns false if the requested match rule is unknown or has already been
511 // removed. Otherwise, returns true and sets |error| accordingly.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900512 //
513 // BLOCKING CALL.
deymo@chromium.org7894ebf2013-01-31 15:08:02 +0900514 virtual bool RemoveMatch(const std::string& match_rule, DBusError* error);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900515
516 // Tries to register the object path. Returns true on success.
517 // Returns false if the object path is already registered.
518 //
519 // |message_function| in |vtable| will be called every time when a new
520 // |message sent to the object path arrives.
521 //
522 // The same object path must not be added more than once.
523 //
524 // See also documentation of |dbus_connection_try_register_object_path| at
525 // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900526 //
527 // BLOCKING CALL.
keybuk@google.combf4649a2012-02-15 06:29:06 +0900528 virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900529 const DBusObjectPathVTable* vtable,
530 void* user_data,
531 DBusError* error);
532
533 // Unregister the object path.
534 //
535 // BLOCKING CALL.
keybuk@google.combf4649a2012-02-15 06:29:06 +0900536 virtual void UnregisterObjectPath(const ObjectPath& object_path);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900537
hashimoto@chromium.org955f6482013-09-26 13:32:29 +0900538 // Returns the task runner of the D-Bus thread.
539 virtual base::TaskRunner* GetDBusTaskRunner();
haruki@chromium.org4a1f9562013-05-08 20:57:14 +0900540
hashimoto@chromium.org955f6482013-09-26 13:32:29 +0900541 // Returns the task runner of the thread that created the bus.
542 virtual base::TaskRunner* GetOriginTaskRunner();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900543
544 // Returns true if the bus has the D-Bus thread.
545 virtual bool HasDBusThread();
546
547 // Check whether the current thread is on the origin thread (the thread
548 // that created the bus). If not, DCHECK will fail.
549 virtual void AssertOnOriginThread();
550
551 // Check whether the current thread is on the D-Bus thread. If not,
552 // DCHECK will fail. If the D-Bus thread is not supplied, it calls
553 // AssertOnOriginThread().
554 virtual void AssertOnDBusThread();
555
thestig@chromium.org56057f22013-05-05 00:48:37 +0900556 // Gets the owner for |service_name| via org.freedesktop.DBus.GetNameOwner.
557 // Returns the owner name, if any, or an empty string on failure.
558 // |options| specifies where to printing error messages or not.
559 //
560 // BLOCKING CALL.
561 virtual std::string GetServiceOwnerAndBlock(const std::string& service_name,
562 GetServiceOwnerOption options);
563
564 // A non-blocking version of GetServiceOwnerAndBlock().
565 // Must be called in the origin thread.
566 virtual void GetServiceOwner(const std::string& service_name,
567 const GetServiceOwnerCallback& callback);
568
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900569 // Whenever the owner for |service_name| changes, run |callback| with the
570 // name of the new owner. If the owner goes away, then |callback| receives
571 // an empty string.
572 //
573 // Any unique (service_name, callback) can be used. Duplicate are ignored.
574 // |service_name| must not be empty and |callback| must not be null.
575 //
576 // Must be called in the origin thread.
577 virtual void ListenForServiceOwnerChange(
578 const std::string& service_name,
579 const GetServiceOwnerCallback& callback);
580
581 // Stop listening for |service_name| owner changes for |callback|.
582 // Any unique (service_name, callback) can be used. Non-registered callbacks
583 // for a given service name are ignored.
584 // |service_name| must not be empty and |callback| must not be null.
585 //
586 // Must be called in the origin thread.
587 virtual void UnlistenForServiceOwnerChange(
588 const std::string& service_name,
589 const GetServiceOwnerCallback& callback);
590
zqiua84d25f2015-07-08 11:08:30 +0900591 // Return the unique name of the bus connnection if it is connected to
592 // D-BUS. Otherwise, return an empty string.
593 std::string GetConnectionName();
594
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900595 // Returns true if the bus is connected to D-Bus.
596 bool is_connected() { return connection_ != NULL; }
597
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900598 protected:
599 // This is protected, so we can define sub classes.
600 virtual ~Bus();
601
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900602 private:
603 friend class base::RefCountedThreadSafe<Bus>;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900604
deymo@chromium.org6d168a72013-01-30 05:29:12 +0900605 // Helper function used for RemoveObjectProxy().
606 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
607 const base::Closure& callback);
608
armansitof4364642014-09-06 02:49:34 +0900609 // Helper functions used for RemoveObjectManager().
610 void RemoveObjectManagerInternal(
611 scoped_refptr<dbus::ObjectManager> object_manager,
612 const base::Closure& callback);
613 void RemoveObjectManagerInternalHelper(
614 scoped_refptr<dbus::ObjectManager> object_manager,
615 const base::Closure& callback);
616
keybuk@chromium.orgd2ca8f32012-03-14 10:18:35 +0900617 // Helper function used for UnregisterExportedObject().
618 void UnregisterExportedObjectInternal(
619 scoped_refptr<dbus::ExportedObject> exported_object);
620
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900621 // Helper function used for ShutdownOnDBusThreadAndBlock().
622 void ShutdownOnDBusThreadAndBlockInternal();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900623
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900624 // Helper function used for RequestOwnership().
625 void RequestOwnershipInternal(const std::string& service_name,
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900626 ServiceOwnershipOptions options,
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900627 OnOwnershipCallback on_ownership_callback);
628
thestig@chromium.org56057f22013-05-05 00:48:37 +0900629 // Helper function used for GetServiceOwner().
630 void GetServiceOwnerInternal(const std::string& service_name,
631 const GetServiceOwnerCallback& callback);
632
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900633 // Helper function used for ListenForServiceOwnerChange().
634 void ListenForServiceOwnerChangeInternal(
635 const std::string& service_name,
636 const GetServiceOwnerCallback& callback);
637
638 // Helper function used for UnListenForServiceOwnerChange().
639 void UnlistenForServiceOwnerChangeInternal(
640 const std::string& service_name,
641 const GetServiceOwnerCallback& callback);
642
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900643 // Processes the all incoming data to the connection, if any.
644 //
645 // BLOCKING CALL.
646 void ProcessAllIncomingDataIfAny();
647
648 // Called when a watch object is added. Used to start monitoring the
649 // file descriptor used for D-Bus communication.
650 dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
651
652 // Called when a watch object is removed.
653 void OnRemoveWatch(DBusWatch* raw_watch);
654
655 // Called when the "enabled" status of |raw_watch| is toggled.
656 void OnToggleWatch(DBusWatch* raw_watch);
657
658 // Called when a timeout object is added. Used to start monitoring
659 // timeout for method calls.
660 dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
661
662 // Called when a timeout object is removed.
663 void OnRemoveTimeout(DBusTimeout* raw_timeout);
664
665 // Called when the "enabled" status of |raw_timeout| is toggled.
666 void OnToggleTimeout(DBusTimeout* raw_timeout);
667
668 // Called when the dispatch status (i.e. if any incoming data is
669 // available) is changed.
670 void OnDispatchStatusChanged(DBusConnection* connection,
671 DBusDispatchStatus status);
672
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900673 // Called when a service owner change occurs.
674 void OnServiceOwnerChanged(DBusMessage* message);
675
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900676 // Callback helper functions. Redirects to the corresponding member function.
677 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
678 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
679 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
680 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
681 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
682 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
683 static void OnDispatchStatusChangedThunk(DBusConnection* connection,
684 DBusDispatchStatus status,
685 void* data);
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900686
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900687 // Calls OnConnectionDisconnected if the Disconnected signal is received.
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900688 static DBusHandlerResult OnConnectionDisconnectedFilter(
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900689 DBusConnection* connection,
690 DBusMessage* message,
691 void* user_data);
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900692
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900693 // Calls OnServiceOwnerChanged for a NameOwnerChanged signal.
694 static DBusHandlerResult OnServiceOwnerChangedFilter(
695 DBusConnection* connection,
696 DBusMessage* message,
697 void* user_data);
698
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900699 const BusType bus_type_;
700 const ConnectionType connection_type_;
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900701 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900702 base::WaitableEvent on_shutdown_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900703 DBusConnection* connection_;
704
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900705 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900706 base::PlatformThreadId origin_thread_id_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900707
708 std::set<std::string> owned_service_names_;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900709 // The following sets are used to check if rules/object_paths/filters
710 // are properly cleaned up before destruction of the bus object.
deymo@chromium.org7894ebf2013-01-31 15:08:02 +0900711 // Since it's not an error to add the same match rule twice, the repeated
712 // match rules are counted in a map.
713 std::map<std::string, int> match_rules_added_;
keybuk@google.combf4649a2012-02-15 06:29:06 +0900714 std::set<ObjectPath> registered_object_paths_;
satorux@chromium.org66bc4c22011-10-06 09:20:53 +0900715 std::set<std::pair<DBusHandleMessageFunction, void*> >
716 filter_functions_added_;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900717
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900718 // ObjectProxyTable is used to hold the object proxies created by the
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900719 // bus object. Key is a pair; the first part is a concatenated string of
720 // service name + object path, like
721 // "org.chromium.TestService/org/chromium/TestObject".
722 // The second part is the ObjectProxy::Options for the proxy.
723 typedef std::map<std::pair<std::string, int>,
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900724 scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
725 ObjectProxyTable object_proxy_table_;
726
727 // ExportedObjectTable is used to hold the exported objects created by
728 // the bus object. Key is a concatenated string of service name +
729 // object path, like "org.chromium.TestService/org/chromium/TestObject".
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900730 typedef std::map<const dbus::ObjectPath,
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900731 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
732 ExportedObjectTable exported_object_table_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900733
keybuk@chromium.org09715012013-03-26 03:20:08 +0900734 // ObjectManagerTable is used to hold the object managers created by the
735 // bus object. Key is a concatenated string of service name + object path,
736 // like "org.chromium.TestService/org/chromium/TestObject".
737 typedef std::map<std::string,
738 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable;
739 ObjectManagerTable object_manager_table_;
740
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900741 // A map of NameOwnerChanged signals to listen for and the callbacks to run
742 // on the origin thread when the owner changes.
743 // Only accessed on the DBus thread.
744 // Key: Service name
745 // Value: Vector of callbacks. Unique and expected to be small. Not using
746 // std::set here because base::Callbacks don't have a '<' operator.
747 typedef std::map<std::string, std::vector<GetServiceOwnerCallback> >
748 ServiceOwnerChangedListenerMap;
749 ServiceOwnerChangedListenerMap service_owner_changed_listener_map_;
750
satorux@chromium.org326a6f82011-08-27 16:26:34 +0900751 bool async_operations_set_up_;
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900752 bool shutdown_completed_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900753
754 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
755 // OnAddTimeout()/OnRemoveTimeou() are balanced.
756 int num_pending_watches_;
757 int num_pending_timeouts_;
758
nona@chromium.org9f638e02012-04-19 12:20:03 +0900759 std::string address_;
760
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900761 DISALLOW_COPY_AND_ASSIGN(Bus);
762};
763
764} // namespace dbus
765
766#endif // DBUS_BUS_H_