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 const size_t kIdLength = 16 /* 128bits */; |
| 23 |
| 24 std::string GetBase64Id() { |
| 25 std::string bytes( |
| 26 kIdLength + 1 /* to avoid bytes being reallocated by WriteInto */, '\0'); |
| 27 |
| 28 crypto::RandBytes( |
| 29 base::WriteInto(&bytes /* str */, kIdLength + 1 /* length_with_null */), |
| 30 kIdLength); |
| 31 |
| 32 base::Base64Encode(bytes, &bytes); |
| 33 |
| 34 return bytes; |
| 35 } |
| 36 } // namespace |
| 37 |
| 38 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
| 39 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
| 40 |
| 41 const std::string& BluetoothAllowedDevicesMap::AddDevice( |
| 42 const url::Origin& origin, |
| 43 const std::string& device_address, |
| 44 const std::vector<BluetoothScanFilter>& filters, |
| 45 const std::vector<BluetoothUUID>& optional_services) { |
| 46 VLOG(1) << "Adding a device to Map of Allowed Devices."; |
| 47 |
| 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 |
| 50 // potentially trustworthy origins; since Bluetooth is only available for |
| 51 // potntially trustworthy origins we should never receive a request from a |
| 52 // "unique" Origin. |
| 53 // See url::Origin for what constitutes a "unique" Origin and the |
| 54 // Secure Contexts spec for what constitutes a Trusworthy Origin: |
| 55 // https://w3c.github.io/webappsec-secure-contexts/ |
| 56 CHECK(!origin.unique()); |
| 57 |
| 58 if (ContainsKey(origin_to_device_address_to_id_map_[origin], |
| 59 device_address)) { |
| 60 VLOG(1) << "Device already in map of allowed devices."; |
| 61 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 62 } |
| 63 const std::string device_id = GenerateDeviceId(origin); |
| 64 VLOG(1) << "Id generated for device: " << device_id; |
| 65 |
| 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; |
| 68 origin_to_device_id_to_services_map_[origin][device_id] = |
| 69 UnionOfServices(filters, optional_services); |
| 70 |
| 71 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 72 } |
| 73 |
| 74 void BluetoothAllowedDevicesMap::RemoveDevice( |
| 75 const url::Origin& origin, |
| 76 const std::string& device_address) { |
| 77 const std::string device_id = GetDeviceId(origin, device_address); |
| 78 DCHECK(!device_id.empty()); |
| 79 |
| 80 // 1. Remove from all three maps. |
| 81 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)); |
| 83 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); |
| 84 |
| 85 // 2. Remove empty map for origin. |
| 86 if (origin_to_device_address_to_id_map_[origin].empty()) { |
| 87 CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
| 88 CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
| 89 CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
| 90 } |
| 91 } |
| 92 |
| 93 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( |
| 94 const url::Origin& origin, |
| 95 const std::string& device_address) { |
| 96 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()) { |
| 98 return base::EmptyString(); |
| 99 } |
| 100 |
| 101 const auto& device_address_to_id_map = address_map_iter->second; |
| 102 |
| 103 auto id_iter = device_address_to_id_map.find(device_address); |
| 104 if (id_iter == device_address_to_id_map.end()) { |
| 105 return base::EmptyString(); |
| 106 } |
| 107 return id_iter->second; |
| 108 } |
| 109 |
| 110 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
| 111 const url::Origin& origin, |
| 112 const std::string& device_id) { |
| 113 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); |
| 114 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { |
| 115 return base::EmptyString(); |
| 116 } |
| 117 |
| 118 const auto& device_id_to_address_map = id_map_iter->second; |
| 119 |
| 120 auto id_iter = device_id_to_address_map.find(device_id); |
| 121 |
| 122 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
| 123 : id_iter->second; |
| 124 } |
| 125 |
| 126 std::string BluetoothAllowedDevicesMap::GenerateDeviceId( |
| 127 const url::Origin& origin) { |
| 128 std::string device_id = GetBase64Id(); |
| 129 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); |
| 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."; |
| 135 device_id = GetBase64Id(); |
| 136 } |
| 137 return device_id; |
| 138 } |
| 139 |
| 140 std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( |
| 141 const std::vector<BluetoothScanFilter>& filters, |
| 142 const std::vector<BluetoothUUID>& optional_services) { |
| 143 std::set<std::string> unionOfServices; |
| 144 for (const auto& filter : filters) { |
| 145 for (const BluetoothUUID& uuid : filter.services) { |
| 146 unionOfServices.insert(uuid.canonical_value()); |
| 147 } |
| 148 } |
| 149 for (const BluetoothUUID& uuid : optional_services) { |
| 150 unionOfServices.insert(uuid.canonical_value()); |
| 151 } |
| 152 return unionOfServices; |
| 153 } |
| 154 |
| 155 } // namespace content |
OLD | NEW |