usb_service_mac.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2020 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_MAC_H_
  5. #define SERVICES_DEVICE_USB_USB_SERVICE_MAC_H_
  6. #include <CoreFoundation/CoreFoundation.h>
  7. #include <IOKit/IOKitLib.h>
  8. #include <IOKit/usb/IOUSBLib.h>
  9. #include <unordered_map>
  10. #include "base/mac/foundation_util.h"
  11. #include "base/mac/scoped_ionotificationportref.h"
  12. #include "base/mac/scoped_ioobject.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "services/device/usb/usb_service.h"
  15. namespace device {
  16. class UsbDeviceMac;
  17. // The USB service is responsible for device discovery on the system, which
  18. // allows it to re-use device handles to prevent competition for the same USB
  19. // device.
  20. class UsbServiceMac final : public UsbService {
  21. public:
  22. UsbServiceMac();
  23. UsbServiceMac(const UsbServiceMac&) = delete;
  24. UsbServiceMac& operator=(const UsbServiceMac&) = delete;
  25. ~UsbServiceMac() override;
  26. private:
  27. // IOService matching callbacks.
  28. static void FirstMatchCallback(void* context, io_iterator_t iterator);
  29. static void TerminatedCallback(void* context, io_iterator_t iterator);
  30. void AddDevices();
  31. void AddDevice(io_service_t device);
  32. void RemoveDevices();
  33. std::unordered_map<uint64_t, scoped_refptr<UsbDeviceMac>> device_map_;
  34. base::mac::ScopedIONotificationPortRef notify_port_;
  35. base::mac::ScopedIOObject<io_iterator_t> devices_added_iterator_;
  36. base::mac::ScopedIOObject<io_iterator_t> devices_removed_iterator_;
  37. base::WeakPtrFactory<UsbServiceMac> weak_factory_{this};
  38. };
  39. } // namespace device
  40. #endif // SERVICES_DEVICE_USB_USB_SERVICE_MAC_H_