summaryrefslogtreecommitdiff
path: root/brillo/dbus/dbus_service_watcher.h
blob: 0031771fb69b522e479124565886f60a4826158e (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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.

#ifndef LIBBRILLO_BRILLO_DBUS_DBUS_SERVICE_WATCHER_H_
#define LIBBRILLO_BRILLO_DBUS_DBUS_SERVICE_WATCHER_H_

#include <string>

#include <base/callback.h>
#include <base/macros.h>
#include <base/memory/ref_counted.h>
#include <base/memory/weak_ptr.h>
#include <brillo/brillo_export.h>
#include <dbus/bus.h>

namespace brillo {
namespace dbus_utils {

// DBusServiceWatcher just asks the bus to notify us when the owner of a remote
// DBus connection transitions to the empty string.  After registering a
// callback to be notified of name owner transitions, for the given
// |connection_name|, DBusServiceWatcher asks for the current owner.  If at any
// point an empty string is found for the connection name owner,
// DBusServiceWatcher will call back to notify of the connection vanishing.
//
// The chief value of this class is that it manages the lifetime of the
// registered callback in the Bus, because failure to remove callbacks will
// cause the Bus to crash the process on destruction.
class BRILLO_EXPORT DBusServiceWatcher {
 public:
  DBusServiceWatcher(scoped_refptr<dbus::Bus> bus,
                     const std::string& connection_name,
                     const base::Closure& on_connection_vanish);
  virtual ~DBusServiceWatcher();
  virtual std::string connection_name() const { return connection_name_; }

 private:
  void OnServiceOwnerChange(const std::string& service_owner);

  scoped_refptr<dbus::Bus> bus_;
  const std::string connection_name_;
  dbus::Bus::GetServiceOwnerCallback monitoring_callback_;
  base::Closure on_connection_vanish_;

  base::WeakPtrFactory<DBusServiceWatcher> weak_factory_{this};
  DISALLOW_COPY_AND_ASSIGN(DBusServiceWatcher);
};

}  // namespace dbus_utils
}  // namespace brillo

#endif  // LIBBRILLO_BRILLO_DBUS_DBUS_SERVICE_WATCHER_H_