Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [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 | |
Michael van Ouwerkerk | c7639b6 | 2021-05-20 12:08:23 | [diff] [blame] | 5 | #ifndef COMPONENTS_PERMISSIONS_CONTEXTS_BLUETOOTH_CHOOSER_CONTEXT_H_ |
| 6 | #define COMPONENTS_PERMISSIONS_CONTEXTS_BLUETOOTH_CHOOSER_CONTEXT_H_ |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 7 | |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 8 | #include <map> |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame^] | 9 | #include <optional> |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 10 | #include <string> |
Ovidio Henriquez | 3d729f6 | 2020-02-07 00:43:29 | [diff] [blame] | 11 | #include <utility> |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 12 | |
Ken Buchanan | 76365d1 | 2021-04-20 18:46:19 | [diff] [blame] | 13 | #include "components/permissions/object_permission_context_base.h" |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 14 | #include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h" |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 15 | #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h" |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | class Value; |
| 19 | } // namespace base |
| 20 | |
Michael van Ouwerkerk | c7639b6 | 2021-05-20 12:08:23 | [diff] [blame] | 21 | namespace content { |
| 22 | class BrowserContext; |
| 23 | } // namespace content |
| 24 | |
| 25 | namespace device { |
| 26 | class BluetoothDevice; |
| 27 | class BluetoothUUID; |
| 28 | } // namespace device |
| 29 | |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 30 | namespace url { |
| 31 | class Origin; |
| 32 | } // namespace url |
| 33 | |
Michael van Ouwerkerk | c7639b6 | 2021-05-20 12:08:23 | [diff] [blame] | 34 | namespace permissions { |
| 35 | |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 36 | // Manages the permissions for Web Bluetooth device objects. A Web Bluetooth |
| 37 | // permission object consists of its WebBluetoothDeviceId and set of Bluetooth |
| 38 | // service UUIDs. The WebBluetoothDeviceId is generated randomly by this class |
| 39 | // and is unique for a given Bluetooth device address and origin pair, so this |
| 40 | // class stores this mapping and provides utility methods to convert between |
| 41 | // the WebBluetoothDeviceId and Bluetooth device address. |
Michael van Ouwerkerk | c7639b6 | 2021-05-20 12:08:23 | [diff] [blame] | 42 | class BluetoothChooserContext : public ObjectPermissionContextBase { |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 43 | public: |
Michael van Ouwerkerk | c7639b6 | 2021-05-20 12:08:23 | [diff] [blame] | 44 | explicit BluetoothChooserContext(content::BrowserContext* browser_context); |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 45 | ~BluetoothChooserContext() override; |
| 46 | |
| 47 | // Set class as move-only. |
| 48 | BluetoothChooserContext(const BluetoothChooserContext&) = delete; |
| 49 | BluetoothChooserContext& operator=(const BluetoothChooserContext&) = delete; |
| 50 | |
Jack Hsieh | 79456011 | 2023-01-26 15:58:51 | [diff] [blame] | 51 | static base::Value::Dict DeviceInfoToValue( |
| 52 | const device::BluetoothDevice* device, |
| 53 | const blink::mojom::WebBluetoothRequestDeviceOptions* options, |
| 54 | const blink::WebBluetoothDeviceId& device_id); |
| 55 | |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 56 | // Helper methods for converting between a WebBluetoothDeviceId and a |
| 57 | // Bluetooth device address string for a given origin pair. |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 58 | blink::WebBluetoothDeviceId GetWebBluetoothDeviceId( |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 59 | const url::Origin& origin, |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 60 | const std::string& device_address); |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 61 | std::string GetDeviceAddress(const url::Origin& origin, |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 62 | const blink::WebBluetoothDeviceId& device_id); |
| 63 | |
| 64 | // Bluetooth scanning specific interface for generating WebBluetoothDeviceIds |
| 65 | // for scanned devices. |
| 66 | blink::WebBluetoothDeviceId AddScannedDevice( |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 67 | const url::Origin& origin, |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 68 | const std::string& device_address); |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 69 | |
| 70 | // Bluetooth-specific interface for granting and checking permissions. |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 71 | blink::WebBluetoothDeviceId GrantServiceAccessPermission( |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 72 | const url::Origin& origin, |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 73 | const device::BluetoothDevice* device, |
Ovidio Henriquez | 3d729f6 | 2020-02-07 00:43:29 | [diff] [blame] | 74 | const blink::mojom::WebBluetoothRequestDeviceOptions* options); |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 75 | bool HasDevicePermission(const url::Origin& origin, |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 76 | const blink::WebBluetoothDeviceId& device_id); |
François Beaufort | f100253a8 | 2022-03-10 11:26:31 | [diff] [blame] | 77 | void RevokeDevicePermissionWebInitiated( |
| 78 | const url::Origin& origin, |
| 79 | const blink::WebBluetoothDeviceId& device_id); |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 80 | bool IsAllowedToAccessAtLeastOneService( |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 81 | const url::Origin& origin, |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 82 | const blink::WebBluetoothDeviceId& device_id); |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 83 | bool IsAllowedToAccessService(const url::Origin& origin, |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 84 | const blink::WebBluetoothDeviceId& device_id, |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 85 | const device::BluetoothUUID& service); |
| 86 | bool IsAllowedToAccessManufacturerData( |
Andy Paicu | 13f3f46 | 2021-03-09 12:14:56 | [diff] [blame] | 87 | const url::Origin& origin, |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 88 | const blink::WebBluetoothDeviceId& device_id, |
| 89 | uint16_t manufacturer_code); |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 90 | |
Ovidio Henriquez | ab1ae30 | 2020-03-10 04:45:38 | [diff] [blame] | 91 | static blink::WebBluetoothDeviceId GetObjectDeviceId( |
Yoichi Osato | c918825 | 2023-04-27 01:21:26 | [diff] [blame] |
|