network_connection_handler.h revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
1// Copyright (c) 2012 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_NETWORK_CONNECTION_HANDLER_H_
6#define CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
7
8#include <set>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/memory/weak_ptr.h"
14#include "base/values.h"
15#include "chromeos/chromeos_export.h"
16#include "chromeos/dbus/dbus_method_call_status.h"
17#include "chromeos/network/network_handler_callbacks.h"
18
19namespace chromeos {
20
21class NetworkState;
22
23// The NetworkConnectionHandler class is used to manage network connection
24// requests. This is the only class that should make Shill Connect calls.
25// It handles the following steps:
26// 1. Determine whether or not sufficient information (e.g. passphrase) is
27//    known to be available to connect to the network.
28// 2. Request additional information (e.g. user data which contains certificate
29//    information) and determine whether sufficient information is available.
30// 3. Send the connect request.
31// 4. Invoke the appropriate callback (always) on success or failure.
32//
33// NetworkConnectionHandler depends on NetworkStateHandler for immediately
34// available State information, and NetworkConfigurationHandler for any
35// configuration calls.
36
37class CHROMEOS_EXPORT NetworkConnectionHandler
38    : public base::SupportsWeakPtr<NetworkConnectionHandler> {
39 public:
40  // Constants for |error_name| from |error_callback| for Connect/Disconnect.
41  static const char kErrorNotFound[];
42  static const char kErrorConnected[];
43  static const char kErrorConnecting[];
44  static const char kErrorNotConnected[];
45  static const char kErrorPassphraseRequired[];
46  static const char kErrorActivationRequired[];
47  static const char kErrorCertificateRequired[];
48  static const char kErrorConfigurationRequired[];
49  static const char kErrorShillError[];
50
51  // Sets the global instance. Must be called before any calls to Get().
52  static void Initialize();
53
54  // Destroys the global instance.
55  static void Shutdown();
56
57  // Gets the global instance. Initialize() must be called first.
58  static NetworkConnectionHandler* Get();
59
60  // ConnectToNetwork() will start an asynchronous connection attempt.
61  // On success, |success_callback| will be called.
62  // On failure, |error_callback| will be called with |error_name| one of:
63  //  kErrorNotFound if no network matching |service_path| is found
64  //    (hidden networks must be configured before connecting).
65  //  kErrorConnected if already connected to the network.
66  //  kErrorConnecting if already connecting to the network.
67  //  kErrorCertificateRequired if the network requires a cert and none exists.
68  //  kErrorPassphraseRequired if passphrase only is required.
69  //  kErrorConfigurationRequired if additional configuration is required.
70  //  kErrorShillError if a DBus or Shill error occurred.
71  // |error_message| will contain an additional error string for debugging.
72  void ConnectToNetwork(const std::string& service_path,
73                        const base::Closure& success_callback,
74                        const network_handler::ErrorCallback& error_callback);
75
76  // DisconnectToNetwork() will send a Disconnect request to Shill.
77  // On success, |success_callback| will be called.
78  // On failure, |error_callback| will be called with |error_name| one of:
79  //  kErrorNotFound if no network matching |service_path| is found.
80  //  kErrorNotConnected if not connected to the network.
81  //  kErrorShillError if a DBus or Shill error occurred.
82  // |error_message| will contain and additional error string for debugging.
83  void DisconnectNetwork(const std::string& service_path,
84                         const base::Closure& success_callback,
85                         const network_handler::ErrorCallback& error_callback);
86
87 private:
88  NetworkConnectionHandler();
89  ~NetworkConnectionHandler();
90
91  // Calls Shill.Manager.Connect asynchronously.
92  void CallShillConnect(
93      const std::string& service_path,
94      const base::Closure& success_callback,
95      const network_handler::ErrorCallback& error_callback);
96
97  // Calls Shill.Manager.Disconnect asynchronously.
98  void CallShillDisconnect(
99      const std::string& service_path,
100      const base::Closure& success_callback,
101      const network_handler::ErrorCallback& error_callback);
102
103  // Callback from Shill.Service.GetProperties. Parses |properties| to verify
104  // whether or not the network appears to be configured. If configured,
105  // attempts a connection, otherwise invokes |error_callback|.
106  void VerifyConfiguredAndConnect(
107      const base::Closure& success_callback,
108      const network_handler::ErrorCallback& error_callback,
109      const std::string& service_path,
110      const base::DictionaryValue& properties);
111
112  // Sets the property for the service with an empty callback (logs errors).
113  void SetServiceProperty(const std::string& service_path,
114                          const std::string& property,
115                          const std::string& value) const;
116
117  // Handle failure from ConfigurationHandler calls.
118  void HandleConfigurationFailure(
119      const std::string& service_path,
120      const network_handler::ErrorCallback& error_callback,
121      const std::string& error_name,
122      scoped_ptr<base::DictionaryValue> error_data);
123
124  // Handle success or failure from Shill.Service.Connect.
125  void HandleShillSuccess(const std::string& service_path,
126                          const base::Closure& success_callback);
127  void HandleShillFailure(const std::string& service_path,
128                          const network_handler::ErrorCallback& error_callback,
129                          const std::string& error_name,
130                          const std::string& error_message);
131
132  // Set of pending connect requests, used to prevent repeat attempts while
133  // waiting for Shill.
134  std::set<std::string> pending_requests_;
135
136  DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandler);
137};
138
139}  // namespace chromeos
140
141#endif  // CHROMEOS_NETWORK_NETWORK_CONNECTION_HANDLER_H_
142