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 #include <brillo/dbus/dbus_service_watcher.h>
6*1a96fba6SXin Li
7*1a96fba6SXin Li #include <base/bind.h>
8*1a96fba6SXin Li
9*1a96fba6SXin Li namespace brillo {
10*1a96fba6SXin Li namespace dbus_utils {
11*1a96fba6SXin Li
DBusServiceWatcher(scoped_refptr<dbus::Bus> bus,const std::string & connection_name,const base::Closure & on_connection_vanish)12*1a96fba6SXin Li DBusServiceWatcher::DBusServiceWatcher(
13*1a96fba6SXin Li scoped_refptr<dbus::Bus> bus,
14*1a96fba6SXin Li const std::string& connection_name,
15*1a96fba6SXin Li const base::Closure& on_connection_vanish)
16*1a96fba6SXin Li : bus_{bus},
17*1a96fba6SXin Li connection_name_{connection_name},
18*1a96fba6SXin Li on_connection_vanish_{on_connection_vanish} {
19*1a96fba6SXin Li monitoring_callback_ = base::Bind(
20*1a96fba6SXin Li &DBusServiceWatcher::OnServiceOwnerChange, weak_factory_.GetWeakPtr());
21*1a96fba6SXin Li // Register to listen, and then request the current owner;
22*1a96fba6SXin Li bus_->ListenForServiceOwnerChange(connection_name_, monitoring_callback_);
23*1a96fba6SXin Li bus_->GetServiceOwner(connection_name_, monitoring_callback_);
24*1a96fba6SXin Li }
25*1a96fba6SXin Li
~DBusServiceWatcher()26*1a96fba6SXin Li DBusServiceWatcher::~DBusServiceWatcher() {
27*1a96fba6SXin Li bus_->UnlistenForServiceOwnerChange(
28*1a96fba6SXin Li connection_name_, monitoring_callback_);
29*1a96fba6SXin Li }
30*1a96fba6SXin Li
OnServiceOwnerChange(const std::string & service_owner)31*1a96fba6SXin Li void DBusServiceWatcher::OnServiceOwnerChange(
32*1a96fba6SXin Li const std::string& service_owner) {
33*1a96fba6SXin Li if (service_owner.empty()) {
34*1a96fba6SXin Li on_connection_vanish_.Run();
35*1a96fba6SXin Li }
36*1a96fba6SXin Li }
37*1a96fba6SXin Li
38*1a96fba6SXin Li } // namespace dbus_utils
39*1a96fba6SXin Li } // namespace brillo
40