Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / browser / api / networking_private / networking_private_linux.h
blobe4b217154bdc79b2198b8329a4e2cc1bee44f650
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.
5 #ifndef EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_LINUX_H_
6 #define EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_LINUX_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/linked_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/threading/thread.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "extensions/browser/api/networking_private/networking_private_delegate.h"
17 namespace context {
18 class BrowserContext;
21 namespace dbus {
22 class Bus;
23 class ObjectPath;
24 class ObjectProxy;
25 class Response;
28 namespace extensions {
30 // Linux NetworkingPrivateDelegate implementation.
31 class NetworkingPrivateLinux : public NetworkingPrivateDelegate {
32 public:
33 typedef std::map<base::string16, linked_ptr<base::DictionaryValue>>
34 NetworkMap;
36 typedef std::vector<std::string> GuidList;
38 NetworkingPrivateLinux(content::BrowserContext* browser_context,
39 scoped_ptr<VerifyDelegate> verify_delegate);
41 // NetworkingPrivateDelegate
42 void GetProperties(const std::string& guid,
43 const DictionaryCallback& success_callback,
44 const FailureCallback& failure_callback) override;
45 void GetManagedProperties(const std::string& guid,
46 const DictionaryCallback& success_callback,
47 const FailureCallback& failure_callback) override;
48 void GetState(const std::string& guid,
49 const DictionaryCallback& success_callback,
50 const FailureCallback& failure_callback) override;
51 void SetProperties(const std::string& guid,
52 scoped_ptr<base::DictionaryValue> properties,
53 const VoidCallback& success_callback,
54 const FailureCallback& failure_callback) override;
55 void CreateNetwork(bool shared,
56 scoped_ptr<base::DictionaryValue> properties,
57 const StringCallback& success_callback,
58 const FailureCallback& failure_callback) override;
59 void ForgetNetwork(const std::string& guid,
60 const VoidCallback& success_callback,
61 const FailureCallback& failure_callback) override;
62 void GetNetworks(const std::string& network_type,
63 bool configured_only,
64 bool visible_only,
65 int limit,
66 const NetworkListCallback& success_callback,
67 const FailureCallback& failure_callback) override;
68 void StartConnect(const std::string& guid,
69 const VoidCallback& success_callback,
70 const FailureCallback& failure_callback) override;
71 void StartDisconnect(const std::string& guid,
72 const VoidCallback& success_callback,
73 const FailureCallback& failure_callback) override;
74 void SetWifiTDLSEnabledState(
75 const std::string& ip_or_mac_address,
76 bool enabled,
77 const StringCallback& success_callback,
78 const FailureCallback& failure_callback) override;
79 void GetWifiTDLSStatus(const std::string& ip_or_mac_address,
80 const StringCallback& success_callback,
81 const FailureCallback& failure_callback) override;
82 void GetCaptivePortalStatus(const std::string& guid,
83 const StringCallback& success_callback,
84 const FailureCallback& failure_callback) override;
85 scoped_ptr<base::ListValue> GetEnabledNetworkTypes() override;
86 scoped_ptr<DeviceStateList> GetDeviceStateList() override;
87 bool EnableNetworkType(const std::string& type) override;
88 bool DisableNetworkType(const std::string& type) override;
89 bool RequestScan() override;
90 void AddObserver(NetworkingPrivateDelegateObserver* observer) override;
91 void RemoveObserver(NetworkingPrivateDelegateObserver* observer) override;
93 private:
94 ~NetworkingPrivateLinux() override;
96 // https://developer.gnome.org/NetworkManager/unstable/spec.html#type-NM_DEVICE_TYPE
97 enum DeviceType {
98 NM_DEVICE_TYPE_UNKNOWN = 0,
99 NM_DEVICE_TYPE_ETHERNET = 1,
100 NM_DEVICE_TYPE_WIFI = 2
103 // https://developer.gnome.org/NetworkManager/unstable/spec.html#type-NM_802_11_AP_SEC
104 enum WirelessSecurityFlags {
105 NM_802_11_AP_SEC_NONE = 0x0,
106 NM_802_11_AP_SEC_PAIR_WEP40 = 0x1,
107 NM_802_11_AP_SEC_PAIR_WEP104 = 0x2,
108 NM_802_11_AP_SEC_PAIR_TKIP = 0x4,
109 NM_802_11_AP_SEC_PAIR_CCMP = 0x8,
110 NM_802_11_AP_SEC_GROUP_WEP40 = 0x10,
111 NM_802_11_AP_SEC_GROUP_WEP104 = 0x20,
112 NM_802_11_AP_SEC_GROUP_TKIP = 0x40,
113 NM_802_11_AP_SEC_GROUP_CCMP = 0x80,
114 NM_802_11_AP_SEC_KEY_MGMT_PSK = 0x100,
115 NM_802_11_AP_SEC_KEY_MGMT_802_1X = 0x200
118 // Initializes the DBus instance and the proxy object to the network manager.
119 // Must be called on |dbus_thread_|.
120 void Initialize();
122 // Enumerates all WiFi adapters and scans for access points on each.
123 // Results are appended into the provided |network_map|.
124 // Must be called on |dbus_thread_|.
125 void GetAllWiFiAccessPoints(bool configured_only,
126 bool visible_only,
127 int limit,
128 NetworkMap* network_map);
130 // Helper function for handling a scan request. This function acts similarly
131 // to the public GetNetworks to get visible networks and fire the
132 // OnNetworkListChanged event, however no callbacks are called.
133 bool GetNetworksForScanRequest();
135 // Initiates the connection to the network.
136 // Must be called on |dbus_thread_|.
137 void ConnectToNetwork(const std::string& guid, std::string* error);
139 // Initiates disconnection from the specified network.
140 // Must be called on |dbus_thread_|
141 void DisconnectFromNetwork(const std::string& guid, std::string* error);
143 // Checks whether the current thread is the DBus thread. If not, DCHECK will
144 // fail.
145 void AssertOnDBusThread();
147 // Verifies that NetworkManager interfaces are initialized.
148 // Returns true if NetworkManager is initialized, otherwise returns false
149 // and the API call will fail with |kErrorNotSupported|.
150 bool CheckNetworkManagerSupported(const FailureCallback& failure_callback);
152 // Gets all network devices on the system.
153 // Returns false if there is an error getting the device paths.
154 bool GetNetworkDevices(std::vector<dbus::ObjectPath>* device_paths);
156 // Returns the DeviceType (eg. WiFi, ethernet). corresponding to the
157 // |device_path|.
158 DeviceType GetDeviceType(const dbus::ObjectPath& device_path);
160 // Helper function to enumerate WiFi networks. Takes a path to a Wireless
161 // device, scans that device and appends networks to network_list.
162 // Returns false if there is an error getting the access points visible
163 // to the |device_path|.
164 bool AddAccessPointsFromDevice(const dbus::ObjectPath& device_path,
165 NetworkMap* network_map);
167 // Reply callback accepts the map of networks and fires the
168 // OnNetworkListChanged event and user callbacks.
169 void OnAccessPointsFound(scoped_ptr<NetworkMap> network_map,
170 const NetworkListCallback& success_callback,
171 const FailureCallback& failure_callback);
173 // Reply callback accepts the map of networks and fires the
174 // OnNetworkListChanged event.
175 void OnAccessPointsFoundViaScan(scoped_ptr<NetworkMap> network_map);
177 // Helper function for OnAccessPointsFound and OnAccessPointsFoundViaScan to
178 // fire the OnNetworkListChangedEvent.
179 void SendNetworkListChangedEvent(const base::ListValue& network_list);
181 // Gets a dictionary of information about the access point.
182 // Returns false if there is an error getting information about the
183 // supplied |access_point_path|.
184 bool GetAccessPointInfo(
185 const dbus::ObjectPath& access_point_path,
186 const scoped_ptr<base::DictionaryValue>& access_point_info);
188 // Helper function to extract a property from a device.
189 // Returns the dbus::Response object from calling Get on the supplied
190 // |property_name|.
191 scoped_ptr<dbus::Response> GetAccessPointProperty(
192 dbus::ObjectProxy* access_point_proxy,
193 const std::string& property_name);
195 // If the access_point is not already in the map it is added. Otherwise
196 // the access point is updated (eg. with the max of the signal
197 // strength).
198 void AddOrUpdateAccessPoint(NetworkMap* network_map,
199 const std::string& network_guid,
200 scoped_ptr<base::DictionaryValue>& access_point);
202 // Maps the WPA security flags to a human readable string.
203 void MapSecurityFlagsToString(uint32 securityFlags, std::string* security);
205 // Gets the connected access point path on the given device. Internally gets
206 // all active connections then checks if the device matches the requested
207 // device, then gets the access point associated with the connection.
208 // Returns false if there is an error getting the connected access point.
209 bool GetConnectedAccessPoint(dbus::ObjectPath device_path,
210 dbus::ObjectPath* access_point_path);
212 // Given a path to an active connection gets the path to the device
213 // that the connection belongs to. Returns false if there is an error getting
214 // the device corresponding to the supplied |connection_path|.
215 bool GetDeviceOfConnection(dbus::ObjectPath connection_path,
216 dbus::ObjectPath* device_path);
218 // Given a path to an active wireless connection gets the path to the
219 // access point associated with that connection.
220 // Returns false if there is an error getting the |access_point_path|
221 // corresponding to the supplied |connection_path|.
222 bool GetAccessPointForConnection(dbus::ObjectPath connection_path,
223 dbus::ObjectPath* access_point_path);
225 // Helper method to set the connection state in the |network_map_| and post
226 // a change event.
227 bool SetConnectionStateAndPostEvent(const std::string& guid,
228 const std::string& ssid,
229 const std::string& connection_state);
231 // Helper method to post an OnNetworkChanged event to the UI thread from the
232 // dbus thread. Used for connection status progress during |StartConnect|.
233 void PostOnNetworksChangedToUIThread(scoped_ptr<GuidList> guid_list);
235 // Helper method to be called from the UI thread and manage ownership of the
236 // passed vector from the |dbus_thread_|.
237 void OnNetworksChangedEventTask(scoped_ptr<GuidList> guid_list);
239 void GetCachedNetworkProperties(const std::string& guid,
240 base::DictionaryValue* properties,
241 std::string* error);
243 void OnNetworksChangedEventOnUIThread(const GuidList& network_guids);
245 void OnNetworkListChangedEventOnUIThread(const GuidList& network_guids);
247 // Browser context.
248 content::BrowserContext* browser_context_;
249 // Thread used for DBus actions.
250 base::Thread dbus_thread_;
251 // DBus instance. Only access on |dbus_thread_|.
252 scoped_refptr<dbus::Bus> dbus_;
253 // Task runner used by the |dbus_| object.
254 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
255 // This is owned by |dbus_| object. Only access on |dbus_thread_|.
256 dbus::ObjectProxy* network_manager_proxy_;
257 // Holds the current mapping of known networks. Only access on |dbus_thread_|.
258 scoped_ptr<NetworkMap> network_map_;
259 // Observers to Network Events.
260 ObserverList<NetworkingPrivateDelegateObserver> network_events_observers_;
262 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateLinux);
265 } // namespace extensions
267 #endif // EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_LINUX_H_