blob: cf4f79dc08aae80bea533bb0ba7a603a63e17a51 [file] [log] [blame]
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07001// Copyright 2016 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
Garrick Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_SHILL_CLIENT_H_
6#define PATCHPANEL_SHILL_CLIENT_H_
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07007
8#include <memory>
Garrick Evans49879532018-12-03 13:15:36 +09009#include <set>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070010#include <string>
Garrick Evansc7fea0a2020-02-04 10:46:42 +090011#include <vector>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070012
13#include <base/macros.h>
14#include <base/memory/weak_ptr.h>
15#include <shill/dbus-proxies.h>
16
Garrick Evans3388a032020-03-24 11:25:55 +090017namespace patchpanel {
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070018
19// Listens for shill signals over dbus in order to figure out which
20// network interface (if any) is being used as the default service.
21class ShillClient {
22 public:
Garrick Evans1b1f67c2020-02-04 16:21:25 +090023 using DefaultInterfaceChangeHandler = base::Callback<void(
24 const std::string& new_ifname, const std::string& prev_ifname)>;
Garrick Evans139708f2020-02-06 14:38:59 +090025 using DevicesChangeHandler =
26 base::Callback<void(const std::set<std::string>& added,
27 const std::set<std::string>& removed)>;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090028
Garrick Evans08843932019-09-17 14:41:08 +090029 explicit ShillClient(const scoped_refptr<dbus::Bus>& bus);
Ben Chan4f386502019-09-20 16:17:59 -070030 virtual ~ShillClient() = default;
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070031
32 void RegisterDefaultInterfaceChangedHandler(
Garrick Evans139708f2020-02-06 14:38:59 +090033 const DefaultInterfaceChangeHandler& handler);
Garrick Evans49879532018-12-03 13:15:36 +090034
Garrick Evans139708f2020-02-06 14:38:59 +090035 void RegisterDevicesChangedHandler(const DevicesChangeHandler& handler);
Garrick Evans49879532018-12-03 13:15:36 +090036
Jie Jiang84c76a12020-04-17 16:45:20 +090037 void ScanDevices();
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070038
Garrick Evansbbdf4b42020-03-05 12:59:06 +090039 // Returns the cached interface name; does not initiate a property fetch.
40 virtual const std::string& default_interface() const;
Hugo Benichicc6850f2020-01-17 13:26:06 +090041 // Returns interface names of all known shill Devices.
42 const std::set<std::string> get_devices() const;
43 // Returns true if |ifname| is a known shill Device.
44 bool has_device(const std::string& ifname) const;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090045
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070046 protected:
47 void OnManagerPropertyChangeRegistration(const std::string& interface,
48 const std::string& signal_name,
49 bool success);
50 void OnManagerPropertyChange(const std::string& property_name,
51 const brillo::Any& property_value);
52
Hugo Benichiddee2812019-05-10 16:03:43 +090053 // Returns the name of the default interface for the system, or an empty
54 // string when the system has no default interface.
55 virtual std::string GetDefaultInterface();
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070056
Hugo Benichiddee2812019-05-10 16:03:43 +090057 private:
Garrick Evans139708f2020-02-06 14:38:59 +090058 void UpdateDevices(const brillo::Any& property_value);
59
Hugo Benichiddee2812019-05-10 16:03:43 +090060 // Sets the internal variable tracking the system default interface and calls
61 // the default interface handler if the default interface changed. When the
62 // default interface is lost and a fallback exists, the fallback is used
Garrick Evans1b1f67c2020-02-04 16:21:25 +090063 // instead. Returns the previous default interface.
64 std::string SetDefaultInterface(std::string new_default);
Hugo Benichiddee2812019-05-10 16:03:43 +090065
66 // Tracks the name of the system default interface chosen by shill.
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070067 std::string default_interface_;
Hugo Benichiddee2812019-05-10 16:03:43 +090068 // Another network interface on the system to use as a possible fallback if
69 // no system default interface exists.
70 std::string fallback_default_interface_;
71 // Tracks all network interfaces managed by shill.
72 std::set<std::string> devices_;
73 // Called when the interface used as the default interface changes.
Garrick Evans139708f2020-02-06 14:38:59 +090074 std::vector<DefaultInterfaceChangeHandler> default_interface_handlers_;
Hugo Benichiddee2812019-05-10 16:03:43 +090075 // Called when the list of network interfaces managed by shill changes.
Garrick Evans139708f2020-02-06 14:38:59 +090076 std::vector<DevicesChangeHandler> device_handlers_;
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070077
78 scoped_refptr<dbus::Bus> bus_;
79 std::unique_ptr<org::chromium::flimflam::ManagerProxy> manager_proxy_;
80
81 base::WeakPtrFactory<ShillClient> weak_factory_{this};
82
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070083 DISALLOW_COPY_AND_ASSIGN(ShillClient);
84};
85
Garrick Evans3388a032020-03-24 11:25:55 +090086} // namespace patchpanel
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070087
Garrick Evans3388a032020-03-24 11:25:55 +090088#endif // PATCHPANEL_SHILL_CLIENT_H_