| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/usb/usb_api.h" | 5 #include "extensions/browser/api/usb/usb_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 } | 181 } |
| 182 return false; | 182 return false; |
| 183 } | 183 } |
| 184 | 184 |
| 185 template <class T> | 185 template <class T> |
| 186 scoped_refptr<net::IOBuffer> CreateBufferForTransfer( | 186 scoped_refptr<net::IOBuffer> CreateBufferForTransfer( |
| 187 const T& input, | 187 const T& input, |
| 188 UsbEndpointDirection direction, | 188 UsbEndpointDirection direction, |
| 189 size_t size) { | 189 size_t size) { |
| 190 if (size >= kMaxTransferLength) | 190 if (size >= kMaxTransferLength) |
| 191 return NULL; | 191 return nullptr; |
| 192 | 192 |
| 193 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This | 193 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This |
| 194 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer | 194 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer |
| 195 // cannot represent a zero-length buffer, while an URB can. | 195 // cannot represent a zero-length buffer, while an URB can. |
| 196 scoped_refptr<net::IOBuffer> buffer = | 196 scoped_refptr<net::IOBuffer> buffer = |
| 197 new net::IOBuffer(std::max(static_cast<size_t>(1), size)); | 197 new net::IOBuffer(std::max(static_cast<size_t>(1), size)); |
| 198 | 198 |
| 199 if (direction == device::USB_DIRECTION_INBOUND) { | 199 if (direction == device::USB_DIRECTION_INBOUND) { |
| 200 return buffer; | 200 return buffer; |
| 201 } else if (direction == device::USB_DIRECTION_OUTBOUND) { | 201 } else if (direction == device::USB_DIRECTION_OUTBOUND) { |
| 202 if (input.data.get() && size <= input.data->size()) { | 202 if (input.data.get() && size <= input.data->size()) { |
| 203 memcpy(buffer->data(), input.data->data(), size); | 203 memcpy(buffer->data(), input.data->data(), size); |
| 204 return buffer; | 204 return buffer; |
| 205 } | 205 } |
| 206 } | 206 } |
| 207 NOTREACHED(); | 207 NOTREACHED(); |
| 208 return NULL; | 208 return nullptr; |
| 209 } | 209 } |
| 210 | 210 |
| 211 const char* ConvertTransferStatusToApi(const UsbTransferStatus status) { | 211 const char* ConvertTransferStatusToApi(const UsbTransferStatus status) { |
| 212 switch (status) { | 212 switch (status) { |
| 213 case device::USB_TRANSFER_COMPLETED: | 213 case device::USB_TRANSFER_COMPLETED: |
| 214 return ""; | 214 return ""; |
| 215 case device::USB_TRANSFER_ERROR: | 215 case device::USB_TRANSFER_ERROR: |
| 216 return kErrorGeneric; | 216 return kErrorGeneric; |
| 217 case device::USB_TRANSFER_TIMEOUT: | 217 case device::USB_TRANSFER_TIMEOUT: |
| 218 return kErrorTimeout; | 218 return kErrorTimeout; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 output->extra_data = | 416 output->extra_data = |
| 417 std::string(reinterpret_cast<const char*>(&input.extra_data[0]), | 417 std::string(reinterpret_cast<const char*>(&input.extra_data[0]), |
| 418 input.extra_data.size()); | 418 input.extra_data.size()); |
| 419 } | 419 } |
| 420 } | 420 } |
| 421 | 421 |
| 422 } // namespace | 422 } // namespace |
| 423 | 423 |
| 424 namespace extensions { | 424 namespace extensions { |
| 425 | 425 |
| 426 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) { | 426 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(nullptr) { |
| 427 } | 427 } |
| 428 | 428 |
| 429 UsbAsyncApiFunction::~UsbAsyncApiFunction() { | 429 UsbAsyncApiFunction::~UsbAsyncApiFunction() { |
| 430 } | 430 } |
| 431 | 431 |
| 432 bool UsbAsyncApiFunction::PrePrepare() { | 432 bool UsbAsyncApiFunction::PrePrepare() { |
| 433 manager_ = ApiResourceManager<UsbDeviceResource>::Get(browser_context()); | 433 manager_ = ApiResourceManager<UsbDeviceResource>::Get(browser_context()); |
| 434 set_work_thread_id(BrowserThread::FILE); | 434 set_work_thread_id(BrowserThread::FILE); |
| 435 return manager_ != NULL; | 435 return manager_ != nullptr; |
| 436 } | 436 } |
| 437 | 437 |
| 438 bool UsbAsyncApiFunction::Respond() { | 438 bool UsbAsyncApiFunction::Respond() { |
| 439 return error_.empty(); | 439 return error_.empty(); |
| 440 } | 440 } |
| 441 | 441 |
| 442 // static | 442 // static |
| 443 void UsbAsyncApiFunction::CreateDeviceFilter(const usb::DeviceFilter& input, | 443 void UsbAsyncApiFunction::CreateDeviceFilter(const usb::DeviceFilter& input, |
| 444 UsbDeviceFilter* output) { | 444 UsbDeviceFilter* output) { |
| 445 if (input.vendor_id) { | 445 if (input.vendor_id) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 466 UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | 466 UsbDevicePermissionData::UNSPECIFIED_INTERFACE); |
| 467 return extension()->permissions_data()->CheckAPIPermissionWithParam( | 467 return extension()->permissions_data()->CheckAPIPermissionWithParam( |
| 468 APIPermission::kUsbDevice, ¶m); | 468 APIPermission::kUsbDevice, ¶m); |
| 469 } | 469 } |
| 470 | 470 |
| 471 scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrCompleteWithError( | 471 scoped_refptr<UsbDevice> UsbAsyncApiFunction::GetDeviceOrCompleteWithError( |
| 472 const Device& input_device) { | 472 const Device& input_device) { |
| 473 UsbService* service = device::DeviceClient::Get()->GetUsbService(); | 473 UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| 474 if (!service) { | 474 if (!service) { |
| 475 CompleteWithError(kErrorInitService); | 475 CompleteWithError(kErrorInitService); |
| 476 return NULL; | 476 return nullptr; |
| 477 } | 477 } |
| 478 | 478 |
| 479 scoped_refptr<UsbDevice> device = service->GetDeviceById(input_device.device); | 479 scoped_refptr<UsbDevice> device = service->GetDeviceById(input_device.device); |
| 480 if (!device.get()) { | 480 if (!device.get()) { |
| 481 CompleteWithError(kErrorNoDevice); | 481 CompleteWithError(kErrorNoDevice); |
| 482 return NULL; | 482 return nullptr; |
| 483 } | 483 } |
| 484 | 484 |
| 485 if (!HasDevicePermission(device)) { | 485 if (!HasDevicePermission(device)) { |
| 486 // Must act as if there is no such a device. | 486 // Must act as if there is no such a device. |
| 487 // Otherwise can be used to finger print unauthorized devices. | 487 // Otherwise can be used to finger print unauthorized devices. |
| 488 CompleteWithError(kErrorNoDevice); | 488 CompleteWithError(kErrorNoDevice); |
| 489 return NULL; | 489 return nullptr; |
| 490 } | 490 } |
| 491 | 491 |
| 492 return device; | 492 return device; |
| 493 } | 493 } |
| 494 | 494 |
| 495 scoped_refptr<UsbDeviceHandle> | 495 scoped_refptr<UsbDeviceHandle> |
| 496 UsbAsyncApiFunction::GetDeviceHandleOrCompleteWithError( | 496 UsbAsyncApiFunction::GetDeviceHandleOrCompleteWithError( |
| 497 const ConnectionHandle& input_device_handle) { | 497 const ConnectionHandle& input_device_handle) { |
| 498 UsbDeviceResource* resource = | 498 UsbDeviceResource* resource = |
| 499 manager_->Get(extension_->id(), input_device_handle.handle); | 499 manager_->Get(extension_->id(), input_device_handle.handle); |
| 500 if (!resource) { | 500 if (!resource) { |
| 501 CompleteWithError(kErrorNoDevice); | 501 CompleteWithError(kErrorNoDevice); |
| 502 return NULL; | 502 return nullptr; |
| 503 } | 503 } |
| 504 | 504 |
| 505 if (!resource->device().get() || !resource->device()->GetDevice().get()) { | 505 if (!resource->device().get() || !resource->device()->GetDevice().get()) { |
| 506 CompleteWithError(kErrorDisconnect); | 506 CompleteWithError(kErrorDisconnect); |
| 507 manager_->Remove(extension_->id(), input_device_handle.handle); | 507 manager_->Remove(extension_->id(), input_device_handle.handle); |
| 508 return NULL; | 508 return nullptr; |
| 509 } | 509 } |
| 510 | 510 |
| 511 if (resource->device()->GetDevice()->vendor_id() != | 511 if (resource->device()->GetDevice()->vendor_id() != |
| 512 input_device_handle.vendor_id || | 512 input_device_handle.vendor_id || |
| 513 resource->device()->GetDevice()->product_id() != | 513 resource->device()->GetDevice()->product_id() != |
| 514 input_device_handle.product_id) { | 514 input_device_handle.product_id) { |
| 515 CompleteWithError(kErrorNoDevice); | 515 CompleteWithError(kErrorNoDevice); |
| 516 return NULL; | 516 return nullptr; |
| 517 } | 517 } |
| 518 | 518 |
| 519 return resource->device(); | 519 return resource->device(); |
| 520 } | 520 } |
| 521 | 521 |
| 522 void UsbAsyncApiFunction::RemoveUsbDeviceResource(int api_resource_id) { | 522 void UsbAsyncApiFunction::RemoveUsbDeviceResource(int api_resource_id) { |
| 523 manager_->Remove(extension_->id(), api_resource_id); | 523 manager_->Remove(extension_->id(), api_resource_id); |
| 524 } | 524 } |
| 525 | 525 |
| 526 void UsbAsyncApiFunction::CompleteWithError(const std::string& error) { | 526 void UsbAsyncApiFunction::CompleteWithError(const std::string& error) { |
| (...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1170 SetResult(new base::FundamentalValue(false)); | 1170 SetResult(new base::FundamentalValue(false)); |
| 1171 CompleteWithError(kErrorResetDevice); | 1171 CompleteWithError(kErrorResetDevice); |
| 1172 return; | 1172 return; |
| 1173 } | 1173 } |
| 1174 | 1174 |
| 1175 SetResult(new base::FundamentalValue(true)); | 1175 SetResult(new base::FundamentalValue(true)); |
| 1176 AsyncWorkCompleted(); | 1176 AsyncWorkCompleted(); |
| 1177 } | 1177 } |
| 1178 | 1178 |
| 1179 } // namespace extensions | 1179 } // namespace extensions |
| OLD | NEW |