Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(708)

Unified Diff: chrome/browser/extensions/api/usb/usb_api.cc

Issue 16316004: Separate usb device handle from usb device. (deprecate) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix the threading mess Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/extensions/api/usb/usb_api.h ('k') | chrome/browser/extensions/api/usb/usb_apitest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/extensions/api/usb/usb_api.cc
diff --git a/chrome/browser/extensions/api/usb/usb_api.cc b/chrome/browser/extensions/api/usb/usb_api.cc
index 29aa7c3fb85ad161e25c914c54e2fdc1ed42f594..cc928e7bba803abdb891e2f33d3955e2caafee7a 100644
--- a/chrome/browser/extensions/api/usb/usb_api.cc
+++ b/chrome/browser/extensions/api/usb/usb_api.cc
@@ -23,6 +23,8 @@ namespace ListInterfaces = extensions::api::usb::ListInterfaces;
namespace CloseDevice = extensions::api::usb::CloseDevice;
namespace ControlTransfer = extensions::api::usb::ControlTransfer;
namespace FindDevices = extensions::api::usb::FindDevices;
+namespace GetDevices = extensions::api::usb::GetDevices;
+namespace OpenDevice = extensions::api::usb::OpenDevice;
namespace InterruptTransfer = extensions::api::usb::InterruptTransfer;
namespace IsochronousTransfer = extensions::api::usb::IsochronousTransfer;
namespace ReleaseInterface = extensions::api::usb::ReleaseInterface;
@@ -36,6 +38,7 @@ using std::string;
using std::vector;
using usb::ControlTransferInfo;
using usb::Device;
+using usb::DeviceHandle;
using usb::Direction;
using usb::EndpointDescriptor;
using usb::GenericTransferInfo;
@@ -76,12 +79,12 @@ static const char kErrorMalformedParameters[] = "Error parsing parameters.";
static const char kErrorNoDevice[] = "No such device.";
static const char kErrorPermissionDenied[] =
"Permission to access device was denied";
-static const char kErrorInvalidTransferLength[] = "Transfer length must be a "
- "positive number less than 104,857,600.";
-static const char kErrorInvalidNumberOfPackets[] = "Number of packets must be "
- "a positive number less than 4,194,304.";
-static const char kErrorInvalidPacketLength[] = "Packet length must be a "
- "positive number less than 65,536.";
+static const char kErrorInvalidTransferLength[] =
+ "Transfer length must be a positive number less than 104,857,600.";
+static const char kErrorInvalidNumberOfPackets[] =
+ "Number of packets must be a positive number less than 4,194,304.";
+static const char kErrorInvalidPacketLength[] =
+ "Packet length must be a positive number less than 65,536.";
static const char kErrorResetDevice[] =
"Error resetting the device. The device has been closed.";
@@ -89,7 +92,7 @@ static const size_t kMaxTransferLength = 100 * 1024 * 1024;
static const int kMaxPackets = 4 * 1024 * 1024;
static const int kMaxPacketLength = 64 * 1024;
-static UsbDevice* device_for_test_ = NULL;
+static UsbDeviceHandle* device_for_test_ = NULL;
static bool ConvertDirectionToApi(const UsbEndpointDirection& input,
Direction* output) {
@@ -151,7 +154,7 @@ static bool ConvertTransferTypeToApi(
}
static bool ConvertUsageTypeToApi(const UsbUsageType& input,
- extensions::api::usb::UsageType* output) {
+ extensions::api::usb::UsageType* output) {
switch (input) {
case USB_USAGE_DATA:
*output = usb::USAGE_TYPE_DATA;
@@ -184,19 +187,19 @@ static bool ConvertDirection(const Direction& input,
}
static bool ConvertRequestType(const RequestType& input,
- UsbDevice::TransferRequestType* output) {
+ UsbDeviceHandle::TransferRequestType* output) {
switch (input) {
case usb::REQUEST_TYPE_STANDARD:
- *output = UsbDevice::STANDARD;
+ *output = UsbDeviceHandle::STANDARD;
return true;
case usb::REQUEST_TYPE_CLASS:
- *output = UsbDevice::CLASS;
+ *output = UsbDeviceHandle::CLASS;
return true;
case usb::REQUEST_TYPE_VENDOR:
- *output = UsbDevice::VENDOR;
+ *output = UsbDeviceHandle::VENDOR;
return true;
case usb::REQUEST_TYPE_RESERVED:
- *output = UsbDevice::RESERVED;
+ *output = UsbDeviceHandle::RESERVED;
return true;
default:
NOTREACHED();
@@ -205,19 +208,19 @@ static bool ConvertRequestType(const RequestType& input,
}
static bool ConvertRecipient(const Recipient& input,
- UsbDevice::TransferRecipient* output) {
+ UsbDeviceHandle::TransferRecipient* output) {
switch (input) {
case usb::RECIPIENT_DEVICE:
- *output = UsbDevice::DEVICE;
+ *output = UsbDeviceHandle::DEVICE;
return true;
case usb::RECIPIENT_INTERFACE:
- *output = UsbDevice::INTERFACE;
+ *output = UsbDeviceHandle::INTERFACE;
return true;
case usb::RECIPIENT_ENDPOINT:
- *output = UsbDevice::ENDPOINT;
+ *output = UsbDeviceHandle::ENDPOINT;
return true;
case usb::RECIPIENT_OTHER:
- *output = UsbDevice::OTHER;
+ *output = UsbDeviceHandle::OTHER;
return true;
default:
NOTREACHED();
@@ -225,7 +228,7 @@ static bool ConvertRecipient(const Recipient& input,
}
}
-template<class T>
+template <class T>
static bool GetTransferSize(const T& input, size_t* output) {
if (input.direction == usb::DIRECTION_IN) {
const int* length = input.length.get();
@@ -243,7 +246,7 @@ static bool GetTransferSize(const T& input, size_t* output) {
return false;
}
-template<class T>
+template <class T>
static scoped_refptr<net::IOBuffer> CreateBufferForTransfer(
const T& input, UsbEndpointDirection direction, size_t size) {
@@ -253,8 +256,8 @@ static scoped_refptr<net::IOBuffer> CreateBufferForTransfer(
// Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This
// is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer
// cannot represent a zero-length buffer, while an URB can.
- scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max(
- static_cast<size_t>(1), size));
+ scoped_refptr<net::IOBuffer> buffer =
+ new net::IOBuffer(std::max(static_cast<size_t>(1), size));
if (direction == USB_DIRECTION_INBOUND) {
return buffer;
@@ -299,23 +302,36 @@ static base::DictionaryValue* CreateTransferInfo(
size_t length) {
base::DictionaryValue* result = new base::DictionaryValue();
result->SetInteger(kResultCodeKey, status);
- result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer(data->data(),
- length));
+ if (data.get()) {
+ result->Set(
+ kDataKey,
+ base::BinaryValue::CreateWithCopiedBuffer(data->data(), length));
+ }
return result;
}
-static base::Value* PopulateDevice(int handle, int vendor_id, int product_id) {
+static base::Value* PopulateDevice(int device_id, int vendor_id,
+ int product_id) {
Device device;
- device.handle = handle;
+ device.device = device_id;
device.vendor_id = vendor_id;
device.product_id = product_id;
return device.ToValue().release();
}
-static base::Value* PopulateInterfaceDescriptor(int interface_number,
- int alternate_setting, int interface_class, int interface_subclass,
- int interface_protocol,
- std::vector<linked_ptr<EndpointDescriptor> >* endpoints) {
+static base::Value* PopulateDeviceHandle(int handle, int vendor_id,
+ int product_id) {
+ DeviceHandle device_handle;
+ device_handle.handle = handle;
+ device_handle.vendor_id = vendor_id;
+ device_handle.product_id = product_id;
+ return device_handle.ToValue().release();
+}
+
+static base::Value* PopulateInterfaceDescriptor(
+ int interface_number, int alternate_setting, int interface_class,
+ int interface_subclass, int interface_protocol,
+ std::vector<linked_ptr<EndpointDescriptor> >* endpoints) {
InterfaceDescriptor descriptor;
descriptor.interface_number = interface_number;
descriptor.alternate_setting = alternate_setting;
@@ -330,25 +346,29 @@ static base::Value* PopulateInterfaceDescriptor(int interface_number,
namespace extensions {
-UsbAsyncApiFunction::UsbAsyncApiFunction()
- : manager_(NULL) {
-}
+UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) {}
-UsbAsyncApiFunction::~UsbAsyncApiFunction() {
-}
+UsbAsyncApiFunction::~UsbAsyncApiFunction() {}
bool UsbAsyncApiFunction::PrePrepare() {
manager_ = ApiResourceManager<UsbDeviceResource>::Get(profile());
return manager_ != NULL;
}
-bool UsbAsyncApiFunction::Respond() {
- return error_.empty();
-}
+bool UsbAsyncApiFunction::Respond() { return error_.empty(); }
UsbDeviceResource* UsbAsyncApiFunction::GetUsbDeviceResource(
int api_resource_id) {
- return manager_->Get(extension_->id(), api_resource_id);
+ UsbDeviceResource* resource =
+ manager_->Get(extension_->id(), api_resource_id);
+
+ if (resource == NULL) return NULL;
+
+ if (device_for_test_) {
+ return resource;
+ }
+
+ return resource;
}
void UsbAsyncApiFunction::RemoveUsbDeviceResource(int api_resource_id) {
@@ -377,62 +397,70 @@ void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status,
bool UsbAsyncApiTransferFunction::ConvertDirectionSafely(
const Direction& input, UsbEndpointDirection* output) {
const bool converted = ConvertDirection(input, output);
- if (!converted)
- SetError(kErrorConvertDirection);
+ if (!converted) SetError(kErrorConvertDirection);
return converted;
}
bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely(
- const RequestType& input, UsbDevice::TransferRequestType* output) {
+ const RequestType& input, UsbDeviceHandle::TransferRequestType* output) {
const bool converted = ConvertRequestType(input, output);
- if (!converted)
- SetError(kErrorConvertRequestType);
+ if (!converted) SetError(kErrorConvertRequestType);
return converted;
}
bool UsbAsyncApiTransferFunction::ConvertRecipientSafely(
- const Recipient& input, UsbDevice::TransferRecipient* output) {
+ const Recipient& input, UsbDeviceHandle::TransferRecipient* output) {
const bool converted = ConvertRecipient(input, output);
- if (!converted)
- SetError(kErrorConvertRecipient);
+ if (!converted) SetError(kErrorConvertRecipient);
return converted;
}
-UsbFindDevicesFunction::UsbFindDevicesFunction() {}
+UsbGetDevicesFunction::UsbGetDevicesFunction()
+ : vendor_id_(0),
+ product_id_(0),
+ interface_id_(UsbDevicePermissionData::ANY_INTERFACE),
+ service_(NULL) {}
-UsbFindDevicesFunction::~UsbFindDevicesFunction() {}
+UsbGetDevicesFunction::~UsbGetDevicesFunction() {}
-void UsbFindDevicesFunction::SetDeviceForTest(UsbDevice* device) {
+void UsbGetDevicesFunction::SetDeviceForTest(UsbDeviceHandle* device) {
device_for_test_ = device;
}
-bool UsbFindDevicesFunction::Prepare() {
- parameters_ = FindDevices::Params::Create(*args_);
- EXTENSION_FUNCTION_VALIDATE(parameters_.get());
+bool UsbGetDevicesFunction::PrePrepare() {
+ if (device_for_test_)
+ return UsbAsyncApiFunction::PrePrepare();
+ service_ = UsbServiceFactory::GetForProfile(profile());
+ if (service_ == NULL) {
+ LOG(WARNING) << "Could not get UsbService for active profile.";
+ SetError(kErrorNoDevice);
+ return false;
+ }
+ return UsbAsyncApiFunction::PrePrepare();
+}
+
+bool UsbGetDevicesFunction::Prepare() {
+ scoped_ptr<GetDevices::Params> parameters =
+ GetDevices::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(parameters.get());
+ vendor_id_ = parameters->options.vendor_id;
+ product_id_ = parameters->options.product_id;
+ if (parameters->options.interface_id.get())
+ interface_id_ = *parameters->options.interface_id;
return true;
}
-void UsbFindDevicesFunction::AsyncWorkStart() {
+void UsbGetDevicesFunction::AsyncWorkStart() {
result_.reset(new base::ListValue());
if (device_for_test_) {
- UsbDeviceResource* const resource = new UsbDeviceResource(
- extension_->id(),
- device_for_test_);
-
- Device device;
- result_->Append(PopulateDevice(manager_->Add(resource), 0, 0));
+ result_->Append(PopulateDevice(device_for_test_->device(), 0, 0));
SetResult(result_.release());
AsyncWorkCompleted();
return;
}
- const uint16_t vendor_id = parameters_->options.vendor_id;
- const uint16_t product_id = parameters_->options.product_id;
- int interface_id = parameters_->options.interface_id.get() ?
- *parameters_->options.interface_id.get() :
- UsbDevicePermissionData::ANY_INTERFACE;
- UsbDevicePermission::CheckParam param(vendor_id, product_id, interface_id);
+ UsbDevicePermission::CheckParam param(vendor_id_, product_id_, interface_id_);
if (!PermissionsData::CheckAPIPermissionWithParam(
GetExtension(), APIPermission::kUsbDevice, &param)) {
LOG(WARNING) << "Insufficient permissions to access device.";
@@ -440,32 +468,123 @@ void UsbFindDevicesFunction::AsyncWorkStart() {
return;
}
- UsbService* const service = UsbServiceFactory::GetInstance()->GetForProfile(
- profile());
- if (!service) {
- LOG(WARNING) << "Could not get UsbService for active profile.";
- CompleteWithError(kErrorNoDevice);
- return;
+ BrowserThread::PostTask(
+ BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(&UsbService::FindDevices, base::Unretained(service_),
+ vendor_id_, product_id_, interface_id_, &devices_,
+ base::Bind(&UsbGetDevicesFunction::OnDevicesFound, this)));
+}
+
+void UsbGetDevicesFunction::OnDevicesFound() {
+ // Redirect this to virtual method.
+ OnCompleted();
+}
+
+void UsbGetDevicesFunction::OnCompleted() {
+ for (size_t i = 0; i < devices_.size(); ++i) {
+ result_->Append(PopulateDevice(devices_[i], vendor_id_, product_id_));
}
- service->FindDevices(vendor_id, product_id, interface_id, &devices_,
- base::Bind(&UsbFindDevicesFunction::OnCompleted, this));
+ SetResult(result_.release());
+ AsyncWorkCompleted();
+}
+
+UsbFindDevicesFunction::UsbFindDevicesFunction() : UsbGetDevicesFunction() {}
+
+UsbFindDevicesFunction::~UsbFindDevicesFunction() {}
+
+bool UsbFindDevicesFunction::Prepare() {
+ scoped_ptr<FindDevices::Params> parameters =
+ FindDevices::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(parameters.get());
+ vendor_id_ = parameters->options.vendor_id;
+ product_id_ = parameters->options.product_id;
+ if (parameters->options.interface_id.get())
+ interface_id_ = *parameters->options.interface_id;
+ return true;
}
void UsbFindDevicesFunction::OnCompleted() {
for (size_t i = 0; i < devices_.size(); ++i) {
- UsbDevice* const device = devices_[i].get();
+ scoped_refptr<UsbDeviceHandle> handle = service_->OpenDevice(devices_[i]);
+ if (handle.get()) handles_.push_back(handle);
+ }
+ BrowserThread::PostTask(
+ BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(&UsbFindDevicesFunction::OpenDevices, this));
+}
+
+void UsbFindDevicesFunction::OpenDevices() {
+ for (size_t i = 0; i < handles_.size(); ++i) {
UsbDeviceResource* const resource =
- new UsbDeviceResource(extension_->id(), device);
+ new UsbDeviceResource(extension_->id(), handles_[i]);
+ result_->Append(
+ PopulateDeviceHandle(manager_->Add(resource), handles_[i]->vendor_id(),
+ handles_[i]->product_id()));
+ }
+ SetResult(result_.release());
+ AsyncWorkCompleted();
+}
+
+UsbOpenDeviceFunction::UsbOpenDeviceFunction() : service_(NULL) {}
- Device js_device;
- result_->Append(PopulateDevice(manager_->Add(resource),
- parameters_->options.vendor_id,
- parameters_->options.product_id));
+UsbOpenDeviceFunction::~UsbOpenDeviceFunction() {}
+
+bool UsbOpenDeviceFunction::PrePrepare() {
+ if (device_for_test_)
+ return UsbAsyncApiFunction::PrePrepare();
+ service_ = UsbServiceFactory::GetForProfile(profile());
+ if (service_ == NULL) {
+ LOG(WARNING) << "Could not get UsbService for active profile.";
+ SetError(kErrorNoDevice);
+ return false;
}
+ return UsbAsyncApiFunction::PrePrepare();
+}
- SetResult(result_.release());
+bool UsbOpenDeviceFunction::Prepare() {
+ parameters_ = OpenDevice::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(parameters_.get());
+ return true;
+}
+
+void UsbOpenDeviceFunction::AsyncWorkStart() {
+ if (device_for_test_) {
+ UsbDeviceResource* const resource =
+ new UsbDeviceResource(extension_->id(), device_for_test_);
+ SetResult(PopulateDeviceHandle(manager_->Add(resource),
+ device_for_test_->vendor_id(),
+ device_for_test_->product_id()));
+ AsyncWorkCompleted();
+ return;
+ }
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
+ base::Bind(&UsbOpenDeviceFunction::OpenDevice, this));
+}
+
+void UsbOpenDeviceFunction::OpenDevice() {
+ scoped_refptr<UsbDeviceHandle> handle =
+ service_->OpenDevice(parameters_->device.device);
+ if (!handle.get()) {
+ CompleteWithError(kErrorDisconnect);
+ return;
+ }
+ // Pass to IO thread to use api resource manager.
+ BrowserThread::PostTask(
+ BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(&UsbOpenDeviceFunction::OnCompleted, this, handle));
+}
+
+void UsbOpenDeviceFunction::OnCompleted(scoped_refptr<UsbDeviceHandle> handle) {
+ UsbDeviceResource* const resource =
+ new UsbDeviceResource(extension_->id(), handle);
+ SetResult(PopulateDeviceHandle(manager_->Add(resource), handle->vendor_id(),
+ handle->product_id()));
AsyncWorkCompleted();
+ return;
}
UsbListInterfacesFunction::UsbListInterfacesFunction() {}
@@ -479,15 +598,15 @@ bool UsbListInterfacesFunction::Prepare() {
}
void UsbListInterfacesFunction::AsyncWorkStart() {
- UsbDeviceResource* const resource = GetUsbDeviceResource(
- parameters_->device.handle);
+ UsbDeviceResource* const resource =
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
}
config_ = new UsbConfigDescriptor();
- resource->device()->ListInterfaces(
+ resource->ListInterfaces(
config_.get(), base::Bind(&UsbListInterfacesFunction::OnCompleted, this));
}
@@ -501,17 +620,17 @@ void UsbListInterfacesFunction::OnCompleted(bool success) {
result_.reset(new base::ListValue());
for (size_t i = 0, numInterfaces = config_->GetNumInterfaces();
- i < numInterfaces; ++i) {
+ i < numInterfaces; ++i) {
scoped_refptr<const UsbInterface> usbInterface(config_->GetInterface(i));
for (size_t j = 0, numDescriptors = usbInterface->GetNumAltSettings();
- j < numDescriptors; ++j) {
- scoped_refptr<const UsbInterfaceDescriptor> descriptor
- = usbInterface->GetAltSetting(j);
+ j < numDescriptors; ++j) {
+ scoped_refptr<const UsbInterfaceDescriptor> descriptor =
+ usbInterface->GetAltSetting(j);
std::vector<linked_ptr<EndpointDescriptor> > endpoints;
for (size_t k = 0, numEndpoints = descriptor->GetNumEndpoints();
- k < numEndpoints; k++) {
- scoped_refptr<const UsbEndpointDescriptor> endpoint
- = descriptor->GetEndpoint(k);
+ k < numEndpoints; ++k) {
+ scoped_refptr<const UsbEndpointDescriptor> endpoint =
+ descriptor->GetEndpoint(k);
linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor());
TransferType type;
@@ -604,21 +723,18 @@ bool UsbCloseDeviceFunction::Prepare() {
}
void UsbCloseDeviceFunction::AsyncWorkStart() {
- UsbDeviceResource* const resource = GetUsbDeviceResource(
- parameters_->device.handle);
+ UsbDeviceResource* const resource =
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
}
- resource->device()->Close(base::Bind(&UsbCloseDeviceFunction::OnCompleted,
- this));
- RemoveUsbDeviceResource(parameters_->device.handle);
+ resource->Close(base::Bind(&UsbCloseDeviceFunction::OnCompleted, this));
+ RemoveUsbDeviceResource(parameters_->handle.handle);
}
-void UsbCloseDeviceFunction::OnCompleted() {
- AsyncWorkCompleted();
-}
+void UsbCloseDeviceFunction::OnCompleted() { AsyncWorkCompleted(); }
UsbClaimInterfaceFunction::UsbClaimInterfaceFunction() {}
@@ -632,13 +748,14 @@ bool UsbClaimInterfaceFunction::Prepare() {
void UsbClaimInterfaceFunction::AsyncWorkStart() {
UsbDeviceResource* resource =
- GetUsbDeviceResource(parameters_->device.handle);
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
}
- resource->device()->ClaimInterface(parameters_->interface_number,
+ resource->ClaimInterface(
+ parameters_->interface_number,
base::Bind(&UsbClaimInterfaceFunction::OnCompleted, this));
}
@@ -660,13 +777,14 @@ bool UsbReleaseInterfaceFunction::Prepare() {
void UsbReleaseInterfaceFunction::AsyncWorkStart() {
UsbDeviceResource* resource =
- GetUsbDeviceResource(parameters_->device.handle);
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
}
- resource->device()->ReleaseInterface(parameters_->interface_number,
+ resource->ReleaseInterface(
+ parameters_->interface_number,
base::Bind(&UsbReleaseInterfaceFunction::OnCompleted, this));
}
@@ -690,15 +808,14 @@ bool UsbSetInterfaceAlternateSettingFunction::Prepare() {
void UsbSetInterfaceAlternateSettingFunction::AsyncWorkStart() {
UsbDeviceResource* resource =
- GetUsbDeviceResource(parameters_->device.handle);
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
}
- resource->device()->SetInterfaceAlternateSetting(
- parameters_->interface_number,
- parameters_->alternate_setting,
+ resource->SetInterfaceAlternateSetting(
+ parameters_->interface_number, parameters_->alternate_setting,
base::Bind(&UsbSetInterfaceAlternateSettingFunction::OnCompleted, this));
}
@@ -719,8 +836,8 @@ bool UsbControlTransferFunction::Prepare() {
}
void UsbControlTransferFunction::AsyncWorkStart() {
- UsbDeviceResource* const resource = GetUsbDeviceResource(
- parameters_->device.handle);
+ UsbDeviceResource* const resource =
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
@@ -729,8 +846,8 @@ void UsbControlTransferFunction::AsyncWorkStart() {
const ControlTransferInfo& transfer = parameters_->transfer_info;
UsbEndpointDirection direction;
- UsbDevice::TransferRequestType request_type;
- UsbDevice::TransferRecipient recipient;
+ UsbDeviceHandle::TransferRequestType request_type;
+ UsbDeviceHandle::TransferRecipient recipient;
size_t size = 0;
if (!ConvertDirectionSafely(transfer.direction, &direction) ||
@@ -745,23 +862,16 @@ void UsbControlTransferFunction::AsyncWorkStart() {
return;
}
- scoped_refptr<net::IOBuffer> buffer = CreateBufferForTransfer(
- transfer, direction, size);
+ scoped_refptr<net::IOBuffer> buffer =
+ CreateBufferForTransfer(transfer, direction, size);
if (!buffer.get()) {
CompleteWithError(kErrorMalformedParameters);
return;
}
- resource->device()->ControlTransfer(
- direction,
- request_type,
- recipient,
- transfer.request,
- transfer.value,
- transfer.index,
- buffer.get(),
- size,
- 0,
+ resource->ControlTransfer(
+ direction, request_type, recipient, transfer.request, transfer.value,
+ transfer.index, buffer.get(), size, 0,
base::Bind(&UsbControlTransferFunction::OnCompleted, this));
}
@@ -776,8 +886,8 @@ bool UsbBulkTransferFunction::Prepare() {
}
void UsbBulkTransferFunction::AsyncWorkStart() {
- UsbDeviceResource* const resource = GetUsbDeviceResource(
- parameters_->device.handle);
+ UsbDeviceResource* const resource =
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
@@ -798,20 +908,16 @@ void UsbBulkTransferFunction::AsyncWorkStart() {
return;
}
- scoped_refptr<net::IOBuffer> buffer = CreateBufferForTransfer(
- transfer, direction, size);
+ scoped_refptr<net::IOBuffer> buffer =
+ CreateBufferForTransfer(transfer, direction, size);
if (!buffer.get()) {
CompleteWithError(kErrorMalformedParameters);
return;
}
- resource->device()
- ->BulkTransfer(direction,
- transfer.endpoint,
- buffer.get(),
- size,
- 0,
- base::Bind(&UsbBulkTransferFunction::OnCompleted, this));
+ resource->BulkTransfer(
+ direction, transfer.endpoint, buffer.get(), size, 0,
+ base::Bind(&UsbBulkTransferFunction::OnCompleted, this));
}
UsbInterruptTransferFunction::UsbInterruptTransferFunction() {}
@@ -825,8 +931,8 @@ bool UsbInterruptTransferFunction::Prepare() {
}
void UsbInterruptTransferFunction::AsyncWorkStart() {
- UsbDeviceResource* const resource = GetUsbDeviceResource(
- parameters_->device.handle);
+ UsbDeviceResource* const resource =
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
@@ -847,14 +953,14 @@ void UsbInterruptTransferFunction::AsyncWorkStart() {
return;
}
- scoped_refptr<net::IOBuffer> buffer = CreateBufferForTransfer(
- transfer, direction, size);
+ scoped_refptr<net::IOBuffer> buffer =
+ CreateBufferForTransfer(transfer, direction, size);
if (!buffer.get()) {
CompleteWithError(kErrorMalformedParameters);
return;
}
- resource->device()->InterruptTransfer(
+ resource->InterruptTransfer(
direction,
transfer.endpoint,
buffer.get(),
@@ -874,8 +980,8 @@ bool UsbIsochronousTransferFunction::Prepare() {
}
void UsbIsochronousTransferFunction::AsyncWorkStart() {
- UsbDeviceResource* const resource = GetUsbDeviceResource(
- parameters_->device.handle);
+ UsbDeviceResource* const resource =
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
@@ -912,14 +1018,14 @@ void UsbIsochronousTransferFunction::AsyncWorkStart() {
return;
}
- scoped_refptr<net::IOBuffer> buffer = CreateBufferForTransfer(
- generic_transfer, direction, size);
+ scoped_refptr<net::IOBuffer> buffer =
+ CreateBufferForTransfer(generic_transfer, direction, size);
if (!buffer.get()) {
CompleteWithError(kErrorMalformedParameters);
return;
}
- resource->device()->IsochronousTransfer(
+ resource->IsochronousTransfer(
direction,
generic_transfer.endpoint,
buffer.get(),
@@ -941,45 +1047,45 @@ bool UsbResetDeviceFunction::Prepare() {
}
void UsbResetDeviceFunction::AsyncWorkStart() {
- UsbDeviceResource* const resource = GetUsbDeviceResource(
- parameters_->device.handle);
+ UsbDeviceResource* const resource =
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
}
- BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
- base::Bind(&UsbResetDeviceFunction::OnStartResest,
- this, resource));
+ BrowserThread::PostTask(
+ BrowserThread::FILE, FROM_HERE,
+ base::Bind(&UsbResetDeviceFunction::OnStartResest, this, resource));
}
void UsbResetDeviceFunction::OnStartResest(UsbDeviceResource* resource) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- resource->device()->ResetDevice(
+ resource->ResetDevice(
base::Bind(&UsbResetDeviceFunction::OnCompletedFileThread, this));
}
void UsbResetDeviceFunction::OnCompletedFileThread(bool success) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
- base::Bind(&UsbResetDeviceFunction::OnCompleted,
- this, success));
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&UsbResetDeviceFunction::OnCompleted, this, success));
return;
}
void UsbResetDeviceFunction::OnCompleted(bool success) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!success) {
- UsbDeviceResource* const resource = GetUsbDeviceResource(
- parameters_->device.handle);
+ UsbDeviceResource* const resource =
+ GetUsbDeviceResource(parameters_->handle.handle);
if (!resource) {
CompleteWithError(kErrorNoDevice);
return;
}
// Close the device now because the handle is invalid after an
// unsuccessful reset.
- resource->device()->Close(
- base::Bind(&UsbResetDeviceFunction::OnError, this));
+ resource->Close(base::Bind(&UsbResetDeviceFunction::OnError, this));
+ RemoveUsbDeviceResource(parameters_->handle.handle);
return;
}
SetResult(Value::CreateBooleanValue(true));
@@ -987,7 +1093,6 @@ void UsbResetDeviceFunction::OnCompleted(bool success) {
}
void UsbResetDeviceFunction::OnError() {
- RemoveUsbDeviceResource(parameters_->device.handle);
SetError(kErrorResetDevice);
SetResult(Value::CreateBooleanValue(false));
AsyncWorkCompleted();
« no previous file with comments | « chrome/browser/extensions/api/usb/usb_api.h ('k') | chrome/browser/extensions/api/usb/usb_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698