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