| 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 "device/usb/webusb_descriptors.h" | 5 #include "device/usb/webusb_descriptors.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 configuration->functions.push_back(function); | 150 configuration->functions.push_back(function); |
| 151 } | 151 } |
| 152 | 152 |
| 153 return true; | 153 return true; |
| 154 } | 154 } |
| 155 | 155 |
| 156 void OnDoneReadingUrls(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins, | 156 void OnDoneReadingUrls(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins, |
| 157 uint8_t landing_page_id, | 157 uint8_t landing_page_id, |
| 158 std::unique_ptr<std::map<uint8_t, GURL>> url_map, | 158 std::unique_ptr<std::map<uint8_t, GURL>> url_map, |
| 159 const ReadWebUsbDescriptorsCallback& callback) { | 159 const ReadWebUsbDescriptorsCallback& callback) { |
| 160 for (uint8_t origin_id : allowed_origins->origin_ids) { | 160 if (allowed_origins) { |
| 161 const auto& it = url_map->find(origin_id); | 161 for (uint8_t origin_id : allowed_origins->origin_ids) { |
| 162 if (it != url_map->end()) | |
| 163 allowed_origins->origins.push_back(it->second.GetOrigin()); | |
| 164 } | |
| 165 | |
| 166 for (auto& configuration : allowed_origins->configurations) { | |
| 167 for (uint8_t origin_id : configuration.origin_ids) { | |
| 168 const auto& it = url_map->find(origin_id); | 162 const auto& it = url_map->find(origin_id); |
| 169 if (it != url_map->end()) | 163 if (it != url_map->end()) |
| 170 configuration.origins.push_back(it->second.GetOrigin()); | 164 allowed_origins->origins.push_back(it->second.GetOrigin()); |
| 171 } | 165 } |
| 172 | 166 |
| 173 for (auto& function : configuration.functions) { | 167 for (auto& configuration : allowed_origins->configurations) { |
| 174 for (uint8_t origin_id : function.origin_ids) { | 168 for (uint8_t origin_id : configuration.origin_ids) { |
| 175 const auto& it = url_map->find(origin_id); | 169 const auto& it = url_map->find(origin_id); |
| 176 if (it != url_map->end()) | 170 if (it != url_map->end()) |
| 177 function.origins.push_back(it->second.GetOrigin()); | 171 configuration.origins.push_back(it->second.GetOrigin()); |
| 172 } |
| 173 |
| 174 for (auto& function : configuration.functions) { |
| 175 for (uint8_t origin_id : function.origin_ids) { |
| 176 const auto& it = url_map->find(origin_id); |
| 177 if (it != url_map->end()) |
| 178 function.origins.push_back(it->second.GetOrigin()); |
| 179 } |
| 178 } | 180 } |
| 179 } | 181 } |
| 180 } | 182 } |
| 181 | 183 |
| 182 GURL landing_page; | 184 GURL landing_page; |
| 183 if (landing_page_id != 0) | 185 if (landing_page_id != 0) |
| 184 landing_page = (*url_map)[landing_page_id]; | 186 landing_page = (*url_map)[landing_page_id]; |
| 185 | 187 |
| 186 callback.Run(std::move(allowed_origins), landing_page); | 188 callback.Run(std::move(allowed_origins), landing_page); |
| 187 } | 189 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 216 const base::Closure& callback) { | 218 const base::Closure& callback) { |
| 217 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(255); | 219 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(255); |
| 218 device_handle->ControlTransfer( | 220 device_handle->ControlTransfer( |
| 219 UsbTransferDirection::INBOUND, UsbControlTransferType::VENDOR, | 221 UsbTransferDirection::INBOUND, UsbControlTransferType::VENDOR, |
| 220 UsbControlTransferRecipient::DEVICE, vendor_code, index, kGetUrlRequest, | 222 UsbControlTransferRecipient::DEVICE, vendor_code, index, kGetUrlRequest, |
| 221 buffer, buffer->size(), kControlTransferTimeoutMs, | 223 buffer, buffer->size(), kControlTransferTimeoutMs, |
| 222 base::Bind(&OnReadUrlDescriptor, url_map, index, callback)); | 224 base::Bind(&OnReadUrlDescriptor, url_map, index, callback)); |
| 223 } | 225 } |
| 224 | 226 |
| 225 // Reads URL descriptors from the device so that it can fill |allowed_origins| | 227 // Reads URL descriptors from the device so that it can fill |allowed_origins| |
| 226 // with the GURLs matching the indicies already collected. | 228 // with the GURLs matching the indices already collected. |
| 227 void ReadUrlDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, | 229 void ReadUrlDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, |
| 228 uint8_t vendor_code, | 230 uint8_t vendor_code, |
| 229 uint8_t landing_page_id, | 231 uint8_t landing_page_id, |
| 230 const ReadWebUsbDescriptorsCallback& callback, | 232 const ReadWebUsbDescriptorsCallback& callback, |
| 231 std::unique_ptr<WebUsbAllowedOrigins> allowed_origins) { | 233 std::unique_ptr<WebUsbAllowedOrigins> allowed_origins) { |
| 232 if (!allowed_origins) { | |
| 233 callback.Run(nullptr, GURL()); | |
| 234 return; | |
| 235 } | |
| 236 | |
| 237 std::set<uint8_t> to_request; | 234 std::set<uint8_t> to_request; |
| 238 if (landing_page_id != 0) | 235 if (landing_page_id != 0) |
| 239 to_request.insert(landing_page_id); | 236 to_request.insert(landing_page_id); |
| 240 | 237 |
| 241 to_request.insert(allowed_origins->origin_ids.begin(), | 238 if (allowed_origins) { |
| 242 allowed_origins->origin_ids.end()); | 239 to_request.insert(allowed_origins->origin_ids.begin(), |
| 243 for (auto& config : allowed_origins->configurations) { | 240 allowed_origins->origin_ids.end()); |
| 244 to_request.insert(config.origin_ids.begin(), config.origin_ids.end()); | 241 for (auto& config : allowed_origins->configurations) { |
| 245 for (auto& function : config.functions) { | 242 to_request.insert(config.origin_ids.begin(), config.origin_ids.end()); |
| 246 to_request.insert(function.origin_ids.begin(), function.origin_ids.end()); | 243 for (auto& function : config.functions) { |
| 244 to_request.insert(function.origin_ids.begin(), |
| 245 function.origin_ids.end()); |
| 246 } |
| 247 } | 247 } |
| 248 } | 248 } |
| 249 | 249 |
| 250 std::unique_ptr<std::map<uint8_t, GURL>> url_map( | 250 std::unique_ptr<std::map<uint8_t, GURL>> url_map( |
| 251 new std::map<uint8_t, GURL>()); | 251 new std::map<uint8_t, GURL>()); |
| 252 std::map<uint8_t, GURL>* url_map_ptr = url_map.get(); | 252 std::map<uint8_t, GURL>* url_map_ptr = url_map.get(); |
| 253 base::Closure barrier = base::BarrierClosure( | 253 base::Closure barrier = base::BarrierClosure( |
| 254 static_cast<int>(to_request.size()), | 254 static_cast<int>(to_request.size()), |
| 255 base::Bind(&OnDoneReadingUrls, base::Passed(&allowed_origins), | 255 base::Bind(&OnDoneReadingUrls, base::Passed(&allowed_origins), |
| 256 landing_page_id, base::Passed(&url_map), callback)); | 256 landing_page_id, base::Passed(&url_map), callback)); |
| 257 | 257 |
| 258 for (uint8_t index : to_request) { | 258 for (uint8_t index : to_request) |
| 259 ReadUrlDescriptor(device_handle, vendor_code, url_map_ptr, index, barrier); | 259 ReadUrlDescriptor(device_handle, vendor_code, url_map_ptr, index, barrier); |
| 260 } | |
| 261 } | 260 } |
| 262 | 261 |
| 263 void OnReadWebUsbAllowedOrigins( | 262 void OnReadWebUsbAllowedOrigins( |
| 264 const ReadWebUsbAllowedOriginsCallback& callback, | 263 const ReadWebUsbAllowedOriginsCallback& callback, |
| 265 UsbTransferStatus status, | 264 UsbTransferStatus status, |
| 266 scoped_refptr<net::IOBuffer> buffer, | 265 scoped_refptr<net::IOBuffer> buffer, |
| 267 size_t length) { | 266 size_t length) { |
| 268 if (status != UsbTransferStatus::COMPLETED) { | 267 if (status != UsbTransferStatus::COMPLETED) { |
| 269 USB_LOG(EVENT) << "Failed to read WebUSB allowed origins."; | 268 USB_LOG(EVENT) << "Failed to read WebUSB allowed origins."; |
| 270 callback.Run(nullptr); | 269 callback.Run(nullptr); |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 | 601 |
| 603 if (base::ContainsValue(function.origins, origin)) | 602 if (base::ContainsValue(function.origins, origin)) |
| 604 return true; | 603 return true; |
| 605 } | 604 } |
| 606 } | 605 } |
| 607 | 606 |
| 608 return false; | 607 return false; |
| 609 } | 608 } |
| 610 | 609 |
| 611 } // namespace device | 610 } // namespace device |
| OLD | NEW |