electron_bluetooth_delegate.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2020 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_BLUETOOTH_ELECTRON_BLUETOOTH_DELEGATE_H_
  5. #define ELECTRON_SHELL_BROWSER_BLUETOOTH_ELECTRON_BLUETOOTH_DELEGATE_H_
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/weak_ptr.h"
  11. #include "content/public/browser/bluetooth_delegate.h"
  12. #include "content/public/browser/render_frame_host.h"
  13. #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
  14. namespace blink {
  15. class WebBluetoothDeviceId;
  16. }
  17. namespace content {
  18. class RenderFrameHost;
  19. }
  20. namespace device {
  21. class BluetoothDevice;
  22. class BluetoothUUID;
  23. } // namespace device
  24. namespace electron {
  25. // Provides an interface for managing device permissions for Web Bluetooth and
  26. // Web Bluetooth Scanning API. This is the Electron-specific implementation of
  27. // the BluetoothDelegate.
  28. class ElectronBluetoothDelegate : public content::BluetoothDelegate {
  29. public:
  30. ElectronBluetoothDelegate();
  31. ~ElectronBluetoothDelegate() override;
  32. // Move-only class.
  33. ElectronBluetoothDelegate(const ElectronBluetoothDelegate&) = delete;
  34. ElectronBluetoothDelegate& operator=(const ElectronBluetoothDelegate&) =
  35. delete;
  36. // BluetoothDelegate implementation:
  37. std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
  38. content::RenderFrameHost* frame,
  39. const content::BluetoothChooser::EventHandler& event_handler) override;
  40. std::unique_ptr<content::BluetoothScanningPrompt> ShowBluetoothScanningPrompt(
  41. content::RenderFrameHost* frame,
  42. const content::BluetoothScanningPrompt::EventHandler& event_handler)
  43. override;
  44. void ShowDevicePairPrompt(content::RenderFrameHost* frame,
  45. const std::u16string& device_identifier,
  46. PairPromptCallback callback,
  47. PairingKind pairing_kind,
  48. const std::optional<std::u16string>& pin) override;
  49. blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
  50. content::RenderFrameHost* frame,
  51. const std::string& device_address) override;
  52. std::string GetDeviceAddress(
  53. content::RenderFrameHost* frame,
  54. const blink::WebBluetoothDeviceId& device_id) override;
  55. blink::WebBluetoothDeviceId AddScannedDevice(
  56. content::RenderFrameHost* frame,
  57. const std::string& device_address) override;
  58. blink::WebBluetoothDeviceId GrantServiceAccessPermission(
  59. content::RenderFrameHost* frame,
  60. const device::BluetoothDevice* device,
  61. const blink::mojom::WebBluetoothRequestDeviceOptions* options) override;
  62. bool HasDevicePermission(
  63. content::RenderFrameHost* frame,
  64. const blink::WebBluetoothDeviceId& device_id) override;
  65. void RevokeDevicePermissionWebInitiated(
  66. content::RenderFrameHost* frame,
  67. const blink::WebBluetoothDeviceId& device_id) override;
  68. bool IsAllowedToAccessService(content::RenderFrameHost* frame,
  69. const blink::WebBluetoothDeviceId& device_id,
  70. const device::BluetoothUUID& service) override;
  71. bool IsAllowedToAccessAtLeastOneService(
  72. content::RenderFrameHost* frame,
  73. const blink::WebBluetoothDeviceId& device_id) override;
  74. bool IsAllowedToAccessManufacturerData(
  75. content::RenderFrameHost* frame,
  76. const blink::WebBluetoothDeviceId& device_id,
  77. uint16_t manufacturer_code) override;
  78. std::vector<blink::mojom::WebBluetoothDevicePtr> GetPermittedDevices(
  79. content::RenderFrameHost* frame) override;
  80. void AddFramePermissionObserver(FramePermissionObserver* observer) override;
  81. void RemoveFramePermissionObserver(
  82. FramePermissionObserver* observer) override;
  83. private:
  84. void OnDevicePairPromptResponse(PairPromptCallback callback,
  85. base::Value::Dict response);
  86. base::WeakPtrFactory<ElectronBluetoothDelegate> weak_factory_{this};
  87. };
  88. } // namespace electron
  89. #endif // ELECTRON_SHELL_BROWSER_BLUETOOTH_ELECTRON_BLUETOOTH_DELEGATE_H_