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