| 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/devices_app/usb/device_impl.h" | 5 #include "device/devices_app/usb/device_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "device/devices_app/usb/type_converters.h" | 10 #include "device/devices_app/usb/type_converters.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 } | 32 } |
| 33 | 33 |
| 34 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) { | 34 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) { |
| 35 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer( | 35 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer( |
| 36 std::max(static_cast<size_t>(1u), static_cast<size_t>(size))); | 36 std::max(static_cast<size_t>(1u), static_cast<size_t>(size))); |
| 37 return buffer; | 37 return buffer; |
| 38 } | 38 } |
| 39 | 39 |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 DeviceImpl::DeviceImpl(scoped_refptr<UsbDeviceHandle> device_handle, | 42 DeviceImpl::DeviceImpl(scoped_refptr<UsbDevice> device, |
| 43 mojo::InterfaceRequest<Device> request) | 43 mojo::InterfaceRequest<Device> request) |
| 44 : binding_(this, request.Pass()), | 44 : binding_(this, request.Pass()), device_(device), weak_factory_(this) {} |
| 45 device_handle_(device_handle), | |
| 46 weak_factory_(this) { | |
| 47 } | |
| 48 | 45 |
| 49 DeviceImpl::~DeviceImpl() { | 46 DeviceImpl::~DeviceImpl() { |
| 50 CloseHandle(); | 47 CloseHandle(); |
| 51 } | 48 } |
| 52 | 49 |
| 53 void DeviceImpl::CloseHandle() { | 50 void DeviceImpl::CloseHandle() { |
| 54 if (device_handle_) | 51 if (device_handle_) |
| 55 device_handle_->Close(); | 52 device_handle_->Close(); |
| 56 device_handle_ = nullptr; | 53 device_handle_ = nullptr; |
| 57 } | 54 } |
| 58 | 55 |
| 56 void DeviceImpl::OnOpen(const OpenCallback& callback, |
| 57 scoped_refptr<UsbDeviceHandle> handle) { |
| 58 device_handle_ = handle; |
| 59 callback.Run(handle ? OPEN_DEVICE_ERROR_OK : OPEN_DEVICE_ERROR_ACCESS_DENIED); |
| 60 } |
| 61 |
| 59 void DeviceImpl::OnTransferIn(const MojoTransferInCallback& callback, | 62 void DeviceImpl::OnTransferIn(const MojoTransferInCallback& callback, |
| 60 UsbTransferStatus status, | 63 UsbTransferStatus status, |
| 61 scoped_refptr<net::IOBuffer> buffer, | 64 scoped_refptr<net::IOBuffer> buffer, |
| 62 size_t buffer_size) { | 65 size_t buffer_size) { |
| 63 mojo::Array<uint8_t> data; | 66 mojo::Array<uint8_t> data; |
| 64 if (buffer) { | 67 if (buffer) { |
| 65 // TODO(rockot/reillyg): We should change UsbDeviceHandle to use a | 68 // TODO(rockot/reillyg): We should change UsbDeviceHandle to use a |
| 66 // std::vector<uint8_t> instead of net::IOBuffer. Then we could move | 69 // std::vector<uint8_t> instead of net::IOBuffer. Then we could move |
| 67 // instead of copy. | 70 // instead of copy. |
| 68 std::vector<uint8_t> bytes(buffer_size); | 71 std::vector<uint8_t> bytes(buffer_size); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 103 } |
| 101 | 104 |
| 102 void DeviceImpl::OnIsochronousTransferOut( | 105 void DeviceImpl::OnIsochronousTransferOut( |
| 103 const MojoTransferOutCallback& callback, | 106 const MojoTransferOutCallback& callback, |
| 104 UsbTransferStatus status, | 107 UsbTransferStatus status, |
| 105 scoped_refptr<net::IOBuffer> buffer, | 108 scoped_refptr<net::IOBuffer> buffer, |
| 106 size_t buffer_size) { | 109 size_t buffer_size) { |
| 107 callback.Run(mojo::ConvertTo<TransferStatus>(status)); | 110 callback.Run(mojo::ConvertTo<TransferStatus>(status)); |
| 108 } | 111 } |
| 109 | 112 |
| 113 void DeviceImpl::GetDeviceInfo(const GetDeviceInfoCallback& callback) { |
| 114 callback.Run(DeviceInfo::From(*device_)); |
| 115 } |
| 116 |
| 117 void DeviceImpl::GetConfiguration(const GetConfigurationCallback& callback) { |
| 118 const UsbConfigDescriptor* config = device_->GetActiveConfiguration(); |
| 119 callback.Run(config ? ConfigurationInfo::From(*config) : nullptr); |
| 120 } |
| 121 |
| 122 void DeviceImpl::Open(const OpenCallback& callback) { |
| 123 device_->Open( |
| 124 base::Bind(&DeviceImpl::OnOpen, weak_factory_.GetWeakPtr(), callback)); |
| 125 } |
| 126 |
| 110 void DeviceImpl::Close(const CloseCallback& callback) { | 127 void DeviceImpl::Close(const CloseCallback& callback) { |
| 111 CloseHandle(); | 128 CloseHandle(); |
| 112 callback.Run(); | 129 callback.Run(); |
| 113 } | 130 } |
| 114 | 131 |
| 115 void DeviceImpl::GetDeviceInfo(const GetDeviceInfoCallback& callback) { | |
| 116 if (!device_handle_) { | |
| 117 callback.Run(DeviceInfoPtr()); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 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]); | |
| 130 } | |
| 131 callback.Run(info.Pass()); | |
| 132 } | |
| 133 | |
| 134 void DeviceImpl::SetConfiguration(uint8_t value, | 132 void DeviceImpl::SetConfiguration(uint8_t value, |
| 135 const SetConfigurationCallback& callback) { | 133 const SetConfigurationCallback& callback) { |
| 136 if (!device_handle_) { | 134 if (!device_handle_) { |
| 137 callback.Run(false); | 135 callback.Run(false); |
| 138 return; | 136 return; |
| 139 } | 137 } |
| 140 | 138 |
| 141 device_handle_->SetConfiguration(value, WrapMojoCallback(callback)); | 139 device_handle_->SetConfiguration(value, WrapMojoCallback(callback)); |
| 142 } | 140 } |
| 143 | 141 |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 } | 317 } |
| 320 device_handle_->IsochronousTransfer( | 318 device_handle_->IsochronousTransfer( |
| 321 USB_DIRECTION_OUTBOUND, endpoint_address, buffer, transfer_size, | 319 USB_DIRECTION_OUTBOUND, endpoint_address, buffer, transfer_size, |
| 322 static_cast<uint32_t>(packets.size()), packet_size, timeout, | 320 static_cast<uint32_t>(packets.size()), packet_size, timeout, |
| 323 base::Bind(&DeviceImpl::OnIsochronousTransferOut, | 321 base::Bind(&DeviceImpl::OnIsochronousTransferOut, |
| 324 weak_factory_.GetWeakPtr(), callback)); | 322 weak_factory_.GetWeakPtr(), callback)); |
| 325 } | 323 } |
| 326 | 324 |
| 327 } // namespace usb | 325 } // namespace usb |
| 328 } // namespace device | 326 } // namespace device |
| OLD | NEW |