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