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 "modules/webusb/USBEndpoint.h" | 5 #include "modules/webusb/USBEndpoint.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
9 #include "core/dom/ExceptionCode.h" | 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "device/usb/public/interfaces/device.mojom-wtf.h" |
10 #include "modules/webusb/USBAlternateInterface.h" | 11 #include "modules/webusb/USBAlternateInterface.h" |
11 #include "public/platform/modules/webusb/WebUSBDevice.h" | 12 |
| 13 using device::usb::wtf::EndpointType; |
| 14 using device::usb::wtf::TransferDirection; |
12 | 15 |
13 namespace blink { | 16 namespace blink { |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 bool convertDirectionFromEnum(const String& direction, WebUSBDevice::TransferDir
ection* webDirection) | 20 String convertDirectionToEnum(const TransferDirection& direction) |
18 { | |
19 if (direction == "in") | |
20 *webDirection = WebUSBDevice::TransferDirection::In; | |
21 else if (direction == "out") | |
22 *webDirection = WebUSBDevice::TransferDirection::Out; | |
23 else | |
24 return false; | |
25 return true; | |
26 } | |
27 | |
28 String convertDirectionToEnum(const WebUSBDevice::TransferDirection& direction) | |
29 { | 21 { |
30 switch (direction) { | 22 switch (direction) { |
31 case WebUSBDevice::TransferDirection::In: | 23 case TransferDirection::INBOUND: |
32 return "in"; | 24 return "in"; |
33 case WebUSBDevice::TransferDirection::Out: | 25 case TransferDirection::OUTBOUND: |
34 return "out"; | 26 return "out"; |
35 default: | 27 default: |
36 ASSERT_NOT_REACHED(); | 28 ASSERT_NOT_REACHED(); |
37 return ""; | 29 return ""; |
38 } | 30 } |
39 } | 31 } |
40 | 32 |
41 String convertTypeToEnum(const WebUSBDeviceInfo::Endpoint::Type& type) | 33 String convertTypeToEnum(const EndpointType& type) |
42 { | 34 { |
43 switch (type) { | 35 switch (type) { |
44 case WebUSBDeviceInfo::Endpoint::Type::Bulk: | 36 case EndpointType::BULK: |
45 return "bulk"; | 37 return "bulk"; |
46 case WebUSBDeviceInfo::Endpoint::Type::Interrupt: | 38 case EndpointType::INTERRUPT: |
47 return "interrupt"; | 39 return "interrupt"; |
48 case WebUSBDeviceInfo::Endpoint::Type::Isochronous: | 40 case EndpointType::ISOCHRONOUS: |
49 return "isochronous"; | 41 return "isochronous"; |
50 default: | 42 default: |
51 ASSERT_NOT_REACHED(); | 43 ASSERT_NOT_REACHED(); |
52 return ""; | 44 return ""; |
53 } | 45 } |
54 } | 46 } |
55 | 47 |
56 } // namespace | 48 } // namespace |
57 | 49 |
58 USBEndpoint* USBEndpoint::create(const USBAlternateInterface* alternate, size_t
endpointIndex) | 50 USBEndpoint* USBEndpoint::create(const USBAlternateInterface* alternate, size_t
endpointIndex) |
59 { | 51 { |
60 return new USBEndpoint(alternate, endpointIndex); | 52 return new USBEndpoint(alternate, endpointIndex); |
61 } | 53 } |
62 | 54 |
63 USBEndpoint* USBEndpoint::create(const USBAlternateInterface* alternate, size_t
endpointNumber, const String& direction, ExceptionState& exceptionState) | 55 USBEndpoint* USBEndpoint::create(const USBAlternateInterface* alternate, size_t
endpointNumber, const String& direction, ExceptionState& exceptionState) |
64 { | 56 { |
65 WebUSBDevice::TransferDirection webDirection; | 57 TransferDirection mojoDirection = direction == "in" ? TransferDirection::INB
OUND : TransferDirection::OUTBOUND; |
66 if (!convertDirectionFromEnum(direction, &webDirection)) { | 58 const auto& endpoints = alternate->info().endpoints; |
67 exceptionState.throwRangeError("Invalid endpoint direction."); | 59 for (size_t i = 0; i < endpoints.size(); ++i) { |
68 return nullptr; | 60 const auto& endpoint = endpoints[i]; |
69 } | 61 if (endpoint->endpoint_number == endpointNumber && endpoint->direction =
= mojoDirection) |
70 for (size_t i = 0; i < alternate->info().endpoints.size(); ++i) { | |
71 const WebUSBDeviceInfo::Endpoint& endpoint = alternate->info().endpoints
[i]; | |
72 if (endpoint.endpointNumber == endpointNumber && endpoint.direction == w
ebDirection) | |
73 return USBEndpoint::create(alternate, i); | 62 return USBEndpoint::create(alternate, i); |
74 } | 63 } |
75 exceptionState.throwRangeError("Invalid endpoint number."); | 64 exceptionState.throwRangeError("No such endpoint exists in the given alterna
te interface."); |
76 return nullptr; | 65 return nullptr; |
77 } | 66 } |
78 | 67 |
79 USBEndpoint::USBEndpoint(const USBAlternateInterface* alternate, size_t endpoint
Index) | 68 USBEndpoint::USBEndpoint(const USBAlternateInterface* alternate, size_t endpoint
Index) |
80 : m_alternate(alternate) | 69 : m_alternate(alternate) |
81 , m_endpointIndex(endpointIndex) | 70 , m_endpointIndex(endpointIndex) |
82 { | 71 { |
83 ASSERT(m_alternate); | 72 ASSERT(m_alternate); |
84 ASSERT(m_endpointIndex < m_alternate->info().endpoints.size()); | 73 ASSERT(m_endpointIndex < m_alternate->info().endpoints.size()); |
85 } | 74 } |
86 | 75 |
87 const WebUSBDeviceInfo::Endpoint& USBEndpoint::info() const | 76 const device::usb::wtf::EndpointInfo& USBEndpoint::info() const |
88 { | 77 { |
89 const WebUSBDeviceInfo::AlternateInterface& alternateInfo = m_alternate->inf
o(); | 78 const device::usb::wtf::AlternateInterfaceInfo& alternateInfo = m_alternate-
>info(); |
90 ASSERT(m_endpointIndex < alternateInfo.endpoints.size()); | 79 ASSERT(m_endpointIndex < alternateInfo.endpoints.size()); |
91 return alternateInfo.endpoints[m_endpointIndex]; | 80 return *alternateInfo.endpoints[m_endpointIndex]; |
92 } | |
93 | |
94 uint8_t USBEndpoint::endpointNumber() const | |
95 { | |
96 return info().endpointNumber; | |
97 } | 81 } |
98 | 82 |
99 String USBEndpoint::direction() const | 83 String USBEndpoint::direction() const |
100 { | 84 { |
101 return convertDirectionToEnum(info().direction); | 85 return convertDirectionToEnum(info().direction); |
102 } | 86 } |
103 | 87 |
104 String USBEndpoint::type() const | 88 String USBEndpoint::type() const |
105 { | 89 { |
106 return convertTypeToEnum(info().type); | 90 return convertTypeToEnum(info().type); |
107 } | 91 } |
108 | 92 |
109 unsigned USBEndpoint::packetSize() const | |
110 { | |
111 return info().packetSize; | |
112 } | |
113 | |
114 DEFINE_TRACE(USBEndpoint) | 93 DEFINE_TRACE(USBEndpoint) |
115 { | 94 { |
116 visitor->trace(m_alternate); | 95 visitor->trace(m_alternate); |
117 } | 96 } |
118 | 97 |
119 } // namespace blink | 98 } // namespace blink |
OLD | NEW |