Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 1 | // 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 | |
| 10 | #include <base/basictypes.h> |
| 11 | #include <base/callback.h> |
| 12 | #include <base/memory/weak_ptr.h> |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | class DBusManager; |
| 17 | |
| 18 | class DBusNameWatcher : public base::SupportsWeakPtr<DBusNameWatcher> { |
| 19 | public: |
| 20 | typedef base::Callback<void(const std::string &name, |
| 21 | const std::string &owner)> NameAppearedCallback; |
| 22 | typedef base::Callback<void(const std::string &name)> NameVanishedCallback; |
| 23 | |
| 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|. |
| 30 | DBusNameWatcher(DBusManager *dbus_manager, |
| 31 | const std::string &name, |
| 32 | const NameAppearedCallback &name_appeared_callback, |
| 33 | const NameVanishedCallback &name_vanished_callback); |
| 34 | ~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. |
| 39 | void OnNameOwnerChanged(const std::string &name) const; |
| 40 | |
| 41 | const std::string &name() const { return name_; } |
| 42 | |
| 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_ |