electron_bluetooth_delegate.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "base/values.h"
  12. #include "content/public/browser/bluetooth_delegate.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 MayUseBluetooth(content::RenderFrameHost* frame) override;
  69. bool IsAllowedToAccessService(content::RenderFrameHost* frame,
  70. const blink::WebBluetoothDeviceId& device_id,
  71. const device::BluetoothUUID& service) override;
  72. bool IsAllowedToAccessAtLeastOneService(
  73. content::RenderFrameHost* frame,
  74. const blink::WebBluetoothDeviceId& device_id) override;
  75. bool IsAllowedToAccessManufacturerData(
  76. content::RenderFrameHost* frame,
  77. const blink::WebBluetoothDeviceId& device_id,
  78. uint16_t manufacturer_code) override;
  79. std::vector<blink::mojom::WebBluetoothDevicePtr> GetPermittedDevices(
  80. content::RenderFrameHost* frame) override;
  81. void AddFramePermissionObserver(FramePermissionObserver* observer) override;
  82. void RemoveFramePermissionObserver(
  83. FramePermissionObserver* observer) override;
  84. private:
  85. void OnDevicePairPromptResponse(PairPromptCallback callback,
  86. base::Value::Dict response);
  87. base::WeakPtrFactory<ElectronBluetoothDelegate> weak_factory_{this};
  88. };
  89. } // namespace electron
  90. #endif // ELECTRON_SHELL_BROWSER_BLUETOOTH_ELECTRON_BLUETOOTH_DELEGATE_H_