1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
6 #define COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
14 #include "base/memory/raw_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/scoped_observation.h"
17 #include "components/permissions/object_permission_context_base.h"
18 #include "content/public/browser/bluetooth_delegate.h"
19 #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
22 class WebBluetoothDeviceId;
26 class RenderFrameHost;
27 } // namespace content
30 class BluetoothDevice;
34 namespace permissions {
36 class BluetoothChooserContext;
38 // Provides an interface for managing device permissions for Web Bluetooth and
39 // Web Bluetooth Scanning API.
40 class BluetoothDelegateImpl : public content::BluetoothDelegate {
42 // Provides embedder-level functionality to BluetoothDelegateImpl.
46 virtual ~Client() = default;
48 Client(const Client&) = delete;
49 Client& operator=(const Client&) = delete;
51 // Provides access to a BluetoothChooserContext without transferring
53 virtual permissions::BluetoothChooserContext* GetBluetoothChooserContext(
54 content::RenderFrameHost* frame) = 0;
56 // See content::BluetoothDelegate::RunBluetoothChooser.
57 virtual std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
58 content::RenderFrameHost* frame,
59 const content::BluetoothChooser::EventHandler& event_handler) = 0;
61 // See content::BluetoothDelegate::ShowBluetoothScanningPrompt.
62 virtual std::unique_ptr<content::BluetoothScanningPrompt>
63 ShowBluetoothScanningPrompt(
64 content::RenderFrameHost* frame,
65 const content::BluetoothScanningPrompt::EventHandler&
68 // Prompt the user for pairing Bluetooth device.
70 // The |device_identifier| is a localized string (device name, address,
71 // etc.) displayed to the user for identification purposes. When the
72 // prompt is complete |callback| is called with the result.
73 // |pairing_kind| is to determine which pairing kind of prompt should be
75 virtual void ShowBluetoothDevicePairDialog(
76 content::RenderFrameHost* frame,
77 const std::u16string& device_identifier,
78 content::BluetoothDelegate::PairPromptCallback callback,
79 content::BluetoothDelegate::PairingKind pairing_kind,
80 const absl::optional<std::u16string>& pin) = 0;
83 explicit BluetoothDelegateImpl(std::unique_ptr<Client> client);
84 ~BluetoothDelegateImpl() override;
86 BluetoothDelegateImpl(const BluetoothDelegateImpl&) = delete;
87 BluetoothDelegateImpl& operator=(const BluetoothDelegateImpl&) = delete;
89 // BluetoothDelegate implementation:
90 std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
91 content::RenderFrameHost* frame,
92 const content::BluetoothChooser::EventHandler& event_handler) override;
93 std::unique_ptr<content::BluetoothScanningPrompt> ShowBluetoothScanningPrompt(
94 content::RenderFrameHost* frame,
95 const content::BluetoothScanningPrompt::EventHandler& event_handler)
98 void ShowDevicePairPrompt(content::RenderFrameHost* frame,
99 const std::u16string& device_identifier,
100 PairPromptCallback callback,
101 PairingKind pairing_kind,
102 const absl::optional<std::u16string>& pin) override;
104 blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
105 content::RenderFrameHost* frame,
106 const std::string& device_address) override;
107 std::string GetDeviceAddress(
108 content::RenderFrameHost* frame,
109 const blink::WebBluetoothDeviceId& device_id) override;
110 blink::WebBluetoothDeviceId AddScannedDevice(
111 content::RenderFrameHost* frame,
112 const std::string& device_address) override;
113 blink::WebBluetoothDeviceId GrantServiceAccessPermission(
114 content::RenderFrameHost* frame,
115 const device::BluetoothDevice* device,
116 const blink::mojom::WebBluetoothRequestDeviceOptions* options) override;
117 bool HasDevicePermission(
118 content::RenderFrameHost* frame,
119 const blink::WebBluetoothDeviceId& device_id) override;
120 void RevokeDevicePermissionWebInitiated(
121 content::RenderFrameHost* frame,
122 const blink::WebBluetoothDeviceId& device_id) override;
123 bool IsAllowedToAccessService(content::RenderFrameHost* frame,
124 const blink::WebBluetoothDeviceId& device_id,
125 const device::BluetoothUUID& service) override;
126 bool IsAllowedToAccessAtLeastOneService(
127 content::RenderFrameHost* frame,
128 const blink::WebBluetoothDeviceId& device_id) override;
129 bool IsAllowedToAccessManufacturerData(
130 content::RenderFrameHost* frame,
131 const blink::WebBluetoothDeviceId& device_id,
132 uint16_t manufacturer_code) override;
133 std::vector<blink::mojom::WebBluetoothDevicePtr> GetPermittedDevices(
134 content::RenderFrameHost* frame) override;
135 void AddFramePermissionObserver(FramePermissionObserver* observer) override;
136 void RemoveFramePermissionObserver(
137 FramePermissionObserver* observer) override;
140 // Manages the FramePermissionObserver list for a particular RFH. Will
141 // self-delete when the last observer is removed from the |owning_delegate|'s
142 // |chooser_observers_| map.
143 class ChooserContextPermissionObserver
144 : public ObjectPermissionContextBase::PermissionObserver {
146 explicit ChooserContextPermissionObserver(
147 BluetoothDelegateImpl* owning_delegate,
148 ObjectPermissionContextBase* context);
149 ~ChooserContextPermissionObserver() override;
151 ChooserContextPermissionObserver(const ChooserContextPermissionObserver&) =
153 ChooserContextPermissionObserver& operator=(
154 const ChooserContextPermissionObserver) = delete;
156 // ObjectPermissionContextBase::PermissionObserver:
157 void OnPermissionRevoked(const url::Origin& origin) override;
159 void AddFramePermissionObserver(FramePermissionObserver* observer);
160 void RemoveFramePermissionObserver(FramePermissionObserver* observer);
163 raw_ptr<BluetoothDelegateImpl> owning_delegate_;
164 base::ObserverList<FramePermissionObserver> observer_list_;
165 std::list<FramePermissionObserver*> observers_pending_removal_;
166 bool is_traversing_observers_ = false;
167 base::ScopedObservation<ObjectPermissionContextBase,
168 ObjectPermissionContextBase::PermissionObserver>
172 std::unique_ptr<Client> client_;
174 std::map<content::RenderFrameHost*,
175 std::unique_ptr<ChooserContextPermissionObserver>>
179 } // namespace permissions
181 #endif // COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_