usb_chooser_context.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2022 Microsoft, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_USB_USB_CHOOSER_CONTEXT_H_
  5. #define ELECTRON_SHELL_BROWSER_USB_USB_CHOOSER_CONTEXT_H_
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/containers/queue.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "base/observer_list_types.h"
  14. #include "base/values.h"
  15. #include "components/keyed_service/core/keyed_service.h"
  16. #include "mojo/public/cpp/bindings/associated_receiver.h"
  17. #include "mojo/public/cpp/bindings/remote.h"
  18. #include "services/device/public/mojom/usb_manager.mojom.h"
  19. #include "services/device/public/mojom/usb_manager_client.mojom.h"
  20. #include "url/origin.h"
  21. namespace mojo {
  22. template <typename T>
  23. class PendingReceiver;
  24. template <typename T>
  25. class PendingRemote;
  26. } // namespace mojo
  27. namespace electron {
  28. class ElectronBrowserContext;
  29. class UsbChooserContext : public KeyedService,
  30. public device::mojom::UsbDeviceManagerClient {
  31. public:
  32. explicit UsbChooserContext(ElectronBrowserContext* context);
  33. UsbChooserContext(const UsbChooserContext&) = delete;
  34. UsbChooserContext& operator=(const UsbChooserContext&) = delete;
  35. ~UsbChooserContext() override;
  36. // This observer can be used to be notified of changes to USB devices that are
  37. // connected.
  38. class DeviceObserver : public base::CheckedObserver {
  39. public:
  40. virtual void OnDeviceAdded(const device::mojom::UsbDeviceInfo&) {}
  41. virtual void OnDeviceRemoved(const device::mojom::UsbDeviceInfo&) {}
  42. virtual void OnDeviceManagerConnectionError() {}
  43. // Called when the BrowserContext is shutting down. Observers must remove
  44. // themselves before returning.
  45. virtual void OnBrowserContextShutdown() = 0;
  46. };
  47. static base::Value DeviceInfoToValue(
  48. const device::mojom::UsbDeviceInfo& device_info);
  49. // Grants |origin| access to the USB device.
  50. void GrantDevicePermission(const url::Origin& origin,
  51. const device::mojom::UsbDeviceInfo& device_info);
  52. // Checks if |origin| has access to a device with |device_info|.
  53. bool HasDevicePermission(const url::Origin& origin,
  54. const device::mojom::UsbDeviceInfo& device_info);
  55. // Revokes |origin| access to the USB device ordered by website.
  56. void RevokeDevicePermissionWebInitiated(
  57. const url::Origin& origin,
  58. const device::mojom::UsbDeviceInfo& device);
  59. void AddObserver(DeviceObserver* observer);
  60. void RemoveObserver(DeviceObserver* observer);
  61. // Forward UsbDeviceManager methods.
  62. void GetDevices(device::mojom::UsbDeviceManager::GetDevicesCallback callback);
  63. void GetDevice(
  64. const std::string& guid,
  65. base::span<const uint8_t> blocked_interface_classes,
  66. mojo::PendingReceiver<device::mojom::UsbDevice> device_receiver,
  67. mojo::PendingRemote<device::mojom::UsbDeviceClient> device_client);
  68. // This method should only be called when you are sure that |devices_| has
  69. // been initialized. It will return nullptr if the guid cannot be found.
  70. const device::mojom::UsbDeviceInfo* GetDeviceInfo(const std::string& guid);
  71. base::WeakPtr<UsbChooserContext> AsWeakPtr();
  72. void InitDeviceList(std::vector<::device::mojom::UsbDeviceInfoPtr> devices);
  73. private:
  74. // device::mojom::UsbDeviceManagerClient implementation.
  75. void OnDeviceAdded(device::mojom::UsbDeviceInfoPtr device_info) override;
  76. void OnDeviceRemoved(device::mojom::UsbDeviceInfoPtr device_info) override;
  77. void RevokeObjectPermissionInternal(const url::Origin& origin,
  78. const base::Value& object,
  79. bool revoked_by_website);
  80. void OnDeviceManagerConnectionError();
  81. void EnsureConnectionWithDeviceManager();
  82. void SetUpDeviceManagerConnection();
  83. bool is_initialized_ = false;
  84. base::queue<device::mojom::UsbDeviceManager::GetDevicesCallback>
  85. pending_get_devices_requests_;
  86. std::map<url::Origin, std::set<std::string>> ephemeral_devices_;
  87. std::map<std::string, device::mojom::UsbDeviceInfoPtr> devices_;
  88. // Connection to |device_manager_instance_|.
  89. mojo::Remote<device::mojom::UsbDeviceManager> device_manager_;
  90. mojo::AssociatedReceiver<device::mojom::UsbDeviceManagerClient>
  91. client_receiver_{this};
  92. base::ObserverList<DeviceObserver> device_observer_list_;
  93. raw_ptr<ElectronBrowserContext> browser_context_;
  94. base::WeakPtrFactory<UsbChooserContext> weak_factory_{this};
  95. };
  96. } // namespace electron
  97. #endif // ELECTRON_SHELL_BROWSER_USB_USB_CHOOSER_CONTEXT_H_