bluetooth_delegate_impl.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2021 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 COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
  5. #define COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
  6. #include <list>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "base/scoped_observation.h"
  14. #include "components/permissions/object_permission_context_base.h"
  15. #include "content/public/browser/bluetooth_delegate.h"
  16. #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
  17. namespace blink {
  18. class WebBluetoothDeviceId;
  19. } // namespace blink
  20. namespace content {
  21. class RenderFrameHost;
  22. } // namespace content
  23. namespace device {
  24. class BluetoothDevice;
  25. class BluetoothUUID;
  26. } // namespace device
  27. namespace permissions {
  28. class BluetoothChooserContext;
  29. // Provides an interface for managing device permissions for Web Bluetooth and
  30. // Web Bluetooth Scanning API.
  31. class BluetoothDelegateImpl : public content::BluetoothDelegate {
  32. public:
  33. // Provides embedder-level functionality to BluetoothDelegateImpl.
  34. class Client {
  35. public:
  36. Client() = default;
  37. virtual ~Client() = default;
  38. Client(const Client&) = delete;
  39. Client& operator=(const Client&) = delete;
  40. // Provides access to a BluetoothChooserContext without transferring
  41. // ownership.
  42. virtual permissions::BluetoothChooserContext* GetBluetoothChooserContext(
  43. content::RenderFrameHost* frame) = 0;
  44. // See content::BluetoothDelegate::RunBluetoothChooser.
  45. virtual std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
  46. content::RenderFrameHost* frame,
  47. const content::BluetoothChooser::EventHandler& event_handler) = 0;
  48. // See content::BluetoothDelegate::ShowBluetoothScanningPrompt.
  49. virtual std::unique_ptr<content::BluetoothScanningPrompt>
  50. ShowBluetoothScanningPrompt(
  51. content::RenderFrameHost* frame,
  52. const content::BluetoothScanningPrompt::EventHandler&
  53. event_handler) = 0;
  54. // Prompt the user for pairing Bluetooth device.
  55. //
  56. // The |device_identifier| is a localized string (device name, address,
  57. // etc.) displayed to the user for identification purposes. When the
  58. // prompt is complete |callback| is called with the result.
  59. // |pairing_kind| is to determine which pairing kind of prompt should be
  60. // shown.
  61. virtual void ShowBluetoothDevicePairDialog(
  62. content::RenderFrameHost* frame,
  63. const std::u16string& device_identifier,
  64. content::BluetoothDelegate::PairPromptCallback callback,
  65. content::BluetoothDelegate::PairingKind pairing_kind,
  66. const absl::optional<std::u16string>& pin) = 0;
  67. };
  68. explicit BluetoothDelegateImpl(std::unique_ptr<Client> client);
  69. ~BluetoothDelegateImpl() override;
  70. BluetoothDelegateImpl(const BluetoothDelegateImpl&) = delete;
  71. BluetoothDelegateImpl& operator=(const BluetoothDelegateImpl&) = delete;
  72. // BluetoothDelegate implementation:
  73. std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
  74. content::RenderFrameHost* frame,
  75. const content::BluetoothChooser::EventHandler& event_handler) override;
  76. std::unique_ptr<content::BluetoothScanningPrompt> ShowBluetoothScanningPrompt(
  77. content::RenderFrameHost* frame,
  78. const content::BluetoothScanningPrompt::EventHandler& event_handler)
  79. override;
  80. void ShowDevicePairPrompt(content::RenderFrameHost* frame,
  81. const std::u16string& device_identifier,
  82. PairPromptCallback callback,
  83. PairingKind pairing_kind,
  84. const absl::optional<std::u16string>& pin) override;
  85. blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
  86. content::RenderFrameHost* frame,
  87. const std::string& device_address) override;
  88. std::string GetDeviceAddress(
  89. content::RenderFrameHost* frame,
  90. const blink::WebBluetoothDeviceId& device_id) override;
  91. blink::WebBluetoothDeviceId AddScannedDevice(
  92. content::RenderFrameHost* frame,
  93. const std::string& device_address) override;
  94. blink::WebBluetoothDeviceId GrantServiceAccessPermission(
  95. content::RenderFrameHost* frame,
  96. const device::BluetoothDevice* device,
  97. const blink::mojom::WebBluetoothRequestDeviceOptions* options) override;
  98. bool HasDevicePermission(
  99. content::RenderFrameHost* frame,
  100. const blink::WebBluetoothDeviceId& device_id) override;
  101. void RevokeDevicePermissionWebInitiated(
  102. content::RenderFrameHost* frame,
  103. const blink::WebBluetoothDeviceId& device_id) override;
  104. bool IsAllowedToAccessService(content::RenderFrameHost* frame,
  105. const blink::WebBluetoothDeviceId& device_id,
  106. const device::BluetoothUUID& service) override;
  107. bool IsAllowedToAccessAtLeastOneService(
  108. content::RenderFrameHost* frame,
  109. const blink::WebBluetoothDeviceId& device_id) override;
  110. bool IsAllowedToAccessManufacturerData(
  111. content::RenderFrameHost* frame,
  112. const blink::WebBluetoothDeviceId& device_id,
  113. uint16_t manufacturer_code) override;
  114. std::vector<blink::mojom::WebBluetoothDevicePtr> GetPermittedDevices(
  115. content::RenderFrameHost* frame) override;
  116. void AddFramePermissionObserver(FramePermissionObserver* observer) override;
  117. void RemoveFramePermissionObserver(
  118. FramePermissionObserver* observer) override;
  119. private:
  120. // Manages the FramePermissionObserver list for a particular RFH. Will
  121. // self-delete when the last observer is removed from the |owning_delegate|'s
  122. // |chooser_observers_| map.
  123. class ChooserContextPermissionObserver
  124. : public ObjectPermissionContextBase::PermissionObserver {
  125. public:
  126. explicit ChooserContextPermissionObserver(
  127. BluetoothDelegateImpl* owning_delegate,
  128. ObjectPermissionContextBase* context);
  129. ~ChooserContextPermissionObserver() override;
  130. ChooserContextPermissionObserver(const ChooserContextPermissionObserver&) =
  131. delete;
  132. ChooserContextPermissionObserver& operator=(
  133. const ChooserContextPermissionObserver) = delete;
  134. // ObjectPermissionContextBase::PermissionObserver:
  135. void OnPermissionRevoked(const url::Origin& origin) override;
  136. void AddFramePermissionObserver(FramePermissionObserver* observer);
  137. void RemoveFramePermissionObserver(FramePermissionObserver* observer);
  138. private:
  139. raw_ptr<BluetoothDelegateImpl> owning_delegate_;
  140. base::ObserverList<FramePermissionObserver> observer_list_;
  141. std::list<FramePermissionObserver*> observers_pending_removal_;
  142. bool is_traversing_observers_ = false;
  143. base::ScopedObservation<ObjectPermissionContextBase,
  144. ObjectPermissionContextBase::PermissionObserver>
  145. observer_{this};
  146. };
  147. std::unique_ptr<Client> client_;
  148. std::map<content::RenderFrameHost*,
  149. std::unique_ptr<ChooserContextPermissionObserver>>
  150. chooser_observers_;
  151. };
  152. } // namespace permissions
  153. #endif // COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_