| 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" |
| 11 #include "base/optional.h" |
| 11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 13 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 14 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
| 14 #include "crypto/random.h" | 15 #include "crypto/random.h" |
| 15 | 16 |
| 16 using device::BluetoothUUID; | 17 using device::BluetoothUUID; |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 123 |
| 123 auto id_iter = device_id_to_address_map.find(device_id); | 124 auto id_iter = device_id_to_address_map.find(device_id); |
| 124 | 125 |
| 125 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 126 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
| 126 : id_iter->second; | 127 : id_iter->second; |
| 127 } | 128 } |
| 128 | 129 |
| 129 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 130 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
| 130 const url::Origin& origin, | 131 const url::Origin& origin, |
| 131 const std::string& device_id, | 132 const std::string& device_id, |
| 132 const std::string& service_uuid) const { | 133 const BluetoothUUID& service_uuid) const { |
| 133 if (BluetoothBlacklist::Get().IsExcluded(BluetoothUUID(service_uuid))) { | 134 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { |
| 134 return false; | 135 return false; |
| 135 } | 136 } |
| 136 | 137 |
| 137 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 138 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); |
| 138 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | 139 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { |
| 139 return false; | 140 return false; |
| 140 } | 141 } |
| 141 | 142 |
| 142 const auto& device_id_to_services_map = id_map_iter->second; | 143 const auto& device_id_to_services_map = id_map_iter->second; |
| 143 | 144 |
| 144 auto id_iter = device_id_to_services_map.find(device_id); | 145 auto id_iter = device_id_to_services_map.find(device_id); |
| 145 | 146 |
| 146 return id_iter == device_id_to_services_map.end() | 147 return id_iter == device_id_to_services_map.end() |
| 147 ? false | 148 ? false |
| 148 : ContainsKey(id_iter->second, service_uuid); | 149 : ContainsKey(id_iter->second, service_uuid); |
| 149 } | 150 } |
| 150 | 151 |
| 151 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { | 152 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { |
| 152 std::string device_id = GetBase64Id(); | 153 std::string device_id = GetBase64Id(); |
| 153 while (ContainsKey(device_id_set_, device_id)) { | 154 while (ContainsKey(device_id_set_, device_id)) { |
| 154 LOG(WARNING) << "Generated repeated id."; | 155 LOG(WARNING) << "Generated repeated id."; |
| 155 device_id = GetBase64Id(); | 156 device_id = GetBase64Id(); |
| 156 } | 157 } |
| 157 return device_id; | 158 return device_id; |
| 158 } | 159 } |
| 159 | 160 |
| 160 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 161 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
| 161 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 162 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
| 162 std::set<std::string>* unionOfServices) { | 163 std::unordered_set<BluetoothUUID>* unionOfServices) { |
| 163 for (const auto& filter : options->filters) { | 164 for (const auto& filter : options->filters) { |
| 164 for (const std::string& uuid : filter->services) { | 165 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { |
| 165 unionOfServices->insert(uuid); | 166 unionOfServices->insert(uuid.value()); |
| 166 } | 167 } |
| 167 } | 168 } |
| 168 for (const std::string& uuid : options->optional_services) { | 169 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { |
| 169 unionOfServices->insert(uuid); | 170 unionOfServices->insert(uuid.value()); |
| 170 } | 171 } |
| 171 } | 172 } |
| 172 | 173 |
| 173 } // namespace content | 174 } // namespace content |
| OLD | NEW |