| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // "Unique" Origins generate the same key in maps. The set of "unique" | 48 // "Unique" Origins generate the same key in maps. The set of "unique" |
| 49 // Origins that generate the same key does not intersect the set of | 49 // Origins that generate the same key does not intersect the set of |
| 50 // potentially trustworthy origins; since Bluetooth is only available for | 50 // potentially trustworthy origins; since Bluetooth is only available for |
| 51 // potntially trustworthy origins we should never receive a request from a | 51 // potntially trustworthy origins we should never receive a request from a |
| 52 // "unique" Origin. | 52 // "unique" Origin. |
| 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 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; |
| 59 device_address)) { | 59 auto id_iter = device_address_to_id_map.find(device_address); |
| 60 if (id_iter != device_address_to_id_map.end()) { |
| 60 VLOG(1) << "Device already in map of allowed devices."; | 61 VLOG(1) << "Device already in map of allowed devices."; |
| 62 const auto& device_id = id_iter->second; |
| 63 |
| 64 AddUnionOfServicesTo( |
| 65 filters, optional_services, |
| 66 &origin_to_device_id_to_services_map_[origin][device_id]); |
| 67 |
| 61 return origin_to_device_address_to_id_map_[origin][device_address]; | 68 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 62 } | 69 } |
| 63 const std::string device_id = GenerateDeviceId(); | 70 const std::string device_id = GenerateDeviceId(); |
| 64 VLOG(1) << "Id generated for device: " << device_id; | 71 VLOG(1) << "Id generated for device: " << device_id; |
| 65 | 72 |
| 66 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 73 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; | 74 origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
| 68 origin_to_device_id_to_services_map_[origin][device_id] = | 75 AddUnionOfServicesTo( |
| 69 UnionOfServices(filters, optional_services); | 76 filters, optional_services, |
| 77 &origin_to_device_id_to_services_map_[origin][device_id]); |
| 70 | 78 |
| 71 CHECK(device_id_set_.insert(device_id).second); | 79 CHECK(device_id_set_.insert(device_id).second); |
| 72 | 80 |
| 73 return origin_to_device_address_to_id_map_[origin][device_address]; | 81 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 74 } | 82 } |
| 75 | 83 |
| 76 void BluetoothAllowedDevicesMap::RemoveDevice( | 84 void BluetoothAllowedDevicesMap::RemoveDevice( |
| 77 const url::Origin& origin, | 85 const url::Origin& origin, |
| 78 const std::string& device_address) { | 86 const std::string& device_address) { |
| 79 const std::string device_id = GetDeviceId(origin, device_address); | 87 const std::string device_id = GetDeviceId(origin, device_address); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 156 |
| 149 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { | 157 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { |
| 150 std::string device_id = GetBase64Id(); | 158 std::string device_id = GetBase64Id(); |
| 151 while (ContainsKey(device_id_set_, device_id)) { | 159 while (ContainsKey(device_id_set_, device_id)) { |
| 152 LOG(WARNING) << "Generated repeated id."; | 160 LOG(WARNING) << "Generated repeated id."; |
| 153 device_id = GetBase64Id(); | 161 device_id = GetBase64Id(); |
| 154 } | 162 } |
| 155 return device_id; | 163 return device_id; |
| 156 } | 164 } |
| 157 | 165 |
| 158 std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( | 166 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
| 159 const std::vector<BluetoothScanFilter>& filters, | 167 const std::vector<BluetoothScanFilter>& filters, |
| 160 const std::vector<BluetoothUUID>& optional_services) { | 168 const std::vector<device::BluetoothUUID>& optional_services, |
| 161 std::set<std::string> unionOfServices; | 169 std::set<std::string>* unionOfServices) { |
| 162 for (const auto& filter : filters) { | 170 for (const auto& filter : filters) { |
| 163 for (const BluetoothUUID& uuid : filter.services) { | 171 for (const BluetoothUUID& uuid : filter.services) { |
| 164 unionOfServices.insert(uuid.canonical_value()); | 172 unionOfServices->insert(uuid.canonical_value()); |
| 165 } | 173 } |
| 166 } | 174 } |
| 167 for (const BluetoothUUID& uuid : optional_services) { | 175 for (const BluetoothUUID& uuid : optional_services) { |
| 168 unionOfServices.insert(uuid.canonical_value()); | 176 unionOfServices->insert(uuid.canonical_value()); |
| 169 } | 177 } |
| 170 return unionOfServices; | |
| 171 } | 178 } |
| 172 | 179 |
| 173 } // namespace content | 180 } // namespace content |
| OLD | NEW |