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_
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"
28 namespace extensions
{
30 // Linux NetworkingPrivateDelegate implementation.
31 class NetworkingPrivateLinux
: public NetworkingPrivateDelegate
{
33 typedef std::map
<base::string16
, linked_ptr
<base::DictionaryValue
>>
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
,
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
,
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 void UnlockCellularSim(const std::string
& guid
,
86 const std::string
& pin
,
87 const std::string
& puk
,
88 const VoidCallback
& success_callback
,
89 const FailureCallback
& failure_callback
) override
;
90 void SetCellularSimState(const std::string
& guid
,
92 const std::string
& current_pin
,
93 const std::string
& new_pin
,
94 const VoidCallback
& success_callback
,
95 const FailureCallback
& failure_callback
) override
;
97 scoped_ptr
<base::ListValue
> GetEnabledNetworkTypes() override
;
98 scoped_ptr
<DeviceStateList
> GetDeviceStateList() override
;
99 bool EnableNetworkType(const std::string
& type
) override
;
100 bool DisableNetworkType(const std::string
& type
) override
;
101 bool RequestScan() override
;
102 void AddObserver(NetworkingPrivateDelegateObserver
* observer
) override
;
103 void RemoveObserver(NetworkingPrivateDelegateObserver
* observer
) override
;
106 ~NetworkingPrivateLinux() override
;
108 // https://developer.gnome.org/NetworkManager/unstable/spec.html#type-NM_DEVICE_TYPE
110 NM_DEVICE_TYPE_UNKNOWN
= 0,
111 NM_DEVICE_TYPE_ETHERNET
= 1,
112 NM_DEVICE_TYPE_WIFI
= 2
115 // https://developer.gnome.org/NetworkManager/unstable/spec.html#type-NM_802_11_AP_SEC
116 enum WirelessSecurityFlags
{
117 NM_802_11_AP_SEC_NONE
= 0x0,
118 NM_802_11_AP_SEC_PAIR_WEP40
= 0x1,
119 NM_802_11_AP_SEC_PAIR_WEP104
= 0x2,
120 NM_802_11_AP_SEC_PAIR_TKIP
= 0x4,
121 NM_802_11_AP_SEC_PAIR_CCMP
= 0x8,
122 NM_802_11_AP_SEC_GROUP_WEP40
= 0x10,
123 NM_802_11_AP_SEC_GROUP_WEP104
= 0x20,
124 NM_802_11_AP_SEC_GROUP_TKIP
= 0x40,
125 NM_802_11_AP_SEC_GROUP_CCMP
= 0x80,
126 NM_802_11_AP_SEC_KEY_MGMT_PSK
= 0x100,
127 NM_802_11_AP_SEC_KEY_MGMT_802_1X
= 0x200
130 // Initializes the DBus instance and the proxy object to the network manager.
131 // Must be called on |dbus_thread_|.
134 // Enumerates all WiFi adapters and scans for access points on each.
135 // Results are appended into the provided |network_map|.
136 // Must be called on |dbus_thread_|.
137 void GetAllWiFiAccessPoints(bool configured_only
,
140 NetworkMap
* network_map
);
142 // Helper function for handling a scan request. This function acts similarly
143 // to the public GetNetworks to get visible networks and fire the
144 // OnNetworkListChanged event, however no callbacks are called.
145 bool GetNetworksForScanRequest();
147 // Initiates the connection to the network.
148 // Must be called on |dbus_thread_|.
149 void ConnectToNetwork(const std::string
& guid
, std::string
* error
);
151 // Initiates disconnection from the specified network.
152 // Must be called on |dbus_thread_|
153 void DisconnectFromNetwork(const std::string
& guid
, std::string
* error
);
155 // Checks whether the current thread is the DBus thread. If not, DCHECK will
157 void AssertOnDBusThread();
159 // Verifies that NetworkManager interfaces are initialized.
160 // Returns true if NetworkManager is initialized, otherwise returns false
161 // and the API call will fail with |kErrorNotSupported|.
162 bool CheckNetworkManagerSupported(const FailureCallback
& failure_callback
);
164 // Gets all network devices on the system.
165 // Returns false if there is an error getting the device paths.
166 bool GetNetworkDevices(std::vector
<dbus::ObjectPath
>* device_paths
);
168 // Returns the DeviceType (eg. WiFi, ethernet). corresponding to the
170 DeviceType
GetDeviceType(const dbus::ObjectPath
& device_path
);
172 // Helper function to enumerate WiFi networks. Takes a path to a Wireless
173 // device, scans that device and appends networks to network_list.
174 // Returns false if there is an error getting the access points visible
175 // to the |device_path|.
176 bool AddAccessPointsFromDevice(const dbus::ObjectPath
& device_path
,
177 NetworkMap
* network_map
);
179 // Reply callback accepts the map of networks and fires the
180 // OnNetworkListChanged event and user callbacks.
181 void OnAccessPointsFound(scoped_ptr
<NetworkMap
> network_map
,
182 const NetworkListCallback
& success_callback
,
183 const FailureCallback
& failure_callback
);
185 // Reply callback accepts the map of networks and fires the
186 // OnNetworkListChanged event.
187 void OnAccessPointsFoundViaScan(scoped_ptr
<NetworkMap
> network_map
);
189 // Helper function for OnAccessPointsFound and OnAccessPointsFoundViaScan to
190 // fire the OnNetworkListChangedEvent.
191 void SendNetworkListChangedEvent(const base::ListValue
& network_list
);
193 // Gets a dictionary of information about the access point.
194 // Returns false if there is an error getting information about the
195 // supplied |access_point_path|.
196 bool GetAccessPointInfo(
197 const dbus::ObjectPath
& access_point_path
,
198 const scoped_ptr
<base::DictionaryValue
>& access_point_info
);
200 // Helper function to extract a property from a device.
201 // Returns the dbus::Response object from calling Get on the supplied
203 scoped_ptr
<dbus::Response
> GetAccessPointProperty(
204 dbus::ObjectProxy
* access_point_proxy
,
205 const std::string
& property_name
);
207 // If the access_point is not already in the map it is added. Otherwise
208 // the access point is updated (eg. with the max of the signal
210 void AddOrUpdateAccessPoint(NetworkMap
* network_map
,
211 const std::string
& network_guid
,
212 scoped_ptr
<base::DictionaryValue
>& access_point
);
214 // Maps the WPA security flags to a human readable string.
215 void MapSecurityFlagsToString(uint32 securityFlags
, std::string
* security
);
217 // Gets the connected access point path on the given device. Internally gets
218 // all active connections then checks if the device matches the requested
219 // device, then gets the access point associated with the connection.
220 // Returns false if there is an error getting the connected access point.
221 bool GetConnectedAccessPoint(dbus::ObjectPath device_path
,
222 dbus::ObjectPath
* access_point_path
);
224 // Given a path to an active connection gets the path to the device
225 // that the connection belongs to. Returns false if there is an error getting
226 // the device corresponding to the supplied |connection_path|.
227 bool GetDeviceOfConnection(dbus::ObjectPath connection_path
,
228 dbus::ObjectPath
* device_path
);
230 // Given a path to an active wireless connection gets the path to the
231 // access point associated with that connection.
232 // Returns false if there is an error getting the |access_point_path|
233 // corresponding to the supplied |connection_path|.
234 bool GetAccessPointForConnection(dbus::ObjectPath connection_path
,
235 dbus::ObjectPath
* access_point_path
);
237 // Helper method to set the connection state in the |network_map_| and post
239 bool SetConnectionStateAndPostEvent(const std::string
& guid
,
240 const std::string
& ssid
,
241 const std::string
& connection_state
);
243 // Helper method to post an OnNetworkChanged event to the UI thread from the
244 // dbus thread. Used for connection status progress during |StartConnect|.
245 void PostOnNetworksChangedToUIThread(scoped_ptr
<GuidList
> guid_list
);
247 // Helper method to be called from the UI thread and manage ownership of the
248 // passed vector from the |dbus_thread_|.
249 void OnNetworksChangedEventTask(scoped_ptr
<GuidList
> guid_list
);
251 void GetCachedNetworkProperties(const std::string
& guid
,
252 base::DictionaryValue
* properties
,
255 void OnNetworksChangedEventOnUIThread(const GuidList
& network_guids
);
257 void OnNetworkListChangedEventOnUIThread(const GuidList
& network_guids
);
260 content::BrowserContext
* browser_context_
;
261 // Thread used for DBus actions.
262 base::Thread dbus_thread_
;
263 // DBus instance. Only access on |dbus_thread_|.
264 scoped_refptr
<dbus::Bus
> dbus_
;
265 // Task runner used by the |dbus_| object.
266 scoped_refptr
<base::SequencedTaskRunner
> dbus_task_runner_
;
267 // This is owned by |dbus_| object. Only access on |dbus_thread_|.
268 dbus::ObjectProxy
* network_manager_proxy_
;
269 // Holds the current mapping of known networks. Only access on |dbus_thread_|.
270 scoped_ptr
<NetworkMap
> network_map_
;
271 // Observers to Network Events.
272 base::ObserverList
<NetworkingPrivateDelegateObserver
>
273 network_events_observers_
;
275 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateLinux
);
278 } // namespace extensions
280 #endif // EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_LINUX_H_