| 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/hid/hid_api.h" | 5 #include "extensions/browser/api/hid/hid_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "device/hid/hid_connection.h" | 10 #include "device/hid/hid_connection.h" |
| 11 #include "device/hid/hid_device_filter.h" |
| 11 #include "device/hid/hid_device_info.h" | 12 #include "device/hid/hid_device_info.h" |
| 12 #include "device/hid/hid_service.h" | 13 #include "device/hid/hid_service.h" |
| 13 #include "extensions/browser/api/api_resource_manager.h" | 14 #include "extensions/browser/api/api_resource_manager.h" |
| 14 #include "extensions/browser/api/extensions_api_client.h" | 15 #include "extensions/browser/api/extensions_api_client.h" |
| 15 #include "extensions/common/api/hid.h" | 16 #include "extensions/common/api/hid.h" |
| 16 #include "extensions/common/permissions/permissions_data.h" | |
| 17 #include "extensions/common/permissions/usb_device_permission.h" | |
| 18 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 19 | 18 |
| 20 namespace hid = extensions::core_api::hid; | 19 namespace hid = extensions::core_api::hid; |
| 21 | 20 |
| 22 using device::HidConnection; | 21 using device::HidConnection; |
| 22 using device::HidDeviceFilter; |
| 23 using device::HidDeviceInfo; | 23 using device::HidDeviceInfo; |
| 24 using device::HidService; | 24 using device::HidService; |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const char kErrorPermissionDenied[] = "Permission to access device was denied."; | 28 const char kErrorPermissionDenied[] = "Permission to access device was denied."; |
| 29 const char kErrorInvalidDeviceId[] = "Invalid HID device ID."; | 29 const char kErrorInvalidDeviceId[] = "Invalid HID device ID."; |
| 30 const char kErrorFailedToOpenDevice[] = "Failed to open HID device."; | 30 const char kErrorFailedToOpenDevice[] = "Failed to open HID device."; |
| 31 const char kErrorConnectionNotFound[] = "Connection not established."; | 31 const char kErrorConnectionNotFound[] = "Connection not established."; |
| 32 const char kErrorTransfer[] = "Transfer failed."; | 32 const char kErrorTransfer[] = "Transfer failed."; |
| 33 | 33 |
| 34 base::Value* PopulateHidConnection(int connection_id, | 34 base::Value* PopulateHidConnection(int connection_id, |
| 35 scoped_refptr<HidConnection> connection) { | 35 scoped_refptr<HidConnection> connection) { |
| 36 hid::HidConnectInfo connection_value; | 36 hid::HidConnectInfo connection_value; |
| 37 connection_value.connection_id = connection_id; | 37 connection_value.connection_id = connection_id; |
| 38 return connection_value.ToValue().release(); | 38 return connection_value.ToValue().release(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 void ConvertHidDeviceFilter(linked_ptr<hid::HidDeviceFilter> input, |
| 42 HidDeviceFilter* output) { |
| 43 if (input->vendor_id) { |
| 44 output->SetVendorId(*input->vendor_id); |
| 45 } |
| 46 if (input->product_id) { |
| 47 output->SetProductId(*input->product_id); |
| 48 } |
| 49 if (input->usage_page) { |
| 50 output->SetUsagePage(*input->usage_page); |
| 51 } |
| 52 if (input->usage) { |
| 53 output->SetUsage(*input->usage); |
| 54 } |
| 55 } |
| 56 |
| 41 } // namespace | 57 } // namespace |
| 42 | 58 |
| 43 namespace extensions { | 59 namespace extensions { |
| 44 | 60 |
| 45 HidAsyncApiFunction::HidAsyncApiFunction() | 61 HidAsyncApiFunction::HidAsyncApiFunction() |
| 46 : device_manager_(NULL), connection_manager_(NULL) {} | 62 : device_manager_(NULL), connection_manager_(NULL) {} |
| 47 | 63 |
| 48 HidAsyncApiFunction::~HidAsyncApiFunction() {} | 64 HidAsyncApiFunction::~HidAsyncApiFunction() {} |
| 49 | 65 |
| 50 bool HidAsyncApiFunction::PrePrepare() { | 66 bool HidAsyncApiFunction::PrePrepare() { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 77 | 93 |
| 78 HidGetDevicesFunction::~HidGetDevicesFunction() {} | 94 HidGetDevicesFunction::~HidGetDevicesFunction() {} |
| 79 | 95 |
| 80 bool HidGetDevicesFunction::Prepare() { | 96 bool HidGetDevicesFunction::Prepare() { |
| 81 parameters_ = hid::GetDevices::Params::Create(*args_); | 97 parameters_ = hid::GetDevices::Params::Create(*args_); |
| 82 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 98 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 83 return true; | 99 return true; |
| 84 } | 100 } |
| 85 | 101 |
| 86 void HidGetDevicesFunction::AsyncWorkStart() { | 102 void HidGetDevicesFunction::AsyncWorkStart() { |
| 87 const uint16_t vendor_id = parameters_->options.vendor_id; | 103 std::vector<HidDeviceFilter> filters; |
| 88 const uint16_t product_id = parameters_->options.product_id; | 104 if (parameters_->options.filters) { |
| 89 UsbDevicePermission::CheckParam param( | 105 filters.resize(parameters_->options.filters->size()); |
| 90 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | 106 for (size_t i = 0; i < parameters_->options.filters->size(); ++i) { |
| 91 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( | 107 ConvertHidDeviceFilter(parameters_->options.filters->at(i), &filters[i]); |
| 92 APIPermission::kUsbDevice, ¶m)) { | 108 } |
| 93 LOG(WARNING) << "Insufficient permissions to access device."; | 109 } |
| 94 CompleteWithError(kErrorPermissionDenied); | 110 if (parameters_->options.vendor_id) { |
| 95 return; | 111 HidDeviceFilter legacy_filter; |
| 112 legacy_filter.SetVendorId(*parameters_->options.vendor_id); |
| 113 if (parameters_->options.product_id) { |
| 114 legacy_filter.SetProductId(*parameters_->options.product_id); |
| 115 } |
| 116 filters.push_back(legacy_filter); |
| 96 } | 117 } |
| 97 | 118 |
| 98 SetResult(device_manager_->GetApiDevices(vendor_id, product_id).release()); | 119 SetResult(device_manager_->GetApiDevices(extension(), filters).release()); |
| 99 AsyncWorkCompleted(); | 120 AsyncWorkCompleted(); |
| 100 } | 121 } |
| 101 | 122 |
| 102 HidConnectFunction::HidConnectFunction() {} | 123 HidConnectFunction::HidConnectFunction() {} |
| 103 | 124 |
| 104 HidConnectFunction::~HidConnectFunction() {} | 125 HidConnectFunction::~HidConnectFunction() {} |
| 105 | 126 |
| 106 bool HidConnectFunction::Prepare() { | 127 bool HidConnectFunction::Prepare() { |
| 107 parameters_ = hid::Connect::Params::Create(*args_); | 128 parameters_ = hid::Connect::Params::Create(*args_); |
| 108 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 129 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 109 return true; | 130 return true; |
| 110 } | 131 } |
| 111 | 132 |
| 112 void HidConnectFunction::AsyncWorkStart() { | 133 void HidConnectFunction::AsyncWorkStart() { |
| 113 device::HidDeviceInfo device_info; | 134 device::HidDeviceInfo device_info; |
| 114 if (!device_manager_->GetDeviceInfo(parameters_->device_id, &device_info)) { | 135 if (!device_manager_->GetDeviceInfo(parameters_->device_id, &device_info)) { |
| 115 CompleteWithError(kErrorInvalidDeviceId); | 136 CompleteWithError(kErrorInvalidDeviceId); |
| 116 return; | 137 return; |
| 117 } | 138 } |
| 118 | 139 |
| 119 UsbDevicePermission::CheckParam param( | 140 if (!device_manager_->HasPermission(extension(), device_info)) { |
| 120 device_info.vendor_id, | |
| 121 device_info.product_id, | |
| 122 UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | |
| 123 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( | |
| 124 APIPermission::kUsbDevice, ¶m)) { | |
| 125 LOG(WARNING) << "Insufficient permissions to access device."; | 141 LOG(WARNING) << "Insufficient permissions to access device."; |
| 126 CompleteWithError(kErrorPermissionDenied); | 142 CompleteWithError(kErrorPermissionDenied); |
| 127 return; | 143 return; |
| 128 } | 144 } |
| 129 | 145 |
| 130 HidService* hid_service = ExtensionsAPIClient::Get()->GetHidService(); | 146 HidService* hid_service = ExtensionsAPIClient::Get()->GetHidService(); |
| 131 DCHECK(hid_service); | 147 DCHECK(hid_service); |
| 132 scoped_refptr<HidConnection> connection = | 148 scoped_refptr<HidConnection> connection = |
| 133 hid_service->Connect(device_info.device_id); | 149 hid_service->Connect(device_info.device_id); |
| 134 if (!connection.get()) { | 150 if (!connection.get()) { |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 335 |
| 320 void HidSendFeatureReportFunction::OnFinished(bool success, size_t bytes) { | 336 void HidSendFeatureReportFunction::OnFinished(bool success, size_t bytes) { |
| 321 if (!success) { | 337 if (!success) { |
| 322 CompleteWithError(kErrorTransfer); | 338 CompleteWithError(kErrorTransfer); |
| 323 return; | 339 return; |
| 324 } | 340 } |
| 325 AsyncWorkCompleted(); | 341 AsyncWorkCompleted(); |
| 326 } | 342 } |
| 327 | 343 |
| 328 } // namespace extensions | 344 } // namespace extensions |
| OLD | NEW |