Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 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_MANAGER_H_ |
| 6 | #define SHILL_DBUS_MANAGER_H_ |
| 7 | |
| 8 | #include <list> |
| 9 | #include <map> |
| 10 | #include <string> |
| 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | #include <base/callback.h> |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 14 | #include <base/cancelable_callback.h> |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 15 | #include <base/memory/scoped_ptr.h> |
| 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 17 | |
| 18 | namespace shill { |
| 19 | |
| 20 | class DBusServiceProxyInterface; |
| 21 | class Error; |
| 22 | class ProxyFactory; |
| 23 | |
| 24 | class DBusManager { |
| 25 | public: |
| 26 | typedef base::Callback<void(const std::string &owner)> AppearedCallback; |
| 27 | typedef base::Closure VanishedCallback; |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 28 | typedef base::CancelableCallback<void( |
| 29 | const std::string &owner)> CancelableAppearedCallback; |
| 30 | typedef base::CancelableClosure CancelableVanishedCallback; |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 31 | |
| 32 | DBusManager(); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 33 | virtual ~DBusManager(); |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 34 | |
| 35 | void Start(); |
| 36 | void Stop(); |
| 37 | |
| 38 | // Registers a watcher for DBus service |name|. When the service appears, |
| 39 | // |on_appear| is invoked if non-null. When the service vanishes, |on_vanish| |
| 40 | // is invoked if non-null. |on_appear| or |on_vanish| will be notified once |
| 41 | // asynchronously if the service has or doesn't have an owner, respectively, |
| 42 | // when WatchName is invoked. |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 43 | virtual void WatchName(const std::string &name, |
| 44 | const AppearedCallback &on_appear, |
| 45 | const VanishedCallback &on_vanish); |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | friend class DBusManagerTest; |
| 49 | FRIEND_TEST(DBusManagerTest, NameWatchers); |
| 50 | |
| 51 | struct NameWatcher { |
| 52 | NameWatcher(const AppearedCallback &on_appear_in, |
| 53 | const VanishedCallback &on_vanish_in) |
| 54 | : on_appear(on_appear_in), on_vanish(on_vanish_in) {} |
| 55 | |
| 56 | AppearedCallback on_appear; |
| 57 | VanishedCallback on_vanish; |
| 58 | }; |
| 59 | |
| 60 | void OnNameOwnerChanged(const std::string &name, |
| 61 | const std::string &old_owner, |
| 62 | const std::string &new_owner); |
| 63 | |
| 64 | void OnGetNameOwnerComplete(const NameWatcher &watcher, |
| 65 | const std::string &unique_name, |
| 66 | const Error &error); |
| 67 | |
| 68 | static void NotifyNameWatcher(const NameWatcher &watcher, |
| 69 | const std::string &owner); |
| 70 | |
| 71 | ProxyFactory *proxy_factory_; |
| 72 | |
| 73 | scoped_ptr<DBusServiceProxyInterface> proxy_; |
| 74 | |
| 75 | std::map<std::string, std::list<NameWatcher> > name_watchers_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(DBusManager); |
| 78 | }; |
| 79 | |
| 80 | } // namespace shill |
| 81 | |
| 82 | #endif // SHILL_DBUS_MANAGER_H_ |