OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Provides wifi scan API binding for suitable for typical linux distributions. | 5 // Provides wifi scan API binding for suitable for typical linux distributions. |
6 // Currently, only the NetworkManager API is used, accessed via D-Bus (in turn | 6 // Currently, only the NetworkManager API is used, accessed via D-Bus (in turn |
7 // accessed via the GLib wrapper). | 7 // accessed via the GLib wrapper). |
8 | 8 |
9 #include "content/browser/geolocation/wifi_data_provider_linux.h" | 9 #include "content/browser/geolocation/wifi_data_provider_linux.h" |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "dbus/bus.h" | 14 #include "dbus/bus.h" |
15 #include "dbus/message.h" | 15 #include "dbus/message.h" |
| 16 #include "dbus/object_path.h" |
16 #include "dbus/object_proxy.h" | 17 #include "dbus/object_proxy.h" |
17 | 18 |
18 namespace { | 19 namespace { |
19 // The time periods between successive polls of the wifi data. | 20 // The time periods between successive polls of the wifi data. |
20 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s | 21 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s |
21 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins | 22 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins |
22 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins | 23 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins |
23 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | 24 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s |
24 | 25 |
25 const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager"; | 26 const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager"; |
(...skipping 23 matching lines...) Expand all Loading... |
49 | 50 |
50 // WifiDataProviderCommon::WlanApiInterface | 51 // WifiDataProviderCommon::WlanApiInterface |
51 // | 52 // |
52 // This function makes blocking D-Bus calls, but it's totally fine as | 53 // This function makes blocking D-Bus calls, but it's totally fine as |
53 // the code runs in "Geolocation" thread, not the browser's UI thread. | 54 // the code runs in "Geolocation" thread, not the browser's UI thread. |
54 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data); | 55 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data); |
55 | 56 |
56 private: | 57 private: |
57 // Enumerates the list of available network adapter devices known to | 58 // Enumerates the list of available network adapter devices known to |
58 // NetworkManager. Return true on success. | 59 // NetworkManager. Return true on success. |
59 bool GetAdapterDeviceList(std::vector<std::string>* device_paths); | 60 bool GetAdapterDeviceList(std::vector<dbus::ObjectPath>* device_paths); |
60 | 61 |
61 // Given the NetworkManager path to a wireless adapater, dumps the wifi scan | 62 // Given the NetworkManager path to a wireless adapater, dumps the wifi scan |
62 // results and appends them to |data|. Returns false if a fatal error is | 63 // results and appends them to |data|. Returns false if a fatal error is |
63 // encountered such that the data set could not be populated. | 64 // encountered such that the data set could not be populated. |
64 bool GetAccessPointsForAdapter(const std::string& adapter_path, | 65 bool GetAccessPointsForAdapter(const dbus::ObjectPath& adapter_path, |
65 WifiData::AccessPointDataSet* data); | 66 WifiData::AccessPointDataSet* data); |
66 | 67 |
67 // Internal method used by |GetAccessPointsForAdapter|, given a wifi access | 68 // Internal method used by |GetAccessPointsForAdapter|, given a wifi access |
68 // point proxy retrieves the named property and returns it. Returns NULL if | 69 // point proxy retrieves the named property and returns it. Returns NULL if |
69 // the property could not be read. | 70 // the property could not be read. |
70 dbus::Response* GetAccessPointProperty(dbus::ObjectProxy* proxy, | 71 dbus::Response* GetAccessPointProperty(dbus::ObjectProxy* proxy, |
71 const std::string& property_name); | 72 const std::string& property_name); |
72 | 73 |
73 scoped_refptr<dbus::Bus> system_bus_; | 74 scoped_refptr<dbus::Bus> system_bus_; |
74 dbus::ObjectProxy* network_manager_proxy_; | 75 dbus::ObjectProxy* network_manager_proxy_; |
(...skipping 30 matching lines...) Expand all Loading... |
105 return InitWithBus(new dbus::Bus(options)); | 106 return InitWithBus(new dbus::Bus(options)); |
106 } | 107 } |
107 | 108 |
108 bool NetworkManagerWlanApi::InitWithBus(dbus::Bus* bus) { | 109 bool NetworkManagerWlanApi::InitWithBus(dbus::Bus* bus) { |
109 system_bus_ = bus; | 110 system_bus_ = bus; |
110 // system_bus_ will own all object proxies created from the bus. | 111 // system_bus_ will own all object proxies created from the bus. |
111 network_manager_proxy_ = | 112 network_manager_proxy_ = |
112 system_bus_->GetObjectProxy(kNetworkManagerServiceName, | 113 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
113 kNetworkManagerPath); | 114 kNetworkManagerPath); |
114 // Validate the proxy object by checking we can enumerate devices. | 115 // Validate the proxy object by checking we can enumerate devices. |
115 std::vector<std::string> adapter_paths; | 116 std::vector<dbus::ObjectPath> adapter_paths; |
116 const bool success = GetAdapterDeviceList(&adapter_paths); | 117 const bool success = GetAdapterDeviceList(&adapter_paths); |
117 VLOG(1) << "Init() result: " << success; | 118 VLOG(1) << "Init() result: " << success; |
118 return success; | 119 return success; |
119 } | 120 } |
120 | 121 |
121 bool NetworkManagerWlanApi::GetAccessPointData( | 122 bool NetworkManagerWlanApi::GetAccessPointData( |
122 WifiData::AccessPointDataSet* data) { | 123 WifiData::AccessPointDataSet* data) { |
123 std::vector<std::string> device_paths; | 124 std::vector<dbus::ObjectPath> device_paths; |
124 if (!GetAdapterDeviceList(&device_paths)) { | 125 if (!GetAdapterDeviceList(&device_paths)) { |
125 LOG(WARNING) << "Could not enumerate access points"; | 126 LOG(WARNING) << "Could not enumerate access points"; |
126 return false; | 127 return false; |
127 } | 128 } |
128 int success_count = 0; | 129 int success_count = 0; |
129 int fail_count = 0; | 130 int fail_count = 0; |
130 | 131 |
131 // Iterate the devices, getting APs for each wireless adapter found | 132 // Iterate the devices, getting APs for each wireless adapter found |
132 for (size_t i = 0; i < device_paths.size(); ++i) { | 133 for (size_t i = 0; i < device_paths.size(); ++i) { |
133 const std::string& device_path = device_paths[i]; | 134 const dbus::ObjectPath& device_path = device_paths[i]; |
134 VLOG(1) << "Checking device: " << device_path; | 135 VLOG(1) << "Checking device: " << device_path; |
135 | 136 |
136 dbus::ObjectProxy* device_proxy = | 137 dbus::ObjectProxy* device_proxy = |
137 system_bus_->GetObjectProxy(kNetworkManagerServiceName, | 138 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
138 device_path); | 139 device_path); |
139 | 140 |
140 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); | 141 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); |
141 dbus::MessageWriter builder(&method_call); | 142 dbus::MessageWriter builder(&method_call); |
142 builder.AppendString("org.freedesktop.NetworkManager.Device"); | 143 builder.AppendString("org.freedesktop.NetworkManager.Device"); |
143 builder.AppendString("DeviceType"); | 144 builder.AppendString("DeviceType"); |
(...skipping 19 matching lines...) Expand all Loading... |
163 ++success_count; | 164 ++success_count; |
164 else | 165 else |
165 ++fail_count; | 166 ++fail_count; |
166 } | 167 } |
167 } | 168 } |
168 // At least one successfull scan overrides any other adapter reporting error. | 169 // At least one successfull scan overrides any other adapter reporting error. |
169 return success_count || fail_count == 0; | 170 return success_count || fail_count == 0; |
170 } | 171 } |
171 | 172 |
172 bool NetworkManagerWlanApi::GetAdapterDeviceList( | 173 bool NetworkManagerWlanApi::GetAdapterDeviceList( |
173 std::vector<std::string>* device_paths) { | 174 std::vector<dbus::ObjectPath>* device_paths) { |
174 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices"); | 175 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices"); |
175 scoped_ptr<dbus::Response> response( | 176 scoped_ptr<dbus::Response> response( |
176 network_manager_proxy_->CallMethodAndBlock( | 177 network_manager_proxy_->CallMethodAndBlock( |
177 &method_call, | 178 &method_call, |
178 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); | 179 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
179 if (!response.get()) { | 180 if (!response.get()) { |
180 LOG(WARNING) << "Failed to get the device list"; | 181 LOG(WARNING) << "Failed to get the device list"; |
181 return false; | 182 return false; |
182 } | 183 } |
183 | 184 |
184 dbus::MessageReader reader(response.get()); | 185 dbus::MessageReader reader(response.get()); |
185 if (!reader.PopArrayOfObjectPaths(device_paths)) { | 186 if (!reader.PopArrayOfObjectPaths(device_paths)) { |
186 LOG(WARNING) << "Unexpected response: " << response->ToString(); | 187 LOG(WARNING) << "Unexpected response: " << response->ToString(); |
187 return false; | 188 return false; |
188 } | 189 } |
189 return true; | 190 return true; |
190 } | 191 } |
191 | 192 |
192 | 193 |
193 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( | 194 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( |
194 const std::string& adapter_path, WifiData::AccessPointDataSet* data) { | 195 const dbus::ObjectPath& adapter_path, WifiData::AccessPointDataSet* data) { |
195 // Create a proxy object for this wifi adapter, and ask it to do a scan | 196 // Create a proxy object for this wifi adapter, and ask it to do a scan |
196 // (or at least, dump its scan results). | 197 // (or at least, dump its scan results). |
197 dbus::ObjectProxy* device_proxy = | 198 dbus::ObjectProxy* device_proxy = |
198 system_bus_->GetObjectProxy(kNetworkManagerServiceName, | 199 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
199 adapter_path); | 200 adapter_path); |
200 dbus::MethodCall method_call( | 201 dbus::MethodCall method_call( |
201 "org.freedesktop.NetworkManager.Device.Wireless", | 202 "org.freedesktop.NetworkManager.Device.Wireless", |
202 "GetAccessPoints"); | 203 "GetAccessPoints"); |
203 scoped_ptr<dbus::Response> response( | 204 scoped_ptr<dbus::Response> response( |
204 device_proxy->CallMethodAndBlock( | 205 device_proxy->CallMethodAndBlock( |
205 &method_call, | 206 &method_call, |
206 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); | 207 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
207 if (!response.get()) { | 208 if (!response.get()) { |
208 LOG(WARNING) << "Failed to get access points data for " << adapter_path; | 209 LOG(WARNING) << "Failed to get access points data for " << adapter_path; |
209 return false; | 210 return false; |
210 } | 211 } |
211 dbus::MessageReader reader(response.get()); | 212 dbus::MessageReader reader(response.get()); |
212 std::vector<std::string> access_point_paths; | 213 std::vector<dbus::ObjectPath> access_point_paths; |
213 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { | 214 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { |
214 LOG(WARNING) << "Unexpected response for " << adapter_path << ": " | 215 LOG(WARNING) << "Unexpected response for " << adapter_path << ": " |
215 << response->ToString(); | 216 << response->ToString(); |
216 return false; | 217 return false; |
217 } | 218 } |
218 | 219 |
219 VLOG(1) << "Wireless adapter " << adapter_path << " found " | 220 VLOG(1) << "Wireless adapter " << adapter_path << " found " |
220 << access_point_paths.size() << " access points."; | 221 << access_point_paths.size() << " access points."; |
221 | 222 |
222 for (size_t i = 0; i < access_point_paths.size(); ++i) { | 223 for (size_t i = 0; i < access_point_paths.size(); ++i) { |
223 const std::string& access_point_path = access_point_paths[i]; | 224 const dbus::ObjectPath& access_point_path = access_point_paths[i]; |
224 VLOG(1) << "Checking access point: " << access_point_path; | 225 VLOG(1) << "Checking access point: " << access_point_path; |
225 | 226 |
226 dbus::ObjectProxy* access_point_proxy = | 227 dbus::ObjectProxy* access_point_proxy = |
227 system_bus_->GetObjectProxy(kNetworkManagerServiceName, | 228 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
228 access_point_path); | 229 access_point_path); |
229 | 230 |
230 AccessPointData access_point_data; | 231 AccessPointData access_point_data; |
231 { | 232 { |
232 scoped_ptr<dbus::Response> response( | 233 scoped_ptr<dbus::Response> response( |
233 GetAccessPointProperty(access_point_proxy, "Ssid")); | 234 GetAccessPointProperty(access_point_proxy, "Ssid")); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 kNoWifiPollingIntervalMilliseconds>; | 366 kNoWifiPollingIntervalMilliseconds>; |
366 } | 367 } |
367 | 368 |
368 WifiDataProviderCommon::WlanApiInterface* | 369 WifiDataProviderCommon::WlanApiInterface* |
369 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { | 370 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { |
370 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); | 371 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); |
371 if (wlan_api->InitWithBus(bus)) | 372 if (wlan_api->InitWithBus(bus)) |
372 return wlan_api.release(); | 373 return wlan_api.release(); |
373 return NULL; | 374 return NULL; |
374 } | 375 } |
OLD | NEW |