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