Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 5 | #ifndef CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_ |
| 6 | #define CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_ |
| 7 | |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 8 | #include <map> |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 9 | #include <string> |
Ovidio Henriquez | 3d729f6 | 2020-02-07 00:43:29 | [diff] [blame^] | 10 | #include <utility> |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 11 | |
| 12 | #include "base/containers/flat_set.h" |
| 13 | #include "chrome/browser/permissions/chooser_context_base.h" |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 14 | #include "device/bluetooth/bluetooth_adapter.h" |
| 15 | #include "device/bluetooth/bluetooth_device.h" |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 16 | #include "device/bluetooth/public/cpp/bluetooth_uuid.h" |
| 17 | #include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h" |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 18 | #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h" |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 19 | |
| 20 | namespace base { |
| 21 | class Value; |
| 22 | } // namespace base |
| 23 | |
| 24 | namespace url { |
| 25 | class Origin; |
| 26 | } // namespace url |
| 27 | |
| 28 | // Manages the permissions for Web Bluetooth device objects. A Web Bluetooth |
| 29 | // permission object consists of its WebBluetoothDeviceId and set of Bluetooth |
| 30 | // service UUIDs. The WebBluetoothDeviceId is generated randomly by this class |
| 31 | // and is unique for a given Bluetooth device address and origin pair, so this |
| 32 | // class stores this mapping and provides utility methods to convert between |
| 33 | // the WebBluetoothDeviceId and Bluetooth device address. |
| 34 | class BluetoothChooserContext : public ChooserContextBase { |
| 35 | public: |
| 36 | explicit BluetoothChooserContext(Profile* profile); |
| 37 | ~BluetoothChooserContext() override; |
| 38 | |
| 39 | // Set class as move-only. |
| 40 | BluetoothChooserContext(const BluetoothChooserContext&) = delete; |
| 41 | BluetoothChooserContext& operator=(const BluetoothChooserContext&) = delete; |
| 42 | |
| 43 | // Helper methods for converting between a WebBluetoothDeviceId and a |
| 44 | // Bluetooth device address string for a given origin pair. |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 45 | blink::WebBluetoothDeviceId GetWebBluetoothDeviceId( |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 46 | const url::Origin& requesting_origin, |
| 47 | const url::Origin& embedding_origin, |
| 48 | const std::string& device_address); |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 49 | std::string GetDeviceAddress(const url::Origin& requesting_origin, |
| 50 | const url::Origin& embedding_origin, |
| 51 | const blink::WebBluetoothDeviceId& device_id); |
| 52 | |
| 53 | // Bluetooth scanning specific interface for generating WebBluetoothDeviceIds |
| 54 | // for scanned devices. |
| 55 | blink::WebBluetoothDeviceId AddScannedDevice( |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 56 | const url::Origin& requesting_origin, |
| 57 | const url::Origin& embedding_origin, |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 58 | const std::string& device_address); |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 59 | |
| 60 | // Bluetooth-specific interface for granting and checking permissions. |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 61 | blink::WebBluetoothDeviceId GrantServiceAccessPermission( |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 62 | const url::Origin& requesting_origin, |
| 63 | const url::Origin& embedding_origin, |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 64 | const device::BluetoothDevice* device, |
Ovidio Henriquez | 3d729f6 | 2020-02-07 00:43:29 | [diff] [blame^] | 65 | const blink::mojom::WebBluetoothRequestDeviceOptions* options); |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 66 | bool HasDevicePermission(const url::Origin& requesting_origin, |
| 67 | const url::Origin& embedding_origin, |
| 68 | const blink::WebBluetoothDeviceId& device_id); |
| 69 | bool IsAllowedToAccessAtLeastOneService( |
| 70 | const url::Origin& requesting_origin, |
| 71 | const url::Origin& embedding_origin, |
| 72 | const blink::WebBluetoothDeviceId& device_id); |
| 73 | bool IsAllowedToAccessService(const url::Origin& requesting_origin, |
| 74 | const url::Origin& embedding_origin, |
| 75 | const blink::WebBluetoothDeviceId& device_id, |
| 76 | device::BluetoothUUID service); |
| 77 | |
| 78 | protected: |
| 79 | // ChooserContextBase implementation; |
| 80 | bool IsValidObject(const base::Value& object) override; |
Ovidio Henriquez | efc822a3 | 2020-01-14 23:44:13 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | // This map records the generated Web Bluetooth IDs for devices discovered via |
| 84 | // the Scanning API. Each requesting/embedding origin pair has its own version |
| 85 | // of this map so that IDs cannot be correlated between cross-origin sites. |
| 86 | using DeviceAddressToIdMap = |
| 87 | std::map<std::string, blink::WebBluetoothDeviceId>; |
| 88 | std::map<std::pair<url::Origin, url::Origin>, DeviceAddressToIdMap> |
| 89 | scanned_devices_; |
Ovidio Henriquez | 3b282713 | 2019-12-04 17:44:30 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | #endif // CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_ |