Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / extensions / browser / api / networking_private / networking_private_linux.h
blob80ee615a62018d597dab1bba88f84eb31113f223
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 GetNetworks(const std::string& network_type,
60 bool configured_only,
61 bool visible_only,
62 int limit,
63 const NetworkListCallback& success_callback,
64 const FailureCallback& failure_callback) override;
65 void StartConnect(const std::string& guid,
66 const VoidCallback& success_callback,
67 const FailureCallback& failure_callback) override;
68 void StartDisconnect(const std::string& guid,
69 const VoidCallback& success_callback,
70 const FailureCallback& failure_callback) override;
71 void SetWifiTDLSEnabledState(
72 const std::string& ip_or_mac_address,
73 bool enabled,
74 const StringCallback& success_callback,
75 const FailureCallback& failure_callback) override;
76 void GetWifiTDLSStatus(const std::string& ip_or_mac_address,
77 const StringCallback& success_callback,
78 const FailureCallback& failure_callback) override;
79 void GetCaptivePortalStatus(const std::string& guid,
80 const StringCallback& success_callback,
81 const FailureCallback& failure_callback) override;
82 scoped_ptr<base::ListValue> GetEnabledNetworkTypes() override;
83 bool EnableNetworkType(const std::string& type) override;
84 bool DisableNetworkType(const std::string& type) override;
85 bool RequestScan() override;
86 void AddObserver(NetworkingPrivateDelegateObserver* observer) override;
87 void RemoveObserver(NetworkingPrivateDelegateObserver* observer) override;
89 private:
90 ~NetworkingPrivateLinux() override;
92 // https://developer.gnome.org/NetworkManager/unstable/spec.html#type-NM_DEVICE_TYPE
93 enum DeviceType {
94 NM_DEVICE_TYPE_UNKNOWN = 0,
95 NM_DEVICE_TYPE_ETHERNET = 1,
96 NM_DEVICE_TYPE_WIFI = 2
99 // https://developer.gnome.org/NetworkManager/unstable/spec.html#type-NM_802_11_AP_SEC
100 enum WirelessSecurityFlags {
101 NM_802_11_AP_SEC_NONE = 0x0,
102 NM_802_11_AP_SEC_PAIR_WEP40 = 0x1,
103 NM_802_11_AP_SEC_PAIR_WEP104 = 0x2,
104 NM_802_11_AP_SEC_PAIR_TKIP = 0x4,
105 NM_802_11_AP_SEC_PAIR_CCMP = 0x8,
106 NM_802_11_AP_SEC_GROUP_WEP40 = 0x10,
107 NM_802_11_AP_SEC_GROUP_WEP104 = 0x20,
108 NM_802_11_AP_SEC_GROUP_TKIP = 0x40,
109 NM_802_11_AP_SEC_GROUP_CCMP = 0x80,
110 NM_802_11_AP_SEC_KEY_MGMT_PSK = 0x100,
111 NM_802_11_AP_SEC_KEY_MGMT_802_1X = 0x200
114 // Initializes the DBus instance and the proxy object to the network manager.
115 // Must be called on |dbus_thread_|.
116 void Initialize();
118 // Enumerates all WiFi adapters and scans for access points on each.
119 // Results are appended into the provided |network_map|.
120 // Must be called on |dbus_thread_|.
121 void GetAllWiFiAccessPoints(bool configured_only,
122 bool visible_only,
123 int limit,
124 NetworkMap* network_map);
126 // Helper function for handling a scan request. This function acts similarly
127 // to the public GetNetworks to get visible networks and fire the
128 // OnNetworkListChanged event, however no callbacks are called.
129 bool GetNetworksForScanRequest();
131 // Initiates the connection to the network.
132 // Must be called on |dbus_thread_|.
133 void ConnectToNetwork(const std::string& guid, std::string* error);
135 // Initiates disconnection from the specified network.
136 // Must be called on |dbus_thread_|
137 void DisconnectFromNetwork(const std::string& guid, std::string* error);
139 // Checks whether the current thread is the DBus thread. If not, DCHECK will
140 // fail.
141 void AssertOnDBusThread();
143 // Verifies that NetworkManager interfaces are initialized.
144 // Returns true if NetworkManager is initialized, otherwise returns false
145 // and the API call will fail with |kErrorNotSupported|.
146 bool CheckNetworkManagerSupported(const FailureCallback& failure_callback);
148 // Gets all network devices on the system.
149 // Returns false if there is an error getting the device paths.
150 bool GetNetworkDevices(std::vector<dbus::ObjectPath>* device_paths);
152 // Returns the DeviceType (eg. WiFi, ethernet). corresponding to the
153 // |device_path|.
154 DeviceType GetDeviceType(const dbus::ObjectPath& device_path);
156 // Helper function to enumerate WiFi networks. Takes a path to a Wireless
157 // device, scans that device and appends networks to network_list.
158 // Returns false if there is an error getting the access points visible
159 // to the |device_path|.
160 bool AddAccessPointsFromDevice(const dbus::ObjectPath& device_path,
161 NetworkMap* network_map);
163 // Reply callback accepts the map of networks and fires the
164 // OnNetworkListChanged event and user callbacks.
165 void OnAccessPointsFound(scoped_ptr<NetworkMap> network_map,
166 const NetworkListCallback& success_callback,
167 const FailureCallback& failure_callback);
169 // Reply callback accepts the map of networks and fires the
170 // OnNetworkListChanged event.
171 void OnAccessPointsFoundViaScan(scoped_ptr<NetworkMap> network_map);
173 // Helper function for OnAccessPointsFound and OnAccessPointsFoundViaScan to
174 // fire the OnNetworkListChangedEvent.
175 void SendNetworkListChangedEvent(const base::ListValue& network_list);
177 // Gets a dictionary of information about the access point.
178 // Returns false if there is an error getting information about the
179 // supplied |access_point_path|.
180 bool GetAccessPointInfo(
181 const dbus::ObjectPath& access_point_path,
182 const scoped_ptr<base::DictionaryValue>& access_point_info);
184 // Helper function to extract a property from a device.
185 // Returns the dbus::Response object from calling Get on the supplied
186 // |property_name|.
187 scoped_ptr<dbus::Response> GetAccessPointProperty(
188 dbus::ObjectProxy* access_point_proxy,
189 const std::string& property_name);
191 // If the access_point is not already in the map it is added. Otherwise
192 // the access point is updated (eg. with the max of the signal
193 // strength).
194 void AddOrUpdateAccessPoint(NetworkMap* network_map,
195 const std::string& network_guid,
196 scoped_ptr<base::DictionaryValue>& access_point);
198 // Maps the WPA security flags to a human readable string.
199 void MapSecurityFlagsToString(uint32 securityFlags, std::string* security);
201 // Gets the connected access point path on the given device. Internally gets
202 // all active connections then checks if the device matches the requested
203 // device, then gets the access point associated with the connection.
204 // Returns false if there is an error getting the connected access point.
205 bool GetConnectedAccessPoint(dbus::ObjectPath device_path,
206 dbus::ObjectPath* access_point_path);
208 // Given a path to an active connection gets the path to the device
209 // that the connection belongs to. Returns false if there is an error getting
210 // the device corresponding to the supplied |connection_path|.
211 bool GetDeviceOfConnection(dbus::ObjectPath connection_path,
212 dbus::ObjectPath* device_path);
214 // Given a path to an active wireless connection gets the path to the
215 // access point associated with that connection.
216 // Returns false if there is an error getting the |access_point_path|
217 // corresponding to the supplied |connection_path|.
218 bool GetAccessPointForConnection(dbus::ObjectPath connection_path,
219 dbus::ObjectPath* access_point_path);
221 // Helper method to set the connection state in the |network_map_| and post
222 // a change event.
223 bool SetConnectionStateAndPostEvent(const std::string& guid,
224 const std::string& ssid,
225 const std::string& connection_state);
227 // Helper method to post an OnNetworkChanged event to the UI thread from the
228 // dbus thread. Used for connection status progress during |StartConnect|.
229 void PostOnNetworksChangedToUIThread(scoped_ptr<GuidList> guid_list);
231 // Helper method to be called from the UI thread and manage ownership of the
232 // passed vector from the |dbus_thread_|.
233 void OnNetworksChangedEventTask(scoped_ptr<GuidList> guid_list);
235 void GetCachedNetworkProperties(const std::string& guid,
236 base::DictionaryValue* properties,
237 std::string* error);
239 void OnNetworksChangedEventOnUIThread(const GuidList& network_guids);
241 void OnNetworkListChangedEventOnUIThread(const GuidList& network_guids);
243 // Browser context.
244 content::BrowserContext* browser_context_;
245 // Thread used for DBus actions.
246 base::Thread dbus_thread_;
247 // DBus instance. Only access on |dbus_thread_|.
248 scoped_refptr<dbus::Bus> dbus_;
249 // Task runner used by the |dbus_| object.
250 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
251 // This is owned by |dbus_| object. Only access on |dbus_thread_|.
252 dbus::ObjectProxy* network_manager_proxy_;
253 // Holds the current mapping of known networks. Only access on |dbus_thread_|.
254 scoped_ptr<NetworkMap> network_map_;
255 // Observers to Network Events.
256 ObserverList<NetworkingPrivateDelegateObserver> network_events_observers_;
258 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateLinux);
261 } // namespace extensions
263 #endif // EXTENSIONS_BROWSER_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_LINUX_H_