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