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.
5 #ifndef CHROMEOS_NETWORK_SHILL_PROPERTY_HANDLER_H_
6 #define CHROMEOS_NETWORK_SHILL_PROPERTY_HANDLER_H_
13 #include "base/memory/weak_ptr.h"
14 #include "chromeos/dbus/dbus_method_call_status.h"
15 #include "chromeos/dbus/shill_property_changed_observer.h"
16 #include "chromeos/network/managed_state.h"
17 #include "chromeos/network/network_handler_callbacks.h"
20 class DictionaryValue
;
27 class ShillManagerClient
;
31 class ShillPropertyObserver
;
33 // This class handles Shill calls and observers to reflect the state of the
34 // Shill Manager and its services and devices. It observes Shill.Manager and
35 // requests properties for new devices/networks. It takes a Listener in its
36 // constructor (e.g. NetworkStateHandler) that it calls when properties change
37 // (including once to set their initial state after Init() gets called).
38 // It also observes Shill.Service for all services in Manager.ServiceWatchList.
39 // This class must not outlive the ShillManagerClient instance.
40 class CHROMEOS_EXPORT ShillPropertyHandler
41 : public ShillPropertyChangedObserver
,
42 public base::SupportsWeakPtr
<ShillPropertyHandler
> {
44 typedef std::map
<std::string
, ShillPropertyObserver
*>
45 ShillPropertyObserverMap
;
47 class CHROMEOS_EXPORT Listener
{
49 // Called when the entries in a managed list have changed.
50 virtual void UpdateManagedList(ManagedState::ManagedType type
,
51 const base::ListValue
& entries
) = 0;
53 // Called when the properties for a managed state have changed.
54 virtual void UpdateManagedStateProperties(
55 ManagedState::ManagedType type
,
56 const std::string
& path
,
57 const base::DictionaryValue
& properties
) = 0;
59 // Called when the list of profiles changes.
60 virtual void ProfileListChanged() = 0;
62 // Called when a property for a watched network service has changed.
63 virtual void UpdateNetworkServiceProperty(
64 const std::string
& service_path
,
65 const std::string
& key
,
66 const base::Value
& value
) = 0;
68 // Called when a property for a watched device has changed.
69 virtual void UpdateDeviceProperty(
70 const std::string
& device_path
,
71 const std::string
& key
,
72 const base::Value
& value
) = 0;
74 // Called when the list of devices with portal check enabled changes.
75 virtual void CheckPortalListChanged(
76 const std::string
& check_portal_list
) = 0;
78 // Called when one or more manager properties (e.g. a technology list)
80 virtual void NotifyManagerPropertyChanged() = 0;
82 // Called when a managed state list has changed, after properties for any
83 // new entries in the list have been received and
84 // UpdateManagedStateProperties has been called for each new entry.
85 virtual void ManagedStateListChanged(ManagedState::ManagedType type
) = 0;
88 virtual ~Listener() {}
91 explicit ShillPropertyHandler(Listener
* listener
);
92 virtual ~ShillPropertyHandler();
94 // Sends an initial property request and sets up the observer.
97 // Returns true if |technology| is available, enabled, etc.
98 bool IsTechnologyAvailable(const std::string
& technology
) const;
99 bool IsTechnologyEnabled(const std::string
& technology
) const;
100 bool IsTechnologyEnabling(const std::string
& technology
) const;
101 bool IsTechnologyUninitialized(const std::string
& technology
) const;
103 // Asynchronously sets the enabled state for |technology|.
104 // Note: Modifes Manager state. Calls |error_callback| on failure.
105 void SetTechnologyEnabled(
106 const std::string
& technology
,
108 const network_handler::ErrorCallback
& error_callback
);
110 // Sets the list of devices on which portal check is enabled.
111 void SetCheckPortalList(const std::string
& check_portal_list
);
113 // Requests an immediate network scan.
114 void RequestScan() const;
116 // Calls Manager.ConnectToBestServices().
117 void ConnectToBestServices() const;
119 // Requests all properties for the service or device (called for new items).
120 void RequestProperties(ManagedState::ManagedType type
,
121 const std::string
& path
);
123 // Returns true if |service_path| is being observed.
124 bool IsObservingNetwork(const std::string
& service_path
) {
125 return observed_networks_
.count(service_path
) != 0;
128 // ShillPropertyChangedObserver overrides
129 virtual void OnPropertyChanged(const std::string
& key
,
130 const base::Value
& value
) OVERRIDE
;
133 // Callback for dbus method fetching properties.
134 void ManagerPropertiesCallback(DBusMethodCallStatus call_status
,
135 const base::DictionaryValue
& properties
);
136 // Called form OnPropertyChanged() and ManagerPropertiesCallback().
137 // Returns true if observers should be notified.
138 bool ManagerPropertyChanged(const std::string
& key
,
139 const base::Value
& value
);
141 // Updates the Shill property observers to observe any entries for |type|.
142 void UpdateObserved(ManagedState::ManagedType type
,
143 const base::ListValue
& entries
);
146 // Sets |*_technologies_| to contain only entries in |technologies|.
147 void UpdateAvailableTechnologies(const base::ListValue
& technologies
);
148 void UpdateEnabledTechnologies(const base::ListValue
& technologies
);
149 void UpdateUninitializedTechnologies(const base::ListValue
& technologies
);
151 void EnableTechnologyFailed(
152 const std::string
& technology
,
153 const network_handler::ErrorCallback
& error_callback
,
154 const std::string
& error_name
,
155 const std::string
& error_message
);
157 // Called when Shill returns the properties for a service or device.
158 void GetPropertiesCallback(ManagedState::ManagedType type
,
159 const std::string
& path
,
160 DBusMethodCallStatus call_status
,
161 const base::DictionaryValue
& properties
);
163 // Callback invoked when a watched property changes. Calls appropriate
164 // handlers and signals observers.
165 void PropertyChangedCallback(ManagedState::ManagedType type
,
166 const std::string
& path
,
167 const std::string
& key
,
168 const base::Value
& value
);
169 void NetworkServicePropertyChangedCallback(const std::string
& path
,
170 const std::string
& key
,
171 const base::Value
& value
);
172 void NetworkDevicePropertyChangedCallback(const std::string
& path
,
173 const std::string
& key
,
174 const base::Value
& value
);
176 // Callback for getting the IPConfig property of a Network. Handled here
177 // instead of in NetworkState so that all asynchronous requests are done
178 // in a single place (also simplifies NetworkState considerably).
179 void GetIPConfigCallback(const std::string
& service_path
,
180 DBusMethodCallStatus call_status
,
181 const base::DictionaryValue
& properties
);
183 // Pointer to containing class (owns this)
186 // Convenience pointer for ShillManagerClient
187 ShillManagerClient
* shill_manager_
;
189 // Pending update list for each managed state type
190 std::map
<ManagedState::ManagedType
, std::set
<std::string
> > pending_updates_
;
192 // List of network services with Shill property changed observers
193 ShillPropertyObserverMap observed_networks_
;
195 // List of network devices with Shill property changed observers
196 ShillPropertyObserverMap observed_devices_
;
198 // Lists of available / enabled / uninitialized technologies
199 std::set
<std::string
> available_technologies_
;
200 std::set
<std::string
> enabled_technologies_
;
201 std::set
<std::string
> enabling_technologies_
;
202 std::set
<std::string
> uninitialized_technologies_
;
204 DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandler
);
207 } // namespace internal
208 } // namespace chromeos
210 #endif // CHROMEOS_NETWORK_SHILL_PROPERTY_HANDLER_H_