network_connection_handler.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
146d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// Copyright (c) 2012 The Chromium Authors. All rights reserved.
258cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com// Use of this source code is governed by a BSD-style license that can be
358cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com// found in the LICENSE file.
458cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com
558cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com#ifndef CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
658cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com#define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
758cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com
858cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com#include <set>
958cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com#include <string>
1058cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com
11729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com#include "base/basictypes.h"
12729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com#include "base/callback.h"
1346d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com#include "base/memory/weak_ptr.h"
1446d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com#include "base/values.h"
1546d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com#include "chromeos/chromeos_export.h"
1646d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com#include "chromeos/dbus/dbus_method_call_status.h"
1746d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com#include "chromeos/network/network_handler.h"
1846d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com#include "chromeos/network/network_handler_callbacks.h"
1946d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com
2046d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.comnamespace chromeos {
2146d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com
2246d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.comclass NetworkState;
2346d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com
2446d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// The NetworkConnectionHandler class is used to manage network connection
2546d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// requests. This is the only class that should make Shill Connect calls.
2646d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// It handles the following steps:
2746d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// 1. Determine whether or not sufficient information (e.g. passphrase) is
2846d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com//    known to be available to connect to the network.
2946d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// 2. Request additional information (e.g. user data which contains certificate
3046d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com//    information) and determine whether sufficient information is available.
3146d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// 3. Send the connect request.
3246d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// 4. Invoke the appropriate callback (always) on success or failure.
3346d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com//
3446d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// NetworkConnectionHandler depends on NetworkStateHandler for immediately
3546d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// available State information, and NetworkConfigurationHandler for any
3646d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com// configuration calls.
3746d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com
3846d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.comclass CHROMEOS_EXPORT NetworkConnectionHandler
3946d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com    : public base::SupportsWeakPtr<NetworkConnectionHandler> {
4046d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com public:
4146d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  // Constants for |error_name| from |error_callback| for Connect/Disconnect.
4246d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorNotFound[];
4346d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorConnected[];
4446d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorConnecting[];
4546d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorNotConnected[];
4646d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorPassphraseRequired[];
4746d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorActivationRequired[];
4846d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorCertificateRequired[];
4946d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorConfigurationRequired[];
5046d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorShillError[];
5146d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  static const char kErrorPreviousConnectFailed[];
5246d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com
5346d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  ~NetworkConnectionHandler();
5446d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com
5546d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  // ConnectToNetwork() will start an asynchronous connection attempt.
5646d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  // On success, |success_callback| will be called.
5746d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  // On failure, |error_callback| will be called with |error_name| one of:
5846d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  //  kErrorNotFound if no network matching |service_path| is found
5969c5522a3dc3d0906ab836350f53dae34a9897d3bradnelson@google.com  //    (hidden networks must be configured before connecting).
6077b7833a5d31c72fb0e081cebea69a04ac28e767bradnelson@google.com  //  kErrorConnected if already connected to the network.
6177b7833a5d31c72fb0e081cebea69a04ac28e767bradnelson@google.com  //  kErrorConnecting if already connecting to the network.
6277b7833a5d31c72fb0e081cebea69a04ac28e767bradnelson@google.com  //  kErrorCertificateRequired if the network requires a cert and none exists.
6346d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  //  kErrorPassphraseRequired if passphrase only is required.
6446d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  //  kErrorConfigurationRequired if additional configuration is required.
6546d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com  //  kErrorShillError if a DBus or Shill error occurred.
66729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com  // |error_message| will contain an additional error string for debugging.
67729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com  // If |ignore_error_state| is true, error state for the network is ignored
6858cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com  //  (e.g. for repeat attempts).
6958cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com  void ConnectToNetwork(const std::string& service_path,
70729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com                        const base::Closure& success_callback,
71729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com                        const network_handler::ErrorCallback& error_callback,
72729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com                        bool ignore_error_state);
73729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com
74729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com  // DisconnectToNetwork() will send a Disconnect request to Shill.
7519501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org  // On success, |success_callback| will be called.
7619501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org  // On failure, |error_callback| will be called with |error_name| one of:
7719501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org  //  kErrorNotFound if no network matching |service_path| is found.
7819501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org  //  kErrorNotConnected if not connected to the network.
7919501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org  //  kErrorShillError if a DBus or Shill error occurred.
8019501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org  // |error_message| will contain and additional error string for debugging.
8119501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org  void DisconnectNetwork(const std::string& service_path,
8245254f3a7401b4b63d829f38c426c0635485f1e0maruel@chromium.org                         const base::Closure& success_callback,
8319501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org                         const network_handler::ErrorCallback& error_callback);
8445254f3a7401b4b63d829f38c426c0635485f1e0maruel@chromium.org
8545254f3a7401b4b63d829f38c426c0635485f1e0maruel@chromium.org private:
8645254f3a7401b4b63d829f38c426c0635485f1e0maruel@chromium.org  friend class NetworkHandler;
8745254f3a7401b4b63d829f38c426c0635485f1e0maruel@chromium.org  friend class NetworkConnectionHandlerTest;
8819501fb2a0a869e744ad9e3af0b71e7a1aa40a60mark@chromium.org  NetworkConnectionHandler();
8945254f3a7401b4b63d829f38c426c0635485f1e0maruel@chromium.org
9045254f3a7401b4b63d829f38c426c0635485f1e0maruel@chromium.org  void Init(NetworkStateHandler* network_state_handler,
9158cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com            NetworkConfigurationHandler* network_configuration_handler);
9245254f3a7401b4b63d829f38c426c0635485f1e0maruel@chromium.org
93729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com  // Calls Shill.Manager.Connect asynchronously.
94729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com  void CallShillConnect(
95729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com      const std::string& service_path,
96729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com      const base::Closure& success_callback,
972b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org      const network_handler::ErrorCallback& error_callback);
9827c626e0515f845338cb60b1d8405f40150a791dscottmg@chromium.org
992b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org  // Calls Shill.Manager.Disconnect asynchronously.
1002b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org  void CallShillDisconnect(
1012b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org      const std::string& service_path,
1022b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org      const base::Closure& success_callback,
10327c626e0515f845338cb60b1d8405f40150a791dscottmg@chromium.org      const network_handler::ErrorCallback& error_callback);
10427c626e0515f845338cb60b1d8405f40150a791dscottmg@chromium.org
10527c626e0515f845338cb60b1d8405f40150a791dscottmg@chromium.org  // Callback from Shill.Service.GetProperties. Parses |properties| to verify
10627c626e0515f845338cb60b1d8405f40150a791dscottmg@chromium.org  // whether or not the network appears to be configured. If configured,
1072b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org  // attempts a connection, otherwise invokes |error_callback|.
1082b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org  void VerifyConfiguredAndConnect(
10946d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com      const base::Closure& success_callback,
11027c626e0515f845338cb60b1d8405f40150a791dscottmg@chromium.org      const network_handler::ErrorCallback& error_callback,
11146d34d835a9fb416adc83e8cf4aba29204bece0bbradnelson@google.com      const std::string& service_path,
1122b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org      const base::DictionaryValue& properties);
1132b3dd69e0ad23c3be7ec455f0403d4fdf740b2a0maruel@chromium.org
114729a886b335e931e49aa3775d4c0d1c11741afdebradnelson@google.com  // Sets the property for the service with an empty callback (logs errors).
11558cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com  void SetServiceProperty(const std::string& service_path,
11658cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com                          const std::string& property,
11758cc35306d7904897b43f82f85ab70b000f92988bradnelson@google.com                          const std::string& value) const;
118f1d6506f0acf422f5a5dbe0e7a3af47ee6c26c2ctorne@chromium.org
119  // Handle failure from ConfigurationHandler calls.
120  void HandleConfigurationFailure(
121      const std::string& service_path,
122      const network_handler::ErrorCallback& error_callback,
123      const std::string& error_name,
124      scoped_ptr<base::DictionaryValue> error_data);
125
126  // Handle success or failure from Shill.Service.Connect.
127  void HandleShillSuccess(const std::string& service_path,
128                          const base::Closure& success_callback);
129  void HandleShillFailure(const std::string& service_path,
130                          const network_handler::ErrorCallback& error_callback,
131                          const std::string& error_name,
132                          const std::string& error_message);
133
134  // Local references to the associated handler instances.
135  NetworkStateHandler* network_state_handler_;
136  NetworkProfileHandler* network_profile_handler_;
137  NetworkConfigurationHandler* network_configuration_handler_;
138
139  // Set of pending connect requests, used to prevent repeat attempts while
140  // waiting for Shill.
141  std::set<std::string> pending_requests_;
142
143  DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler);
144};
145
146}  // namespace chromeos
147
148#endif  // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
149