shill_property_handler.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 one or more manager properties (e.g. a technology list)
75    // changes.
76    virtual void ManagerPropertyChanged() = 0;
77
78    // Called when a managed state list has changed, after properties for any
79    // new entries in the list have been received and
80    // UpdateManagedStateProperties has been called for each new entry.
81    virtual void ManagedStateListChanged(ManagedState::ManagedType type) = 0;
82
83   protected:
84    virtual ~Listener() {}
85  };
86
87  explicit ShillPropertyHandler(Listener* listener);
88  virtual ~ShillPropertyHandler();
89
90  // Sends an initial property request and sets up the observer.
91  void Init();
92
93  // Returns true if |technology| is available, enabled, etc.
94  bool IsTechnologyAvailable(const std::string& technology) const;
95  bool IsTechnologyEnabled(const std::string& technology) const;
96  bool IsTechnologyEnabling(const std::string& technology) const;
97  bool IsTechnologyUninitialized(const std::string& technology) const;
98
99  // Asynchronously sets the enabled state for |technology|.
100  // Note: Modifes Manager state. Calls |error_callback| on failure.
101  void SetTechnologyEnabled(
102      const std::string& technology,
103      bool enabled,
104      const network_handler::ErrorCallback& error_callback);
105
106  // Requests an immediate network scan.
107  void RequestScan() const;
108
109  // Calls Manager.ConnectToBestServices().
110  void ConnectToBestServices() const;
111
112  // Requests all properties for the service or device (called for new items).
113  void RequestProperties(ManagedState::ManagedType type,
114                         const std::string& path);
115
116  // Returns true if |service_path| is being observed.
117  bool IsObservingNetwork(const std::string& service_path) {
118    return observed_networks_.count(service_path) != 0;
119  }
120
121  // ShillPropertyChangedObserver overrides
122  virtual void OnPropertyChanged(const std::string& key,
123                                 const base::Value& value) OVERRIDE;
124
125 private:
126  // Callback for dbus method fetching properties.
127  void ManagerPropertiesCallback(DBusMethodCallStatus call_status,
128                                 const base::DictionaryValue& properties);
129  // Called form OnPropertyChanged() and ManagerPropertiesCallback().
130  // Returns true if observers should be notified.
131  bool ManagerPropertyChanged(const std::string& key, const base::Value& value);
132
133  // Updates the Shill property observers to observe any entries for |type|.
134  void UpdateObserved(ManagedState::ManagedType type,
135                      const base::ListValue& entries);
136
137
138  // Sets |*_technologies_| to contain only entries in |technologies|.
139  void UpdateAvailableTechnologies(const base::ListValue& technologies);
140  void UpdateEnabledTechnologies(const base::ListValue& technologies);
141  void UpdateUninitializedTechnologies(const base::ListValue& technologies);
142
143  void EnableTechnologyFailed(
144      const std::string& technology,
145      const network_handler::ErrorCallback& error_callback,
146      const std::string& error_name,
147      const std::string& error_message);
148
149  // Called when Shill returns the properties for a service or device.
150  void GetPropertiesCallback(ManagedState::ManagedType type,
151                             const std::string& path,
152                             DBusMethodCallStatus call_status,
153                             const base::DictionaryValue& properties);
154
155  // Callback invoked when a watched property changes. Calls appropriate
156  // handlers and signals observers.
157  void PropertyChangedCallback(ManagedState::ManagedType type,
158                               const std::string& path,
159                               const std::string& key,
160                               const base::Value& value);
161  void NetworkServicePropertyChangedCallback(const std::string& path,
162                                             const std::string& key,
163                                             const base::Value& value);
164  void NetworkDevicePropertyChangedCallback(const std::string& path,
165                                            const std::string& key,
166                                            const base::Value& value);
167
168  // Callback for getting the IPConfig property of a Network. Handled here
169  // instead of in NetworkState so that all asynchronous requests are done
170  // in a single place (also simplifies NetworkState considerably).
171  void GetIPConfigCallback(const std::string& service_path,
172                           DBusMethodCallStatus call_status,
173                           const base::DictionaryValue& properties);
174
175  // Pointer to containing class (owns this)
176  Listener* listener_;
177
178  // Convenience pointer for ShillManagerClient
179  ShillManagerClient* shill_manager_;
180
181  // Pending update list for each managed state type
182  std::map<ManagedState::ManagedType, std::set<std::string> > pending_updates_;
183
184  // List of network services with Shill property changed observers
185  ShillPropertyObserverMap observed_networks_;
186
187  // List of network devices with Shill property changed observers
188  ShillPropertyObserverMap observed_devices_;
189
190  // Lists of available / enabled / uninitialized technologies
191  std::set<std::string> available_technologies_;
192  std::set<std::string> enabled_technologies_;
193  std::set<std::string> enabling_technologies_;
194  std::set<std::string> uninitialized_technologies_;
195
196  DISALLOW_COPY_AND_ASSIGN(ShillPropertyHandler);
197};
198
199}  // namespace internal
200}  // namespace chromeos
201
202#endif  // CHROMEOS_NETWORK_SHILL_PROPERTY_HANDLER_H_
203