Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chromeos / network / shill_property_handler.h
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
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"
18
19 namespace base {
20 class DictionaryValue;
21 class ListValue;
22 class Value;
23 }
24
25 namespace chromeos {
26
27 class ShillManagerClient;
28
29 namespace internal {
30
31 class ShillPropertyObserver;
32
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> {
43  public:
44   typedef std::map<std::string, ShillPropertyObserver*>
45       ShillPropertyObserverMap;
46
47   class CHROMEOS_EXPORT Listener {
48    public:
49     // Called when the entries in a managed list have changed.
50     virtual void UpdateManagedList(ManagedState::ManagedType type,
51                                    const base::ListValue& entries) = 0;
52
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;
58
59     // Called when the list of profiles changes.
60     virtual void ProfileListChanged() = 0;
61
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;
67
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;
73
74     // Called when a watched network or device IPConfig property changes.
75     virtual void UpdateIPConfigProperties(
76         ManagedState::ManagedType type,
77         const std::string& path,
78         const std::string& ip_config_path,
79         const base::DictionaryValue& properties) = 0;
80
81     // Called when the list of devices with portal check enabled changes.
82     virtual void CheckPortalListChanged(
83          const std::string& check_portal_list) = 0;
84
85     // Called when a technology list changes.
86     virtual void TechnologyListChanged() = 0;
87
88     // Called when a managed state list has changed, after properties for any
89     // new entries in the list have been received and
90     // UpdateManagedStateProperties has been called for each new entry.
91     virtual void ManagedStateListChanged(ManagedState::ManagedType type) = 0;
92
93     // Called when the default network service changes.
94     virtual void DefaultNetworkServiceChanged(
95         const std::string& service_path) = 0;
96
97    protected:
98     virtual ~Listener() {}
99   };
100
101   explicit ShillPropertyHandler(Listener* listener);
102   virtual ~ShillPropertyHandler();
103
104   // Sets up the observer and calls UpdateManagerProperties().
105   void Init();
106
107   // Requests all Manager properties. Called from Init() and any time
108   // properties that do not signal changes might have been updated (e.g.
109   // ServiceCompleteList).
110   void UpdateManagerProperties();
111
112   // Returns true if |technology| is available, enabled, etc.
113   bool IsTechnologyAvailable(const std::string& technology) const;
114   bool IsTechnologyEnabled(const std::string& technology) const;
115   bool IsTechnologyEnabling(const std::string& technology) const;
116   bool IsTechnologyUninitialized(const std::string& technology) const;
117
118   // Asynchronously sets the enabled state for |technology|.
119   // Note: Modifies Manager state. Calls |error_callback| on failure.
120   void SetTechnologyEnabled(
121       const std::string& technology,
122       bool enabled,
123       const network_handler::ErrorCallback& error_callback);
124
125   // Sets the list of devices on which portal check is enabled.
126   void SetCheckPortalList(const std::string& check_portal_list);
127
128   // Sets the Manager.WakeOnLan property. Note: we do not track this state, we
129   // only set it.
130   void SetWakeOnLanEnabled(bool enabled);
131
132   // Requests an immediate network scan.
133   void RequestScan() const;
134
135   // Calls Manager.ConnectToBestServices().
136   void ConnectToBestServices() const;
137
138   // Requests all properties for the service or device (called for new items).
139   void RequestProperties(ManagedState::ManagedType type,
140                          const std::string& path);
141
142   // ShillPropertyChangedObserver overrides
143   virtual void OnPropertyChanged(const std::string& key,
144                                  const base::Value& value) override;
145
146  private:
147   typedef std::map<ManagedState::ManagedType, std::set<std::string> >
148       TypeRequestMap;
149
150   // Callback for dbus method fetching properties.
151   void ManagerPropertiesCallback(DBusMethodCallStatus call_status,
152                                  const base::DictionaryValue& properties);
153
154   // Notifies the listener when a ManagedStateList has changed and all pending
155   // updates have been received. |key| can either identify the list that
156   // has changed or an empty string if multiple lists may have changed.
157   void CheckPendingStateListUpdates(const std::string& key);
158
159   // Called form OnPropertyChanged() and ManagerPropertiesCallback().
160   void ManagerPropertyChanged(const std::string& key,
161                               const base::Value& value);
162
163   // Requests properties for new entries in the list for |type|.
164   void UpdateProperties(ManagedState::ManagedType type,
165                         const base::ListValue& entries);
166
167   // Updates the Shill property observers to observe any entries for |type|.
168   void UpdateObserved(ManagedState::ManagedType type,
169                       const base::ListValue& entries);
170
171
172   // Sets |*_technologies_| to contain only entries in |technologies|.
173   void UpdateAvailableTechnologies(const base::ListValue& technologies);
174   void UpdateEnabledTechnologies(const base::ListValue& technologies);
175   void UpdateUninitializedTechnologies(const base::ListValue& technologies);
176
177   void EnableTechnologyFailed(
178       const std::string& technology,
179       const network_handler::ErrorCallback& error_callback,
180       const std::string& dbus_error_name,
181       const std::string& dbus_error_message);
182
183   // Called when Shill returns the properties for a service or device.
184   void GetPropertiesCallback(ManagedState::ManagedType type,
185                              const std::string& path,
186                              DBusMethodCallStatus call_status,
187                              const base::DictionaryValue& properties);
188
189   // Callback invoked when a watched property changes. Calls appropriate
190   // handlers and signals observers.
191   void PropertyChangedCallback(ManagedState::ManagedType type,
192                                const std::string& path,
193                                const std::string& key,
194                                const base::Value& value);
195
196   // Request a single IPConfig object corresponding to |ip_config_path_value|
197   // from Shill.IPConfigClient and trigger a call to UpdateIPConfigProperties
198   // for the network or device corresponding to |type| and |path|.
199   void RequestIPConfig(ManagedState::ManagedType type,
200                        const std::string& path,
201                        const base::Value& ip_config_path_value);
202
203   // Request the IPConfig objects corresponding to entries in
204   // |ip_config_list_value| from Shill.IPConfigClient and trigger a call to
205   // UpdateIPConfigProperties with each object for the network or device
206   // corresponding to |type| and |path|.
207   void RequestIPConfigsList(ManagedState::ManagedType type,
208                             const std::string& path,
209                             const base::Value& ip_config_list_value);
210
211   // Callback for getting the IPConfig property of a network or device. Handled
212   // here instead of in NetworkState so that all asynchronous requests are done
213   // in a single place (also simplifies NetworkState considerably).
214   void GetIPConfigCallback(ManagedState::ManagedType type,
215                            const std::string& path,
216                            const std::string& ip_config_path,
217                            DBusMethodCallStatus call_status,
218                            const base::DictionaryValue& properties);
219
220   // Pointer to containing class (owns this)
221   Listener* listener_;
222
223   // Convenience pointer for ShillManagerClient
224   ShillManagerClient* shill_manager_;
225
226   // Pending update list for each managed state type
227   TypeRequestMap pending_updates_;
228
229   // List of states for which properties have been requested, for each managed
230   // state type
231   TypeRequestMap requested_updates_;
232
233   // List of network services with Shill property changed observers
234   ShillPropertyObserverMap observed_networks_;
235
236   // List of network devices with Shill property changed observers
237   ShillPropertyObserverMap observed_devices_;
238
239   // Lists of available / enabled / uninitialized technologies
240   std::set<std::string> available_technologies_;
241   std::set<std::string> enabled_technologies_;
242   std::set<std::string> enabling_technologies_;
243   std::set<std::string> uninitialized_technologies_;
244
245   DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandler);
246 };
247
248 }  // namespace internal
249 }  // namespace chromeos
250
251 #endif  // CHROMEOS_NETWORK_SHILL_PROPERTY_HANDLER_H_