Home | History | Annotate | Download | only in dbus
      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.
      4 
      5 #ifndef DBUS_BUS_H_
      6 #define DBUS_BUS_H_
      7 
      8 #include <dbus/dbus.h>
      9 
     10 #include <map>
     11 #include <set>
     12 #include <string>
     13 #include <utility>
     14 #include <vector>
     15 
     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"
     22 
     23 namespace base {
     24 class SequencedTaskRunner;
     25 class SingleThreadTaskRunner;
     26 class TaskRunner;
     27 }
     28 
     29 namespace tracked_objects {
     30 class Location;
     31 }
     32 
     33 namespace dbus {
     34 
     35 class ExportedObject;
     36 class ObjectManager;
     37 class ObjectProxy;
     38 
     39 // Bus is used to establish a connection with D-Bus, create object
     40 // proxies, and export objects.
     41 //
     42 // For asynchronous operations such as an asynchronous method call, the
     43 // bus object will use a task runner to monitor the underlying file
     44 // descriptor used for D-Bus communication. By default, the bus will use
     45 // the current thread's task runner. If |dbus_task_runner| option is
     46 // specified, the bus will use that task runner instead.
     47 //
     48 // THREADING
     49 //
     50 // In the D-Bus library, we use the two threads:
     51 //
     52 // - The origin thread: the thread that created the Bus object.
     53 // - The D-Bus thread: the thread servicing |dbus_task_runner|.
     54 //
     55 // The origin thread is usually Chrome's UI thread. The D-Bus thread is
     56 // usually a dedicated thread for the D-Bus library.
     57 //
     58 // BLOCKING CALLS
     59 //
     60 // Functions that issue blocking calls are marked "BLOCKING CALL" and
     61 // these functions should be called in the D-Bus thread (if
     62 // supplied). AssertOnDBusThread() is placed in these functions.
     63 //
     64 // Note that it's hard to tell if a libdbus function is actually blocking
     65 // or not (ex. dbus_bus_request_name() internally calls
     66 // dbus_connection_send_with_reply_and_block(), which is a blocking
     67 // call). To err on the safe side, we consider all libdbus functions that
     68 // deal with the connection to dbus-daemon to be blocking.
     69 //
     70 // SHUTDOWN
     71 //
     72 // The Bus object must be shut down manually by ShutdownAndBlock() and
     73 // friends. We require the manual shutdown to make the operation explicit
     74 // rather than doing it silently in the destructor.
     75 //
     76 // EXAMPLE USAGE:
     77 //
     78 // Synchronous method call:
     79 //
     80 //   dbus::Bus::Options options;
     81 //   // Set up the bus options here.
     82 //   ...
     83 //   dbus::Bus bus(options);
     84 //
     85 //   dbus::ObjectProxy* object_proxy =
     86 //       bus.GetObjectProxy(service_name, object_path);
     87 //
     88 //   dbus::MethodCall method_call(interface_name, method_name);
     89 //   scoped_ptr<dbus::Response> response(
     90 //       object_proxy.CallMethodAndBlock(&method_call, timeout_ms));
     91 //   if (response.get() != NULL) {  // Success.
     92 //     ...
     93 //   }
     94 //
     95 // Asynchronous method call:
     96 //
     97 //   void OnResponse(dbus::Response* response) {
     98 //     // response is NULL if the method call failed.
     99 //     if (!response)
    100 //       return;
    101 //   }
    102 //
    103 //   ...
    104 //   object_proxy.CallMethod(&method_call, timeout_ms,
    105 //                           base::Bind(&OnResponse));
    106 //
    107 // Exporting a method:
    108 //
    109 //   void Echo(dbus::MethodCall* method_call,
    110 //             dbus::ExportedObject::ResponseSender response_sender) {
    111 //     // Do something with method_call.
    112 //     Response* response = Response::FromMethodCall(method_call);
    113 //     // Build response here.
    114 //     // Can send an immediate response here to implement a synchronous service
    115 //     // or store the response_sender and send a response later to implement an
    116 //     // asynchronous service.
    117 //     response_sender.Run(response);
    118 //   }
    119 //
    120 //   void OnExported(const std::string& interface_name,
    121 //                   const ObjectPath& object_path,
    122 //                   bool success) {
    123 //     // success is true if the method was exported successfully.
    124 //   }
    125 //
    126 //   ...
    127 //   dbus::ExportedObject* exported_object =
    128 //       bus.GetExportedObject(service_name, object_path);
    129 //   exported_object.ExportMethod(interface_name, method_name,
    130 //                                base::Bind(&Echo),
    131 //                                base::Bind(&OnExported));
    132 //
    133 // WHY IS THIS A REF COUNTED OBJECT?
    134 //
    135 // Bus is a ref counted object, to ensure that |this| of the object is
    136 // alive when callbacks referencing |this| are called. However, after the
    137 // bus is shut down, |connection_| can be NULL. Hence, callbacks should
    138 // not rely on that |connection_| is alive.
    139 class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
    140  public:
    141   // Specifies the bus type. SESSION is used to communicate with per-user
    142   // services like GNOME applications. SYSTEM is used to communicate with
    143   // system-wide services like NetworkManager. CUSTOM_ADDRESS is used to
    144   // communicate with an user specified address.
    145   enum BusType {
    146     SESSION = DBUS_BUS_SESSION,
    147     SYSTEM = DBUS_BUS_SYSTEM,
    148     CUSTOM_ADDRESS,
    149   };
    150 
    151   // Specifies the connection type. PRIVATE should usually be used unless
    152   // you are sure that SHARED is safe for you, which is unlikely the case
    153   // in Chrome.
    154   //
    155   // PRIVATE gives you a private connection, that won't be shared with
    156   // other Bus objects.
    157   //
    158   // SHARED gives you a connection shared among other Bus objects, which
    159   // is unsafe if the connection is shared with multiple threads.
    160   enum ConnectionType {
    161     PRIVATE,
    162     SHARED,
    163   };
    164 
    165   // Specifies whether the GetServiceOwnerAndBlock call should report or
    166   // suppress errors.
    167   enum GetServiceOwnerOption {
    168     REPORT_ERRORS,
    169     SUPPRESS_ERRORS,
    170   };
    171 
    172   // Specifies service ownership options.
    173   //
    174   // REQUIRE_PRIMARY indicates that you require primary ownership of the
    175   // service name.
    176   //
    177   // ALLOW_REPLACEMENT indicates that you'll allow another connection to
    178   // steal ownership of this service name from you.
    179   //
    180   // REQUIRE_PRIMARY_ALLOW_REPLACEMENT does the obvious.
    181   enum ServiceOwnershipOptions {
    182     REQUIRE_PRIMARY = (DBUS_NAME_FLAG_DO_NOT_QUEUE |
    183                        DBUS_NAME_FLAG_REPLACE_EXISTING),
    184     REQUIRE_PRIMARY_ALLOW_REPLACEMENT = (REQUIRE_PRIMARY |
    185                                          DBUS_NAME_FLAG_ALLOW_REPLACEMENT),
    186   };
    187 
    188   // Options used to create a Bus object.
    189   struct CHROME_DBUS_EXPORT Options {
    190     Options();
    191     ~Options();
    192 
    193     BusType bus_type;  // SESSION by default.
    194     ConnectionType connection_type;  // PRIVATE by default.
    195     // If dbus_task_runner is set, the bus object will use that
    196     // task runner to process asynchronous operations.
    197     //
    198     // The thread servicing the task runner should meet the following
    199     // requirements:
    200     // 1) Already running.
    201     // 2) Has a MessageLoopForIO.
    202     scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
    203 
    204     // Specifies the server addresses to be connected. If you want to
    205     // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to
    206     // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to
    207     // connect to. The format of this address value is the dbus address style
    208     // which is described in
    209     // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
    210     //
    211     // EXAMPLE USAGE:
    212     //   dbus::Bus::Options options;
    213     //   options.bus_type = CUSTOM_ADDRESS;
    214     //   options.address.assign("unix:path=/tmp/dbus-XXXXXXX");
    215     //   // Set up other options
    216     //   dbus::Bus bus(options);
    217     //
    218     //   // Do something.
    219     //
    220     std::string address;
    221 
    222     // If the connection with dbus-daemon is closed, |disconnected_callback|
    223     // will be called on the origin thread. This is also called when the
    224     // disonnection by ShutdownAndBlock. |disconnected_callback| can be null
    225     // callback
    226     base::Closure disconnected_callback;
    227   };
    228 
    229   // Creates a Bus object. The actual connection will be established when
    230   // Connect() is called.
    231   explicit Bus(const Options& options);
    232 
    233   // Called when an ownership request is complete.
    234   // Parameters:
    235   // - the requested service name.
    236   // - whether ownership has been obtained or not.
    237   typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback;
    238 
    239   // Called when GetServiceOwner() completes.
    240   // |service_owner| is the return value from GetServiceOwnerAndBlock().
    241   typedef base::Callback<void (const std::string& service_owner)>
    242       GetServiceOwnerCallback;
    243 
    244   // TODO(satorux): Remove the service name parameter as the caller of
    245   // RequestOwnership() knows the service name.
    246 
    247   // Gets the object proxy for the given service name and the object path.
    248   // The caller must not delete the returned object.
    249   //
    250   // Returns an existing object proxy if the bus object already owns the
    251   // object proxy for the given service name and the object path.
    252   // Never returns NULL.
    253   //
    254   // The bus will own all object proxies created by the bus, to ensure
    255   // that the object proxies are detached from remote objects at the
    256   // shutdown time of the bus.
    257   //
    258   // The object proxy is used to call methods of remote objects, and
    259   // receive signals from them.
    260   //
    261   // |service_name| looks like "org.freedesktop.NetworkManager", and
    262   // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
    263   //
    264   // Must be called in the origin thread.
    265   virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
    266                                       const ObjectPath& object_path);
    267 
    268   // Same as above, but also takes a bitfield of ObjectProxy::Options.
    269   // See object_proxy.h for available options.
    270   virtual ObjectProxy* GetObjectProxyWithOptions(
    271       const std::string& service_name,
    272       const ObjectPath& object_path,
    273       int options);
    274 
    275   // Removes the previously created object proxy for the given service
    276   // name and the object path and releases its memory.
    277   //
    278   // If and object proxy for the given service name and object was
    279   // created with GetObjectProxy, this function removes it from the
    280   // bus object and detaches the ObjectProxy, invalidating any pointer
    281   // previously acquired for it with GetObjectProxy. A subsequent call
    282   // to GetObjectProxy will return a new object.
    283   //
    284   // All the object proxies are detached from remote objects at the
    285   // shutdown time of the bus, but they can be detached early to reduce
    286   // memory footprint and used match rules for the bus connection.
    287   //
    288   // |service_name| looks like "org.freedesktop.NetworkManager", and
    289   // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
    290   // |callback| is called when the object proxy is successfully removed and
    291   // detached.
    292   //
    293   // The function returns true when there is an object proxy matching the
    294   // |service_name| and |object_path| to remove, and calls |callback| when it
    295   // is removed. Otherwise, it returns false and the |callback| function is
    296   // never called. The |callback| argument must not be null.
    297   //
    298   // Must be called in the origin thread.
    299   virtual bool RemoveObjectProxy(const std::string& service_name,
    300                                  const ObjectPath& object_path,
    301                                  const base::Closure& callback);
    302 
    303   // Same as above, but also takes a bitfield of ObjectProxy::Options.
    304   // See object_proxy.h for available options.
    305   virtual bool RemoveObjectProxyWithOptions(
    306       const std::string& service_name,
    307       const ObjectPath& object_path,
    308       int options,
    309       const base::Closure& callback);
    310 
    311   // Gets the exported object for the given object path.
    312   // The caller must not delete the returned object.
    313   //
    314   // Returns an existing exported object if the bus object already owns
    315   // the exported object for the given object path. Never returns NULL.
    316   //
    317   // The bus will own all exported objects created by the bus, to ensure
    318   // that the exported objects are unregistered at the shutdown time of
    319   // the bus.
    320   //
    321   // The exported object is used to export methods of local objects, and
    322   // send signal from them.
    323   //
    324   // Must be called in the origin thread.
    325   virtual ExportedObject* GetExportedObject(const ObjectPath& object_path);
    326 
    327   // Unregisters the exported object for the given object path |object_path|.
    328   //
    329   // Getting an exported object for the same object path after this call
    330   // will return a new object, method calls on any remaining copies of the
    331   // previous object will not be called.
    332   //
    333   // Must be called in the origin thread.
    334   virtual void UnregisterExportedObject(const ObjectPath& object_path);
    335 
    336 
    337   // Gets an object manager for the given remote object path |object_path|
    338   // exported by the service |service_name|.
    339   //
    340   // Returns an existing object manager if the bus object already owns a
    341   // matching object manager, never returns NULL.
    342   //
    343   // The caller must not delete the returned object, the bus retains ownership
    344   // of all object managers.
    345   //
    346   // Must be called in the origin thread.
    347   virtual ObjectManager* GetObjectManager(const std::string& service_name,
    348                                           const ObjectPath& object_path);
    349 
    350   // Unregisters the object manager for the given remote object path
    351   // |object_path| exported by the srevice |service_name|.
    352   //
    353   // Getting an object manager for the same remote object after this call
    354   // will return a new object, method calls on any remaining copies of the
    355   // previous object are not permitted.
    356   //
    357   // Must be called in the origin thread.
    358   virtual void RemoveObjectManager(const std::string& service_name,
    359                                    const ObjectPath& object_path);
    360 
    361   // Instructs all registered object managers to retrieve their set of managed
    362   // objects from their respective remote objects. There is no need to call this
    363   // manually, this is called automatically by the D-Bus thread manager once
    364   // implementation classes are registered.
    365   virtual void GetManagedObjects();
    366 
    367   // Shuts down the bus and blocks until it's done. More specifically, this
    368   // function does the following:
    369   //
    370   // - Unregisters the object paths
    371   // - Releases the service names
    372   // - Closes the connection to dbus-daemon.
    373   //
    374   // This function can be called multiple times and it is no-op for the 2nd time
    375   // calling.
    376   //
    377   // BLOCKING CALL.
    378   virtual void ShutdownAndBlock();
    379 
    380   // Similar to ShutdownAndBlock(), but this function is used to
    381   // synchronously shut down the bus that uses the D-Bus thread. This
    382   // function is intended to be used at the very end of the browser
    383   // shutdown, where it makes more sense to shut down the bus
    384   // synchronously, than trying to make it asynchronous.
    385   //
    386   // BLOCKING CALL, but must be called in the origin thread.
    387   virtual void ShutdownOnDBusThreadAndBlock();
    388 
    389   // Returns true if the shutdown has been completed.
    390   bool shutdown_completed() { return shutdown_completed_; }
    391 
    392   //
    393   // The public functions below are not intended to be used in client
    394   // code. These are used to implement ObjectProxy and ExportedObject.
    395   //
    396 
    397   // Connects the bus to the dbus-daemon.
    398   // Returns true on success, or the bus is already connected.
    399   //
    400   // BLOCKING CALL.
    401   virtual bool Connect();
    402 
    403   // Disconnects the bus from the dbus-daemon.
    404   // Safe to call multiple times and no operation after the first call.
    405   // Do not call for shared connection it will be released by libdbus.
    406   //
    407   // BLOCKING CALL.
    408   virtual void ClosePrivateConnection();
    409 
    410   // Requests the ownership of the service name given by |service_name|.
    411   // See also RequestOwnershipAndBlock().
    412   //
    413   // |on_ownership_callback| is called when the service name is obtained
    414   // or failed to be obtained, in the origin thread.
    415   //
    416   // Must be called in the origin thread.
    417   virtual void RequestOwnership(const std::string& service_name,
    418                                 ServiceOwnershipOptions options,
    419                                 OnOwnershipCallback on_ownership_callback);
    420 
    421   // Requests the ownership of the given service name.
    422   // Returns true on success, or the the service name is already obtained.
    423   //
    424   // Note that it's important to expose methods before requesting a service
    425   // name with this method.  See also ExportedObject::ExportMethodAndBlock()
    426   // for details.
    427   //
    428   // BLOCKING CALL.
    429   virtual bool RequestOwnershipAndBlock(const std::string& service_name,
    430                                         ServiceOwnershipOptions options);
    431 
    432   // Releases the ownership of the given service name.
    433   // Returns true on success.
    434   //
    435   // BLOCKING CALL.
    436   virtual bool ReleaseOwnership(const std::string& service_name);
    437 
    438   // Sets up async operations.
    439   // Returns true on success, or it's already set up.
    440   // This function needs to be called before starting async operations.
    441   //
    442   // BLOCKING CALL.
    443   virtual bool SetUpAsyncOperations();
    444 
    445   // Sends a message to the bus and blocks until the response is
    446   // received. Used to implement synchronous method calls.
    447   //
    448   // BLOCKING CALL.
    449   virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
    450                                              int timeout_ms,
    451                                              DBusError* error);
    452 
    453   // Requests to send a message to the bus. The reply is handled with
    454   // |pending_call| at a later time.
    455   //
    456   // BLOCKING CALL.
    457   virtual void SendWithReply(DBusMessage* request,
    458                              DBusPendingCall** pending_call,
    459                              int timeout_ms);
    460 
    461   // Requests to send a message to the bus. The message serial number will
    462   // be stored in |serial|.
    463   //
    464   // BLOCKING CALL.
    465   virtual void Send(DBusMessage* request, uint32* serial);
    466 
    467   // Adds the message filter function. |filter_function| will be called
    468   // when incoming messages are received. Returns true on success.
    469   //
    470   // When a new incoming message arrives, filter functions are called in
    471   // the order that they were added until the the incoming message is
    472   // handled by a filter function.
    473   //
    474   // The same filter function associated with the same user data cannot be
    475   // added more than once. Returns false for this case.
    476   //
    477   // BLOCKING CALL.
    478   virtual bool AddFilterFunction(DBusHandleMessageFunction filter_function,
    479                                  void* user_data);
    480 
    481   // Removes the message filter previously added by AddFilterFunction().
    482   // Returns true on success.
    483   //
    484   // BLOCKING CALL.
    485   virtual bool RemoveFilterFunction(DBusHandleMessageFunction filter_function,
    486                                     void* user_data);
    487 
    488   // Adds the match rule. Messages that match the rule will be processed
    489   // by the filter functions added by AddFilterFunction().
    490   //
    491   // You cannot specify which filter function to use for a match rule.
    492   // Instead, you should check if an incoming message is what you are
    493   // interested in, in the filter functions.
    494   //
    495   // The same match rule can be added more than once and should be removed
    496   // as many times as it was added.
    497   //
    498   // The match rule looks like:
    499   // "type='signal', interface='org.chromium.SomeInterface'".
    500   //
    501   // See "Message Bus Message Routing" section in the D-Bus specification
    502   // for details about match rules:
    503   // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
    504   //
    505   // BLOCKING CALL.
    506   virtual void AddMatch(const std::string& match_rule, DBusError* error);
    507 
    508   // Removes the match rule previously added by AddMatch().
    509   // Returns false if the requested match rule is unknown or has already been
    510   // removed. Otherwise, returns true and sets |error| accordingly.
    511   //
    512   // BLOCKING CALL.
    513   virtual bool RemoveMatch(const std::string& match_rule, DBusError* error);
    514 
    515   // Tries to register the object path. Returns true on success.
    516   // Returns false if the object path is already registered.
    517   //
    518   // |message_function| in |vtable| will be called every time when a new
    519   // |message sent to the object path arrives.
    520   //
    521   // The same object path must not be added more than once.
    522   //
    523   // See also documentation of |dbus_connection_try_register_object_path| at
    524   // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
    525   //
    526   // BLOCKING CALL.
    527   virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
    528                                      const DBusObjectPathVTable* vtable,
    529                                      void* user_data,
    530                                      DBusError* error);
    531 
    532   // Unregister the object path.
    533   //
    534   // BLOCKING CALL.
    535   virtual void UnregisterObjectPath(const ObjectPath& object_path);
    536 
    537   // Returns the task runner of the D-Bus thread.
    538   virtual base::TaskRunner* GetDBusTaskRunner();
    539 
    540   // Returns the task runner of the thread that created the bus.
    541   virtual base::TaskRunner* GetOriginTaskRunner();
    542 
    543   // Returns true if the bus has the D-Bus thread.
    544   virtual bool HasDBusThread();
    545 
    546   // Check whether the current thread is on the origin thread (the thread
    547   // that created the bus). If not, DCHECK will fail.
    548   virtual void AssertOnOriginThread();
    549 
    550   // Check whether the current thread is on the D-Bus thread. If not,
    551   // DCHECK will fail. If the D-Bus thread is not supplied, it calls
    552   // AssertOnOriginThread().
    553   virtual void AssertOnDBusThread();
    554 
    555   // Gets the owner for |service_name| via org.freedesktop.DBus.GetNameOwner.
    556   // Returns the owner name, if any, or an empty string on failure.
    557   // |options| specifies where to printing error messages or not.
    558   //
    559   // BLOCKING CALL.
    560   virtual std::string GetServiceOwnerAndBlock(const std::string& service_name,
    561                                               GetServiceOwnerOption options);
    562 
    563   // A non-blocking version of GetServiceOwnerAndBlock().
    564   // Must be called in the origin thread.
    565   virtual void GetServiceOwner(const std::string& service_name,
    566                                const GetServiceOwnerCallback& callback);
    567 
    568   // Whenever the owner for |service_name| changes, run |callback| with the
    569   // name of the new owner. If the owner goes away, then |callback| receives
    570   // an empty string.
    571   //
    572   // Any unique (service_name, callback) can be used. Duplicate are ignored.
    573   // |service_name| must not be empty and |callback| must not be null.
    574   //
    575   // Must be called in the origin thread.
    576   virtual void ListenForServiceOwnerChange(
    577       const std::string& service_name,
    578       const GetServiceOwnerCallback& callback);
    579 
    580   // Stop listening for |service_name| owner changes for |callback|.
    581   // Any unique (service_name, callback) can be used. Non-registered callbacks
    582   // for a given service name are ignored.
    583   // |service_name| must not be empty and |callback| must not be null.
    584   //
    585   // Must be called in the origin thread.
    586   virtual void UnlistenForServiceOwnerChange(
    587       const std::string& service_name,
    588       const GetServiceOwnerCallback& callback);
    589 
    590   // Returns true if the bus is connected to D-Bus.
    591   bool is_connected() { return connection_ != NULL; }
    592 
    593  protected:
    594   // This is protected, so we can define sub classes.
    595   virtual ~Bus();
    596 
    597  private:
    598   friend class base::RefCountedThreadSafe<Bus>;
    599 
    600   // Helper function used for RemoveObjectProxy().
    601   void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
    602                                  const base::Closure& callback);
    603 
    604   // Helper function used for UnregisterExportedObject().
    605   void UnregisterExportedObjectInternal(
    606       scoped_refptr<dbus::ExportedObject> exported_object);
    607 
    608   // Helper function used for ShutdownOnDBusThreadAndBlock().
    609   void ShutdownOnDBusThreadAndBlockInternal();
    610 
    611   // Helper function used for RequestOwnership().
    612   void RequestOwnershipInternal(const std::string& service_name,
    613                                 ServiceOwnershipOptions options,
    614                                 OnOwnershipCallback on_ownership_callback);
    615 
    616   // Helper function used for GetServiceOwner().
    617   void GetServiceOwnerInternal(const std::string& service_name,
    618                                const GetServiceOwnerCallback& callback);
    619 
    620   // Helper function used for ListenForServiceOwnerChange().
    621   void ListenForServiceOwnerChangeInternal(
    622       const std::string& service_name,
    623       const GetServiceOwnerCallback& callback);
    624 
    625   // Helper function used for UnListenForServiceOwnerChange().
    626   void UnlistenForServiceOwnerChangeInternal(
    627       const std::string& service_name,
    628       const GetServiceOwnerCallback& callback);
    629 
    630   // Processes the all incoming data to the connection, if any.
    631   //
    632   // BLOCKING CALL.
    633   void ProcessAllIncomingDataIfAny();
    634 
    635   // Called when a watch object is added. Used to start monitoring the
    636   // file descriptor used for D-Bus communication.
    637   dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
    638 
    639   // Called when a watch object is removed.
    640   void OnRemoveWatch(DBusWatch* raw_watch);
    641 
    642   // Called when the "enabled" status of |raw_watch| is toggled.
    643   void OnToggleWatch(DBusWatch* raw_watch);
    644 
    645   // Called when a timeout object is added. Used to start monitoring
    646   // timeout for method calls.
    647   dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
    648 
    649   // Called when a timeout object is removed.
    650   void OnRemoveTimeout(DBusTimeout* raw_timeout);
    651 
    652   // Called when the "enabled" status of |raw_timeout| is toggled.
    653   void OnToggleTimeout(DBusTimeout* raw_timeout);
    654 
    655   // Called when the dispatch status (i.e. if any incoming data is
    656   // available) is changed.
    657   void OnDispatchStatusChanged(DBusConnection* connection,
    658                                DBusDispatchStatus status);
    659 
    660   // Called when the connection is diconnected.
    661   void OnConnectionDisconnected(DBusConnection* connection);
    662 
    663   // Called when a service owner change occurs.
    664   void OnServiceOwnerChanged(DBusMessage* message);
    665 
    666   // Callback helper functions. Redirects to the corresponding member function.
    667   static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
    668   static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
    669   static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
    670   static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
    671   static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
    672   static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
    673   static void OnDispatchStatusChangedThunk(DBusConnection* connection,
    674                                            DBusDispatchStatus status,
    675                                            void* data);
    676 
    677   // Calls OnConnectionDisconnected if the Disconnected signal is received.
    678   static DBusHandlerResult OnConnectionDisconnectedFilter(
    679       DBusConnection* connection,
    680       DBusMessage* message,
    681       void* user_data);
    682 
    683   // Calls OnServiceOwnerChanged for a NameOwnerChanged signal.
    684   static DBusHandlerResult OnServiceOwnerChangedFilter(
    685       DBusConnection* connection,
    686       DBusMessage* message,
    687       void* user_data);
    688 
    689   const BusType bus_type_;
    690   const ConnectionType connection_type_;
    691   scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
    692   base::WaitableEvent on_shutdown_;
    693   DBusConnection* connection_;
    694 
    695   scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
    696   base::PlatformThreadId origin_thread_id_;
    697 
    698   std::set<std::string> owned_service_names_;
    699   // The following sets are used to check if rules/object_paths/filters
    700   // are properly cleaned up before destruction of the bus object.
    701   // Since it's not an error to add the same match rule twice, the repeated
    702   // match rules are counted in a map.
    703   std::map<std::string, int> match_rules_added_;
    704   std::set<ObjectPath> registered_object_paths_;
    705   std::set<std::pair<DBusHandleMessageFunction, void*> >
    706       filter_functions_added_;
    707 
    708   // ObjectProxyTable is used to hold the object proxies created by the
    709   // bus object. Key is a pair; the first part is a concatenated string of
    710   // service name + object path, like
    711   // "org.chromium.TestService/org/chromium/TestObject".
    712   // The second part is the ObjectProxy::Options for the proxy.
    713   typedef std::map<std::pair<std::string, int>,
    714                    scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
    715   ObjectProxyTable object_proxy_table_;
    716 
    717   // ExportedObjectTable is used to hold the exported objects created by
    718   // the bus object. Key is a concatenated string of service name +
    719   // object path, like "org.chromium.TestService/org/chromium/TestObject".
    720   typedef std::map<const dbus::ObjectPath,
    721                    scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
    722   ExportedObjectTable exported_object_table_;
    723 
    724   // ObjectManagerTable is used to hold the object managers created by the
    725   // bus object. Key is a concatenated string of service name + object path,
    726   // like "org.chromium.TestService/org/chromium/TestObject".
    727   typedef std::map<std::string,
    728                    scoped_refptr<dbus::ObjectManager> > ObjectManagerTable;
    729   ObjectManagerTable object_manager_table_;
    730 
    731   // A map of NameOwnerChanged signals to listen for and the callbacks to run
    732   // on the origin thread when the owner changes.
    733   // Only accessed on the DBus thread.
    734   // Key: Service name
    735   // Value: Vector of callbacks. Unique and expected to be small. Not using
    736   //        std::set here because base::Callbacks don't have a '<' operator.
    737   typedef std::map<std::string, std::vector<GetServiceOwnerCallback> >
    738       ServiceOwnerChangedListenerMap;
    739   ServiceOwnerChangedListenerMap service_owner_changed_listener_map_;
    740 
    741   bool async_operations_set_up_;
    742   bool shutdown_completed_;
    743 
    744   // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
    745   // OnAddTimeout()/OnRemoveTimeou() are balanced.
    746   int num_pending_watches_;
    747   int num_pending_timeouts_;
    748 
    749   std::string address_;
    750   base::Closure on_disconnected_closure_;
    751 
    752   DISALLOW_COPY_AND_ASSIGN(Bus);
    753 };
    754 
    755 }  // namespace dbus
    756 
    757 #endif  // DBUS_BUS_H_
    758