chromeos: bluetooth: add BluetoothNodeClient
[chromium-blink-merge.git] / dbus / bus.h
blobe045386cfce107136f4b6f2b11f508b986495a43
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_
7 #pragma once
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <utility>
13 #include <dbus/dbus.h>
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 "base/tracked_objects.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 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.
139 enum BusType {
140 SESSION = DBUS_BUS_SESSION,
141 SYSTEM = DBUS_BUS_SYSTEM,
144 // Specifies the connection type. PRIVATE should usually be used unless
145 // you are sure that SHARED is safe for you, which is unlikely the case
146 // in Chrome.
148 // PRIVATE gives you a private connection, that won't be shared with
149 // other Bus objects.
151 // SHARED gives you a connection shared among other Bus objects, which
152 // is unsafe if the connection is shared with multiple threads.
153 enum ConnectionType {
154 PRIVATE,
155 SHARED,
158 // Options used to create a Bus object.
159 struct Options {
160 Options();
161 ~Options();
163 BusType bus_type; // SESSION by default.
164 ConnectionType connection_type; // PRIVATE by default.
165 // If dbus_thread_message_loop_proxy is set, the bus object will use that
166 // message loop to process asynchronous operations.
168 // The thread servicing the message loop proxy should meet the following
169 // requirements:
170 // 1) Already running.
171 // 2) Has a MessageLoopForIO.
172 scoped_refptr<base::MessageLoopProxy> dbus_thread_message_loop_proxy;
175 // Creates a Bus object. The actual connection will be established when
176 // Connect() is called.
177 explicit Bus(const Options& options);
179 // Gets the object proxy for the given service name and the object path.
180 // The caller must not delete the returned object.
182 // Returns an existing object proxy if the bus object already owns the
183 // object proxy for the given service name and the object path.
184 // Never returns NULL.
186 // The bus will own all object proxies created by the bus, to ensure
187 // that the object proxies are detached from remote objects at the
188 // shutdown time of the bus.
190 // The object proxy is used to call methods of remote objects, and
191 // receive signals from them.
193 // |service_name| looks like "org.freedesktop.NetworkManager", and
194 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
196 // Must be called in the origin thread.
197 virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
198 const ObjectPath& object_path);
200 // Same as above, but also takes a bitfield of ObjectProxy::Options.
201 // See object_proxy.h for available options.
202 virtual ObjectProxy* GetObjectProxyWithOptions(
203 const std::string& service_name,
204 const ObjectPath& object_path,
205 int options);
207 // Gets the exported object for the given service name and the object
208 // path. The caller must not delete the returned object.
210 // Returns an existing exported object if the bus object already owns
211 // the exported object for the given service name and the object path.
212 // Never returns NULL.
214 // The bus will own all exported objects created by the bus, to ensure
215 // that the exported objects are unregistered at the shutdown time of
216 // the bus.
218 // The exported object is used to export methods of local objects, and
219 // send signal from them.
221 // Must be called in the origin thread.
222 virtual ExportedObject* GetExportedObject(const std::string& service_name,
223 const ObjectPath& object_path);
225 // Shuts down the bus and blocks until it's done. More specifically, this
226 // function does the following:
228 // - Unregisters the object paths
229 // - Releases the service names
230 // - Closes the connection to dbus-daemon.
232 // BLOCKING CALL.
233 virtual void ShutdownAndBlock();
235 // Similar to ShutdownAndBlock(), but this function is used to
236 // synchronously shut down the bus that uses the D-Bus thread. This
237 // function is intended to be used at the very end of the browser
238 // shutdown, where it makes more sense to shut down the bus
239 // synchronously, than trying to make it asynchronous.
241 // BLOCKING CALL, but must be called in the origin thread.
242 virtual void ShutdownOnDBusThreadAndBlock();
244 // Returns true if the shutdown has been completed.
245 bool shutdown_completed() { return shutdown_completed_; }
248 // The public functions below are not intended to be used in client
249 // code. These are used to implement ObjectProxy and ExportedObject.
252 // Connects the bus to the dbus-daemon.
253 // Returns true on success, or the bus is already connected.
255 // BLOCKING CALL.
256 virtual bool Connect();
258 // Requests the ownership of the given service name.
259 // Returns true on success, or the the service name is already obtained.
261 // BLOCKING CALL.
262 virtual bool RequestOwnership(const std::string& service_name);
264 // Releases the ownership of the given service name.
265 // Returns true on success.
267 // BLOCKING CALL.
268 virtual bool ReleaseOwnership(const std::string& service_name);
270 // Sets up async operations.
271 // Returns true on success, or it's already set up.
272 // This function needs to be called before starting async operations.
274 // BLOCKING CALL.
275 virtual bool SetUpAsyncOperations();
277 // Sends a message to the bus and blocks until the response is
278 // received. Used to implement synchronous method calls.
280 // BLOCKING CALL.
281 virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
282 int timeout_ms,
283 DBusError* error);
285 // Requests to send a message to the bus. The reply is handled with
286 // |pending_call| at a later time.
288 // BLOCKING CALL.
289 virtual void SendWithReply(DBusMessage* request,
290 DBusPendingCall** pending_call,
291 int timeout_ms);
293 // Requests to send a message to the bus. The message serial number will
294 // be stored in |serial|.
296 // BLOCKING CALL.
297 virtual void Send(DBusMessage* request, uint32* serial);
299 // Adds the message filter function. |filter_function| will be called
300 // when incoming messages are received. Returns true on success.
302 // When a new incoming message arrives, filter functions are called in
303 // the order that they were added until the the incoming message is
304 // handled by a filter function.
306 // The same filter function associated with the same user data cannot be
307 // added more than once. Returns false for this case.
309 // BLOCKING CALL.
310 virtual bool AddFilterFunction(DBusHandleMessageFunction filter_function,
311 void* user_data);
313 // Removes the message filter previously added by AddFilterFunction().
314 // Returns true on success.
316 // BLOCKING CALL.
317 virtual bool RemoveFilterFunction(DBusHandleMessageFunction filter_function,
318 void* user_data);
320 // Adds the match rule. Messages that match the rule will be processed
321 // by the filter functions added by AddFilterFunction().
323 // You cannot specify which filter function to use for a match rule.
324 // Instead, you should check if an incoming message is what you are
325 // interested in, in the filter functions.
327 // The same match rule can be added more than once, but ignored from the
328 // second time.
330 // The match rule looks like:
331 // "type='signal', interface='org.chromium.SomeInterface'".
333 // See "Message Bus Message Routing" section in the D-Bus specification
334 // for details about match rules:
335 // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
337 // BLOCKING CALL.
338 virtual void AddMatch(const std::string& match_rule, DBusError* error);
340 // Removes the match rule previously added by AddMatch().
342 // BLOCKING CALL.
343 virtual void RemoveMatch(const std::string& match_rule, DBusError* error);
345 // Tries to register the object path. Returns true on success.
346 // Returns false if the object path is already registered.
348 // |message_function| in |vtable| will be called every time when a new
349 // |message sent to the object path arrives.
351 // The same object path must not be added more than once.
353 // See also documentation of |dbus_connection_try_register_object_path| at
354 // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
356 // BLOCKING CALL.
357 virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
358 const DBusObjectPathVTable* vtable,
359 void* user_data,
360 DBusError* error);
362 // Unregister the object path.
364 // BLOCKING CALL.
365 virtual void UnregisterObjectPath(const ObjectPath& object_path);
367 // Posts the task to the message loop of the thread that created the bus.
368 virtual void PostTaskToOriginThread(
369 const tracked_objects::Location& from_here,
370 const base::Closure& task);
372 // Posts the task to the message loop of the D-Bus thread. If D-Bus
373 // thread is not supplied, the message loop of the origin thread will be
374 // used.
375 virtual void PostTaskToDBusThread(
376 const tracked_objects::Location& from_here,
377 const base::Closure& task);
379 // Posts the delayed task to the message loop of the D-Bus thread. If
380 // D-Bus thread is not supplied, the message loop of the origin thread
381 // will be used.
382 virtual void PostDelayedTaskToDBusThread(
383 const tracked_objects::Location& from_here,
384 const base::Closure& task,
385 int delay_ms);
387 // Returns true if the bus has the D-Bus thread.
388 virtual bool HasDBusThread();
390 // Check whether the current thread is on the origin thread (the thread
391 // that created the bus). If not, DCHECK will fail.
392 virtual void AssertOnOriginThread();
394 // Check whether the current thread is on the D-Bus thread. If not,
395 // DCHECK will fail. If the D-Bus thread is not supplied, it calls
396 // AssertOnOriginThread().
397 virtual void AssertOnDBusThread();
399 // Returns true if the bus is connected to D-Bus.
400 bool is_connected() { return connection_ != NULL; }
402 protected:
403 // This is protected, so we can define sub classes.
404 virtual ~Bus();
406 private:
407 friend class base::RefCountedThreadSafe<Bus>;
409 // Helper function used for ShutdownOnDBusThreadAndBlock().
410 void ShutdownOnDBusThreadAndBlockInternal();
412 // Processes the all incoming data to the connection, if any.
414 // BLOCKING CALL.
415 void ProcessAllIncomingDataIfAny();
417 // Called when a watch object is added. Used to start monitoring the
418 // file descriptor used for D-Bus communication.
419 dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
421 // Called when a watch object is removed.
422 void OnRemoveWatch(DBusWatch* raw_watch);
424 // Called when the "enabled" status of |raw_watch| is toggled.
425 void OnToggleWatch(DBusWatch* raw_watch);
427 // Called when a timeout object is added. Used to start monitoring
428 // timeout for method calls.
429 dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
431 // Called when a timeout object is removed.
432 void OnRemoveTimeout(DBusTimeout* raw_timeout);
434 // Called when the "enabled" status of |raw_timeout| is toggled.
435 void OnToggleTimeout(DBusTimeout* raw_timeout);
437 // Called when the dispatch status (i.e. if any incoming data is
438 // available) is changed.
439 void OnDispatchStatusChanged(DBusConnection* connection,
440 DBusDispatchStatus status);
442 // Callback helper functions. Redirects to the corresponding member function.
443 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
444 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
445 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
446 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
447 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
448 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
449 static void OnDispatchStatusChangedThunk(DBusConnection* connection,
450 DBusDispatchStatus status,
451 void* data);
452 const BusType bus_type_;
453 const ConnectionType connection_type_;
454 scoped_refptr<base::MessageLoopProxy> dbus_thread_message_loop_proxy_;
455 base::WaitableEvent on_shutdown_;
456 DBusConnection* connection_;
458 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_;
459 base::PlatformThreadId origin_thread_id_;
461 std::set<std::string> owned_service_names_;
462 // The following sets are used to check if rules/object_paths/filters
463 // are properly cleaned up before destruction of the bus object.
464 std::set<std::string> match_rules_added_;
465 std::set<ObjectPath> registered_object_paths_;
466 std::set<std::pair<DBusHandleMessageFunction, void*> >
467 filter_functions_added_;
469 // ObjectProxyTable is used to hold the object proxies created by the
470 // bus object. Key is a pair; the first part is a concatenated string of
471 // service name + object path, like
472 // "org.chromium.TestService/org/chromium/TestObject".
473 // The second part is the ObjectProxy::Options for the proxy.
474 typedef std::map<std::pair<std::string, int>,
475 scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
476 ObjectProxyTable object_proxy_table_;
478 // ExportedObjectTable is used to hold the exported objects created by
479 // the bus object. Key is a concatenated string of service name +
480 // object path, like "org.chromium.TestService/org/chromium/TestObject".
481 typedef std::map<std::string,
482 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
483 ExportedObjectTable exported_object_table_;
485 bool async_operations_set_up_;
486 bool shutdown_completed_;
488 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
489 // OnAddTimeout()/OnRemoveTimeou() are balanced.
490 int num_pending_watches_;
491 int num_pending_timeouts_;
493 DISALLOW_COPY_AND_ASSIGN(Bus);
496 } // namespace dbus
498 #endif // DBUS_BUS_H_