Home | History | Annotate | Download | only in dbus
      1 // Copyright 2015 The Chromium OS 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 LIBBRILLO_BRILLO_DBUS_DBUS_SERVICE_WATCHER_H_
      6 #define LIBBRILLO_BRILLO_DBUS_DBUS_SERVICE_WATCHER_H_
      7 
      8 #include <string>
      9 
     10 #include <base/callback.h>
     11 #include <base/macros.h>
     12 #include <base/memory/ref_counted.h>
     13 #include <base/memory/weak_ptr.h>
     14 #include <brillo/brillo_export.h>
     15 #include <dbus/bus.h>
     16 
     17 namespace brillo {
     18 namespace dbus_utils {
     19 
     20 // DBusServiceWatcher just asks the bus to notify us when the owner of a remote
     21 // DBus connection transitions to the empty string.  After registering a
     22 // callback to be notified of name owner transitions, for the given
     23 // |connection_name|, DBusServiceWatcher asks for the current owner.  If at any
     24 // point an empty string is found for the connection name owner,
     25 // DBusServiceWatcher will call back to notify of the connection vanishing.
     26 //
     27 // The chief value of this class is that it manages the lifetime of the
     28 // registered callback in the Bus, because failure to remove callbacks will
     29 // cause the Bus to crash the process on destruction.
     30 class BRILLO_EXPORT DBusServiceWatcher {
     31  public:
     32   DBusServiceWatcher(scoped_refptr<dbus::Bus> bus,
     33                      const std::string& connection_name,
     34                      const base::Closure& on_connection_vanish);
     35   virtual ~DBusServiceWatcher();
     36   virtual std::string connection_name() const { return connection_name_; }
     37 
     38  private:
     39   void OnServiceOwnerChange(const std::string& service_owner);
     40 
     41   scoped_refptr<dbus::Bus> bus_;
     42   const std::string connection_name_;
     43   dbus::Bus::GetServiceOwnerCallback monitoring_callback_;
     44   base::Closure on_connection_vanish_;
     45 
     46   base::WeakPtrFactory<DBusServiceWatcher> weak_factory_{this};
     47   DISALLOW_COPY_AND_ASSIGN(DBusServiceWatcher);
     48 };
     49 
     50 }  // namespace dbus_utils
     51 }  // namespace brillo
     52 
     53 #endif  // LIBBRILLO_BRILLO_DBUS_DBUS_SERVICE_WATCHER_H_
     54