| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | 5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 13 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
| 14 #include "content/common/bluetooth/bluetooth_scan_filter.h" | |
| 15 #include "crypto/random.h" | 14 #include "crypto/random.h" |
| 16 #include "device/bluetooth/bluetooth_uuid.h" | |
| 17 | 15 |
| 18 using device::BluetoothUUID; | 16 using device::BluetoothUUID; |
| 19 | 17 |
| 20 namespace content { | 18 namespace content { |
| 21 | 19 |
| 22 namespace { | 20 namespace { |
| 23 const size_t kIdLength = 16 /* 128bits */; | 21 const size_t kIdLength = 16 /* 128bits */; |
| 24 | 22 |
| 25 std::string GetBase64Id() { | 23 std::string GetBase64Id() { |
| 26 std::string bytes( | 24 std::string bytes( |
| 27 kIdLength + 1 /* to avoid bytes being reallocated by WriteInto */, '\0'); | 25 kIdLength + 1 /* to avoid bytes being reallocated by WriteInto */, '\0'); |
| 28 | 26 |
| 29 crypto::RandBytes( | 27 crypto::RandBytes( |
| 30 base::WriteInto(&bytes /* str */, kIdLength + 1 /* length_with_null */), | 28 base::WriteInto(&bytes /* str */, kIdLength + 1 /* length_with_null */), |
| 31 kIdLength); | 29 kIdLength); |
| 32 | 30 |
| 33 base::Base64Encode(bytes, &bytes); | 31 base::Base64Encode(bytes, &bytes); |
| 34 | 32 |
| 35 return bytes; | 33 return bytes; |
| 36 } | 34 } |
| 37 } // namespace | 35 } // namespace |
| 38 | 36 |
| 39 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} | 37 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
| 40 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | 38 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
| 41 | 39 |
| 42 const std::string& BluetoothAllowedDevicesMap::AddDevice( | 40 const std::string& BluetoothAllowedDevicesMap::AddDevice( |
| 43 const url::Origin& origin, | 41 const url::Origin& origin, |
| 44 const std::string& device_address, | 42 const std::string& device_address, |
| 45 const std::vector<BluetoothScanFilter>& filters, | 43 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| 46 const std::vector<BluetoothUUID>& optional_services) { | |
| 47 VLOG(1) << "Adding a device to Map of Allowed Devices."; | 44 VLOG(1) << "Adding a device to Map of Allowed Devices."; |
| 48 | 45 |
| 49 // "Unique" Origins generate the same key in maps. The set of "unique" | 46 // "Unique" Origins generate the same key in maps, therefore are not |
| 50 // Origins that generate the same key does not intersect the set of | 47 // supported. |
| 51 // potentially trustworthy origins; since Bluetooth is only available for | |
| 52 // potntially trustworthy origins we should never receive a request from a | |
| 53 // "unique" Origin. | |
| 54 // See url::Origin for what constitutes a "unique" Origin and the | |
| 55 // Secure Contexts spec for what constitutes a Trusworthy Origin: | |
| 56 // https://w3c.github.io/webappsec-secure-contexts/ | |
| 57 CHECK(!origin.unique()); | 48 CHECK(!origin.unique()); |
| 58 | 49 |
| 59 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; | 50 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; |
| 60 auto id_iter = device_address_to_id_map.find(device_address); | 51 auto id_iter = device_address_to_id_map.find(device_address); |
| 61 if (id_iter != device_address_to_id_map.end()) { | 52 if (id_iter != device_address_to_id_map.end()) { |
| 62 VLOG(1) << "Device already in map of allowed devices."; | 53 VLOG(1) << "Device already in map of allowed devices."; |
| 63 const auto& device_id = id_iter->second; | 54 const auto& device_id = id_iter->second; |
| 64 | 55 |
| 65 AddUnionOfServicesTo( | 56 AddUnionOfServicesTo( |
| 66 filters, optional_services, | 57 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
| 67 &origin_to_device_id_to_services_map_[origin][device_id]); | |
| 68 | 58 |
| 69 return origin_to_device_address_to_id_map_[origin][device_address]; | 59 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 70 } | 60 } |
| 71 const std::string device_id = GenerateDeviceId(); | 61 const std::string device_id = GenerateDeviceId(); |
| 72 VLOG(1) << "Id generated for device: " << device_id; | 62 VLOG(1) << "Id generated for device: " << device_id; |
| 73 | 63 |
| 74 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 64 origin_to_device_address_to_id_map_[origin][device_address] = device_id; |
| 75 origin_to_device_id_to_address_map_[origin][device_id] = device_address; | 65 origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
| 76 AddUnionOfServicesTo( | 66 AddUnionOfServicesTo( |
| 77 filters, optional_services, | 67 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
| 78 &origin_to_device_id_to_services_map_[origin][device_id]); | |
| 79 | 68 |
| 80 CHECK(device_id_set_.insert(device_id).second); | 69 CHECK(device_id_set_.insert(device_id).second); |
| 81 | 70 |
| 82 return origin_to_device_address_to_id_map_[origin][device_address]; | 71 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 83 } | 72 } |
| 84 | 73 |
| 85 void BluetoothAllowedDevicesMap::RemoveDevice( | 74 void BluetoothAllowedDevicesMap::RemoveDevice( |
| 86 const url::Origin& origin, | 75 const url::Origin& origin, |
| 87 const std::string& device_address) { | 76 const std::string& device_address) { |
| 88 const std::string device_id = GetDeviceId(origin, device_address); | 77 const std::string device_id = GetDeviceId(origin, device_address); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 auto id_iter = device_id_to_address_map.find(device_id); | 123 auto id_iter = device_id_to_address_map.find(device_id); |
| 135 | 124 |
| 136 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 125 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
| 137 : id_iter->second; | 126 : id_iter->second; |
| 138 } | 127 } |
| 139 | 128 |
| 140 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 129 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
| 141 const url::Origin& origin, | 130 const url::Origin& origin, |
| 142 const std::string& device_id, | 131 const std::string& device_id, |
| 143 const std::string& service_uuid) const { | 132 const std::string& service_uuid) const { |
| 144 if (BluetoothBlacklist::Get().IsExcluded(BluetoothUUID(service_uuid))) | 133 if (BluetoothBlacklist::Get().IsExcluded(BluetoothUUID(service_uuid))) { |
| 145 return false; | 134 return false; |
| 135 } |
| 146 | 136 |
| 147 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 137 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); |
| 148 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | 138 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { |
| 149 return false; | 139 return false; |
| 150 } | 140 } |
| 151 | 141 |
| 152 const auto& device_id_to_services_map = id_map_iter->second; | 142 const auto& device_id_to_services_map = id_map_iter->second; |
| 153 | 143 |
| 154 auto id_iter = device_id_to_services_map.find(device_id); | 144 auto id_iter = device_id_to_services_map.find(device_id); |
| 155 | 145 |
| 156 return id_iter == device_id_to_services_map.end() | 146 return id_iter == device_id_to_services_map.end() |
| 157 ? false | 147 ? false |
| 158 : ContainsKey(id_iter->second, service_uuid); | 148 : ContainsKey(id_iter->second, service_uuid); |
| 159 } | 149 } |
| 160 | 150 |
| 161 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { | 151 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { |
| 162 std::string device_id = GetBase64Id(); | 152 std::string device_id = GetBase64Id(); |
| 163 while (ContainsKey(device_id_set_, device_id)) { | 153 while (ContainsKey(device_id_set_, device_id)) { |
| 164 LOG(WARNING) << "Generated repeated id."; | 154 LOG(WARNING) << "Generated repeated id."; |
| 165 device_id = GetBase64Id(); | 155 device_id = GetBase64Id(); |
| 166 } | 156 } |
| 167 return device_id; | 157 return device_id; |
| 168 } | 158 } |
| 169 | 159 |
| 170 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 160 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
| 171 const std::vector<BluetoothScanFilter>& filters, | 161 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
| 172 const std::vector<device::BluetoothUUID>& optional_services, | |
| 173 std::set<std::string>* unionOfServices) { | 162 std::set<std::string>* unionOfServices) { |
| 174 for (const auto& filter : filters) { | 163 for (const auto& filter : options->filters) { |
| 175 for (const BluetoothUUID& uuid : filter.services) { | 164 for (const std::string& uuid : filter->services) { |
| 176 unionOfServices->insert(uuid.canonical_value()); | 165 unionOfServices->insert(uuid); |
| 177 } | 166 } |
| 178 } | 167 } |
| 179 for (const BluetoothUUID& uuid : optional_services) { | 168 for (const std::string& uuid : options->optional_services) { |
| 180 unionOfServices->insert(uuid.canonical_value()); | 169 unionOfServices->insert(uuid); |
| 181 } | 170 } |
| 182 } | 171 } |
| 183 | 172 |
| 184 } // namespace content | 173 } // namespace content |
| OLD | NEW |