usb_service_linux.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_DEVICE_USB_USB_SERVICE_LINUX_H_
  5. #define SERVICES_DEVICE_USB_USB_SERVICE_LINUX_H_
  6. #include <list>
  7. #include <memory>
  8. #include <string>
  9. #include <unordered_map>
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/threading/sequence_bound.h"
  12. #include "build/chromeos_buildflags.h"
  13. #include "services/device/usb/usb_service.h"
  14. namespace device {
  15. struct UsbDeviceDescriptor;
  16. class UsbDeviceLinux;
  17. class UsbServiceLinux final : public UsbService {
  18. public:
  19. UsbServiceLinux();
  20. UsbServiceLinux(const UsbServiceLinux&) = delete;
  21. UsbServiceLinux& operator=(const UsbServiceLinux&) = delete;
  22. ~UsbServiceLinux() override;
  23. // device::UsbService implementation
  24. void GetDevices(GetDevicesCallback callback) override;
  25. private:
  26. using DeviceMap =
  27. std::unordered_map<std::string, scoped_refptr<UsbDeviceLinux>>;
  28. class BlockingTaskRunnerHelper;
  29. void OnDeviceAdded(const std::string& device_path,
  30. std::unique_ptr<UsbDeviceDescriptor> descriptor);
  31. void DeviceReady(scoped_refptr<UsbDeviceLinux> device, bool success);
  32. void OnDeviceRemoved(const std::string& device_path);
  33. void HelperStarted();
  34. bool enumeration_ready() {
  35. return helper_started_ && first_enumeration_countdown_ == 0;
  36. }
  37. // |helper_started_| is set once OnDeviceAdded has been called for all devices
  38. // initially found on the system. |first_enumeration_countdown_| is then
  39. // decremented as DeviceReady is called for these devices.
  40. // |enumeration_callbacks_| holds the callbacks passed to GetDevices before
  41. // this process completes and the device list is ready.
  42. bool helper_started_ = false;
  43. uint32_t first_enumeration_countdown_ = 0;
  44. std::list<GetDevicesCallback> enumeration_callbacks_;
  45. base::SequenceBound<BlockingTaskRunnerHelper> helper_;
  46. DeviceMap devices_by_path_;
  47. base::WeakPtrFactory<UsbServiceLinux> weak_factory_{this};
  48. };
  49. } // namespace device
  50. #endif // SERVICES_DEVICE_USB_USB_SERVICE_LINUX_H_