blob: e3deb5eb167a24d56334ac838ca141ae87514ac0 [file] [log] [blame]
Darin Petkov002c58e2012-06-19 02:56:05 +02001// 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 Petkov2b8e44e2012-06-25 15:13:26 +020014#include <base/cancelable_callback.h>
Darin Petkov002c58e2012-06-19 02:56:05 +020015#include <base/memory/scoped_ptr.h>
Ben Chan084faca2013-07-02 14:25:12 -070016#include <base/memory/weak_ptr.h>
Darin Petkov002c58e2012-06-19 02:56:05 +020017#include <gtest/gtest_prod.h> // for FRIEND_TEST
18
Ben Chan084faca2013-07-02 14:25:12 -070019#include "shill/dbus_name_watcher.h"
20
Darin Petkov002c58e2012-06-19 02:56:05 +020021namespace shill {
22
23class DBusServiceProxyInterface;
24class Error;
25class ProxyFactory;
26
Ben Chan084faca2013-07-02 14:25:12 -070027class DBusManager : public base::SupportsWeakPtr<DBusManager> {
Darin Petkov002c58e2012-06-19 02:56:05 +020028 public:
Darin Petkov002c58e2012-06-19 02:56:05 +020029 DBusManager();
Darin Petkov2b8e44e2012-06-25 15:13:26 +020030 virtual ~DBusManager();
Darin Petkov002c58e2012-06-19 02:56:05 +020031
32 void Start();
33 void Stop();
34
Ben Chan084faca2013-07-02 14:25:12 -070035 // 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 Petkov002c58e2012-06-19 02:56:05 +020039 // asynchronously if the service has or doesn't have an owner, respectively,
Ben Chan084faca2013-07-02 14:25:12 -070040 // 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 Petkov002c58e2012-06-19 02:56:05 +020053
54 private:
55 friend class DBusManagerTest;
Ben Chan66174a12014-01-08 21:27:00 -080056 friend class ModemInfoTest;
57 friend class ModemManagerTest;
Ben Chan084faca2013-07-02 14:25:12 -070058 friend class WiFiObjectTest;
59 friend class WiMaxProviderTest;
Darin Petkov002c58e2012-06-19 02:56:05 +020060 FRIEND_TEST(DBusManagerTest, NameWatchers);
Ben Chan084faca2013-07-02 14:25:12 -070061 FRIEND_TEST(WiMaxProviderTest, StartStop);
Ben Chan66174a12014-01-08 21:27:00 -080062 FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish);
Darin Petkov002c58e2012-06-19 02:56:05 +020063
64 void OnNameOwnerChanged(const std::string &name,
65 const std::string &old_owner,
66 const std::string &new_owner);
67
Ben Chan084faca2013-07-02 14:25:12 -070068 void OnGetNameOwnerComplete(
69 const base::WeakPtr<DBusNameWatcher> &name_watcher,
70 const std::string &unique_name,
71 const Error &error);
Darin Petkov002c58e2012-06-19 02:56:05 +020072
73 ProxyFactory *proxy_factory_;
74
75 scoped_ptr<DBusServiceProxyInterface> proxy_;
76
Ben Chan084faca2013-07-02 14:25:12 -070077 std::map<std::string, std::list<DBusNameWatcher *>> name_watchers_;
Darin Petkov002c58e2012-06-19 02:56:05 +020078
79 DISALLOW_COPY_AND_ASSIGN(DBusManager);
80};
81
82} // namespace shill
83
84#endif // SHILL_DBUS_MANAGER_H_