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 <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_blacklist.h" | 14 #include "content/browser/bluetooth/bluetooth_blacklist.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 namespace { |
| 22 |
| 23 std::ostream& operator<<(std::ostream& out, |
| 24 const WebBluetoothDeviceId& device_id) { |
| 25 return out << device_id.str(); |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
21 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} | 30 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
22 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | 31 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
23 | 32 |
24 const WebBluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( | 33 const WebBluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( |
25 const url::Origin& origin, | 34 const url::Origin& origin, |
26 const std::string& device_address, | 35 const std::string& device_address, |
27 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 36 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
28 VLOG(1) << "Adding a device to Map of Allowed Devices."; | 37 VLOG(1) << "Adding a device to Map of Allowed Devices."; |
29 | 38 |
30 // "Unique" Origins generate the same key in maps, therefore are not | 39 // "Unique" Origins generate the same key in maps, therefore are not |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 88 } |
80 | 89 |
81 // 3. Remove from set of ids. | 90 // 3. Remove from set of ids. |
82 CHECK(device_id_set_.erase(device_id)); | 91 CHECK(device_id_set_.erase(device_id)); |
83 } | 92 } |
84 | 93 |
85 const WebBluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( | 94 const WebBluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( |
86 const url::Origin& origin, | 95 const url::Origin& origin, |
87 const std::string& device_address) { | 96 const std::string& device_address) { |
88 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 97 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); |
89 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { | 98 if (address_map_iter == origin_to_device_address_to_id_map_.end()) |
90 return nullptr; | 99 return nullptr; |
91 } | |
92 | 100 |
93 const auto& device_address_to_id_map = address_map_iter->second; | 101 const auto& device_address_to_id_map = address_map_iter->second; |
94 | 102 |
95 auto id_iter = device_address_to_id_map.find(device_address); | 103 auto id_iter = device_address_to_id_map.find(device_address); |
96 if (id_iter == device_address_to_id_map.end()) { | 104 if (id_iter == device_address_to_id_map.end()) |
97 return nullptr; | 105 return nullptr; |
98 } | 106 |
99 return &(id_iter->second); | 107 return &(id_iter->second); |
100 } | 108 } |
101 | 109 |
102 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | 110 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
103 const url::Origin& origin, | 111 const url::Origin& origin, |
104 const WebBluetoothDeviceId& device_id) { | 112 const WebBluetoothDeviceId& device_id) { |
105 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 113 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); |
106 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | 114 if (id_map_iter == origin_to_device_id_to_address_map_.end()) |
107 return base::EmptyString(); | 115 return base::EmptyString(); |
108 } | |
109 | 116 |
110 const auto& device_id_to_address_map = id_map_iter->second; | 117 const auto& device_id_to_address_map = id_map_iter->second; |
111 | 118 |
112 auto id_iter = device_id_to_address_map.find(device_id); | 119 auto id_iter = device_id_to_address_map.find(device_id); |
113 | 120 |
114 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 121 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
115 : id_iter->second; | 122 : id_iter->second; |
116 } | 123 } |
117 | 124 |
118 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessAtLeastOneService( | 125 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessAtLeastOneService( |
119 const url::Origin& origin, | 126 const url::Origin& origin, |
120 const WebBluetoothDeviceId& device_id) const { | 127 const WebBluetoothDeviceId& device_id) const { |
121 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 128 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); |
122 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | 129 if (id_map_iter == origin_to_device_id_to_services_map_.end()) |
123 return false; | 130 return false; |
124 } | |
125 | 131 |
126 const auto& device_id_to_services_map = id_map_iter->second; | 132 const auto& device_id_to_services_map = id_map_iter->second; |
127 | 133 |
128 auto id_iter = device_id_to_services_map.find(device_id); | 134 auto id_iter = device_id_to_services_map.find(device_id); |
129 | 135 |
130 return id_iter == device_id_to_services_map.end() ? false | 136 return id_iter == device_id_to_services_map.end() ? false |
131 : !id_iter->second.empty(); | 137 : !id_iter->second.empty(); |
132 } | 138 } |
133 | 139 |
134 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 140 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
135 const url::Origin& origin, | 141 const url::Origin& origin, |
136 const WebBluetoothDeviceId& device_id, | 142 const WebBluetoothDeviceId& device_id, |
137 const BluetoothUUID& service_uuid) const { | 143 const BluetoothUUID& service_uuid) const { |
138 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { | 144 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) |
139 return false; | 145 return false; |
140 } | |
141 | 146 |
142 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 147 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); |
143 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | 148 if (id_map_iter == origin_to_device_id_to_services_map_.end()) |
144 return false; | 149 return false; |
145 } | |
146 | 150 |
147 const auto& device_id_to_services_map = id_map_iter->second; | 151 const auto& device_id_to_services_map = id_map_iter->second; |
148 | 152 |
149 auto id_iter = device_id_to_services_map.find(device_id); | 153 auto id_iter = device_id_to_services_map.find(device_id); |
150 | 154 |
151 return id_iter == device_id_to_services_map.end() | 155 return id_iter == device_id_to_services_map.end() |
152 ? false | 156 ? false |
153 : base::ContainsKey(id_iter->second, service_uuid); | 157 : base::ContainsKey(id_iter->second, service_uuid); |
154 } | 158 } |
155 | 159 |
156 WebBluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { | 160 WebBluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { |
157 WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create(); | 161 WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create(); |
158 while (base::ContainsKey(device_id_set_, device_id)) { | 162 while (base::ContainsKey(device_id_set_, device_id)) { |
159 LOG(WARNING) << "Generated repeated id."; | 163 LOG(WARNING) << "Generated repeated id."; |
160 device_id = WebBluetoothDeviceId::Create(); | 164 device_id = WebBluetoothDeviceId::Create(); |
161 } | 165 } |
162 return device_id; | 166 return device_id; |
163 } | 167 } |
164 | 168 |
165 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 169 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
166 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 170 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
167 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* | 171 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* |
168 unionOfServices) { | 172 unionOfServices) { |
169 for (const auto& filter : options->filters) { | 173 for (const auto& filter : options->filters) { |
170 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { | 174 if (!filter->services) |
171 unionOfServices->insert(uuid.value()); | 175 continue; |
172 } | 176 |
| 177 for (const BluetoothUUID& uuid : filter->services.value()) |
| 178 unionOfServices->insert(uuid); |
173 } | 179 } |
174 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { | 180 for (const BluetoothUUID& uuid : options->optional_services) |
175 unionOfServices->insert(uuid.value()); | 181 unionOfServices->insert(uuid); |
176 } | |
177 } | 182 } |
178 | 183 |
179 } // namespace content | 184 } // namespace content |
OLD | NEW |