Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ |
| 6 | #define CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ |
| 7 | |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 8 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 9 | #include <optional> |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 10 | #include <string> |
Ovidio Henriquez | 278801c2 | 2020-03-10 21:52:01 | [diff] [blame] | 11 | #include <unordered_map> |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 14 | #include "base/functional/callback.h" |
Jun Cai | e27db6b | 2019-06-05 03:52:15 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
Andy Paicu | 1d7fc17 | 2021-01-20 18:29:45 | [diff] [blame] | 16 | #include "base/scoped_observation.h" |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 17 | #include "build/build_config.h" |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 18 | #include "content/browser/bad_message.h" |
Chris Mumford | 02de39b | 2021-05-06 15:28:59 | [diff] [blame] | 19 | #include "content/browser/bluetooth/web_bluetooth_pairing_manager_delegate.h" |
Matt Reynolds | 8cca57d5 | 2025-02-20 18:20:59 | [diff] [blame] | 20 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 21 | #include "content/common/content_export.h" |
Andy Paicu | 1d7fc17 | 2021-01-20 18:29:45 | [diff] [blame] | 22 | #include "content/public/browser/bluetooth_delegate.h" |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 23 | #include "content/public/browser/bluetooth_scanning_prompt.h" |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 24 | #include "content/public/browser/document_user_data.h" |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 25 | #include "content/public/browser/web_contents_observer.h" |
| 26 | #include "device/bluetooth/bluetooth_adapter.h" |
Henrique Ferreiro | db47b3e | 2019-09-11 14:29:39 | [diff] [blame] | 27 | #include "mojo/public/cpp/bindings/associated_remote.h" |
| 28 | #include "mojo/public/cpp/bindings/pending_associated_remote.h" |
| 29 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 30 | #include "mojo/public/cpp/bindings/receiver.h" |
Julie Jeongeun Kim | 4f1b5aa | 2019-05-08 01:32:09 | [diff] [blame] | 31 | #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom.h" |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 32 | |
| 33 | namespace url { |
| 34 | class Origin; |
| 35 | } // namespace url |
| 36 | |
Lei Zhang | 5874c7e | 2021-07-16 01:39:49 | [diff] [blame] | 37 | namespace device { |
| 38 | class BluetoothDiscoverySession; |
| 39 | class BluetoothGattConnection; |
| 40 | class BluetoothGattNotifySession; |
| 41 | class BluetoothRemoteGattCharacteristic; |
| 42 | class BluetoothUUID; |
| 43 | } // namespace device |
| 44 | |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 45 | namespace content { |
| 46 | |
Lei Zhang | 5874c7e | 2021-07-16 01:39:49 | [diff] [blame] | 47 | class BluetoothAllowedDevices; |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 48 | class BluetoothDeviceChooserController; |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 49 | class BluetoothDeviceScanningPromptController; |
juncai | 00441ebd | 2017-01-09 19:14:51 | [diff] [blame] | 50 | struct CacheQueryResult; |
ortuno | b6c45d4f | 2016-05-07 04:19:42 | [diff] [blame] | 51 | class FrameConnectedBluetoothDevices; |
juncai | 5fbf7e6 | 2017-03-23 21:21:56 | [diff] [blame] | 52 | struct GATTNotifySessionAndCharacteristicClient; |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 53 | class RenderProcessHost; |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 54 | class WebBluetoothPairingManager; |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 55 | |
Md. Hasanur Rashid | 28c8495 | 2020-01-05 11:36:36 | [diff] [blame] | 56 | bool HasValidFilter( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 57 | const std::optional<std::vector<blink::mojom::WebBluetoothLeScanFilterPtr>>& |
| 58 | filters); |
Doug Turner | 9b931de | 2019-01-08 18:22:37 | [diff] [blame] | 59 | |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 60 | // Implementation of Mojo WebBluetoothService located in |
Sina Firoozabadi | 36a29bc | 2023-03-08 02:29:36 | [diff] [blame] | 61 | // third_party/blink/renderer/modules/bluetooth. |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 62 | // It handles Web Bluetooth API requests coming from Blink / renderer |
| 63 | // process and uses the platform abstraction of device/bluetooth. |
| 64 | // WebBluetoothServiceImpl is not thread-safe and should be created on the |
| 65 | // UI thread as required by device/bluetooth. |
| 66 | // This class is instantiated on-demand via Mojo's ConnectToRemoteService |
| 67 | // from the renderer when the first Web Bluetooth API request is handled. |
ortuno | 77bb42c | 2016-08-01 18:41:09 | [diff] [blame] | 68 | class CONTENT_EXPORT WebBluetoothServiceImpl |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 69 | : public blink::mojom::WebBluetoothService, |
| 70 | public DocumentUserData<WebBluetoothServiceImpl>, |
ortuno | 77bb42c | 2016-08-01 18:41:09 | [diff] [blame] | 71 | public WebContentsObserver, |
Andy Paicu | 1d7fc17 | 2021-01-20 18:29:45 | [diff] [blame] | 72 | public device::BluetoothAdapter::Observer, |
Chris Mumford | 02de39b | 2021-05-06 15:28:59 | [diff] [blame] | 73 | public BluetoothDelegate::FramePermissionObserver, |
| 74 | public WebBluetoothPairingManagerDelegate { |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 75 | public: |
Chris Mumford | 02de39b | 2021-05-06 15:28:59 | [diff] [blame] | 76 | static blink::mojom::WebBluetoothResult TranslateConnectErrorAndRecord( |
| 77 | device::BluetoothDevice::ConnectErrorCode error_code); |
| 78 | |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 79 | // Binds `receiver` to `WebBluetoothServiceImpl` for the currently active |
| 80 | // document for `render_frame_host`, if no security checks fail. See |
| 81 | // `DocumentUserData` for additional details about lifetime. |
| 82 | static void BindIfAllowed( |
| 83 | RenderFrameHost* render_frame_host, |
| 84 | mojo::PendingReceiver<blink::mojom::WebBluetoothService> receiver); |
| 85 | |
| 86 | // Wrapper around `BindIfAllowed()` that also returns the created |
| 87 | // WebBluetoothServiceImpl, if any. |
| 88 | static WebBluetoothServiceImpl* CreateForTesting( |
Henrique Ferreiro | db47b3e | 2019-09-11 14:29:39 | [diff] [blame] | 89 | RenderFrameHost* render_frame_host, |
| 90 | mojo::PendingReceiver<blink::mojom::WebBluetoothService> receiver); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 91 | |
Gabriel Brito | bf12a4e | 2022-02-03 21:46:33 | [diff] [blame] | 92 | // Calling this methods prevents WebBluetoothServiceImpl from clearing up its |
| 93 | // WatchAdvertisement clients when the window either loses focus or becomes |
| 94 | // hidden or occluded. This method is meant to be called from browser tests to |
| 95 | // prevent flakiness. |
| 96 | static void IgnoreVisibilityRequirementsForTesting(); |
| 97 | |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 98 | ~WebBluetoothServiceImpl() override; |
| 99 | |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 100 | WebBluetoothServiceImpl(const WebBluetoothServiceImpl&) = delete; |
| 101 | WebBluetoothServiceImpl& operator=(const WebBluetoothServiceImpl&) = delete; |
| 102 | |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 103 | // Prefer `receiver_.ReportBadMessage()` in new code. Existing callers should |
| 104 | // be migrated as well. |
| 105 | void ReceivedBadMessage(bad_message::BadMessageReason reason); |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 106 | |
Ovidio Henriquez | 6215447 | 2019-08-21 14:45:11 | [diff] [blame] | 107 | // Checks the current requesting and embedding origins as well as the policy |
| 108 | // or global Web Bluetooth block to determine if Web Bluetooth is allowed. |
| 109 | // Returns |SUCCESS| if Bluetooth is allowed. |
| 110 | blink::mojom::WebBluetoothResult GetBluetoothAllowed(); |
| 111 | |
juncai | 87d0929 | 2016-09-15 04:02:53 | [diff] [blame] | 112 | // Returns whether the device is paired with the |render_frame_host_|'s |
| 113 | // GetLastCommittedOrigin(). |
| 114 | bool IsDevicePaired(const std::string& device_address); |
| 115 | |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 116 | // Informs each client in |scanning_clients_| of the user's permission |
| 117 | // decision. |
| 118 | void OnBluetoothScanningPromptEvent( |
| 119 | BluetoothScanningPrompt::Event event, |
| 120 | BluetoothDeviceScanningPromptController* prompt_controller); |
| 121 | |
Andy Paicu | 1d7fc17 | 2021-01-20 18:29:45 | [diff] [blame] | 122 | // BluetoothDelegate::FramePermissionObserverimplementation: |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 123 | void OnPermissionRevoked(const url::Origin& origin) override; |
Andy Paicu | 1d7fc17 | 2021-01-20 18:29:45 | [diff] [blame] | 124 | content::RenderFrameHost* GetRenderFrameHost() override; |
| 125 | |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 126 | #if PAIR_BLUETOOTH_ON_DEMAND() |
| 127 | void SetPairingManagerForTesting( |
| 128 | std::unique_ptr<WebBluetoothPairingManager> pairing_manager); |
| 129 | #endif // PAIR_BLUETOOTH_ON_DEMAND() |
| 130 | |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 131 | private: |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 132 | friend DocumentUserData; |
Claudio DeSouza | 3b4ad05 | 2021-11-23 02:32:43 | [diff] [blame] | 133 | |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 134 | DOCUMENT_USER_DATA_KEY_DECL(); |
| 135 | |
| 136 | // `render_frame_host`: The RFH of the document that owns this instance. |
| 137 | explicit WebBluetoothServiceImpl(RenderFrameHost* render_frame_host); |
Claudio DeSouza | 3b4ad05 | 2021-11-23 02:32:43 | [diff] [blame] | 138 | |
Reilly Grant | a255d1be | 2019-11-14 07:01:05 | [diff] [blame] | 139 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
Claudio DeSouza | 3b4ad05 | 2021-11-23 02:32:43 | [diff] [blame] | 140 | DestroyedDuringRequestDevice); |
Ovidio Henriquez | 00defd2 | 2019-12-12 22:49:56 | [diff] [blame] | 141 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
Claudio DeSouza | 3b4ad05 | 2021-11-23 02:32:43 | [diff] [blame] | 142 | DestroyedDuringRequestScanningStart); |
Jun Cai | 10d9696 | 2019-06-18 21:51:39 | [diff] [blame] | 143 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, PermissionAllowed); |
| 144 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 145 | PermissionPromptCanceled); |
Jun Cai | e27db6b | 2019-06-05 03:52:15 | [diff] [blame] | 146 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 147 | BluetoothScanningPermissionRevokedWhenTabHidden); |
| 148 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 149 | BluetoothScanningPermissionRevokedWhenTabOccluded); |
Jun Cai | a1f8bf25d9 | 2019-06-07 19:06:53 | [diff] [blame] | 150 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 151 | BluetoothScanningPermissionRevokedWhenBlocked); |
Ovidio Henriquez | 5bfc1816 | 2020-03-24 16:17:27 | [diff] [blame] | 152 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 153 | BluetoothScanningPermissionRevokedWhenFocusIsLost); |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 154 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 155 | ReadCharacteristicValueErrorWithValueIgnored); |
Julie Jeongeun Kim | 8a889c76 | 2021-07-15 08:41:26 | [diff] [blame] | 156 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplBrowserTest, |
| 157 | NoShowBluetoothScanningPromptInPrerendering); |
Chris Mumford | e6fa96a4 | 2021-07-20 00:15:14 | [diff] [blame] | 158 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 159 | DeferredStartNotifySession); |
Jack Hsieh | adebefb | 2022-02-15 18:57:53 | [diff] [blame] | 160 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, DeviceDisconnected); |
| 161 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 162 | DeviceGattServicesDiscoveryTimeout); |
Alvin Ji | f4c234d41 | 2023-08-23 16:58:54 | [diff] [blame] | 163 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 164 | TwoWatchAdvertisementsReqSuccess); |
| 165 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 166 | TwoWatchAdvertisementsReqFail); |
| 167 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 168 | SecWatchAdvertisementsReqAfterFirstSuccess); |
Alex N. Jose | d1930d8 | 2024-08-13 21:36:07 | [diff] [blame] | 169 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTestWithBaseAdapter, |
| 170 | EmulatedAdapterRemovalRestoresOriginalAdapter); |
Alex N. Jose | a036a38 | 2024-08-15 23:29:13 | [diff] [blame] | 171 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
| 172 | ServiceDestroyedDuringAdapterAcquisition); |
Alvin Ji | f4c234d41 | 2023-08-23 16:58:54 | [diff] [blame] | 173 | |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 174 | #if PAIR_BLUETOOTH_ON_DEMAND() |
Alvin Ji | 40288acb | 2022-05-23 18:18:40 | [diff] [blame] | 175 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 176 | ReadCharacteristicValueNotAuthorized); |
Alvin Ji | 40288acb | 2022-05-23 18:18:40 | [diff] [blame] | 177 | FRIEND_TEST_ALL_PREFIXES(WebBluetoothServiceImplTest, |
Reilly Grant | 12e53d9 | 2022-01-13 00:58:37 | [diff] [blame] | 178 | IncompletePairingOnShutdown); |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 179 | #endif // PAIR_BLUETOOTH_ON_DEMAND() |
Julie Jeongeun Kim | 8a889c76 | 2021-07-15 08:41:26 | [diff] [blame] | 180 | |
ortuno | 77bb42c | 2016-08-01 18:41:09 | [diff] [blame] | 181 | friend class FrameConnectedBluetoothDevicesTest; |
Jun Cai | e27db6b | 2019-06-05 03:52:15 | [diff] [blame] | 182 | friend class WebBluetoothServiceImplTest; |
Alex N. Jose | d1930d8 | 2024-08-13 21:36:07 | [diff] [blame] | 183 | friend class WebBluetoothServiceImplTestWithBaseAdapter; |
| 184 | |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 185 | using PrimaryServicesRequestCallback = |
| 186 | base::OnceCallback<void(device::BluetoothDevice*)>; |
Jun Cai | 732a05e3 | 2019-05-29 19:34:19 | [diff] [blame] | 187 | using ScanFilters = std::vector<blink::mojom::WebBluetoothLeScanFilterPtr>; |
ortuno | 67acd83 | 2016-04-30 00:13:22 | [diff] [blame] | 188 | |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 189 | class AdvertisementClient; |
| 190 | class WatchAdvertisementsClient; |
| 191 | class ScanningClient; |
Chris Mumford | e6fa96a4 | 2021-07-20 00:15:14 | [diff] [blame] | 192 | struct DeferredStartNotificationData; |
Doug Turner | 9b931de | 2019-01-08 18:22:37 | [diff] [blame] | 193 | |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 194 | // Returns false if `this` is already bound. |
| 195 | bool Bind(mojo::PendingReceiver<blink::mojom::WebBluetoothService> receiver); |
| 196 | |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 197 | // WebContentsObserver: |
| 198 | // These functions should always check that the affected RenderFrameHost |
Claudio DeSouza | 3b4ad05 | 2021-11-23 02:32:43 | [diff] [blame] | 199 | // is this->render_frame_host() and not some other frame in the same tab. |
Jun Cai | e27db6b | 2019-06-05 03:52:15 | [diff] [blame] | 200 | void OnVisibilityChanged(Visibility visibility) override; |
Ovidio Henriquez | 5bfc1816 | 2020-03-24 16:17:27 | [diff] [blame] | 201 | void OnWebContentsLostFocus(RenderWidgetHost* render_widget_host) override; |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 202 | |
| 203 | // BluetoothAdapter::Observer: |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 204 | void AdapterPoweredChanged(device::BluetoothAdapter* adapter, |
| 205 | bool powered) override; |
| 206 | void DeviceAdded(device::BluetoothAdapter* adapter, |
| 207 | device::BluetoothDevice* device) override; |
ortuno | b6c45d4f | 2016-05-07 04:19:42 | [diff] [blame] | 208 | void DeviceChanged(device::BluetoothAdapter* adapter, |
| 209 | device::BluetoothDevice* device) override; |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 210 | void DeviceAdvertisementReceived( |
| 211 | const std::string& device_address, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 212 | const std::optional<std::string>& device_name, |
| 213 | const std::optional<std::string>& advertisement_name, |
| 214 | std::optional<int8_t> rssi, |
| 215 | std::optional<int8_t> tx_power, |
| 216 | std::optional<uint16_t> appearance, |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 217 | const device::BluetoothDevice::UUIDList& advertised_uuids, |
| 218 | const device::BluetoothDevice::ServiceDataMap& service_data_map, |
| 219 | const device::BluetoothDevice::ManufacturerDataMap& manufacturer_data_map) |
| 220 | override; |
ortuno | 67acd83 | 2016-04-30 00:13:22 | [diff] [blame] | 221 | void GattServicesDiscovered(device::BluetoothAdapter* adapter, |
| 222 | device::BluetoothDevice* device) override; |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 223 | void GattCharacteristicValueChanged( |
| 224 | device::BluetoothAdapter* adapter, |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 225 | device::BluetoothRemoteGattCharacteristic* characteristic, |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 226 | const std::vector<uint8_t>& value) override; |
| 227 | |
| 228 | // Notifies the WebBluetoothServiceClient that characteristic |
| 229 | // |characteristic_instance_id| changed it's value. We only do this for |
| 230 | // characteristics that have been returned to the client in the past. |
| 231 | void NotifyCharacteristicValueChanged( |
| 232 | const std::string& characteristic_instance_id, |
juncai | 1ef7dd4 | 2016-11-29 04:28:21 | [diff] [blame] | 233 | const std::vector<uint8_t>& value); |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 234 | |
| 235 | // WebBluetoothService methods: |
Ovidio Henriquez | 6215447 | 2019-08-21 14:45:11 | [diff] [blame] | 236 | void GetAvailability(GetAvailabilityCallback callback) override; |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 237 | void RequestDevice(blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 238 | RequestDeviceCallback callback) override; |
Ovidio Henriquez | 278801c2 | 2020-03-10 21:52:01 | [diff] [blame] | 239 | void GetDevices(GetDevicesCallback callback) override; |
François Beaufort | f100253a8 | 2022-03-10 11:26:31 | [diff] [blame] | 240 | void ForgetDevice(const blink::WebBluetoothDeviceId& device_id, |
| 241 | ForgetDeviceCallback callback) override; |
ortuno | b6c45d4f | 2016-05-07 04:19:42 | [diff] [blame] | 242 | void RemoteServerConnect( |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 243 | const blink::WebBluetoothDeviceId& device_id, |
Henrique Ferreiro | db47b3e | 2019-09-11 14:29:39 | [diff] [blame] | 244 | mojo::PendingAssociatedRemote<blink::mojom::WebBluetoothServerClient> |
| 245 | client, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 246 | RemoteServerConnectCallback callback) override; |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 247 | void RemoteServerDisconnect( |
| 248 | const blink::WebBluetoothDeviceId& device_id) override; |
beaufort.francois | 7952f00 | 2016-06-14 16:44:09 | [diff] [blame] | 249 | void RemoteServerGetPrimaryServices( |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 250 | const blink::WebBluetoothDeviceId& device_id, |
beaufort.francois | 7952f00 | 2016-06-14 16:44:09 | [diff] [blame] | 251 | blink::mojom::WebBluetoothGATTQueryQuantity quantity, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 252 | const std::optional<device::BluetoothUUID>& services_uuid, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 253 | RemoteServerGetPrimaryServicesCallback callback) override; |
ortuno | b0fb6a18 | 2016-04-27 01:45:26 | [diff] [blame] | 254 | void RemoteServiceGetCharacteristics( |
juncai | 1ef7dd4 | 2016-11-29 04:28:21 | [diff] [blame] | 255 | const std::string& service_instance_id, |
ortuno | b0fb6a18 | 2016-04-27 01:45:26 | [diff] [blame] | 256 | blink::mojom::WebBluetoothGATTQueryQuantity quantity, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 257 | const std::optional<device::BluetoothUUID>& characteristics_uuid, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 258 | RemoteServiceGetCharacteristicsCallback callback) override; |
ortuno | 12e91f07 | 2016-04-15 22:57:33 | [diff] [blame] | 259 | void RemoteCharacteristicReadValue( |
juncai | 1ef7dd4 | 2016-11-29 04:28:21 | [diff] [blame] | 260 | const std::string& characteristic_instance_id, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 261 | RemoteCharacteristicReadValueCallback callback) override; |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 262 | void RemoteCharacteristicWriteValue( |
juncai | 1ef7dd4 | 2016-11-29 04:28:21 | [diff] [blame] | 263 | const std::string& characteristic_instance_id, |
Reilly Grant | 8002d42f | 2024-10-23 22:40:31 | [diff] [blame] | 264 | base::span<const uint8_t> value, |
David Lechner | 2e7a43a | 2020-05-26 20:29:59 | [diff] [blame] | 265 | blink::mojom::WebBluetoothWriteType write_type, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 266 | RemoteCharacteristicWriteValueCallback callback) override; |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 267 | void RemoteCharacteristicStartNotifications( |
juncai | 1ef7dd4 | 2016-11-29 04:28:21 | [diff] [blame] | 268 | const std::string& characteristic_instance_id, |
Henrique Ferreiro | db47b3e | 2019-09-11 14:29:39 | [diff] [blame] | 269 | mojo::PendingAssociatedRemote< |
| 270 | blink::mojom::WebBluetoothCharacteristicClient> client, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 271 | RemoteCharacteristicStartNotificationsCallback callback) override; |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 272 | void RemoteCharacteristicStartNotificationsInternal( |
| 273 | const std::string& characteristic_instance_id, |
| 274 | mojo::AssociatedRemote<blink::mojom::WebBluetoothCharacteristicClient> |
| 275 | client, |
| 276 | RemoteCharacteristicStartNotificationsCallback callback) override; |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 277 | void RemoteCharacteristicStopNotifications( |
juncai | 1ef7dd4 | 2016-11-29 04:28:21 | [diff] [blame] | 278 | const std::string& characteristic_instance_id, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 279 | RemoteCharacteristicStopNotificationsCallback callback) override; |
dougt | 4f2237c | 2017-01-14 04:14:13 | [diff] [blame] | 280 | void RemoteCharacteristicGetDescriptors( |
| 281 | const std::string& service_instance_id, |
| 282 | blink::mojom::WebBluetoothGATTQueryQuantity quantity, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 283 | const std::optional<device::BluetoothUUID>& characteristics_uuid, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 284 | RemoteCharacteristicGetDescriptorsCallback callback) override; |
dougt | a2fe05521 | 2017-01-27 05:35:30 | [diff] [blame] | 285 | void RemoteDescriptorReadValue( |
Chris Mumford | 61ee9ba | 2021-06-22 16:16:15 | [diff] [blame] | 286 | const std::string& descriptor_instance_id, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 287 | RemoteDescriptorReadValueCallback callback) override; |
dougt | be62e9d | 2017-02-01 16:13:55 | [diff] [blame] | 288 | void RemoteDescriptorWriteValue( |
| 289 | const std::string& descriptor_instance_id, |
Reilly Grant | 8002d42f | 2024-10-23 22:40:31 | [diff] [blame] | 290 | base::span<const uint8_t> value, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 291 | RemoteDescriptorWriteValueCallback callback) override; |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 292 | void RequestScanningStart( |
Ovidio Henriquez | 3dee626c | 2020-05-05 19:37:10 | [diff] [blame] | 293 | mojo::PendingAssociatedRemote< |
Alvin Ji | c0c16c7 | 2023-08-25 18:41:57 | [diff] [blame] | 294 | blink::mojom::WebBluetoothAdvertisementClient> client_remote, |
Doug Turner | 9b931de | 2019-01-08 18:22:37 | [diff] [blame] | 295 | blink::mojom::WebBluetoothRequestLEScanOptionsPtr options, |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 296 | RequestScanningStartCallback callback) override; |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 297 | void WatchAdvertisementsForDevice( |
| 298 | const blink::WebBluetoothDeviceId& device_id, |
| 299 | mojo::PendingAssociatedRemote< |
Alvin Ji | c0c16c7 | 2023-08-25 18:41:57 | [diff] [blame] | 300 | blink::mojom::WebBluetoothAdvertisementClient> client_remote, |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 301 | WatchAdvertisementsForDeviceCallback callback) override; |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 302 | |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 303 | void RequestDeviceImpl( |
| 304 | blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 305 | RequestDeviceCallback callback, |
Reilly Grant | a255d1be | 2019-11-14 07:01:05 | [diff] [blame] | 306 | scoped_refptr<device::BluetoothAdapter> adapter); |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 307 | |
Ovidio Henriquez | 278801c2 | 2020-03-10 21:52:01 | [diff] [blame] | 308 | void GetDevicesImpl(GetDevicesCallback callback, |
| 309 | scoped_refptr<device::BluetoothAdapter> adapter); |
| 310 | |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 311 | // Callbacks for BLE scanning. |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 312 | void RequestScanningStartImpl( |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 313 | mojo::PendingAssociatedRemote< |
Alvin Ji | c0c16c7 | 2023-08-25 18:41:57 | [diff] [blame] | 314 | blink::mojom::WebBluetoothAdvertisementClient> client_remote, |
Doug Turner | 9b931de | 2019-01-08 18:22:37 | [diff] [blame] | 315 | blink::mojom::WebBluetoothRequestLEScanOptionsPtr options, |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 316 | RequestScanningStartCallback callback, |
Reilly Grant | a255d1be | 2019-11-14 07:01:05 | [diff] [blame] | 317 | scoped_refptr<device::BluetoothAdapter> adapter); |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 318 | void OnStartDiscoverySessionForScanning( |
| 319 | mojo::PendingAssociatedRemote< |
Alvin Ji | c0c16c7 | 2023-08-25 18:41:57 | [diff] [blame] | 320 | blink::mojom::WebBluetoothAdvertisementClient> client_remote, |
Doug Turner | 9b931de | 2019-01-08 18:22:37 | [diff] [blame] | 321 | blink::mojom::WebBluetoothRequestLEScanOptionsPtr options, |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 322 | std::unique_ptr<device::BluetoothDiscoverySession> session); |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 323 | void OnDiscoverySessionErrorForScanning(); |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 324 | |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 325 | // Callbacks for watch advertisements for device. |
| 326 | void WatchAdvertisementsForDeviceImpl( |
| 327 | const blink::WebBluetoothDeviceId& device_id, |
| 328 | mojo::PendingAssociatedRemote< |
Alvin Ji | c0c16c7 | 2023-08-25 18:41:57 | [diff] [blame] | 329 | blink::mojom::WebBluetoothAdvertisementClient> client_remote, |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 330 | WatchAdvertisementsForDeviceCallback callback, |
| 331 | scoped_refptr<device::BluetoothAdapter> adapter); |
| 332 | void OnStartDiscoverySessionForWatchAdvertisements( |
| 333 | std::unique_ptr<device::BluetoothDiscoverySession> session); |
| 334 | void OnDiscoverySessionErrorForWatchAdvertisements(); |
| 335 | |
| 336 | // Remove WatchAdvertisementsClients and ScanningClients with disconnected |
| 337 | // WebBluetoothAdvertisementClients from their respective containers. |
| 338 | void RemoveDisconnectedClients(); |
| 339 | |
| 340 | // Stop active discovery sessions and destroy them if there aren't any active |
| 341 | // AdvertisementClients. |
| 342 | void MaybeStopDiscovery(); |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 343 | |
ortuno | 67acd83 | 2016-04-30 00:13:22 | [diff] [blame] | 344 | // Should only be run after the services have been discovered for |
| 345 | // |device_address|. |
beaufort.francois | 7952f00 | 2016-06-14 16:44:09 | [diff] [blame] | 346 | void RemoteServerGetPrimaryServicesImpl( |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 347 | const blink::WebBluetoothDeviceId& device_id, |
beaufort.francois | 7952f00 | 2016-06-14 16:44:09 | [diff] [blame] | 348 | blink::mojom::WebBluetoothGATTQueryQuantity quantity, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 349 | const std::optional<device::BluetoothUUID>& services_uuid, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 350 | RemoteServerGetPrimaryServicesCallback callback, |
ortuno | 67acd83 | 2016-04-30 00:13:22 | [diff] [blame] | 351 | device::BluetoothDevice* device); |
| 352 | |
Reilly Grant | b936f9e | 2021-04-29 21:04:43 | [diff] [blame] | 353 | // Callback for BluetoothDeviceChooserController::GetDevice. |
| 354 | void OnGetDevice(RequestDeviceCallback callback, |
| 355 | blink::mojom::WebBluetoothResult result, |
| 356 | blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
| 357 | const std::string& device_id); |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 358 | |
ortuno | b6c45d4f | 2016-05-07 04:19:42 | [diff] [blame] | 359 | // Callbacks for BluetoothDevice::CreateGattConnection. |
Chris Mumford | f59d16b | 2021-06-23 00:32:11 | [diff] [blame] | 360 | void OnCreateGATTConnection( |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 361 | const blink::WebBluetoothDeviceId& device_id, |
Henrique Ferreiro | db47b3e | 2019-09-11 14:29:39 | [diff] [blame] | 362 | mojo::AssociatedRemote<blink::mojom::WebBluetoothServerClient> client, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 363 | RemoteServerConnectCallback callback, |
Chris Mumford | f59d16b | 2021-06-23 00:32:11 | [diff] [blame] | 364 | std::unique_ptr<device::BluetoothGattConnection> connection, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 365 | std::optional<device::BluetoothDevice::ConnectErrorCode> error_code); |
ortuno | b6c45d4f | 2016-05-07 04:19:42 | [diff] [blame] | 366 | |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 367 | // Callbacks for BluetoothRemoteGattCharacteristic::ReadRemoteCharacteristic. |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 368 | void OnCharacteristicReadValue( |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 369 | const std::string& characteristic_instance_id, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 370 | RemoteCharacteristicReadValueCallback callback, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 371 | std::optional<device::BluetoothGattService::GattErrorCode> error_code, |
dougt | 4f2237c | 2017-01-14 04:14:13 | [diff] [blame] | 372 | const std::vector<uint8_t>& value); |
ortuno | 12e91f07 | 2016-04-15 22:57:33 | [diff] [blame] | 373 | |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 374 | // Callbacks for BluetoothRemoteGattCharacteristic::WriteRemoteCharacteristic. |
dougt | 4f2237c | 2017-01-14 04:14:13 | [diff] [blame] | 375 | void OnCharacteristicWriteValueSuccess( |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 376 | RemoteCharacteristicWriteValueCallback callback); |
dougt | 4f2237c | 2017-01-14 04:14:13 | [diff] [blame] | 377 | void OnCharacteristicWriteValueFailed( |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 378 | const std::string& characteristic_instance_id, |
| 379 | const std::vector<uint8_t>& value, |
| 380 | blink::mojom::WebBluetoothWriteType write_type, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 381 | RemoteCharacteristicWriteValueCallback callback, |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 382 | device::BluetoothGattService::GattErrorCode error_code); |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 383 | |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 384 | // Callbacks for BluetoothRemoteGattCharacteristic::StartNotifySession. |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 385 | void OnStartNotifySessionSuccess( |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 386 | RemoteCharacteristicStartNotificationsCallback callback, |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 387 | std::unique_ptr<device::BluetoothGattNotifySession> notify_session); |
| 388 | void OnStartNotifySessionFailed( |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 389 | RemoteCharacteristicStartNotificationsCallback callback, |
Chris Mumford | e6fa96a4 | 2021-07-20 00:15:14 | [diff] [blame] | 390 | const std::string& characteristic_instance_id, |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 391 | device::BluetoothGattService::GattErrorCode error_code); |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 392 | |
| 393 | // Callback for BluetoothGattNotifySession::Stop. |
| 394 | void OnStopNotifySessionComplete( |
| 395 | const std::string& characteristic_instance_id, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 396 | RemoteCharacteristicStopNotificationsCallback callback); |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 397 | |
dougt | a2fe05521 | 2017-01-27 05:35:30 | [diff] [blame] | 398 | // Callbacks for BluetoothRemoteGattDescriptor::ReadRemoteDescriptor. |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 399 | void OnDescriptorReadValue( |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 400 | const std::string& descriptor_instance_id, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 401 | RemoteDescriptorReadValueCallback callback, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 402 | std::optional<device::BluetoothGattService::GattErrorCode> error_code, |
Chris Mumford | 6627ae4 | 2021-05-03 18:54:40 | [diff] [blame] | 403 | const std::vector<uint8_t>& value); |
dougt | a2fe05521 | 2017-01-27 05:35:30 | [diff] [blame] | 404 | |
dougt | be62e9d | 2017-02-01 16:13:55 | [diff] [blame] | 405 | // Callbacks for BluetoothRemoteGattDescriptor::WriteRemoteDescriptor. |
| 406 | void OnDescriptorWriteValueSuccess( |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 407 | RemoteDescriptorWriteValueCallback callback); |
dougt | be62e9d | 2017-02-01 16:13:55 | [diff] [blame] | 408 | void OnDescriptorWriteValueFailed( |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 409 | const std::string& descriptor_instance_id, |
| 410 | const std::vector<uint8_t>& value, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 411 | RemoteDescriptorWriteValueCallback callback, |
Chris Mumford | 860d05e | 2021-05-12 17:08:51 | [diff] [blame] | 412 | device::BluetoothGattService::GattErrorCode error_code); |
dougt | be62e9d | 2017-02-01 16:13:55 | [diff] [blame] | 413 | |
ortuno | b0fb6a18 | 2016-04-27 01:45:26 | [diff] [blame] | 414 | // Functions to query the platform cache for the bluetooth object. |
| 415 | // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the |
| 416 | // cache. Otherwise result.outcome that can used to record the outcome and |
| 417 | // result.error will contain the error that should be sent to the renderer. |
| 418 | // One of the possible outcomes is BAD_RENDERER. In this case we crash the |
| 419 | // renderer, record the reason and close the pipe, so it's safe to drop |
| 420 | // any callbacks. |
| 421 | |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 422 | // Queries the platform cache for a Device with |device_id| for |origin|. |
| 423 | // Fills in the |outcome| field and the |device| field if successful. |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 424 | CacheQueryResult QueryCacheForDevice( |
| 425 | const blink::WebBluetoothDeviceId& device_id); |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 426 | |
Chris Mumford | 550e012 | 2021-06-17 00:32:32 | [diff] [blame] | 427 | // Return the cached BluetoothDevice for the given |device_id|. |
| 428 | device::BluetoothDevice* GetCachedDevice( |
| 429 | const blink::WebBluetoothDeviceId& device_id); |
| 430 | |
ortuno | 67acd83 | 2016-04-30 00:13:22 | [diff] [blame] | 431 | // Queries the platform cache for a Service with |service_instance_id|. Fills |
| 432 | // in the |outcome| field, and |device| and |service| fields if successful. |
| 433 | CacheQueryResult QueryCacheForService(const std::string& service_instance_id); |
| 434 | |
ortuno | b0fb6a18 | 2016-04-27 01:45:26 | [diff] [blame] | 435 | // Queries the platform cache for a characteristic with |
| 436 | // |characteristic_instance_id|. Fills in the |outcome| field, and |device|, |
| 437 | // |service| and |characteristic| fields if successful. |
| 438 | CacheQueryResult QueryCacheForCharacteristic( |
| 439 | const std::string& characteristic_instance_id); |
| 440 | |
dougt | a2fe05521 | 2017-01-27 05:35:30 | [diff] [blame] | 441 | // Queries the platform cache for a descriptor with |descriptor_instance_id|. |
| 442 | // Fills in the |outcome| field, and |device|, |service|, |characteristic|, |
| 443 | // |descriptor| fields if successful. |
| 444 | CacheQueryResult QueryCacheForDescriptor( |
| 445 | const std::string& descriptor_instance_id); |
| 446 | |
Giovanni Ortuño Urquidi | 8688a0d | 2017-05-20 06:12:32 | [diff] [blame] | 447 | void RunPendingPrimaryServicesRequests(device::BluetoothDevice* device); |
| 448 | |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 449 | RenderProcessHost* GetRenderProcessHost(); |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 450 | device::BluetoothAdapter* GetAdapter(); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 451 | BluetoothAllowedDevices& allowed_devices(); |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 452 | |
Jun Cai | 732a05e3 | 2019-05-29 19:34:19 | [diff] [blame] | 453 | void StoreAllowedScanOptions( |
| 454 | const blink::mojom::WebBluetoothRequestLEScanOptions& options); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 455 | bool AreScanFiltersAllowed(const std::optional<ScanFilters>& filters) const; |
Jun Cai | 732a05e3 | 2019-05-29 19:34:19 | [diff] [blame] | 456 | |
Ovidio Henriquez | 5bfc1816 | 2020-03-24 16:17:27 | [diff] [blame] | 457 | // Clears state associated with Bluetooth LE Scanning. |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 458 | void ClearAdvertisementClients(); |
Ovidio Henriquez | 5bfc1816 | 2020-03-24 16:17:27 | [diff] [blame] | 459 | |
Ovidio Henriquez | 3d729f6 | 2020-02-07 00:43:29 | [diff] [blame] | 460 | bool IsAllowedToAccessAtLeastOneService( |
| 461 | const blink::WebBluetoothDeviceId& device_id); |
| 462 | bool IsAllowedToAccessService(const blink::WebBluetoothDeviceId& device_id, |
| 463 | const device::BluetoothUUID& service); |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 464 | bool IsAllowedToAccessManufacturerData( |
| 465 | const blink::WebBluetoothDeviceId& device_id, |
| 466 | uint16_t manufacturer_code); |
Ovidio Henriquez | 3d729f6 | 2020-02-07 00:43:29 | [diff] [blame] | 467 | |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 468 | // Returns true if at least |ble_scan_discovery_session_| or |
| 469 | // |watch_advertisements_discovery_session_| is active. |
| 470 | bool HasActiveDiscoverySession(); |
| 471 | |
Matt Reynolds | 8cca57d5 | 2025-02-20 18:20:59 | [diff] [blame] | 472 | // Prevents the associated RenderFrameHost from entering the back forward |
| 473 | // cache and evicts it if it was already added. |
| 474 | void PreventBackForwardCache(); |
| 475 | |
Chris Mumford | 02de39b | 2021-05-06 15:28:59 | [diff] [blame] | 476 | // WebBluetoothPairingManagerDelegate implementation: |
| 477 | blink::WebBluetoothDeviceId GetCharacteristicDeviceID( |
| 478 | const std::string& characteristic_instance_id) override; |
Chris Mumford | 61ee9ba | 2021-06-22 16:16:15 | [diff] [blame] | 479 | blink::WebBluetoothDeviceId GetDescriptorDeviceId( |
| 480 | const std::string& descriptor_instance_id) override; |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 481 | blink::WebBluetoothDeviceId GetWebBluetoothDeviceId( |
| 482 | const std::string& device_address) override; |
Chris Mumford | f59d16b | 2021-06-23 00:32:11 | [diff] [blame] | 483 | void PairDevice(const blink::WebBluetoothDeviceId& device_id, |
| 484 | device::BluetoothDevice::PairingDelegate* pairing_delegate, |
| 485 | device::BluetoothDevice::ConnectCallback callback) override; |
Chris Mumford | 550e012 | 2021-06-17 00:32:32 | [diff] [blame] | 486 | void CancelPairing(const blink::WebBluetoothDeviceId& device_id) override; |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 487 | void SetPinCode(const blink::WebBluetoothDeviceId& device_id, |
| 488 | const std::string& pincode) override; |
Alvin Ji | f610013 | 2022-07-07 21:53:43 | [diff] [blame] | 489 | void PromptForBluetoothPairing( |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 490 | const std::u16string& device_identifier, |
Alvin Ji | f610013 | 2022-07-07 21:53:43 | [diff] [blame] | 491 | BluetoothDelegate::PairPromptCallback callback, |
Alvin Ji | bab887b | 2022-07-27 00:59:54 | [diff] [blame] | 492 | BluetoothDelegate::PairingKind pairing_kind, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 493 | const std::optional<std::u16string>& pin) override; |
Alvin Ji | 3443b9b | 2022-06-17 19:57:47 | [diff] [blame] | 494 | void PairConfirmed(const blink::WebBluetoothDeviceId& device_id) override; |
Daniel Cheng | d6978b06 | 2023-11-16 00:11:38 | [diff] [blame] | 495 | |
| 496 | mojo::Receiver<blink::mojom::WebBluetoothService> receiver_; |
| 497 | |
ortuno | b6374dd8 | 2016-05-27 03:04:07 | [diff] [blame] | 498 | // Used to open a BluetoothChooser and start a device discovery session. |
| 499 | std::unique_ptr<BluetoothDeviceChooserController> device_chooser_controller_; |
| 500 | |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 501 | // Used to open a BluetoothScanningPrompt. |
| 502 | std::unique_ptr<BluetoothDeviceScanningPromptController> |
| 503 | device_scanning_prompt_controller_; |
| 504 | |
ortuno | b0fb6a18 | 2016-04-27 01:45:26 | [diff] [blame] | 505 | // Maps to get the object's parent based on its instanceID. |
ortuno | 67acd83 | 2016-04-30 00:13:22 | [diff] [blame] | 506 | std::unordered_map<std::string, std::string> service_id_to_device_address_; |
ortuno | b0fb6a18 | 2016-04-27 01:45:26 | [diff] [blame] | 507 | std::unordered_map<std::string, std::string> characteristic_id_to_service_id_; |
dougt | 4f2237c | 2017-01-14 04:14:13 | [diff] [blame] | 508 | std::unordered_map<std::string, std::string> |
| 509 | descriptor_id_to_characteristic_id_; |
ortuno | b0fb6a18 | 2016-04-27 01:45:26 | [diff] [blame] | 510 | |
ortuno | b6c45d4f | 2016-05-07 04:19:42 | [diff] [blame] | 511 | // Map to keep track of the connected Bluetooth devices. |
| 512 | std::unique_ptr<FrameConnectedBluetoothDevices> connected_devices_; |
| 513 | |
ortuno | 67acd83 | 2016-04-30 00:13:22 | [diff] [blame] | 514 | // Maps a device address to callbacks that are waiting for services to |
| 515 | // be discovered for that device. |
| 516 | std::unordered_map<std::string, std::vector<PrimaryServicesRequestCallback>> |
| 517 | pending_primary_services_requests_; |
| 518 | |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 519 | // Map to keep track of the characteristics' notify sessions. |
| 520 | std::unordered_map<std::string, |
juncai | 5fbf7e6 | 2017-03-23 21:21:56 | [diff] [blame] | 521 | std::unique_ptr<GATTNotifySessionAndCharacteristicClient>> |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 522 | characteristic_id_to_notify_session_; |
Chris Mumford | e6fa96a4 | 2021-07-20 00:15:14 | [diff] [blame] | 523 | // Map characteristic instance ID to deferred startNotification data. |
| 524 | std::unordered_map< |
| 525 | std::string, |
| 526 | base::queue<std::unique_ptr<DeferredStartNotificationData>>> |
| 527 | characteristic_id_to_deferred_start_; |
ortuno | bc3bce1 | 2016-04-15 21:22:55 | [diff] [blame] | 528 | |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 529 | // Keeps track of our BLE scanning session. |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 530 | std::unique_ptr<device::BluetoothDiscoverySession> |
| 531 | ble_scan_discovery_session_; |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 532 | |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 533 | // Keeps track of our watch advertisements discovery session. |
| 534 | std::unique_ptr<device::BluetoothDiscoverySession> |
| 535 | watch_advertisements_discovery_session_; |
| 536 | |
| 537 | // This queues up a scanning start callback so that we only have one |
| 538 | // BluetoothDiscoverySession start request at a time for a BLE scan. |
| 539 | RequestScanningStartCallback request_scanning_start_callback_; |
| 540 | |
Alvin Ji | f4c234d41 | 2023-08-23 16:58:54 | [diff] [blame] | 541 | // This queues up pending watch advertisements clients so that |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 542 | // we only have one BluetoothDiscoverySession start request at a time for |
| 543 | // watching device advertisements. |
Alvin Ji | f4c234d41 | 2023-08-23 16:58:54 | [diff] [blame] | 544 | std::vector<std::unique_ptr<WatchAdvertisementsClient>> |
| 545 | watch_advertisements_pending_clients_; |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 546 | |
| 547 | // List of clients that we must broadcast scan changes to. |
Doug Turner | 9b931de | 2019-01-08 18:22:37 | [diff] [blame] | 548 | std::vector<std::unique_ptr<ScanningClient>> scanning_clients_; |
Ovidio Henriquez | 887bb13 | 2020-06-04 21:45:55 | [diff] [blame] | 549 | std::vector<std::unique_ptr<WatchAdvertisementsClient>> |
| 550 | watch_advertisements_clients_; |
Doug Turner | 23a658b9 | 2018-12-20 01:57:54 | [diff] [blame] | 551 | |
Jun Cai | 732a05e3 | 2019-05-29 19:34:19 | [diff] [blame] | 552 | // Allowed Bluetooth scanning filters. |
| 553 | ScanFilters allowed_scan_filters_; |
| 554 | |
| 555 | // Whether a site has been allowed to receive all Bluetooth advertisement |
| 556 | // packets. |
| 557 | bool accept_all_advertisements_ = false; |
| 558 | |
Chris Mumford | 4d1cf3078 | 2021-10-05 23:02:47 | [diff] [blame] | 559 | #if PAIR_BLUETOOTH_ON_DEMAND() |
| 560 | std::unique_ptr<WebBluetoothPairingManager> pairing_manager_; |
| 561 | #endif |
| 562 | |
Matt Reynolds | 8cca57d5 | 2025-02-20 18:20:59 | [diff] [blame] | 563 | // When valid, prevents the frame from entering the back forward cache. |
| 564 | RenderFrameHostImpl::BackForwardCacheDisablingFeatureHandle |
| 565 | back_forward_cache_feature_handle_; |
| 566 | |
Andy Paicu | 1d7fc17 | 2021-01-20 18:29:45 | [diff] [blame] | 567 | base::ScopedObservation<BluetoothDelegate, |
Andrew Rayskiy | ebdd7f2 | 2022-11-07 18:59:27 | [diff] [blame] | 568 | BluetoothDelegate::FramePermissionObserver> |
Andy Paicu | 1d7fc17 | 2021-01-20 18:29:45 | [diff] [blame] | 569 | observer_{this}; |
| 570 | |
Chengwei Hsieh | 3de031c3 | 2025-08-01 23:32:02 | [diff] [blame] | 571 | // A set contains device ids that have GATT connection attempt ongoing. |
| 572 | std::unordered_set<blink::WebBluetoothDeviceId, |
| 573 | blink::WebBluetoothDeviceIdHash> |
| 574 | pending_connection_device_ids_; |
| 575 | |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 576 | base::WeakPtrFactory<WebBluetoothServiceImpl> weak_ptr_factory_{this}; |
ortuno | ad6b0fea | 2016-03-31 18:49:11 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | } // namespace content |
| 580 | |
| 581 | #endif // CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ |