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> |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 16 | #include <base/memory/weak_ptr.h> |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 17 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 18 | |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 19 | #include "shill/dbus_name_watcher.h" |
| 20 | |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 21 | namespace shill { |
| 22 | |
| 23 | class DBusServiceProxyInterface; |
| 24 | class Error; |
| 25 | class ProxyFactory; |
| 26 | |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 27 | class DBusManager : public base::SupportsWeakPtr<DBusManager> { |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 28 | public: |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 29 | DBusManager(); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 30 | virtual ~DBusManager(); |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 31 | |
| 32 | void Start(); |
| 33 | void Stop(); |
| 34 | |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 35 | // Creates and registers a watcher for DBus service |name|. When the service |
| 36 | // appears, |name_appeared_callback| is invoked if non-null. When the service |
| 37 | // vanishes, |name_vanished_callback| is invoked if non-null. |
| 38 | // |name_appeared_callback| or |name_vanished_callback| will be notified once |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 39 | // asynchronously if the service has or doesn't have an owner, respectively, |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 40 | // when this method is invoked. The returned watcher should be managed by the |
| 41 | // caller and may outlive this DBus manager. The watcher holds a weak pointer |
| 42 | // to this DBus manager. When it is destructed, it automatically calls |
| 43 | // RemoveNameWatcher() to deregister and remove itself from this DBus |
| 44 | // manager. |
| 45 | virtual DBusNameWatcher *CreateNameWatcher( |
| 46 | const std::string &name, |
| 47 | const DBusNameWatcher::NameAppearedCallback &name_appeared_callback, |
| 48 | const DBusNameWatcher::NameVanishedCallback &name_vanished_callback); |
| 49 | |
| 50 | // Deregisters and removes the watcher such that it stops monitoring the |
| 51 | // associated DBus service name. |
| 52 | virtual void RemoveNameWatcher(DBusNameWatcher *name_watcher); |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | friend class DBusManagerTest; |
Ben Chan | 66174a1 | 2014-01-08 21:27:00 -0800 | [diff] [blame] | 56 | friend class ModemInfoTest; |
| 57 | friend class ModemManagerTest; |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 58 | friend class WiFiObjectTest; |
| 59 | friend class WiMaxProviderTest; |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 60 | FRIEND_TEST(DBusManagerTest, NameWatchers); |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 61 | FRIEND_TEST(WiMaxProviderTest, StartStop); |
Ben Chan | 66174a1 | 2014-01-08 21:27:00 -0800 | [diff] [blame] | 62 | FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish); |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 63 | |
| 64 | void OnNameOwnerChanged(const std::string &name, |
| 65 | const std::string &old_owner, |
| 66 | const std::string &new_owner); |
| 67 | |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 68 | void OnGetNameOwnerComplete( |
| 69 | const base::WeakPtr<DBusNameWatcher> &name_watcher, |
| 70 | const std::string &unique_name, |
| 71 | const Error &error); |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 72 | |
| 73 | ProxyFactory *proxy_factory_; |
| 74 | |
| 75 | scoped_ptr<DBusServiceProxyInterface> proxy_; |
| 76 | |
Ben Chan | 084faca | 2013-07-02 14:25:12 -0700 | [diff] [blame] | 77 | std::map<std::string, std::list<DBusNameWatcher *>> name_watchers_; |
Darin Petkov | 002c58e | 2012-06-19 02:56:05 +0200 | [diff] [blame] | 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(DBusManager); |
| 80 | }; |
| 81 | |
| 82 | } // namespace shill |
| 83 | |
| 84 | #endif // SHILL_DBUS_MANAGER_H_ |