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

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

Issue 1874893002: Convert //content/browser from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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) 2012 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 <stddef.h> 11 #include <stddef.h>
12 #include <stdint.h> 12 #include <stdint.h>
13 13
14 #include <memory>
15
14 #include "base/macros.h" 16 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "content/browser/geolocation/wifi_data_provider_manager.h" 19 #include "content/browser/geolocation/wifi_data_provider_manager.h"
19 #include "dbus/bus.h" 20 #include "dbus/bus.h"
20 #include "dbus/message.h" 21 #include "dbus/message.h"
21 #include "dbus/object_path.h" 22 #include "dbus/object_path.h"
22 #include "dbus/object_proxy.h" 23 #include "dbus/object_proxy.h"
23 24
24 namespace content { 25 namespace content {
25 namespace { 26 namespace {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 68
68 // Given the NetworkManager path to a wireless adapater, dumps the wifi scan 69 // Given the NetworkManager path to a wireless adapater, dumps the wifi scan
69 // results and appends them to |data|. Returns false if a fatal error is 70 // results and appends them to |data|. Returns false if a fatal error is
70 // encountered such that the data set could not be populated. 71 // encountered such that the data set could not be populated.
71 bool GetAccessPointsForAdapter(const dbus::ObjectPath& adapter_path, 72 bool GetAccessPointsForAdapter(const dbus::ObjectPath& adapter_path,
72 WifiData::AccessPointDataSet* data); 73 WifiData::AccessPointDataSet* data);
73 74
74 // Internal method used by |GetAccessPointsForAdapter|, given a wifi access 75 // Internal method used by |GetAccessPointsForAdapter|, given a wifi access
75 // point proxy retrieves the named property and returns it. Returns NULL in 76 // point proxy retrieves the named property and returns it. Returns NULL in
76 // a scoped_ptr if the property could not be read. 77 // a scoped_ptr if the property could not be read.
77 scoped_ptr<dbus::Response> GetAccessPointProperty( 78 std::unique_ptr<dbus::Response> GetAccessPointProperty(
78 dbus::ObjectProxy* proxy, 79 dbus::ObjectProxy* proxy,
79 const std::string& property_name); 80 const std::string& property_name);
80 81
81 scoped_refptr<dbus::Bus> system_bus_; 82 scoped_refptr<dbus::Bus> system_bus_;
82 dbus::ObjectProxy* network_manager_proxy_; 83 dbus::ObjectProxy* network_manager_proxy_;
83 84
84 DISALLOW_COPY_AND_ASSIGN(NetworkManagerWlanApi); 85 DISALLOW_COPY_AND_ASSIGN(NetworkManagerWlanApi);
85 }; 86 };
86 87
87 // Convert a wifi frequency to the corresponding channel. Adapted from 88 // Convert a wifi frequency to the corresponding channel. Adapted from
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 VLOG(1) << "Checking device: " << device_path.value(); 143 VLOG(1) << "Checking device: " << device_path.value();
143 144
144 dbus::ObjectProxy* device_proxy = 145 dbus::ObjectProxy* device_proxy =
145 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 146 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
146 device_path); 147 device_path);
147 148
148 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); 149 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get");
149 dbus::MessageWriter builder(&method_call); 150 dbus::MessageWriter builder(&method_call);
150 builder.AppendString("org.freedesktop.NetworkManager.Device"); 151 builder.AppendString("org.freedesktop.NetworkManager.Device");
151 builder.AppendString("DeviceType"); 152 builder.AppendString("DeviceType");
152 scoped_ptr<dbus::Response> response( 153 std::unique_ptr<dbus::Response> response(device_proxy->CallMethodAndBlock(
153 device_proxy->CallMethodAndBlock( 154 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
154 &method_call,
155 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
156 if (!response) { 155 if (!response) {
157 LOG(WARNING) << "Failed to get the device type for " 156 LOG(WARNING) << "Failed to get the device type for "
158 << device_path.value(); 157 << device_path.value();
159 continue; // Check the next device. 158 continue; // Check the next device.
160 } 159 }
161 dbus::MessageReader reader(response.get()); 160 dbus::MessageReader reader(response.get());
162 uint32_t device_type = 0; 161 uint32_t device_type = 0;
163 if (!reader.PopVariantOfUint32(&device_type)) { 162 if (!reader.PopVariantOfUint32(&device_type)) {
164 LOG(WARNING) << "Unexpected response for " << device_type << ": " 163 LOG(WARNING) << "Unexpected response for " << device_type << ": "
165 << response->ToString(); 164 << response->ToString();
166 continue; // Check the next device. 165 continue; // Check the next device.
167 } 166 }
168 VLOG(1) << "Device type: " << device_type; 167 VLOG(1) << "Device type: " << device_type;
169 168
170 if (device_type == NM_DEVICE_TYPE_WIFI) { // Found a wlan adapter 169 if (device_type == NM_DEVICE_TYPE_WIFI) { // Found a wlan adapter
171 if (GetAccessPointsForAdapter(device_path, data)) 170 if (GetAccessPointsForAdapter(device_path, data))
172 ++success_count; 171 ++success_count;
173 else 172 else
174 ++fail_count; 173 ++fail_count;
175 } 174 }
176 } 175 }
177 // At least one successful scan overrides any other adapter reporting error. 176 // At least one successful scan overrides any other adapter reporting error.
178 return success_count || fail_count == 0; 177 return success_count || fail_count == 0;
179 } 178 }
180 179
181 bool NetworkManagerWlanApi::GetAdapterDeviceList( 180 bool NetworkManagerWlanApi::GetAdapterDeviceList(
182 std::vector<dbus::ObjectPath>* device_paths) { 181 std::vector<dbus::ObjectPath>* device_paths) {
183 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices"); 182 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices");
184 scoped_ptr<dbus::Response> response( 183 std::unique_ptr<dbus::Response> response(
185 network_manager_proxy_->CallMethodAndBlock( 184 network_manager_proxy_->CallMethodAndBlock(
186 &method_call, 185 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
187 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
188 if (!response) { 186 if (!response) {
189 LOG(WARNING) << "Failed to get the device list"; 187 LOG(WARNING) << "Failed to get the device list";
190 return false; 188 return false;
191 } 189 }
192 190
193 dbus::MessageReader reader(response.get()); 191 dbus::MessageReader reader(response.get());
194 if (!reader.PopArrayOfObjectPaths(device_paths)) { 192 if (!reader.PopArrayOfObjectPaths(device_paths)) {
195 LOG(WARNING) << "Unexpected response: " << response->ToString(); 193 LOG(WARNING) << "Unexpected response: " << response->ToString();
196 return false; 194 return false;
197 } 195 }
198 return true; 196 return true;
199 } 197 }
200 198
201 199
202 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( 200 bool NetworkManagerWlanApi::GetAccessPointsForAdapter(
203 const dbus::ObjectPath& adapter_path, WifiData::AccessPointDataSet* data) { 201 const dbus::ObjectPath& adapter_path, WifiData::AccessPointDataSet* data) {
204 // Create a proxy object for this wifi adapter, and ask it to do a scan 202 // Create a proxy object for this wifi adapter, and ask it to do a scan
205 // (or at least, dump its scan results). 203 // (or at least, dump its scan results).
206 dbus::ObjectProxy* device_proxy = 204 dbus::ObjectProxy* device_proxy =
207 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 205 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
208 adapter_path); 206 adapter_path);
209 dbus::MethodCall method_call( 207 dbus::MethodCall method_call(
210 "org.freedesktop.NetworkManager.Device.Wireless", 208 "org.freedesktop.NetworkManager.Device.Wireless",
211 "GetAccessPoints"); 209 "GetAccessPoints");
212 scoped_ptr<dbus::Response> response( 210 std::unique_ptr<dbus::Response> response(device_proxy->CallMethodAndBlock(
213 device_proxy->CallMethodAndBlock( 211 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
214 &method_call,
215 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
216 if (!response) { 212 if (!response) {
217 LOG(WARNING) << "Failed to get access points data for " 213 LOG(WARNING) << "Failed to get access points data for "
218 << adapter_path.value(); 214 << adapter_path.value();
219 return false; 215 return false;
220 } 216 }
221 dbus::MessageReader reader(response.get()); 217 dbus::MessageReader reader(response.get());
222 std::vector<dbus::ObjectPath> access_point_paths; 218 std::vector<dbus::ObjectPath> access_point_paths;
223 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { 219 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) {
224 LOG(WARNING) << "Unexpected response for " << adapter_path.value() << ": " 220 LOG(WARNING) << "Unexpected response for " << adapter_path.value() << ": "
225 << response->ToString(); 221 << response->ToString();
226 return false; 222 return false;
227 } 223 }
228 224
229 VLOG(1) << "Wireless adapter " << adapter_path.value() << " found " 225 VLOG(1) << "Wireless adapter " << adapter_path.value() << " found "
230 << access_point_paths.size() << " access points."; 226 << access_point_paths.size() << " access points.";
231 227
232 for (size_t i = 0; i < access_point_paths.size(); ++i) { 228 for (size_t i = 0; i < access_point_paths.size(); ++i) {
233 const dbus::ObjectPath& access_point_path = access_point_paths[i]; 229 const dbus::ObjectPath& access_point_path = access_point_paths[i];
234 VLOG(1) << "Checking access point: " << access_point_path.value(); 230 VLOG(1) << "Checking access point: " << access_point_path.value();
235 231
236 dbus::ObjectProxy* access_point_proxy = 232 dbus::ObjectProxy* access_point_proxy =
237 system_bus_->GetObjectProxy(kNetworkManagerServiceName, 233 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
238 access_point_path); 234 access_point_path);
239 235
240 AccessPointData access_point_data; 236 AccessPointData access_point_data;
241 { 237 {
242 scoped_ptr<dbus::Response> response( 238 std::unique_ptr<dbus::Response> response(
243 GetAccessPointProperty(access_point_proxy, "Ssid")); 239 GetAccessPointProperty(access_point_proxy, "Ssid"));
244 if (!response) 240 if (!response)
245 continue; 241 continue;
246 // The response should contain a variant that contains an array of bytes. 242 // The response should contain a variant that contains an array of bytes.
247 dbus::MessageReader reader(response.get()); 243 dbus::MessageReader reader(response.get());
248 dbus::MessageReader variant_reader(response.get()); 244 dbus::MessageReader variant_reader(response.get());
249 if (!reader.PopVariant(&variant_reader)) { 245 if (!reader.PopVariant(&variant_reader)) {
250 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 246 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
251 << ": " << response->ToString(); 247 << ": " << response->ToString();
252 continue; 248 continue;
253 } 249 }
254 const uint8_t* ssid_bytes = NULL; 250 const uint8_t* ssid_bytes = NULL;
255 size_t ssid_length = 0; 251 size_t ssid_length = 0;
256 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) { 252 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) {
257 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 253 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
258 << ": " << response->ToString(); 254 << ": " << response->ToString();
259 continue; 255 continue;
260 } 256 }
261 std::string ssid(ssid_bytes, ssid_bytes + ssid_length); 257 std::string ssid(ssid_bytes, ssid_bytes + ssid_length);
262 access_point_data.ssid = base::UTF8ToUTF16(ssid); 258 access_point_data.ssid = base::UTF8ToUTF16(ssid);
263 } 259 }
264 260
265 { // Read the mac address 261 { // Read the mac address
266 scoped_ptr<dbus::Response> response( 262 std::unique_ptr<dbus::Response> response(
267 GetAccessPointProperty(access_point_proxy, "HwAddress")); 263 GetAccessPointProperty(access_point_proxy, "HwAddress"));
268 if (!response) 264 if (!response)
269 continue; 265 continue;
270 dbus::MessageReader reader(response.get()); 266 dbus::MessageReader reader(response.get());
271 std::string mac; 267 std::string mac;
272 if (!reader.PopVariantOfString(&mac)) { 268 if (!reader.PopVariantOfString(&mac)) {
273 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 269 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
274 << ": " << response->ToString(); 270 << ": " << response->ToString();
275 continue; 271 continue;
276 } 272 }
277 273
278 base::ReplaceSubstringsAfterOffset(&mac, 0U, ":", base::StringPiece()); 274 base::ReplaceSubstringsAfterOffset(&mac, 0U, ":", base::StringPiece());
279 std::vector<uint8_t> mac_bytes; 275 std::vector<uint8_t> mac_bytes;
280 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) { 276 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) {
281 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size() 277 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size()
282 << " bytes) so using raw string: " << mac; 278 << " bytes) so using raw string: " << mac;
283 access_point_data.mac_address = base::UTF8ToUTF16(mac); 279 access_point_data.mac_address = base::UTF8ToUTF16(mac);
284 } else { 280 } else {
285 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]); 281 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]);
286 } 282 }
287 } 283 }
288 284
289 { // Read signal strength. 285 { // Read signal strength.
290 scoped_ptr<dbus::Response> response( 286 std::unique_ptr<dbus::Response> response(
291 GetAccessPointProperty(access_point_proxy, "Strength")); 287 GetAccessPointProperty(access_point_proxy, "Strength"));
292 if (!response) 288 if (!response)
293 continue; 289 continue;
294 dbus::MessageReader reader(response.get()); 290 dbus::MessageReader reader(response.get());
295 uint8_t strength = 0; 291 uint8_t strength = 0;
296 if (!reader.PopVariantOfByte(&strength)) { 292 if (!reader.PopVariantOfByte(&strength)) {
297 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 293 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
298 << ": " << response->ToString(); 294 << ": " << response->ToString();
299 continue; 295 continue;
300 } 296 }
301 // Convert strength as a percentage into dBs. 297 // Convert strength as a percentage into dBs.
302 access_point_data.radio_signal_strength = -100 + strength / 2; 298 access_point_data.radio_signal_strength = -100 + strength / 2;
303 } 299 }
304 300
305 { // Read the channel 301 { // Read the channel
306 scoped_ptr<dbus::Response> response( 302 std::unique_ptr<dbus::Response> response(
307 GetAccessPointProperty(access_point_proxy, "Frequency")); 303 GetAccessPointProperty(access_point_proxy, "Frequency"));
308 if (!response) 304 if (!response)
309 continue; 305 continue;
310 dbus::MessageReader reader(response.get()); 306 dbus::MessageReader reader(response.get());
311 uint32_t frequency = 0; 307 uint32_t frequency = 0;
312 if (!reader.PopVariantOfUint32(&frequency)) { 308 if (!reader.PopVariantOfUint32(&frequency)) {
313 LOG(WARNING) << "Unexpected response for " << access_point_path.value() 309 LOG(WARNING) << "Unexpected response for " << access_point_path.value()
314 << ": " << response->ToString(); 310 << ": " << response->ToString();
315 continue; 311 continue;
316 } 312 }
317 313
318 // NetworkManager returns frequency in MHz. 314 // NetworkManager returns frequency in MHz.
319 access_point_data.channel = 315 access_point_data.channel =
320 frquency_in_khz_to_channel(frequency * 1000); 316 frquency_in_khz_to_channel(frequency * 1000);
321 } 317 }
322 VLOG(1) << "Access point data of " << access_point_path.value() << ": " 318 VLOG(1) << "Access point data of " << access_point_path.value() << ": "
323 << "SSID: " << access_point_data.ssid << ", " 319 << "SSID: " << access_point_data.ssid << ", "
324 << "MAC: " << access_point_data.mac_address << ", " 320 << "MAC: " << access_point_data.mac_address << ", "
325 << "Strength: " << access_point_data.radio_signal_strength << ", " 321 << "Strength: " << access_point_data.radio_signal_strength << ", "
326 << "Channel: " << access_point_data.channel; 322 << "Channel: " << access_point_data.channel;
327 323
328 data->insert(access_point_data); 324 data->insert(access_point_data);
329 } 325 }
330 return true; 326 return true;
331 } 327 }
332 328
333 scoped_ptr<dbus::Response> NetworkManagerWlanApi::GetAccessPointProperty( 329 std::unique_ptr<dbus::Response> NetworkManagerWlanApi::GetAccessPointProperty(
334 dbus::ObjectProxy* access_point_proxy, 330 dbus::ObjectProxy* access_point_proxy,
335 const std::string& property_name) { 331 const std::string& property_name) {
336 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); 332 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get");
337 dbus::MessageWriter builder(&method_call); 333 dbus::MessageWriter builder(&method_call);
338 builder.AppendString("org.freedesktop.NetworkManager.AccessPoint"); 334 builder.AppendString("org.freedesktop.NetworkManager.AccessPoint");
339 builder.AppendString(property_name); 335 builder.AppendString(property_name);
340 scoped_ptr<dbus::Response> response = access_point_proxy->CallMethodAndBlock( 336 std::unique_ptr<dbus::Response> response =
341 &method_call, 337 access_point_proxy->CallMethodAndBlock(
342 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); 338 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
343 if (!response) { 339 if (!response) {
344 LOG(WARNING) << "Failed to get property for " << property_name; 340 LOG(WARNING) << "Failed to get property for " << property_name;
345 } 341 }
346 return response; 342 return response;
347 } 343 }
348 344
349 } // namespace 345 } // namespace
350 346
351 // static 347 // static
352 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { 348 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() {
353 return new WifiDataProviderLinux(); 349 return new WifiDataProviderLinux();
354 } 350 }
355 351
356 WifiDataProviderLinux::WifiDataProviderLinux() { 352 WifiDataProviderLinux::WifiDataProviderLinux() {
357 } 353 }
358 354
359 WifiDataProviderLinux::~WifiDataProviderLinux() { 355 WifiDataProviderLinux::~WifiDataProviderLinux() {
360 } 356 }
361 357
362 WifiDataProviderCommon::WlanApiInterface* 358 WifiDataProviderCommon::WlanApiInterface*
363 WifiDataProviderLinux::NewWlanApi() { 359 WifiDataProviderLinux::NewWlanApi() {
364 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); 360 std::unique_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi);
365 if (wlan_api->Init()) 361 if (wlan_api->Init())
366 return wlan_api.release(); 362 return wlan_api.release();
367 return NULL; 363 return NULL;
368 } 364 }
369 365
370 WifiPollingPolicy* WifiDataProviderLinux::NewPollingPolicy() { 366 WifiPollingPolicy* WifiDataProviderLinux::NewPollingPolicy() {
371 return new GenericWifiPollingPolicy<kDefaultPollingIntervalMilliseconds, 367 return new GenericWifiPollingPolicy<kDefaultPollingIntervalMilliseconds,
372 kNoChangePollingIntervalMilliseconds, 368 kNoChangePollingIntervalMilliseconds,
373 kTwoNoChangePollingIntervalMilliseconds, 369 kTwoNoChangePollingIntervalMilliseconds,
374 kNoWifiPollingIntervalMilliseconds>; 370 kNoWifiPollingIntervalMilliseconds>;
375 } 371 }
376 372
377 WifiDataProviderCommon::WlanApiInterface* 373 WifiDataProviderCommon::WlanApiInterface*
378 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { 374 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) {
379 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); 375 std::unique_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi);
380 if (wlan_api->InitWithBus(bus)) 376 if (wlan_api->InitWithBus(bus))
381 return wlan_api.release(); 377 return wlan_api.release();
382 return NULL; 378 return NULL;
383 } 379 }
384 380
385 } // namespace content 381 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698