Fix build break
[chromium-blink-merge.git] / dbus / bus.h
blobec6bef1ea417edfb0f4d80875875f8eb98720ecb
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef DBUS_BUS_H_
6 #define DBUS_BUS_H_
8 #include <dbus/dbus.h>
10 #include <map>
11 #include <set>
12 #include <string>
13 #include <utility>
15 #include "base/callback.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/platform_thread.h"
19 #include "dbus/dbus_export.h"
20 #include "dbus/object_path.h"
22 namespace base {
23 class SequencedTaskRunner;
24 class SingleThreadTaskRunner;
25 class Thread;
28 namespace tracked_objects {
29 class Location;
32 namespace dbus {
34 class ExportedObject;
35 class ObjectManager;
36 class ObjectProxy;
38 // Bus is used to establish a connection with D-Bus, create object
39 // proxies, and export objects.
41 // For asynchronous operations such as an asynchronous method call, the
42 // bus object will use a task runner to monitor the underlying file
43 // descriptor used for D-Bus communication. By default, the bus will use
44 // the current thread's task runner. If |dbus_task_runner| option is
45 // specified, the bus will use that task runner instead.
47 // THREADING
49 // In the D-Bus library, we use the two threads:
51 // - The origin thread: the thread that created the Bus object.
52 // - The D-Bus thread: the thread servicing |dbus_task_runner|.
54 // The origin thread is usually Chrome's UI thread. The D-Bus thread is
55 // usually a dedicated thread for the D-Bus library.
57 // BLOCKING CALLS
59 // Functions that issue blocking calls are marked "BLOCKING CALL" and
60 // these functions should be called in the D-Bus thread (if
61 // supplied). AssertOnDBusThread() is placed in these functions.
63 // Note that it's hard to tell if a libdbus function is actually blocking
64 // or not (ex. dbus_bus_request_name() internally calls
65 // dbus_connection_send_with_reply_and_block(), which is a blocking
66 // call). To err on the safe side, we consider all libdbus functions that
67 // deal with the connection to dbus-daemon to be blocking.
69 // SHUTDOWN
71 // The Bus object must be shut down manually by ShutdownAndBlock() and
72 // friends. We require the manual shutdown to make the operation explicit
73 // rather than doing it silently in the destructor.
75 // EXAMPLE USAGE:
77 // Synchronous method call:
79 // dbus::Bus::Options options;
80 // // Set up the bus options here.
81 // ...
82 // dbus::Bus bus(options);
84 // dbus::ObjectProxy* object_proxy =
85 // bus.GetObjectProxy(service_name, object_path);
87 // dbus::MethodCall method_call(interface_name, method_name);
88 // scoped_ptr<dbus::Response> response(
89 // object_proxy.CallMethodAndBlock(&method_call, timeout_ms));
90 // if (response.get() != NULL) { // Success.
91 // ...
92 // }
94 // Asynchronous method call:
96 // void OnResponse(dbus::Response* response) {
97 // // response is NULL if the method call failed.
98 // if (!response)
99 // return;
100 // }
102 // ...
103 // object_proxy.CallMethod(&method_call, timeout_ms,
104 // base::Bind(&OnResponse));
106 // Exporting a method:
108 // void Echo(dbus::MethodCall* method_call,
109 // dbus::ExportedObject::ResponseSender response_sender) {
110 // // Do something with method_call.
111 // Response* response = Response::FromMethodCall(method_call);
112 // // Build response here.
113 // // Can send an immediate response here to implement a synchronous service
114 // // or store the response_sender and send a response later to implement an
115 // // asynchronous service.
116 // response_sender.Run(response);
117 // }
119 // void OnExported(const std::string& interface_name,
120 // const ObjectPath& object_path,
121 // bool success) {
122 // // success is true if the method was exported successfully.
123 // }
125 // ...
126 // dbus::ExportedObject* exported_object =
127 // bus.GetExportedObject(service_name, object_path);
128 // exported_object.ExportMethod(interface_name, method_name,
129 // base::Bind(&Echo),
130 // base::Bind(&OnExported));
132 // WHY IS THIS A REF COUNTED OBJECT?
134 // Bus is a ref counted object, to ensure that |this| of the object is
135 // alive when callbacks referencing |this| are called. However, after the
136 // bus is shut down, |connection_| can be NULL. Hence, callbacks should
137 // not rely on that |connection_| is alive.
138 class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
139 public:
140 // Specifies the bus type. SESSION is used to communicate with per-user
141 // services like GNOME applications. SYSTEM is used to communicate with
142 // system-wide services like NetworkManager. CUSTOM_ADDRESS is used to
143 // communicate with an user specified address.
144 enum BusType {
145 SESSION = DBUS_BUS_SESSION,
146 SYSTEM = DBUS_BUS_SYSTEM,
147 CUSTOM_ADDRESS,
150 // Specifies the connection type. PRIVATE should usually be used unless
151 // you are sure that SHARED is safe for you, which is unlikely the case
152 // in Chrome.
154 // PRIVATE gives you a private connection, that won't be shared with
155 // other Bus objects.
157 // SHARED gives you a connection shared among other Bus objects, which
158 // is unsafe if the connection is shared with multiple threads.
159 enum ConnectionType {
160 PRIVATE,
161 SHARED,
164 // Options used to create a Bus object.
165 struct CHROME_DBUS_EXPORT Options {
166 Options();
167 ~Options();
169 BusType bus_type; // SESSION by default.
170 ConnectionType connection_type; // PRIVATE by default.
171 // If dbus_task_runner is set, the bus object will use that
172 // task runner to process asynchronous operations.
174 // The thread servicing the task runner should meet the following
175 // requirements:
176 // 1) Already running.
177 // 2) Has a MessageLoopForIO.
178 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
180 // Specifies the server addresses to be connected. If you want to
181 // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to
182 // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to
183 // connect to. The format of this address value is the dbus address style
184 // which is described in
185 // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
187 // EXAMPLE USAGE:
188 // dbus::Bus::Options options;
189 // options.bus_type = CUSTOM_ADDRESS;
190 // options.address.assign("unix:path=/tmp/dbus-XXXXXXX");
191 // // Set up other options
192 // dbus::Bus bus(options);
194 // // Do something.
196 std::string address;
198 // If the connection with dbus-daemon is closed, |disconnected_callback|
199 // will be called on the origin thread. This is also called when the
200 // disonnection by ShutdownAndBlock. |disconnected_callback| can be null
201 // callback
202 base::Closure disconnected_callback;
205 // Creates a Bus object. The actual connection will be established when
206 // Connect() is called.
207 explicit Bus(const Options& options);
209 // Called when an ownership request is complete.
210 // Parameters:
211 // - the requested service name.
212 // - whether ownership has been obtained or not.
213 typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback;
214 // TODO(satorux): Remove the service name parameter as the caller of
215 // RequestOwnership() knows the service name.
217 // Gets the object proxy for the given service name and the object path.
218 // The caller must not delete the returned object.
220 // Returns an existing object proxy if the bus object already owns the
221 // object proxy for the given service name and the object path.
222 // Never returns NULL.
224 // The bus will own all object proxies created by the bus, to ensure
225 // that the object proxies are detached from remote objects at the
226 // shutdown time of the bus.
228 // The object proxy is used to call methods of remote objects, and
229 // receive signals from them.
231 // |service_name| looks like "org.freedesktop.NetworkManager", and
232 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
234 // Must be called in the origin thread.
235 virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
236 const ObjectPath& object_path);
238 // Same as above, but also takes a bitfield of ObjectProxy::Options.
239 // See object_proxy.h for available options.
240 virtual ObjectProxy* GetObjectProxyWithOptions(
241 const std::string& service_name,
242 const ObjectPath& object_path,
243 int options);
245 // Removes the previously created object proxy for the given service
246 // name and the object path and releases its memory.
248 // If and object proxy for the given service name and object was
249 // created with GetObjectProxy, this function removes it from the
250 // bus object and detaches the ObjectProxy, invalidating any pointer
251 // previously acquired for it with GetObjectProxy. A subsequent call
252 // to GetObjectProxy will return a new object.
254 // All the object proxies are detached from remote objects at the
255 // shutdown time of the bus, but they can be detached early to reduce
256 // memory footprint and used match rules for the bus connection.
258 // |service_name| looks like "org.freedesktop.NetworkManager", and
259 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
260 // |callback| is called when the object proxy is successfully removed and
261 // detached.
263 // The function returns true when there is an object proxy matching the
264 // |service_name| and |object_path| to remove, and calls |callback| when it
265 // is removed. Otherwise, it returns false and the |callback| function is
266 // never called. The |callback| argument must not be null.
268 // Must be called in the origin thread.
269 virtual bool RemoveObjectProxy(const std::string& service_name,
270 const ObjectPath& object_path,
271 const base::Closure& callback);
273 // Same as above, but also takes a bitfield of ObjectProxy::Options.
274 // See object_proxy.h for available options.
275 virtual bool RemoveObjectProxyWithOptions(
276 const std::string& service_name,
277 const ObjectPath& object_path,
278 int options,
279 const base::Closure& callback);
281 // Gets the exported object for the given object path.
282 // The caller must not delete the returned object.
284 // Returns an existing exported object if the bus object already owns
285 // the exported object for the given object path. Never returns NULL.
287 // The bus will own all exported objects created by the bus, to ensure
288 // that the exported objects are unregistered at the shutdown time of
289 // the bus.
291 // The exported object is used to export methods of local objects, and
292 // send signal from them.
294 // Must be called in the origin thread.
295 virtual ExportedObject* GetExportedObject(const ObjectPath& object_path);
297 // Unregisters the exported object for the given object path |object_path|.
299 // Getting an exported object for the same object path after this call
300 // will return a new object, method calls on any remaining copies of the
301 // previous object will not be called.
303 // Must be called in the origin thread.
304 virtual void UnregisterExportedObject(const ObjectPath& object_path);
307 // Gets an object manager for the given remote object path |object_path|
308 // exported by the service |service_name|.
310 // Returns an existing object manager if the bus object already owns a
311 // matching object manager, never returns NULL.
313 // The caller must not delete the returned object, the bus retains ownership
314 // of all object managers.
316 // Must be called in the origin thread.
317 virtual ObjectManager* GetObjectManager(const std::string& service_name,
318 const ObjectPath& object_path);
320 // Unregisters the object manager for the given remote object path
321 // |object_path| exported by the srevice |service_name|.
323 // Getting an object manager for the same remote object after this call
324 // will return a new object, method calls on any remaining copies of the
325 // previous object are not permitted.
327 // Must be called in the origin thread.
328 virtual void RemoveObjectManager(const std::string& service_name,
329 const ObjectPath& object_path);
331 // Instructs all registered object managers to retrieve their set of managed
332 // objects from their respective remote objects. There is no need to call this
333 // manually, this is called automatically by the D-Bus thread manager once
334 // implementation classes are registered.
335 virtual void GetManagedObjects();
337 // Shuts down the bus and blocks until it's done. More specifically, this
338 // function does the following:
340 // - Unregisters the object paths
341 // - Releases the service names
342 // - Closes the connection to dbus-daemon.
344 // This function can be called multiple times and it is no-op for the 2nd time
345 // calling.
347 // BLOCKING CALL.
348 virtual void ShutdownAndBlock();
350 // Similar to ShutdownAndBlock(), but this function is used to
351 // synchronously shut down the bus that uses the D-Bus thread. This
352 // function is intended to be used at the very end of the browser
353 // shutdown, where it makes more sense to shut down the bus
354 // synchronously, than trying to make it asynchronous.
356 // BLOCKING CALL, but must be called in the origin thread.
357 virtual void ShutdownOnDBusThreadAndBlock();
359 // Returns true if the shutdown has been completed.
360 bool shutdown_completed() { return shutdown_completed_; }
363 // The public functions below are not intended to be used in client
364 // code. These are used to implement ObjectProxy and ExportedObject.
367 // Connects the bus to the dbus-daemon.
368 // Returns true on success, or the bus is already connected.
370 // BLOCKING CALL.
371 virtual bool Connect();
373 // Disconnects the bus from the dbus-daemon.
374 // Safe to call multiple times and no operation after the first call.
375 // Do not call for shared connection it will be released by libdbus.
377 // BLOCKING CALL.
378 virtual void ClosePrivateConnection();
380 // Requests the ownership of the service name given by |service_name|.
381 // See also RequestOwnershipAndBlock().
383 // |on_ownership_callback| is called when the service name is obtained
384 // or failed to be obtained, in the origin thread.
386 // Must be called in the origin thread.
387 virtual void RequestOwnership(const std::string& service_name,
388 OnOwnershipCallback on_ownership_callback);
390 // Requests the ownership of the given service name.
391 // Returns true on success, or the the service name is already obtained.
393 // BLOCKING CALL.
394 virtual bool RequestOwnershipAndBlock(const std::string& service_name);
396 // Releases the ownership of the given service name.
397 // Returns true on success.
399 // BLOCKING CALL.
400 virtual bool ReleaseOwnership(const std::string& service_name);
402 // Sets up async operations.
403 // Returns true on success, or it's already set up.
404 // This function needs to be called before starting async operations.
406 // BLOCKING CALL.
407 virtual bool SetUpAsyncOperations();
409 // Sends a message to the bus and blocks until the response is
410 // received. Used to implement synchronous method calls.
412 // BLOCKING CALL.
413 virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
414 int timeout_ms,
415 DBusError* error);
417 // Requests to send a message to the bus. The reply is handled with
418 // |pending_call| at a later time.
420 // BLOCKING CALL.
421 virtual void SendWithReply(DBusMessage* request,
422 DBusPendingCall** pending_call,
423 int timeout_ms);
425 // Requests to send a message to the bus. The message serial number will
426 // be stored in |serial|.
428 // BLOCKING CALL.
429 virtual void Send(DBusMessage* request, uint32* serial);
431 // Adds the message filter function. |filter_function| will be called
432 // when incoming messages are received. Returns true on success.
434 // When a new incoming message arrives, filter functions are called in
435 // the order that they were added until the the incoming message is
436 // handled by a filter function.
438 // The same filter function associated with the same user data cannot be
439 // added more than once. Returns false for this case.
441 // BLOCKING CALL.
442 virtual bool AddFilterFunction(DBusHandleMessageFunction filter_function,
443 void* user_data);
445 // Removes the message filter previously added by AddFilterFunction().
446 // Returns true on success.
448 // BLOCKING CALL.
449 virtual bool RemoveFilterFunction(DBusHandleMessageFunction filter_function,
450 void* user_data);
452 // Adds the match rule. Messages that match the rule will be processed
453 // by the filter functions added by AddFilterFunction().
455 // You cannot specify which filter function to use for a match rule.
456 // Instead, you should check if an incoming message is what you are
457 // interested in, in the filter functions.
459 // The same match rule can be added more than once and should be removed
460 // as many times as it was added.
462 // The match rule looks like:
463 // "type='signal', interface='org.chromium.SomeInterface'".
465 // See "Message Bus Message Routing" section in the D-Bus specification
466 // for details about match rules:
467 // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
469 // BLOCKING CALL.
470 virtual void AddMatch(const std::string& match_rule, DBusError* error);
472 // Removes the match rule previously added by AddMatch().
473 // Returns false if the requested match rule is unknown or has already been
474 // removed. Otherwise, returns true and sets |error| accordingly.
476 // BLOCKING CALL.
477 virtual bool RemoveMatch(const std::string& match_rule, DBusError* error);
479 // Tries to register the object path. Returns true on success.
480 // Returns false if the object path is already registered.
482 // |message_function| in |vtable| will be called every time when a new
483 // |message sent to the object path arrives.
485 // The same object path must not be added more than once.
487 // See also documentation of |dbus_connection_try_register_object_path| at
488 // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
490 // BLOCKING CALL.
491 virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
492 const DBusObjectPathVTable* vtable,
493 void* user_data,
494 DBusError* error);
496 // Unregister the object path.
498 // BLOCKING CALL.
499 virtual void UnregisterObjectPath(const ObjectPath& object_path);
501 // Posts the task to the task runner of the thread that created the bus.
502 virtual void PostTaskToOriginThread(
503 const tracked_objects::Location& from_here,
504 const base::Closure& task);
506 // Posts the task to the task runner of the D-Bus thread. If D-Bus
507 // thread is not supplied, the task runner of the origin thread will be
508 // used.
509 virtual void PostTaskToDBusThread(
510 const tracked_objects::Location& from_here,
511 const base::Closure& task);
513 // Posts the delayed task to the task runner of the D-Bus thread. If
514 // D-Bus thread is not supplied, the task runner of the origin thread
515 // will be used.
516 virtual void PostDelayedTaskToDBusThread(
517 const tracked_objects::Location& from_here,
518 const base::Closure& task,
519 base::TimeDelta delay);
521 // Returns true if the bus has the D-Bus thread.
522 virtual bool HasDBusThread();
524 // Check whether the current thread is on the origin thread (the thread
525 // that created the bus). If not, DCHECK will fail.
526 virtual void AssertOnOriginThread();
528 // Check whether the current thread is on the D-Bus thread. If not,
529 // DCHECK will fail. If the D-Bus thread is not supplied, it calls
530 // AssertOnOriginThread().
531 virtual void AssertOnDBusThread();
533 // Returns true if the bus is connected to D-Bus.
534 bool is_connected() { return connection_ != NULL; }
536 protected:
537 // This is protected, so we can define sub classes.
538 virtual ~Bus();
540 private:
541 friend class base::RefCountedThreadSafe<Bus>;
543 // Helper function used for RemoveObjectProxy().
544 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
545 const base::Closure& callback);
547 // Helper function used for UnregisterExportedObject().
548 void UnregisterExportedObjectInternal(
549 scoped_refptr<dbus::ExportedObject> exported_object);
551 // Helper function used for ShutdownOnDBusThreadAndBlock().
552 void ShutdownOnDBusThreadAndBlockInternal();
554 // Helper function used for RequestOwnership().
555 void RequestOwnershipInternal(const std::string& service_name,
556 OnOwnershipCallback on_ownership_callback);
558 // Processes the all incoming data to the connection, if any.
560 // BLOCKING CALL.
561 void ProcessAllIncomingDataIfAny();
563 // Called when a watch object is added. Used to start monitoring the
564 // file descriptor used for D-Bus communication.
565 dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
567 // Called when a watch object is removed.
568 void OnRemoveWatch(DBusWatch* raw_watch);
570 // Called when the "enabled" status of |raw_watch| is toggled.
571 void OnToggleWatch(DBusWatch* raw_watch);
573 // Called when a timeout object is added. Used to start monitoring
574 // timeout for method calls.
575 dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
577 // Called when a timeout object is removed.
578 void OnRemoveTimeout(DBusTimeout* raw_timeout);
580 // Called when the "enabled" status of |raw_timeout| is toggled.
581 void OnToggleTimeout(DBusTimeout* raw_timeout);
583 // Called when the dispatch status (i.e. if any incoming data is
584 // available) is changed.
585 void OnDispatchStatusChanged(DBusConnection* connection,
586 DBusDispatchStatus status);
588 // Called when the connection is diconnected.
589 void OnConnectionDisconnected(DBusConnection* connection);
591 // Callback helper functions. Redirects to the corresponding member function.
592 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
593 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
594 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
595 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
596 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
597 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
598 static void OnDispatchStatusChangedThunk(DBusConnection* connection,
599 DBusDispatchStatus status,
600 void* data);
602 // Calls OnConnectionDisconnected if the Diconnected signal is received.
603 static DBusHandlerResult OnConnectionDisconnectedFilter(
604 DBusConnection* connection,
605 DBusMessage* message,
606 void* user_data);
608 const BusType bus_type_;
609 const ConnectionType connection_type_;
610 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
611 base::WaitableEvent on_shutdown_;
612 DBusConnection* connection_;
614 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
615 base::PlatformThreadId origin_thread_id_;
617 std::set<std::string> owned_service_names_;
618 // The following sets are used to check if rules/object_paths/filters
619 // are properly cleaned up before destruction of the bus object.
620 // Since it's not an error to add the same match rule twice, the repeated
621 // match rules are counted in a map.
622 std::map<std::string, int> match_rules_added_;
623 std::set<ObjectPath> registered_object_paths_;
624 std::set<std::pair<DBusHandleMessageFunction, void*> >
625 filter_functions_added_;
627 // ObjectProxyTable is used to hold the object proxies created by the
628 // bus object. Key is a pair; the first part is a concatenated string of
629 // service name + object path, like
630 // "org.chromium.TestService/org/chromium/TestObject".
631 // The second part is the ObjectProxy::Options for the proxy.
632 typedef std::map<std::pair<std::string, int>,
633 scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
634 ObjectProxyTable object_proxy_table_;
636 // ExportedObjectTable is used to hold the exported objects created by
637 // the bus object. Key is a concatenated string of service name +
638 // object path, like "org.chromium.TestService/org/chromium/TestObject".
639 typedef std::map<const dbus::ObjectPath,
640 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
641 ExportedObjectTable exported_object_table_;
643 // ObjectManagerTable is used to hold the object managers created by the
644 // bus object. Key is a concatenated string of service name + object path,
645 // like "org.chromium.TestService/org/chromium/TestObject".
646 typedef std::map<std::string,
647 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable;
648 ObjectManagerTable object_manager_table_;
650 bool async_operations_set_up_;
651 bool shutdown_completed_;
653 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
654 // OnAddTimeout()/OnRemoveTimeou() are balanced.
655 int num_pending_watches_;
656 int num_pending_timeouts_;
658 std::string address_;
659 base::Closure on_disconnected_closure_;
661 DISALLOW_COPY_AND_ASSIGN(Bus);
664 } // namespace dbus
666 #endif // DBUS_BUS_H_