Simulate a mouse click.
[chromium-blink-merge.git] / dbus / bus.h
blob2e6db70174c6fade7a56a0057d6eff24f1ab6990
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>
14 #include <vector>
16 #include "base/callback.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/synchronization/waitable_event.h"
19 #include "base/threading/platform_thread.h"
20 #include "dbus/dbus_export.h"
21 #include "dbus/object_path.h"
23 namespace base {
24 class SequencedTaskRunner;
25 class SingleThreadTaskRunner;
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 // Specifies whether the GetServiceOwnerAndBlock call should report or
165 // suppress errors.
166 enum GetServiceOwnerOption {
167 REPORT_ERRORS,
168 SUPPRESS_ERRORS,
171 // Specifies service ownership options.
173 // REQUIRE_PRIMARY indicates that you require primary ownership of the
174 // service name.
176 // ALLOW_REPLACEMENT indicates that you'll allow another connection to
177 // steal ownership of this service name from you.
179 // REQUIRE_PRIMARY_ALLOW_REPLACEMENT does the obvious.
180 enum ServiceOwnershipOptions {
181 REQUIRE_PRIMARY = (DBUS_NAME_FLAG_DO_NOT_QUEUE |
182 DBUS_NAME_FLAG_REPLACE_EXISTING),
183 REQUIRE_PRIMARY_ALLOW_REPLACEMENT = (REQUIRE_PRIMARY |
184 DBUS_NAME_FLAG_ALLOW_REPLACEMENT),
187 // Options used to create a Bus object.
188 struct CHROME_DBUS_EXPORT Options {
189 Options();
190 ~Options();
192 BusType bus_type; // SESSION by default.
193 ConnectionType connection_type; // PRIVATE by default.
194 // If dbus_task_runner is set, the bus object will use that
195 // task runner to process asynchronous operations.
197 // The thread servicing the task runner should meet the following
198 // requirements:
199 // 1) Already running.
200 // 2) Has a MessageLoopForIO.
201 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
203 // Specifies the server addresses to be connected. If you want to
204 // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to
205 // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to
206 // connect to. The format of this address value is the dbus address style
207 // which is described in
208 // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
210 // EXAMPLE USAGE:
211 // dbus::Bus::Options options;
212 // options.bus_type = CUSTOM_ADDRESS;
213 // options.address.assign("unix:path=/tmp/dbus-XXXXXXX");
214 // // Set up other options
215 // dbus::Bus bus(options);
217 // // Do something.
219 std::string address;
221 // If the connection with dbus-daemon is closed, |disconnected_callback|
222 // will be called on the origin thread. This is also called when the
223 // disonnection by ShutdownAndBlock. |disconnected_callback| can be null
224 // callback
225 base::Closure disconnected_callback;
228 // Creates a Bus object. The actual connection will be established when
229 // Connect() is called.
230 explicit Bus(const Options& options);
232 // Called when an ownership request is complete.
233 // Parameters:
234 // - the requested service name.
235 // - whether ownership has been obtained or not.
236 typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback;
238 // Called when GetServiceOwner() completes.
239 // |service_owner| is the return value from GetServiceOwnerAndBlock().
240 typedef base::Callback<void (const std::string& service_owner)>
241 GetServiceOwnerCallback;
243 // TODO(satorux): Remove the service name parameter as the caller of
244 // RequestOwnership() knows the service name.
246 // Gets the object proxy for the given service name and the object path.
247 // The caller must not delete the returned object.
249 // Returns an existing object proxy if the bus object already owns the
250 // object proxy for the given service name and the object path.
251 // Never returns NULL.
253 // The bus will own all object proxies created by the bus, to ensure
254 // that the object proxies are detached from remote objects at the
255 // shutdown time of the bus.
257 // The object proxy is used to call methods of remote objects, and
258 // receive signals from them.
260 // |service_name| looks like "org.freedesktop.NetworkManager", and
261 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
263 // Must be called in the origin thread.
264 virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
265 const ObjectPath& object_path);
267 // Same as above, but also takes a bitfield of ObjectProxy::Options.
268 // See object_proxy.h for available options.
269 virtual ObjectProxy* GetObjectProxyWithOptions(
270 const std::string& service_name,
271 const ObjectPath& object_path,
272 int options);
274 // Removes the previously created object proxy for the given service
275 // name and the object path and releases its memory.
277 // If and object proxy for the given service name and object was
278 // created with GetObjectProxy, this function removes it from the
279 // bus object and detaches the ObjectProxy, invalidating any pointer
280 // previously acquired for it with GetObjectProxy. A subsequent call
281 // to GetObjectProxy will return a new object.
283 // All the object proxies are detached from remote objects at the
284 // shutdown time of the bus, but they can be detached early to reduce
285 // memory footprint and used match rules for the bus connection.
287 // |service_name| looks like "org.freedesktop.NetworkManager", and
288 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
289 // |callback| is called when the object proxy is successfully removed and
290 // detached.
292 // The function returns true when there is an object proxy matching the
293 // |service_name| and |object_path| to remove, and calls |callback| when it
294 // is removed. Otherwise, it returns false and the |callback| function is
295 // never called. The |callback| argument must not be null.
297 // Must be called in the origin thread.
298 virtual bool RemoveObjectProxy(const std::string& service_name,
299 const ObjectPath& object_path,
300 const base::Closure& callback);
302 // Same as above, but also takes a bitfield of ObjectProxy::Options.
303 // See object_proxy.h for available options.
304 virtual bool RemoveObjectProxyWithOptions(
305 const std::string& service_name,
306 const ObjectPath& object_path,
307 int options,
308 const base::Closure& callback);
310 // Gets the exported object for the given object path.
311 // The caller must not delete the returned object.
313 // Returns an existing exported object if the bus object already owns
314 // the exported object for the given object path. Never returns NULL.
316 // The bus will own all exported objects created by the bus, to ensure
317 // that the exported objects are unregistered at the shutdown time of
318 // the bus.
320 // The exported object is used to export methods of local objects, and
321 // send signal from them.
323 // Must be called in the origin thread.
324 virtual ExportedObject* GetExportedObject(const ObjectPath& object_path);
326 // Unregisters the exported object for the given object path |object_path|.
328 // Getting an exported object for the same object path after this call
329 // will return a new object, method calls on any remaining copies of the
330 // previous object will not be called.
332 // Must be called in the origin thread.
333 virtual void UnregisterExportedObject(const ObjectPath& object_path);
336 // Gets an object manager for the given remote object path |object_path|
337 // exported by the service |service_name|.
339 // Returns an existing object manager if the bus object already owns a
340 // matching object manager, never returns NULL.
342 // The caller must not delete the returned object, the bus retains ownership
343 // of all object managers.
345 // Must be called in the origin thread.
346 virtual ObjectManager* GetObjectManager(const std::string& service_name,
347 const ObjectPath& object_path);
349 // Unregisters the object manager for the given remote object path
350 // |object_path| exported by the srevice |service_name|.
352 // Getting an object manager for the same remote object after this call
353 // will return a new object, method calls on any remaining copies of the
354 // previous object are not permitted.
356 // Must be called in the origin thread.
357 virtual void RemoveObjectManager(const std::string& service_name,
358 const ObjectPath& object_path);
360 // Instructs all registered object managers to retrieve their set of managed
361 // objects from their respective remote objects. There is no need to call this
362 // manually, this is called automatically by the D-Bus thread manager once
363 // implementation classes are registered.
364 virtual void GetManagedObjects();
366 // Shuts down the bus and blocks until it's done. More specifically, this
367 // function does the following:
369 // - Unregisters the object paths
370 // - Releases the service names
371 // - Closes the connection to dbus-daemon.
373 // This function can be called multiple times and it is no-op for the 2nd time
374 // calling.
376 // BLOCKING CALL.
377 virtual void ShutdownAndBlock();
379 // Similar to ShutdownAndBlock(), but this function is used to
380 // synchronously shut down the bus that uses the D-Bus thread. This
381 // function is intended to be used at the very end of the browser
382 // shutdown, where it makes more sense to shut down the bus
383 // synchronously, than trying to make it asynchronous.
385 // BLOCKING CALL, but must be called in the origin thread.
386 virtual void ShutdownOnDBusThreadAndBlock();
388 // Returns true if the shutdown has been completed.
389 bool shutdown_completed() { return shutdown_completed_; }
392 // The public functions below are not intended to be used in client
393 // code. These are used to implement ObjectProxy and ExportedObject.
396 // Connects the bus to the dbus-daemon.
397 // Returns true on success, or the bus is already connected.
399 // BLOCKING CALL.
400 virtual bool Connect();
402 // Disconnects the bus from the dbus-daemon.
403 // Safe to call multiple times and no operation after the first call.
404 // Do not call for shared connection it will be released by libdbus.
406 // BLOCKING CALL.
407 virtual void ClosePrivateConnection();
409 // Requests the ownership of the service name given by |service_name|.
410 // See also RequestOwnershipAndBlock().
412 // |on_ownership_callback| is called when the service name is obtained
413 // or failed to be obtained, in the origin thread.
415 // Must be called in the origin thread.
416 virtual void RequestOwnership(const std::string& service_name,
417 ServiceOwnershipOptions options,
418 OnOwnershipCallback on_ownership_callback);
420 // Requests the ownership of the given service name.
421 // Returns true on success, or the the service name is already obtained.
423 // BLOCKING CALL.
424 virtual bool RequestOwnershipAndBlock(const std::string& service_name,
425 ServiceOwnershipOptions options);
427 // Releases the ownership of the given service name.
428 // Returns true on success.
430 // BLOCKING CALL.
431 virtual bool ReleaseOwnership(const std::string& service_name);
433 // Sets up async operations.
434 // Returns true on success, or it's already set up.
435 // This function needs to be called before starting async operations.
437 // BLOCKING CALL.
438 virtual bool SetUpAsyncOperations();
440 // Sends a message to the bus and blocks until the response is
441 // received. Used to implement synchronous method calls.
443 // BLOCKING CALL.
444 virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
445 int timeout_ms,
446 DBusError* error);
448 // Requests to send a message to the bus. The reply is handled with
449 // |pending_call| at a later time.
451 // BLOCKING CALL.
452 virtual void SendWithReply(DBusMessage* request,
453 DBusPendingCall** pending_call,
454 int timeout_ms);
456 // Requests to send a message to the bus. The message serial number will
457 // be stored in |serial|.
459 // BLOCKING CALL.
460 virtual void Send(DBusMessage* request, uint32* serial);
462 // Adds the message filter function. |filter_function| will be called
463 // when incoming messages are received. Returns true on success.
465 // When a new incoming message arrives, filter functions are called in
466 // the order that they were added until the the incoming message is
467 // handled by a filter function.
469 // The same filter function associated with the same user data cannot be
470 // added more than once. Returns false for this case.
472 // BLOCKING CALL.
473 virtual bool AddFilterFunction(DBusHandleMessageFunction filter_function,
474 void* user_data);
476 // Removes the message filter previously added by AddFilterFunction().
477 // Returns true on success.
479 // BLOCKING CALL.
480 virtual bool RemoveFilterFunction(DBusHandleMessageFunction filter_function,
481 void* user_data);
483 // Adds the match rule. Messages that match the rule will be processed
484 // by the filter functions added by AddFilterFunction().
486 // You cannot specify which filter function to use for a match rule.
487 // Instead, you should check if an incoming message is what you are
488 // interested in, in the filter functions.
490 // The same match rule can be added more than once and should be removed
491 // as many times as it was added.
493 // The match rule looks like:
494 // "type='signal', interface='org.chromium.SomeInterface'".
496 // See "Message Bus Message Routing" section in the D-Bus specification
497 // for details about match rules:
498 // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
500 // BLOCKING CALL.
501 virtual void AddMatch(const std::string& match_rule, DBusError* error);
503 // Removes the match rule previously added by AddMatch().
504 // Returns false if the requested match rule is unknown or has already been
505 // removed. Otherwise, returns true and sets |error| accordingly.
507 // BLOCKING CALL.
508 virtual bool RemoveMatch(const std::string& match_rule, DBusError* error);
510 // Tries to register the object path. Returns true on success.
511 // Returns false if the object path is already registered.
513 // |message_function| in |vtable| will be called every time when a new
514 // |message sent to the object path arrives.
516 // The same object path must not be added more than once.
518 // See also documentation of |dbus_connection_try_register_object_path| at
519 // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
521 // BLOCKING CALL.
522 virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
523 const DBusObjectPathVTable* vtable,
524 void* user_data,
525 DBusError* error);
527 // Unregister the object path.
529 // BLOCKING CALL.
530 virtual void UnregisterObjectPath(const ObjectPath& object_path);
532 // Posts |task| to the task runner of the D-Bus thread. On completion, |reply|
533 // is posted to the origin thread.
534 virtual void PostTaskToDBusThreadAndReply(
535 const tracked_objects::Location& from_here,
536 const base::Closure& task,
537 const base::Closure& reply);
539 // Posts the task to the task runner of the thread that created the bus.
540 virtual void PostTaskToOriginThread(
541 const tracked_objects::Location& from_here,
542 const base::Closure& task);
544 // Posts the task to the task runner of the D-Bus thread. If D-Bus
545 // thread is not supplied, the task runner of the origin thread will be
546 // used.
547 virtual void PostTaskToDBusThread(
548 const tracked_objects::Location& from_here,
549 const base::Closure& task);
551 // Posts the delayed task to the task runner of the D-Bus thread. If
552 // D-Bus thread is not supplied, the task runner of the origin thread
553 // will be used.
554 virtual void PostDelayedTaskToDBusThread(
555 const tracked_objects::Location& from_here,
556 const base::Closure& task,
557 base::TimeDelta delay);
559 // Returns true if the bus has the D-Bus thread.
560 virtual bool HasDBusThread();
562 // Check whether the current thread is on the origin thread (the thread
563 // that created the bus). If not, DCHECK will fail.
564 virtual void AssertOnOriginThread();
566 // Check whether the current thread is on the D-Bus thread. If not,
567 // DCHECK will fail. If the D-Bus thread is not supplied, it calls
568 // AssertOnOriginThread().
569 virtual void AssertOnDBusThread();
571 // Gets the owner for |service_name| via org.freedesktop.DBus.GetNameOwner.
572 // Returns the owner name, if any, or an empty string on failure.
573 // |options| specifies where to printing error messages or not.
575 // BLOCKING CALL.
576 virtual std::string GetServiceOwnerAndBlock(const std::string& service_name,
577 GetServiceOwnerOption options);
579 // A non-blocking version of GetServiceOwnerAndBlock().
580 // Must be called in the origin thread.
581 virtual void GetServiceOwner(const std::string& service_name,
582 const GetServiceOwnerCallback& callback);
584 // Whenever the owner for |service_name| changes, run |callback| with the
585 // name of the new owner. If the owner goes away, then |callback| receives
586 // an empty string.
588 // Any unique (service_name, callback) can be used. Duplicate are ignored.
589 // |service_name| must not be empty and |callback| must not be null.
591 // Must be called in the origin thread.
592 virtual void ListenForServiceOwnerChange(
593 const std::string& service_name,
594 const GetServiceOwnerCallback& callback);
596 // Stop listening for |service_name| owner changes for |callback|.
597 // Any unique (service_name, callback) can be used. Non-registered callbacks
598 // for a given service name are ignored.
599 // |service_name| must not be empty and |callback| must not be null.
601 // Must be called in the origin thread.
602 virtual void UnlistenForServiceOwnerChange(
603 const std::string& service_name,
604 const GetServiceOwnerCallback& callback);
606 // Returns true if the bus is connected to D-Bus.
607 bool is_connected() { return connection_ != NULL; }
609 protected:
610 // This is protected, so we can define sub classes.
611 virtual ~Bus();
613 private:
614 friend class base::RefCountedThreadSafe<Bus>;
616 // Helper function used for RemoveObjectProxy().
617 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
618 const base::Closure& callback);
620 // Helper function used for UnregisterExportedObject().
621 void UnregisterExportedObjectInternal(
622 scoped_refptr<dbus::ExportedObject> exported_object);
624 // Helper function used for ShutdownOnDBusThreadAndBlock().
625 void ShutdownOnDBusThreadAndBlockInternal();
627 // Helper function used for RequestOwnership().
628 void RequestOwnershipInternal(const std::string& service_name,
629 ServiceOwnershipOptions options,
630 OnOwnershipCallback on_ownership_callback);
632 // Helper function used for GetServiceOwner().
633 void GetServiceOwnerInternal(const std::string& service_name,
634 const GetServiceOwnerCallback& callback);
636 // Helper function used for ListenForServiceOwnerChange().
637 void ListenForServiceOwnerChangeInternal(
638 const std::string& service_name,
639 const GetServiceOwnerCallback& callback);
641 // Helper function used for UnListenForServiceOwnerChange().
642 void UnlistenForServiceOwnerChangeInternal(
643 const std::string& service_name,
644 const GetServiceOwnerCallback& callback);
646 // Processes the all incoming data to the connection, if any.
648 // BLOCKING CALL.
649 void ProcessAllIncomingDataIfAny();
651 // Called when a watch object is added. Used to start monitoring the
652 // file descriptor used for D-Bus communication.
653 dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
655 // Called when a watch object is removed.
656 void OnRemoveWatch(DBusWatch* raw_watch);
658 // Called when the "enabled" status of |raw_watch| is toggled.
659 void OnToggleWatch(DBusWatch* raw_watch);
661 // Called when a timeout object is added. Used to start monitoring
662 // timeout for method calls.
663 dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
665 // Called when a timeout object is removed.
666 void OnRemoveTimeout(DBusTimeout* raw_timeout);
668 // Called when the "enabled" status of |raw_timeout| is toggled.
669 void OnToggleTimeout(DBusTimeout* raw_timeout);
671 // Called when the dispatch status (i.e. if any incoming data is
672 // available) is changed.
673 void OnDispatchStatusChanged(DBusConnection* connection,
674 DBusDispatchStatus status);
676 // Called when the connection is diconnected.
677 void OnConnectionDisconnected(DBusConnection* connection);
679 // Called when a service owner change occurs.
680 void OnServiceOwnerChanged(DBusMessage* message);
682 // Callback helper functions. Redirects to the corresponding member function.
683 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
684 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
685 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
686 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
687 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
688 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
689 static void OnDispatchStatusChangedThunk(DBusConnection* connection,
690 DBusDispatchStatus status,
691 void* data);
693 // Calls OnConnectionDisconnected if the Disconnected signal is received.
694 static DBusHandlerResult OnConnectionDisconnectedFilter(
695 DBusConnection* connection,
696 DBusMessage* message,
697 void* user_data);
699 // Calls OnServiceOwnerChanged for a NameOwnerChanged signal.
700 static DBusHandlerResult OnServiceOwnerChangedFilter(
701 DBusConnection* connection,
702 DBusMessage* message,
703 void* user_data);
705 const BusType bus_type_;
706 const ConnectionType connection_type_;
707 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
708 base::WaitableEvent on_shutdown_;
709 DBusConnection* connection_;
711 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
712 base::PlatformThreadId origin_thread_id_;
714 std::set<std::string> owned_service_names_;
715 // The following sets are used to check if rules/object_paths/filters
716 // are properly cleaned up before destruction of the bus object.
717 // Since it's not an error to add the same match rule twice, the repeated
718 // match rules are counted in a map.
719 std::map<std::string, int> match_rules_added_;
720 std::set<ObjectPath> registered_object_paths_;
721 std::set<std::pair<DBusHandleMessageFunction, void*> >
722 filter_functions_added_;
724 // ObjectProxyTable is used to hold the object proxies created by the
725 // bus object. Key is a pair; the first part is a concatenated string of
726 // service name + object path, like
727 // "org.chromium.TestService/org/chromium/TestObject".
728 // The second part is the ObjectProxy::Options for the proxy.
729 typedef std::map<std::pair<std::string, int>,
730 scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
731 ObjectProxyTable object_proxy_table_;
733 // ExportedObjectTable is used to hold the exported objects created by
734 // the bus object. Key is a concatenated string of service name +
735 // object path, like "org.chromium.TestService/org/chromium/TestObject".
736 typedef std::map<const dbus::ObjectPath,
737 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
738 ExportedObjectTable exported_object_table_;
740 // ObjectManagerTable is used to hold the object managers created by the
741 // bus object. Key is a concatenated string of service name + object path,
742 // like "org.chromium.TestService/org/chromium/TestObject".
743 typedef std::map<std::string,
744 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable;
745 ObjectManagerTable object_manager_table_;
747 // A map of NameOwnerChanged signals to listen for and the callbacks to run
748 // on the origin thread when the owner changes.
749 // Only accessed on the DBus thread.
750 // Key: Service name
751 // Value: Vector of callbacks. Unique and expected to be small. Not using
752 // std::set here because base::Callbacks don't have a '<' operator.
753 typedef std::map<std::string, std::vector<GetServiceOwnerCallback> >
754 ServiceOwnerChangedListenerMap;
755 ServiceOwnerChangedListenerMap service_owner_changed_listener_map_;
757 bool async_operations_set_up_;
758 bool shutdown_completed_;
760 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
761 // OnAddTimeout()/OnRemoveTimeou() are balanced.
762 int num_pending_watches_;
763 int num_pending_timeouts_;
765 std::string address_;
766 base::Closure on_disconnected_closure_;
768 DISALLOW_COPY_AND_ASSIGN(Bus);
771 } // namespace dbus
773 #endif // DBUS_BUS_H_