Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: chrome/browser/extensions/api/hid/hid_api.cc

Issue 161823002: Clean up HID backend and API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Many cleanup, such device ID, woww. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/extensions/api/hid/hid_api.h" 5 #include "chrome/browser/extensions/api/hid/hid_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "chrome/browser/extensions/api/api_resource_manager.h" 10 #include "chrome/browser/extensions/api/api_resource_manager.h"
11 #include "chrome/browser/extensions/api/hid/hid_device_resource.h" 11 #include "chrome/browser/extensions/api/hid/hid_device_resource.h"
12 #include "chrome/common/extensions/api/hid.h" 12 #include "chrome/common/extensions/api/hid.h"
13 #include "chrome/common/extensions/permissions/usb_device_permission.h" 13 #include "chrome/common/extensions/permissions/usb_device_permission.h"
14 #include "device/hid/hid_connection.h" 14 #include "device/hid/hid_connection.h"
15 #include "device/hid/hid_device_info.h" 15 #include "device/hid/hid_device_info.h"
16 #include "device/hid/hid_service.h" 16 #include "device/hid/hid_service.h"
17 #include "extensions/common/permissions/permissions_data.h" 17 #include "extensions/common/permissions/permissions_data.h"
18 #include "net/base/io_buffer.h" 18 #include "net/base/io_buffer.h"
19 19
20 namespace hid = extensions::api::hid; 20 namespace hid = extensions::api::hid;
21 21
22 using device::HidConnection; 22 using device::HidConnection;
23 using device::HidService; 23 using device::HidService;
24 using device::HidDeviceInfo; 24 using device::HidDeviceInfo;
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 kErrorServiceFailed[] = "HID service failed be created."; 29 const char kErrorServiceFailed[] = "HID service failed be created.";
30 const char kErrorDeviceNotFound[] = "HID device not found.";
31 const char kErrorFailedToOpenDevice[] = "Failed to open HID device."; 30 const char kErrorFailedToOpenDevice[] = "Failed to open HID device.";
32 const char kErrorConnectionNotFound[] = "Connection not established."; 31 const char kErrorConnectionNotFound[] = "Connection not established.";
33 const char kErrorTransfer[] = "Transfer failed."; 32 const char kErrorTransfer[] = "Transfer failed.";
34 33
35 base::Value* PopulateHidDevice(const HidDeviceInfo& info) { 34 base::Value* PopulateHidDevice(const HidDeviceInfo& info) {
36 hid::HidDeviceInfo device_info; 35 hid::HidDeviceInfo device_info;
37 device_info.path = info.device_id; 36 device_info.device_id = info.device_id;
38 device_info.product_id = info.product_id; 37 device_info.product_id = info.product_id;
39 device_info.vendor_id = info.vendor_id; 38 device_info.vendor_id = info.vendor_id;
40 return device_info.ToValue().release(); 39 return device_info.ToValue().release();
41 } 40 }
42 41
43 base::Value* PopulateHidConnection(int connection_id, 42 base::Value* PopulateHidConnection(int connection_id,
44 scoped_refptr<HidConnection> connection) { 43 scoped_refptr<HidConnection> connection) {
45 hid::HidConnectInfo connection_value; 44 hid::HidConnectInfo connection_value;
46 connection_value.connection_id = connection_id; 45 connection_value.connection_id = connection_id;
47 return connection_value.ToValue().release(); 46 return connection_value.ToValue().release();
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 128 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
130 return true; 129 return true;
131 } 130 }
132 131
133 void HidConnectFunction::AsyncWorkStart() { 132 void HidConnectFunction::AsyncWorkStart() {
134 HidService* service = HidService::GetInstance(); 133 HidService* service = HidService::GetInstance();
135 if (!service) { 134 if (!service) {
136 CompleteWithError(kErrorServiceFailed); 135 CompleteWithError(kErrorServiceFailed);
137 return; 136 return;
138 } 137 }
139 HidDeviceInfo device; 138 scoped_refptr<HidConnection> connection =
140 if (!service->GetInfo(parameters_->device_info.path, &device)) { 139 service->Connect(parameters_->device_id);
141 CompleteWithError(kErrorDeviceNotFound);
142 return;
143 }
144 if (device.vendor_id != parameters_->device_info.vendor_id ||
145 device.product_id != parameters_->device_info.product_id) {
146 CompleteWithError(kErrorDeviceNotFound);
147 return;
148 }
149 scoped_refptr<HidConnection> connection = service->Connect(device.device_id);
150 if (!connection) { 140 if (!connection) {
151 CompleteWithError(kErrorFailedToOpenDevice); 141 CompleteWithError(kErrorFailedToOpenDevice);
152 return; 142 return;
153 } 143 }
154 int connection_id = 144 int connection_id =
155 manager_->Add(new HidConnectionResource(extension_->id(), connection)); 145 manager_->Add(new HidConnectionResource(extension_->id(), connection));
156 SetResult(PopulateHidConnection(connection_id, connection)); 146 SetResult(PopulateHidConnection(connection_id, connection));
157 AsyncWorkCompleted(); 147 AsyncWorkCompleted();
158 } 148 }
159 149
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 181
192 void HidReceiveFunction::AsyncWorkStart() { 182 void HidReceiveFunction::AsyncWorkStart() {
193 int connection_id = parameters_->connection_id; 183 int connection_id = parameters_->connection_id;
194 HidConnectionResource* resource = 184 HidConnectionResource* resource =
195 manager_->Get(extension_->id(), connection_id); 185 manager_->Get(extension_->id(), connection_id);
196 if (!resource) { 186 if (!resource) {
197 CompleteWithError(kErrorConnectionNotFound); 187 CompleteWithError(kErrorConnectionNotFound);
198 return; 188 return;
199 } 189 }
200 190
201 buffer_ = new net::IOBuffer(parameters_->size); 191 buffer_ = new net::IOBufferWithSize(parameters_->size);
202 resource->connection()->Read( 192 resource->connection()->Read(
203 buffer_, 193 buffer_,
204 parameters_->size,
205 base::Bind(&HidReceiveFunction::OnFinished, this)); 194 base::Bind(&HidReceiveFunction::OnFinished, this));
206 } 195 }
207 196
208 void HidReceiveFunction::OnFinished(bool success, size_t bytes) { 197 void HidReceiveFunction::OnFinished(bool success, size_t bytes) {
209 if (!success) { 198 if (!success) {
210 CompleteWithError(kErrorTransfer); 199 CompleteWithError(kErrorTransfer);
211 return; 200 return;
212 } 201 }
213 202
214 SetResult(base::BinaryValue::CreateWithCopiedBuffer(buffer_->data(), bytes)); 203 SetResult(base::BinaryValue::CreateWithCopiedBuffer(buffer_->data(), bytes));
(...skipping 12 matching lines...) Expand all
227 216
228 void HidSendFunction::AsyncWorkStart() { 217 void HidSendFunction::AsyncWorkStart() {
229 int connection_id = parameters_->connection_id; 218 int connection_id = parameters_->connection_id;
230 HidConnectionResource* resource = 219 HidConnectionResource* resource =
231 manager_->Get(extension_->id(), connection_id); 220 manager_->Get(extension_->id(), connection_id);
232 if (!resource) { 221 if (!resource) {
233 CompleteWithError(kErrorConnectionNotFound); 222 CompleteWithError(kErrorConnectionNotFound);
234 return; 223 return;
235 } 224 }
236 225
237 scoped_refptr<net::IOBuffer> buffer( 226 scoped_refptr<net::IOBufferWithSize> buffer(
238 new net::WrappedIOBuffer(parameters_->data.c_str())); 227 new net::IOBufferWithSize(parameters_->data.size()));
239 memcpy(buffer->data(), 228 memcpy(buffer->data(),
240 parameters_->data.c_str(), 229 parameters_->data.c_str(),
241 parameters_->data.size()); 230 parameters_->data.size());
242 resource->connection()->Write( 231 resource->connection()->Write(
243 buffer, 232 buffer,
244 parameters_->data.size(),
245 base::Bind(&HidSendFunction::OnFinished, this)); 233 base::Bind(&HidSendFunction::OnFinished, this));
246 } 234 }
247 235
248 void HidSendFunction::OnFinished(bool success, size_t bytes) { 236 void HidSendFunction::OnFinished(bool success, size_t bytes) {
249 if (!success) { 237 if (!success) {
250 CompleteWithError(kErrorTransfer); 238 CompleteWithError(kErrorTransfer);
251 return; 239 return;
252 } 240 }
253 AsyncWorkCompleted(); 241 AsyncWorkCompleted();
254 } 242 }
255 243
256 HidReceiveFeatureReportFunction::HidReceiveFeatureReportFunction() {} 244 HidReceiveFeatureReportFunction::HidReceiveFeatureReportFunction() {}
257 245
258 HidReceiveFeatureReportFunction::~HidReceiveFeatureReportFunction() {} 246 HidReceiveFeatureReportFunction::~HidReceiveFeatureReportFunction() {}
259 247
260 bool HidReceiveFeatureReportFunction::Prepare() { 248 bool HidReceiveFeatureReportFunction::Prepare() {
261 parameters_ = hid::ReceiveFeatureReport::Params::Create(*args_); 249 parameters_ = hid::ReceiveFeatureReport::Params::Create(*args_);
262 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 250 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
263 return true; 251 return true;
264 } 252 }
265 253
266 void HidReceiveFeatureReportFunction::AsyncWorkStart() { 254 void HidReceiveFeatureReportFunction::AsyncWorkStart() {
267 int connection_id = parameters_->connection_id; 255 int connection_id = parameters_->connection_id;
268 HidConnectionResource* resource = 256 HidConnectionResource* resource =
269 manager_->Get(extension_->id(), connection_id); 257 manager_->Get(extension_->id(), connection_id);
270 if (!resource) { 258 if (!resource) {
271 CompleteWithError(kErrorConnectionNotFound); 259 CompleteWithError(kErrorConnectionNotFound);
272 return; 260 return;
273 } 261 }
274 buffer_ = new net::IOBuffer(parameters_->size); 262 buffer_ = new net::IOBufferWithSize(parameters_->size);
275 resource->connection()->GetFeatureReport( 263 resource->connection()->GetFeatureReport(
276 buffer_, 264 buffer_,
277 parameters_->size,
278 base::Bind(&HidReceiveFeatureReportFunction::OnFinished, this)); 265 base::Bind(&HidReceiveFeatureReportFunction::OnFinished, this));
279 } 266 }
280 267
281 void HidReceiveFeatureReportFunction::OnFinished(bool success, size_t bytes) { 268 void HidReceiveFeatureReportFunction::OnFinished(bool success, size_t bytes) {
282 if (!success) { 269 if (!success) {
283 CompleteWithError(kErrorTransfer); 270 CompleteWithError(kErrorTransfer);
284 return; 271 return;
285 } 272 }
286 273
287 SetResult(base::BinaryValue::CreateWithCopiedBuffer(buffer_->data(), bytes)); 274 SetResult(base::BinaryValue::CreateWithCopiedBuffer(buffer_->data(), bytes));
(...skipping 11 matching lines...) Expand all
299 } 286 }
300 287
301 void HidSendFeatureReportFunction::AsyncWorkStart() { 288 void HidSendFeatureReportFunction::AsyncWorkStart() {
302 int connection_id = parameters_->connection_id; 289 int connection_id = parameters_->connection_id;
303 HidConnectionResource* resource = 290 HidConnectionResource* resource =
304 manager_->Get(extension_->id(), connection_id); 291 manager_->Get(extension_->id(), connection_id);
305 if (!resource) { 292 if (!resource) {
306 CompleteWithError(kErrorConnectionNotFound); 293 CompleteWithError(kErrorConnectionNotFound);
307 return; 294 return;
308 } 295 }
309 scoped_refptr<net::IOBuffer> buffer( 296 scoped_refptr<net::IOBufferWithSize> buffer(
310 new net::WrappedIOBuffer(parameters_->data.c_str())); 297 new net::IOBufferWithSize(parameters_->data.size()));
311 resource->connection()->SendFeatureReport( 298 resource->connection()->SendFeatureReport(
312 buffer, 299 buffer,
313 parameters_->data.size(),
314 base::Bind(&HidSendFeatureReportFunction::OnFinished, this)); 300 base::Bind(&HidSendFeatureReportFunction::OnFinished, this));
315 } 301 }
316 302
317 void HidSendFeatureReportFunction::OnFinished(bool success, size_t bytes) { 303 void HidSendFeatureReportFunction::OnFinished(bool success, size_t bytes) {
318 if (!success) { 304 if (!success) {
319 CompleteWithError(kErrorTransfer); 305 CompleteWithError(kErrorTransfer);
320 return; 306 return;
321 } 307 }
322 AsyncWorkCompleted(); 308 AsyncWorkCompleted();
323 } 309 }
324 310
325 } // namespace extensions 311 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698