1a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#ifndef CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include <set>
9a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include <string>
10a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/basictypes.h"
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/callback.h"
13a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/memory/weak_ptr.h"
1423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "base/time/time.h"
15a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/values.h"
16558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "chromeos/cert_loader.h"
17a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chromeos/chromeos_export.h"
18a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chromeos/dbus/dbus_method_call_status.h"
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chromeos/login/login_state.h"
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chromeos/network/network_handler.h"
21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chromeos/network/network_handler_callbacks.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chromeos/network/network_policy_observer.h"
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/network/network_state_handler_observer.h"
24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace chromeos {
26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)class NetworkState;
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// The NetworkConnectionHandler class is used to manage network connection
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// requests. This is the only class that should make Shill Connect calls.
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// It handles the following steps:
32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 1. Determine whether or not sufficient information (e.g. passphrase) is
33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)//    known to be available to connect to the network.
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 2. Request additional information (e.g. user data which contains certificate
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)//    information) and determine whether sufficient information is available.
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// 3. Possibly configure the network certificate info (tpm slot and pkcs11 id).
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// 4. Send the connect request.
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// 5. Wait for the network state to change to a non connecting state.
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// 6. Invoke the appropriate callback (always) on success or failure.
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)//
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// NetworkConnectionHandler depends on NetworkStateHandler for immediately
42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// available State information, and NetworkConfigurationHandler for any
43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// configuration calls.
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)class CHROMEOS_EXPORT NetworkConnectionHandler
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : public LoginState::Observer,
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      public CertLoader::Observer,
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      public NetworkStateHandlerObserver,
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      public NetworkPolicyObserver,
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      public base::SupportsWeakPtr<NetworkConnectionHandler> {
51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) public:
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Constants for |error_name| from |error_callback| for Connect.
5358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  //  No network matching |service_path| is found (hidden networks must be
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  //  configured before connecting).
56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  static const char kErrorNotFound[];
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Already connected to the network.
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  static const char kErrorConnected[];
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Already connecting to the network.
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  static const char kErrorConnecting[];
6358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The passphrase is missing or invalid.
65a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  static const char kErrorPassphraseRequired[];
6658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  static const char kErrorActivationRequired[];
6858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The network requires a cert and none exists.
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  static const char kErrorCertificateRequired[];
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The network had an authentication error, indicating that additional or
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // different authentication information is required.
74a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static const char kErrorAuthenticationRequired[];
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Additional configuration is required.
77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  static const char kErrorConfigurationRequired[];
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Configuration failed during the configure stage of the connect flow.
80ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static const char kErrorConfigureFailed[];
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // For Disconnect or Activate, an unexpected DBus or Shill error occurred.
8358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  static const char kErrorShillError[];
8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // A new network connect request canceled this one.
86ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static const char kErrorConnectCanceled[];
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Constants for |error_name| from |error_callback| for Disconnect.
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const char kErrorNotConnected[];
90a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
9123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Certificate load timed out.
9223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  static const char kErrorCertLoadTimeout[];
9323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~NetworkConnectionHandler();
95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // ConnectToNetwork() will start an asynchronous connection attempt.
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // On success, |success_callback| will be called.
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // On failure, |error_callback| will be called with |error_name| one of the
9968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  //   constants defined above, or shill::kErrorConnectFailed or
10068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  //   shill::kErrorBadPassphrase if the Shill Error property (from a
10158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  //   previous connect attempt) was set to one of those.
102a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // |error_message| will contain an additional error string for debugging.
103a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // If |check_error_state| is true, the current state of the network is
104a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  //  checked for errors, otherwise current state is ignored (e.g. for recently
105a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  //  configured networks or repeat attempts).
106a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  void ConnectToNetwork(const std::string& service_path,
107a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                        const base::Closure& success_callback,
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                        const network_handler::ErrorCallback& error_callback,
109a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                        bool check_error_state);
110a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // DisconnectNetwork() will send a Disconnect request to Shill.
112a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // On success, |success_callback| will be called.
113a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // On failure, |error_callback| will be called with |error_name| one of:
114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  //  kErrorNotFound if no network matching |service_path| is found.
115a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  //  kErrorNotConnected if not connected to the network.
116a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  //  kErrorShillError if a DBus or Shill error occurred.
117a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // |error_message| will contain and additional error string for debugging.
118a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  void DisconnectNetwork(const std::string& service_path,
119a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                         const base::Closure& success_callback,
120a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                         const network_handler::ErrorCallback& error_callback);
121a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns true if ConnectToNetwork has been called with |service_path| and
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // has not completed (i.e. success or error callback has been called).
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool HasConnectingNetwork(const std::string& service_path);
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Returns true if there are any pending connect requests.
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  bool HasPendingConnectRequest();
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // NetworkStateHandlerObserver
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void NetworkListChanged() OVERRIDE;
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE;
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // LoginState::Observer
134d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual void LoggedInStateChanged() OVERRIDE;
1357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // CertLoader::Observer
1377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnCertificatesLoaded(const net::CertificateList& cert_list,
1387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                    bool initial_load) OVERRIDE;
1397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // NetworkPolicyObserver
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void PolicyChanged(const std::string& userhash) OVERRIDE;
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
143a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) private:
14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  friend class NetworkHandler;
14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  friend class NetworkConnectionHandlerTest;
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  struct ConnectRequest;
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
149a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  NetworkConnectionHandler();
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
151558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  void Init(NetworkStateHandler* network_state_handler,
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            NetworkConfigurationHandler* network_configuration_handler,
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            ManagedNetworkConfigurationHandler*
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                managed_network_configuration_handler);
155a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
156c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  ConnectRequest* GetPendingRequest(const std::string& service_path);
157a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
158a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Callback from Shill.Service.GetProperties. Parses |properties| to verify
159a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // whether or not the network appears to be configured. If configured,
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // attempts a connection, otherwise invokes error_callback from
161a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // pending_requests_[service_path]. |check_error_state| is passed from
162a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // ConnectToNetwork(), see comment for info.
163a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void VerifyConfiguredAndConnect(bool check_error_state,
164a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                  const std::string& service_path,
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                  const base::DictionaryValue& properties);
166a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
16723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Queues a connect request until certificates have loaded.
16823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  void QueueConnectRequest(const std::string& service_path);
16923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
17023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Checks to see if certificates have loaded and if not, cancels any queued
17123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // connect request and notifies the user.
17223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  void CheckCertificatesLoaded();
17323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
17423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Handles connecting to a queued network after certificates are loaded or
17523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // handle cert load timeout.
17623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  void ConnectToQueuedNetwork();
17723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Calls Shill.Manager.Connect asynchronously.
179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void CallShillConnect(const std::string& service_path);
180a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
18123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Handles failure from ConfigurationHandler calls.
182a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  void HandleConfigurationFailure(
183a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      const std::string& service_path,
184a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      const std::string& error_name,
185a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      scoped_ptr<base::DictionaryValue> error_data);
186a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
18723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Handles success or failure from Shill.Service.Connect.
188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void HandleShillConnectSuccess(const std::string& service_path);
189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void HandleShillConnectFailure(const std::string& service_path,
190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                 const std::string& error_name,
191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                 const std::string& error_message);
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void CheckPendingRequest(const std::string service_path);
194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void CheckAllPendingRequests();
195c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
1967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void ErrorCallbackForPendingRequest(const std::string& service_path,
1977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      const std::string& error_name);
198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Calls Shill.Manager.Disconnect asynchronously.
200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void CallShillDisconnect(
201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const std::string& service_path,
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const base::Closure& success_callback,
203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const network_handler::ErrorCallback& error_callback);
204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
205ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Handle success from Shill.Service.Disconnect.
206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void HandleShillDisconnectSuccess(const std::string& service_path,
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                    const base::Closure& success_callback);
208a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // If the policy to prevent unmanaged & shared networks to autoconnect is
210cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // enabled, then disconnect all such networks except wired networks. Does
211cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // nothing on consecutive calls.
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This is enforced once after a user logs in 1) to allow mananged networks to
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // autoconnect and 2) to prevent a previous user from foisting a network on
214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // the new user. Therefore, this function is called on startup, at login and
215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // when the device policy is changed.
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void DisconnectIfPolicyRequires();
217cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
218cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Requests a connect to the 'best' available network once after login and
219cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // after any disconnect required by policy is executed (see
220cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // DisconnectIfPolicyRequires()). To include networks with client
221cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // certificates, no request is sent until certificates are loaded. Therefore,
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // this function is called on the initial certificate load and by
223cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // DisconnectIfPolicyRequires().
224cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void ConnectToBestNetworkAfterLogin();
225ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
22690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Local references to the associated handler instances.
2277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  CertLoader* cert_loader_;
22890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NetworkStateHandler* network_state_handler_;
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NetworkConfigurationHandler* configuration_handler_;
230cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ManagedNetworkConfigurationHandler* managed_configuration_handler_;
23190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Map of pending connect requests, used to prevent repeated attempts while
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // waiting for Shill and to trigger callbacks on eventual success or failure.
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::map<std::string, ConnectRequest> pending_requests_;
2357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_ptr<ConnectRequest> queued_connect_;
2367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Track certificate loading state.
2387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  bool logged_in_;
2397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  bool certificates_loaded_;
24023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  base::TimeTicks logged_in_time_;
241a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
242cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Whether the autoconnect policy was applied already, see
243cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // DisconnectIfPolicyRequires().
244cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool applied_autoconnect_policy_;
245cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
246cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Whether the handler already requested a 'ConnectToBestNetwork' after login,
247cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // see ConnectToBestNetworkAfterLogin().
248cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool requested_connect_to_best_network_;
249cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
250a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler);
251a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)};
252a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
253a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}  // namespace chromeos
254a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
255a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#endif  // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
256