Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 1 | // 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 5 | #ifndef PATCHPANEL_SHILL_CLIENT_H_ |
| 6 | #define PATCHPANEL_SHILL_CLIENT_H_ |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 9 | #include <set> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 10 | #include <string> |
Garrick Evans | c7fea0a | 2020-02-04 10:46:42 +0900 | [diff] [blame] | 11 | #include <vector> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 12 | |
| 13 | #include <base/macros.h> |
| 14 | #include <base/memory/weak_ptr.h> |
| 15 | #include <shill/dbus-proxies.h> |
| 16 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 17 | namespace patchpanel { |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 18 | |
| 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. |
| 21 | class ShillClient { |
| 22 | public: |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 23 | using DefaultInterfaceChangeHandler = base::Callback<void( |
| 24 | const std::string& new_ifname, const std::string& prev_ifname)>; |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 25 | using DevicesChangeHandler = |
| 26 | base::Callback<void(const std::set<std::string>& added, |
| 27 | const std::set<std::string>& removed)>; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 28 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 29 | explicit ShillClient(const scoped_refptr<dbus::Bus>& bus); |
Ben Chan | 4f38650 | 2019-09-20 16:17:59 -0700 | [diff] [blame] | 30 | virtual ~ShillClient() = default; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 31 | |
| 32 | void RegisterDefaultInterfaceChangedHandler( |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 33 | const DefaultInterfaceChangeHandler& handler); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 34 | |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 35 | void RegisterDevicesChangedHandler(const DevicesChangeHandler& handler); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 36 | |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 37 | void ScanDevices(const DevicesChangeHandler& handler); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 38 | |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 39 | // Returns the cached interface name; does not initiate a property fetch. |
| 40 | virtual const std::string& default_interface() const; |
Hugo Benichi | cc6850f | 2020-01-17 13:26:06 +0900 | [diff] [blame] | 41 | // 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 Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 45 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 46 | 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 Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 53 | // 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 Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 56 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 57 | private: |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 58 | void UpdateDevices(const brillo::Any& property_value); |
| 59 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 60 | // 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 Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 63 | // instead. Returns the previous default interface. |
| 64 | std::string SetDefaultInterface(std::string new_default); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 65 | |
| 66 | // Tracks the name of the system default interface chosen by shill. |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 67 | std::string default_interface_; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 68 | // 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 Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 74 | std::vector<DefaultInterfaceChangeHandler> default_interface_handlers_; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 75 | // Called when the list of network interfaces managed by shill changes. |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 76 | std::vector<DevicesChangeHandler> device_handlers_; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 77 | |
| 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 Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 83 | DISALLOW_COPY_AND_ASSIGN(ShillClient); |
| 84 | }; |
| 85 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 86 | } // namespace patchpanel |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 87 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 88 | #endif // PATCHPANEL_SHILL_CLIENT_H_ |