blob: c4c52474e0ab92909e5a09717cd3e168f712b8de [file] [log] [blame]
Ben Chan084faca2013-07-02 14:25:12 -07001// Copyright (c) 2014 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#ifndef SHILL_DBUS_NAME_WATCHER_H_
6#define SHILL_DBUS_NAME_WATCHER_H_
7
8#include <string>
9
Ben Chan084faca2013-07-02 14:25:12 -070010#include <base/callback.h>
Ben Chancc67c522014-09-03 07:19:18 -070011#include <base/macros.h>
Ben Chan084faca2013-07-02 14:25:12 -070012#include <base/memory/weak_ptr.h>
13
14namespace shill {
15
16class DBusManager;
17
18class DBusNameWatcher : public base::SupportsWeakPtr<DBusNameWatcher> {
19 public:
Paul Stewarta794cd62015-06-16 13:13:10 -070020 typedef base::Callback<void(const std::string& name,
21 const std::string& owner)> NameAppearedCallback;
22 typedef base::Callback<void(const std::string& name)> NameVanishedCallback;
Ben Chan084faca2013-07-02 14:25:12 -070023
24 // Constructs a watcher to monitor a given DBus service |name|. When the
25 // service appears, |name_appeared_callback| is invoked if non-null. When the
26 // service vanishes, |name_vanished_callback| is invoked if non-null. At
27 // construction, it registers to a DBus manager |dbus_manager| in order to
28 // receive notifications when |name| appears on or vanishes from DBus. At
29 // desctruction, it dereigisters from |dbus_manager|.
Paul Stewarta794cd62015-06-16 13:13:10 -070030 DBusNameWatcher(DBusManager* dbus_manager,
31 const std::string& name,
32 const NameAppearedCallback& name_appeared_callback,
33 const NameVanishedCallback& name_vanished_callback);
Ben Chan084faca2013-07-02 14:25:12 -070034 ~DBusNameWatcher();
35
36 // Called by |dbus_manager_| when |name_| appears on or vanishes from DBus.
37 // |name_appeared_callback_| or |name_vanished_callback_| is invoked
38 // accordingly if non-null.
Paul Stewarta794cd62015-06-16 13:13:10 -070039 void OnNameOwnerChanged(const std::string& name) const;
Ben Chan084faca2013-07-02 14:25:12 -070040
Paul Stewarta794cd62015-06-16 13:13:10 -070041 const std::string& name() const { return name_; }
Ben Chan084faca2013-07-02 14:25:12 -070042
43 private:
44 base::WeakPtr<DBusManager> dbus_manager_;
45 std::string name_;
46 NameAppearedCallback name_appeared_callback_;
47 NameVanishedCallback name_vanished_callback_;
48
49 DISALLOW_COPY_AND_ASSIGN(DBusNameWatcher);
50};
51
52} // namespace shill
53
54#endif // SHILL_DBUS_NAME_WATCHER_H_