Roll WebKit 141933:141963
[chromium-blink-merge.git] / dbus / bus.h
blobe6a9b20096a29e710e37dbeee82effe45d477a95
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 <map>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <dbus/dbus.h>
14 #include "base/callback.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/platform_thread.h"
18 #include "base/tracked_objects.h"
19 #include "dbus/dbus_export.h"
20 #include "dbus/object_path.h"
22 class MessageLoop;
24 namespace base {
25 class Thread;
26 class MessageLoopProxy;
29 namespace dbus {
31 class ExportedObject;
32 class ObjectProxy;
34 // Bus is used to establish a connection with D-Bus, create object
35 // proxies, and export objects.
37 // For asynchronous operations such as an asynchronous method call, the
38 // bus object will use a message loop to monitor the underlying file
39 // descriptor used for D-Bus communication. By default, the bus will use
40 // the current thread's MessageLoopForIO. If |dbus_thread_message_loop_proxy|
41 // option is specified, the bus will use that message loop instead.
43 // THREADING
45 // In the D-Bus library, we use the two threads:
47 // - The origin thread: the thread that created the Bus object.
48 // - The D-Bus thread: the thread servicing |dbus_thread_message_loop_proxy|.
50 // The origin thread is usually Chrome's UI thread. The D-Bus thread is
51 // usually a dedicated thread for the D-Bus library.
53 // BLOCKING CALLS
55 // Functions that issue blocking calls are marked "BLOCKING CALL" and
56 // these functions should be called in the D-Bus thread (if
57 // supplied). AssertOnDBusThread() is placed in these functions.
59 // Note that it's hard to tell if a libdbus function is actually blocking
60 // or not (ex. dbus_bus_request_name() internally calls
61 // dbus_connection_send_with_reply_and_block(), which is a blocking
62 // call). To err on the safe side, we consider all libdbus functions that
63 // deal with the connection to dbus-daemon to be blocking.
65 // SHUTDOWN
67 // The Bus object must be shut down manually by ShutdownAndBlock() and
68 // friends. We require the manual shutdown to make the operation explicit
69 // rather than doing it silently in the destructor.
71 // EXAMPLE USAGE:
73 // Synchronous method call:
75 // dbus::Bus::Options options;
76 // // Set up the bus options here.
77 // ...
78 // dbus::Bus bus(options);
80 // dbus::ObjectProxy* object_proxy =
81 // bus.GetObjectProxy(service_name, object_path);
83 // dbus::MethodCall method_call(interface_name, method_name);
84 // scoped_ptr<dbus::Response> response(
85 // object_proxy.CallMethodAndBlock(&method_call, timeout_ms));
86 // if (response.get() != NULL) { // Success.
87 // ...
88 // }
90 // Asynchronous method call:
92 // void OnResponse(dbus::Response* response) {
93 // // response is NULL if the method call failed.
94 // if (!response)
95 // return;
96 // }
98 // ...
99 // object_proxy.CallMethod(&method_call, timeout_ms,
100 // base::Bind(&OnResponse));
102 // Exporting a method:
104 // void Echo(dbus::MethodCall* method_call,
105 // dbus::ExportedObject::ResponseSender response_sender) {
106 // // Do something with method_call.
107 // Response* response = Response::FromMethodCall(method_call);
108 // // Build response here.
109 // // Can send an immediate response here to implement a synchronous service
110 // // or store the response_sender and send a response later to implement an
111 // // asynchronous service.
112 // response_sender.Run(response);
113 // }
115 // void OnExported(const std::string& interface_name,
116 // const ObjectPath& object_path,
117 // bool success) {
118 // // success is true if the method was exported successfully.
119 // }
121 // ...
122 // dbus::ExportedObject* exported_object =
123 // bus.GetExportedObject(service_name, object_path);
124 // exported_object.ExportMethod(interface_name, method_name,
125 // base::Bind(&Echo),
126 // base::Bind(&OnExported));
128 // WHY IS THIS A REF COUNTED OBJECT?
130 // Bus is a ref counted object, to ensure that |this| of the object is
131 // alive when callbacks referencing |this| are called. However, after the
132 // bus is shut down, |connection_| can be NULL. Hence, callbacks should
133 // not rely on that |connection_| is alive.
134 class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
135 public:
136 // Specifies the bus type. SESSION is used to communicate with per-user
137 // services like GNOME applications. SYSTEM is used to communicate with
138 // system-wide services like NetworkManager. CUSTOM_ADDRESS is used to
139 // communicate with an user specified address.
140 enum BusType {
141 SESSION = DBUS_BUS_SESSION,
142 SYSTEM = DBUS_BUS_SYSTEM,
143 CUSTOM_ADDRESS,
146 // Specifies the connection type. PRIVATE should usually be used unless
147 // you are sure that SHARED is safe for you, which is unlikely the case
148 // in Chrome.
150 // PRIVATE gives you a private connection, that won't be shared with
151 // other Bus objects.
153 // SHARED gives you a connection shared among other Bus objects, which
154 // is unsafe if the connection is shared with multiple threads.
155 enum ConnectionType {
156 PRIVATE,
157 SHARED,
160 // Options used to create a Bus object.
161 struct CHROME_DBUS_EXPORT Options {
162 Options();
163 ~Options();
165 BusType bus_type; // SESSION by default.
166 ConnectionType connection_type; // PRIVATE by default.
167 // If dbus_thread_message_loop_proxy is set, the bus object will use that
168 // message loop to process asynchronous operations.
170 // The thread servicing the message loop proxy should meet the following
171 // requirements:
172 // 1) Already running.
173 // 2) Has a MessageLoopForIO.
174 scoped_refptr<base::MessageLoopProxy> dbus_thread_message_loop_proxy;
176 // Specifies the server addresses to be connected. If you want to
177 // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to
178 // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to
179 // connect to. The format of this address value is the dbus address style
180 // which is described in
181 // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
183 // EXAMPLE USAGE:
184 // dbus::Bus::Options options;
185 // options.bus_type = CUSTOM_ADDRESS;
186 // options.address.assign("unix:path=/tmp/dbus-XXXXXXX");
187 // // Set up other options
188 // dbus::Bus bus(options);
190 // // Do something.
192 std::string address;
195 // Creates a Bus object. The actual connection will be established when
196 // Connect() is called.
197 explicit Bus(const Options& options);
199 // Called when an ownership request is complete.
200 // Parameters:
201 // - the requested service name.
202 // - whether ownership has been obtained or not.
203 typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback;
204 // TODO(satorux): Remove the service name parameter as the caller of
205 // RequestOwnership() knows the service name.
207 // Gets the object proxy for the given service name and the object path.
208 // The caller must not delete the returned object.
210 // Returns an existing object proxy if the bus object already owns the
211 // object proxy for the given service name and the object path.
212 // Never returns NULL.
214 // The bus will own all object proxies created by the bus, to ensure
215 // that the object proxies are detached from remote objects at the
216 // shutdown time of the bus.
218 // The object proxy is used to call methods of remote objects, and
219 // receive signals from them.
221 // |service_name| looks like "org.freedesktop.NetworkManager", and
222 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
224 // Must be called in the origin thread.
225 virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
226 const ObjectPath& object_path);
228 // Same as above, but also takes a bitfield of ObjectProxy::Options.
229 // See object_proxy.h for available options.
230 virtual ObjectProxy* GetObjectProxyWithOptions(
231 const std::string& service_name,
232 const ObjectPath& object_path,
233 int options);
235 // Removes the previously created object proxy for the given service
236 // name and the object path and releases its memory.
238 // If and object proxy for the given service name and object was
239 // created with GetObjectProxy, this function removes it from the
240 // bus object and detaches the ObjectProxy, invalidating any pointer
241 // previously acquired for it with GetObjectProxy. A subsequent call
242 // to GetObjectProxy will return a new object.
244 // All the object proxies are detached from remote objects at the
245 // shutdown time of the bus, but they can be detached early to reduce
246 // memory footprint and used match rules for the bus connection.
248 // |service_name| looks like "org.freedesktop.NetworkManager", and
249 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
250 // |callback| is called when the object proxy is successfully removed and
251 // detached.
253 // The function returns true when there is an object proxy matching the
254 // |service_name| and |object_path| to remove, and calls |callback| when it
255 // is removed. Otherwise, it returns false and the |callback| function is
256 // never called. The |callback| argument must not be null.
258 // Must be called in the origin thread.
259 virtual bool RemoveObjectProxy(const std::string& service_name,
260 const ObjectPath& object_path,
261 const base::Closure& callback);
263 // Same as above, but also takes a bitfield of ObjectProxy::Options.
264 // See object_proxy.h for available options.
265 virtual bool RemoveObjectProxyWithOptions(
266 const std::string& service_name,
267 const ObjectPath& object_path,
268 int options,
269 const base::Closure& callback);
271 // Gets the exported object for the given object path.
272 // The caller must not delete the returned object.
274 // Returns an existing exported object if the bus object already owns
275 // the exported object for the given object path. Never returns NULL.
277 // The bus will own all exported objects created by the bus, to ensure
278 // that the exported objects are unregistered at the shutdown time of
279 // the bus.
281 // The exported object is used to export methods of local objects, and
282 // send signal from them.
284 // Must be called in the origin thread.
285 virtual ExportedObject* GetExportedObject(const ObjectPath& object_path);
287 // Unregisters the exported object for the given object path |object_path|.
289 // Getting an exported object for the same object path after this call
290 // will return a new object, method calls on any remaining copies of the
291 // previous object will not be called.
293 // Must be called in the origin thread.
294 virtual void UnregisterExportedObject(const ObjectPath& object_path);
296 // Shuts down the bus and blocks until it's done. More specifically, this
297 // function does the following:
299 // - Unregisters the object paths
300 // - Releases the service names
301 // - Closes the connection to dbus-daemon.
303 // BLOCKING CALL.
304 virtual void ShutdownAndBlock();
306 // Similar to ShutdownAndBlock(), but this function is used to
307 // synchronously shut down the bus that uses the D-Bus thread. This
308 // function is intended to be used at the very end of the browser
309 // shutdown, where it makes more sense to shut down the bus
310 // synchronously, than trying to make it asynchronous.
312 // BLOCKING CALL, but must be called in the origin thread.
313 virtual void ShutdownOnDBusThreadAndBlock();
315 // Returns true if the shutdown has been completed.
316 bool shutdown_completed() { return shutdown_completed_; }
319 // The public functions below are not intended to be used in client
320 // code. These are used to implement ObjectProxy and ExportedObject.
323 // Connects the bus to the dbus-daemon.
324 // Returns true on success, or the bus is already connected.
326 // BLOCKING CALL.
327 virtual bool Connect();
329 // Requests the ownership of the service name given by |service_name|.
330 // See also RequestOwnershipAndBlock().
332 // |on_ownership_callback| is called when the service name is obtained
333 // or failed to be obtained, in the origin thread.
335 // Must be called in the origin thread.
336 virtual void RequestOwnership(const std::string& service_name,
337 OnOwnershipCallback on_ownership_callback);
339 // Requests the ownership of the given service name.
340 // Returns true on success, or the the service name is already obtained.
342 // BLOCKING CALL.
343 virtual bool RequestOwnershipAndBlock(const std::string& service_name);
345 // Releases the ownership of the given service name.
346 // Returns true on success.
348 // BLOCKING CALL.
349 virtual bool ReleaseOwnership(const std::string& service_name);
351 // Sets up async operations.
352 // Returns true on success, or it's already set up.
353 // This function needs to be called before starting async operations.
355 // BLOCKING CALL.
356 virtual bool SetUpAsyncOperations();
358 // Sends a message to the bus and blocks until the response is
359 // received. Used to implement synchronous method calls.
361 // BLOCKING CALL.
362 virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
363 int timeout_ms,
364 DBusError* error);
366 // Requests to send a message to the bus. The reply is handled with
367 // |pending_call| at a later time.
369 // BLOCKING CALL.
370 virtual void SendWithReply(DBusMessage* request,
371 DBusPendingCall** pending_call,
372 int timeout_ms);
374 // Requests to send a message to the bus. The message serial number will
375 // be stored in |serial|.
377 // BLOCKING CALL.
378 virtual void Send(DBusMessage* request, uint32* serial);
380 // Adds the message filter function. |filter_function| will be called
381 // when incoming messages are received. Returns true on success.
383 // When a new incoming message arrives, filter functions are called in
384 // the order that they were added until the the incoming message is
385 // handled by a filter function.
387 // The same filter function associated with the same user data cannot be
388 // added more than once. Returns false for this case.
390 // BLOCKING CALL.
391 virtual bool AddFilterFunction(DBusHandleMessageFunction filter_function,
392 void* user_data);
394 // Removes the message filter previously added by AddFilterFunction().
395 // Returns true on success.
397 // BLOCKING CALL.
398 virtual bool RemoveFilterFunction(DBusHandleMessageFunction filter_function,
399 void* user_data);
401 // Adds the match rule. Messages that match the rule will be processed
402 // by the filter functions added by AddFilterFunction().
404 // You cannot specify which filter function to use for a match rule.
405 // Instead, you should check if an incoming message is what you are
406 // interested in, in the filter functions.
408 // The same match rule can be added more than once and should be removed
409 // as many times as it was added.
411 // The match rule looks like:
412 // "type='signal', interface='org.chromium.SomeInterface'".
414 // See "Message Bus Message Routing" section in the D-Bus specification
415 // for details about match rules:
416 // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
418 // BLOCKING CALL.
419 virtual void AddMatch(const std::string& match_rule, DBusError* error);
421 // Removes the match rule previously added by AddMatch().
422 // Returns false if the requested match rule is unknown or has already been
423 // removed. Otherwise, returns true and sets |error| accordingly.
425 // BLOCKING CALL.
426 virtual bool RemoveMatch(const std::string& match_rule, DBusError* error);
428 // Tries to register the object path. Returns true on success.
429 // Returns false if the object path is already registered.
431 // |message_function| in |vtable| will be called every time when a new
432 // |message sent to the object path arrives.
434 // The same object path must not be added more than once.
436 // See also documentation of |dbus_connection_try_register_object_path| at
437 // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
439 // BLOCKING CALL.
440 virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
441 const DBusObjectPathVTable* vtable,
442 void* user_data,
443 DBusError* error);
445 // Unregister the object path.
447 // BLOCKING CALL.
448 virtual void UnregisterObjectPath(const ObjectPath& object_path);
450 // Posts the task to the message loop of the thread that created the bus.
451 virtual void PostTaskToOriginThread(
452 const tracked_objects::Location& from_here,
453 const base::Closure& task);
455 // Posts the task to the message loop of the D-Bus thread. If D-Bus
456 // thread is not supplied, the message loop of the origin thread will be
457 // used.
458 virtual void PostTaskToDBusThread(
459 const tracked_objects::Location& from_here,
460 const base::Closure& task);
462 // Posts the delayed task to the message loop of the D-Bus thread. If
463 // D-Bus thread is not supplied, the message loop of the origin thread
464 // will be used.
465 virtual void PostDelayedTaskToDBusThread(
466 const tracked_objects::Location& from_here,
467 const base::Closure& task,
468 base::TimeDelta delay);
470 // Returns true if the bus has the D-Bus thread.
471 virtual bool HasDBusThread();
473 // Check whether the current thread is on the origin thread (the thread
474 // that created the bus). If not, DCHECK will fail.
475 virtual void AssertOnOriginThread();
477 // Check whether the current thread is on the D-Bus thread. If not,
478 // DCHECK will fail. If the D-Bus thread is not supplied, it calls
479 // AssertOnOriginThread().
480 virtual void AssertOnDBusThread();
482 // Returns true if the bus is connected to D-Bus.
483 bool is_connected() { return connection_ != NULL; }
485 protected:
486 // This is protected, so we can define sub classes.
487 virtual ~Bus();
489 private:
490 friend class base::RefCountedThreadSafe<Bus>;
492 // Helper function used for RemoveObjectProxy().
493 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
494 const base::Closure& callback);
496 // Helper function used for UnregisterExportedObject().
497 void UnregisterExportedObjectInternal(
498 scoped_refptr<dbus::ExportedObject> exported_object);
500 // Helper function used for ShutdownOnDBusThreadAndBlock().
501 void ShutdownOnDBusThreadAndBlockInternal();
503 // Helper function used for RequestOwnership().
504 void RequestOwnershipInternal(const std::string& service_name,
505 OnOwnershipCallback on_ownership_callback);
507 // Processes the all incoming data to the connection, if any.
509 // BLOCKING CALL.
510 void ProcessAllIncomingDataIfAny();
512 // Called when a watch object is added. Used to start monitoring the
513 // file descriptor used for D-Bus communication.
514 dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
516 // Called when a watch object is removed.
517 void OnRemoveWatch(DBusWatch* raw_watch);
519 // Called when the "enabled" status of |raw_watch| is toggled.
520 void OnToggleWatch(DBusWatch* raw_watch);
522 // Called when a timeout object is added. Used to start monitoring
523 // timeout for method calls.
524 dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
526 // Called when a timeout object is removed.
527 void OnRemoveTimeout(DBusTimeout* raw_timeout);
529 // Called when the "enabled" status of |raw_timeout| is toggled.
530 void OnToggleTimeout(DBusTimeout* raw_timeout);
532 // Called when the dispatch status (i.e. if any incoming data is
533 // available) is changed.
534 void OnDispatchStatusChanged(DBusConnection* connection,
535 DBusDispatchStatus status);
537 // Callback helper functions. Redirects to the corresponding member function.
538 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
539 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
540 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
541 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
542 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
543 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
544 static void OnDispatchStatusChangedThunk(DBusConnection* connection,
545 DBusDispatchStatus status,
546 void* data);
547 const BusType bus_type_;
548 const ConnectionType connection_type_;
549 scoped_refptr<base::MessageLoopProxy> dbus_thread_message_loop_proxy_;
550 base::WaitableEvent on_shutdown_;
551 DBusConnection* connection_;
553 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_;
554 base::PlatformThreadId origin_thread_id_;
556 std::set<std::string> owned_service_names_;
557 // The following sets are used to check if rules/object_paths/filters
558 // are properly cleaned up before destruction of the bus object.
559 // Since it's not an error to add the same match rule twice, the repeated
560 // match rules are counted in a map.
561 std::map<std::string, int> match_rules_added_;
562 std::set<ObjectPath> registered_object_paths_;
563 std::set<std::pair<DBusHandleMessageFunction, void*> >
564 filter_functions_added_;
566 // ObjectProxyTable is used to hold the object proxies created by the
567 // bus object. Key is a pair; the first part is a concatenated string of
568 // service name + object path, like
569 // "org.chromium.TestService/org/chromium/TestObject".
570 // The second part is the ObjectProxy::Options for the proxy.
571 typedef std::map<std::pair<std::string, int>,
572 scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
573 ObjectProxyTable object_proxy_table_;
575 // ExportedObjectTable is used to hold the exported objects created by
576 // the bus object. Key is a concatenated string of service name +
577 // object path, like "org.chromium.TestService/org/chromium/TestObject".
578 typedef std::map<const dbus::ObjectPath,
579 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
580 ExportedObjectTable exported_object_table_;
582 bool async_operations_set_up_;
583 bool shutdown_completed_;
585 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
586 // OnAddTimeout()/OnRemoveTimeou() are balanced.
587 int num_pending_watches_;
588 int num_pending_timeouts_;
590 std::string address_;
592 DISALLOW_COPY_AND_ASSIGN(Bus);
595 } // namespace dbus
597 #endif // DBUS_BUS_H_