Index: content/browser/bluetooth/bluetooth_allowed_devices_map.cc |
diff --git a/content/browser/bluetooth/bluetooth_allowed_devices_map.cc b/content/browser/bluetooth/bluetooth_allowed_devices_map.cc |
index b45166427242eb2f7b022f18ac24eac2e57ba7d2..1e8f79b115a6f415c6e463504eb09461137a851b 100644 |
--- a/content/browser/bluetooth/bluetooth_allowed_devices_map.cc |
+++ b/content/browser/bluetooth/bluetooth_allowed_devices_map.cc |
@@ -4,24 +4,41 @@ |
#include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
-#include <string> |
#include <vector> |
+#include "base/base64.h" |
#include "base/logging.h" |
#include "base/optional.h" |
#include "base/stl_util.h" |
#include "base/strings/string_util.h" |
#include "content/browser/bluetooth/bluetooth_blacklist.h" |
-#include "content/common/bluetooth/web_bluetooth_device_id.h" |
+#include "crypto/random.h" |
using device::BluetoothUUID; |
namespace content { |
+namespace { |
+const size_t kIdLength = 16 /* 128bits */; |
+ |
+std::string GetBase64Id() { |
+ std::string bytes( |
+ kIdLength + 1 /* to avoid bytes being reallocated by WriteInto */, '\0'); |
+ |
+ crypto::RandBytes( |
+ base::WriteInto(&bytes /* str */, kIdLength + 1 /* length_with_null */), |
+ kIdLength); |
+ |
+ base::Base64Encode(bytes, &bytes); |
+ |
+ return bytes; |
+} |
+} // namespace |
+ |
BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
-const WebBluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( |
+const std::string& BluetoothAllowedDevicesMap::AddDevice( |
const url::Origin& origin, |
const std::string& device_address, |
const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
@@ -42,7 +59,7 @@ |
return origin_to_device_address_to_id_map_[origin][device_address]; |
} |
- const WebBluetoothDeviceId device_id = GenerateUniqueDeviceId(); |
+ const std::string device_id = GenerateDeviceId(); |
VLOG(1) << "Id generated for device: " << device_id; |
origin_to_device_address_to_id_map_[origin][device_address] = device_id; |
@@ -58,13 +75,8 @@ |
void BluetoothAllowedDevicesMap::RemoveDevice( |
const url::Origin& origin, |
const std::string& device_address) { |
- const WebBluetoothDeviceId* device_id_ptr = |
- GetDeviceId(origin, device_address); |
- DCHECK(device_id_ptr != nullptr); |
- |
- // We make a copy because we are going to remove the original value from its |
- // map. |
- WebBluetoothDeviceId device_id = *device_id_ptr; |
+ const std::string device_id = GetDeviceId(origin, device_address); |
+ DCHECK(!device_id.empty()); |
// 1. Remove from all three maps. |
CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); |
@@ -82,26 +94,26 @@ |
CHECK(device_id_set_.erase(device_id)); |
} |
-const WebBluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( |
+const std::string& BluetoothAllowedDevicesMap::GetDeviceId( |
const url::Origin& origin, |
const std::string& device_address) { |
auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); |
if (address_map_iter == origin_to_device_address_to_id_map_.end()) { |
- return nullptr; |
+ return base::EmptyString(); |
} |
const auto& device_address_to_id_map = address_map_iter->second; |
auto id_iter = device_address_to_id_map.find(device_address); |
if (id_iter == device_address_to_id_map.end()) { |
- return nullptr; |
+ return base::EmptyString(); |
} |
- return &(id_iter->second); |
+ return id_iter->second; |
} |
const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
const url::Origin& origin, |
- const WebBluetoothDeviceId& device_id) { |
+ const std::string& device_id) { |
auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); |
if (id_map_iter == origin_to_device_id_to_address_map_.end()) { |
return base::EmptyString(); |
@@ -117,7 +129,7 @@ |
bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
const url::Origin& origin, |
- const WebBluetoothDeviceId& device_id, |
+ const std::string& device_id, |
const BluetoothUUID& service_uuid) const { |
if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { |
return false; |
@@ -137,11 +149,11 @@ |
: ContainsKey(id_iter->second, service_uuid); |
} |
-WebBluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { |
- WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create(); |
+std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { |
+ std::string device_id = GetBase64Id(); |
while (ContainsKey(device_id_set_, device_id)) { |
LOG(WARNING) << "Generated repeated id."; |
- device_id = WebBluetoothDeviceId::Create(); |
+ device_id = GetBase64Id(); |
} |
return device_id; |
} |