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.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 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_blocklist.h" | 14 #include "content/browser/bluetooth/bluetooth_blocklist.h" |
| 15 #include "content/common/bluetooth/web_bluetooth_device_id.h" | 15 #include "content/common/bluetooth/web_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 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} | 21 BluetoothAllowedDevices::BluetoothAllowedDevices() {} |
| 22 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | 22 BluetoothAllowedDevices::BluetoothAllowedDevices( |
| 23 const BluetoothAllowedDevices& other) = default; | |
| 24 BluetoothAllowedDevices::~BluetoothAllowedDevices() {} | |
| 23 | 25 |
| 24 const WebBluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( | 26 const WebBluetoothDeviceId& BluetoothAllowedDevices::AddDevice( |
| 25 const url::Origin& origin, | |
| 26 const std::string& device_address, | 27 const std::string& device_address, |
| 27 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 28 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| 28 DVLOG(1) << "Adding a device to Map of Allowed Devices."; | 29 DVLOG(1) << "Adding a device to Map of Allowed Devices."; |
| 29 | 30 |
| 30 // "Unique" Origins generate the same key in maps, therefore are not | 31 auto id_iter = device_address_to_id_map_.find(device_address); |
| 31 // supported. | 32 if (id_iter != device_address_to_id_map_.end()) { |
| 32 CHECK(!origin.unique()); | |
| 33 | |
| 34 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; | |
| 35 auto id_iter = device_address_to_id_map.find(device_address); | |
| 36 if (id_iter != device_address_to_id_map.end()) { | |
| 37 DVLOG(1) << "Device already in map of allowed devices."; | 33 DVLOG(1) << "Device already in map of allowed devices."; |
| 38 const auto& device_id = id_iter->second; | 34 const auto& device_id = id_iter->second; |
| 39 | 35 |
| 40 AddUnionOfServicesTo( | 36 AddUnionOfServicesTo(options, &device_id_to_services_map_[device_id]); |
| 41 options, &origin_to_device_id_to_services_map_[origin][device_id]); | |
| 42 | 37 |
| 43 return origin_to_device_address_to_id_map_[origin][device_address]; | 38 return device_address_to_id_map_[device_address]; |
| 44 } | 39 } |
| 45 const WebBluetoothDeviceId device_id = GenerateUniqueDeviceId(); | 40 const WebBluetoothDeviceId device_id = GenerateUniqueDeviceId(); |
| 46 DVLOG(1) << "Id generated for device: " << device_id; | 41 DVLOG(1) << "Id generated for device: " << device_id; |
| 47 | 42 |
| 48 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 43 device_address_to_id_map_[device_address] = device_id; |
| 49 origin_to_device_id_to_address_map_[origin][device_id] = device_address; | 44 device_id_to_address_map_[device_id] = device_address; |
| 50 AddUnionOfServicesTo( | 45 AddUnionOfServicesTo(options, &device_id_to_services_map_[device_id]); |
| 51 options, &origin_to_device_id_to_services_map_[origin][device_id]); | |
| 52 | 46 |
| 53 CHECK(device_id_set_.insert(device_id).second); | 47 CHECK(device_id_set_.insert(device_id).second); |
| 54 | 48 |
| 55 return origin_to_device_address_to_id_map_[origin][device_address]; | 49 return device_address_to_id_map_[device_address]; |
| 56 } | 50 } |
| 57 | 51 |
| 58 void BluetoothAllowedDevicesMap::RemoveDevice( | 52 void BluetoothAllowedDevices::RemoveDevice(const std::string& device_address) { |
| 59 const url::Origin& origin, | 53 const WebBluetoothDeviceId* device_id_ptr = GetDeviceId(device_address); |
| 60 const std::string& device_address) { | |
| 61 const WebBluetoothDeviceId* device_id_ptr = | |
| 62 GetDeviceId(origin, device_address); | |
| 63 DCHECK(device_id_ptr != nullptr); | 54 DCHECK(device_id_ptr != nullptr); |
| 64 | 55 |
| 65 // We make a copy because we are going to remove the original value from its | 56 // We make a copy because we are going to remove the original value from its |
| 66 // map. | 57 // map. |
| 67 WebBluetoothDeviceId device_id = *device_id_ptr; | 58 WebBluetoothDeviceId device_id = *device_id_ptr; |
| 68 | 59 |
| 69 // 1. Remove from all three maps. | 60 // 1. Remove from all three maps. |
| 70 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | 61 CHECK(device_address_to_id_map_.erase(device_address)); |
| 71 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); | 62 CHECK(device_id_to_address_map_.erase(device_id)); |
| 72 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | 63 CHECK(device_id_to_services_map_.erase(device_id)); |
| 73 | 64 |
| 74 // 2. Remove empty map for origin. | 65 // 2. Remove from set of ids. |
| 75 if (origin_to_device_address_to_id_map_[origin].empty()) { | |
| 76 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | |
| 77 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | |
| 78 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | |
| 79 } | |
| 80 | |
| 81 // 3. Remove from set of ids. | |
| 82 CHECK(device_id_set_.erase(device_id)); | 66 CHECK(device_id_set_.erase(device_id)); |
| 83 } | 67 } |
| 84 | 68 |
| 85 const WebBluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( | 69 const WebBluetoothDeviceId* BluetoothAllowedDevices::GetDeviceId( |
| 86 const url::Origin& origin, | |
| 87 const std::string& device_address) { | 70 const std::string& device_address) { |
| 88 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 71 auto id_iter = device_address_to_id_map_.find(device_address); |
| 89 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { | 72 if (id_iter == device_address_to_id_map_.end()) { |
| 90 return nullptr; | |
| 91 } | |
| 92 | |
| 93 const auto& device_address_to_id_map = address_map_iter->second; | |
| 94 | |
| 95 auto id_iter = device_address_to_id_map.find(device_address); | |
| 96 if (id_iter == device_address_to_id_map.end()) { | |
| 97 return nullptr; | 73 return nullptr; |
| 98 } | 74 } |
|
Reilly Grant (use Gerrit)
2017/01/31 23:47:37
nit: no braces around single-line if
juncai
2017/02/03 04:47:55
In the //content/browser/bluetooth directory, the
| |
| 99 return &(id_iter->second); | 75 return &(id_iter->second); |
| 100 } | 76 } |
| 101 | 77 |
| 102 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | 78 const std::string& BluetoothAllowedDevices::GetDeviceAddress( |
| 103 const url::Origin& origin, | |
| 104 const WebBluetoothDeviceId& device_id) { | 79 const WebBluetoothDeviceId& device_id) { |
| 105 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 80 auto id_iter = device_id_to_address_map_.find(device_id); |
| 106 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | |
| 107 return base::EmptyString(); | |
| 108 } | |
| 109 | 81 |
| 110 const auto& device_id_to_address_map = id_map_iter->second; | 82 return id_iter == device_id_to_address_map_.end() ? base::EmptyString() |
| 111 | 83 : id_iter->second; |
| 112 auto id_iter = device_id_to_address_map.find(device_id); | |
| 113 | |
| 114 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | |
| 115 : id_iter->second; | |
| 116 } | 84 } |
| 117 | 85 |
| 118 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessAtLeastOneService( | 86 bool BluetoothAllowedDevices::IsAllowedToAccessAtLeastOneService( |
| 119 const url::Origin& origin, | |
| 120 const WebBluetoothDeviceId& device_id) const { | 87 const WebBluetoothDeviceId& device_id) const { |
| 121 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 88 auto id_iter = device_id_to_services_map_.find(device_id); |
| 122 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | |
| 123 return false; | |
| 124 } | |
| 125 | 89 |
| 126 const auto& device_id_to_services_map = id_map_iter->second; | 90 return id_iter == device_id_to_services_map_.end() ? false |
| 127 | 91 : !id_iter->second.empty(); |
| 128 auto id_iter = device_id_to_services_map.find(device_id); | |
| 129 | |
| 130 return id_iter == device_id_to_services_map.end() ? false | |
| 131 : !id_iter->second.empty(); | |
| 132 } | 92 } |
| 133 | 93 |
| 134 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 94 bool BluetoothAllowedDevices::IsAllowedToAccessService( |
| 135 const url::Origin& origin, | |
| 136 const WebBluetoothDeviceId& device_id, | 95 const WebBluetoothDeviceId& device_id, |
| 137 const BluetoothUUID& service_uuid) const { | 96 const BluetoothUUID& service_uuid) const { |
| 138 if (BluetoothBlocklist::Get().IsExcluded(service_uuid)) { | 97 if (BluetoothBlocklist::Get().IsExcluded(service_uuid)) { |
| 139 return false; | 98 return false; |
| 140 } | 99 } |
| 141 | 100 |
| 142 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 101 auto id_iter = device_id_to_services_map_.find(device_id); |
| 143 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | |
| 144 return false; | |
| 145 } | |
| 146 | 102 |
| 147 const auto& device_id_to_services_map = id_map_iter->second; | 103 return id_iter == device_id_to_services_map_.end() |
| 148 | |
| 149 auto id_iter = device_id_to_services_map.find(device_id); | |
| 150 | |
| 151 return id_iter == device_id_to_services_map.end() | |
| 152 ? false | 104 ? false |
| 153 : base::ContainsKey(id_iter->second, service_uuid); | 105 : base::ContainsKey(id_iter->second, service_uuid); |
| 154 } | 106 } |
| 155 | 107 |
| 156 WebBluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { | 108 WebBluetoothDeviceId BluetoothAllowedDevices::GenerateUniqueDeviceId() { |
| 157 WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create(); | 109 WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create(); |
| 158 while (base::ContainsKey(device_id_set_, device_id)) { | 110 while (base::ContainsKey(device_id_set_, device_id)) { |
| 159 LOG(WARNING) << "Generated repeated id."; | 111 LOG(WARNING) << "Generated repeated id."; |
| 160 device_id = WebBluetoothDeviceId::Create(); | 112 device_id = WebBluetoothDeviceId::Create(); |
| 161 } | 113 } |
| 162 return device_id; | 114 return device_id; |
| 163 } | 115 } |
| 164 | 116 |
| 165 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 117 void BluetoothAllowedDevices::AddUnionOfServicesTo( |
| 166 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 118 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
| 167 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* | 119 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* |
| 168 unionOfServices) { | 120 unionOfServices) { |
| 169 if (options->filters) { | 121 if (options->filters) { |
| 170 for (const auto& filter : options->filters.value()) { | 122 for (const auto& filter : options->filters.value()) { |
| 171 if (!filter->services) { | 123 if (!filter->services) { |
| 172 continue; | 124 continue; |
| 173 } | 125 } |
| 174 | 126 |
| 175 for (const BluetoothUUID& uuid : filter->services.value()) { | 127 for (const BluetoothUUID& uuid : filter->services.value()) { |
| 176 unionOfServices->insert(uuid); | 128 unionOfServices->insert(uuid); |
| 177 } | 129 } |
| 178 } | 130 } |
| 179 } | 131 } |
| 180 | 132 |
| 181 for (const BluetoothUUID& uuid : options->optional_services) { | 133 for (const BluetoothUUID& uuid : options->optional_services) { |
| 182 unionOfServices->insert(uuid); | 134 unionOfServices->insert(uuid); |
| 183 } | 135 } |
| 184 } | 136 } |
| 185 | 137 |
| 186 } // namespace content | 138 } // namespace content |
| OLD | NEW |