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> |
| 11 #include <memory> |
11 #include <set> | 12 #include <set> |
12 | 13 |
13 #include "base/barrier_closure.h" | 14 #include "base/barrier_closure.h" |
14 #include "base/bind.h" | 15 #include "base/bind.h" |
15 #include "base/callback.h" | 16 #include "base/callback.h" |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
18 #include "components/device_event_log/device_event_log.h" | 19 #include "components/device_event_log/device_event_log.h" |
19 #include "device/usb/usb_device_handle.h" | 20 #include "device/usb/usb_device_handle.h" |
20 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
(...skipping 19 matching lines...) Expand all Loading... |
40 const uint8_t kGetUrlRequest = 0x02; | 41 const uint8_t kGetUrlRequest = 0x02; |
41 | 42 |
42 const uint8_t kWebUsbCapabilityUUID[16] = { | 43 const uint8_t kWebUsbCapabilityUUID[16] = { |
43 // Little-endian encoding of {3408b638-09a9-47a0-8bfd-a0768815b665}. | 44 // Little-endian encoding of {3408b638-09a9-47a0-8bfd-a0768815b665}. |
44 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, | 45 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, |
45 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65}; | 46 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65}; |
46 | 47 |
47 const int kControlTransferTimeout = 60000; // 1 minute | 48 const int kControlTransferTimeout = 60000; // 1 minute |
48 | 49 |
49 using ReadWebUsbDescriptorsCallback = | 50 using ReadWebUsbDescriptorsCallback = |
50 base::Callback<void(scoped_ptr<WebUsbAllowedOrigins> allowed_origins, | 51 base::Callback<void(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins, |
51 const GURL& landing_page)>; | 52 const GURL& landing_page)>; |
52 | 53 |
53 using ReadWebUsbAllowedOriginsCallback = | 54 using ReadWebUsbAllowedOriginsCallback = |
54 base::Callback<void(scoped_ptr<WebUsbAllowedOrigins> allowed_origins)>; | 55 base::Callback<void(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins)>; |
55 | 56 |
56 // Parses a WebUSB Function Subset Header: | 57 // Parses a WebUSB Function Subset Header: |
57 // http://wicg.github.io/webusb/#dfn-function-subset-header | 58 // http://wicg.github.io/webusb/#dfn-function-subset-header |
58 // | 59 // |
59 // 0 1 2 3 | 60 // 0 1 2 3 |
60 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | 61 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
61 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 62 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
62 // | length | type | 1st interface | origin[0] | | 63 // | length | type | 1st interface | origin[0] | |
63 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 64 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
64 // | origin[1] | ... | 65 // | origin[1] | ... |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 for (size_t i = 0; i < num_functions; ++i) { | 146 for (size_t i = 0; i < num_functions; ++i) { |
146 WebUsbFunctionSubset function; | 147 WebUsbFunctionSubset function; |
147 if (!ParseFunction(&function, it, end)) | 148 if (!ParseFunction(&function, it, end)) |
148 return false; | 149 return false; |
149 configuration->functions.push_back(function); | 150 configuration->functions.push_back(function); |
150 } | 151 } |
151 | 152 |
152 return true; | 153 return true; |
153 } | 154 } |
154 | 155 |
155 void OnDoneReadingUrls(scoped_ptr<WebUsbAllowedOrigins> allowed_origins, | 156 void OnDoneReadingUrls(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins, |
156 uint8_t landing_page_id, | 157 uint8_t landing_page_id, |
157 scoped_ptr<std::map<uint8_t, GURL>> url_map, | 158 std::unique_ptr<std::map<uint8_t, GURL>> url_map, |
158 const ReadWebUsbDescriptorsCallback& callback) { | 159 const ReadWebUsbDescriptorsCallback& callback) { |
159 for (uint8_t origin_id : allowed_origins->origin_ids) { | 160 for (uint8_t origin_id : allowed_origins->origin_ids) { |
160 const auto& it = url_map->find(origin_id); | 161 const auto& it = url_map->find(origin_id); |
161 if (it != url_map->end()) | 162 if (it != url_map->end()) |
162 allowed_origins->origins.push_back(it->second.GetOrigin()); | 163 allowed_origins->origins.push_back(it->second.GetOrigin()); |
163 } | 164 } |
164 | 165 |
165 for (auto& configuration : allowed_origins->configurations) { | 166 for (auto& configuration : allowed_origins->configurations) { |
166 for (uint8_t origin_id : configuration.origin_ids) { | 167 for (uint8_t origin_id : configuration.origin_ids) { |
167 const auto& it = url_map->find(origin_id); | 168 const auto& it = url_map->find(origin_id); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 kControlTransferTimeout, | 221 kControlTransferTimeout, |
221 base::Bind(&OnReadUrlDescriptor, url_map, index, callback)); | 222 base::Bind(&OnReadUrlDescriptor, url_map, index, callback)); |
222 } | 223 } |
223 | 224 |
224 // Reads URL descriptors from the device so that it can fill |allowed_origins| | 225 // Reads URL descriptors from the device so that it can fill |allowed_origins| |
225 // with the GURLs matching the indicies already collected. | 226 // with the GURLs matching the indicies already collected. |
226 void ReadUrlDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, | 227 void ReadUrlDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, |
227 uint8_t vendor_code, | 228 uint8_t vendor_code, |
228 uint8_t landing_page_id, | 229 uint8_t landing_page_id, |
229 const ReadWebUsbDescriptorsCallback& callback, | 230 const ReadWebUsbDescriptorsCallback& callback, |
230 scoped_ptr<WebUsbAllowedOrigins> allowed_origins) { | 231 std::unique_ptr<WebUsbAllowedOrigins> allowed_origins) { |
231 if (!allowed_origins) { | 232 if (!allowed_origins) { |
232 callback.Run(nullptr, GURL()); | 233 callback.Run(nullptr, GURL()); |
233 return; | 234 return; |
234 } | 235 } |
235 | 236 |
236 std::set<uint8_t> to_request; | 237 std::set<uint8_t> to_request; |
237 if (landing_page_id != 0) | 238 if (landing_page_id != 0) |
238 to_request.insert(landing_page_id); | 239 to_request.insert(landing_page_id); |
239 | 240 |
240 to_request.insert(allowed_origins->origin_ids.begin(), | 241 to_request.insert(allowed_origins->origin_ids.begin(), |
241 allowed_origins->origin_ids.end()); | 242 allowed_origins->origin_ids.end()); |
242 for (auto& config : allowed_origins->configurations) { | 243 for (auto& config : allowed_origins->configurations) { |
243 to_request.insert(config.origin_ids.begin(), config.origin_ids.end()); | 244 to_request.insert(config.origin_ids.begin(), config.origin_ids.end()); |
244 for (auto& function : config.functions) { | 245 for (auto& function : config.functions) { |
245 to_request.insert(function.origin_ids.begin(), function.origin_ids.end()); | 246 to_request.insert(function.origin_ids.begin(), function.origin_ids.end()); |
246 } | 247 } |
247 } | 248 } |
248 | 249 |
249 scoped_ptr<std::map<uint8_t, GURL>> url_map(new std::map<uint8_t, GURL>()); | 250 std::unique_ptr<std::map<uint8_t, GURL>> url_map( |
| 251 new std::map<uint8_t, GURL>()); |
250 std::map<uint8_t, GURL>* url_map_ptr = url_map.get(); | 252 std::map<uint8_t, GURL>* url_map_ptr = url_map.get(); |
251 base::Closure barrier = base::BarrierClosure( | 253 base::Closure barrier = base::BarrierClosure( |
252 static_cast<int>(to_request.size()), | 254 static_cast<int>(to_request.size()), |
253 base::Bind(&OnDoneReadingUrls, base::Passed(&allowed_origins), | 255 base::Bind(&OnDoneReadingUrls, base::Passed(&allowed_origins), |
254 landing_page_id, base::Passed(&url_map), callback)); | 256 landing_page_id, base::Passed(&url_map), callback)); |
255 | 257 |
256 for (uint8_t index : to_request) { | 258 for (uint8_t index : to_request) { |
257 ReadUrlDescriptor(device_handle, vendor_code, url_map_ptr, index, barrier); | 259 ReadUrlDescriptor(device_handle, vendor_code, url_map_ptr, index, barrier); |
258 } | 260 } |
259 } | 261 } |
260 | 262 |
261 void OnReadWebUsbAllowedOrigins( | 263 void OnReadWebUsbAllowedOrigins( |
262 const ReadWebUsbAllowedOriginsCallback& callback, | 264 const ReadWebUsbAllowedOriginsCallback& callback, |
263 UsbTransferStatus status, | 265 UsbTransferStatus status, |
264 scoped_refptr<net::IOBuffer> buffer, | 266 scoped_refptr<net::IOBuffer> buffer, |
265 size_t length) { | 267 size_t length) { |
266 if (status != USB_TRANSFER_COMPLETED) { | 268 if (status != USB_TRANSFER_COMPLETED) { |
267 USB_LOG(EVENT) << "Failed to read WebUSB allowed origins."; | 269 USB_LOG(EVENT) << "Failed to read WebUSB allowed origins."; |
268 callback.Run(nullptr); | 270 callback.Run(nullptr); |
269 return; | 271 return; |
270 } | 272 } |
271 | 273 |
272 scoped_ptr<WebUsbAllowedOrigins> allowed_origins(new WebUsbAllowedOrigins()); | 274 std::unique_ptr<WebUsbAllowedOrigins> allowed_origins( |
| 275 new WebUsbAllowedOrigins()); |
273 if (allowed_origins->Parse( | 276 if (allowed_origins->Parse( |
274 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) { | 277 std::vector<uint8_t>(buffer->data(), buffer->data() + length))) { |
275 callback.Run(std::move(allowed_origins)); | 278 callback.Run(std::move(allowed_origins)); |
276 } else { | 279 } else { |
277 callback.Run(nullptr); | 280 callback.Run(nullptr); |
278 } | 281 } |
279 } | 282 } |
280 | 283 |
281 void OnReadWebUsbAllowedOriginsHeader( | 284 void OnReadWebUsbAllowedOriginsHeader( |
282 scoped_refptr<UsbDeviceHandle> device_handle, | 285 scoped_refptr<UsbDeviceHandle> device_handle, |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 for (const auto& function : config.functions) { | 588 for (const auto& function : config.functions) { |
586 if (ContainsValue(function.origins, origin)) | 589 if (ContainsValue(function.origins, origin)) |
587 return true; | 590 return true; |
588 } | 591 } |
589 } | 592 } |
590 | 593 |
591 return false; | 594 return false; |
592 } | 595 } |
593 | 596 |
594 } // namespace device | 597 } // namespace device |
OLD | NEW |