| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "device/usb/usb_service_linux.h" | 5 #include "device/usb/usb_service_linux.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 } | 210 } |
| 211 | 211 |
| 212 void UsbServiceLinux::OnDeviceAdded(const std::string& device_path, | 212 void UsbServiceLinux::OnDeviceAdded(const std::string& device_path, |
| 213 const UsbDeviceDescriptor& descriptor, | 213 const UsbDeviceDescriptor& descriptor, |
| 214 const std::string& manufacturer, | 214 const std::string& manufacturer, |
| 215 const std::string& product, | 215 const std::string& product, |
| 216 const std::string& serial_number, | 216 const std::string& serial_number, |
| 217 uint8_t active_configuration) { | 217 uint8_t active_configuration) { |
| 218 DCHECK(CalledOnValidThread()); | 218 DCHECK(CalledOnValidThread()); |
| 219 | 219 |
| 220 if (ContainsKey(devices_by_path_, device_path)) { |
| 221 USB_LOG(ERROR) << "Got duplicate add event for path: " << device_path; |
| 222 return; |
| 223 } |
| 224 |
| 220 // Devices that appear during initial enumeration are gathered into the first | 225 // Devices that appear during initial enumeration are gathered into the first |
| 221 // result returned by GetDevices() and prevent device add/remove notifications | 226 // result returned by GetDevices() and prevent device add/remove notifications |
| 222 // from being sent. | 227 // from being sent. |
| 223 if (!enumeration_ready()) | 228 if (!enumeration_ready()) |
| 224 ++first_enumeration_countdown_; | 229 ++first_enumeration_countdown_; |
| 225 | 230 |
| 226 scoped_refptr<UsbDeviceLinux> device(new UsbDeviceLinux( | 231 scoped_refptr<UsbDeviceLinux> device(new UsbDeviceLinux( |
| 227 device_path, descriptor, manufacturer, product, serial_number, | 232 device_path, descriptor, manufacturer, product, serial_number, |
| 228 active_configuration, blocking_task_runner())); | 233 active_configuration, blocking_task_runner())); |
| 229 devices_by_path_[device->device_path()] = device; | 234 devices_by_path_[device->device_path()] = device; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 243 bool enumeration_became_ready = false; | 248 bool enumeration_became_ready = false; |
| 244 if (!enumeration_ready()) { | 249 if (!enumeration_ready()) { |
| 245 DCHECK_GT(first_enumeration_countdown_, 0u); | 250 DCHECK_GT(first_enumeration_countdown_, 0u); |
| 246 if (--first_enumeration_countdown_ == 0) | 251 if (--first_enumeration_countdown_ == 0) |
| 247 enumeration_became_ready = true; | 252 enumeration_became_ready = true; |
| 248 } | 253 } |
| 249 | 254 |
| 250 // If |device| was disconnected while descriptors were being read then it | 255 // If |device| was disconnected while descriptors were being read then it |
| 251 // will have been removed from |devices_by_path_|. | 256 // will have been removed from |devices_by_path_|. |
| 252 auto it = devices_by_path_.find(device->device_path()); | 257 auto it = devices_by_path_.find(device->device_path()); |
| 253 if (it != devices_by_path_.end()) { | 258 if (it == devices_by_path_.end()) { |
| 254 if (success) { | 259 success = false; |
| 255 DCHECK(!ContainsKey(devices(), device->guid())); | 260 } else if (success) { |
| 256 devices()[device->guid()] = device; | 261 DCHECK(!ContainsKey(devices(), device->guid())); |
| 262 devices()[device->guid()] = device; |
| 257 | 263 |
| 258 USB_LOG(USER) << "USB device added: path=" << device->device_path() | 264 USB_LOG(USER) << "USB device added: path=" << device->device_path() |
| 259 << " vendor=" << device->vendor_id() << " \"" | 265 << " vendor=" << device->vendor_id() << " \"" |
| 260 << device->manufacturer_string() | 266 << device->manufacturer_string() |
| 261 << "\", product=" << device->product_id() << " \"" | 267 << "\", product=" << device->product_id() << " \"" |
| 262 << device->product_string() << "\", serial=\"" | 268 << device->product_string() << "\", serial=\"" |
| 263 << device->serial_number() << "\", guid=" << device->guid(); | 269 << device->serial_number() << "\", guid=" << device->guid(); |
| 264 } else { | 270 } else { |
| 265 devices_by_path_.erase(it); | 271 devices_by_path_.erase(it); |
| 266 } | |
| 267 } | 272 } |
| 268 | 273 |
| 269 if (enumeration_became_ready) { | 274 if (enumeration_became_ready) { |
| 270 std::vector<scoped_refptr<UsbDevice>> result; | 275 std::vector<scoped_refptr<UsbDevice>> result; |
| 271 result.reserve(devices().size()); | 276 result.reserve(devices().size()); |
| 272 for (const auto& map_entry : devices()) | 277 for (const auto& map_entry : devices()) |
| 273 result.push_back(map_entry.second); | 278 result.push_back(map_entry.second); |
| 274 for (const auto& callback : enumeration_callbacks_) | 279 for (const auto& callback : enumeration_callbacks_) |
| 275 callback.Run(result); | 280 callback.Run(result); |
| 276 enumeration_callbacks_.clear(); | 281 enumeration_callbacks_.clear(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 307 result.reserve(devices().size()); | 312 result.reserve(devices().size()); |
| 308 for (const auto& map_entry : devices()) | 313 for (const auto& map_entry : devices()) |
| 309 result.push_back(map_entry.second); | 314 result.push_back(map_entry.second); |
| 310 for (const auto& callback : enumeration_callbacks_) | 315 for (const auto& callback : enumeration_callbacks_) |
| 311 callback.Run(result); | 316 callback.Run(result); |
| 312 enumeration_callbacks_.clear(); | 317 enumeration_callbacks_.clear(); |
| 313 } | 318 } |
| 314 } | 319 } |
| 315 | 320 |
| 316 } // namespace device | 321 } // namespace device |
| OLD | NEW |