Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: content/browser/geolocation/wifi_data_provider_linux.cc

Issue 9363045: Revert 121920 - dbus: add ObjectPath type (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 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"
17 #include "dbus/object_proxy.h" 16 #include "dbus/object_proxy.h"
18 17
19 namespace { 18 namespace {
20 // The time periods between successive polls of the wifi data. 19 // The time periods between successive polls of the wifi data.
21 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s 20 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s
22 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins 21 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins
23 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins 22 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins
24 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s 23 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s
25 24
26 const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager"; 25 const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager";
(...skipping 23 matching lines...) Expand all
50 49
51 // WifiDataProviderCommon::WlanApiInterface 50 // WifiDataProviderCommon::WlanApiInterface
52 // 51 //
53 // This function makes blocking D-Bus calls, but it's totally fine as 52 // This function makes blocking D-Bus calls, but it's totally fine as
54 // the code runs in "Geolocation" thread, not the browser's UI thread. 53 // the code runs in "Geolocation" thread, not the browser's UI thread.
55 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data); 54 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data);
56 55
57 private: 56 private:
58 // Enumerates the list of available network adapter devices known to 57 // Enumerates the list of available network adapter devices known to
59 // NetworkManager. Return true on success. 58 // NetworkManager. Return true on success.
60 bool GetAdapterDeviceList(std::vector<dbus::ObjectPath>* device_paths); 59 bool GetAdapterDeviceList(std::vector<std::string>* device_paths);
61 60
62 // Given the NetworkManager path to a wireless adapater, dumps the wifi scan 61 // Given the NetworkManager path to a wireless adapater, dumps the wifi scan
63 // results and appends them to |data|. Returns false if a fatal error is 62 // results and appends them to |data|. Returns false if a fatal error is
64 // encountered such that the data set could not be populated. 63 // encountered such that the data set could not be populated.
65 bool GetAccessPointsForAdapter(const dbus::ObjectPath& adapter_path, 64 bool GetAccessPointsForAdapter(const std::string& adapter_path,
66 WifiData::AccessPointDataSet* data); 65 WifiData::AccessPointDataSet* data);
67 66
68 // Internal method used by |GetAccessPointsForAdapter|, given a wifi access 67 // Internal method used by |GetAccessPointsForAdapter|, given a wifi access
69 // point proxy retrieves the named property and returns it. Returns NULL if 68 // point proxy retrieves the named property and returns it. Returns NULL if
70 // the property could not be read. 69 // the property could not be read.
71 dbus::Response* GetAccessPointProperty(dbus::ObjectProxy* proxy, 70 dbus::Response* GetAccessPointProperty(dbus::ObjectProxy* proxy,
72 const std::string& property_name); 71 const std::string& property_name);
73 72
74 scoped_refptr<dbus::Bus> system_bus_; 73 scoped_refptr<dbus::Bus> system_bus_;
75 dbus::ObjectProxy* network_manager_proxy_; 74 dbus::ObjectProxy* network_manager_proxy_;
(...skipping 28 matching lines...) Expand all
104 options.bus_type = dbus::Bus::SYSTEM; 103 options.bus_type = dbus::Bus::SYSTEM;
105 options.connection_type = dbus::Bus::PRIVATE; 104 options.connection_type = dbus::Bus::PRIVATE;
106 return InitWithBus(new dbus::Bus(options)); 105 return InitWithBus(new dbus::Bus(options));
107 } 106 }
108 107
109 bool NetworkManagerWlanApi::InitWithBus(dbus::Bus* bus) { 108 bool NetworkManagerWlanApi::InitWithBus(dbus::Bus* bus) {
110 system_bus_ = bus; 109 system_bus_ = bus;
111 // system_bus_ will own all object proxies created from the bus. 110 // system_bus_ will own all object proxies created from the bus.
112 network_manager_proxy_ = 111 network_manager_proxy_ =
113 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 112 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
114 dbus::ObjectPath(kNetworkManagerPath)); 113 kNetworkManagerPath);
115 // Validate the proxy object by checking we can enumerate devices. 114 // Validate the proxy object by checking we can enumerate devices.
116 std::vector<dbus::ObjectPath> adapter_paths; 115 std::vector<std::string> adapter_paths;
117 const bool success = GetAdapterDeviceList(&adapter_paths); 116 const bool success = GetAdapterDeviceList(&adapter_paths);
118 VLOG(1) << "Init() result: " << success; 117 VLOG(1) << "Init() result: " << success;
119 return success; 118 return success;
120 } 119 }
121 120
122 bool NetworkManagerWlanApi::GetAccessPointData( 121 bool NetworkManagerWlanApi::GetAccessPointData(
123 WifiData::AccessPointDataSet* data) { 122 WifiData::AccessPointDataSet* data) {
124 std::vector<dbus::ObjectPath> device_paths; 123 std::vector<std::string> device_paths;
125 if (!GetAdapterDeviceList(&device_paths)) { 124 if (!GetAdapterDeviceList(&device_paths)) {
126 LOG(WARNING) << "Could not enumerate access points"; 125 LOG(WARNING) << "Could not enumerate access points";
127 return false; 126 return false;
128 } 127 }
129 int success_count = 0; 128 int success_count = 0;
130 int fail_count = 0; 129 int fail_count = 0;
131 130
132 // Iterate the devices, getting APs for each wireless adapter found 131 // Iterate the devices, getting APs for each wireless adapter found
133 for (size_t i = 0; i < device_paths.size(); ++i) { 132 for (size_t i = 0; i < device_paths.size(); ++i) {
134 const dbus::ObjectPath& device_path = device_paths[i]; 133 const std::string& device_path = device_paths[i];
135 VLOG(1) << "Checking device: " << device_path.value(); 134 VLOG(1) << "Checking device: " << device_path;
136 135
137 dbus::ObjectProxy* device_proxy = 136 dbus::ObjectProxy* device_proxy =
138 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 137 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
139 device_path); 138 device_path);
140 139
141 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); 140 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get");
142 dbus::MessageWriter builder(&method_call); 141 dbus::MessageWriter builder(&method_call);
143 builder.AppendString("org.freedesktop.NetworkManager.Device"); 142 builder.AppendString("org.freedesktop.NetworkManager.Device");
144 builder.AppendString("DeviceType"); 143 builder.AppendString("DeviceType");
145 scoped_ptr<dbus::Response> response( 144 scoped_ptr<dbus::Response> response(
146 device_proxy->CallMethodAndBlock( 145 device_proxy->CallMethodAndBlock(
147 &method_call, 146 &method_call,
148 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); 147 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
149 if (!response.get()) { 148 if (!response.get()) {
150 LOG(WARNING) << "Failed to get the device type for " 149 LOG(WARNING) << "Failed to get the device type for " << device_path;
151 << device_path.value();
152 continue; // Check the next device. 150 continue; // Check the next device.
153 } 151 }
154 dbus::MessageReader reader(response.get()); 152 dbus::MessageReader reader(response.get());
155 uint32 device_type = 0; 153 uint32 device_type = 0;
156 if (!reader.PopVariantOfUint32(&device_type)) { 154 if (!reader.PopVariantOfUint32(&device_type)) {
157 LOG(WARNING) << "Unexpected response for " << device_type << ": " 155 LOG(WARNING) << "Unexpected response for " << device_type << ": "
158 << response->ToString(); 156 << response->ToString();
159 continue; // Check the next device. 157 continue; // Check the next device.
160 } 158 }
161 VLOG(1) << "Device type: " << device_type; 159 VLOG(1) << "Device type: " << device_type;
162 160
163 if (device_type == NM_DEVICE_TYPE_WIFI) { // Found a wlan adapter 161 if (device_type == NM_DEVICE_TYPE_WIFI) { // Found a wlan adapter
164 if (GetAccessPointsForAdapter(device_path, data)) 162 if (GetAccessPointsForAdapter(device_path, data))
165 ++success_count; 163 ++success_count;
166 else 164 else
167 ++fail_count; 165 ++fail_count;
168 } 166 }
169 } 167 }
170 // At least one successfull scan overrides any other adapter reporting error. 168 // At least one successfull scan overrides any other adapter reporting error.
171 return success_count || fail_count == 0; 169 return success_count || fail_count == 0;
172 } 170 }
173 171
174 bool NetworkManagerWlanApi::GetAdapterDeviceList( 172 bool NetworkManagerWlanApi::GetAdapterDeviceList(
175 std::vector<dbus::ObjectPath>* device_paths) { 173 std::vector<std::string>* device_paths) {
176 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices"); 174 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices");
177 scoped_ptr<dbus::Response> response( 175 scoped_ptr<dbus::Response> response(
178 network_manager_proxy_->CallMethodAndBlock( 176 network_manager_proxy_->CallMethodAndBlock(
179 &method_call, 177 &method_call,
180 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); 178 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
181 if (!response.get()) { 179 if (!response.get()) {
182 LOG(WARNING) << "Failed to get the device list"; 180 LOG(WARNING) << "Failed to get the device list";
183 return false; 181 return false;
184 } 182 }
185 183
186 dbus::MessageReader reader(response.get()); 184 dbus::MessageReader reader(response.get());
187 if (!reader.PopArrayOfObjectPaths(device_paths)) { 185 if (!reader.PopArrayOfObjectPaths(device_paths)) {
188 LOG(WARNING) << "Unexpected response: " << response->ToString(); 186 LOG(WARNING) << "Unexpected response: " << response->ToString();
189 return false; 187 return false;
190 } 188 }
191 return true; 189 return true;
192 } 190 }
193 191
194 192
195 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( 193 bool NetworkManagerWlanApi::GetAccessPointsForAdapter(
196 const dbus::ObjectPath& adapter_path, WifiData::AccessPointDataSet* data) { 194 const std::string& adapter_path, WifiData::AccessPointDataSet* data) {
197 // Create a proxy object for this wifi adapter, and ask it to do a scan 195 // Create a proxy object for this wifi adapter, and ask it to do a scan
198 // (or at least, dump its scan results). 196 // (or at least, dump its scan results).
199 dbus::ObjectProxy* device_proxy = 197 dbus::ObjectProxy* device_proxy =
200 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 198 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
201 adapter_path); 199 adapter_path);
202 dbus::MethodCall method_call( 200 dbus::MethodCall method_call(
203 "org.freedesktop.NetworkManager.Device.Wireless", 201 "org.freedesktop.NetworkManager.Device.Wireless",
204 "GetAccessPoints"); 202 "GetAccessPoints");
205 scoped_ptr<dbus::Response> response( 203 scoped_ptr<dbus::Response> response(
206 device_proxy->CallMethodAndBlock( 204 device_proxy->CallMethodAndBlock(
207 &method_call, 205 &method_call,
208 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); 206 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
209 if (!response.get()) { 207 if (!response.get()) {
210 LOG(WARNING) << "Failed to get access points data for " 208 LOG(WARNING) << "Failed to get access points data for " << adapter_path;
211 << adapter_path.value();
212 return false; 209 return false;
213 } 210 }
214 dbus::MessageReader reader(response.get()); 211 dbus::MessageReader reader(response.get());
215 std::vector<dbus::ObjectPath> access_point_paths; 212 std::vector<std::string> access_point_paths;
216 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { 213 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) {
217 LOG(WARNING) << "Unexpected response for " << adapter_path.value() << ": " 214 LOG(WARNING) << "Unexpected response for " << adapter_path << ": "
218 << response->ToString(); 215 << response->ToString();
219 return false; 216 return false;
220 } 217 }
221 218
222 VLOG(1) << "Wireless adapter " << adapter_path.value() << " found " 219 VLOG(1) << "Wireless adapter " << adapter_path << " found "
223 << access_point_paths.size() << " access points."; 220 << access_point_paths.size() << " access points.";
224 221
225 for (size_t i = 0; i < access_point_paths.size(); ++i) { 222 for (size_t i = 0; i < access_point_paths.size(); ++i) {
226 const dbus::ObjectPath& access_point_path = access_point_paths[i]; 223 const std::string& access_point_path = access_point_paths[i];
227 VLOG(1) << "Checking access point: " << access_point_path.value(); 224 VLOG(1) << "Checking access point: " << access_point_path;
228 225
229 dbus::ObjectProxy* access_point_proxy = 226 dbus::ObjectProxy* access_point_proxy =
230 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 227 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
231 access_point_path); 228 access_point_path);
232 229
233 AccessPointData access_point_data; 230 AccessPointData access_point_data;
234 { 231 {
235 scoped_ptr<dbus::Response> response( 232 scoped_ptr<dbus::Response> response(
236 GetAccessPointProperty(access_point_proxy, "Ssid")); 233 GetAccessPointProperty(access_point_proxy, "Ssid"));
237 if (!response.get()) 234 if (!response.get())
238 continue; 235 continue;
239 // The response should contain a variant that contains an array of bytes. 236 // The response should contain a variant that contains an array of bytes.
240 dbus::MessageReader reader(response.get()); 237 dbus::MessageReader reader(response.get());
241 dbus::MessageReader variant_reader(response.get()); 238 dbus::MessageReader variant_reader(response.get());
242 if (!reader.PopVariant(&variant_reader)) { 239 if (!reader.PopVariant(&variant_reader)) {
243 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 240 LOG(WARNING) << "Unexpected response for " << access_point_path << ": "
244 << ": " << response->ToString(); 241 << response->ToString();
245 continue; 242 continue;
246 } 243 }
247 uint8* ssid_bytes = NULL; 244 uint8* ssid_bytes = NULL;
248 size_t ssid_length = 0; 245 size_t ssid_length = 0;
249 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) { 246 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) {
250 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 247 LOG(WARNING) << "Unexpected response for " << access_point_path << ": "
251 << ": " << response->ToString(); 248 << response->ToString();
252 continue; 249 continue;
253 } 250 }
254 std::string ssid(ssid_bytes, ssid_bytes + ssid_length); 251 std::string ssid(ssid_bytes, ssid_bytes + ssid_length);
255 access_point_data.ssid = UTF8ToUTF16(ssid); 252 access_point_data.ssid = UTF8ToUTF16(ssid);
256 } 253 }
257 254
258 { // Read the mac address 255 { // Read the mac address
259 scoped_ptr<dbus::Response> response( 256 scoped_ptr<dbus::Response> response(
260 GetAccessPointProperty(access_point_proxy, "HwAddress")); 257 GetAccessPointProperty(access_point_proxy, "HwAddress"));
261 if (!response.get()) 258 if (!response.get())
262 continue; 259 continue;
263 dbus::MessageReader reader(response.get()); 260 dbus::MessageReader reader(response.get());
264 std::string mac; 261 std::string mac;
265 if (!reader.PopVariantOfString(&mac)) { 262 if (!reader.PopVariantOfString(&mac)) {
266 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 263 LOG(WARNING) << "Unexpected response for " << access_point_path << ": "
267 << ": " << response->ToString(); 264 << response->ToString();
268 continue; 265 continue;
269 } 266 }
270 267
271 ReplaceSubstringsAfterOffset(&mac, 0U, ":", ""); 268 ReplaceSubstringsAfterOffset(&mac, 0U, ":", "");
272 std::vector<uint8> mac_bytes; 269 std::vector<uint8> mac_bytes;
273 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) { 270 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) {
274 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size() 271 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size()
275 << " bytes) so using raw string: " << mac; 272 << " bytes) so using raw string: " << mac;
276 access_point_data.mac_address = UTF8ToUTF16(mac); 273 access_point_data.mac_address = UTF8ToUTF16(mac);
277 } else { 274 } else {
278 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]); 275 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]);
279 } 276 }
280 } 277 }
281 278
282 { // Read signal strength. 279 { // Read signal strength.
283 scoped_ptr<dbus::Response> response( 280 scoped_ptr<dbus::Response> response(
284 GetAccessPointProperty(access_point_proxy, "Strength")); 281 GetAccessPointProperty(access_point_proxy, "Strength"));
285 if (!response.get()) 282 if (!response.get())
286 continue; 283 continue;
287 dbus::MessageReader reader(response.get()); 284 dbus::MessageReader reader(response.get());
288 uint8 strength = 0; 285 uint8 strength = 0;
289 if (!reader.PopVariantOfByte(&strength)) { 286 if (!reader.PopVariantOfByte(&strength)) {
290 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 287 LOG(WARNING) << "Unexpected response for " << access_point_path << ": "
291 << ": " << response->ToString(); 288 << response->ToString();
292 continue; 289 continue;
293 } 290 }
294 // Convert strength as a percentage into dBs. 291 // Convert strength as a percentage into dBs.
295 access_point_data.radio_signal_strength = -100 + strength / 2; 292 access_point_data.radio_signal_strength = -100 + strength / 2;
296 } 293 }
297 294
298 { // Read the channel 295 { // Read the channel
299 scoped_ptr<dbus::Response> response( 296 scoped_ptr<dbus::Response> response(
300 GetAccessPointProperty(access_point_proxy, "Frequency")); 297 GetAccessPointProperty(access_point_proxy, "Frequency"));
301 if (!response.get()) 298 if (!response.get())
302 continue; 299 continue;
303 dbus::MessageReader reader(response.get()); 300 dbus::MessageReader reader(response.get());
304 uint32 frequency = 0; 301 uint32 frequency = 0;
305 if (!reader.PopVariantOfUint32(&frequency)) { 302 if (!reader.PopVariantOfUint32(&frequency)) {
306 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 303 LOG(WARNING) << "Unexpected response for " << access_point_path << ": "
307 << ": " << response->ToString(); 304 << response->ToString();
308 continue; 305 continue;
309 } 306 }
310 307
311 // NetworkManager returns frequency in MHz. 308 // NetworkManager returns frequency in MHz.
312 access_point_data.channel = 309 access_point_data.channel =
313 frquency_in_khz_to_channel(frequency * 1000); 310 frquency_in_khz_to_channel(frequency * 1000);
314 } 311 }
315 VLOG(1) << "Access point data of " << access_point_path.value() << ": " 312 VLOG(1) << "Access point data of " << access_point_path << ": "
316 << "SSID: " << access_point_data.ssid << ", " 313 << "SSID: " << access_point_data.ssid << ", "
317 << "MAC: " << access_point_data.mac_address << ", " 314 << "MAC: " << access_point_data.mac_address << ", "
318 << "Strength: " << access_point_data.radio_signal_strength << ", " 315 << "Strength: " << access_point_data.radio_signal_strength << ", "
319 << "Channel: " << access_point_data.channel; 316 << "Channel: " << access_point_data.channel;
320 317
321 data->insert(access_point_data); 318 data->insert(access_point_data);
322 } 319 }
323 return true; 320 return true;
324 } 321 }
325 322
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 kNoWifiPollingIntervalMilliseconds>; 365 kNoWifiPollingIntervalMilliseconds>;
369 } 366 }
370 367
371 WifiDataProviderCommon::WlanApiInterface* 368 WifiDataProviderCommon::WlanApiInterface*
372 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { 369 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) {
373 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); 370 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi);
374 if (wlan_api->InitWithBus(bus)) 371 if (wlan_api->InitWithBus(bus))
375 return wlan_api.release(); 372 return wlan_api.release();
376 return NULL; 373 return NULL;
377 } 374 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698