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