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 content::WebContents* web_contents = GetSenderWebContents(); | |
| 115 if (!web_contents || !user_gesture()) { | |
| 116 return RespondNow(OneArgument(new base::ListValue())); | |
| 117 } | |
| 118 | |
| 119 bool multiple = false; | |
| 120 std::vector<HidDeviceFilter> filters; | |
| 121 if (parameters->options) { | |
| 122 multiple = parameters->options->multiple && *parameters->options->multiple; | |
| 123 if (parameters->options->filters) { | |
| 124 const auto& api_filters = *parameters->options->filters; | |
| 125 filters.resize(api_filters.size()); | |
| 126 for (size_t i = 0; i < api_filters.size(); ++i) { | |
| 127 ConvertHidDeviceFilter(api_filters[i], &filters[i]); | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 prompt_ = | |
| 133 ExtensionsAPIClient::Get()->CreateDevicePermissionsPrompt(web_contents); | |
| 134 CHECK(prompt_); | |
| 135 prompt_->AskForHidDevices( | |
|
Devlin
2015/05/14 17:38:11
This is problematic with your handling of having n
Reilly Grant (use Gerrit)
2015/05/14 18:04:28
You're right that the documentation for RespondLat
Devlin
2015/05/14 18:32:02
But there's a larger problem, which is that the AP
| |
| 136 extension(), browser_context(), multiple, filters, | |
| 137 base::Bind(&HidGetUserSelectedDevicesFunction::OnDevicesChosen, this)); | |
| 138 return RespondLater(); | |
| 139 } | |
| 140 | |
| 141 void HidGetUserSelectedDevicesFunction::OnDevicesChosen( | |
| 142 const std::vector<scoped_refptr<HidDeviceInfo>>& devices) { | |
| 143 HidDeviceManager* device_manager = HidDeviceManager::Get(browser_context()); | |
| 144 CHECK(device_manager); | |
| 145 Respond(OneArgument(device_manager->GetApiDevicesFromList(devices).Pass())); | |
| 146 } | |
| 147 | |
| 104 HidConnectFunction::HidConnectFunction() : connection_manager_(nullptr) { | 148 HidConnectFunction::HidConnectFunction() : connection_manager_(nullptr) { |
| 105 } | 149 } |
| 106 | 150 |
| 107 HidConnectFunction::~HidConnectFunction() {} | 151 HidConnectFunction::~HidConnectFunction() {} |
| 108 | 152 |
| 109 ExtensionFunction::ResponseAction HidConnectFunction::Run() { | 153 ExtensionFunction::ResponseAction HidConnectFunction::Run() { |
| 110 scoped_ptr<core_api::hid::Connect::Params> parameters = | 154 scoped_ptr<core_api::hid::Connect::Params> parameters = |
| 111 hid::Connect::Params::Create(*args_); | 155 hid::Connect::Params::Create(*args_); |
| 112 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | 156 EXTENSION_FUNCTION_VALIDATE(parameters); |
| 113 | 157 |
| 114 HidDeviceManager* device_manager = HidDeviceManager::Get(browser_context()); | 158 HidDeviceManager* device_manager = HidDeviceManager::Get(browser_context()); |
| 115 if (!device_manager) { | 159 CHECK(device_manager); |
| 116 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 117 } | |
| 118 | 160 |
| 119 connection_manager_ = | 161 connection_manager_ = |
| 120 ApiResourceManager<HidConnectionResource>::Get(browser_context()); | 162 ApiResourceManager<HidConnectionResource>::Get(browser_context()); |
| 121 if (!connection_manager_) { | 163 CHECK(connection_manager_); |
| 122 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 123 } | |
| 124 | 164 |
| 125 scoped_refptr<HidDeviceInfo> device_info = | 165 scoped_refptr<HidDeviceInfo> device_info = |
| 126 device_manager->GetDeviceInfo(parameters->device_id); | 166 device_manager->GetDeviceInfo(parameters->device_id); |
| 127 if (!device_info) { | 167 if (!device_info) { |
| 128 return RespondNow(Error(kErrorInvalidDeviceId)); | 168 return RespondNow(Error(kErrorInvalidDeviceId)); |
| 129 } | 169 } |
| 130 | 170 |
| 131 if (!HidDeviceManager::HasPermission(extension(), device_info)) { | 171 if (!device_manager->HasPermission(extension(), device_info, true)) { |
| 132 return RespondNow(Error(kErrorPermissionDenied)); | 172 return RespondNow(Error(kErrorPermissionDenied)); |
| 133 } | 173 } |
| 134 | 174 |
| 135 HidService* hid_service = device::DeviceClient::Get()->GetHidService(); | 175 HidService* hid_service = device::DeviceClient::Get()->GetHidService(); |
| 136 if (!hid_service) { | 176 CHECK(hid_service); |
| 137 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 138 } | |
| 139 | 177 |
| 140 hid_service->Connect( | 178 hid_service->Connect( |
| 141 device_info->device_id(), | 179 device_info->device_id(), |
| 142 base::Bind(&HidConnectFunction::OnConnectComplete, this)); | 180 base::Bind(&HidConnectFunction::OnConnectComplete, this)); |
| 143 return RespondLater(); | 181 return RespondLater(); |
| 144 } | 182 } |
| 145 | 183 |
| 146 void HidConnectFunction::OnConnectComplete( | 184 void HidConnectFunction::OnConnectComplete( |
| 147 scoped_refptr<HidConnection> connection) { | 185 scoped_refptr<HidConnection> connection) { |
| 148 if (!connection.get()) { | 186 if (!connection) { |
| 149 Respond(Error(kErrorFailedToOpenDevice)); | 187 Respond(Error(kErrorFailedToOpenDevice)); |
| 150 return; | 188 return; |
| 151 } | 189 } |
| 152 | 190 |
| 153 DCHECK(connection_manager_); | 191 DCHECK(connection_manager_); |
| 154 int connection_id = connection_manager_->Add( | 192 int connection_id = connection_manager_->Add( |
| 155 new HidConnectionResource(extension_id(), connection)); | 193 new HidConnectionResource(extension_id(), connection)); |
| 156 Respond(OneArgument(PopulateHidConnection(connection_id, connection))); | 194 Respond(OneArgument(PopulateHidConnection(connection_id, connection))); |
| 157 } | 195 } |
| 158 | 196 |
| 159 HidDisconnectFunction::HidDisconnectFunction() {} | 197 HidDisconnectFunction::HidDisconnectFunction() {} |
| 160 | 198 |
| 161 HidDisconnectFunction::~HidDisconnectFunction() {} | 199 HidDisconnectFunction::~HidDisconnectFunction() {} |
| 162 | 200 |
| 163 ExtensionFunction::ResponseAction HidDisconnectFunction::Run() { | 201 ExtensionFunction::ResponseAction HidDisconnectFunction::Run() { |
| 164 scoped_ptr<core_api::hid::Disconnect::Params> parameters = | 202 scoped_ptr<core_api::hid::Disconnect::Params> parameters = |
| 165 hid::Disconnect::Params::Create(*args_); | 203 hid::Disconnect::Params::Create(*args_); |
| 166 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | 204 EXTENSION_FUNCTION_VALIDATE(parameters); |
| 167 | 205 |
| 168 ApiResourceManager<HidConnectionResource>* connection_manager = | 206 ApiResourceManager<HidConnectionResource>* connection_manager = |
| 169 ApiResourceManager<HidConnectionResource>::Get(browser_context()); | 207 ApiResourceManager<HidConnectionResource>::Get(browser_context()); |
| 170 if (!connection_manager) { | 208 CHECK(connection_manager); |
| 171 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 172 } | |
| 173 | 209 |
| 174 int connection_id = parameters->connection_id; | 210 int connection_id = parameters->connection_id; |
| 175 HidConnectionResource* resource = | 211 HidConnectionResource* resource = |
| 176 connection_manager->Get(extension_id(), connection_id); | 212 connection_manager->Get(extension_id(), connection_id); |
| 177 if (!resource) { | 213 if (!resource) { |
| 178 return RespondNow(Error(kErrorConnectionNotFound)); | 214 return RespondNow(Error(kErrorConnectionNotFound)); |
| 179 } | 215 } |
| 180 | 216 |
| 181 connection_manager->Remove(extension_id(), connection_id); | 217 connection_manager->Remove(extension_id(), connection_id); |
| 182 return RespondNow(NoArguments()); | 218 return RespondNow(NoArguments()); |
| 183 } | 219 } |
| 184 | 220 |
| 185 HidConnectionIoFunction::HidConnectionIoFunction() { | 221 HidConnectionIoFunction::HidConnectionIoFunction() { |
| 186 } | 222 } |
| 187 | 223 |
| 188 HidConnectionIoFunction::~HidConnectionIoFunction() { | 224 HidConnectionIoFunction::~HidConnectionIoFunction() { |
| 189 } | 225 } |
| 190 | 226 |
| 191 ExtensionFunction::ResponseAction HidConnectionIoFunction::Run() { | 227 ExtensionFunction::ResponseAction HidConnectionIoFunction::Run() { |
| 192 if (!ValidateParameters()) { | 228 if (!ValidateParameters()) { |
| 193 return RespondNow(Error(error_)); | 229 return RespondNow(Error(error_)); |
| 194 } | 230 } |
| 195 | 231 |
| 196 ApiResourceManager<HidConnectionResource>* connection_manager = | 232 ApiResourceManager<HidConnectionResource>* connection_manager = |
| 197 ApiResourceManager<HidConnectionResource>::Get(browser_context()); | 233 ApiResourceManager<HidConnectionResource>::Get(browser_context()); |
| 198 if (!connection_manager) { | 234 CHECK(connection_manager); |
| 199 return RespondNow(Error(kErrorServiceUnavailable)); | |
| 200 } | |
| 201 | 235 |
| 202 HidConnectionResource* resource = | 236 HidConnectionResource* resource = |
| 203 connection_manager->Get(extension_id(), connection_id_); | 237 connection_manager->Get(extension_id(), connection_id_); |
| 204 if (!resource) { | 238 if (!resource) { |
| 205 return RespondNow(Error(kErrorConnectionNotFound)); | 239 return RespondNow(Error(kErrorConnectionNotFound)); |
| 206 } | 240 } |
| 207 | 241 |
| 208 StartWork(resource->connection().get()); | 242 StartWork(resource->connection().get()); |
| 209 return RespondLater(); | 243 return RespondLater(); |
| 210 } | 244 } |
| 211 | 245 |
| 212 HidReceiveFunction::HidReceiveFunction() {} | 246 HidReceiveFunction::HidReceiveFunction() {} |
| 213 | 247 |
| 214 HidReceiveFunction::~HidReceiveFunction() {} | 248 HidReceiveFunction::~HidReceiveFunction() {} |
| 215 | 249 |
| 216 bool HidReceiveFunction::ValidateParameters() { | 250 bool HidReceiveFunction::ValidateParameters() { |
| 217 parameters_ = hid::Receive::Params::Create(*args_); | 251 parameters_ = hid::Receive::Params::Create(*args_); |
| 218 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 252 EXTENSION_FUNCTION_VALIDATE(parameters_); |
| 219 set_connection_id(parameters_->connection_id); | 253 set_connection_id(parameters_->connection_id); |
| 220 return true; | 254 return true; |
| 221 } | 255 } |
| 222 | 256 |
| 223 void HidReceiveFunction::StartWork(HidConnection* connection) { | 257 void HidReceiveFunction::StartWork(HidConnection* connection) { |
| 224 connection->Read(base::Bind(&HidReceiveFunction::OnFinished, this)); | 258 connection->Read(base::Bind(&HidReceiveFunction::OnFinished, this)); |
| 225 } | 259 } |
| 226 | 260 |
| 227 void HidReceiveFunction::OnFinished(bool success, | 261 void HidReceiveFunction::OnFinished(bool success, |
| 228 scoped_refptr<net::IOBuffer> buffer, | 262 scoped_refptr<net::IOBuffer> buffer, |
| 229 size_t size) { | 263 size_t size) { |
| 230 if (success) { | 264 if (success) { |
| 231 DCHECK_GE(size, 1u); | 265 DCHECK_GE(size, 1u); |
| 232 int report_id = reinterpret_cast<uint8_t*>(buffer->data())[0]; | 266 int report_id = reinterpret_cast<uint8_t*>(buffer->data())[0]; |
| 233 | 267 |
| 234 Respond(TwoArguments(new base::FundamentalValue(report_id), | 268 Respond(TwoArguments(new base::FundamentalValue(report_id), |
| 235 base::BinaryValue::CreateWithCopiedBuffer( | 269 base::BinaryValue::CreateWithCopiedBuffer( |
| 236 buffer->data() + 1, size - 1))); | 270 buffer->data() + 1, size - 1))); |
| 237 } else { | 271 } else { |
| 238 Respond(Error(kErrorTransfer)); | 272 Respond(Error(kErrorTransfer)); |
| 239 } | 273 } |
| 240 } | 274 } |
| 241 | 275 |
| 242 HidSendFunction::HidSendFunction() {} | 276 HidSendFunction::HidSendFunction() {} |
| 243 | 277 |
| 244 HidSendFunction::~HidSendFunction() {} | 278 HidSendFunction::~HidSendFunction() {} |
| 245 | 279 |
| 246 bool HidSendFunction::ValidateParameters() { | 280 bool HidSendFunction::ValidateParameters() { |
| 247 parameters_ = hid::Send::Params::Create(*args_); | 281 parameters_ = hid::Send::Params::Create(*args_); |
| 248 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 282 EXTENSION_FUNCTION_VALIDATE(parameters_); |
| 249 set_connection_id(parameters_->connection_id); | 283 set_connection_id(parameters_->connection_id); |
| 250 return true; | 284 return true; |
| 251 } | 285 } |
| 252 | 286 |
| 253 void HidSendFunction::StartWork(HidConnection* connection) { | 287 void HidSendFunction::StartWork(HidConnection* connection) { |
| 254 scoped_refptr<net::IOBufferWithSize> buffer( | 288 scoped_refptr<net::IOBufferWithSize> buffer( |
| 255 new net::IOBufferWithSize(parameters_->data.size() + 1)); | 289 new net::IOBufferWithSize(parameters_->data.size() + 1)); |
| 256 buffer->data()[0] = static_cast<uint8_t>(parameters_->report_id); | 290 buffer->data()[0] = static_cast<uint8_t>(parameters_->report_id); |
| 257 memcpy(buffer->data() + 1, parameters_->data.data(), | 291 memcpy(buffer->data() + 1, parameters_->data.data(), |
| 258 parameters_->data.size()); | 292 parameters_->data.size()); |
| 259 connection->Write(buffer, buffer->size(), | 293 connection->Write(buffer, buffer->size(), |
| 260 base::Bind(&HidSendFunction::OnFinished, this)); | 294 base::Bind(&HidSendFunction::OnFinished, this)); |
| 261 } | 295 } |
| 262 | 296 |
| 263 void HidSendFunction::OnFinished(bool success) { | 297 void HidSendFunction::OnFinished(bool success) { |
| 264 if (success) { | 298 if (success) { |
| 265 Respond(NoArguments()); | 299 Respond(NoArguments()); |
| 266 } else { | 300 } else { |
| 267 Respond(Error(kErrorTransfer)); | 301 Respond(Error(kErrorTransfer)); |
| 268 } | 302 } |
| 269 } | 303 } |
| 270 | 304 |
| 271 HidReceiveFeatureReportFunction::HidReceiveFeatureReportFunction() {} | 305 HidReceiveFeatureReportFunction::HidReceiveFeatureReportFunction() {} |
| 272 | 306 |
| 273 HidReceiveFeatureReportFunction::~HidReceiveFeatureReportFunction() {} | 307 HidReceiveFeatureReportFunction::~HidReceiveFeatureReportFunction() {} |
| 274 | 308 |
| 275 bool HidReceiveFeatureReportFunction::ValidateParameters() { | 309 bool HidReceiveFeatureReportFunction::ValidateParameters() { |
| 276 parameters_ = hid::ReceiveFeatureReport::Params::Create(*args_); | 310 parameters_ = hid::ReceiveFeatureReport::Params::Create(*args_); |
| 277 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 311 EXTENSION_FUNCTION_VALIDATE(parameters_); |
| 278 set_connection_id(parameters_->connection_id); | 312 set_connection_id(parameters_->connection_id); |
| 279 return true; | 313 return true; |
| 280 } | 314 } |
| 281 | 315 |
| 282 void HidReceiveFeatureReportFunction::StartWork(HidConnection* connection) { | 316 void HidReceiveFeatureReportFunction::StartWork(HidConnection* connection) { |
| 283 connection->GetFeatureReport( | 317 connection->GetFeatureReport( |
| 284 static_cast<uint8_t>(parameters_->report_id), | 318 static_cast<uint8_t>(parameters_->report_id), |
| 285 base::Bind(&HidReceiveFeatureReportFunction::OnFinished, this)); | 319 base::Bind(&HidReceiveFeatureReportFunction::OnFinished, this)); |
| 286 } | 320 } |
| 287 | 321 |
| 288 void HidReceiveFeatureReportFunction::OnFinished( | 322 void HidReceiveFeatureReportFunction::OnFinished( |
| 289 bool success, | 323 bool success, |
| 290 scoped_refptr<net::IOBuffer> buffer, | 324 scoped_refptr<net::IOBuffer> buffer, |
| 291 size_t size) { | 325 size_t size) { |
| 292 if (success) { | 326 if (success) { |
| 293 Respond(OneArgument( | 327 Respond(OneArgument( |
| 294 base::BinaryValue::CreateWithCopiedBuffer(buffer->data(), size))); | 328 base::BinaryValue::CreateWithCopiedBuffer(buffer->data(), size))); |
| 295 } else { | 329 } else { |
| 296 Respond(Error(kErrorTransfer)); | 330 Respond(Error(kErrorTransfer)); |
| 297 } | 331 } |
| 298 } | 332 } |
| 299 | 333 |
| 300 HidSendFeatureReportFunction::HidSendFeatureReportFunction() {} | 334 HidSendFeatureReportFunction::HidSendFeatureReportFunction() {} |
| 301 | 335 |
| 302 HidSendFeatureReportFunction::~HidSendFeatureReportFunction() {} | 336 HidSendFeatureReportFunction::~HidSendFeatureReportFunction() {} |
| 303 | 337 |
| 304 bool HidSendFeatureReportFunction::ValidateParameters() { | 338 bool HidSendFeatureReportFunction::ValidateParameters() { |
| 305 parameters_ = hid::SendFeatureReport::Params::Create(*args_); | 339 parameters_ = hid::SendFeatureReport::Params::Create(*args_); |
| 306 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 340 EXTENSION_FUNCTION_VALIDATE(parameters_); |
| 307 set_connection_id(parameters_->connection_id); | 341 set_connection_id(parameters_->connection_id); |
| 308 return true; | 342 return true; |
| 309 } | 343 } |
| 310 | 344 |
| 311 void HidSendFeatureReportFunction::StartWork(HidConnection* connection) { | 345 void HidSendFeatureReportFunction::StartWork(HidConnection* connection) { |
| 312 scoped_refptr<net::IOBufferWithSize> buffer( | 346 scoped_refptr<net::IOBufferWithSize> buffer( |
| 313 new net::IOBufferWithSize(parameters_->data.size() + 1)); | 347 new net::IOBufferWithSize(parameters_->data.size() + 1)); |
| 314 buffer->data()[0] = static_cast<uint8_t>(parameters_->report_id); | 348 buffer->data()[0] = static_cast<uint8_t>(parameters_->report_id); |
| 315 memcpy(buffer->data() + 1, vector_as_array(¶meters_->data), | 349 memcpy(buffer->data() + 1, vector_as_array(¶meters_->data), |
| 316 parameters_->data.size()); | 350 parameters_->data.size()); |
| 317 connection->SendFeatureReport( | 351 connection->SendFeatureReport( |
| 318 buffer, buffer->size(), | 352 buffer, buffer->size(), |
| 319 base::Bind(&HidSendFeatureReportFunction::OnFinished, this)); | 353 base::Bind(&HidSendFeatureReportFunction::OnFinished, this)); |
| 320 } | 354 } |
| 321 | 355 |
| 322 void HidSendFeatureReportFunction::OnFinished(bool success) { | 356 void HidSendFeatureReportFunction::OnFinished(bool success) { |
| 323 if (success) { | 357 if (success) { |
| 324 Respond(NoArguments()); | 358 Respond(NoArguments()); |
| 325 } else { | 359 } else { |
| 326 Respond(Error(kErrorTransfer)); | 360 Respond(Error(kErrorTransfer)); |
| 327 } | 361 } |
| 328 } | 362 } |
| 329 | 363 |
| 330 } // namespace extensions | 364 } // namespace extensions |
| OLD | NEW |