Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/base64.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "content/common/bluetooth/bluetooth_scan_filter.h" | |
| 14 #include "crypto/random.h" | |
| 15 #include "device/bluetooth/bluetooth_uuid.h" | |
| 16 | |
| 17 using device::BluetoothUUID; | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 namespace { | |
| 22 std::string GetBase64Id() { | |
| 23 std::string bytes(16 /* 128bits */, '\0'); | |
| 24 | |
| 25 crypto::RandBytes(base::WriteInto(&bytes /* str */, | |
| 26 bytes.size() + 1 /* length_with_null */), | |
| 27 bytes.size()); | |
| 28 | |
| 29 base::Base64Encode(bytes, &bytes); | |
| 30 | |
| 31 return bytes; | |
| 32 } | |
| 33 } // namespace | |
| 34 | |
| 35 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} | |
| 36 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | |
| 37 | |
| 38 const std::string& BluetoothAllowedDevicesMap::AddDevice( | |
| 39 const std::string& origin, | |
| 40 const std::string& device_address, | |
| 41 const std::vector<BluetoothScanFilter>& filters, | |
| 42 const std::vector<BluetoothUUID>& optional_services) { | |
| 43 VLOG(1) << "Adding a device to Map of Allowed Devices."; | |
| 44 | |
| 45 if (ContainsKey(origin_to_device_address_to_id_map_[origin], | |
| 46 device_address)) { | |
| 47 VLOG(1) << "Device already in map of allowed devices."; | |
| 48 return origin_to_device_address_to_id_map_[origin][device_address]; | |
| 49 } | |
| 50 std::string device_id = GenerateDeviceId(origin); | |
|
Jeffrey Yasskin
2016/01/13 02:31:36
This can still be const, since it's never moved fr
ortuno
2016/01/13 21:34:31
Done.
| |
| 51 VLOG(1) << "Id generated for device: " << device_id; | |
| 52 | |
| 53 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | |
| 54 origin_to_device_id_to_address_map_[origin][device_id] = device_address; | |
| 55 origin_to_device_id_to_services_map_[origin][device_id] = | |
| 56 UnionOfServices(filters, optional_services); | |
| 57 | |
| 58 return origin_to_device_address_to_id_map_[origin][device_address]; | |
| 59 } | |
| 60 | |
| 61 void BluetoothAllowedDevicesMap::RemoveDevice( | |
| 62 const std::string& origin, | |
| 63 const std::string& device_address) { | |
| 64 if (GetDeviceId(origin, device_address).empty()) { | |
| 65 return; | |
| 66 } | |
| 67 // 1. Remove from all three maps. | |
| 68 std::string device_id = | |
|
Jeffrey Yasskin
2016/01/13 02:31:36
Assign this from GetDeviceId:
const std::string d
ortuno
2016/01/13 21:34:30
Done.
| |
| 69 origin_to_device_address_to_id_map_[origin][device_address]; | |
| 70 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)); | |
| 72 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | |
| 73 | |
| 74 // 2. Remove empty map for origin. | |
| 75 if (origin_to_device_address_to_id_map_[origin].empty()) { | |
| 76 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | |
| 77 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | |
| 78 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | |
| 83 const std::string& origin, | |
| 84 const std::string& device_address) { | |
| 85 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | |
| 86 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { | |
| 87 return base::EmptyString(); | |
| 88 } | |
| 89 | |
| 90 const DeviceAddressToIdMap& device_address_to_id_map = | |
| 91 address_map_iter->second; | |
| 92 | |
| 93 auto id_iter = device_address_to_id_map.find(device_address); | |
| 94 if (id_iter == device_address_to_id_map.end()) { | |
| 95 return base::EmptyString(); | |
| 96 } | |
| 97 return id_iter->second; | |
| 98 } | |
| 99 | |
| 100 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | |
| 101 const std::string& origin, | |
| 102 const std::string& device_id) { | |
| 103 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | |
| 104 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | |
| 105 return base::EmptyString(); | |
| 106 } | |
| 107 | |
| 108 const DeviceIdToAddressMap& device_id_to_address_map = id_map_iter->second; | |
| 109 | |
| 110 auto id_iter = device_id_to_address_map.find(device_id); | |
| 111 if (id_iter == device_id_to_address_map.end()) { | |
| 112 return base::EmptyString(); | |
| 113 } | |
| 114 return id_iter->second; | |
| 115 } | |
| 116 | |
| 117 std::string BluetoothAllowedDevicesMap::GenerateDeviceId( | |
| 118 const std::string& origin) { | |
| 119 std::string device_id = GetBase64Id(); | |
| 120 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | |
| 121 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | |
| 122 return device_id; | |
| 123 } | |
| 124 while (ContainsKey(id_map_iter->second, device_id)) { | |
| 125 LOG(WARNING) << "Generated repeated id."; | |
| 126 device_id = GetBase64Id(); | |
| 127 } | |
| 128 return device_id; | |
| 129 } | |
| 130 | |
| 131 std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( | |
| 132 const std::vector<BluetoothScanFilter>& filters, | |
| 133 const std::vector<BluetoothUUID>& optional_services) { | |
| 134 std::set<std::string> unionOfServices; | |
| 135 for (const BluetoothScanFilter& filter : filters) { | |
| 136 for (const BluetoothUUID& uuid : filter.services) { | |
| 137 unionOfServices.insert(uuid.canonical_value()); | |
| 138 } | |
| 139 } | |
| 140 for (const BluetoothUUID& uuid : optional_services) { | |
| 141 unionOfServices.insert(uuid.canonical_value()); | |
| 142 } | |
| 143 return unionOfServices; | |
| 144 } | |
| 145 | |
| 146 } // namespace content | |
| OLD | NEW |