OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/renderer/usb/type_converters.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/logging.h" | |
10 | |
11 namespace mojo { | |
12 | |
13 // static | |
14 blink::WebUSBDevice::TransferDirection | |
15 TypeConverter<blink::WebUSBDevice::TransferDirection, | |
16 device::usb::TransferDirection>:: | |
17 Convert(const device::usb::TransferDirection& direction) { | |
18 switch (direction) { | |
19 case device::usb::TransferDirection::INBOUND: | |
20 return blink::WebUSBDevice::TransferDirection::In; | |
21 case device::usb::TransferDirection::OUTBOUND: | |
22 return blink::WebUSBDevice::TransferDirection::Out; | |
23 default: | |
24 NOTREACHED(); | |
25 return blink::WebUSBDevice::TransferDirection::In; | |
26 } | |
27 } | |
28 | |
29 // static | |
30 device::usb::TransferDirection | |
31 TypeConverter<device::usb::TransferDirection, | |
32 blink::WebUSBDevice::TransferDirection>:: | |
33 Convert(const blink::WebUSBDevice::TransferDirection& direction) { | |
34 switch (direction) { | |
35 case blink::WebUSBDevice::TransferDirection::In: | |
36 return device::usb::TransferDirection::INBOUND; | |
37 case blink::WebUSBDevice::TransferDirection::Out: | |
38 return device::usb::TransferDirection::OUTBOUND; | |
39 default: | |
40 NOTREACHED(); | |
41 return device::usb::TransferDirection::INBOUND; | |
42 } | |
43 } | |
44 | |
45 // static | |
46 device::usb::ControlTransferType | |
47 TypeConverter<device::usb::ControlTransferType, | |
48 blink::WebUSBDevice::RequestType>:: | |
49 Convert(const blink::WebUSBDevice::RequestType& direction) { | |
50 switch (direction) { | |
51 case blink::WebUSBDevice::RequestType::Standard: | |
52 return device::usb::ControlTransferType::STANDARD; | |
53 case blink::WebUSBDevice::RequestType::Class: | |
54 return device::usb::ControlTransferType::CLASS; | |
55 case blink::WebUSBDevice::RequestType::Vendor: | |
56 return device::usb::ControlTransferType::VENDOR; | |
57 default: | |
58 NOTREACHED(); | |
59 return device::usb::ControlTransferType::STANDARD; | |
60 } | |
61 } | |
62 | |
63 // static | |
64 device::usb::ControlTransferRecipient | |
65 TypeConverter<device::usb::ControlTransferRecipient, | |
66 blink::WebUSBDevice::RequestRecipient>:: | |
67 Convert(const blink::WebUSBDevice::RequestRecipient& direction) { | |
68 switch (direction) { | |
69 case blink::WebUSBDevice::RequestRecipient::Device: | |
70 return device::usb::ControlTransferRecipient::DEVICE; | |
71 case blink::WebUSBDevice::RequestRecipient::Interface: | |
72 return device::usb::ControlTransferRecipient::INTERFACE; | |
73 case blink::WebUSBDevice::RequestRecipient::Endpoint: | |
74 return device::usb::ControlTransferRecipient::ENDPOINT; | |
75 case blink::WebUSBDevice::RequestRecipient::Other: | |
76 return device::usb::ControlTransferRecipient::OTHER; | |
77 default: | |
78 NOTREACHED(); | |
79 return device::usb::ControlTransferRecipient::DEVICE; | |
80 } | |
81 } | |
82 | |
83 // static | |
84 device::usb::ControlTransferParamsPtr | |
85 TypeConverter<device::usb::ControlTransferParamsPtr, | |
86 blink::WebUSBDevice::ControlTransferParameters>:: | |
87 Convert(const blink::WebUSBDevice::ControlTransferParameters& parameters) { | |
88 device::usb::ControlTransferParamsPtr params = | |
89 device::usb::ControlTransferParams::New(); | |
90 params->type = | |
91 mojo::ConvertTo<device::usb::ControlTransferType>(parameters.type); | |
92 params->recipient = mojo::ConvertTo<device::usb::ControlTransferRecipient>( | |
93 parameters.recipient); | |
94 params->request = parameters.request; | |
95 params->value = parameters.value; | |
96 params->index = parameters.index; | |
97 return params; | |
98 } | |
99 | |
100 // static | |
101 blink::WebUSBDeviceInfo::Endpoint::Type TypeConverter< | |
102 blink::WebUSBDeviceInfo::Endpoint::Type, | |
103 device::usb::EndpointType>::Convert(const device::usb::EndpointType& | |
104 endpoint_type) { | |
105 switch (endpoint_type) { | |
106 case device::usb::EndpointType::BULK: | |
107 return blink::WebUSBDeviceInfo::Endpoint::Type::Bulk; | |
108 case device::usb::EndpointType::INTERRUPT: | |
109 return blink::WebUSBDeviceInfo::Endpoint::Type::Interrupt; | |
110 case device::usb::EndpointType::ISOCHRONOUS: | |
111 return blink::WebUSBDeviceInfo::Endpoint::Type::Isochronous; | |
112 default: | |
113 NOTREACHED(); | |
114 return blink::WebUSBDeviceInfo::Endpoint::Type::Bulk; | |
115 } | |
116 } | |
117 | |
118 // static | |
119 blink::WebUSBDeviceInfo::Endpoint | |
120 TypeConverter<blink::WebUSBDeviceInfo::Endpoint, device::usb::EndpointInfoPtr>:: | |
121 Convert(const device::usb::EndpointInfoPtr& info) { | |
122 blink::WebUSBDeviceInfo::Endpoint endpoint; | |
123 endpoint.endpointNumber = info->endpoint_number; | |
124 endpoint.direction = | |
125 mojo::ConvertTo<blink::WebUSBDevice::TransferDirection>(info->direction); | |
126 endpoint.type = | |
127 mojo::ConvertTo<blink::WebUSBDeviceInfo::Endpoint::Type>(info->type); | |
128 endpoint.packetSize = info->packet_size; | |
129 return endpoint; | |
130 } | |
131 | |
132 // static | |
133 blink::WebUSBDeviceInfo::AlternateInterface | |
134 TypeConverter<blink::WebUSBDeviceInfo::AlternateInterface, | |
135 device::usb::AlternateInterfaceInfoPtr>:: | |
136 Convert(const device::usb::AlternateInterfaceInfoPtr& info) { | |
137 blink::WebUSBDeviceInfo::AlternateInterface alternate; | |
138 alternate.alternateSetting = info->alternate_setting; | |
139 alternate.classCode = info->class_code; | |
140 alternate.subclassCode = info->subclass_code; | |
141 alternate.protocolCode = info->protocol_code; | |
142 if (!info->interface_name.is_null()) | |
143 alternate.interfaceName = blink::WebString::fromUTF8(info->interface_name); | |
144 alternate.endpoints = blink::WebVector<blink::WebUSBDeviceInfo::Endpoint>( | |
145 info->endpoints.size()); | |
146 for (size_t i = 0; i < info->endpoints.size(); ++i) { | |
147 alternate.endpoints[i] = | |
148 mojo::ConvertTo<blink::WebUSBDeviceInfo::Endpoint>(info->endpoints[i]); | |
149 } | |
150 return alternate; | |
151 } | |
152 | |
153 // static | |
154 blink::WebUSBDeviceInfo::Interface TypeConverter< | |
155 blink::WebUSBDeviceInfo::Interface, | |
156 device::usb::InterfaceInfoPtr>::Convert(const device::usb::InterfaceInfoPtr& | |
157 info) { | |
158 blink::WebUSBDeviceInfo::Interface interface; | |
159 interface.interfaceNumber = info->interface_number; | |
160 interface.alternates = | |
161 blink::WebVector<blink::WebUSBDeviceInfo::AlternateInterface>( | |
162 info->alternates.size()); | |
163 for (size_t i = 0; i < info->alternates.size(); ++i) { | |
164 interface.alternates[i] = | |
165 mojo::ConvertTo<blink::WebUSBDeviceInfo::AlternateInterface>( | |
166 info->alternates[i]); | |
167 } | |
168 return interface; | |
169 } | |
170 | |
171 // static | |
172 blink::WebUSBDeviceInfo::Configuration | |
173 TypeConverter<blink::WebUSBDeviceInfo::Configuration, | |
174 device::usb::ConfigurationInfoPtr>:: | |
175 Convert(const device::usb::ConfigurationInfoPtr& info) { | |
176 blink::WebUSBDeviceInfo::Configuration config; | |
177 config.configurationValue = info->configuration_value; | |
178 if (!info->configuration_name.is_null()) { | |
179 config.configurationName = | |
180 blink::WebString::fromUTF8(info->configuration_name); | |
181 } | |
182 config.interfaces = blink::WebVector<blink::WebUSBDeviceInfo::Interface>( | |
183 info->interfaces.size()); | |
184 for (size_t i = 0; i < info->interfaces.size(); ++i) { | |
185 config.interfaces[i] = mojo::ConvertTo<blink::WebUSBDeviceInfo::Interface>( | |
186 info->interfaces[i]); | |
187 } | |
188 return config; | |
189 } | |
190 | |
191 // static | |
192 blink::WebUSBDeviceInfo | |
193 TypeConverter<blink::WebUSBDeviceInfo, device::usb::DeviceInfoPtr>::Convert( | |
194 const device::usb::DeviceInfoPtr& info) { | |
195 blink::WebUSBDeviceInfo device; | |
196 device.guid = blink::WebString::fromUTF8(info->guid); | |
197 device.usbVersionMajor = info->usb_version_major; | |
198 device.usbVersionMinor = info->usb_version_minor; | |
199 device.usbVersionSubminor = info->usb_version_subminor; | |
200 device.deviceClass = info->class_code; | |
201 device.deviceSubclass = info->subclass_code; | |
202 device.deviceProtocol = info->protocol_code; | |
203 device.vendorID = info->vendor_id; | |
204 device.productID = info->product_id; | |
205 device.deviceVersionMajor = info->device_version_major; | |
206 device.deviceVersionMinor = info->device_version_minor; | |
207 device.deviceVersionSubminor = info->device_version_subminor; | |
208 if (!info->manufacturer_name.is_null()) { | |
209 device.manufacturerName = | |
210 blink::WebString::fromUTF8(info->manufacturer_name); | |
211 } | |
212 if (!info->product_name.is_null()) | |
213 device.productName = blink::WebString::fromUTF8(info->product_name); | |
214 if (!info->serial_number.is_null()) | |
215 device.serialNumber = blink::WebString::fromUTF8(info->serial_number); | |
216 device.activeConfiguration = info->active_configuration; | |
217 device.configurations = | |
218 blink::WebVector<blink::WebUSBDeviceInfo::Configuration>( | |
219 info->configurations.size()); | |
220 for (size_t i = 0; i < info->configurations.size(); ++i) { | |
221 device.configurations[i] = | |
222 mojo::ConvertTo<blink::WebUSBDeviceInfo::Configuration>( | |
223 info->configurations[i]); | |
224 } | |
225 return device; | |
226 } | |
227 | |
228 // static | |
229 device::usb::DeviceFilterPtr | |
230 TypeConverter<device::usb::DeviceFilterPtr, blink::WebUSBDeviceFilter>::Convert( | |
231 const blink::WebUSBDeviceFilter& web_filter) { | |
232 device::usb::DeviceFilterPtr filter = device::usb::DeviceFilter::New(); | |
233 filter->has_vendor_id = web_filter.hasVendorID; | |
234 filter->vendor_id = web_filter.vendorID; | |
235 filter->has_product_id = web_filter.hasProductID; | |
236 filter->product_id = web_filter.productID; | |
237 filter->has_class_code = web_filter.hasClassCode; | |
238 filter->class_code = web_filter.classCode; | |
239 filter->has_subclass_code = web_filter.hasSubclassCode; | |
240 filter->subclass_code = web_filter.subclassCode; | |
241 filter->has_protocol_code = web_filter.hasProtocolCode; | |
242 filter->protocol_code = web_filter.protocolCode; | |
243 return filter; | |
244 } | |
245 | |
246 // static | |
247 device::usb::EnumerationOptionsPtr | |
248 TypeConverter<device::usb::EnumerationOptionsPtr, | |
249 blink::WebUSBDeviceRequestOptions>:: | |
250 Convert(const blink::WebUSBDeviceRequestOptions& web_options) { | |
251 device::usb::EnumerationOptionsPtr options = | |
252 device::usb::EnumerationOptions::New(); | |
253 options->filters = mojo::Array<device::usb::DeviceFilterPtr>::New( | |
254 web_options.filters.size()); | |
255 for (size_t i = 0; i < web_options.filters.size(); ++i) { | |
256 options->filters[i] = | |
257 device::usb::DeviceFilter::From(web_options.filters[i]); | |
258 } | |
259 return options; | |
260 } | |
261 | |
262 } // namespace mojo | |
OLD | NEW |