cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / device / devices_app / usb / device_impl.cc
blob8cf6dbd75f25021f6c6048b8e8479dc7999678b1
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 "device/devices_app/usb/device_impl.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/stl_util.h"
10 #include "device/devices_app/usb/type_converters.h"
11 #include "device/usb/usb_descriptors.h"
12 #include "device/usb/usb_device.h"
13 #include "net/base/io_buffer.h"
15 namespace device {
16 namespace usb {
18 namespace {
20 template <typename... Args>
21 void CallMojoCallback(const mojo::Callback<void(Args...)>& callback,
22 Args... args) {
23 callback.Run(args...);
26 // Generic wrapper to convert a Mojo callback to something we can rebind and
27 // pass around. This is only usable for callbacks with no move-only arguments.
28 template <typename... Args>
29 base::Callback<void(Args...)> WrapMojoCallback(
30 const mojo::Callback<void(Args...)>& callback) {
31 return base::Bind(&CallMojoCallback<Args...>, callback);
34 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) {
35 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(
36 std::max(static_cast<size_t>(1u), static_cast<size_t>(size)));
37 return buffer;
40 } // namespace
42 DeviceImpl::DeviceImpl(scoped_refptr<UsbDeviceHandle> device_handle,
43 mojo::InterfaceRequest<Device> request)
44 : binding_(this, request.Pass()),
45 device_handle_(device_handle),
46 weak_factory_(this) {
49 DeviceImpl::~DeviceImpl() {
50 CloseHandle();
53 void DeviceImpl::CloseHandle() {
54 if (device_handle_)
55 device_handle_->Close();
56 device_handle_ = nullptr;
59 void DeviceImpl::OnTransferIn(const MojoTransferInCallback& callback,
60 UsbTransferStatus status,
61 scoped_refptr<net::IOBuffer> buffer,
62 size_t buffer_size) {
63 mojo::Array<uint8_t> data;
64 if (buffer) {
65 // TODO(rockot/reillyg): We should change UsbDeviceHandle to use a
66 // std::vector<uint8_t> instead of net::IOBuffer. Then we could move
67 // instead of copy.
68 std::vector<uint8_t> bytes(buffer_size);
69 std::copy(buffer->data(), buffer->data() + buffer_size, bytes.begin());
70 data.Swap(&bytes);
72 callback.Run(mojo::ConvertTo<TransferStatus>(status), data.Pass());
75 void DeviceImpl::OnTransferOut(const MojoTransferOutCallback& callback,
76 UsbTransferStatus status,
77 scoped_refptr<net::IOBuffer> buffer,
78 size_t buffer_size) {
79 callback.Run(mojo::ConvertTo<TransferStatus>(status));
82 void DeviceImpl::OnIsochronousTransferIn(
83 const IsochronousTransferInCallback& callback,
84 uint32_t packet_size,
85 UsbTransferStatus status,
86 scoped_refptr<net::IOBuffer> buffer,
87 size_t buffer_size) {
88 size_t num_packets = buffer_size / packet_size;
89 mojo::Array<mojo::Array<uint8_t>> packets(num_packets);
90 if (buffer) {
91 for (size_t i = 0; i < num_packets; ++i) {
92 size_t packet_index = i * packet_size;
93 std::vector<uint8_t> bytes(packet_size);
94 std::copy(buffer->data() + packet_index,
95 buffer->data() + packet_index + packet_size, bytes.begin());
96 packets[i].Swap(&bytes);
99 callback.Run(mojo::ConvertTo<TransferStatus>(status), packets.Pass());
102 void DeviceImpl::OnIsochronousTransferOut(
103 const MojoTransferOutCallback& callback,
104 UsbTransferStatus status,
105 scoped_refptr<net::IOBuffer> buffer,
106 size_t buffer_size) {
107 callback.Run(mojo::ConvertTo<TransferStatus>(status));
110 void DeviceImpl::Close(const CloseCallback& callback) {
111 CloseHandle();
112 callback.Run();
115 void DeviceImpl::GetDeviceInfo(const GetDeviceInfoCallback& callback) {
116 if (!device_handle_) {
117 callback.Run(DeviceInfoPtr());
118 return;
121 // TODO(rockot/reillyg): Converting configuration info should be done in the
122 // TypeConverter, but GetConfiguration() is non-const so we do it here for
123 // now.
124 DeviceInfoPtr info = DeviceInfo::From(*device_handle_->GetDevice());
125 const std::vector<UsbConfigDescriptor>& configs =
126 device_handle_->GetDevice()->configurations();
127 info->configurations = mojo::Array<ConfigurationInfoPtr>::New(configs.size());
128 for (size_t i = 0; i < configs.size(); ++i) {
129 info->configurations[i] = ConfigurationInfo::From(configs[i]);
131 callback.Run(info.Pass());
134 void DeviceImpl::SetConfiguration(uint8_t value,
135 const SetConfigurationCallback& callback) {
136 if (!device_handle_) {
137 callback.Run(false);
138 return;
141 device_handle_->SetConfiguration(value, WrapMojoCallback(callback));
144 void DeviceImpl::ClaimInterface(uint8_t interface_number,
145 const ClaimInterfaceCallback& callback) {
146 if (!device_handle_) {
147 callback.Run(false);
148 return;
151 device_handle_->ClaimInterface(interface_number, WrapMojoCallback(callback));
154 void DeviceImpl::ReleaseInterface(uint8_t interface_number,
155 const ReleaseInterfaceCallback& callback) {
156 if (!device_handle_) {
157 callback.Run(false);
158 return;
161 callback.Run(device_handle_->ReleaseInterface(interface_number));
164 void DeviceImpl::SetInterfaceAlternateSetting(
165 uint8_t interface_number,
166 uint8_t alternate_setting,
167 const SetInterfaceAlternateSettingCallback& callback) {
168 if (!device_handle_) {
169 callback.Run(false);
170 return;
173 device_handle_->SetInterfaceAlternateSetting(
174 interface_number, alternate_setting, WrapMojoCallback(callback));
177 void DeviceImpl::Reset(const ResetCallback& callback) {
178 if (!device_handle_) {
179 callback.Run(false);
180 return;
183 device_handle_->ResetDevice(WrapMojoCallback(callback));
186 void DeviceImpl::ClearHalt(uint8_t endpoint,
187 const ClearHaltCallback& callback) {
188 if (!device_handle_) {
189 callback.Run(false);
190 return;
193 device_handle_->ClearHalt(endpoint, WrapMojoCallback(callback));
196 void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params,
197 uint32_t length,
198 uint32_t timeout,
199 const ControlTransferInCallback& callback) {
200 if (!device_handle_) {
201 callback.Run(TRANSFER_STATUS_ERROR, mojo::Array<uint8_t>());
202 return;
205 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length);
206 device_handle_->ControlTransfer(
207 USB_DIRECTION_INBOUND,
208 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type),
209 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient),
210 params->request, params->value, params->index, buffer, length, timeout,
211 base::Bind(&DeviceImpl::OnTransferIn, weak_factory_.GetWeakPtr(),
212 callback));
215 void DeviceImpl::ControlTransferOut(
216 ControlTransferParamsPtr params,
217 mojo::Array<uint8_t> data,
218 uint32_t timeout,
219 const ControlTransferOutCallback& callback) {
220 if (!device_handle_) {
221 callback.Run(TRANSFER_STATUS_ERROR);
222 return;
225 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size());
226 const std::vector<uint8_t>& storage = data.storage();
227 std::copy(storage.begin(), storage.end(), buffer->data());
228 device_handle_->ControlTransfer(
229 USB_DIRECTION_OUTBOUND,
230 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type),
231 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient),
232 params->request, params->value, params->index, buffer, data.size(),
233 timeout, base::Bind(&DeviceImpl::OnTransferOut,
234 weak_factory_.GetWeakPtr(), callback));
237 void DeviceImpl::GenericTransferIn(uint8_t endpoint_number,
238 uint32_t length,
239 uint32_t timeout,
240 const GenericTransferInCallback& callback) {
241 if (!device_handle_) {
242 callback.Run(TRANSFER_STATUS_ERROR, mojo::Array<uint8_t>());
243 return;
246 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length);
247 device_handle_->GenericTransfer(
248 USB_DIRECTION_INBOUND, endpoint_number, buffer, length, timeout,
249 base::Bind(&DeviceImpl::OnTransferIn, weak_factory_.GetWeakPtr(),
250 callback));
253 void DeviceImpl::GenericTransferOut(
254 uint8_t endpoint_number,
255 mojo::Array<uint8_t> data,
256 uint32_t timeout,
257 const GenericTransferOutCallback& callback) {
258 if (!device_handle_) {
259 callback.Run(TRANSFER_STATUS_ERROR);
260 return;
263 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size());
264 const std::vector<uint8_t>& storage = data.storage();
265 std::copy(storage.begin(), storage.end(), buffer->data());
266 device_handle_->GenericTransfer(
267 USB_DIRECTION_OUTBOUND, endpoint_number, buffer, data.size(), timeout,
268 base::Bind(&DeviceImpl::OnTransferOut, weak_factory_.GetWeakPtr(),
269 callback));
272 void DeviceImpl::IsochronousTransferIn(
273 uint8_t endpoint_number,
274 uint32_t num_packets,
275 uint32_t packet_length,
276 uint32_t timeout,
277 const IsochronousTransferInCallback& callback) {
278 if (!device_handle_) {
279 callback.Run(TRANSFER_STATUS_ERROR, mojo::Array<mojo::Array<uint8_t>>());
280 return;
283 size_t transfer_size = static_cast<size_t>(num_packets) * packet_length;
284 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(transfer_size);
285 device_handle_->IsochronousTransfer(
286 USB_DIRECTION_INBOUND, endpoint_number, buffer, transfer_size,
287 num_packets, packet_length, timeout,
288 base::Bind(&DeviceImpl::OnIsochronousTransferIn,
289 weak_factory_.GetWeakPtr(), callback, packet_length));
292 void DeviceImpl::IsochronousTransferOut(
293 uint8_t endpoint_number,
294 mojo::Array<mojo::Array<uint8_t>> packets,
295 uint32_t timeout,
296 const IsochronousTransferOutCallback& callback) {
297 if (!device_handle_) {
298 callback.Run(TRANSFER_STATUS_ERROR);
299 return;
302 uint32_t packet_size = 0;
303 for (size_t i = 0; i < packets.size(); ++i) {
304 packet_size =
305 std::max(packet_size, static_cast<uint32_t>(packets[i].size()));
307 size_t transfer_size = packet_size * packets.size();
308 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(transfer_size);
309 memset(buffer->data(), 0, transfer_size);
310 for (size_t i = 0; i < packets.size(); ++i) {
311 uint8_t* packet =
312 reinterpret_cast<uint8_t*>(&buffer->data()[i * packet_size]);
313 DCHECK_LE(packets[i].size(), static_cast<size_t>(packet_size));
314 memcpy(packet, packets[i].storage().data(), packets[i].size());
316 device_handle_->IsochronousTransfer(
317 USB_DIRECTION_OUTBOUND, endpoint_number, buffer, transfer_size,
318 static_cast<uint32_t>(packets.size()), packet_size, timeout,
319 base::Bind(&DeviceImpl::OnIsochronousTransferOut,
320 weak_factory_.GetWeakPtr(), callback));
323 } // namespace usb
324 } // namespace device