1// Copyright 2013 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_MANAGED_NETWORK_CONFIGURATION_HANDLER_IMPL_H_
6#define CHROMEOS_NETWORK_MANAGED_NETWORK_CONFIGURATION_HANDLER_IMPL_H_
7
8#include <map>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/memory/linked_ptr.h"
14#include "base/memory/weak_ptr.h"
15#include "chromeos/chromeos_export.h"
16#include "chromeos/network/managed_network_configuration_handler.h"
17#include "chromeos/network/network_handler_callbacks.h"
18#include "chromeos/network/network_profile_observer.h"
19#include "chromeos/network/policy_applicator.h"
20
21namespace base {
22class DictionaryValue;
23}
24
25namespace chromeos {
26
27class NetworkConfigurationHandler;
28struct NetworkProfile;
29class NetworkProfileHandler;
30class NetworkStateHandler;
31
32class CHROMEOS_EXPORT ManagedNetworkConfigurationHandlerImpl
33    : public ManagedNetworkConfigurationHandler,
34      public NetworkProfileObserver,
35      public PolicyApplicator::ConfigurationHandler {
36 public:
37  virtual ~ManagedNetworkConfigurationHandlerImpl();
38
39  // ManagedNetworkConfigurationHandler overrides
40  virtual void AddObserver(NetworkPolicyObserver* observer) OVERRIDE;
41  virtual void RemoveObserver(NetworkPolicyObserver* observer) OVERRIDE;
42
43  virtual void GetProperties(
44      const std::string& service_path,
45      const network_handler::DictionaryResultCallback& callback,
46      const network_handler::ErrorCallback& error_callback) OVERRIDE;
47
48  virtual void GetManagedProperties(
49      const std::string& userhash,
50      const std::string& service_path,
51      const network_handler::DictionaryResultCallback& callback,
52      const network_handler::ErrorCallback& error_callback) OVERRIDE;
53
54  virtual void SetProperties(
55      const std::string& service_path,
56      const base::DictionaryValue& user_settings,
57      const base::Closure& callback,
58      const network_handler::ErrorCallback& error_callback) const OVERRIDE;
59
60  virtual void CreateConfiguration(
61      const std::string& userhash,
62      const base::DictionaryValue& properties,
63      const network_handler::StringResultCallback& callback,
64      const network_handler::ErrorCallback& error_callback) const OVERRIDE;
65
66  virtual void RemoveConfiguration(
67      const std::string& service_path,
68      const base::Closure& callback,
69      const network_handler::ErrorCallback& error_callback) const OVERRIDE;
70
71  virtual void SetPolicy(
72      onc::ONCSource onc_source,
73      const std::string& userhash,
74      const base::ListValue& network_configs_onc,
75      const base::DictionaryValue& global_network_config) OVERRIDE;
76
77  virtual const base::DictionaryValue* FindPolicyByGUID(
78      const std::string userhash,
79      const std::string& guid,
80      onc::ONCSource* onc_source) const OVERRIDE;
81
82  virtual const base::DictionaryValue* GetGlobalConfigFromPolicy(
83      const std::string userhash) const OVERRIDE;
84
85  virtual const base::DictionaryValue* FindPolicyByGuidAndProfile(
86      const std::string& guid,
87      const std::string& profile_path) const OVERRIDE;
88
89  // NetworkProfileObserver overrides
90  virtual void OnProfileAdded(const NetworkProfile& profile) OVERRIDE;
91  virtual void OnProfileRemoved(const NetworkProfile& profile) OVERRIDE;
92
93  // PolicyApplicator::ConfigurationHandler overrides
94  virtual void CreateConfigurationFromPolicy(
95      const base::DictionaryValue& shill_properties) OVERRIDE;
96
97  virtual void UpdateExistingConfigurationWithPropertiesFromPolicy(
98      const base::DictionaryValue& existing_properties,
99      const base::DictionaryValue& new_properties) OVERRIDE;
100
101  virtual void OnPoliciesApplied() OVERRIDE;
102
103 private:
104  friend class ClientCertResolverTest;
105  friend class ManagedNetworkConfigurationHandlerTest;
106  friend class NetworkConnectionHandlerTest;
107  friend class NetworkHandler;
108
109  struct Policies;
110  typedef std::map<std::string, linked_ptr<Policies> > UserToPoliciesMap;
111  typedef base::Callback<void(const std::string& service_path,
112                              scoped_ptr<base::DictionaryValue> properties)>
113      GetDevicePropertiesCallback;
114
115  ManagedNetworkConfigurationHandlerImpl();
116
117  // Handlers may be NULL in tests so long as they do not execute any paths
118  // that require the handlers.
119  void Init(NetworkStateHandler* network_state_handler,
120            NetworkProfileHandler* network_profile_handler,
121            NetworkConfigurationHandler* network_configuration_handler,
122            NetworkDeviceHandler* network_device_handler);
123
124  // Sends the response to the caller of GetManagedProperties.
125  void SendManagedProperties(
126      const std::string& userhash,
127      const network_handler::DictionaryResultCallback& callback,
128      const network_handler::ErrorCallback& error_callback,
129      const std::string& service_path,
130      scoped_ptr<base::DictionaryValue> shill_properties);
131
132  // Sends the response to the caller of GetProperties.
133  void SendProperties(
134      const network_handler::DictionaryResultCallback& callback,
135      const network_handler::ErrorCallback& error_callback,
136      const std::string& service_path,
137      scoped_ptr<base::DictionaryValue> shill_properties);
138
139  const Policies* GetPoliciesForUser(const std::string& userhash) const;
140  const Policies* GetPoliciesForProfile(const NetworkProfile& profile) const;
141
142  void OnPolicyAppliedToNetwork(const std::string& service_path);
143
144  // Helper method to append associated Device properties to |properties|.
145  void GetDeviceStateProperties(const std::string& service_path,
146                                base::DictionaryValue* properties);
147
148  // Callback for NetworkConfigurationHandler::GetProperties requests from
149  // Get{Managed}Properties. This callback fills in properties from
150  // DeviceState and may request additional Device properties.
151  // Note: Requesting Device properties requires an additional fetch and
152  // additional copying of data, so we only do it for Cellular networks which
153  // contain a lot of necessary state in the associated Device object.
154  void GetPropertiesCallback(
155      GetDevicePropertiesCallback send_callback,
156      const std::string& service_path,
157      const base::DictionaryValue& shill_properties);
158
159  void GetDevicePropertiesSuccess(
160      const std::string& service_path,
161      scoped_ptr<base::DictionaryValue> network_properties,
162      GetDevicePropertiesCallback send_callback,
163      const std::string& device_path,
164      const base::DictionaryValue& device_properties);
165  void GetDevicePropertiesFailure(
166      const std::string& service_path,
167      scoped_ptr<base::DictionaryValue> network_properties,
168      GetDevicePropertiesCallback send_callback,
169      const std::string& error_name,
170      scoped_ptr<base::DictionaryValue> error_data);
171
172  // If present, the empty string maps to the device policy.
173  UserToPoliciesMap policies_by_user_;
174
175  // Local references to the associated handler instances.
176  NetworkStateHandler* network_state_handler_;
177  NetworkProfileHandler* network_profile_handler_;
178  NetworkConfigurationHandler* network_configuration_handler_;
179  NetworkDeviceHandler* network_device_handler_;
180
181  ObserverList<NetworkPolicyObserver> observers_;
182
183  // For Shill client callbacks
184  base::WeakPtrFactory<ManagedNetworkConfigurationHandlerImpl>
185      weak_ptr_factory_;
186
187  DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerImpl);
188};
189
190}  // namespace chromeos
191
192#endif  // CHROMEOS_NETWORK_MANAGED_NETWORK_CONFIGURATION_HANDLER_IMPL_H_
193