| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // See url::Origin for what constitutes a "unique" Origin and the | 53 // See url::Origin for what constitutes a "unique" Origin and the |
| 54 // Secure Contexts spec for what constitutes a Trusworthy Origin: | 54 // Secure Contexts spec for what constitutes a Trusworthy Origin: |
| 55 // https://w3c.github.io/webappsec-secure-contexts/ | 55 // https://w3c.github.io/webappsec-secure-contexts/ |
| 56 CHECK(!origin.unique()); | 56 CHECK(!origin.unique()); |
| 57 | 57 |
| 58 if (ContainsKey(origin_to_device_address_to_id_map_[origin], | 58 if (ContainsKey(origin_to_device_address_to_id_map_[origin], |
| 59 device_address)) { | 59 device_address)) { |
| 60 VLOG(1) << "Device already in map of allowed devices."; | 60 VLOG(1) << "Device already in map of allowed devices."; |
| 61 return origin_to_device_address_to_id_map_[origin][device_address]; | 61 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 62 } | 62 } |
| 63 const std::string device_id = GenerateDeviceId(origin); | 63 const std::string device_id = GenerateDeviceId(); |
| 64 VLOG(1) << "Id generated for device: " << device_id; | 64 VLOG(1) << "Id generated for device: " << device_id; |
| 65 | 65 |
| 66 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 66 origin_to_device_address_to_id_map_[origin][device_address] = device_id; |
| 67 origin_to_device_id_to_address_map_[origin][device_id] = device_address; | 67 origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
| 68 origin_to_device_id_to_services_map_[origin][device_id] = | 68 origin_to_device_id_to_services_map_[origin][device_id] = |
| 69 UnionOfServices(filters, optional_services); | 69 UnionOfServices(filters, optional_services); |
| 70 | 70 |
| 71 CHECK(device_id_set_.insert(device_id).second); |
| 72 |
| 71 return origin_to_device_address_to_id_map_[origin][device_address]; | 73 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 72 } | 74 } |
| 73 | 75 |
| 74 void BluetoothAllowedDevicesMap::RemoveDevice( | 76 void BluetoothAllowedDevicesMap::RemoveDevice( |
| 75 const url::Origin& origin, | 77 const url::Origin& origin, |
| 76 const std::string& device_address) { | 78 const std::string& device_address) { |
| 77 const std::string device_id = GetDeviceId(origin, device_address); | 79 const std::string device_id = GetDeviceId(origin, device_address); |
| 78 DCHECK(!device_id.empty()); | 80 DCHECK(!device_id.empty()); |
| 79 | 81 |
| 80 // 1. Remove from all three maps. | 82 // 1. Remove from all three maps. |
| 81 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | 83 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); |
| 82 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); | 84 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); |
| 83 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | 85 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); |
| 84 | 86 |
| 85 // 2. Remove empty map for origin. | 87 // 2. Remove empty map for origin. |
| 86 if (origin_to_device_address_to_id_map_[origin].empty()) { | 88 if (origin_to_device_address_to_id_map_[origin].empty()) { |
| 87 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | 89 CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
| 88 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | 90 CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
| 89 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | 91 CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
| 90 } | 92 } |
| 93 |
| 94 // 3. Remove from set of ids. |
| 95 CHECK(device_id_set_.erase(device_id)); |
| 91 } | 96 } |
| 92 | 97 |
| 93 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | 98 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( |
| 94 const url::Origin& origin, | 99 const url::Origin& origin, |
| 95 const std::string& device_address) { | 100 const std::string& device_address) { |
| 96 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 101 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); |
| 97 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { | 102 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { |
| 98 return base::EmptyString(); | 103 return base::EmptyString(); |
| 99 } | 104 } |
| 100 | 105 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 116 } | 121 } |
| 117 | 122 |
| 118 const auto& device_id_to_address_map = id_map_iter->second; | 123 const auto& device_id_to_address_map = id_map_iter->second; |
| 119 | 124 |
| 120 auto id_iter = device_id_to_address_map.find(device_id); | 125 auto id_iter = device_id_to_address_map.find(device_id); |
| 121 | 126 |
| 122 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 127 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
| 123 : id_iter->second; | 128 : id_iter->second; |
| 124 } | 129 } |
| 125 | 130 |
| 126 std::string BluetoothAllowedDevicesMap::GenerateDeviceId( | 131 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { |
| 127 const url::Origin& origin) { | |
| 128 std::string device_id = GetBase64Id(); | 132 std::string device_id = GetBase64Id(); |
| 129 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 133 while (ContainsKey(device_id_set_, device_id)) { |
| 130 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | |
| 131 return device_id; | |
| 132 } | |
| 133 while (ContainsKey(id_map_iter->second, device_id)) { | |
| 134 LOG(WARNING) << "Generated repeated id."; | 134 LOG(WARNING) << "Generated repeated id."; |
| 135 device_id = GetBase64Id(); | 135 device_id = GetBase64Id(); |
| 136 } | 136 } |
| 137 return device_id; | 137 return device_id; |
| 138 } | 138 } |
| 139 | 139 |
| 140 std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( | 140 std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( |
| 141 const std::vector<BluetoothScanFilter>& filters, | 141 const std::vector<BluetoothScanFilter>& filters, |
| 142 const std::vector<BluetoothUUID>& optional_services) { | 142 const std::vector<BluetoothUUID>& optional_services) { |
| 143 std::set<std::string> unionOfServices; | 143 std::set<std::string> unionOfServices; |
| 144 for (const auto& filter : filters) { | 144 for (const auto& filter : filters) { |
| 145 for (const BluetoothUUID& uuid : filter.services) { | 145 for (const BluetoothUUID& uuid : filter.services) { |
| 146 unionOfServices.insert(uuid.canonical_value()); | 146 unionOfServices.insert(uuid.canonical_value()); |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 for (const BluetoothUUID& uuid : optional_services) { | 149 for (const BluetoothUUID& uuid : optional_services) { |
| 150 unionOfServices.insert(uuid.canonical_value()); | 150 unionOfServices.insert(uuid.canonical_value()); |
| 151 } | 151 } |
| 152 return unionOfServices; | 152 return unionOfServices; |
| 153 } | 153 } |
| 154 | 154 |
| 155 } // namespace content | 155 } // namespace content |
| OLD | NEW |