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