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