| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 13 #include "base/memory/ptr_util.h" |
| 12 #include "device/core/device_client.h" | 14 #include "device/core/device_client.h" |
| 13 #include "device/hid/hid_connection.h" | 15 #include "device/hid/hid_connection.h" |
| 14 #include "device/hid/hid_device_filter.h" | 16 #include "device/hid/hid_device_filter.h" |
| 15 #include "device/hid/hid_device_info.h" | 17 #include "device/hid/hid_device_info.h" |
| 16 #include "device/hid/hid_service.h" | 18 #include "device/hid/hid_service.h" |
| 17 #include "extensions/browser/api/api_resource_manager.h" | 19 #include "extensions/browser/api/api_resource_manager.h" |
| 18 #include "extensions/browser/api/device_permissions_prompt.h" | 20 #include "extensions/browser/api/device_permissions_prompt.h" |
| 19 #include "extensions/browser/api/extensions_api_client.h" | 21 #include "extensions/browser/api/extensions_api_client.h" |
| 20 #include "extensions/common/api/hid.h" | 22 #include "extensions/common/api/hid.h" |
| 21 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 49 using device::HidService; | 51 using device::HidService; |
| 50 | 52 |
| 51 namespace { | 53 namespace { |
| 52 | 54 |
| 53 const char kErrorPermissionDenied[] = "Permission to access device was denied."; | 55 const char kErrorPermissionDenied[] = "Permission to access device was denied."; |
| 54 const char kErrorInvalidDeviceId[] = "Invalid HID device ID."; | 56 const char kErrorInvalidDeviceId[] = "Invalid HID device ID."; |
| 55 const char kErrorFailedToOpenDevice[] = "Failed to open HID device."; | 57 const char kErrorFailedToOpenDevice[] = "Failed to open HID device."; |
| 56 const char kErrorConnectionNotFound[] = "Connection not established."; | 58 const char kErrorConnectionNotFound[] = "Connection not established."; |
| 57 const char kErrorTransfer[] = "Transfer failed."; | 59 const char kErrorTransfer[] = "Transfer failed."; |
| 58 | 60 |
| 59 base::Value* PopulateHidConnection(int connection_id, | 61 std::unique_ptr<base::Value> PopulateHidConnection( |
| 60 scoped_refptr<HidConnection> connection) { | 62 int connection_id, |
| 63 scoped_refptr<HidConnection> connection) { |
| 61 hid::HidConnectInfo connection_value; | 64 hid::HidConnectInfo connection_value; |
| 62 connection_value.connection_id = connection_id; | 65 connection_value.connection_id = connection_id; |
| 63 return connection_value.ToValue().release(); | 66 return connection_value.ToValue(); |
| 64 } | 67 } |
| 65 | 68 |
| 66 void ConvertHidDeviceFilter(const hid::DeviceFilter& input, | 69 void ConvertHidDeviceFilter(const hid::DeviceFilter& input, |
| 67 HidDeviceFilter* output) { | 70 HidDeviceFilter* output) { |
| 68 if (input.vendor_id) { | 71 if (input.vendor_id) { |
| 69 output->SetVendorId(*input.vendor_id); | 72 output->SetVendorId(*input.vendor_id); |
| 70 } | 73 } |
| 71 if (input.product_id) { | 74 if (input.product_id) { |
| 72 output->SetProductId(*input.product_id); | 75 output->SetProductId(*input.product_id); |
| 73 } | 76 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } | 115 } |
| 113 | 116 |
| 114 device_manager->GetApiDevices( | 117 device_manager->GetApiDevices( |
| 115 extension(), filters, | 118 extension(), filters, |
| 116 base::Bind(&HidGetDevicesFunction::OnEnumerationComplete, this)); | 119 base::Bind(&HidGetDevicesFunction::OnEnumerationComplete, this)); |
| 117 return RespondLater(); | 120 return RespondLater(); |
| 118 } | 121 } |
| 119 | 122 |
| 120 void HidGetDevicesFunction::OnEnumerationComplete( | 123 void HidGetDevicesFunction::OnEnumerationComplete( |
| 121 std::unique_ptr<base::ListValue> devices) { | 124 std::unique_ptr<base::ListValue> devices) { |
| 122 Respond(OneArgument(devices.release())); | 125 Respond(OneArgument(std::move(devices))); |
| 123 } | 126 } |
| 124 | 127 |
| 125 HidGetUserSelectedDevicesFunction::HidGetUserSelectedDevicesFunction() { | 128 HidGetUserSelectedDevicesFunction::HidGetUserSelectedDevicesFunction() { |
| 126 } | 129 } |
| 127 | 130 |
| 128 HidGetUserSelectedDevicesFunction::~HidGetUserSelectedDevicesFunction() { | 131 HidGetUserSelectedDevicesFunction::~HidGetUserSelectedDevicesFunction() { |
| 129 } | 132 } |
| 130 | 133 |
| 131 ExtensionFunction::ResponseAction HidGetUserSelectedDevicesFunction::Run() { | 134 ExtensionFunction::ResponseAction HidGetUserSelectedDevicesFunction::Run() { |
| 132 std::unique_ptr<api::hid::GetUserSelectedDevices::Params> parameters = | 135 std::unique_ptr<api::hid::GetUserSelectedDevices::Params> parameters = |
| 133 hid::GetUserSelectedDevices::Params::Create(*args_); | 136 hid::GetUserSelectedDevices::Params::Create(*args_); |
| 134 EXTENSION_FUNCTION_VALIDATE(parameters); | 137 EXTENSION_FUNCTION_VALIDATE(parameters); |
| 135 | 138 |
| 136 content::WebContents* web_contents = GetSenderWebContents(); | 139 content::WebContents* web_contents = GetSenderWebContents(); |
| 137 if (!web_contents || !user_gesture()) { | 140 if (!web_contents || !user_gesture()) { |
| 138 return RespondNow(OneArgument(new base::ListValue())); | 141 return RespondNow(OneArgument(base::MakeUnique<base::ListValue>())); |
| 139 } | 142 } |
| 140 | 143 |
| 141 bool multiple = false; | 144 bool multiple = false; |
| 142 std::vector<HidDeviceFilter> filters; | 145 std::vector<HidDeviceFilter> filters; |
| 143 if (parameters->options) { | 146 if (parameters->options) { |
| 144 multiple = parameters->options->multiple && *parameters->options->multiple; | 147 multiple = parameters->options->multiple && *parameters->options->multiple; |
| 145 if (parameters->options->filters) { | 148 if (parameters->options->filters) { |
| 146 const auto& api_filters = *parameters->options->filters; | 149 const auto& api_filters = *parameters->options->filters; |
| 147 filters.resize(api_filters.size()); | 150 filters.resize(api_filters.size()); |
| 148 for (size_t i = 0; i < api_filters.size(); ++i) { | 151 for (size_t i = 0; i < api_filters.size(); ++i) { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 connection->Read(base::Bind(&HidReceiveFunction::OnFinished, this)); | 283 connection->Read(base::Bind(&HidReceiveFunction::OnFinished, this)); |
| 281 } | 284 } |
| 282 | 285 |
| 283 void HidReceiveFunction::OnFinished(bool success, | 286 void HidReceiveFunction::OnFinished(bool success, |
| 284 scoped_refptr<net::IOBuffer> buffer, | 287 scoped_refptr<net::IOBuffer> buffer, |
| 285 size_t size) { | 288 size_t size) { |
| 286 if (success) { | 289 if (success) { |
| 287 DCHECK_GE(size, 1u); | 290 DCHECK_GE(size, 1u); |
| 288 int report_id = reinterpret_cast<uint8_t*>(buffer->data())[0]; | 291 int report_id = reinterpret_cast<uint8_t*>(buffer->data())[0]; |
| 289 | 292 |
| 290 Respond(TwoArguments(new base::FundamentalValue(report_id), | 293 Respond( |
| 291 base::BinaryValue::CreateWithCopiedBuffer( | 294 TwoArguments(base::MakeUnique<base::FundamentalValue>(report_id), |
| 292 buffer->data() + 1, size - 1))); | 295 base::WrapUnique(base::BinaryValue::CreateWithCopiedBuffer( |
| 296 buffer->data() + 1, size - 1)))); |
| 293 } else { | 297 } else { |
| 294 Respond(Error(kErrorTransfer)); | 298 Respond(Error(kErrorTransfer)); |
| 295 } | 299 } |
| 296 } | 300 } |
| 297 | 301 |
| 298 HidSendFunction::HidSendFunction() {} | 302 HidSendFunction::HidSendFunction() {} |
| 299 | 303 |
| 300 HidSendFunction::~HidSendFunction() {} | 304 HidSendFunction::~HidSendFunction() {} |
| 301 | 305 |
| 302 bool HidSendFunction::ValidateParameters() { | 306 bool HidSendFunction::ValidateParameters() { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 connection->GetFeatureReport( | 343 connection->GetFeatureReport( |
| 340 static_cast<uint8_t>(parameters_->report_id), | 344 static_cast<uint8_t>(parameters_->report_id), |
| 341 base::Bind(&HidReceiveFeatureReportFunction::OnFinished, this)); | 345 base::Bind(&HidReceiveFeatureReportFunction::OnFinished, this)); |
| 342 } | 346 } |
| 343 | 347 |
| 344 void HidReceiveFeatureReportFunction::OnFinished( | 348 void HidReceiveFeatureReportFunction::OnFinished( |
| 345 bool success, | 349 bool success, |
| 346 scoped_refptr<net::IOBuffer> buffer, | 350 scoped_refptr<net::IOBuffer> buffer, |
| 347 size_t size) { | 351 size_t size) { |
| 348 if (success) { | 352 if (success) { |
| 349 Respond(OneArgument( | 353 Respond(OneArgument(base::WrapUnique( |
| 350 base::BinaryValue::CreateWithCopiedBuffer(buffer->data(), size))); | 354 base::BinaryValue::CreateWithCopiedBuffer(buffer->data(), size)))); |
| 351 } else { | 355 } else { |
| 352 Respond(Error(kErrorTransfer)); | 356 Respond(Error(kErrorTransfer)); |
| 353 } | 357 } |
| 354 } | 358 } |
| 355 | 359 |
| 356 HidSendFeatureReportFunction::HidSendFeatureReportFunction() {} | 360 HidSendFeatureReportFunction::HidSendFeatureReportFunction() {} |
| 357 | 361 |
| 358 HidSendFeatureReportFunction::~HidSendFeatureReportFunction() {} | 362 HidSendFeatureReportFunction::~HidSendFeatureReportFunction() {} |
| 359 | 363 |
| 360 bool HidSendFeatureReportFunction::ValidateParameters() { | 364 bool HidSendFeatureReportFunction::ValidateParameters() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 377 | 381 |
| 378 void HidSendFeatureReportFunction::OnFinished(bool success) { | 382 void HidSendFeatureReportFunction::OnFinished(bool success) { |
| 379 if (success) { | 383 if (success) { |
| 380 Respond(NoArguments()); | 384 Respond(NoArguments()); |
| 381 } else { | 385 } else { |
| 382 Respond(Error(kErrorTransfer)); | 386 Respond(Error(kErrorTransfer)); |
| 383 } | 387 } |
| 384 } | 388 } |
| 385 | 389 |
| 386 } // namespace extensions | 390 } // namespace extensions |
| OLD | NEW |