#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "extensions/browser/api/usb/usb_api.h"
#include <algorithm>
#include <memory>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/barrier_closure.h"
#include "base/functional/bind.h"
#include "base/values.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/api/api_resource_manager.h"
#include "extensions/browser/api/device_permissions_manager.h"
#include "extensions/browser/api/device_permissions_prompt.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/usb/usb_device_resource.h"
#include "extensions/browser/extension_function_constants.h"
#include "extensions/common/api/usb.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/permissions/usb_device_permission.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "services/device/public/cpp/usb/usb_utils.h"
#include "services/device/public/mojom/usb_device.mojom.h"
#include "services/device/public/mojom/usb_enumeration_options.mojom.h"
usb;
BulkTransfer;
ClaimInterface;
CloseDevice;
ControlTransfer;
FindDevices;
GetConfigurations;
GetDevices;
GetUserSelectedDevices;
InterruptTransfer;
IsochronousTransfer;
SetConfiguration;
GetConfiguration;
ListInterfaces;
OpenDevice;
ReleaseInterface;
RequestAccess;
ResetDevice;
SetInterfaceAlternateSetting;
BrowserThread;
UsbClaimInterfaceResult;
UsbControlTransferParams;
UsbControlTransferRecipient;
UsbControlTransferType;
UsbDeviceFilterPtr;
UsbIsochronousPacketPtr;
UsbSynchronizationType;
UsbTransferDirection;
UsbTransferStatus;
UsbTransferType;
UsbUsageType;
string;
vector;
ConfigDescriptor;
ConnectionHandle;
ControlTransferInfo;
Device;
Direction;
EndpointDescriptor;
GenericTransferInfo;
InterfaceDescriptor;
IsochronousTransferInfo;
Recipient;
RequestType;
SynchronizationType;
TransferType;
UsageType;
namespace extensions {
namespace {
const char kDataKey[] = …;
const char kResultCodeKey[] = …;
const char kErrorInitService[] = …;
const char kErrorOpen[] = …;
const char kErrorCancelled[] = …;
const char kErrorDisconnect[] = …;
const char kErrorGeneric[] = …;
const char kErrorNotSupported[] = …;
const char kErrorNotConfigured[] = …;
const char kErrorOverflow[] = …;
const char kErrorStalled[] = …;
const char kErrorTimeout[] = …;
const char kErrorTransferLength[] = …;
const char kErrorCannotSetConfiguration[] = …;
const char kErrorCannotClaimInterface[] = …;
const char kErrorCannotReleaseInterface[] = …;
const char kErrorCannotSetInterfaceAlternateSetting[] = …;
const char kErrorConvertDirection[] = …;
const char kErrorConvertRecipient[] = …;
const char kErrorConvertRequestType[] = …;
const char kErrorMalformedParameters[] = …;
const char kErrorNoConnection[] = …;
const char kErrorNoDevice[] = …;
const char kErrorPermissionDenied[] = …;
const char kErrorInvalidTransferLength[] = …;
const char kErrorInvalidNumberOfPackets[] = …;
const char kErrorInvalidPacketLength[] = …;
const char kErrorInvalidTimeout[] = …;
const char kErrorResetDevice[] = …;
const size_t kMaxTransferLength = …;
const int kMaxPackets = …;
const int kMaxPacketLength = …;
bool ConvertDirectionFromApi(const Direction& input,
UsbTransferDirection* output) { … }
bool ConvertRequestTypeFromApi(const RequestType& input,
UsbControlTransferType* output) { … }
bool ConvertRecipientFromApi(const Recipient& input,
UsbControlTransferRecipient* output) { … }
template <class T>
bool GetTransferInSize(const T& input, uint32_t* output) { … }
const char* ConvertTransferStatusToApi(const UsbTransferStatus status) { … }
base::Value::Dict PopulateConnectionHandle(int handle,
int vendor_id,
int product_id) { … }
TransferType ConvertTransferTypeToApi(const UsbTransferType& input) { … }
Direction ConvertDirectionToApi(const UsbTransferDirection& input) { … }
SynchronizationType ConvertSynchronizationTypeToApi(
const UsbSynchronizationType& input) { … }
usb::UsageType ConvertUsageTypeToApi(const UsbUsageType& input) { … }
EndpointDescriptor ConvertEndpointDescriptor(
const device::mojom::UsbEndpointInfo& input) { … }
InterfaceDescriptor ConvertInterfaceDescriptor(
uint8_t interface_number,
const device::mojom::UsbAlternateInterfaceInfo& input) { … }
ConfigDescriptor ConvertConfigDescriptor(
const device::mojom::UsbConfigurationInfo& input) { … }
device::mojom::UsbDeviceFilterPtr ConvertDeviceFilter(
const usb::DeviceFilter& input) { … }
}
UsbExtensionFunction::UsbExtensionFunction() = default;
UsbExtensionFunction::~UsbExtensionFunction() = default;
UsbDeviceManager* UsbExtensionFunction::usb_device_manager() { … }
bool UsbExtensionFunction::IsUsbDeviceAllowedByPolicy(int vendor_id,
int product_id) { … }
UsbPermissionCheckingFunction::UsbPermissionCheckingFunction()
: … { … }
UsbPermissionCheckingFunction::~UsbPermissionCheckingFunction() = default;
bool UsbPermissionCheckingFunction::HasDevicePermission(
const device::mojom::UsbDeviceInfo& device) { … }
void UsbPermissionCheckingFunction::RecordDeviceLastUsed() { … }
UsbConnectionFunction::UsbConnectionFunction() = default;
UsbConnectionFunction::~UsbConnectionFunction() = default;
UsbDeviceResource* UsbConnectionFunction::GetResourceFromHandle(
const ConnectionHandle& handle) { … }
device::mojom::UsbDevice* UsbConnectionFunction::GetDeviceFromHandle(
const ConnectionHandle& handle) { … }
const device::mojom::UsbDeviceInfo*
UsbConnectionFunction::GetDeviceInfoFromHandle(const ConnectionHandle& handle) { … }
void UsbConnectionFunction::ReleaseDeviceResource(
const ConnectionHandle& handle) { … }
UsbTransferFunction::UsbTransferFunction() = default;
UsbTransferFunction::~UsbTransferFunction() = default;
void UsbTransferFunction::OnCompleted(UsbTransferStatus status,
base::Value::Dict transfer_info) { … }
void UsbTransferFunction::OnTransferInCompleted(
UsbTransferStatus status,
base::span<const uint8_t> data) { … }
void UsbTransferFunction::OnTransferOutCompleted(UsbTransferStatus status) { … }
void UsbTransferFunction::OnDisconnect() { … }
UsbGenericTransferFunction::UsbGenericTransferFunction() = default;
UsbGenericTransferFunction::~UsbGenericTransferFunction() = default;
template <typename T>
ExtensionFunction::ResponseAction UsbGenericTransferFunction::DoTransfer(
const T& params) { … }
UsbFindDevicesFunction::UsbFindDevicesFunction() = default;
UsbFindDevicesFunction::~UsbFindDevicesFunction() = default;
ExtensionFunction::ResponseAction UsbFindDevicesFunction::Run() { … }
void UsbFindDevicesFunction::OnGetDevicesComplete(
std::vector<device::mojom::UsbDeviceInfoPtr> devices) { … }
void UsbFindDevicesFunction::OnDeviceOpened(
const std::string& guid,
mojo::Remote<device::mojom::UsbDevice> device,
device::mojom::UsbOpenDeviceResultPtr result) { … }
void UsbFindDevicesFunction::OnDisconnect() { … }
void UsbFindDevicesFunction::OpenComplete() { … }
UsbGetDevicesFunction::UsbGetDevicesFunction() = default;
UsbGetDevicesFunction::~UsbGetDevicesFunction() = default;
ExtensionFunction::ResponseAction UsbGetDevicesFunction::Run() { … }
void UsbGetDevicesFunction::OnGetDevicesComplete(
std::vector<device::mojom::UsbDeviceInfoPtr> devices) { … }
UsbGetUserSelectedDevicesFunction::UsbGetUserSelectedDevicesFunction() =
default;
UsbGetUserSelectedDevicesFunction::~UsbGetUserSelectedDevicesFunction() =
default;
ExtensionFunction::ResponseAction UsbGetUserSelectedDevicesFunction::Run() { … }
void UsbGetUserSelectedDevicesFunction::OnDevicesChosen(
std::vector<device::mojom::UsbDeviceInfoPtr> devices) { … }
UsbGetConfigurationsFunction::UsbGetConfigurationsFunction() = default;
UsbGetConfigurationsFunction::~UsbGetConfigurationsFunction() = default;
ExtensionFunction::ResponseAction UsbGetConfigurationsFunction::Run() { … }
UsbRequestAccessFunction::UsbRequestAccessFunction() = default;
UsbRequestAccessFunction::~UsbRequestAccessFunction() = default;
ExtensionFunction::ResponseAction UsbRequestAccessFunction::Run() { … }
UsbOpenDeviceFunction::UsbOpenDeviceFunction() = default;
UsbOpenDeviceFunction::~UsbOpenDeviceFunction() = default;
ExtensionFunction::ResponseAction UsbOpenDeviceFunction::Run() { … }
void UsbOpenDeviceFunction::OnDeviceOpened(
std::string guid,
mojo::Remote<device::mojom::UsbDevice> device,
device::mojom::UsbOpenDeviceResultPtr result) { … }
void UsbOpenDeviceFunction::OnDisconnect() { … }
UsbSetConfigurationFunction::UsbSetConfigurationFunction() = default;
UsbSetConfigurationFunction::~UsbSetConfigurationFunction() = default;
ExtensionFunction::ResponseAction UsbSetConfigurationFunction::Run() { … }
void UsbSetConfigurationFunction::OnComplete(const std::string& guid,
uint8_t config_value,
bool success) { … }
UsbGetConfigurationFunction::UsbGetConfigurationFunction() = default;
UsbGetConfigurationFunction::~UsbGetConfigurationFunction() = default;
ExtensionFunction::ResponseAction UsbGetConfigurationFunction::Run() { … }
UsbListInterfacesFunction::UsbListInterfacesFunction() = default;
UsbListInterfacesFunction::~UsbListInterfacesFunction() = default;
ExtensionFunction::ResponseAction UsbListInterfacesFunction::Run() { … }
UsbCloseDeviceFunction::UsbCloseDeviceFunction() = default;
UsbCloseDeviceFunction::~UsbCloseDeviceFunction() = default;
ExtensionFunction::ResponseAction UsbCloseDeviceFunction::Run() { … }
UsbClaimInterfaceFunction::UsbClaimInterfaceFunction() = default;
UsbClaimInterfaceFunction::~UsbClaimInterfaceFunction() = default;
ExtensionFunction::ResponseAction UsbClaimInterfaceFunction::Run() { … }
void UsbClaimInterfaceFunction::OnComplete(UsbClaimInterfaceResult result) { … }
UsbReleaseInterfaceFunction::UsbReleaseInterfaceFunction() = default;
UsbReleaseInterfaceFunction::~UsbReleaseInterfaceFunction() = default;
ExtensionFunction::ResponseAction UsbReleaseInterfaceFunction::Run() { … }
void UsbReleaseInterfaceFunction::OnComplete(bool success) { … }
UsbSetInterfaceAlternateSettingFunction::
UsbSetInterfaceAlternateSettingFunction() = default;
UsbSetInterfaceAlternateSettingFunction::
~UsbSetInterfaceAlternateSettingFunction() = default;
ExtensionFunction::ResponseAction
UsbSetInterfaceAlternateSettingFunction::Run() { … }
void UsbSetInterfaceAlternateSettingFunction::OnComplete(bool success) { … }
UsbControlTransferFunction::UsbControlTransferFunction() = default;
UsbControlTransferFunction::~UsbControlTransferFunction() = default;
ExtensionFunction::ResponseAction UsbControlTransferFunction::Run() { … }
UsbBulkTransferFunction::UsbBulkTransferFunction() = default;
UsbBulkTransferFunction::~UsbBulkTransferFunction() = default;
ExtensionFunction::ResponseAction UsbBulkTransferFunction::Run() { … }
UsbInterruptTransferFunction::UsbInterruptTransferFunction() = default;
UsbInterruptTransferFunction::~UsbInterruptTransferFunction() = default;
ExtensionFunction::ResponseAction UsbInterruptTransferFunction::Run() { … }
UsbIsochronousTransferFunction::UsbIsochronousTransferFunction() = default;
UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() = default;
ExtensionFunction::ResponseAction UsbIsochronousTransferFunction::Run() { … }
void UsbIsochronousTransferFunction::OnTransferInCompleted(
base::span<const uint8_t> data,
std::vector<UsbIsochronousPacketPtr> packets) { … }
void UsbIsochronousTransferFunction::OnTransferOutCompleted(
std::vector<UsbIsochronousPacketPtr> packets) { … }
UsbResetDeviceFunction::UsbResetDeviceFunction() = default;
UsbResetDeviceFunction::~UsbResetDeviceFunction() = default;
ExtensionFunction::ResponseAction UsbResetDeviceFunction::Run() { … }
void UsbResetDeviceFunction::OnComplete(bool success) { … }
}