summaryrefslogtreecommitdiffstats
path: root/chromeos/network/managed_network_configuration_handler_impl.h
blob: c94e446729e867d032efb34bc94793c437245041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_NETWORK_MANAGED_NETWORK_CONFIGURATION_HANDLER_IMPL_H_
#define CHROMEOS_NETWORK_MANAGED_NETWORK_CONFIGURATION_HANDLER_IMPL_H_

#include <map>
#include <set>
#include <string>

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/network/managed_network_configuration_handler.h"
#include "chromeos/network/network_handler_callbacks.h"
#include "chromeos/network/network_profile_observer.h"
#include "chromeos/network/policy_applicator.h"

namespace base {
class DictionaryValue;
}

namespace chromeos {

class NetworkConfigurationHandler;
struct NetworkProfile;
class NetworkProfileHandler;
class NetworkStateHandler;

class CHROMEOS_EXPORT ManagedNetworkConfigurationHandlerImpl
    : public ManagedNetworkConfigurationHandler,
      public NetworkProfileObserver,
      public PolicyApplicator::ConfigurationHandler {
 public:
  ~ManagedNetworkConfigurationHandlerImpl() override;

  // ManagedNetworkConfigurationHandler overrides
  void AddObserver(NetworkPolicyObserver* observer) override;
  void RemoveObserver(NetworkPolicyObserver* observer) override;

  void GetProperties(
      const std::string& userhash,
      const std::string& service_path,
      const network_handler::DictionaryResultCallback& callback,
      const network_handler::ErrorCallback& error_callback) override;

  void GetManagedProperties(
      const std::string& userhash,
      const std::string& service_path,
      const network_handler::DictionaryResultCallback& callback,
      const network_handler::ErrorCallback& error_callback) override;

  void SetProperties(
      const std::string& service_path,
      const base::DictionaryValue& user_settings,
      const base::Closure& callback,
      const network_handler::ErrorCallback& error_callback) override;

  void CreateConfiguration(
      const std::string& userhash,
      const base::DictionaryValue& properties,
      const network_handler::ServiceResultCallback& callback,
      const network_handler::ErrorCallback& error_callback) const override;

  void RemoveConfiguration(
      const std::string& service_path,
      const base::Closure& callback,
      const network_handler::ErrorCallback& error_callback) const override;

  void SetPolicy(onc::ONCSource onc_source,
                 const std::string& userhash,
                 const base::ListValue& network_configs_onc,
                 const base::DictionaryValue& global_network_config) override;

  bool IsAnyPolicyApplicationRunning() const override;

  const base::DictionaryValue* FindPolicyByGUID(
      const std::string userhash,
      const std::string& guid,
      onc::ONCSource* onc_source) const override;

  const GuidToPolicyMap* GetNetworkConfigsFromPolicy(
      const std::string& userhash) const override;

  const base::DictionaryValue* GetGlobalConfigFromPolicy(
      const std::string& userhash) const override;

  const base::DictionaryValue* FindPolicyByGuidAndProfile(
      const std::string& guid,
      const std::string& profile_path) const override;

  // NetworkProfileObserver overrides
  void OnProfileAdded(const NetworkProfile& profile) override;
  void OnProfileRemoved(const NetworkProfile& profile) override;

  // PolicyApplicator::ConfigurationHandler overrides
  void CreateConfigurationFromPolicy(
      const base::DictionaryValue& shill_properties) override;

  void UpdateExistingConfigurationWithPropertiesFromPolicy(
      const base::DictionaryValue& existing_properties,
      const base::DictionaryValue& new_properties) override;

  void OnPoliciesApplied(const NetworkProfile& profile) override;

 private:
  friend class AutoConnectHandlerTest;
  friend class ClientCertResolverTest;
  friend class ManagedNetworkConfigurationHandlerTest;
  friend class NetworkConnectionHandlerTest;
  friend class NetworkHandler;
  friend class ProhibitedTechnologiesHandlerTest;

  struct Policies;
  typedef base::Callback<void(const std::string& service_path,
                              scoped_ptr<base::DictionaryValue> properties)>
      GetDevicePropertiesCallback;
  typedef std::map<std::string, scoped_ptr<Policies>> UserToPoliciesMap;
  typedef std::map<std::string, scoped_ptr<PolicyApplicator>>
      UserToPolicyApplicatorMap;
  typedef std::map<std::string, std::set<std::string>>
      UserToModifiedPoliciesMap;

  ManagedNetworkConfigurationHandlerImpl();

  // Handlers may be NULL in tests so long as they do not execute any paths
  // that require the handlers.
  void Init(NetworkStateHandler* network_state_handler,
            NetworkProfileHandler* network_profile_handler,
            NetworkConfigurationHandler* network_configuration_handler,
            NetworkDeviceHandler* network_device_handler,
            ProhibitedTechnologiesHandler* prohibitied_technologies_handler);

  // Sends the response to the caller of GetManagedProperties.
  void SendManagedProperties(
      const std::string& userhash,
      const network_handler::DictionaryResultCallback& callback,
      const network_handler::ErrorCallback& error_callback,
      const std::string& service_path,
      scoped_ptr<base::DictionaryValue> shill_properties);

  // Sends the response to the caller of GetProperties.
  void SendProperties(const std::string& userhash,
                      const network_handler::DictionaryResultCallback& callback,
                      const network_handler::ErrorCallback& error_callback,
                      const std::string& service_path,
                      scoped_ptr<base::DictionaryValue> shill_properties);

  const Policies* GetPoliciesForUser(const std::string& userhash) const;
  const Policies* GetPoliciesForProfile(const NetworkProfile& profile) const;

  void OnPolicyAppliedToNetwork(const std::string& service_path,
                                const std::string& guid);

  // Helper method to append associated Device properties to |properties|.
  void GetDeviceStateProperties(const std::string& service_path,
                                base::DictionaryValue* properties);

  // Callback for NetworkConfigurationHandler::GetProperties requests from
  // Get{Managed}Properties. This callback fills in properties from
  // DeviceState and may request additional Device properties.
  // Note: Requesting Device properties requires an additional fetch and
  // additional copying of data, so we only do it for Cellular networks which
  // contain a lot of necessary state in the associated Device object.
  void GetPropertiesCallback(
      GetDevicePropertiesCallback send_callback,
      const std::string& service_path,
      const base::DictionaryValue& shill_properties);

  void GetDevicePropertiesSuccess(
      const std::string& service_path,
      scoped_ptr<base::DictionaryValue> network_properties,
      GetDevicePropertiesCallback send_callback,
      const std::string& device_path,
      const base::DictionaryValue& device_properties);
  void GetDevicePropertiesFailure(
      const std::string& service_path,
      scoped_ptr<base::DictionaryValue> network_properties,
      GetDevicePropertiesCallback send_callback,
      const std::string& error_name,
      scoped_ptr<base::DictionaryValue> error_data);

  // Called from SetProperties, calls NCH::SetShillProperties.
  void SetShillProperties(const std::string& service_path,
                          scoped_ptr<base::DictionaryValue> shill_dictionary,
                          const base::Closure& callback,
                          const network_handler::ErrorCallback& error_callback);

  // Applies policies for |userhash|. |modified_policies| must be not null and
  // contain the GUIDs of the network configurations that changed since the last
  // policy application. Returns true if policy application was started and
  // false if it was queued or delayed.
  bool ApplyOrQueuePolicies(const std::string& userhash,
                            std::set<std::string>* modified_policies);

  // If present, the empty string maps to the device policy.
  UserToPoliciesMap policies_by_user_;

  // Local references to the associated handler instances.
  NetworkStateHandler* network_state_handler_;
  NetworkProfileHandler* network_profile_handler_;
  NetworkConfigurationHandler* network_configuration_handler_;
  NetworkDeviceHandler* network_device_handler_;
  ProhibitedTechnologiesHandler* prohibited_technologies_handler_;

  // Owns the currently running PolicyApplicators.
  UserToPolicyApplicatorMap policy_applicators_;

  // Per userhash (or empty string for device policy), contains the GUIDs of the
  // policies that were modified.
  // If this map contains a userhash as key, it means that a policy application
  // for this userhash is pending even if no policies were modified and the
  // associated set of GUIDs is empty.
  UserToModifiedPoliciesMap queued_modified_policies_;

  base::ObserverList<NetworkPolicyObserver, true> observers_;

  // For Shill client callbacks
  base::WeakPtrFactory<ManagedNetworkConfigurationHandlerImpl>
      weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerImpl);
};

}  // namespace chromeos

#endif  // CHROMEOS_NETWORK_MANAGED_NETWORK_CONFIGURATION_HANDLER_IMPL_H_