Chromium Code Reviews| 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 <string> | |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/base64.h" | |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/optional.h" | 11 #include "base/optional.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 14 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
| 15 #include "crypto/random.h" | 15 #include "content/common/bluetooth/bluetooth_device_id.h" |
| 16 | 16 |
| 17 using device::BluetoothUUID; | 17 using device::BluetoothUUID; |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 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() {} | 21 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
| 39 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | 22 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
| 40 | 23 |
| 41 const std::string& BluetoothAllowedDevicesMap::AddDevice( | 24 const BluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( |
| 42 const url::Origin& origin, | 25 const url::Origin& origin, |
| 43 const std::string& device_address, | 26 const std::string& device_address, |
| 44 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 27 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| 45 VLOG(1) << "Adding a device to Map of Allowed Devices."; | 28 VLOG(1) << "Adding a device to Map of Allowed Devices."; |
| 46 | 29 |
| 47 // "Unique" Origins generate the same key in maps, therefore are not | 30 // "Unique" Origins generate the same key in maps, therefore are not |
| 48 // supported. | 31 // supported. |
| 49 CHECK(!origin.unique()); | 32 CHECK(!origin.unique()); |
| 50 | 33 |
| 51 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; | 34 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; |
| 52 auto id_iter = device_address_to_id_map.find(device_address); | 35 auto id_iter = device_address_to_id_map.find(device_address); |
| 53 if (id_iter != device_address_to_id_map.end()) { | 36 if (id_iter != device_address_to_id_map.end()) { |
| 54 VLOG(1) << "Device already in map of allowed devices."; | 37 VLOG(1) << "Device already in map of allowed devices."; |
| 55 const auto& device_id = id_iter->second; | 38 const auto& device_id = id_iter->second; |
| 56 | 39 |
| 57 AddUnionOfServicesTo( | 40 AddUnionOfServicesTo( |
| 58 options, &origin_to_device_id_to_services_map_[origin][device_id]); | 41 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
| 59 | 42 |
| 60 return origin_to_device_address_to_id_map_[origin][device_address]; | 43 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 61 } | 44 } |
| 62 const std::string device_id = GenerateDeviceId(); | 45 const BluetoothDeviceId device_id = GenerateUniqueDeviceId(); |
| 63 VLOG(1) << "Id generated for device: " << device_id; | 46 VLOG(1) << "Id generated for device: " << device_id.str(); |
|
Jeffrey Yasskin
2016/06/06 23:44:02
You can omit the .str() now.
ortuno
2016/06/24 17:39:43
Done.
| |
| 64 | 47 |
| 65 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 48 origin_to_device_address_to_id_map_[origin][device_address] = device_id; |
| 66 origin_to_device_id_to_address_map_[origin][device_id] = device_address; | 49 origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
| 67 AddUnionOfServicesTo( | 50 AddUnionOfServicesTo( |
| 68 options, &origin_to_device_id_to_services_map_[origin][device_id]); | 51 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
| 69 | 52 |
| 70 CHECK(device_id_set_.insert(device_id).second); | 53 CHECK(device_id_set_.insert(device_id).second); |
| 71 | 54 |
| 72 return origin_to_device_address_to_id_map_[origin][device_address]; | 55 return origin_to_device_address_to_id_map_[origin][device_address]; |
| 73 } | 56 } |
| 74 | 57 |
| 75 void BluetoothAllowedDevicesMap::RemoveDevice( | 58 void BluetoothAllowedDevicesMap::RemoveDevice( |
| 76 const url::Origin& origin, | 59 const url::Origin& origin, |
| 77 const std::string& device_address) { | 60 const std::string& device_address) { |
| 78 const std::string device_id = GetDeviceId(origin, device_address); | 61 const BluetoothDeviceId* device_id_ptr = GetDeviceId(origin, device_address); |
| 79 DCHECK(!device_id.empty()); | 62 DCHECK(device_id_ptr != nullptr); |
| 63 | |
| 64 // We make a copy because we are going to remove the original value from its | |
| 65 // map. | |
| 66 BluetoothDeviceId device_id = *device_id_ptr; | |
| 80 | 67 |
| 81 // 1. Remove from all three maps. | 68 // 1. Remove from all three maps. |
| 82 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | 69 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); |
| 83 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); | 70 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); |
| 84 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | 71 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); |
| 85 | 72 |
| 86 // 2. Remove empty map for origin. | 73 // 2. Remove empty map for origin. |
| 87 if (origin_to_device_address_to_id_map_[origin].empty()) { | 74 if (origin_to_device_address_to_id_map_[origin].empty()) { |
| 88 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | 75 CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
| 89 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | 76 CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
| 90 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | 77 CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
| 91 } | 78 } |
| 92 | 79 |
| 93 // 3. Remove from set of ids. | 80 // 3. Remove from set of ids. |
| 94 CHECK(device_id_set_.erase(device_id)); | 81 CHECK(device_id_set_.erase(device_id)); |
| 95 } | 82 } |
| 96 | 83 |
| 97 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | 84 const BluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( |
| 98 const url::Origin& origin, | 85 const url::Origin& origin, |
| 99 const std::string& device_address) { | 86 const std::string& device_address) { |
| 100 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 87 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); |
| 101 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { | 88 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { |
| 102 return base::EmptyString(); | 89 return nullptr; |
| 103 } | 90 } |
| 104 | 91 |
| 105 const auto& device_address_to_id_map = address_map_iter->second; | 92 const auto& device_address_to_id_map = address_map_iter->second; |
| 106 | 93 |
| 107 auto id_iter = device_address_to_id_map.find(device_address); | 94 auto id_iter = device_address_to_id_map.find(device_address); |
| 108 if (id_iter == device_address_to_id_map.end()) { | 95 if (id_iter == device_address_to_id_map.end()) { |
| 109 return base::EmptyString(); | 96 return nullptr; |
| 110 } | 97 } |
| 111 return id_iter->second; | 98 return &(id_iter->second); |
| 112 } | 99 } |
| 113 | 100 |
| 114 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | 101 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
| 115 const url::Origin& origin, | 102 const url::Origin& origin, |
| 116 const std::string& device_id) { | 103 const BluetoothDeviceId& device_id) { |
| 117 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 104 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); |
| 118 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | 105 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { |
| 119 return base::EmptyString(); | 106 return base::EmptyString(); |
| 120 } | 107 } |
| 121 | 108 |
| 122 const auto& device_id_to_address_map = id_map_iter->second; | 109 const auto& device_id_to_address_map = id_map_iter->second; |
| 123 | 110 |
| 124 auto id_iter = device_id_to_address_map.find(device_id); | 111 auto id_iter = device_id_to_address_map.find(device_id); |
| 125 | 112 |
| 126 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 113 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
| 127 : id_iter->second; | 114 : id_iter->second; |
| 128 } | 115 } |
| 129 | 116 |
| 130 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 117 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
| 131 const url::Origin& origin, | 118 const url::Origin& origin, |
| 132 const std::string& device_id, | 119 const BluetoothDeviceId& device_id, |
| 133 const BluetoothUUID& service_uuid) const { | 120 const BluetoothUUID& service_uuid) const { |
| 134 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { | 121 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { |
| 135 return false; | 122 return false; |
| 136 } | 123 } |
| 137 | 124 |
| 138 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 125 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); |
| 139 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | 126 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { |
| 140 return false; | 127 return false; |
| 141 } | 128 } |
| 142 | 129 |
| 143 const auto& device_id_to_services_map = id_map_iter->second; | 130 const auto& device_id_to_services_map = id_map_iter->second; |
| 144 | 131 |
| 145 auto id_iter = device_id_to_services_map.find(device_id); | 132 auto id_iter = device_id_to_services_map.find(device_id); |
| 146 | 133 |
| 147 return id_iter == device_id_to_services_map.end() | 134 return id_iter == device_id_to_services_map.end() |
| 148 ? false | 135 ? false |
| 149 : ContainsKey(id_iter->second, service_uuid); | 136 : ContainsKey(id_iter->second, service_uuid); |
| 150 } | 137 } |
| 151 | 138 |
| 152 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { | 139 BluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { |
| 153 std::string device_id = GetBase64Id(); | 140 BluetoothDeviceId device_id = BluetoothDeviceId::Create(); |
| 154 while (ContainsKey(device_id_set_, device_id)) { | 141 while (ContainsKey(device_id_set_, device_id)) { |
| 155 LOG(WARNING) << "Generated repeated id."; | 142 LOG(WARNING) << "Generated repeated id."; |
| 156 device_id = GetBase64Id(); | 143 device_id = BluetoothDeviceId::Create(); |
| 157 } | 144 } |
| 158 return device_id; | 145 return device_id; |
| 159 } | 146 } |
| 160 | 147 |
| 161 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 148 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
| 162 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 149 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
| 163 std::unordered_set<BluetoothUUID>* unionOfServices) { | 150 std::unordered_set<BluetoothUUID>* unionOfServices) { |
| 164 for (const auto& filter : options->filters) { | 151 for (const auto& filter : options->filters) { |
| 165 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { | 152 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { |
| 166 unionOfServices->insert(uuid.value()); | 153 unionOfServices->insert(uuid.value()); |
| 167 } | 154 } |
| 168 } | 155 } |
| 169 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { | 156 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { |
| 170 unionOfServices->insert(uuid.value()); | 157 unionOfServices->insert(uuid.value()); |
| 171 } | 158 } |
| 172 } | 159 } |
| 173 | 160 |
| 174 } // namespace content | 161 } // namespace content |
| OLD | NEW |