wifi_manager.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
1// Copyright 2014 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 CHROME_BROWSER_LOCAL_DISCOVERY_WIFI_WIFI_MANAGER_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_WIFI_WIFI_MANAGER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/callback.h"
12#include "base/memory/scoped_ptr.h"
13#include "components/wifi/network_properties.h"
14
15namespace local_discovery {
16
17namespace wifi {
18
19// Convenience definition for users of this header, since ::wifi and
20// local_discovery::wifi may conflict.
21using ::wifi::NetworkProperties;
22
23typedef std::vector<NetworkProperties> NetworkPropertiesList;
24
25// Credentials for WiFi networks. Currently only supports PSK-based networks.
26// TODO(noamsml): Support for 802.11X and other authentication methods.
27struct WifiCredentials {
28  static WifiCredentials FromPSK(const std::string& psk);
29
30  std::string psk;
31};
32
33class WifiManagerFactory;
34
35// Observer for the network list. Classes may implement this interface and call
36// |AddNetworkListObserver| to be notified of changes to the visible network
37// list.
38class NetworkListObserver {
39 public:
40  virtual ~NetworkListObserver() {}
41
42  virtual void OnNetworkListChanged(const NetworkPropertiesList& ssid) = 0;
43};
44
45// A class to manage listing, connecting to, and getting the credentials of WiFi
46// networks.
47class WifiManager {
48 public:
49  typedef base::Callback<void(const NetworkPropertiesList& ssids)>
50      SSIDListCallback;
51  typedef base::Callback<void(bool success)> SuccessCallback;
52  typedef base::Callback<
53      void(bool success, const std::string& ssid, const std::string& password)>
54      CredentialsCallback;
55
56  virtual ~WifiManager() {}
57
58  static scoped_ptr<WifiManager> Create();
59
60  static void SetFactory(WifiManagerFactory* factory);
61
62  // Start the wifi manager. This must be called before any other method calls.
63  virtual void Start() = 0;
64
65  // Get the list of visible SSIDs in the vicinity. This does not initiate a
66  // scan, but merely gets the list of networks from the system.
67  virtual void GetSSIDList(const SSIDListCallback& callback) = 0;
68
69  // Request a scan for networks nearby.
70  virtual void RequestScan() = 0;
71
72  // Configure and connect to a network with a given SSID and
73  // credentials. |callback| will be called once the network is connected or
74  // after it has failed to connect.
75  virtual void ConfigureAndConnectNetwork(const std::string& ssid,
76                                          const WifiCredentials& credentials,
77                                          const SuccessCallback& callback) = 0;
78
79  // Connect to a configured network with a given network ID. |callback| will be
80  // called once the network is connected or after it has failed to connect.
81  virtual void ConnectToNetworkByID(const std::string& internal_id,
82                                    const SuccessCallback& callback) = 0;
83
84  // Reequest the credentials for a network with a given network ID from the
85  // system. |callback| will be called with credentials if they can be
86  // retrieved. Depending on platform, this may bring up a confirmation dialog
87  // or password prompt.
88  virtual void RequestNetworkCredentials(
89      const std::string& internal_id,
90      const CredentialsCallback& callback) = 0;
91
92  // Add a network list observer. This observer will be notified every time the
93  // network list changes.
94  virtual void AddNetworkListObserver(NetworkListObserver* observer) = 0;
95
96  // Remove a network list observer.
97  virtual void RemoveNetworkListObserver(NetworkListObserver* observer) = 0;
98
99 private:
100  static scoped_ptr<WifiManager> CreateDefault();
101};
102
103class WifiManagerFactory {
104 public:
105  virtual ~WifiManagerFactory() {}
106
107  virtual scoped_ptr<WifiManager> CreateWifiManager() = 0;
108};
109
110}  // namespace wifi
111
112}  // namespace local_discovery
113
114#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_WIFI_WIFI_MANAGER_H_
115