Chromium Code Reviews| 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 1e7074b848db2e876318b945eef244262fe58c70..8bd97084a3dbaea2f001b3c06c6f5670f91f9ba3 100644 |
| --- a/content/browser/bluetooth/bluetooth_allowed_devices_map.cc |
| +++ b/content/browser/bluetooth/bluetooth_allowed_devices_map.cc |
| @@ -4,41 +4,24 @@ |
| #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 "crypto/random.h" |
| +#include "content/common/bluetooth/bluetooth_device_id.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 std::string& BluetoothAllowedDevicesMap::AddDevice( |
| +const BluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( |
| const url::Origin& origin, |
| const std::string& device_address, |
| const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| @@ -59,8 +42,8 @@ const std::string& BluetoothAllowedDevicesMap::AddDevice( |
| return origin_to_device_address_to_id_map_[origin][device_address]; |
| } |
| - const std::string device_id = GenerateDeviceId(); |
| - VLOG(1) << "Id generated for device: " << device_id; |
| + const BluetoothDeviceId device_id = GenerateUniqueDeviceId(); |
| + VLOG(1) << "Id generated for device: " << device_id.str(); |
| origin_to_device_address_to_id_map_[origin][device_address] = device_id; |
| origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
| @@ -75,13 +58,14 @@ const std::string& BluetoothAllowedDevicesMap::AddDevice( |
| void BluetoothAllowedDevicesMap::RemoveDevice( |
| const url::Origin& origin, |
| const std::string& device_address) { |
| - const std::string device_id = GetDeviceId(origin, device_address); |
| - DCHECK(!device_id.empty()); |
| + const base::Optional<BluetoothDeviceId> device_id = |
| + GetDeviceId(origin, device_address); |
| + DCHECK(device_id); |
| // 1. Remove from all three maps. |
| CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); |
| - CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); |
| - CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); |
| + CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id.value())); |
| + CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id.value())); |
| // 2. Remove empty map for origin. |
| if (origin_to_device_address_to_id_map_[origin].empty()) { |
| @@ -91,29 +75,29 @@ void BluetoothAllowedDevicesMap::RemoveDevice( |
| } |
| // 3. Remove from set of ids. |
| - CHECK(device_id_set_.erase(device_id)); |
| + CHECK(device_id_set_.erase(device_id.value())); |
| } |
| -const std::string& BluetoothAllowedDevicesMap::GetDeviceId( |
| +base::Optional<BluetoothDeviceId> 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 base::EmptyString(); |
| + return base::nullopt; |
| } |
| 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 base::EmptyString(); |
| + return base::nullopt; |
| } |
| - return id_iter->second; |
| + return base::make_optional(id_iter->second); |
|
Jeffrey Yasskin
2016/06/03 17:22:04
It'd be nice to avoid needing a copy here. Have th
ortuno
2016/06/06 22:22:59
Done.
|
| } |
| const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
| const url::Origin& origin, |
| - const std::string& device_id) { |
| + const BluetoothDeviceId& 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(); |
| @@ -129,7 +113,7 @@ const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
| bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
| const url::Origin& origin, |
| - const std::string& device_id, |
| + const BluetoothDeviceId& device_id, |
| const BluetoothUUID& service_uuid) const { |
| if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { |
| return false; |
| @@ -149,11 +133,11 @@ bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
| : ContainsKey(id_iter->second, service_uuid); |
| } |
| -std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { |
| - std::string device_id = GetBase64Id(); |
| +BluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { |
| + BluetoothDeviceId device_id = BluetoothDeviceId::Create(); |
| while (ContainsKey(device_id_set_, device_id)) { |
| LOG(WARNING) << "Generated repeated id."; |
| - device_id = GetBase64Id(); |
| + device_id = BluetoothDeviceId::Create(); |
| } |
| return device_id; |
| } |