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

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

Issue 9378039: dbus: add ObjectPath type (Closed) Base URL: http://git.chromium.org/git/chromium/src@master
Patch Set: add patch for cryptohome_client 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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
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 28 matching lines...) Expand all
103 options.bus_type = dbus::Bus::SYSTEM; 104 options.bus_type = dbus::Bus::SYSTEM;
104 options.connection_type = dbus::Bus::PRIVATE; 105 options.connection_type = dbus::Bus::PRIVATE;
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 dbus::ObjectPath(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.value();
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");
144 scoped_ptr<dbus::Response> response( 145 scoped_ptr<dbus::Response> response(
145 device_proxy->CallMethodAndBlock( 146 device_proxy->CallMethodAndBlock(
146 &method_call, 147 &method_call,
147 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); 148 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
148 if (!response.get()) { 149 if (!response.get()) {
149 LOG(WARNING) << "Failed to get the device type for " << device_path; 150 LOG(WARNING) << "Failed to get the device type for "
151 << device_path.value();
150 continue; // Check the next device. 152 continue; // Check the next device.
151 } 153 }
152 dbus::MessageReader reader(response.get()); 154 dbus::MessageReader reader(response.get());
153 uint32 device_type = 0; 155 uint32 device_type = 0;
154 if (!reader.PopVariantOfUint32(&device_type)) { 156 if (!reader.PopVariantOfUint32(&device_type)) {
155 LOG(WARNING) << "Unexpected response for " << device_type << ": " 157 LOG(WARNING) << "Unexpected response for " << device_type << ": "
156 << response->ToString(); 158 << response->ToString();
157 continue; // Check the next device. 159 continue; // Check the next device.
158 } 160 }
159 VLOG(1) << "Device type: " << device_type; 161 VLOG(1) << "Device type: " << device_type;
160 162
161 if (device_type == NM_DEVICE_TYPE_WIFI) { // Found a wlan adapter 163 if (device_type == NM_DEVICE_TYPE_WIFI) { // Found a wlan adapter
162 if (GetAccessPointsForAdapter(device_path, data)) 164 if (GetAccessPointsForAdapter(device_path, data))
163 ++success_count; 165 ++success_count;
164 else 166 else
165 ++fail_count; 167 ++fail_count;
166 } 168 }
167 } 169 }
168 // At least one successfull scan overrides any other adapter reporting error. 170 // At least one successfull scan overrides any other adapter reporting error.
169 return success_count || fail_count == 0; 171 return success_count || fail_count == 0;
170 } 172 }
171 173
172 bool NetworkManagerWlanApi::GetAdapterDeviceList( 174 bool NetworkManagerWlanApi::GetAdapterDeviceList(
173 std::vector<std::string>* device_paths) { 175 std::vector<dbus::ObjectPath>* device_paths) {
174 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices"); 176 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices");
175 scoped_ptr<dbus::Response> response( 177 scoped_ptr<dbus::Response> response(
176 network_manager_proxy_->CallMethodAndBlock( 178 network_manager_proxy_->CallMethodAndBlock(
177 &method_call, 179 &method_call,
178 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); 180 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
179 if (!response.get()) { 181 if (!response.get()) {
180 LOG(WARNING) << "Failed to get the device list"; 182 LOG(WARNING) << "Failed to get the device list";
181 return false; 183 return false;
182 } 184 }
183 185
184 dbus::MessageReader reader(response.get()); 186 dbus::MessageReader reader(response.get());
185 if (!reader.PopArrayOfObjectPaths(device_paths)) { 187 if (!reader.PopArrayOfObjectPaths(device_paths)) {
186 LOG(WARNING) << "Unexpected response: " << response->ToString(); 188 LOG(WARNING) << "Unexpected response: " << response->ToString();
187 return false; 189 return false;
188 } 190 }
189 return true; 191 return true;
190 } 192 }
191 193
192 194
193 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( 195 bool NetworkManagerWlanApi::GetAccessPointsForAdapter(
194 const std::string& adapter_path, WifiData::AccessPointDataSet* data) { 196 const dbus::ObjectPath& adapter_path, WifiData::AccessPointDataSet* data) {
195 // Create a proxy object for this wifi adapter, and ask it to do a scan 197 // Create a proxy object for this wifi adapter, and ask it to do a scan
196 // (or at least, dump its scan results). 198 // (or at least, dump its scan results).
197 dbus::ObjectProxy* device_proxy = 199 dbus::ObjectProxy* device_proxy =
198 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 200 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
199 adapter_path); 201 adapter_path);
200 dbus::MethodCall method_call( 202 dbus::MethodCall method_call(
201 "org.freedesktop.NetworkManager.Device.Wireless", 203 "org.freedesktop.NetworkManager.Device.Wireless",
202 "GetAccessPoints"); 204 "GetAccessPoints");
203 scoped_ptr<dbus::Response> response( 205 scoped_ptr<dbus::Response> response(
204 device_proxy->CallMethodAndBlock( 206 device_proxy->CallMethodAndBlock(
205 &method_call, 207 &method_call,
206 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); 208 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
207 if (!response.get()) { 209 if (!response.get()) {
208 LOG(WARNING) << "Failed to get access points data for " << adapter_path; 210 LOG(WARNING) << "Failed to get access points data for "
211 << adapter_path.value();
209 return false; 212 return false;
210 } 213 }
211 dbus::MessageReader reader(response.get()); 214 dbus::MessageReader reader(response.get());
212 std::vector<std::string> access_point_paths; 215 std::vector<dbus::ObjectPath> access_point_paths;
213 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { 216 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) {
214 LOG(WARNING) << "Unexpected response for " << adapter_path << ": " 217 LOG(WARNING) << "Unexpected response for " << adapter_path.value() << ": "
215 << response->ToString(); 218 << response->ToString();
216 return false; 219 return false;
217 } 220 }
218 221
219 VLOG(1) << "Wireless adapter " << adapter_path << " found " 222 VLOG(1) << "Wireless adapter " << adapter_path.value() << " found "
220 << access_point_paths.size() << " access points."; 223 << access_point_paths.size() << " access points.";
221 224
222 for (size_t i = 0; i < access_point_paths.size(); ++i) { 225 for (size_t i = 0; i < access_point_paths.size(); ++i) {
223 const std::string& access_point_path = access_point_paths[i]; 226 const dbus::ObjectPath& access_point_path = access_point_paths[i];
224 VLOG(1) << "Checking access point: " << access_point_path; 227 VLOG(1) << "Checking access point: " << access_point_path.value();
225 228
226 dbus::ObjectProxy* access_point_proxy = 229 dbus::ObjectProxy* access_point_proxy =
227 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 230 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
228 access_point_path); 231 access_point_path);
229 232
230 AccessPointData access_point_data; 233 AccessPointData access_point_data;
231 { 234 {
232 scoped_ptr<dbus::Response> response( 235 scoped_ptr<dbus::Response> response(
233 GetAccessPointProperty(access_point_proxy, "Ssid")); 236 GetAccessPointProperty(access_point_proxy, "Ssid"));
234 if (!response.get()) 237 if (!response.get())
235 continue; 238 continue;
236 // The response should contain a variant that contains an array of bytes. 239 // The response should contain a variant that contains an array of bytes.
237 dbus::MessageReader reader(response.get()); 240 dbus::MessageReader reader(response.get());
238 dbus::MessageReader variant_reader(response.get()); 241 dbus::MessageReader variant_reader(response.get());
239 if (!reader.PopVariant(&variant_reader)) { 242 if (!reader.PopVariant(&variant_reader)) {
240 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " 243 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
241 << response->ToString(); 244 << ": " << response->ToString();
242 continue; 245 continue;
243 } 246 }
244 uint8* ssid_bytes = NULL; 247 uint8* ssid_bytes = NULL;
245 size_t ssid_length = 0; 248 size_t ssid_length = 0;
246 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) { 249 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) {
247 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " 250 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
248 << response->ToString(); 251 << ": " << response->ToString();
249 continue; 252 continue;
250 } 253 }
251 std::string ssid(ssid_bytes, ssid_bytes + ssid_length); 254 std::string ssid(ssid_bytes, ssid_bytes + ssid_length);
252 access_point_data.ssid = UTF8ToUTF16(ssid); 255 access_point_data.ssid = UTF8ToUTF16(ssid);
253 } 256 }
254 257
255 { // Read the mac address 258 { // Read the mac address
256 scoped_ptr<dbus::Response> response( 259 scoped_ptr<dbus::Response> response(
257 GetAccessPointProperty(access_point_proxy, "HwAddress")); 260 GetAccessPointProperty(access_point_proxy, "HwAddress"));
258 if (!response.get()) 261 if (!response.get())
259 continue; 262 continue;
260 dbus::MessageReader reader(response.get()); 263 dbus::MessageReader reader(response.get());
261 std::string mac; 264 std::string mac;
262 if (!reader.PopVariantOfString(&mac)) { 265 if (!reader.PopVariantOfString(&mac)) {
263 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " 266 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
264 << response->ToString(); 267 << ": " << response->ToString();
265 continue; 268 continue;
266 } 269 }
267 270
268 ReplaceSubstringsAfterOffset(&mac, 0U, ":", ""); 271 ReplaceSubstringsAfterOffset(&mac, 0U, ":", "");
269 std::vector<uint8> mac_bytes; 272 std::vector<uint8> mac_bytes;
270 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) { 273 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) {
271 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size() 274 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size()
272 << " bytes) so using raw string: " << mac; 275 << " bytes) so using raw string: " << mac;
273 access_point_data.mac_address = UTF8ToUTF16(mac); 276 access_point_data.mac_address = UTF8ToUTF16(mac);
274 } else { 277 } else {
275 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]); 278 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]);
276 } 279 }
277 } 280 }
278 281
279 { // Read signal strength. 282 { // Read signal strength.
280 scoped_ptr<dbus::Response> response( 283 scoped_ptr<dbus::Response> response(
281 GetAccessPointProperty(access_point_proxy, "Strength")); 284 GetAccessPointProperty(access_point_proxy, "Strength"));
282 if (!response.get()) 285 if (!response.get())
283 continue; 286 continue;
284 dbus::MessageReader reader(response.get()); 287 dbus::MessageReader reader(response.get());
285 uint8 strength = 0; 288 uint8 strength = 0;
286 if (!reader.PopVariantOfByte(&strength)) { 289 if (!reader.PopVariantOfByte(&strength)) {
287 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " 290 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
288 << response->ToString(); 291 << ": " << response->ToString();
289 continue; 292 continue;
290 } 293 }
291 // Convert strength as a percentage into dBs. 294 // Convert strength as a percentage into dBs.
292 access_point_data.radio_signal_strength = -100 + strength / 2; 295 access_point_data.radio_signal_strength = -100 + strength / 2;
293 } 296 }
294 297
295 { // Read the channel 298 { // Read the channel
296 scoped_ptr<dbus::Response> response( 299 scoped_ptr<dbus::Response> response(
297 GetAccessPointProperty(access_point_proxy, "Frequency")); 300 GetAccessPointProperty(access_point_proxy, "Frequency"));
298 if (!response.get()) 301 if (!response.get())
299 continue; 302 continue;
300 dbus::MessageReader reader(response.get()); 303 dbus::MessageReader reader(response.get());
301 uint32 frequency = 0; 304 uint32 frequency = 0;
302 if (!reader.PopVariantOfUint32(&frequency)) { 305 if (!reader.PopVariantOfUint32(&frequency)) {
303 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " 306 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
304 << response->ToString(); 307 << ": " << response->ToString();
305 continue; 308 continue;
306 } 309 }
307 310
308 // NetworkManager returns frequency in MHz. 311 // NetworkManager returns frequency in MHz.
309 access_point_data.channel = 312 access_point_data.channel =
310 frquency_in_khz_to_channel(frequency * 1000); 313 frquency_in_khz_to_channel(frequency * 1000);
311 } 314 }
312 VLOG(1) << "Access point data of " << access_point_path << ": " 315 VLOG(1) << "Access point data of " << access_point_path.value() << ": "
313 << "SSID: " << access_point_data.ssid << ", " 316 << "SSID: " << access_point_data.ssid << ", "
314 << "MAC: " << access_point_data.mac_address << ", " 317 << "MAC: " << access_point_data.mac_address << ", "
315 << "Strength: " << access_point_data.radio_signal_strength << ", " 318 << "Strength: " << access_point_data.radio_signal_strength << ", "
316 << "Channel: " << access_point_data.channel; 319 << "Channel: " << access_point_data.channel;
317 320
318 data->insert(access_point_data); 321 data->insert(access_point_data);
319 } 322 }
320 return true; 323 return true;
321 } 324 }
322 325
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 kNoWifiPollingIntervalMilliseconds>; 368 kNoWifiPollingIntervalMilliseconds>;
366 } 369 }
367 370
368 WifiDataProviderCommon::WlanApiInterface* 371 WifiDataProviderCommon::WlanApiInterface*
369 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { 372 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) {
370 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); 373 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi);
371 if (wlan_api->InitWithBus(bus)) 374 if (wlan_api->InitWithBus(bus))
372 return wlan_api.release(); 375 return wlan_api.release();
373 return NULL; 376 return NULL;
374 } 377 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698