Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
juncai | f70c5117 | 2017-02-10 23:49:17 | [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 | #include "content/browser/bluetooth/bluetooth_allowed_devices.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
Lei Zhang | de19767 | 2021-04-29 08:11:24 | [diff] [blame] | 10 | #include "base/containers/contains.h" |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 11 | #include "base/logging.h" |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
| 13 | #include "content/browser/bluetooth/bluetooth_blocklist.h" |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 14 | |
| 15 | using device::BluetoothUUID; |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | BluetoothAllowedDevices::BluetoothAllowedDevices() {} |
| 20 | BluetoothAllowedDevices::BluetoothAllowedDevices( |
| 21 | const BluetoothAllowedDevices& other) = default; |
| 22 | BluetoothAllowedDevices::~BluetoothAllowedDevices() {} |
| 23 | |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 24 | const blink::WebBluetoothDeviceId& BluetoothAllowedDevices::AddDevice( |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 25 | const std::string& device_address, |
| 26 | const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
Doug Turner | 10051df | 2019-01-17 02:21:55 | [diff] [blame] | 27 | auto& device_id = AddDevice(device_address); |
| 28 | AddUnionOfServicesTo(options, &device_id_to_services_map_[device_id]); |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 29 | AddManufacturerDataTo(options, &device_id_to_manufacturers_map_[device_id]); |
Doug Turner | 10051df | 2019-01-17 02:21:55 | [diff] [blame] | 30 | |
| 31 | // Currently, devices that are added with WebBluetoothRequestDeviceOptionsPtr |
| 32 | // |options| come from RequestDevice() and therefore have the ablity to be |
| 33 | // connected to. |
| 34 | device_id_to_connectable_map_[device_id] = true; |
| 35 | |
| 36 | return device_id; |
| 37 | } |
| 38 | |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 39 | const blink::WebBluetoothDeviceId& BluetoothAllowedDevices::AddDevice( |
Doug Turner | 10051df | 2019-01-17 02:21:55 | [diff] [blame] | 40 | const std::string& device_address) { |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 41 | DVLOG(1) << "Adding a device to Map of Allowed Devices."; |
| 42 | |
| 43 | auto id_iter = device_address_to_id_map_.find(device_address); |
| 44 | if (id_iter != device_address_to_id_map_.end()) { |
| 45 | DVLOG(1) << "Device already in map of allowed devices."; |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 46 | return device_address_to_id_map_[device_address]; |
| 47 | } |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 48 | const blink::WebBluetoothDeviceId device_id = GenerateUniqueDeviceId(); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 49 | DVLOG(1) << "Id generated for device: " << device_id; |
| 50 | |
| 51 | device_address_to_id_map_[device_address] = device_id; |
| 52 | device_id_to_address_map_[device_id] = device_address; |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 53 | |
| 54 | CHECK(device_id_set_.insert(device_id).second); |
| 55 | |
| 56 | return device_address_to_id_map_[device_address]; |
| 57 | } |
| 58 | |
| 59 | void BluetoothAllowedDevices::RemoveDevice(const std::string& device_address) { |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 60 | const blink::WebBluetoothDeviceId* device_id_ptr = |
| 61 | GetDeviceId(device_address); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 62 | DCHECK(device_id_ptr != nullptr); |
| 63 | |
| 64 | // We make a copy because we are going to remove the original value from its |
| 65 | // map. |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 66 | blink::WebBluetoothDeviceId device_id = *device_id_ptr; |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 67 | |
| 68 | // 1. Remove from all three maps. |
| 69 | CHECK(device_address_to_id_map_.erase(device_address)); |
| 70 | CHECK(device_id_to_address_map_.erase(device_id)); |
| 71 | CHECK(device_id_to_services_map_.erase(device_id)); |
| 72 | |
Doug Turner | 10051df | 2019-01-17 02:21:55 | [diff] [blame] | 73 | // Not all devices are connectable. |
| 74 | device_id_to_connectable_map_.erase(device_id); |
| 75 | |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 76 | // 2. Remove from set of ids. |
| 77 | CHECK(device_id_set_.erase(device_id)); |
| 78 | } |
| 79 | |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 80 | const blink::WebBluetoothDeviceId* BluetoothAllowedDevices::GetDeviceId( |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 81 | const std::string& device_address) { |
| 82 | auto id_iter = device_address_to_id_map_.find(device_address); |
| 83 | if (id_iter == device_address_to_id_map_.end()) { |
| 84 | return nullptr; |
| 85 | } |
| 86 | return &(id_iter->second); |
| 87 | } |
| 88 | |
| 89 | const std::string& BluetoothAllowedDevices::GetDeviceAddress( |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 90 | const blink::WebBluetoothDeviceId& device_id) { |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 91 | auto id_iter = device_id_to_address_map_.find(device_id); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 92 | return id_iter == device_id_to_address_map_.end() ? base::EmptyString() |
| 93 | : id_iter->second; |
| 94 | } |
| 95 | |
| 96 | bool BluetoothAllowedDevices::IsAllowedToAccessAtLeastOneService( |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 97 | const blink::WebBluetoothDeviceId& device_id) const { |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 98 | auto id_iter = device_id_to_services_map_.find(device_id); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 99 | return id_iter == device_id_to_services_map_.end() ? false |
| 100 | : !id_iter->second.empty(); |
| 101 | } |
| 102 | |
| 103 | bool BluetoothAllowedDevices::IsAllowedToAccessService( |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 104 | const blink::WebBluetoothDeviceId& device_id, |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 105 | const BluetoothUUID& service_uuid) const { |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 106 | if (BluetoothBlocklist::Get().IsExcluded(service_uuid)) |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 107 | return false; |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 108 | |
| 109 | auto id_iter = device_id_to_services_map_.find(device_id); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 110 | return id_iter == device_id_to_services_map_.end() |
| 111 | ? false |
Jan Wilken Dörrie | 77c581a | 2019-06-07 16:25:06 | [diff] [blame] | 112 | : base::Contains(id_iter->second, service_uuid); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 113 | } |
| 114 | |
Doug Turner | 10051df | 2019-01-17 02:21:55 | [diff] [blame] | 115 | bool BluetoothAllowedDevices::IsAllowedToGATTConnect( |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 116 | const blink::WebBluetoothDeviceId& device_id) const { |
Doug Turner | 10051df | 2019-01-17 02:21:55 | [diff] [blame] | 117 | auto id_iter = device_id_to_connectable_map_.find(device_id); |
| 118 | if (id_iter == device_id_to_connectable_map_.end()) |
| 119 | return false; |
| 120 | return id_iter->second; |
| 121 | } |
| 122 | |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 123 | bool BluetoothAllowedDevices::IsAllowedToAccessManufacturerData( |
| 124 | const blink::WebBluetoothDeviceId& device_id, |
| 125 | const uint16_t manufacturer_code) const { |
| 126 | auto id_iter = device_id_to_manufacturers_map_.find(device_id); |
| 127 | return id_iter == device_id_to_manufacturers_map_.end() |
| 128 | ? false |
| 129 | : base::Contains(id_iter->second, manufacturer_code); |
| 130 | } |
| 131 | |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 132 | blink::WebBluetoothDeviceId BluetoothAllowedDevices::GenerateUniqueDeviceId() { |
| 133 | blink::WebBluetoothDeviceId device_id = blink::WebBluetoothDeviceId::Create(); |
Jan Wilken Dörrie | 77c581a | 2019-06-07 16:25:06 | [diff] [blame] | 134 | while (base::Contains(device_id_set_, device_id)) { |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 135 | LOG(WARNING) << "Generated repeated id."; |
Ovidio Henriquez | 0e8ab707 | 2019-05-31 21:38:07 | [diff] [blame] | 136 | device_id = blink::WebBluetoothDeviceId::Create(); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 137 | } |
| 138 | return device_id; |
| 139 | } |
| 140 | |
| 141 | void BluetoothAllowedDevices::AddUnionOfServicesTo( |
| 142 | const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
| 143 | std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* |
| 144 | unionOfServices) { |
| 145 | if (options->filters) { |
| 146 | for (const auto& filter : options->filters.value()) { |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 147 | if (!filter->services) |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 148 | continue; |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 149 | |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 150 | for (const BluetoothUUID& uuid : filter->services.value()) |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 151 | unionOfServices->insert(uuid); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 155 | for (const BluetoothUUID& uuid : options->optional_services) |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 156 | unionOfServices->insert(uuid); |
Ovidio Henriquez | bbc7853c | 2020-09-17 22:36:56 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void BluetoothAllowedDevices::AddManufacturerDataTo( |
| 160 | const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
| 161 | base::flat_set<uint16_t>* manufacturer_codes) { |
| 162 | for (const uint16_t manufacturer_code : options->optional_manufacturer_data) |
| 163 | manufacturer_codes->insert(manufacturer_code); |
juncai | f70c5117 | 2017-02-10 23:49:17 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | } // namespace content |