aboutsummaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/dbus_service_watcher.cc
blob: 6b6319e1b83cf180ac4a08d847c2850a1e96cf0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <chromeos/dbus/dbus_service_watcher.h>

#include <base/bind.h>

namespace chromeos {
namespace dbus_utils {

DBusServiceWatcher::DBusServiceWatcher(
    scoped_refptr<dbus::Bus> bus,
    const std::string& connection_name,
    const base::Closure& on_connection_vanish)
    : bus_{bus},
      connection_name_{connection_name},
      on_connection_vanish_{on_connection_vanish} {
  monitoring_callback_ = base::Bind(
      &DBusServiceWatcher::OnServiceOwnerChange, weak_factory_.GetWeakPtr());
  // Register to listen, and then request the current owner;
  bus_->ListenForServiceOwnerChange(connection_name_, monitoring_callback_);
  bus_->GetServiceOwner(connection_name_, monitoring_callback_);
}

DBusServiceWatcher::~DBusServiceWatcher() {
  bus_->UnlistenForServiceOwnerChange(
      connection_name_, monitoring_callback_);
}

void DBusServiceWatcher::OnServiceOwnerChange(
    const std::string& service_owner) {
  if (service_owner.empty()) {
    on_connection_vanish_.Run();
  }
}

}  // namespace dbus_utils
}  // namespace chromeos