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

Side by Side Diff: chrome/browser/extensions/api/usb/usb_api.cc

Issue 23571007: Fail gracefully when libusb_init fails (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-safe-exit
Patch Set: Fix tests Created 7 years, 3 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
« no previous file with comments | « no previous file | chrome/browser/usb/usb_context.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/usb/usb_api.h" 5 #include "chrome/browser/extensions/api/usb/usb_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 using usb::UsageType; 52 using usb::UsageType;
53 53
54 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector; 54 typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector;
55 typedef scoped_ptr<DeviceVector> ScopedDeviceVector; 55 typedef scoped_ptr<DeviceVector> ScopedDeviceVector;
56 56
57 namespace { 57 namespace {
58 58
59 const char kDataKey[] = "data"; 59 const char kDataKey[] = "data";
60 const char kResultCodeKey[] = "resultCode"; 60 const char kResultCodeKey[] = "resultCode";
61 61
62 const char kErrorInitService[] = "Failed to initialize USB service.";
63
62 const char kErrorOpen[] = "Failed to open device."; 64 const char kErrorOpen[] = "Failed to open device.";
63 const char kErrorCancelled[] = "Transfer was cancelled."; 65 const char kErrorCancelled[] = "Transfer was cancelled.";
64 const char kErrorDisconnect[] = "Device disconnected."; 66 const char kErrorDisconnect[] = "Device disconnected.";
65 const char kErrorGeneric[] = "Transfer failed."; 67 const char kErrorGeneric[] = "Transfer failed.";
66 const char kErrorNotSupported[] = "Not supported on this platform."; 68 const char kErrorNotSupported[] = "Not supported on this platform.";
67 const char kErrorOverflow[] = "Inbound transfer overflow."; 69 const char kErrorOverflow[] = "Inbound transfer overflow.";
68 const char kErrorStalled[] = "Transfer stalled."; 70 const char kErrorStalled[] = "Transfer stalled.";
69 const char kErrorTimeout[] = "Transfer timed out."; 71 const char kErrorTimeout[] = "Transfer timed out.";
70 const char kErrorTransferLength[] = "Transfer length is insufficient."; 72 const char kErrorTransferLength[] = "Transfer length is insufficient.";
71 73
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 UsbDevicePermission::CheckParam param( 407 UsbDevicePermission::CheckParam param(
406 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); 408 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE);
407 if (!PermissionsData::CheckAPIPermissionWithParam( 409 if (!PermissionsData::CheckAPIPermissionWithParam(
408 GetExtension(), APIPermission::kUsbDevice, &param)) { 410 GetExtension(), APIPermission::kUsbDevice, &param)) {
409 LOG(WARNING) << "Insufficient permissions to access device."; 411 LOG(WARNING) << "Insufficient permissions to access device.";
410 CompleteWithError(kErrorPermissionDenied); 412 CompleteWithError(kErrorPermissionDenied);
411 return NULL; 413 return NULL;
412 } 414 }
413 415
414 UsbService* service = UsbService::GetInstance(); 416 UsbService* service = UsbService::GetInstance();
417 if (!service) {
418 CompleteWithError(kErrorInitService);
419 return NULL;
420 }
415 scoped_refptr<UsbDevice> device; 421 scoped_refptr<UsbDevice> device;
416 422
417 device = service->GetDeviceById(input_device.device); 423 device = service->GetDeviceById(input_device.device);
418 424
419 if (!device) { 425 if (!device) {
420 CompleteWithError(kErrorNoDevice); 426 CompleteWithError(kErrorNoDevice);
421 return NULL; 427 return NULL;
422 } 428 }
423 429
424 if (device->vendor_id() != input_device.vendor_id || 430 if (device->vendor_id() != input_device.vendor_id ||
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 UsbDevicePermissionData::ANY_INTERFACE; 536 UsbDevicePermissionData::ANY_INTERFACE;
531 UsbDevicePermission::CheckParam param(vendor_id, product_id, interface_id); 537 UsbDevicePermission::CheckParam param(vendor_id, product_id, interface_id);
532 if (!PermissionsData::CheckAPIPermissionWithParam( 538 if (!PermissionsData::CheckAPIPermissionWithParam(
533 GetExtension(), APIPermission::kUsbDevice, &param)) { 539 GetExtension(), APIPermission::kUsbDevice, &param)) {
534 LOG(WARNING) << "Insufficient permissions to access device."; 540 LOG(WARNING) << "Insufficient permissions to access device.";
535 CompleteWithError(kErrorPermissionDenied); 541 CompleteWithError(kErrorPermissionDenied);
536 return; 542 return;
537 } 543 }
538 544
539 UsbService *service = UsbService::GetInstance(); 545 UsbService *service = UsbService::GetInstance();
546 if (!service) {
547 CompleteWithError(kErrorInitService);
548 return;
549 }
550
540 ScopedDeviceVector devices(new DeviceVector()); 551 ScopedDeviceVector devices(new DeviceVector());
541 service->GetDevices(devices.get()); 552 service->GetDevices(devices.get());
542 553
543 for (DeviceVector::iterator it = devices->begin(); 554 for (DeviceVector::iterator it = devices->begin();
544 it != devices->end();) { 555 it != devices->end();) {
545 if ((*it)->vendor_id() != vendor_id || (*it)->product_id() != product_id) { 556 if ((*it)->vendor_id() != vendor_id || (*it)->product_id() != product_id) {
546 it = devices->erase(it); 557 it = devices->erase(it);
547 } else { 558 } else {
548 ++it; 559 ++it;
549 } 560 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 UsbDevicePermission::CheckParam param( 628 UsbDevicePermission::CheckParam param(
618 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE); 629 vendor_id, product_id, UsbDevicePermissionData::UNSPECIFIED_INTERFACE);
619 if (!PermissionsData::CheckAPIPermissionWithParam( 630 if (!PermissionsData::CheckAPIPermissionWithParam(
620 GetExtension(), APIPermission::kUsbDevice, &param)) { 631 GetExtension(), APIPermission::kUsbDevice, &param)) {
621 LOG(WARNING) << "Insufficient permissions to access device."; 632 LOG(WARNING) << "Insufficient permissions to access device.";
622 CompleteWithError(kErrorPermissionDenied); 633 CompleteWithError(kErrorPermissionDenied);
623 return; 634 return;
624 } 635 }
625 636
626 UsbService* service = UsbService::GetInstance(); 637 UsbService* service = UsbService::GetInstance();
638 if (!service) {
639 CompleteWithError(kErrorInitService);
640 return;
641 }
642
627 DeviceVector devices; 643 DeviceVector devices;
628 service->GetDevices(&devices); 644 service->GetDevices(&devices);
629 645
630 for (DeviceVector::iterator it = devices.begin(); it != devices.end();) { 646 for (DeviceVector::iterator it = devices.begin(); it != devices.end();) {
631 if ((*it)->vendor_id() != vendor_id || (*it)->product_id() != product_id) { 647 if ((*it)->vendor_id() != vendor_id || (*it)->product_id() != product_id) {
632 it = devices.erase(it); 648 it = devices.erase(it);
633 } else { 649 } else {
634 ++it; 650 ++it;
635 } 651 }
636 } 652 }
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after
1143 SetResult(new base::FundamentalValue(false)); 1159 SetResult(new base::FundamentalValue(false));
1144 CompleteWithError(kErrorResetDevice); 1160 CompleteWithError(kErrorResetDevice);
1145 return; 1161 return;
1146 } 1162 }
1147 1163
1148 SetResult(new base::FundamentalValue(true)); 1164 SetResult(new base::FundamentalValue(true));
1149 AsyncWorkCompleted(); 1165 AsyncWorkCompleted();
1150 } 1166 }
1151 1167
1152 } // namespace extensions 1168 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/usb/usb_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698