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