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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f734467555b8412e4327ad71e14f6dcd4bacbab9 |
| --- /dev/null |
| +++ b/content/browser/bluetooth/bluetooth_allowed_devices_map.cc |
| @@ -0,0 +1,145 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/base64.h" |
| +#include "base/logging.h" |
| +#include "base/stl_util.h" |
| +#include "base/strings/string_util.h" |
| +#include "content/common/bluetooth/bluetooth_scan_filter.h" |
| +#include "crypto/random.h" |
| +#include "device/bluetooth/bluetooth_uuid.h" |
| + |
| +using device::BluetoothUUID; |
| + |
| +namespace content { |
| + |
| +namespace { |
| +const std::string GetBase64Id() { |
| + std::string bytes(16 /* 128bits */, '\0'); |
| + |
| + crypto::RandBytes(base::WriteInto(&bytes /* str */, |
| + bytes.size() + 1 /* lenght_with_null */), |
|
Jeffrey Yasskin
2016/01/06 00:47:57
s/lenght/length/
ortuno
2016/01/13 01:41:43
Done.
|
| + bytes.size()); |
| + |
| + base::Base64Encode(bytes, &bytes); |
| + |
| + return bytes; |
| +} |
| +} // namespace |
| + |
| +BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
| +BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
| + |
| +const std::string& BluetoothAllowedDevicesMap::AddDevice( |
| + const std::string& origin, |
| + const std::string& device_address, |
| + const std::vector<BluetoothScanFilter>& filters, |
| + const std::vector<BluetoothUUID>& optional_services) { |
| + VLOG(1) << "Adding a device to Map of Allowed Devices."; |
| + |
| + if (ContainsKey(origin_to_device_address_to_id_map_[origin], |
| + device_address)) { |
| + VLOG(1) << "Device already in map of allowed devices."; |
| + return origin_to_device_address_to_id_map_[origin][device_address]; |
| + } |
| + const std::string device_id = GenerateDeviceId(origin); |
| + VLOG(1) << "Id generated for device: " << device_id; |
| + |
| + origin_to_device_address_to_id_map_[origin][device_address] = device_id; |
| + origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
| + origin_to_device_id_to_services_map_[origin][device_id] = |
| + UnionOfServices(filters, optional_services); |
| + |
| + return origin_to_device_address_to_id_map_[origin][device_address]; |
| +} |
| + |
| +void BluetoothAllowedDevicesMap::RemoveDevice( |
| + const std::string& origin, |
| + const std::string& device_address) { |
| + if (!HasDevicePermissionFromDeviceAddress(origin, device_address)) { |
| + return; |
| + } |
| + // 1. Remove from all three maps. |
| + std::string device_id = |
| + origin_to_device_address_to_id_map_[origin][device_address]; |
| + 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)); |
| + |
| + // 2. Remove empty map for origin. |
| + if (origin_to_device_address_to_id_map_[origin].empty()) { |
| + CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
| + CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
| + CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
| + } |
| +} |
| + |
| +bool BluetoothAllowedDevicesMap::HasDevicePermissionFromDeviceId( |
| + const std::string& origin, |
| + 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 false; |
| + } |
| + return ContainsKey(id_map_iter->second, device_id); |
| +} |
| + |
| +bool BluetoothAllowedDevicesMap::HasDevicePermissionFromDeviceAddress( |
| + const std::string& 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 false; |
| + } |
| + return ContainsKey(address_map_iter->second, device_address); |
| +} |
| + |
| +const std::string& BluetoothAllowedDevicesMap::GetDeviceId( |
| + const std::string& origin, |
| + const std::string& device_address) { |
| + CHECK(HasDevicePermissionFromDeviceAddress(origin, device_address)); |
| + return origin_to_device_address_to_id_map_[origin][device_address]; |
| +} |
| + |
| +const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
| + const std::string& origin, |
| + const std::string& device_id) { |
| + CHECK(HasDevicePermissionFromDeviceId(origin, device_id)); |
| + return origin_to_device_id_to_address_map_[origin][device_id]; |
| +} |
| + |
| +const std::string BluetoothAllowedDevicesMap::GenerateDeviceId( |
|
Jeffrey Yasskin
2016/01/06 00:47:56
Don't make by-value return types const: it prevent
ortuno
2016/01/13 01:41:43
Done. You mean it prevents the optimization in whi
Jeffrey Yasskin
2016/01/13 02:31:36
Yep.
|
| + const std::string& origin) { |
| + std::string device_id = GetBase64Id(); |
| + 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 device_id; |
| + } |
| + while (ContainsKey(id_map_iter->second, device_id)) { |
| + LOG(WARNING) << "Generated repeated id."; |
| + device_id = GetBase64Id(); |
| + } |
| + return GetBase64Id(); |
|
Jeffrey Yasskin
2016/01/06 00:47:56
Return device_id, right?
ortuno
2016/01/13 01:41:43
Done.
|
| +} |
| + |
| +const std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( |
|
Jeffrey Yasskin
2016/01/06 00:47:56
Un-const this too.
ortuno
2016/01/13 01:41:43
Done.
|
| + const std::vector<BluetoothScanFilter>& filters, |
| + const std::vector<BluetoothUUID>& optional_services) { |
| + std::set<std::string> unionOfServices; |
| + for (const BluetoothScanFilter& filter : filters) { |
| + for (const BluetoothUUID& uuid : filter.services) { |
| + unionOfServices.insert(uuid.canonical_value()); |
| + } |
| + } |
| + for (const BluetoothUUID& uuid : optional_services) { |
| + unionOfServices.insert(uuid.canonical_value()); |
| + } |
| + return unionOfServices; |
| +} |
| + |
| +} // namespace content |