| 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/usb/usb_api.h" | 5 #include "extensions/browser/api/usb/usb_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <numeric> | 9 #include <numeric> |
| 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <utility> | 12 #include <utility> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/barrier_closure.h" | 15 #include "base/barrier_closure.h" |
| 15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 16 #include "device/base/device_client.h" | 17 #include "device/base/device_client.h" |
| 17 #include "device/usb/usb_descriptors.h" | 18 #include "device/usb/usb_descriptors.h" |
| 18 #include "device/usb/usb_device_handle.h" | 19 #include "device/usb/usb_device_handle.h" |
| 19 #include "device/usb/usb_service.h" | 20 #include "device/usb/usb_service.h" |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 | 403 |
| 403 DevicePermissions* device_permissions = | 404 DevicePermissions* device_permissions = |
| 404 device_permissions_manager_->GetForExtension(extension_id()); | 405 device_permissions_manager_->GetForExtension(extension_id()); |
| 405 DCHECK(device_permissions); | 406 DCHECK(device_permissions); |
| 406 | 407 |
| 407 permission_entry_ = device_permissions->FindUsbDeviceEntry(device); | 408 permission_entry_ = device_permissions->FindUsbDeviceEntry(device); |
| 408 if (permission_entry_.get()) { | 409 if (permission_entry_.get()) { |
| 409 return true; | 410 return true; |
| 410 } | 411 } |
| 411 | 412 |
| 412 UsbDevicePermission::CheckParam param( | 413 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 413 device->vendor_id(), | 414 UsbDevicePermission::CheckParam::ForUsbDevice(extension(), device.get()); |
| 414 device->product_id(), | |
| 415 UsbDevicePermissionData::UNSPECIFIED_INTERFACE); | |
| 416 if (extension()->permissions_data()->CheckAPIPermissionWithParam( | 415 if (extension()->permissions_data()->CheckAPIPermissionWithParam( |
| 417 APIPermission::kUsbDevice, ¶m)) { | 416 APIPermission::kUsbDevice, param.get())) { |
| 418 return true; | 417 return true; |
| 419 } | 418 } |
| 420 | 419 |
| 421 return false; | 420 return false; |
| 422 } | 421 } |
| 423 | 422 |
| 424 void UsbPermissionCheckingFunction::RecordDeviceLastUsed() { | 423 void UsbPermissionCheckingFunction::RecordDeviceLastUsed() { |
| 425 if (permission_entry_.get()) { | 424 if (permission_entry_.get()) { |
| 426 device_permissions_manager_->UpdateLastUsed(extension_id(), | 425 device_permissions_manager_->UpdateLastUsed(extension_id(), |
| 427 permission_entry_); | 426 permission_entry_); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 | 496 |
| 498 ExtensionFunction::ResponseAction UsbFindDevicesFunction::Run() { | 497 ExtensionFunction::ResponseAction UsbFindDevicesFunction::Run() { |
| 499 std::unique_ptr<extensions::api::usb::FindDevices::Params> parameters = | 498 std::unique_ptr<extensions::api::usb::FindDevices::Params> parameters = |
| 500 FindDevices::Params::Create(*args_); | 499 FindDevices::Params::Create(*args_); |
| 501 EXTENSION_FUNCTION_VALIDATE(parameters.get()); | 500 EXTENSION_FUNCTION_VALIDATE(parameters.get()); |
| 502 | 501 |
| 503 vendor_id_ = parameters->options.vendor_id; | 502 vendor_id_ = parameters->options.vendor_id; |
| 504 product_id_ = parameters->options.product_id; | 503 product_id_ = parameters->options.product_id; |
| 505 int interface_id = parameters->options.interface_id.get() | 504 int interface_id = parameters->options.interface_id.get() |
| 506 ? *parameters->options.interface_id | 505 ? *parameters->options.interface_id |
| 507 : UsbDevicePermissionData::ANY_INTERFACE; | 506 : UsbDevicePermissionData::SPECIAL_VALUE_ANY; |
| 508 UsbDevicePermission::CheckParam param(vendor_id_, product_id_, interface_id); | 507 // Bail out early if there is no chance that the app has manifest permission |
| 508 // for the USB device described by vendor ID, product ID, and interface ID. |
| 509 // Note that this will match any permission filter that has only interface |
| 510 // class specified - in order to match interface class information about |
| 511 // device interfaces is needed, which is not known at this point; the |
| 512 // permission will have to be checked again when the USB device info is |
| 513 // fetched. |
| 514 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 515 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( |
| 516 extension(), vendor_id_, product_id_, interface_id); |
| 509 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( | 517 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( |
| 510 APIPermission::kUsbDevice, ¶m)) { | 518 APIPermission::kUsbDevice, param.get())) { |
| 511 return RespondNow(Error(kErrorPermissionDenied)); | 519 return RespondNow(Error(kErrorPermissionDenied)); |
| 512 } | 520 } |
| 513 | 521 |
| 514 UsbService* service = device::DeviceClient::Get()->GetUsbService(); | 522 UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| 515 if (!service) { | 523 if (!service) { |
| 516 return RespondNow(Error(kErrorInitService)); | 524 return RespondNow(Error(kErrorInitService)); |
| 517 } | 525 } |
| 518 | 526 |
| 519 service->GetDevices( | 527 service->GetDevices( |
| 520 base::Bind(&UsbFindDevicesFunction::OnGetDevicesComplete, this)); | 528 base::Bind(&UsbFindDevicesFunction::OnGetDevicesComplete, this)); |
| 521 return RespondLater(); | 529 return RespondLater(); |
| 522 } | 530 } |
| 523 | 531 |
| 524 void UsbFindDevicesFunction::OnGetDevicesComplete( | 532 void UsbFindDevicesFunction::OnGetDevicesComplete( |
| 525 const std::vector<scoped_refptr<UsbDevice>>& devices) { | 533 const std::vector<scoped_refptr<UsbDevice>>& devices) { |
| 526 result_.reset(new base::ListValue()); | 534 result_.reset(new base::ListValue()); |
| 527 barrier_ = base::BarrierClosure( | 535 barrier_ = base::BarrierClosure( |
| 528 devices.size(), base::Bind(&UsbFindDevicesFunction::OpenComplete, this)); | 536 devices.size(), base::Bind(&UsbFindDevicesFunction::OpenComplete, this)); |
| 529 | 537 |
| 530 for (const scoped_refptr<UsbDevice>& device : devices) { | 538 for (const scoped_refptr<UsbDevice>& device : devices) { |
| 539 // Skip the device whose vendor and product ID do not match the target one. |
| 531 if (device->vendor_id() != vendor_id_ || | 540 if (device->vendor_id() != vendor_id_ || |
| 532 device->product_id() != product_id_) { | 541 device->product_id() != product_id_) { |
| 533 barrier_.Run(); | 542 barrier_.Run(); |
| 543 continue; |
| 544 } |
| 545 |
| 546 // Verify that the app has permission for the device again, this time taking |
| 547 // device's interface classes into account - in case there is a USB device |
| 548 // permission specifying only interfaceClass, permissions check in |Run| |
| 549 // might have passed even though the app did not have permission for |
| 550 // specified vendor and product ID (as actual permissions check had to be |
| 551 // deferred until device's interface classes are known). |
| 552 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 553 UsbDevicePermission::CheckParam::ForUsbDevice(extension(), |
| 554 device.get()); |
| 555 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( |
| 556 APIPermission::kUsbDevice, param.get())) { |
| 557 barrier_.Run(); |
| 534 } else { | 558 } else { |
| 535 device->Open(base::Bind(&UsbFindDevicesFunction::OnDeviceOpened, this)); | 559 device->Open(base::Bind(&UsbFindDevicesFunction::OnDeviceOpened, this)); |
| 536 } | 560 } |
| 537 } | 561 } |
| 538 } | 562 } |
| 539 | 563 |
| 540 void UsbFindDevicesFunction::OnDeviceOpened( | 564 void UsbFindDevicesFunction::OnDeviceOpened( |
| 541 scoped_refptr<UsbDeviceHandle> device_handle) { | 565 scoped_refptr<UsbDeviceHandle> device_handle) { |
| 542 if (device_handle.get()) { | 566 if (device_handle.get()) { |
| 543 ApiResourceManager<UsbDeviceResource>* manager = | 567 ApiResourceManager<UsbDeviceResource>* manager = |
| (...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1271 | 1295 |
| 1272 std::unique_ptr<base::ListValue> error_args(new base::ListValue()); | 1296 std::unique_ptr<base::ListValue> error_args(new base::ListValue()); |
| 1273 error_args->AppendBoolean(false); | 1297 error_args->AppendBoolean(false); |
| 1274 // Using ErrorWithArguments is discouraged but required to maintain | 1298 // Using ErrorWithArguments is discouraged but required to maintain |
| 1275 // compatibility with existing applications. | 1299 // compatibility with existing applications. |
| 1276 Respond(ErrorWithArguments(std::move(error_args), kErrorResetDevice)); | 1300 Respond(ErrorWithArguments(std::move(error_args), kErrorResetDevice)); |
| 1277 } | 1301 } |
| 1278 } | 1302 } |
| 1279 | 1303 |
| 1280 } // namespace extensions | 1304 } // namespace extensions |
| OLD | NEW |