blob: c231bc4e83b5e4fdfd088e7f474d5a3093039575 [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>
Ben Chand1fce552014-10-17 01:12:52 -070010#include <memory>
Darin Petkov002c58e2012-06-19 02:56:05 +020011#include <string>
12
Darin Petkov002c58e2012-06-19 02:56:05 +020013#include <base/callback.h>
Darin Petkov2b8e44e2012-06-25 15:13:26 +020014#include <base/cancelable_callback.h>
Ben Chancc67c522014-09-03 07:19:18 -070015#include <base/macros.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
Peter Qiu608ec292015-07-30 15:46:16 -070023class ControlInterface;
Darin Petkov002c58e2012-06-19 02:56:05 +020024class DBusServiceProxyInterface;
25class Error;
Darin Petkov002c58e2012-06-19 02:56:05 +020026
Ben Chan084faca2013-07-02 14:25:12 -070027class DBusManager : public base::SupportsWeakPtr<DBusManager> {
Darin Petkov002c58e2012-06-19 02:56:05 +020028 public:
Peter Qiu608ec292015-07-30 15:46:16 -070029 explicit DBusManager(ControlInterface* control_interface);
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.
Paul Stewarta794cd62015-06-16 13:13:10 -070045 virtual DBusNameWatcher* CreateNameWatcher(
46 const std::string& name,
47 const DBusNameWatcher::NameAppearedCallback& name_appeared_callback,
48 const DBusNameWatcher::NameVanishedCallback& name_vanished_callback);
Ben Chan084faca2013-07-02 14:25:12 -070049
50 // Deregisters and removes the watcher such that it stops monitoring the
51 // associated DBus service name.
Paul Stewarta794cd62015-06-16 13:13:10 -070052 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;
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070058 friend class PowerManagerTest;
Ben Chan084faca2013-07-02 14:25:12 -070059 friend class WiFiObjectTest;
60 friend class WiMaxProviderTest;
Darin Petkov002c58e2012-06-19 02:56:05 +020061 FRIEND_TEST(DBusManagerTest, NameWatchers);
Ben Chan084faca2013-07-02 14:25:12 -070062 FRIEND_TEST(WiMaxProviderTest, StartStop);
Ben Chan66174a12014-01-08 21:27:00 -080063 FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish);
Darin Petkov002c58e2012-06-19 02:56:05 +020064
Paul Stewarta794cd62015-06-16 13:13:10 -070065 void OnNameOwnerChanged(const std::string& name,
66 const std::string& old_owner,
67 const std::string& new_owner);
Darin Petkov002c58e2012-06-19 02:56:05 +020068
Ben Chan084faca2013-07-02 14:25:12 -070069 void OnGetNameOwnerComplete(
Paul Stewarta794cd62015-06-16 13:13:10 -070070 const base::WeakPtr<DBusNameWatcher>& name_watcher,
71 const std::string& unique_name,
72 const Error& error);
Darin Petkov002c58e2012-06-19 02:56:05 +020073
Peter Qiu608ec292015-07-30 15:46:16 -070074 ControlInterface* control_interface_;
Darin Petkov002c58e2012-06-19 02:56:05 +020075
Ben Chand1fce552014-10-17 01:12:52 -070076 std::unique_ptr<DBusServiceProxyInterface> proxy_;
Darin Petkov002c58e2012-06-19 02:56:05 +020077
Paul Stewarta794cd62015-06-16 13:13:10 -070078 std::map<std::string, std::list<DBusNameWatcher*>> name_watchers_;
Darin Petkov002c58e2012-06-19 02:56:05 +020079
80 DISALLOW_COPY_AND_ASSIGN(DBusManager);
81};
82
83} // namespace shill
84
85#endif // SHILL_DBUS_MANAGER_H_