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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
// Copyright (c) 2012 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_NETWORK_CONNECTION_HANDLER_H_
#define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
#include <map>
#include <set>
#include <string>
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chromeos/cert_loader.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/login/login_state.h"
#include "chromeos/network/network_connection_observer.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_handler_callbacks.h"
#include "chromeos/network/network_state_handler_observer.h"
namespace chromeos {
class NetworkState;
// The NetworkConnectionHandler class is used to manage network connection
// requests. This is the only class that should make Shill Connect calls.
// It handles the following steps:
// 1. Determine whether or not sufficient information (e.g. passphrase) is
// known to be available to connect to the network.
// 2. Request additional information (e.g. user data which contains certificate
// information) and determine whether sufficient information is available.
// 3. Possibly configure the network certificate info (tpm slot and pkcs11 id).
// 4. Send the connect request.
// 5. Wait for the network state to change to a non connecting state.
// 6. Invoke the appropriate callback (always) on success or failure.
//
// NetworkConnectionHandler depends on NetworkStateHandler for immediately
// available State information, and NetworkConfigurationHandler for any
// configuration calls.
class CHROMEOS_EXPORT NetworkConnectionHandler
: public LoginState::Observer,
public CertLoader::Observer,
public NetworkStateHandlerObserver,
public base::SupportsWeakPtr<NetworkConnectionHandler> {
public:
// Constants for |error_name| from |error_callback| for Connect.
// No network matching |service_path| is found (hidden networks must be
// configured before connecting).
static const char kErrorNotFound[];
// Already connected to the network.
static const char kErrorConnected[];
// Already connecting to the network.
static const char kErrorConnecting[];
// The passphrase is missing or invalid.
static const char kErrorPassphraseRequired[];
// The passphrase is incorrect.
static const char kErrorBadPassphrase[];
// The network requires a cert and none exists.
static const char kErrorCertificateRequired[];
// The network had an authentication error, indicating that additional or
// different authentication information is required.
static const char kErrorAuthenticationRequired[];
// Additional configuration is required.
static const char kErrorConfigurationRequired[];
// Configuration failed during the configure stage of the connect flow.
static const char kErrorConfigureFailed[];
// An unexpected DBus or Shill error occurred while connecting.
static const char kErrorConnectFailed[];
// An unexpected DBus or Shill error occurred while disconnecting.
static const char kErrorDisconnectFailed[];
// A new network connect request canceled this one.
static const char kErrorConnectCanceled[];
// Constants for |error_name| from |error_callback| for Disconnect.
static const char kErrorNotConnected[];
// Certificate load timed out.
static const char kErrorCertLoadTimeout[];
~NetworkConnectionHandler() override;
void AddObserver(NetworkConnectionObserver* observer);
void RemoveObserver(NetworkConnectionObserver* observer);
// ConnectToNetwork() will start an asynchronous connection attempt.
// On success, |success_callback| will be called.
// On failure, |error_callback| will be called with |error_name| one of the
// constants defined above.
// |error_message| will contain an additional error string for debugging.
// If |check_error_state| is true, the current state of the network is
// checked for errors, otherwise current state is ignored (e.g. for recently
// configured networks or repeat attempts).
void ConnectToNetwork(const std::string& service_path,
const base::Closure& success_callback,
const network_handler::ErrorCallback& error_callback,
bool check_error_state);
// DisconnectNetwork() will send a Disconnect request to Shill.
// On success, |success_callback| will be called.
// On failure, |error_callback| will be called with |error_name| one of:
// kErrorNotFound if no network matching |service_path| is found.
// kErrorNotConnected if not connected to the network.
// kErrorDisconnectFailed if a DBus or Shill error occurred.
// |error_message| will contain and additional error string for debugging.
void DisconnectNetwork(const std::string& service_path,
const base::Closure& success_callback,
const network_handler::ErrorCallback& error_callback);
// Returns true if ConnectToNetwork has been called with |service_path| and
// has not completed (i.e. success or error callback has been called).
bool HasConnectingNetwork(const std::string& service_path);
// Returns true if there are any pending connect requests.
bool HasPendingConnectRequest();
// NetworkStateHandlerObserver
void NetworkListChanged() override;
void NetworkPropertiesUpdated(const NetworkState* network) override;
// LoginState::Observer
void LoggedInStateChanged() override;
// CertLoader::Observer
void OnCertificatesLoaded(const net::CertificateList& cert_list,
bool initial_load) override;
private:
friend class NetworkHandler;
friend class NetworkConnectionHandlerTest;
struct ConnectRequest;
NetworkConnectionHandler();
void Init(NetworkStateHandler* network_state_handler,
NetworkConfigurationHandler* network_configuration_handler,
ManagedNetworkConfigurationHandler*
managed_network_configuration_handler);
ConnectRequest* GetPendingRequest(const std::string& service_path);
// Callback from Shill.Service.GetProperties. Parses |properties| to verify
// whether or not the network appears to be configured. If configured,
// attempts a connection, otherwise invokes error_callback from
// pending_requests_[service_path]. |check_error_state| is passed from
// ConnectToNetwork(), see comment for info.
void VerifyConfiguredAndConnect(bool check_error_state,
const std::string& service_path,
const base::DictionaryValue& properties);
// Queues a connect request until certificates have loaded.
void QueueConnectRequest(const std::string& service_path);
// Checks to see if certificates have loaded and if not, cancels any queued
// connect request and notifies the user.
void CheckCertificatesLoaded();
// Handles connecting to a queued network after certificates are loaded or
// handle cert load timeout.
void ConnectToQueuedNetwork();
// Calls Shill.Manager.Connect asynchronously.
void CallShillConnect(const std::string& service_path);
// Handles failure from ConfigurationHandler calls.
void HandleConfigurationFailure(
const std::string& service_path,
const std::string& error_name,
scoped_ptr<base::DictionaryValue> error_data);
// Handles success or failure from Shill.Service.Connect.
void HandleShillConnectSuccess(const std::string& service_path);
void HandleShillConnectFailure(const std::string& service_path,
const std::string& error_name,
const std::string& error_message);
void CheckPendingRequest(const std::string service_path);
void CheckAllPendingRequests();
// Notify caller and observers that the connect request succeeded.
void InvokeConnectSuccessCallback(const std::string& service_path,
const base::Closure& success_callback);
// Look up the ConnectRequest for |service_path| and call
// InvokeConnectErrorCallback.
void ErrorCallbackForPendingRequest(const std::string& service_path,
const std::string& error_name);
// Notify caller and observers that the connect request failed.
// |error_name| will be one of the kError* messages defined above.
void InvokeConnectErrorCallback(
const std::string& service_path,
const network_handler::ErrorCallback& error_callback,
const std::string& error_name);
// Calls Shill.Manager.Disconnect asynchronously.
void CallShillDisconnect(
const std::string& service_path,
const base::Closure& success_callback,
const network_handler::ErrorCallback& error_callback);
// Handle success from Shill.Service.Disconnect.
void HandleShillDisconnectSuccess(const std::string& service_path,
const base::Closure& success_callback);
ObserverList<NetworkConnectionObserver> observers_;
// Local references to the associated handler instances.
CertLoader* cert_loader_;
NetworkStateHandler* network_state_handler_;
NetworkConfigurationHandler* configuration_handler_;
ManagedNetworkConfigurationHandler* managed_configuration_handler_;
// Map of pending connect requests, used to prevent repeated attempts while
// waiting for Shill and to trigger callbacks on eventual success or failure.
std::map<std::string, ConnectRequest> pending_requests_;
scoped_ptr<ConnectRequest> queued_connect_;
// Track certificate loading state.
bool logged_in_;
bool certificates_loaded_;
base::TimeTicks logged_in_time_;
DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler);
};
} // namespace chromeos
#endif // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
|