blob: 544d31fc169bdf0038e1943af28f7823d1776b7d [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>
9
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +090010#include <map>
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090011#include <set>
12#include <string>
satorux@chromium.org66bc4c22011-10-06 09:20:53 +090013#include <utility>
thestig@chromium.orgc2482f12013-06-11 07:52:34 +090014#include <vector>
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090015
16#include "base/callback.h"
17#include "base/memory/ref_counted.h"
satorux@chromium.orgd336d452011-09-02 15:56:23 +090018#include "base/synchronization/waitable_event.h"
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +090019#include "base/threading/platform_thread.h"
tfarina@chromium.org7928ea22012-11-05 10:56:14 +090020#include "dbus/dbus_export.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090021#include "dbus/object_path.h"
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090022
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090023namespace base {
thestig@chromium.org074b1db2013-02-20 10:36:53 +090024class SequencedTaskRunner;
25class SingleThreadTaskRunner;
hashimoto@chromium.org955f6482013-09-26 13:32:29 +090026class TaskRunner;
thestig@chromium.org074b1db2013-02-20 10:36:53 +090027}
28
29namespace tracked_objects {
30class Location;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090031}
32
33namespace dbus {
34
35class ExportedObject;
keybuk@chromium.org09715012013-03-26 03:20:08 +090036class ObjectManager;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090037class ObjectProxy;
38
39// Bus is used to establish a connection with D-Bus, create object
40// proxies, and export objects.
41//
42// For asynchronous operations such as an asynchronous method call, the
thestig@chromium.org074b1db2013-02-20 10:36:53 +090043// bus object will use a task runner to monitor the underlying file
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090044// descriptor used for D-Bus communication. By default, the bus will use
thestig@chromium.org074b1db2013-02-20 10:36:53 +090045// the current thread's task runner. If |dbus_task_runner| option is
46// specified, the bus will use that task runner instead.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090047//
48// THREADING
49//
50// In the D-Bus library, we use the two threads:
51//
52// - The origin thread: the thread that created the Bus object.
thestig@chromium.org074b1db2013-02-20 10:36:53 +090053// - The D-Bus thread: the thread servicing |dbus_task_runner|.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090054//
55// The origin thread is usually Chrome's UI thread. The D-Bus thread is
56// usually a dedicated thread for the D-Bus library.
57//
58// BLOCKING CALLS
59//
60// Functions that issue blocking calls are marked "BLOCKING CALL" and
61// these functions should be called in the D-Bus thread (if
62// supplied). AssertOnDBusThread() is placed in these functions.
63//
64// Note that it's hard to tell if a libdbus function is actually blocking
65// or not (ex. dbus_bus_request_name() internally calls
66// dbus_connection_send_with_reply_and_block(), which is a blocking
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +090067// call). To err on the safe side, we consider all libdbus functions that
satorux@chromium.orgc6ac7572011-09-01 03:02:43 +090068// deal with the connection to dbus-daemon to be blocking.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090069//
satorux@chromium.org326a6f82011-08-27 16:26:34 +090070// SHUTDOWN
71//
satorux@chromium.orgd336d452011-09-02 15:56:23 +090072// The Bus object must be shut down manually by ShutdownAndBlock() and
73// friends. We require the manual shutdown to make the operation explicit
74// rather than doing it silently in the destructor.
satorux@chromium.org326a6f82011-08-27 16:26:34 +090075//
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090076// EXAMPLE USAGE:
77//
78// Synchronous method call:
79//
80// dbus::Bus::Options options;
81// // Set up the bus options here.
82// ...
83// dbus::Bus bus(options);
84//
85// dbus::ObjectProxy* object_proxy =
86// bus.GetObjectProxy(service_name, object_path);
87//
88// dbus::MethodCall method_call(interface_name, method_name);
mdm@chromium.org45f2c6a2011-09-07 05:03:24 +090089// scoped_ptr<dbus::Response> response(
90// object_proxy.CallMethodAndBlock(&method_call, timeout_ms));
91// if (response.get() != NULL) { // Success.
92// ...
93// }
satorux@chromium.org163f1cb2011-08-18 05:58:12 +090094//
95// Asynchronous method call:
96//
97// void OnResponse(dbus::Response* response) {
98// // response is NULL if the method call failed.
99// if (!response)
100// return;
101// }
102//
103// ...
104// object_proxy.CallMethod(&method_call, timeout_ms,
105// base::Bind(&OnResponse));
106//
107// Exporting a method:
108//
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900109// void Echo(dbus::MethodCall* method_call,
110// dbus::ExportedObject::ResponseSender response_sender) {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900111// // Do something with method_call.
112// Response* response = Response::FromMethodCall(method_call);
113// // Build response here.
vlaviano@chromium.org241215d2011-11-30 13:57:42 +0900114// // Can send an immediate response here to implement a synchronous service
115// // or store the response_sender and send a response later to implement an
116// // asynchronous service.
117// response_sender.Run(response);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900118// }
119//
120// void OnExported(const std::string& interface_name,
keybuk@google.combf4649a2012-02-15 06:29:06 +0900121// const ObjectPath& object_path,
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900122// bool success) {
123// // success is true if the method was exported successfully.
124// }
125//
126// ...
127// dbus::ExportedObject* exported_object =
128// bus.GetExportedObject(service_name, object_path);
129// exported_object.ExportMethod(interface_name, method_name,
130// base::Bind(&Echo),
131// base::Bind(&OnExported));
132//
133// WHY IS THIS A REF COUNTED OBJECT?
134//
135// Bus is a ref counted object, to ensure that |this| of the object is
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900136// alive when callbacks referencing |this| are called. However, after the
137// bus is shut down, |connection_| can be NULL. Hence, callbacks should
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900138// not rely on that |connection_| is alive.
tfarina@chromium.org7928ea22012-11-05 10:56:14 +0900139class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900140 public:
141 // Specifies the bus type. SESSION is used to communicate with per-user
142 // services like GNOME applications. SYSTEM is used to communicate with
nona@chromium.org9f638e02012-04-19 12:20:03 +0900143 // system-wide services like NetworkManager. CUSTOM_ADDRESS is used to
144 // communicate with an user specified address.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900145 enum BusType {
146 SESSION = DBUS_BUS_SESSION,
nona@chromium.org9f638e02012-04-19 12:20:03 +0900147 SYSTEM = DBUS_BUS_SYSTEM,
148 CUSTOM_ADDRESS,
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900149 };
150
151 // Specifies the connection type. PRIVATE should usually be used unless
152 // you are sure that SHARED is safe for you, which is unlikely the case
153 // in Chrome.
154 //
155 // PRIVATE gives you a private connection, that won't be shared with
156 // other Bus objects.
157 //
158 // SHARED gives you a connection shared among other Bus objects, which
159 // is unsafe if the connection is shared with multiple threads.
160 enum ConnectionType {
161 PRIVATE,
162 SHARED,
163 };
164
thestig@chromium.org56057f22013-05-05 00:48:37 +0900165 // Specifies whether the GetServiceOwnerAndBlock call should report or
166 // suppress errors.
167 enum GetServiceOwnerOption {
168 REPORT_ERRORS,
169 SUPPRESS_ERRORS,
170 };
171
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900172 // Specifies service ownership options.
173 //
174 // REQUIRE_PRIMARY indicates that you require primary ownership of the
175 // service name.
176 //
177 // ALLOW_REPLACEMENT indicates that you'll allow another connection to
178 // steal ownership of this service name from you.
179 //
180 // REQUIRE_PRIMARY_ALLOW_REPLACEMENT does the obvious.
181 enum ServiceOwnershipOptions {
182 REQUIRE_PRIMARY = (DBUS_NAME_FLAG_DO_NOT_QUEUE |
183 DBUS_NAME_FLAG_REPLACE_EXISTING),
184 REQUIRE_PRIMARY_ALLOW_REPLACEMENT = (REQUIRE_PRIMARY |
185 DBUS_NAME_FLAG_ALLOW_REPLACEMENT),
186 };
187
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900188 // Options used to create a Bus object.
tfarina@chromium.org7928ea22012-11-05 10:56:14 +0900189 struct CHROME_DBUS_EXPORT Options {
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900190 Options();
191 ~Options();
192
193 BusType bus_type; // SESSION by default.
194 ConnectionType connection_type; // PRIVATE by default.
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900195 // If dbus_task_runner is set, the bus object will use that
196 // task runner to process asynchronous operations.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900197 //
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900198 // The thread servicing the task runner should meet the following
mdm@chromium.org45f2c6a2011-09-07 05:03:24 +0900199 // requirements:
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900200 // 1) Already running.
201 // 2) Has a MessageLoopForIO.
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900202 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
nona@chromium.org9f638e02012-04-19 12:20:03 +0900203
204 // Specifies the server addresses to be connected. If you want to
205 // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to
206 // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to
207 // connect to. The format of this address value is the dbus address style
208 // which is described in
209 // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
210 //
211 // EXAMPLE USAGE:
212 // dbus::Bus::Options options;
213 // options.bus_type = CUSTOM_ADDRESS;
214 // options.address.assign("unix:path=/tmp/dbus-XXXXXXX");
215 // // Set up other options
216 // dbus::Bus bus(options);
217 //
218 // // Do something.
219 //
220 std::string address;
nona@chromium.org1de76fd2013-02-16 01:44:40 +0900221
222 // If the connection with dbus-daemon is closed, |disconnected_callback|
223 // will be called on the origin thread. This is also called when the
224 // disonnection by ShutdownAndBlock. |disconnected_callback| can be null
225 // callback
226 base::Closure disconnected_callback;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900227 };
228
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900229 // Creates a Bus object. The actual connection will be established when
230 // Connect() is called.
231 explicit Bus(const Options& options);
232
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900233 // Called when an ownership request is complete.
234 // Parameters:
235 // - the requested service name.
236 // - whether ownership has been obtained or not.
237 typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback;
thestig@chromium.org56057f22013-05-05 00:48:37 +0900238
239 // Called when GetServiceOwner() completes.
240 // |service_owner| is the return value from GetServiceOwnerAndBlock().
241 typedef base::Callback<void (const std::string& service_owner)>
242 GetServiceOwnerCallback;
243
deymo@chromium.org6d168a72013-01-30 05:29:12 +0900244 // TODO(satorux): Remove the service name parameter as the caller of
245 // RequestOwnership() knows the service name.
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900246
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900247 // Gets the object proxy for the given service name and the object path.
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900248 // The caller must not delete the returned object.
249 //
250 // Returns an existing object proxy if the bus object already owns the
251 // object proxy for the given service name and the object path.
252 // Never returns NULL.
253 //
254 // The bus will own all object proxies created by the bus, to ensure
255 // that the object proxies are detached from remote objects at the
256 // shutdown time of the bus.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900257 //
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900258 // The object proxy is used to call methods of remote objects, and
259 // receive signals from them.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900260 //
261 // |service_name| looks like "org.freedesktop.NetworkManager", and
262 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
263 //
264 // Must be called in the origin thread.
265 virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
keybuk@google.combf4649a2012-02-15 06:29:06 +0900266 const ObjectPath& object_path);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900267
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900268 // Same as above, but also takes a bitfield of ObjectProxy::Options.
269 // See object_proxy.h for available options.
270 virtual ObjectProxy* GetObjectProxyWithOptions(
271 const std::string& service_name,
keybuk@google.combf4649a2012-02-15 06:29:06 +0900272 const ObjectPath& object_path,
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900273 int options);
274
deymo@chromium.org6d168a72013-01-30 05:29:12 +0900275 // Removes the previously created object proxy for the given service
276 // name and the object path and releases its memory.
277 //
278 // If and object proxy for the given service name and object was
279 // created with GetObjectProxy, this function removes it from the
280 // bus object and detaches the ObjectProxy, invalidating any pointer
281 // previously acquired for it with GetObjectProxy. A subsequent call
282 // to GetObjectProxy will return a new object.
283 //
284 // All the object proxies are detached from remote objects at the
285 // shutdown time of the bus, but they can be detached early to reduce
286 // memory footprint and used match rules for the bus connection.
287 //
288 // |service_name| looks like "org.freedesktop.NetworkManager", and
289 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
290 // |callback| is called when the object proxy is successfully removed and
291 // detached.
292 //
293 // The function returns true when there is an object proxy matching the
294 // |service_name| and |object_path| to remove, and calls |callback| when it
295 // is removed. Otherwise, it returns false and the |callback| function is
296 // never called. The |callback| argument must not be null.
297 //
298 // Must be called in the origin thread.
299 virtual bool RemoveObjectProxy(const std::string& service_name,
300 const ObjectPath& object_path,
301 const base::Closure& callback);
302
303 // Same as above, but also takes a bitfield of ObjectProxy::Options.
304 // See object_proxy.h for available options.
305 virtual bool RemoveObjectProxyWithOptions(
306 const std::string& service_name,
307 const ObjectPath& object_path,
308 int options,
309 const base::Closure& callback);
310
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900311 // Gets the exported object for the given object path.
312 // The caller must not delete the returned object.
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900313 //
314 // Returns an existing exported object if the bus object already owns
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900315 // the exported object for the given object path. Never returns NULL.
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900316 //
317 // The bus will own all exported objects created by the bus, to ensure
318 // that the exported objects are unregistered at the shutdown time of
319 // the bus.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900320 //
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900321 // The exported object is used to export methods of local objects, and
322 // send signal from them.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900323 //
324 // Must be called in the origin thread.
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900325 virtual ExportedObject* GetExportedObject(const ObjectPath& object_path);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900326
keybuk@chromium.orgd2ca8f32012-03-14 10:18:35 +0900327 // Unregisters the exported object for the given object path |object_path|.
328 //
329 // Getting an exported object for the same object path after this call
330 // will return a new object, method calls on any remaining copies of the
331 // previous object will not be called.
332 //
333 // Must be called in the origin thread.
334 virtual void UnregisterExportedObject(const ObjectPath& object_path);
335
keybuk@chromium.org09715012013-03-26 03:20:08 +0900336
337 // Gets an object manager for the given remote object path |object_path|
338 // exported by the service |service_name|.
339 //
340 // Returns an existing object manager if the bus object already owns a
341 // matching object manager, never returns NULL.
342 //
343 // The caller must not delete the returned object, the bus retains ownership
344 // of all object managers.
345 //
346 // Must be called in the origin thread.
347 virtual ObjectManager* GetObjectManager(const std::string& service_name,
348 const ObjectPath& object_path);
349
350 // Unregisters the object manager for the given remote object path
351 // |object_path| exported by the srevice |service_name|.
352 //
353 // Getting an object manager for the same remote object after this call
354 // will return a new object, method calls on any remaining copies of the
355 // previous object are not permitted.
356 //
armansitof4364642014-09-06 02:49:34 +0900357 // This method will asynchronously clean up any match rules that have been
358 // added for the object manager and invoke |callback| when the operation is
359 // complete. If this method returns false, then |callback| is never called.
360 // The |callback| argument must not be null.
361 //
keybuk@chromium.org09715012013-03-26 03:20:08 +0900362 // Must be called in the origin thread.
armansitof4364642014-09-06 02:49:34 +0900363 virtual bool RemoveObjectManager(const std::string& service_name,
364 const ObjectPath& object_path,
365 const base::Closure& callback);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900366
367 // Instructs all registered object managers to retrieve their set of managed
368 // objects from their respective remote objects. There is no need to call this
369 // manually, this is called automatically by the D-Bus thread manager once
370 // implementation classes are registered.
371 virtual void GetManagedObjects();
372
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900373 // Shuts down the bus and blocks until it's done. More specifically, this
374 // function does the following:
375 //
376 // - Unregisters the object paths
377 // - Releases the service names
378 // - Closes the connection to dbus-daemon.
379 //
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900380 // This function can be called multiple times and it is no-op for the 2nd time
381 // calling.
382 //
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900383 // BLOCKING CALL.
384 virtual void ShutdownAndBlock();
385
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900386 // Similar to ShutdownAndBlock(), but this function is used to
387 // synchronously shut down the bus that uses the D-Bus thread. This
388 // function is intended to be used at the very end of the browser
389 // shutdown, where it makes more sense to shut down the bus
390 // synchronously, than trying to make it asynchronous.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900391 //
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900392 // BLOCKING CALL, but must be called in the origin thread.
393 virtual void ShutdownOnDBusThreadAndBlock();
394
395 // Returns true if the shutdown has been completed.
396 bool shutdown_completed() { return shutdown_completed_; }
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900397
398 //
399 // The public functions below are not intended to be used in client
400 // code. These are used to implement ObjectProxy and ExportedObject.
401 //
402
403 // Connects the bus to the dbus-daemon.
404 // Returns true on success, or the bus is already connected.
405 //
406 // BLOCKING CALL.
407 virtual bool Connect();
408
nona@chromium.org1de76fd2013-02-16 01:44:40 +0900409 // Disconnects the bus from the dbus-daemon.
410 // Safe to call multiple times and no operation after the first call.
411 // Do not call for shared connection it will be released by libdbus.
412 //
413 // BLOCKING CALL.
414 virtual void ClosePrivateConnection();
415
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900416 // Requests the ownership of the service name given by |service_name|.
417 // See also RequestOwnershipAndBlock().
418 //
419 // |on_ownership_callback| is called when the service name is obtained
420 // or failed to be obtained, in the origin thread.
421 //
422 // Must be called in the origin thread.
423 virtual void RequestOwnership(const std::string& service_name,
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900424 ServiceOwnershipOptions options,
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900425 OnOwnershipCallback on_ownership_callback);
426
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900427 // Requests the ownership of the given service name.
428 // Returns true on success, or the the service name is already obtained.
429 //
satorux@chromium.org49e0bc72014-01-09 13:39:17 +0900430 // Note that it's important to expose methods before requesting a service
431 // name with this method. See also ExportedObject::ExportMethodAndBlock()
432 // for details.
433 //
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900434 // BLOCKING CALL.
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900435 virtual bool RequestOwnershipAndBlock(const std::string& service_name,
436 ServiceOwnershipOptions options);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900437
438 // Releases the ownership of the given service name.
439 // Returns true on success.
440 //
441 // BLOCKING CALL.
442 virtual bool ReleaseOwnership(const std::string& service_name);
443
444 // Sets up async operations.
445 // Returns true on success, or it's already set up.
446 // This function needs to be called before starting async operations.
447 //
448 // BLOCKING CALL.
449 virtual bool SetUpAsyncOperations();
450
451 // Sends a message to the bus and blocks until the response is
452 // received. Used to implement synchronous method calls.
453 //
454 // BLOCKING CALL.
455 virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
456 int timeout_ms,
457 DBusError* error);
458
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900459 // Requests to send a message to the bus. The reply is handled with
460 // |pending_call| at a later time.
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900461 //
462 // BLOCKING CALL.
463 virtual void SendWithReply(DBusMessage* request,
464 DBusPendingCall** pending_call,
465 int timeout_ms);
466
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900467 // Requests to send a message to the bus. The message serial number will
468 // be stored in |serial|.
469 //
470 // BLOCKING CALL.
471 virtual void Send(DBusMessage* request, uint32* serial);
472
473 // Adds the message filter function. |filter_function| will be called
satorux@chromium.org66bc4c22011-10-06 09:20:53 +0900474 // when incoming messages are received. Returns true on success.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900475 //
476 // When a new incoming message arrives, filter functions are called in
477 // the order that they were added until the the incoming message is
478 // handled by a filter function.
479 //
satorux@chromium.org66bc4c22011-10-06 09:20:53 +0900480 // The same filter function associated with the same user data cannot be
481 // added more than once. Returns false for this case.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900482 //
483 // BLOCKING CALL.
satorux@chromium.org66bc4c22011-10-06 09:20:53 +0900484 virtual bool AddFilterFunction(DBusHandleMessageFunction filter_function,
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900485 void* user_data);
486
487 // Removes the message filter previously added by AddFilterFunction().
satorux@chromium.org66bc4c22011-10-06 09:20:53 +0900488 // Returns true on success.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900489 //
490 // BLOCKING CALL.
satorux@chromium.org66bc4c22011-10-06 09:20:53 +0900491 virtual bool RemoveFilterFunction(DBusHandleMessageFunction filter_function,
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900492 void* user_data);
493
494 // Adds the match rule. Messages that match the rule will be processed
495 // by the filter functions added by AddFilterFunction().
496 //
497 // You cannot specify which filter function to use for a match rule.
498 // Instead, you should check if an incoming message is what you are
499 // interested in, in the filter functions.
500 //
deymo@chromium.org7894ebf2013-01-31 15:08:02 +0900501 // The same match rule can be added more than once and should be removed
502 // as many times as it was added.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900503 //
504 // The match rule looks like:
505 // "type='signal', interface='org.chromium.SomeInterface'".
506 //
507 // See "Message Bus Message Routing" section in the D-Bus specification
508 // for details about match rules:
509 // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
510 //
511 // BLOCKING CALL.
512 virtual void AddMatch(const std::string& match_rule, DBusError* error);
513
514 // Removes the match rule previously added by AddMatch().
deymo@chromium.org7894ebf2013-01-31 15:08:02 +0900515 // Returns false if the requested match rule is unknown or has already been
516 // removed. Otherwise, returns true and sets |error| accordingly.
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900517 //
518 // BLOCKING CALL.
deymo@chromium.org7894ebf2013-01-31 15:08:02 +0900519 virtual bool RemoveMatch(const std::string& match_rule, DBusError* error);
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900520
521 // Tries to register the object path. Returns true on success.
522 // Returns false if the object path is already registered.
523 //
524 // |message_function| in |vtable| will be called every time when a new
525 // |message sent to the object path arrives.
526 //
527 // The same object path must not be added more than once.
528 //
529 // See also documentation of |dbus_connection_try_register_object_path| at
530 // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900531 //
532 // BLOCKING CALL.
keybuk@google.combf4649a2012-02-15 06:29:06 +0900533 virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900534 const DBusObjectPathVTable* vtable,
535 void* user_data,
536 DBusError* error);
537
538 // Unregister the object path.
539 //
540 // BLOCKING CALL.
keybuk@google.combf4649a2012-02-15 06:29:06 +0900541 virtual void UnregisterObjectPath(const ObjectPath& object_path);
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900542
hashimoto@chromium.org955f6482013-09-26 13:32:29 +0900543 // Returns the task runner of the D-Bus thread.
544 virtual base::TaskRunner* GetDBusTaskRunner();
haruki@chromium.org4a1f9562013-05-08 20:57:14 +0900545
hashimoto@chromium.org955f6482013-09-26 13:32:29 +0900546 // Returns the task runner of the thread that created the bus.
547 virtual base::TaskRunner* GetOriginTaskRunner();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900548
549 // Returns true if the bus has the D-Bus thread.
550 virtual bool HasDBusThread();
551
552 // Check whether the current thread is on the origin thread (the thread
553 // that created the bus). If not, DCHECK will fail.
554 virtual void AssertOnOriginThread();
555
556 // Check whether the current thread is on the D-Bus thread. If not,
557 // DCHECK will fail. If the D-Bus thread is not supplied, it calls
558 // AssertOnOriginThread().
559 virtual void AssertOnDBusThread();
560
thestig@chromium.org56057f22013-05-05 00:48:37 +0900561 // Gets the owner for |service_name| via org.freedesktop.DBus.GetNameOwner.
562 // Returns the owner name, if any, or an empty string on failure.
563 // |options| specifies where to printing error messages or not.
564 //
565 // BLOCKING CALL.
566 virtual std::string GetServiceOwnerAndBlock(const std::string& service_name,
567 GetServiceOwnerOption options);
568
569 // A non-blocking version of GetServiceOwnerAndBlock().
570 // Must be called in the origin thread.
571 virtual void GetServiceOwner(const std::string& service_name,
572 const GetServiceOwnerCallback& callback);
573
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900574 // Whenever the owner for |service_name| changes, run |callback| with the
575 // name of the new owner. If the owner goes away, then |callback| receives
576 // an empty string.
577 //
578 // Any unique (service_name, callback) can be used. Duplicate are ignored.
579 // |service_name| must not be empty and |callback| must not be null.
580 //
581 // Must be called in the origin thread.
582 virtual void ListenForServiceOwnerChange(
583 const std::string& service_name,
584 const GetServiceOwnerCallback& callback);
585
586 // Stop listening for |service_name| owner changes for |callback|.
587 // Any unique (service_name, callback) can be used. Non-registered callbacks
588 // for a given service name are ignored.
589 // |service_name| must not be empty and |callback| must not be null.
590 //
591 // Must be called in the origin thread.
592 virtual void UnlistenForServiceOwnerChange(
593 const std::string& service_name,
594 const GetServiceOwnerCallback& callback);
595
satorux@chromium.orgc9ebea22011-10-08 01:26:30 +0900596 // Returns true if the bus is connected to D-Bus.
597 bool is_connected() { return connection_ != NULL; }
598
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900599 protected:
600 // This is protected, so we can define sub classes.
601 virtual ~Bus();
602
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900603 private:
604 friend class base::RefCountedThreadSafe<Bus>;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900605
deymo@chromium.org6d168a72013-01-30 05:29:12 +0900606 // Helper function used for RemoveObjectProxy().
607 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
608 const base::Closure& callback);
609
armansitof4364642014-09-06 02:49:34 +0900610 // Helper functions used for RemoveObjectManager().
611 void RemoveObjectManagerInternal(
612 scoped_refptr<dbus::ObjectManager> object_manager,
613 const base::Closure& callback);
614 void RemoveObjectManagerInternalHelper(
615 scoped_refptr<dbus::ObjectManager> object_manager,
616 const base::Closure& callback);
617
keybuk@chromium.orgd2ca8f32012-03-14 10:18:35 +0900618 // Helper function used for UnregisterExportedObject().
619 void UnregisterExportedObjectInternal(
620 scoped_refptr<dbus::ExportedObject> exported_object);
621
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900622 // Helper function used for ShutdownOnDBusThreadAndBlock().
623 void ShutdownOnDBusThreadAndBlockInternal();
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900624
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900625 // Helper function used for RequestOwnership().
626 void RequestOwnershipInternal(const std::string& service_name,
cmasone@chromium.org989857e2013-07-31 15:34:59 +0900627 ServiceOwnershipOptions options,
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900628 OnOwnershipCallback on_ownership_callback);
629
thestig@chromium.org56057f22013-05-05 00:48:37 +0900630 // Helper function used for GetServiceOwner().
631 void GetServiceOwnerInternal(const std::string& service_name,
632 const GetServiceOwnerCallback& callback);
633
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900634 // Helper function used for ListenForServiceOwnerChange().
635 void ListenForServiceOwnerChangeInternal(
636 const std::string& service_name,
637 const GetServiceOwnerCallback& callback);
638
639 // Helper function used for UnListenForServiceOwnerChange().
640 void UnlistenForServiceOwnerChangeInternal(
641 const std::string& service_name,
642 const GetServiceOwnerCallback& callback);
643
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900644 // Processes the all incoming data to the connection, if any.
645 //
646 // BLOCKING CALL.
647 void ProcessAllIncomingDataIfAny();
648
649 // Called when a watch object is added. Used to start monitoring the
650 // file descriptor used for D-Bus communication.
651 dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
652
653 // Called when a watch object is removed.
654 void OnRemoveWatch(DBusWatch* raw_watch);
655
656 // Called when the "enabled" status of |raw_watch| is toggled.
657 void OnToggleWatch(DBusWatch* raw_watch);
658
659 // Called when a timeout object is added. Used to start monitoring
660 // timeout for method calls.
661 dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
662
663 // Called when a timeout object is removed.
664 void OnRemoveTimeout(DBusTimeout* raw_timeout);
665
666 // Called when the "enabled" status of |raw_timeout| is toggled.
667 void OnToggleTimeout(DBusTimeout* raw_timeout);
668
669 // Called when the dispatch status (i.e. if any incoming data is
670 // available) is changed.
671 void OnDispatchStatusChanged(DBusConnection* connection,
672 DBusDispatchStatus status);
673
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900674 // Called when the connection is diconnected.
675 void OnConnectionDisconnected(DBusConnection* connection);
676
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900677 // Called when a service owner change occurs.
678 void OnServiceOwnerChanged(DBusMessage* message);
679
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900680 // Callback helper functions. Redirects to the corresponding member function.
681 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
682 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
683 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
684 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
685 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
686 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
687 static void OnDispatchStatusChangedThunk(DBusConnection* connection,
688 DBusDispatchStatus status,
689 void* data);
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900690
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900691 // Calls OnConnectionDisconnected if the Disconnected signal is received.
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900692 static DBusHandlerResult OnConnectionDisconnectedFilter(
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900693 DBusConnection* connection,
694 DBusMessage* message,
695 void* user_data);
nona@chromium.org5a44d2b2013-02-08 19:53:39 +0900696
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900697 // Calls OnServiceOwnerChanged for a NameOwnerChanged signal.
698 static DBusHandlerResult OnServiceOwnerChangedFilter(
699 DBusConnection* connection,
700 DBusMessage* message,
701 void* user_data);
702
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900703 const BusType bus_type_;
704 const ConnectionType connection_type_;
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900705 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900706 base::WaitableEvent on_shutdown_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900707 DBusConnection* connection_;
708
thestig@chromium.org074b1db2013-02-20 10:36:53 +0900709 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900710 base::PlatformThreadId origin_thread_id_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900711
712 std::set<std::string> owned_service_names_;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900713 // The following sets are used to check if rules/object_paths/filters
714 // are properly cleaned up before destruction of the bus object.
deymo@chromium.org7894ebf2013-01-31 15:08:02 +0900715 // Since it's not an error to add the same match rule twice, the repeated
716 // match rules are counted in a map.
717 std::map<std::string, int> match_rules_added_;
keybuk@google.combf4649a2012-02-15 06:29:06 +0900718 std::set<ObjectPath> registered_object_paths_;
satorux@chromium.org66bc4c22011-10-06 09:20:53 +0900719 std::set<std::pair<DBusHandleMessageFunction, void*> >
720 filter_functions_added_;
satorux@chromium.org7f0c4512011-08-23 16:29:21 +0900721
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900722 // ObjectProxyTable is used to hold the object proxies created by the
adamk@chromium.org35c0eef2012-02-11 06:45:23 +0900723 // bus object. Key is a pair; the first part is a concatenated string of
724 // service name + object path, like
725 // "org.chromium.TestService/org/chromium/TestObject".
726 // The second part is the ObjectProxy::Options for the proxy.
727 typedef std::map<std::pair<std::string, int>,
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900728 scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
729 ObjectProxyTable object_proxy_table_;
730
731 // ExportedObjectTable is used to hold the exported objects created by
732 // the bus object. Key is a concatenated string of service name +
733 // object path, like "org.chromium.TestService/org/chromium/TestObject".
keybuk@chromium.org9cb73f02012-03-10 10:12:52 +0900734 typedef std::map<const dbus::ObjectPath,
satorux@chromium.orgdccbb7b2011-08-24 04:25:20 +0900735 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
736 ExportedObjectTable exported_object_table_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900737
keybuk@chromium.org09715012013-03-26 03:20:08 +0900738 // ObjectManagerTable is used to hold the object managers created by the
739 // bus object. Key is a concatenated string of service name + object path,
740 // like "org.chromium.TestService/org/chromium/TestObject".
741 typedef std::map<std::string,
742 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable;
743 ObjectManagerTable object_manager_table_;
744
thestig@chromium.orgc2482f12013-06-11 07:52:34 +0900745 // A map of NameOwnerChanged signals to listen for and the callbacks to run
746 // on the origin thread when the owner changes.
747 // Only accessed on the DBus thread.
748 // Key: Service name
749 // Value: Vector of callbacks. Unique and expected to be small. Not using
750 // std::set here because base::Callbacks don't have a '<' operator.
751 typedef std::map<std::string, std::vector<GetServiceOwnerCallback> >
752 ServiceOwnerChangedListenerMap;
753 ServiceOwnerChangedListenerMap service_owner_changed_listener_map_;
754
satorux@chromium.org326a6f82011-08-27 16:26:34 +0900755 bool async_operations_set_up_;
satorux@chromium.orgd336d452011-09-02 15:56:23 +0900756 bool shutdown_completed_;
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900757
758 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
759 // OnAddTimeout()/OnRemoveTimeou() are balanced.
760 int num_pending_watches_;
761 int num_pending_timeouts_;
762
nona@chromium.org9f638e02012-04-19 12:20:03 +0900763 std::string address_;
nona@chromium.org1de76fd2013-02-16 01:44:40 +0900764 base::Closure on_disconnected_closure_;
nona@chromium.org9f638e02012-04-19 12:20:03 +0900765
satorux@chromium.org163f1cb2011-08-18 05:58:12 +0900766 DISALLOW_COPY_AND_ASSIGN(Bus);
767};
768
769} // namespace dbus
770
771#endif // DBUS_BUS_H_