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