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
3 // found in the LICENSE file.
5 #include "content/renderer/usb/type_converters.h"
7 #include "base/logging.h"
12 blink::WebUSBDevice::TransferDirection
13 TypeConverter
<blink::WebUSBDevice::TransferDirection
,
14 device::usb::TransferDirection
>::
15 Convert(const device::usb::TransferDirection
& direction
) {
17 case device::usb::TRANSFER_DIRECTION_IN
:
18 return blink::WebUSBDevice::TransferDirection::In
;
19 case device::usb::TRANSFER_DIRECTION_OUT
:
20 return blink::WebUSBDevice::TransferDirection::Out
;
23 return blink::WebUSBDevice::TransferDirection::In
;
28 device::usb::TransferDirection
29 TypeConverter
<device::usb::TransferDirection
,
30 blink::WebUSBDevice::TransferDirection
>::
31 Convert(const blink::WebUSBDevice::TransferDirection
& direction
) {
33 case blink::WebUSBDevice::TransferDirection::In
:
34 return device::usb::TRANSFER_DIRECTION_IN
;
35 case blink::WebUSBDevice::TransferDirection::Out
:
36 return device::usb::TRANSFER_DIRECTION_OUT
;
39 return device::usb::TRANSFER_DIRECTION_IN
;
44 device::usb::ControlTransferType
45 TypeConverter
<device::usb::ControlTransferType
,
46 blink::WebUSBDevice::RequestType
>::
47 Convert(const blink::WebUSBDevice::RequestType
& direction
) {
49 case blink::WebUSBDevice::RequestType::Standard
:
50 return device::usb::CONTROL_TRANSFER_TYPE_STANDARD
;
51 case blink::WebUSBDevice::RequestType::Class
:
52 return device::usb::CONTROL_TRANSFER_TYPE_CLASS
;
53 case blink::WebUSBDevice::RequestType::Vendor
:
54 return device::usb::CONTROL_TRANSFER_TYPE_VENDOR
;
57 return device::usb::CONTROL_TRANSFER_TYPE_STANDARD
;
62 device::usb::ControlTransferRecipient
63 TypeConverter
<device::usb::ControlTransferRecipient
,
64 blink::WebUSBDevice::RequestRecipient
>::
65 Convert(const blink::WebUSBDevice::RequestRecipient
& direction
) {
67 case blink::WebUSBDevice::RequestRecipient::Device
:
68 return device::usb::CONTROL_TRANSFER_RECIPIENT_DEVICE
;
69 case blink::WebUSBDevice::RequestRecipient::Interface
:
70 return device::usb::CONTROL_TRANSFER_RECIPIENT_INTERFACE
;
71 case blink::WebUSBDevice::RequestRecipient::Endpoint
:
72 return device::usb::CONTROL_TRANSFER_RECIPIENT_ENDPOINT
;
73 case blink::WebUSBDevice::RequestRecipient::Other
:
74 return device::usb::CONTROL_TRANSFER_RECIPIENT_OTHER
;
77 return device::usb::CONTROL_TRANSFER_RECIPIENT_DEVICE
;
82 device::usb::ControlTransferParamsPtr
83 TypeConverter
<device::usb::ControlTransferParamsPtr
,
84 blink::WebUSBDevice::ControlTransferParameters
>::
85 Convert(const blink::WebUSBDevice::ControlTransferParameters
& parameters
) {
86 device::usb::ControlTransferParamsPtr params
=
87 device::usb::ControlTransferParams::New();
89 mojo::ConvertTo
<device::usb::ControlTransferType
>(parameters
.type
);
90 params
->recipient
= mojo::ConvertTo
<device::usb::ControlTransferRecipient
>(
91 parameters
.recipient
);
92 params
->request
= parameters
.request
;
93 params
->value
= parameters
.value
;
94 params
->index
= parameters
.index
;
99 blink::WebUSBDeviceInfo::Endpoint::Type TypeConverter
<
100 blink::WebUSBDeviceInfo::Endpoint::Type
,
101 device::usb::EndpointType
>::Convert(const device::usb::EndpointType
&
103 switch (endpoint_type
) {
104 case device::usb::ENDPOINT_TYPE_BULK
:
105 return blink::WebUSBDeviceInfo::Endpoint::Type::Bulk
;
106 case device::usb::ENDPOINT_TYPE_INTERRUPT
:
107 return blink::WebUSBDeviceInfo::Endpoint::Type::Interrupt
;
108 case device::usb::ENDPOINT_TYPE_ISOCHRONOUS
:
109 return blink::WebUSBDeviceInfo::Endpoint::Type::Isochronous
;
112 return blink::WebUSBDeviceInfo::Endpoint::Type::Bulk
;
117 blink::WebUSBDeviceInfo::Endpoint
118 TypeConverter
<blink::WebUSBDeviceInfo::Endpoint
, device::usb::EndpointInfoPtr
>::
119 Convert(const device::usb::EndpointInfoPtr
& info
) {
120 blink::WebUSBDeviceInfo::Endpoint endpoint
;
121 endpoint
.endpointNumber
= info
->endpoint_number
;
123 mojo::ConvertTo
<blink::WebUSBDevice::TransferDirection
>(info
->direction
);
125 mojo::ConvertTo
<blink::WebUSBDeviceInfo::Endpoint::Type
>(info
->type
);
126 endpoint
.packetSize
= info
->packet_size
;
131 blink::WebUSBDeviceInfo::AlternateInterface
132 TypeConverter
<blink::WebUSBDeviceInfo::AlternateInterface
,
133 device::usb::AlternateInterfaceInfoPtr
>::
134 Convert(const device::usb::AlternateInterfaceInfoPtr
& info
) {
135 blink::WebUSBDeviceInfo::AlternateInterface alternate
;
136 alternate
.alternateSetting
= info
->alternate_setting
;
137 alternate
.classCode
= info
->class_code
;
138 alternate
.subclassCode
= info
->subclass_code
;
139 alternate
.protocolCode
= info
->protocol_code
;
140 if (!info
->interface_name
.is_null())
141 alternate
.interfaceName
= blink::WebString::fromUTF8(info
->interface_name
);
142 alternate
.endpoints
= blink::WebVector
<blink::WebUSBDeviceInfo::Endpoint
>(
143 info
->endpoints
.size());
144 for (size_t i
= 0; i
< info
->endpoints
.size(); ++i
) {
145 alternate
.endpoints
[i
] =
146 mojo::ConvertTo
<blink::WebUSBDeviceInfo::Endpoint
>(info
->endpoints
[i
]);
152 blink::WebUSBDeviceInfo::Interface TypeConverter
<
153 blink::WebUSBDeviceInfo::Interface
,
154 device::usb::InterfaceInfoPtr
>::Convert(const device::usb::InterfaceInfoPtr
&
156 blink::WebUSBDeviceInfo::Interface interface
;
157 interface
.interfaceNumber
= info
->interface_number
;
158 interface
.alternates
=
159 blink::WebVector
<blink::WebUSBDeviceInfo::AlternateInterface
>(
160 info
->alternates
.size());
161 for (size_t i
= 0; i
< info
->alternates
.size(); ++i
) {
162 interface
.alternates
[i
] =
163 mojo::ConvertTo
<blink::WebUSBDeviceInfo::AlternateInterface
>(
164 info
->alternates
[i
]);
170 blink::WebUSBDeviceInfo::Configuration
171 TypeConverter
<blink::WebUSBDeviceInfo::Configuration
,
172 device::usb::ConfigurationInfoPtr
>::
173 Convert(const device::usb::ConfigurationInfoPtr
& info
) {
174 blink::WebUSBDeviceInfo::Configuration config
;
175 config
.configurationValue
= info
->configuration_value
;
176 if (!info
->configuration_name
.is_null()) {
177 config
.configurationName
=
178 blink::WebString::fromUTF8(info
->configuration_name
);
180 config
.interfaces
= blink::WebVector
<blink::WebUSBDeviceInfo::Interface
>(
181 info
->interfaces
.size());
182 for (size_t i
= 0; i
< info
->interfaces
.size(); ++i
) {
183 config
.interfaces
[i
] = mojo::ConvertTo
<blink::WebUSBDeviceInfo::Interface
>(
184 info
->interfaces
[i
]);
190 blink::WebUSBDeviceInfo
191 TypeConverter
<blink::WebUSBDeviceInfo
, device::usb::DeviceInfoPtr
>::Convert(
192 const device::usb::DeviceInfoPtr
& info
) {
193 blink::WebUSBDeviceInfo device
;
194 device
.guid
= blink::WebString::fromUTF8(info
->guid
);
195 device
.usbVersionMajor
= info
->usb_version_major
;
196 device
.usbVersionMinor
= info
->usb_version_minor
;
197 device
.usbVersionSubminor
= info
->usb_version_subminor
;
198 device
.deviceClass
= info
->class_code
;
199 device
.deviceSubclass
= info
->subclass_code
;
200 device
.deviceProtocol
= info
->protocol_code
;
201 device
.vendorID
= info
->vendor_id
;
202 device
.productID
= info
->product_id
;
203 device
.deviceVersionMajor
= info
->device_version_major
;
204 device
.deviceVersionMinor
= info
->device_version_minor
;
205 device
.deviceVersionSubminor
= info
->device_version_subminor
;
206 if (!info
->manufacturer_name
.is_null()) {
207 device
.manufacturerName
=
208 blink::WebString::fromUTF8(info
->manufacturer_name
);
210 if (!info
->product_name
.is_null())
211 device
.productName
= blink::WebString::fromUTF8(info
->product_name
);
212 if (!info
->serial_number
.is_null())
213 device
.serialNumber
= blink::WebString::fromUTF8(info
->serial_number
);
214 device
.configurations
=
215 blink::WebVector
<blink::WebUSBDeviceInfo::Configuration
>(
216 info
->configurations
.size());
217 for (size_t i
= 0; i
< info
->configurations
.size(); ++i
) {
218 device
.configurations
[i
] =
219 mojo::ConvertTo
<blink::WebUSBDeviceInfo::Configuration
>(
220 info
->configurations
[i
]);
226 device::usb::DeviceFilterPtr
227 TypeConverter
<device::usb::DeviceFilterPtr
, blink::WebUSBDeviceFilter
>::Convert(
228 const blink::WebUSBDeviceFilter
& web_filter
) {
229 device::usb::DeviceFilterPtr filter
= device::usb::DeviceFilter::New();
230 filter
->has_vendor_id
= web_filter
.hasVendorID
;
231 filter
->vendor_id
= web_filter
.vendorID
;
232 filter
->has_product_id
= web_filter
.hasProductID
;
233 filter
->product_id
= web_filter
.productID
;
234 filter
->has_class_code
= web_filter
.hasClassCode
;
235 filter
->class_code
= web_filter
.classCode
;
236 filter
->has_subclass_code
= web_filter
.hasSubclassCode
;
237 filter
->subclass_code
= web_filter
.subclassCode
;
238 filter
->has_protocol_code
= web_filter
.hasProtocolCode
;
239 filter
->protocol_code
= web_filter
.protocolCode
;
240 return filter
.Pass();
244 device::usb::EnumerationOptionsPtr
245 TypeConverter
<device::usb::EnumerationOptionsPtr
,
246 blink::WebUSBDeviceRequestOptions
>::
247 Convert(const blink::WebUSBDeviceRequestOptions
& web_options
) {
248 device::usb::EnumerationOptionsPtr options
=
249 device::usb::EnumerationOptions::New();
250 options
->filters
= mojo::Array
<device::usb::DeviceFilterPtr
>::New(
251 web_options
.filters
.size());
252 for (size_t i
= 0; i
< web_options
.filters
.size(); ++i
) {
253 options
->filters
[i
] =
254 device::usb::DeviceFilter::From(web_options
.filters
[i
]);
256 return options
.Pass();