| 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 <string> | |
| 8 #include <vector> | 7 #include <vector> |
| 9 | 8 |
| 9 #include "base/base64.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/optional.h" | 11 #include "base/optional.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 14 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
| 15 #include "content/common/bluetooth/web_bluetooth_device_id.h" | 15 #include "crypto/random.h" |
| 16 | 16 |
| 17 using device::BluetoothUUID; | 17 using device::BluetoothUUID; |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 namespace { |
| 22 const size_t kIdLength = 16 /* 128bits */; |
| 23 |
| 24 std::string GetBase64Id() { |
| 25 std::string bytes( |
| 26 kIdLength + 1 /* to avoid bytes being reallocated by WriteInto */, '\0'); |
| 27 |
| 28 crypto::RandBytes( |
| 29 base::WriteInto(&bytes /* str */, kIdLength + 1 /* length_with_null */), |
| 30 kIdLength); |
| 31 |
| 32 base::Base64Encode(bytes, &bytes); |
| 33 |
| 34 return bytes; |
| 35 } |
| 36 } // namespace |
| 37 |
| 21 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} | 38 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
| 22 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | 39 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
| 23 | 40 |
| 24 const WebBluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( | 41 const std::string& BluetoothAllowedDevicesMap::AddDevice( |
| 25 const url::Origin& origin, | 42 const url::Origin& origin, |
| 26 const std::string& device_address, | 43 const std::string& device_address, |
| 27 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 44 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| 28 VLOG(1) << "Adding a device to Map of Allowed Devices."; | 45 VLOG(1) << "Adding a device to Map of Allowed Devices."; |
| 29 | 46 |
| 30 // "Unique" Origins generate the same key in maps, therefore are not | 47 // "Unique" Origins generate the same key in maps, therefore are not |
| 31 // supported. | 48 // supported. |
| 32 CHECK(!origin.unique()); | 49 CHECK(!origin.unique()); |
| 33 | 50 |
| 34 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; | 51 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; |
| 35 auto id_iter = device_address_to_id_map.find(device_address); | 52 auto id_iter = device_address_to_id_map.find(device_address); |
| 36 if (id_iter != device_address_to_id_map.end()) { | 53 if (id_iter != device_address_to_id_map.end()) { |
| 37 VLOG(1) << "Device already in map of allowed devices."; | 54 VLOG(1) << "Device already in map of allowed devices."; |
| 38 const auto& device_id = id_iter->second; | 55 const auto& device_id = id_iter->second; |
| 39 | 56 |
| 40 AddUnionOfServicesTo( | 57 AddUnionOfServicesTo( |
| 41 options, &origin_to_device_id_to_services_map_[origin][device_id]); | 58 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
| 42 | 59 |
| 43 return origin_to_device_address_to_id_map_[origin][device_address]; | 60 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 44 } | 61 } |
| 45 const WebBluetoothDeviceId device_id = GenerateUniqueDeviceId(); | 62 const std::string device_id = GenerateDeviceId(); |
| 46 VLOG(1) << "Id generated for device: " << device_id; | 63 VLOG(1) << "Id generated for device: " << device_id; |
| 47 | 64 |
| 48 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 65 origin_to_device_address_to_id_map_[origin][device_address] = device_id; |
| 49 origin_to_device_id_to_address_map_[origin][device_id] = device_address; | 66 origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
| 50 AddUnionOfServicesTo( | 67 AddUnionOfServicesTo( |
| 51 options, &origin_to_device_id_to_services_map_[origin][device_id]); | 68 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
| 52 | 69 |
| 53 CHECK(device_id_set_.insert(device_id).second); | 70 CHECK(device_id_set_.insert(device_id).second); |
| 54 | 71 |
| 55 return origin_to_device_address_to_id_map_[origin][device_address]; | 72 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 56 } | 73 } |
| 57 | 74 |
| 58 void BluetoothAllowedDevicesMap::RemoveDevice( | 75 void BluetoothAllowedDevicesMap::RemoveDevice( |
| 59 const url::Origin& origin, | 76 const url::Origin& origin, |
| 60 const std::string& device_address) { | 77 const std::string& device_address) { |
| 61 const WebBluetoothDeviceId* device_id_ptr = | 78 const std::string device_id = GetDeviceId(origin, device_address); |
| 62 GetDeviceId(origin, device_address); | 79 DCHECK(!device_id.empty()); |
| 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. | |
| 67 WebBluetoothDeviceId device_id = *device_id_ptr; | |
| 68 | 80 |
| 69 // 1. Remove from all three maps. | 81 // 1. Remove from all three maps. |
| 70 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | 82 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); |
| 71 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); | 83 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); |
| 72 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | 84 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); |
| 73 | 85 |
| 74 // 2. Remove empty map for origin. | 86 // 2. Remove empty map for origin. |
| 75 if (origin_to_device_address_to_id_map_[origin].empty()) { | 87 if (origin_to_device_address_to_id_map_[origin].empty()) { |
| 76 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | 88 CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
| 77 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | 89 CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
| 78 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | 90 CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
| 79 } | 91 } |
| 80 | 92 |
| 81 // 3. Remove from set of ids. | 93 // 3. Remove from set of ids. |
| 82 CHECK(device_id_set_.erase(device_id)); | 94 CHECK(device_id_set_.erase(device_id)); |
| 83 } | 95 } |
| 84 | 96 |
| 85 const WebBluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( | 97 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( |
| 86 const url::Origin& origin, | 98 const url::Origin& origin, |
| 87 const std::string& device_address) { | 99 const std::string& device_address) { |
| 88 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 100 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); |
| 89 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { | 101 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { |
| 90 return nullptr; | 102 return base::EmptyString(); |
| 91 } | 103 } |
| 92 | 104 |
| 93 const auto& device_address_to_id_map = address_map_iter->second; | 105 const auto& device_address_to_id_map = address_map_iter->second; |
| 94 | 106 |
| 95 auto id_iter = device_address_to_id_map.find(device_address); | 107 auto id_iter = device_address_to_id_map.find(device_address); |
| 96 if (id_iter == device_address_to_id_map.end()) { | 108 if (id_iter == device_address_to_id_map.end()) { |
| 97 return nullptr; | 109 return base::EmptyString(); |
| 98 } | 110 } |
| 99 return &(id_iter->second); | 111 return id_iter->second; |
| 100 } | 112 } |
| 101 | 113 |
| 102 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | 114 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
| 103 const url::Origin& origin, | 115 const url::Origin& origin, |
| 104 const WebBluetoothDeviceId& device_id) { | 116 const std::string& device_id) { |
| 105 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 117 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); |
| 106 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | 118 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { |
| 107 return base::EmptyString(); | 119 return base::EmptyString(); |
| 108 } | 120 } |
| 109 | 121 |
| 110 const auto& device_id_to_address_map = id_map_iter->second; | 122 const auto& device_id_to_address_map = id_map_iter->second; |
| 111 | 123 |
| 112 auto id_iter = device_id_to_address_map.find(device_id); | 124 auto id_iter = device_id_to_address_map.find(device_id); |
| 113 | 125 |
| 114 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 126 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
| 115 : id_iter->second; | 127 : id_iter->second; |
| 116 } | 128 } |
| 117 | 129 |
| 118 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 130 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
| 119 const url::Origin& origin, | 131 const url::Origin& origin, |
| 120 const WebBluetoothDeviceId& device_id, | 132 const std::string& device_id, |
| 121 const BluetoothUUID& service_uuid) const { | 133 const BluetoothUUID& service_uuid) const { |
| 122 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { | 134 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { |
| 123 return false; | 135 return false; |
| 124 } | 136 } |
| 125 | 137 |
| 126 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 138 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); |
| 127 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | 139 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { |
| 128 return false; | 140 return false; |
| 129 } | 141 } |
| 130 | 142 |
| 131 const auto& device_id_to_services_map = id_map_iter->second; | 143 const auto& device_id_to_services_map = id_map_iter->second; |
| 132 | 144 |
| 133 auto id_iter = device_id_to_services_map.find(device_id); | 145 auto id_iter = device_id_to_services_map.find(device_id); |
| 134 | 146 |
| 135 return id_iter == device_id_to_services_map.end() | 147 return id_iter == device_id_to_services_map.end() |
| 136 ? false | 148 ? false |
| 137 : ContainsKey(id_iter->second, service_uuid); | 149 : ContainsKey(id_iter->second, service_uuid); |
| 138 } | 150 } |
| 139 | 151 |
| 140 WebBluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { | 152 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { |
| 141 WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create(); | 153 std::string device_id = GetBase64Id(); |
| 142 while (ContainsKey(device_id_set_, device_id)) { | 154 while (ContainsKey(device_id_set_, device_id)) { |
| 143 LOG(WARNING) << "Generated repeated id."; | 155 LOG(WARNING) << "Generated repeated id."; |
| 144 device_id = WebBluetoothDeviceId::Create(); | 156 device_id = GetBase64Id(); |
| 145 } | 157 } |
| 146 return device_id; | 158 return device_id; |
| 147 } | 159 } |
| 148 | 160 |
| 149 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 161 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
| 150 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 162 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
| 151 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* | 163 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* |
| 152 unionOfServices) { | 164 unionOfServices) { |
| 153 for (const auto& filter : options->filters) { | 165 for (const auto& filter : options->filters) { |
| 154 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { | 166 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { |
| 155 unionOfServices->insert(uuid.value()); | 167 unionOfServices->insert(uuid.value()); |
| 156 } | 168 } |
| 157 } | 169 } |
| 158 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { | 170 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { |
| 159 unionOfServices->insert(uuid.value()); | 171 unionOfServices->insert(uuid.value()); |
| 160 } | 172 } |
| 161 } | 173 } |
| 162 | 174 |
| 163 } // namespace content | 175 } // namespace content |
| OLD | NEW |