Chromium Code Reviews| 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 "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "device/core/device_client.h" | 11 #include "device/core/device_client.h" |
| 12 #include "device/hid/hid_connection.h" | 12 #include "device/hid/hid_connection.h" |
| 13 #include "device/hid/hid_device_filter.h" | 13 #include "device/hid/hid_device_filter.h" |
| 14 #include "device/hid/hid_device_info.h" | 14 #include "device/hid/hid_device_info.h" |
| 15 #include "device/hid/hid_service.h" | 15 #include "device/hid/hid_service.h" |
| 16 #include "extensions/browser/api/api_resource_manager.h" | 16 #include "extensions/browser/api/api_resource_manager.h" |
| 17 #include "extensions/browser/api/device_permissions_prompt.h" | |
| 18 #include "extensions/browser/api/extensions_api_client.h" | |
| 17 #include "extensions/common/api/hid.h" | 19 #include "extensions/common/api/hid.h" |
| 18 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 19 | 21 |
| 20 namespace hid = extensions::core_api::hid; | 22 namespace hid = extensions::core_api::hid; |
| 21 | 23 |
| 22 using device::HidConnection; | 24 using device::HidConnection; |
| 23 using device::HidDeviceFilter; | 25 using device::HidDeviceFilter; |
| 24 using device::HidDeviceInfo; | 26 using device::HidDeviceInfo; |
| 25 using device::HidService; | 27 using device::HidService; |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 const char kErrorServiceUnavailable[] = "HID services are unavailable."; | |
| 30 const char kErrorPermissionDenied[] = "Permission to access device was denied."; | 31 const char kErrorPermissionDenied[] = "Permission to access device was denied."; |
| 31 const char kErrorInvalidDeviceId[] = "Invalid HID device ID."; | 32 const char kErrorInvalidDeviceId[] = "Invalid HID device ID."; |
| 32 const char kErrorFailedToOpenDevice[] = "Failed to open HID device."; | 33 const char kErrorFailedToOpenDevice[] = "Failed to open HID device."; |
| 33 const char kErrorConnectionNotFound[] = "Connection not established."; | 34 const char kErrorConnectionNotFound[] = "Connection not established."; |
| 34 const char kErrorTransfer[] = "Transfer failed."; | 35 const char kErrorTransfer[] = "Transfer failed."; |
| 35 | 36 |
| 36 base::Value* PopulateHidConnection(int connection_id, | 37 base::Value* PopulateHidConnection(int connection_id, |
| 37 scoped_refptr<HidConnection> connection) { | 38 scoped_refptr<HidConnection> connection) { |
| 38 hid::HidConnectInfo connection_value; | 39 hid::HidConnectInfo connection_value; |
| 39 connection_value.connection_id = connection_id; | 40 connection_value.connection_id = connection_id; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 60 | 61 |
| 61 namespace extensions { | 62 namespace extensions { |
| 62 | 63 |
| 63 HidGetDevicesFunction::HidGetDevicesFunction() {} | 64 HidGetDevicesFunction::HidGetDevicesFunction() {} |
| 64 | 65 |
| 65 HidGetDevicesFunction::~HidGetDevicesFunction() {} | 66 HidGetDevicesFunction::~HidGetDevicesFunction() {} |
| 66 | 67 |
| 67 ExtensionFunction::ResponseAction HidGetDevicesFunction::Run() { | 68 ExtensionFunction::ResponseAction HidGetDevicesFunction::Run() { |
| 68 scoped_ptr<core_api::hid::GetDevices::Params> parameters = | 69 scoped_ptr<core_api::hid::GetDevices::Params> parameters = |
| 69 hid::GetDevices::Params::Create(*args_); | 70 hid::GetDevices::Params::Create(*args_); |
| 70 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | 71 EXTENSION_FUNCTION_VALIDATE(parameters); |
| 71 | 72 |
| 72 HidDeviceManager* device_manager = HidDeviceManager::Get(browser_context()); | 73 HidDeviceManager* device_manager = HidDeviceManager::Get(browser_context()); |
| 73 if (!device_manager) { | 74 CHECK(device_manager); |
| 74 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 75 } | |
| 76 | 75 |
| 77 std::vector<HidDeviceFilter> filters; | 76 std::vector<HidDeviceFilter> filters; |
| 78 if (parameters->options.filters) { | 77 if (parameters->options.filters) { |
| 79 filters.resize(parameters->options.filters->size()); | 78 filters.resize(parameters->options.filters->size()); |
| 80 for (size_t i = 0; i < parameters->options.filters->size(); ++i) { | 79 for (size_t i = 0; i < parameters->options.filters->size(); ++i) { |
| 81 ConvertHidDeviceFilter(parameters->options.filters->at(i), &filters[i]); | 80 ConvertHidDeviceFilter(parameters->options.filters->at(i), &filters[i]); |
| 82 } | 81 } |
| 83 } | 82 } |
| 84 if (parameters->options.vendor_id) { | 83 if (parameters->options.vendor_id) { |
| 85 HidDeviceFilter legacy_filter; | 84 HidDeviceFilter legacy_filter; |
| 86 legacy_filter.SetVendorId(*parameters->options.vendor_id); | 85 legacy_filter.SetVendorId(*parameters->options.vendor_id); |
| 87 if (parameters->options.product_id) { | 86 if (parameters->options.product_id) { |
| 88 legacy_filter.SetProductId(*parameters->options.product_id); | 87 legacy_filter.SetProductId(*parameters->options.product_id); |
| 89 } | 88 } |
| 90 filters.push_back(legacy_filter); | 89 filters.push_back(legacy_filter); |
| 91 } | 90 } |
| 92 | 91 |
| 93 device_manager->GetApiDevices( | 92 device_manager->GetApiDevices( |
| 94 extension(), filters, | 93 extension(), filters, |
| 95 base::Bind(&HidGetDevicesFunction::OnEnumerationComplete, this)); | 94 base::Bind(&HidGetDevicesFunction::OnEnumerationComplete, this)); |
| 96 return RespondLater(); | 95 return RespondLater(); |
| 97 } | 96 } |
| 98 | 97 |
| 99 void HidGetDevicesFunction::OnEnumerationComplete( | 98 void HidGetDevicesFunction::OnEnumerationComplete( |
| 100 scoped_ptr<base::ListValue> devices) { | 99 scoped_ptr<base::ListValue> devices) { |
| 101 Respond(OneArgument(devices.release())); | 100 Respond(OneArgument(devices.release())); |
| 102 } | 101 } |
| 103 | 102 |
| 103 HidGetUserSelectedDevicesFunction::HidGetUserSelectedDevicesFunction() { | |
| 104 } | |
| 105 | |
| 106 HidGetUserSelectedDevicesFunction::~HidGetUserSelectedDevicesFunction() { | |
| 107 } | |
| 108 | |
| 109 ExtensionFunction::ResponseAction HidGetUserSelectedDevicesFunction::Run() { | |
| 110 scoped_ptr<core_api::hid::GetUserSelectedDevices::Params> parameters = | |
| 111 hid::GetUserSelectedDevices::Params::Create(*args_); | |
| 112 EXTENSION_FUNCTION_VALIDATE(parameters); | |
| 113 | |
| 114 if (!user_gesture()) { | |
| 115 return RespondNow(OneArgument(new base::ListValue())); | |
| 116 } | |
| 117 | |
| 118 bool multiple = false; | |
| 119 std::vector<HidDeviceFilter> filters; | |
| 120 if (parameters->options) { | |
| 121 multiple = parameters->options->multiple && *parameters->options->multiple; | |
| 122 if (parameters->options->filters) { | |
| 123 const auto& api_filters = *parameters->options->filters; | |
| 124 filters.resize(api_filters.size()); | |
| 125 for (size_t i = 0; i < api_filters.size(); ++i) { | |
| 126 ConvertHidDeviceFilter(api_filters[i], &filters[i]); | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 prompt_ = ExtensionsAPIClient::Get()->CreateDevicePermissionsPrompt( | |
| 132 GetSenderWebContents()); | |
| 133 CHECK(prompt_); | |
|
Devlin
2015/05/07 20:43:55
This has me a little concerned. If the extension
Reilly Grant (use Gerrit)
2015/05/07 20:53:39
There's no check in this path for whether the web
Devlin
2015/05/07 21:02:43
Yeah, that's my point - I think we'll just crash.
| |
| 134 prompt_->AskForHidDevices( | |
| 135 extension(), browser_context(), multiple, filters, | |
| 136 base::Bind(&HidGetUserSelectedDevicesFunction::OnDevicesChosen, this)); | |
| 137 return RespondLater(); | |
| 138 } | |
| 139 | |
| 140 void HidGetUserSelectedDevicesFunction::OnDevicesChosen( | |
| 141 const std::vector<scoped_refptr<HidDeviceInfo>>& devices) { | |
| 142 HidDeviceManager* device_manager = HidDeviceManager::Get(browser_context()); | |
| 143 CHECK(device_manager); | |
| 144 Respond(OneArgument(device_manager->GetApiDevicesFromList(devices).Pass())); | |
| 145 } | |
| 146 | |
| 104 HidConnectFunction::HidConnectFunction() : connection_manager_(nullptr) { | 147 HidConnectFunction::HidConnectFunction() : connection_manager_(nullptr) { |
| 105 } | 148 } |
| 106 | 149 |
| 107 HidConnectFunction::~HidConnectFunction() {} | 150 HidConnectFunction::~HidConnectFunction() {} |
| 108 | 151 |
| 109 ExtensionFunction::ResponseAction HidConnectFunction::Run() { | 152 ExtensionFunction::ResponseAction HidConnectFunction::Run() { |
| 110 scoped_ptr<core_api::hid::Connect::Params> parameters = | 153 scoped_ptr<core_api::hid::Connect::Params> parameters = |
| 111 hid::Connect::Params::Create(*args_); | 154 hid::Connect::Params::Create(*args_); |
| 112 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | 155 EXTENSION_FUNCTION_VALIDATE(parameters); |
| 113 | 156 |
| 114 HidDeviceManager* device_manager = HidDeviceManager::Get(browser_context()); | 157 HidDeviceManager* device_manager = HidDeviceManager::Get(browser_context()); |
| 115 if (!device_manager) { | 158 CHECK(device_manager); |
| 116 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 117 } | |
| 118 | 159 |
| 119 connection_manager_ = | 160 connection_manager_ = |
| 120 ApiResourceManager<HidConnectionResource>::Get(browser_context()); | 161 ApiResourceManager<HidConnectionResource>::Get(browser_context()); |
| 121 if (!connection_manager_) { | 162 CHECK(connection_manager_); |
| 122 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 123 } | |
| 124 | 163 |
| 125 scoped_refptr<HidDeviceInfo> device_info = | 164 scoped_refptr<HidDeviceInfo> device_info = |
| 126 device_manager->GetDeviceInfo(parameters->device_id); | 165 device_manager->GetDeviceInfo(parameters->device_id); |
| 127 if (!device_info) { | 166 if (!device_info) { |
| 128 return RespondNow(Error(kErrorInvalidDeviceId)); | 167 return RespondNow(Error(kErrorInvalidDeviceId)); |
| 129 } | 168 } |
| 130 | 169 |
| 131 if (!HidDeviceManager::HasPermission(extension(), device_info)) { | 170 if (!device_manager->HasPermission(extension(), device_info, true)) { |
| 132 return RespondNow(Error(kErrorPermissionDenied)); | 171 return RespondNow(Error(kErrorPermissionDenied)); |
| 133 } | 172 } |
| 134 | 173 |
| 135 HidService* hid_service = device::DeviceClient::Get()->GetHidService(); | 174 HidService* hid_service = device::DeviceClient::Get()->GetHidService(); |
| 136 if (!hid_service) { | 175 CHECK(hid_service); |
| 137 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 138 } | |
| 139 | 176 |
| 140 hid_service->Connect( | 177 hid_service->Connect( |
| 141 device_info->device_id(), | 178 device_info->device_id(), |
| 142 base::Bind(&HidConnectFunction::OnConnectComplete, this)); | 179 base::Bind(&HidConnectFunction::OnConnectComplete, this)); |
| 143 return RespondLater(); | 180 return RespondLater(); |
| 144 } | 181 } |
| 145 | 182 |
| 146 void HidConnectFunction::OnConnectComplete( | 183 void HidConnectFunction::OnConnectComplete( |
| 147 scoped_refptr<HidConnection> connection) { | 184 scoped_refptr<HidConnection> connection) { |
| 148 if (!connection.get()) { | 185 if (!connection) { |
| 149 Respond(Error(kErrorFailedToOpenDevice)); | 186 Respond(Error(kErrorFailedToOpenDevice)); |
| 150 return; | 187 return; |
| 151 } | 188 } |
| 152 | 189 |
| 153 DCHECK(connection_manager_); | 190 DCHECK(connection_manager_); |
| 154 int connection_id = connection_manager_->Add( | 191 int connection_id = connection_manager_->Add( |
| 155 new HidConnectionResource(extension_id(), connection)); | 192 new HidConnectionResource(extension_id(), connection)); |
| 156 Respond(OneArgument(PopulateHidConnection(connection_id, connection))); | 193 Respond(OneArgument(PopulateHidConnection(connection_id, connection))); |
| 157 } | 194 } |
| 158 | 195 |
| 159 HidDisconnectFunction::HidDisconnectFunction() {} | 196 HidDisconnectFunction::HidDisconnectFunction() {} |
| 160 | 197 |
| 161 HidDisconnectFunction::~HidDisconnectFunction() {} | 198 HidDisconnectFunction::~HidDisconnectFunction() {} |
| 162 | 199 |
| 163 ExtensionFunction::ResponseAction HidDisconnectFunction::Run() { | 200 ExtensionFunction::ResponseAction HidDisconnectFunction::Run() { |
| 164 scoped_ptr<core_api::hid::Disconnect::Params> parameters = | 201 scoped_ptr<core_api::hid::Disconnect::Params> parameters = |
| 165 hid::Disconnect::Params::Create(*args_); | 202 hid::Disconnect::Params::Create(*args_); |
| 166 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | 203 EXTENSION_FUNCTION_VALIDATE(parameters); |
| 167 | 204 |
| 168 ApiResourceManager<HidConnectionResource>* connection_manager = | 205 ApiResourceManager<HidConnectionResource>* connection_manager = |
| 169 ApiResourceManager<HidConnectionResource>::Get(browser_context()); | 206 ApiResourceManager<HidConnectionResource>::Get(browser_context()); |
| 170 if (!connection_manager) { | 207 CHECK(connection_manager); |
| 171 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 172 } | |
| 173 | 208 |
| 174 int connection_id = parameters->connection_id; | 209 int connection_id = parameters->connection_id; |
| 175 HidConnectionResource* resource = | 210 HidConnectionResource* resource = |
| 176 connection_manager->Get(extension_id(), connection_id); | 211 connection_manager->Get(extension_id(), connection_id); |
| 177 if (!resource) { | 212 if (!resource) { |
| 178 return RespondNow(Error(kErrorConnectionNotFound)); | 213 return RespondNow(Error(kErrorConnectionNotFound)); |
| 179 } | 214 } |
| 180 | 215 |
| 181 connection_manager->Remove(extension_id(), connection_id); | 216 connection_manager->Remove(extension_id(), connection_id); |
| 182 return RespondNow(NoArguments()); | 217 return RespondNow(NoArguments()); |
| 183 } | 218 } |
| 184 | 219 |
| 185 HidConnectionIoFunction::HidConnectionIoFunction() { | 220 HidConnectionIoFunction::HidConnectionIoFunction() { |
| 186 } | 221 } |
| 187 | 222 |
| 188 HidConnectionIoFunction::~HidConnectionIoFunction() { | 223 HidConnectionIoFunction::~HidConnectionIoFunction() { |
| 189 } | 224 } |
| 190 | 225 |
| 191 ExtensionFunction::ResponseAction HidConnectionIoFunction::Run() { | 226 ExtensionFunction::ResponseAction HidConnectionIoFunction::Run() { |
| 192 if (!ValidateParameters()) { | 227 if (!ValidateParameters()) { |
| 193 return RespondNow(Error(error_)); | 228 return RespondNow(Error(error_)); |
| 194 } | 229 } |
| 195 | 230 |
| 196 ApiResourceManager<HidConnectionResource>* connection_manager = | 231 ApiResourceManager<HidConnectionResource>* connection_manager = |
| 197 ApiResourceManager<HidConnectionResource>::Get(browser_context()); | 232 ApiResourceManager<HidConnectionResource>::Get(browser_context()); |
| 198 if (!connection_manager) { | 233 CHECK(connection_manager); |
| 199 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 200 } | |
| 201 | 234 |
| 202 HidConnectionResource* resource = | 235 HidConnectionResource* resource = |
| 203 connection_manager->Get(extension_id(), connection_id_); | 236 connection_manager->Get(extension_id(), connection_id_); |
| 204 if (!resource) { | 237 if (!resource) { |
| 205 return RespondNow(Error(kErrorConnectionNotFound)); | 238 return RespondNow(Error(kErrorConnectionNotFound)); |
| 206 } | 239 } |
| 207 | 240 |
| 208 StartWork(resource->connection().get()); | 241 StartWork(resource->connection().get()); |
| 209 return RespondLater(); | 242 return RespondLater(); |
| 210 } | 243 } |
| 211 | 244 |
| 212 HidReceiveFunction::HidReceiveFunction() {} | 245 HidReceiveFunction::HidReceiveFunction() {} |
| 213 | 246 |
| 214 HidReceiveFunction::~HidReceiveFunction() {} | 247 HidReceiveFunction::~HidReceiveFunction() {} |
| 215 | 248 |
| 216 bool HidReceiveFunction::ValidateParameters() { | 249 bool HidReceiveFunction::ValidateParameters() { |
| 217 parameters_ = hid::Receive::Params::Create(*args_); | 250 parameters_ = hid::Receive::Params::Create(*args_); |
| 218 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 251 EXTENSION_FUNCTION_VALIDATE(parameters_); |
| 219 set_connection_id(parameters_->connection_id); | 252 set_connection_id(parameters_->connection_id); |
| 220 return true; | 253 return true; |
| 221 } | 254 } |
| 222 | 255 |
| 223 void HidReceiveFunction::StartWork(HidConnection* connection) { | 256 void HidReceiveFunction::StartWork(HidConnection* connection) { |
| 224 connection->Read(base::Bind(&HidReceiveFunction::OnFinished, this)); | 257 connection->Read(base::Bind(&HidReceiveFunction::OnFinished, this)); |
| 225 } | 258 } |
| 226 | 259 |
| 227 void HidReceiveFunction::OnFinished(bool success, | 260 void HidReceiveFunction::OnFinished(bool success, |
| 228 scoped_refptr<net::IOBuffer> buffer, | 261 scoped_refptr<net::IOBuffer> buffer, |
| 229 size_t size) { | 262 size_t size) { |
| 230 if (success) { | 263 if (success) { |
| 231 DCHECK_GE(size, 1u); | 264 DCHECK_GE(size, 1u); |
| 232 int report_id = reinterpret_cast<uint8_t*>(buffer->data())[0]; | 265 int report_id = reinterpret_cast<uint8_t*>(buffer->data())[0]; |
| 233 | 266 |
| 234 Respond(TwoArguments(new base::FundamentalValue(report_id), | 267 Respond(TwoArguments(new base::FundamentalValue(report_id), |
| 235 base::BinaryValue::CreateWithCopiedBuffer( | 268 base::BinaryValue::CreateWithCopiedBuffer( |
| 236 buffer->data() + 1, size - 1))); | 269 buffer->data() + 1, size - 1))); |
| 237 } else { | 270 } else { |
| 238 Respond(Error(kErrorTransfer)); | 271 Respond(Error(kErrorTransfer)); |
| 239 } | 272 } |
| 240 } | 273 } |
| 241 | 274 |
| 242 HidSendFunction::HidSendFunction() {} | 275 HidSendFunction::HidSendFunction() {} |
| 243 | 276 |
| 244 HidSendFunction::~HidSendFunction() {} | 277 HidSendFunction::~HidSendFunction() {} |
| 245 | 278 |
| 246 bool HidSendFunction::ValidateParameters() { | 279 bool HidSendFunction::ValidateParameters() { |
| 247 parameters_ = hid::Send::Params::Create(*args_); | 280 parameters_ = hid::Send::Params::Create(*args_); |
| 248 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 281 EXTENSION_FUNCTION_VALIDATE(parameters_); |
| 249 set_connection_id(parameters_->connection_id); | 282 set_connection_id(parameters_->connection_id); |
| 250 return true; | 283 return true; |
| 251 } | 284 } |
| 252 | 285 |
| 253 void HidSendFunction::StartWork(HidConnection* connection) { | 286 void HidSendFunction::StartWork(HidConnection* connection) { |
| 254 scoped_refptr<net::IOBufferWithSize> buffer( | 287 scoped_refptr<net::IOBufferWithSize> buffer( |
| 255 new net::IOBufferWithSize(parameters_->data.size() + 1)); | 288 new net::IOBufferWithSize(parameters_->data.size() + 1)); |
| 256 buffer->data()[0] = static_cast<uint8_t>(parameters_->report_id); | 289 buffer->data()[0] = static_cast<uint8_t>(parameters_->report_id); |
| 257 memcpy(buffer->data() + 1, parameters_->data.data(), | 290 memcpy(buffer->data() + 1, parameters_->data.data(), |
| 258 parameters_->data.size()); | 291 parameters_->data.size()); |
| 259 connection->Write(buffer, buffer->size(), | 292 connection->Write(buffer, buffer->size(), |
| 260 base::Bind(&HidSendFunction::OnFinished, this)); | 293 base::Bind(&HidSendFunction::OnFinished, this)); |
| 261 } | 294 } |
| 262 | 295 |
| 263 void HidSendFunction::OnFinished(bool success) { | 296 void HidSendFunction::OnFinished(bool success) { |
| 264 if (success) { | 297 if (success) { |
| 265 Respond(NoArguments()); | 298 Respond(NoArguments()); |
| 266 } else { | 299 } else { |
| 267 Respond(Error(kErrorTransfer)); | 300 Respond(Error(kErrorTransfer)); |
| 268 } | 301 } |
| 269 } | 302 } |
| 270 | 303 |
| 271 HidReceiveFeatureReportFunction::HidReceiveFeatureReportFunction() {} | 304 HidReceiveFeatureReportFunction::HidReceiveFeatureReportFunction() {} |
| 272 | 305 |
| 273 HidReceiveFeatureReportFunction::~HidReceiveFeatureReportFunction() {} | 306 HidReceiveFeatureReportFunction::~HidReceiveFeatureReportFunction() {} |
| 274 | 307 |
| 275 bool HidReceiveFeatureReportFunction::ValidateParameters() { | 308 bool HidReceiveFeatureReportFunction::ValidateParameters() { |
| 276 parameters_ = hid::ReceiveFeatureReport::Params::Create(*args_); | 309 parameters_ = hid::ReceiveFeatureReport::Params::Create(*args_); |
| 277 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 310 EXTENSION_FUNCTION_VALIDATE(parameters_); |
| 278 set_connection_id(parameters_->connection_id); | 311 set_connection_id(parameters_->connection_id); |
| 279 return true; | 312 return true; |
| 280 } | 313 } |
| 281 | 314 |
| 282 void HidReceiveFeatureReportFunction::StartWork(HidConnection* connection) { | 315 void HidReceiveFeatureReportFunction::StartWork(HidConnection* connection) { |
| 283 connection->GetFeatureReport( | 316 connection->GetFeatureReport( |
| 284 static_cast<uint8_t>(parameters_->report_id), | 317 static_cast<uint8_t>(parameters_->report_id), |
| 285 base::Bind(&HidReceiveFeatureReportFunction::OnFinished, this)); | 318 base::Bind(&HidReceiveFeatureReportFunction::OnFinished, this)); |
| 286 } | 319 } |
| 287 | 320 |
| 288 void HidReceiveFeatureReportFunction::OnFinished( | 321 void HidReceiveFeatureReportFunction::OnFinished( |
| 289 bool success, | 322 bool success, |
| 290 scoped_refptr<net::IOBuffer> buffer, | 323 scoped_refptr<net::IOBuffer> buffer, |
| 291 size_t size) { | 324 size_t size) { |
| 292 if (success) { | 325 if (success) { |
| 293 Respond(OneArgument( | 326 Respond(OneArgument( |
| 294 base::BinaryValue::CreateWithCopiedBuffer(buffer->data(), size))); | 327 base::BinaryValue::CreateWithCopiedBuffer(buffer->data(), size))); |
| 295 } else { | 328 } else { |
| 296 Respond(Error(kErrorTransfer)); | 329 Respond(Error(kErrorTransfer)); |
| 297 } | 330 } |
| 298 } | 331 } |
| 299 | 332 |
| 300 HidSendFeatureReportFunction::HidSendFeatureReportFunction() {} | 333 HidSendFeatureReportFunction::HidSendFeatureReportFunction() {} |
| 301 | 334 |
| 302 HidSendFeatureReportFunction::~HidSendFeatureReportFunction() {} | 335 HidSendFeatureReportFunction::~HidSendFeatureReportFunction() {} |
| 303 | 336 |
| 304 bool HidSendFeatureReportFunction::ValidateParameters() { | 337 bool HidSendFeatureReportFunction::ValidateParameters() { |
| 305 parameters_ = hid::SendFeatureReport::Params::Create(*args_); | 338 parameters_ = hid::SendFeatureReport::Params::Create(*args_); |
| 306 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 339 EXTENSION_FUNCTION_VALIDATE(parameters_); |
| 307 set_connection_id(parameters_->connection_id); | 340 set_connection_id(parameters_->connection_id); |
| 308 return true; | 341 return true; |
| 309 } | 342 } |
| 310 | 343 |
| 311 void HidSendFeatureReportFunction::StartWork(HidConnection* connection) { | 344 void HidSendFeatureReportFunction::StartWork(HidConnection* connection) { |
| 312 scoped_refptr<net::IOBufferWithSize> buffer( | 345 scoped_refptr<net::IOBufferWithSize> buffer( |
| 313 new net::IOBufferWithSize(parameters_->data.size() + 1)); | 346 new net::IOBufferWithSize(parameters_->data.size() + 1)); |
| 314 buffer->data()[0] = static_cast<uint8_t>(parameters_->report_id); | 347 buffer->data()[0] = static_cast<uint8_t>(parameters_->report_id); |
| 315 memcpy(buffer->data() + 1, vector_as_array(¶meters_->data), | 348 memcpy(buffer->data() + 1, vector_as_array(¶meters_->data), |
| 316 parameters_->data.size()); | 349 parameters_->data.size()); |
| 317 connection->SendFeatureReport( | 350 connection->SendFeatureReport( |
| 318 buffer, buffer->size(), | 351 buffer, buffer->size(), |
| 319 base::Bind(&HidSendFeatureReportFunction::OnFinished, this)); | 352 base::Bind(&HidSendFeatureReportFunction::OnFinished, this)); |
| 320 } | 353 } |
| 321 | 354 |
| 322 void HidSendFeatureReportFunction::OnFinished(bool success) { | 355 void HidSendFeatureReportFunction::OnFinished(bool success) { |
| 323 if (success) { | 356 if (success) { |
| 324 Respond(NoArguments()); | 357 Respond(NoArguments()); |
| 325 } else { | 358 } else { |
| 326 Respond(Error(kErrorTransfer)); | 359 Respond(Error(kErrorTransfer)); |
| 327 } | 360 } |
| 328 } | 361 } |
| 329 | 362 |
| 330 } // namespace extensions | 363 } // namespace extensions |
| OLD | NEW |