summaryrefslogtreecommitdiff
path: root/chromium/device/usb/usb_service_linux.h
blob: b334de39b0e8ba145cbc87d3953175cc9b7317a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <list>
#include <memory>
#include <unordered_map>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "device/usb/usb_service.h"

namespace device {

struct UsbDeviceDescriptor;
class UsbDeviceLinux;

class UsbServiceLinux : public UsbService {
 public:
  UsbServiceLinux();
  ~UsbServiceLinux() override;

  // device::UsbService implementation
  void GetDevices(const GetDevicesCallback& callback) override;

 private:
  using DeviceMap =
      std::unordered_map<std::string, scoped_refptr<UsbDeviceLinux>>;

  class FileThreadHelper;

  void OnDeviceAdded(const std::string& device_path,
                     const UsbDeviceDescriptor& descriptor,
                     const std::string& manufacturer,
                     const std::string& product,
                     const std::string& serial_number,
                     uint8_t active_configuration);
  void DeviceReady(scoped_refptr<UsbDeviceLinux> device, bool success);
  void OnDeviceRemoved(const std::string& device_path);
  void HelperStarted();

  bool enumeration_ready() {
    return helper_started_ && first_enumeration_countdown_ == 0;
  }

  // |helper_started_| is set once OnDeviceAdded has been called for all devices
  // initially found on the system. |first_enumeration_countdown_| is then
  // decremented as DeviceReady is called for these devices.
  // |enumeration_callbacks_| holds the callbacks passed to GetDevices before
  // this process completes and the device list is ready.
  bool helper_started_ = false;
  uint32_t first_enumeration_countdown_ = 0;
  std::list<GetDevicesCallback> enumeration_callbacks_;

  std::unique_ptr<FileThreadHelper> helper_;
  DeviceMap devices_by_path_;

  base::WeakPtrFactory<UsbServiceLinux> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(UsbServiceLinux);
};

}  // namespace device