hid_manager_impl.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2017 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_HID_HID_MANAGER_IMPL_H_
  5. #define SERVICES_DEVICE_HID_HID_MANAGER_IMPL_H_
  6. #include "base/callback.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/scoped_observation.h"
  10. #include "mojo/public/cpp/bindings/pending_associated_remote.h"
  11. #include "mojo/public/cpp/bindings/pending_receiver.h"
  12. #include "mojo/public/cpp/bindings/receiver_set.h"
  13. #include "mojo/public/cpp/bindings/remote_set.h"
  14. #include "services/device/hid/hid_device_info.h"
  15. #include "services/device/hid/hid_service.h"
  16. #include "services/device/public/mojom/hid.mojom.h"
  17. namespace device {
  18. // HidManagerImpl is owned by Device Service. It is reponsible for handling mojo
  19. // communications from clients. It delegates to HidService the real work of
  20. // talking with different platforms.
  21. class HidManagerImpl : public mojom::HidManager, public HidService::Observer {
  22. public:
  23. HidManagerImpl();
  24. HidManagerImpl(HidManagerImpl&) = delete;
  25. HidManagerImpl& operator=(HidManagerImpl&) = delete;
  26. ~HidManagerImpl() override;
  27. // SetHidServiceForTesting only effects the next call to HidManagerImpl's
  28. // constructor in which the HidManagerImpl will take over the ownership of
  29. // passed |hid_service|.
  30. static void SetHidServiceForTesting(std::unique_ptr<HidService> hid_service);
  31. // IsHidServiceTesting() will return true when the next call to the
  32. // constructor will use the HidService instance set by
  33. // SetHidServiceForTesting().
  34. static bool IsHidServiceTesting();
  35. void AddReceiver(mojo::PendingReceiver<mojom::HidManager> receiver) override;
  36. // mojom::HidManager implementation:
  37. void GetDevicesAndSetClient(
  38. mojo::PendingAssociatedRemote<mojom::HidManagerClient> client,
  39. GetDevicesCallback callback) override;
  40. void GetDevices(GetDevicesCallback callback) override;
  41. void Connect(
  42. const std::string& device_guid,
  43. mojo::PendingRemote<mojom::HidConnectionClient> connection_client,
  44. mojo::PendingRemote<mojom::HidConnectionWatcher> watcher,
  45. bool allow_protected_reports,
  46. bool allow_fido_reports,
  47. ConnectCallback callback) override;
  48. private:
  49. void CreateDeviceList(
  50. GetDevicesCallback callback,
  51. mojo::PendingAssociatedRemote<mojom::HidManagerClient> client,
  52. std::vector<mojom::HidDeviceInfoPtr> devices);
  53. void CreateConnection(
  54. ConnectCallback callback,
  55. mojo::PendingRemote<mojom::HidConnectionClient> connection_client,
  56. mojo::PendingRemote<mojom::HidConnectionWatcher> watcher,
  57. scoped_refptr<HidConnection> connection);
  58. // HidService::Observer:
  59. void OnDeviceAdded(mojom::HidDeviceInfoPtr device_info) override;
  60. void OnDeviceRemoved(mojom::HidDeviceInfoPtr device_info) override;
  61. void OnDeviceChanged(mojom::HidDeviceInfoPtr device_info) override;
  62. std::unique_ptr<HidService> hid_service_;
  63. mojo::ReceiverSet<mojom::HidManager> receivers_;
  64. mojo::AssociatedRemoteSet<mojom::HidManagerClient> clients_;
  65. base::ScopedObservation<HidService, HidService::Observer>
  66. hid_service_observation_{this};
  67. base::WeakPtrFactory<HidManagerImpl> weak_factory_{this};
  68. };
  69. } // namespace device
  70. #endif // SERVICES_DEVICE_HID_HID_MANAGER_IMPL_H_