[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium 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 | |
[email protected] | b7c377e | 2013-01-15 10:00:39 | [diff] [blame] | 5 | #ifndef CHROMEOS_NETWORK_SHILL_PROPERTY_HANDLER_H_ |
| 6 | #define CHROMEOS_NETWORK_SHILL_PROPERTY_HANDLER_H_ |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 7 | |
| 8 | #include <list> |
| 9 | #include <map> |
| 10 | #include <set> |
| 11 | #include <string> |
fqj | fee5067a | 2015-11-13 20:12:59 | [diff] [blame] | 12 | #include <vector> |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 13 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
| 16 | #include "chromeos/dbus/dbus_method_call_status.h" |
| 17 | #include "chromeos/dbus/shill_property_changed_observer.h" |
| 18 | #include "chromeos/network/managed_state.h" |
[email protected] | eb052c9 | 2012-11-26 20:12:21 | [diff] [blame] | 19 | #include "chromeos/network/network_handler_callbacks.h" |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | class DictionaryValue; |
| 23 | class ListValue; |
| 24 | class Value; |
| 25 | } |
| 26 | |
| 27 | namespace chromeos { |
| 28 | |
| 29 | class ShillManagerClient; |
| 30 | |
| 31 | namespace internal { |
| 32 | |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 33 | class ShillPropertyObserver; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 34 | |
| 35 | // This class handles Shill calls and observers to reflect the state of the |
| 36 | // Shill Manager and its services and devices. It observes Shill.Manager and |
| 37 | // requests properties for new devices/networks. It takes a Listener in its |
| 38 | // constructor (e.g. NetworkStateHandler) that it calls when properties change |
| 39 | // (including once to set their initial state after Init() gets called). |
| 40 | // It also observes Shill.Service for all services in Manager.ServiceWatchList. |
| 41 | // This class must not outlive the ShillManagerClient instance. |
| 42 | class CHROMEOS_EXPORT ShillPropertyHandler |
[email protected] | 8617f98 | 2013-04-01 21:50:22 | [diff] [blame] | 43 | : public ShillPropertyChangedObserver, |
| 44 | public base::SupportsWeakPtr<ShillPropertyHandler> { |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 45 | public: |
avi | 8194ad6 | 2016-09-20 16:58:36 | [diff] [blame] | 46 | typedef std::map<std::string, std::unique_ptr<ShillPropertyObserver>> |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 47 | ShillPropertyObserverMap; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 48 | |
| 49 | class CHROMEOS_EXPORT Listener { |
| 50 | public: |
| 51 | // Called when the entries in a managed list have changed. |
| 52 | virtual void UpdateManagedList(ManagedState::ManagedType type, |
| 53 | const base::ListValue& entries) = 0; |
| 54 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 55 | // Called when the properties for a managed state have changed. |
| 56 | virtual void UpdateManagedStateProperties( |
| 57 | ManagedState::ManagedType type, |
| 58 | const std::string& path, |
| 59 | const base::DictionaryValue& properties) = 0; |
| 60 | |
[email protected] | 4a3ed97 | 2013-04-11 22:03:23 | [diff] [blame] | 61 | // Called when the list of profiles changes. |
| 62 | virtual void ProfileListChanged() = 0; |
| 63 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 64 | // Called when a property for a watched network service has changed. |
| 65 | virtual void UpdateNetworkServiceProperty( |
| 66 | const std::string& service_path, |
| 67 | const std::string& key, |
| 68 | const base::Value& value) = 0; |
| 69 | |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 70 | // Called when a property for a watched device has changed. |
| 71 | virtual void UpdateDeviceProperty( |
| 72 | const std::string& device_path, |
| 73 | const std::string& key, |
| 74 | const base::Value& value) = 0; |
| 75 | |
[email protected] | 0e7085a | 2014-05-09 16:45:46 | [diff] [blame] | 76 | // Called when a watched network or device IPConfig property changes. |
| 77 | virtual void UpdateIPConfigProperties( |
| 78 | ManagedState::ManagedType type, |
| 79 | const std::string& path, |
| 80 | const std::string& ip_config_path, |
| 81 | const base::DictionaryValue& properties) = 0; |
| 82 | |
[email protected] | 68196e45 | 2013-05-31 01:16:38 | [diff] [blame] | 83 | // Called when the list of devices with portal check enabled changes. |
| 84 | virtual void CheckPortalListChanged( |
| 85 | const std::string& check_portal_list) = 0; |
| 86 | |
[email protected] | e925cfd | 2013-09-13 18:38:37 | [diff] [blame] | 87 | // Called when a technology list changes. |
| 88 | virtual void TechnologyListChanged() = 0; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 89 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 90 | // Called when a managed state list has changed, after properties for any |
| 91 | // new entries in the list have been received and |
| 92 | // UpdateManagedStateProperties has been called for each new entry. |
| 93 | virtual void ManagedStateListChanged(ManagedState::ManagedType type) = 0; |
| 94 | |
[email protected] | 4d201ec | 2014-03-05 19:54:40 | [diff] [blame] | 95 | // Called when the default network service changes. |
| 96 | virtual void DefaultNetworkServiceChanged( |
| 97 | const std::string& service_path) = 0; |
| 98 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 99 | protected: |
| 100 | virtual ~Listener() {} |
| 101 | }; |
| 102 | |
| 103 | explicit ShillPropertyHandler(Listener* listener); |
dcheng | aeb064e | 2015-01-20 20:56:38 | [diff] [blame] | 104 | ~ShillPropertyHandler() override; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 105 | |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 106 | // Sets up the observer and calls UpdateManagerProperties(). |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 107 | void Init(); |
| 108 | |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 109 | // Requests all Manager properties. Called from Init() and any time |
| 110 | // properties that do not signal changes might have been updated (e.g. |
| 111 | // ServiceCompleteList). |
| 112 | void UpdateManagerProperties(); |
| 113 | |
[email protected] | b15f493 | 2013-04-15 21:09:25 | [diff] [blame] | 114 | // Returns true if |technology| is available, enabled, etc. |
| 115 | bool IsTechnologyAvailable(const std::string& technology) const; |
| 116 | bool IsTechnologyEnabled(const std::string& technology) const; |
| 117 | bool IsTechnologyEnabling(const std::string& technology) const; |
fqj | 9bbb6b3f | 2015-11-17 22:41:53 | [diff] [blame] | 118 | bool IsTechnologyProhibited(const std::string& technology) const; |
[email protected] | b15f493 | 2013-04-15 21:09:25 | [diff] [blame] | 119 | bool IsTechnologyUninitialized(const std::string& technology) const; |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 120 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 121 | // Asynchronously sets the enabled state for |technology|. |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 122 | // Note: Modifies Manager state. Calls |error_callback| on failure. |
[email protected] | eb052c9 | 2012-11-26 20:12:21 | [diff] [blame] | 123 | void SetTechnologyEnabled( |
| 124 | const std::string& technology, |
| 125 | bool enabled, |
| 126 | const network_handler::ErrorCallback& error_callback); |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 127 | |
fqj | fee5067a | 2015-11-13 20:12:59 | [diff] [blame] | 128 | // Asynchronously sets the prohibited state for every network technology |
| 129 | // listed in |technologies|. Note: Modifies Manager state. Calls |
| 130 | // |error_callback| on failure. |
| 131 | void SetProhibitedTechnologies( |
| 132 | const std::vector<std::string>& technologies, |
| 133 | const network_handler::ErrorCallback& error_callback); |
| 134 | |
[email protected] | 68196e45 | 2013-05-31 01:16:38 | [diff] [blame] | 135 | // Sets the list of devices on which portal check is enabled. |
| 136 | void SetCheckPortalList(const std::string& check_portal_list); |
| 137 | |
stevenjb | 19d2337 | 2014-10-31 20:27:21 | [diff] [blame] | 138 | // Sets the Manager.WakeOnLan property. Note: we do not track this state, we |
| 139 | // only set it. |
| 140 | void SetWakeOnLanEnabled(bool enabled); |
| 141 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 142 | // Requests an immediate network scan. |
| 143 | void RequestScan() const; |
| 144 | |
| 145 | // Requests all properties for the service or device (called for new items). |
| 146 | void RequestProperties(ManagedState::ManagedType type, |
| 147 | const std::string& path); |
| 148 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 149 | // ShillPropertyChangedObserver overrides |
dcheng | aeb064e | 2015-01-20 20:56:38 | [diff] [blame] | 150 | void OnPropertyChanged(const std::string& key, |
| 151 | const base::Value& value) override; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 152 | |
| 153 | private: |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 154 | typedef std::map<ManagedState::ManagedType, std::set<std::string> > |
| 155 | TypeRequestMap; |
| 156 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 157 | // Callback for dbus method fetching properties. |
| 158 | void ManagerPropertiesCallback(DBusMethodCallStatus call_status, |
| 159 | const base::DictionaryValue& properties); |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 160 | |
| 161 | // Notifies the listener when a ManagedStateList has changed and all pending |
| 162 | // updates have been received. |key| can either identify the list that |
| 163 | // has changed or an empty string if multiple lists may have changed. |
| 164 | void CheckPendingStateListUpdates(const std::string& key); |
| 165 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 166 | // Called form OnPropertyChanged() and ManagerPropertiesCallback(). |
[email protected] | e925cfd | 2013-09-13 18:38:37 | [diff] [blame] | 167 | void ManagerPropertyChanged(const std::string& key, |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 168 | const base::Value& value); |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 169 | |
[email protected] | fa0336f | 2014-06-12 22:08:38 | [diff] [blame] | 170 | // Requests properties for new entries in the list for |type|. |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 171 | void UpdateProperties(ManagedState::ManagedType type, |
| 172 | const base::ListValue& entries); |
| 173 | |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 174 | // Updates the Shill property observers to observe any entries for |type|. |
| 175 | void UpdateObserved(ManagedState::ManagedType type, |
| 176 | const base::ListValue& entries); |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 177 | |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 178 | |
| 179 | // Sets |*_technologies_| to contain only entries in |technologies|. |
| 180 | void UpdateAvailableTechnologies(const base::ListValue& technologies); |
| 181 | void UpdateEnabledTechnologies(const base::ListValue& technologies); |
| 182 | void UpdateUninitializedTechnologies(const base::ListValue& technologies); |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 183 | |
[email protected] | b15f493 | 2013-04-15 21:09:25 | [diff] [blame] | 184 | void EnableTechnologyFailed( |
| 185 | const std::string& technology, |
| 186 | const network_handler::ErrorCallback& error_callback, |
[email protected] | 77ac540 | 2013-07-09 21:41:42 | [diff] [blame] | 187 | const std::string& dbus_error_name, |
| 188 | const std::string& dbus_error_message); |
[email protected] | b15f493 | 2013-04-15 21:09:25 | [diff] [blame] | 189 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 190 | // Called when Shill returns the properties for a service or device. |
| 191 | void GetPropertiesCallback(ManagedState::ManagedType type, |
| 192 | const std::string& path, |
| 193 | DBusMethodCallStatus call_status, |
| 194 | const base::DictionaryValue& properties); |
| 195 | |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 196 | // Callback invoked when a watched property changes. Calls appropriate |
| 197 | // handlers and signals observers. |
| 198 | void PropertyChangedCallback(ManagedState::ManagedType type, |
| 199 | const std::string& path, |
| 200 | const std::string& key, |
| 201 | const base::Value& value); |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 202 | |
[email protected] | 0e7085a | 2014-05-09 16:45:46 | [diff] [blame] | 203 | // Request a single IPConfig object corresponding to |ip_config_path_value| |
| 204 | // from Shill.IPConfigClient and trigger a call to UpdateIPConfigProperties |
| 205 | // for the network or device corresponding to |type| and |path|. |
| 206 | void RequestIPConfig(ManagedState::ManagedType type, |
| 207 | const std::string& path, |
| 208 | const base::Value& ip_config_path_value); |
| 209 | |
| 210 | // Request the IPConfig objects corresponding to entries in |
| 211 | // |ip_config_list_value| from Shill.IPConfigClient and trigger a call to |
| 212 | // UpdateIPConfigProperties with each object for the network or device |
| 213 | // corresponding to |type| and |path|. |
| 214 | void RequestIPConfigsList(ManagedState::ManagedType type, |
| 215 | const std::string& path, |
| 216 | const base::Value& ip_config_list_value); |
| 217 | |
| 218 | // Callback for getting the IPConfig property of a network or device. Handled |
| 219 | // here instead of in NetworkState so that all asynchronous requests are done |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 220 | // in a single place (also simplifies NetworkState considerably). |
[email protected] | 0e7085a | 2014-05-09 16:45:46 | [diff] [blame] | 221 | void GetIPConfigCallback(ManagedState::ManagedType type, |
| 222 | const std::string& path, |
| 223 | const std::string& ip_config_path, |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 224 | DBusMethodCallStatus call_status, |
| 225 | const base::DictionaryValue& properties); |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 226 | |
fqj | fee5067a | 2015-11-13 20:12:59 | [diff] [blame] | 227 | void SetProhibitedTechnologiesEnforced(bool enforced); |
| 228 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 229 | // Pointer to containing class (owns this) |
| 230 | Listener* listener_; |
| 231 | |
| 232 | // Convenience pointer for ShillManagerClient |
| 233 | ShillManagerClient* shill_manager_; |
| 234 | |
[email protected] | fa6797e | 2012-11-28 15:15:44 | [diff] [blame] | 235 | // Pending update list for each managed state type |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 236 | TypeRequestMap pending_updates_; |
| 237 | |
| 238 | // List of states for which properties have been requested, for each managed |
| 239 | // state type |
| 240 | TypeRequestMap requested_updates_; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 241 | |
| 242 | // List of network services with Shill property changed observers |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 243 | ShillPropertyObserverMap observed_networks_; |
| 244 | |
| 245 | // List of network devices with Shill property changed observers |
| 246 | ShillPropertyObserverMap observed_devices_; |
| 247 | |
| 248 | // Lists of available / enabled / uninitialized technologies |
| 249 | std::set<std::string> available_technologies_; |
| 250 | std::set<std::string> enabled_technologies_; |
[email protected] | b15f493 | 2013-04-15 21:09:25 | [diff] [blame] | 251 | std::set<std::string> enabling_technologies_; |
fqj | fee5067a | 2015-11-13 20:12:59 | [diff] [blame] | 252 | std::set<std::string> prohibited_technologies_; |
[email protected] | b16d79d7 | 2013-02-07 08:14:46 | [diff] [blame] | 253 | std::set<std::string> uninitialized_technologies_; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 254 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 255 | DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandler); |
| 256 | }; |
| 257 | |
| 258 | } // namespace internal |
| 259 | } // namespace chromeos |
| 260 | |
[email protected] | b7c377e | 2013-01-15 10:00:39 | [diff] [blame] | 261 | #endif // CHROMEOS_NETWORK_SHILL_PROPERTY_HANDLER_H_ |