blob: adab5c23ce62be1b971b10dea555eba15d21aa21 [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>
16#include <gtest/gtest_prod.h> // for FRIEND_TEST
17
18namespace shill {
19
20class DBusServiceProxyInterface;
21class Error;
22class ProxyFactory;
23
24class DBusManager {
25 public:
26 typedef base::Callback<void(const std::string &owner)> AppearedCallback;
27 typedef base::Closure VanishedCallback;
Darin Petkov2b8e44e2012-06-25 15:13:26 +020028 typedef base::CancelableCallback<void(
29 const std::string &owner)> CancelableAppearedCallback;
30 typedef base::CancelableClosure CancelableVanishedCallback;
Darin Petkov002c58e2012-06-19 02:56:05 +020031
32 DBusManager();
Darin Petkov2b8e44e2012-06-25 15:13:26 +020033 virtual ~DBusManager();
Darin Petkov002c58e2012-06-19 02:56:05 +020034
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 Petkov2b8e44e2012-06-25 15:13:26 +020043 virtual void WatchName(const std::string &name,
44 const AppearedCallback &on_appear,
45 const VanishedCallback &on_vanish);
Darin Petkov002c58e2012-06-19 02:56:05 +020046
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_