1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_USB_USB_API_H_
#define EXTENSIONS_BROWSER_API_USB_USB_API_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/values.h"
#include "extensions/browser/api/usb/usb_device_manager.h"
#include "extensions/browser/extension_function.h"
#include "extensions/common/api/usb.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/usb_device.mojom.h"
namespace extensions {
class DevicePermissionEntry;
class DevicePermissionsPrompt;
class DevicePermissionsManager;
class UsbDeviceResource;
class UsbExtensionFunction : public ExtensionFunction {
protected:
UsbExtensionFunction();
~UsbExtensionFunction() override;
UsbDeviceManager* usb_device_manager();
bool IsUsbDeviceAllowedByPolicy(int vendor_id, int product_id);
private:
raw_ptr<UsbDeviceManager> usb_device_manager_ = nullptr;
};
class UsbPermissionCheckingFunction : public UsbExtensionFunction {
protected:
UsbPermissionCheckingFunction();
~UsbPermissionCheckingFunction() override;
bool HasDevicePermission(const device::mojom::UsbDeviceInfo& device);
void RecordDeviceLastUsed();
private:
raw_ptr<DevicePermissionsManager> device_permissions_manager_;
scoped_refptr<DevicePermissionEntry> permission_entry_;
};
class UsbConnectionFunction : public UsbExtensionFunction {
protected:
UsbConnectionFunction();
~UsbConnectionFunction() override;
UsbDeviceResource* GetResourceFromHandle(
const api::usb::ConnectionHandle& handle);
device::mojom::UsbDevice* GetDeviceFromHandle(
const api::usb::ConnectionHandle& handle);
const device::mojom::UsbDeviceInfo* GetDeviceInfoFromHandle(
const api::usb::ConnectionHandle& handle);
void ReleaseDeviceResource(const api::usb::ConnectionHandle& handle);
};
class UsbTransferFunction : public UsbConnectionFunction {
protected:
UsbTransferFunction();
~UsbTransferFunction() override;
void OnCompleted(device::mojom::UsbTransferStatus status,
base::Value::Dict transfer_info);
void OnTransferInCompleted(device::mojom::UsbTransferStatus status,
base::span<const uint8_t> data);
void OnTransferOutCompleted(device::mojom::UsbTransferStatus status);
void OnDisconnect();
};
class UsbGenericTransferFunction : public UsbTransferFunction {
protected:
UsbGenericTransferFunction();
~UsbGenericTransferFunction() override;
// InterruptTransfer::Params and BulkTransfer::Params
template <typename T>
ExtensionFunction::ResponseAction DoTransfer(const T& params);
};
class UsbFindDevicesFunction : public UsbExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.findDevices", USB_FINDDEVICES)
UsbFindDevicesFunction();
UsbFindDevicesFunction(const UsbFindDevicesFunction&) = delete;
UsbFindDevicesFunction& operator=(const UsbFindDevicesFunction&) = delete;
private:
~UsbFindDevicesFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnGetDevicesComplete(
std::vector<device::mojom::UsbDeviceInfoPtr> devices);
void OnDeviceOpened(const std::string& guid,
mojo::Remote<device::mojom::UsbDevice> device_ptr,
device::mojom::UsbOpenDeviceResultPtr result);
void OpenComplete();
void OnDisconnect();
uint16_t vendor_id_;
uint16_t product_id_;
base::Value::List result_;
base::RepeatingClosure barrier_;
};
class UsbGetDevicesFunction : public UsbPermissionCheckingFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.getDevices", USB_GETDEVICES)
UsbGetDevicesFunction();
UsbGetDevicesFunction(const UsbGetDevicesFunction&) = delete;
UsbGetDevicesFunction& operator=(const UsbGetDevicesFunction&) = delete;
private:
~UsbGetDevicesFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnGetDevicesComplete(
std::vector<device::mojom::UsbDeviceInfoPtr> devices);
std::vector<device::mojom::UsbDeviceFilterPtr> filters_;
};
class UsbGetUserSelectedDevicesFunction : public UsbExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.getUserSelectedDevices",
USB_GETUSERSELECTEDDEVICES)
UsbGetUserSelectedDevicesFunction();
UsbGetUserSelectedDevicesFunction(const UsbGetUserSelectedDevicesFunction&) =
delete;
UsbGetUserSelectedDevicesFunction& operator=(
const UsbGetUserSelectedDevicesFunction&) = delete;
private:
~UsbGetUserSelectedDevicesFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnDevicesChosen(std::vector<device::mojom::UsbDeviceInfoPtr> devices);
std::unique_ptr<DevicePermissionsPrompt> prompt_;
};
class UsbGetConfigurationsFunction : public UsbPermissionCheckingFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.getConfigurations", USB_GETCONFIGURATIONS)
UsbGetConfigurationsFunction();
UsbGetConfigurationsFunction(const UsbGetConfigurationsFunction&) = delete;
UsbGetConfigurationsFunction& operator=(const UsbGetConfigurationsFunction&) =
delete;
private:
~UsbGetConfigurationsFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class UsbRequestAccessFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.requestAccess", USB_REQUESTACCESS)
UsbRequestAccessFunction();
UsbRequestAccessFunction(const UsbRequestAccessFunction&) = delete;
UsbRequestAccessFunction& operator=(const UsbRequestAccessFunction&) = delete;
private:
~UsbRequestAccessFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class UsbOpenDeviceFunction : public UsbPermissionCheckingFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.openDevice", USB_OPENDEVICE)
UsbOpenDeviceFunction();
UsbOpenDeviceFunction(const UsbOpenDeviceFunction&) = delete;
UsbOpenDeviceFunction& operator=(const UsbOpenDeviceFunction&) = delete;
private:
~UsbOpenDeviceFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnDeviceOpened(std::string guid,
mojo::Remote<device::mojom::UsbDevice> device,
device::mojom::UsbOpenDeviceResultPtr result);
void OnDisconnect();
};
class UsbSetConfigurationFunction : public UsbConnectionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.setConfiguration", USB_SETCONFIGURATION)
UsbSetConfigurationFunction();
UsbSetConfigurationFunction(const UsbSetConfigurationFunction&) = delete;
UsbSetConfigurationFunction& operator=(const UsbSetConfigurationFunction&) =
delete;
private:
~UsbSetConfigurationFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnComplete(const std::string& guid, uint8_t config_value, bool success);
};
class UsbGetConfigurationFunction : public UsbConnectionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.getConfiguration", USB_GETCONFIGURATION)
UsbGetConfigurationFunction();
UsbGetConfigurationFunction(const UsbGetConfigurationFunction&) = delete;
UsbGetConfigurationFunction& operator=(const UsbGetConfigurationFunction&) =
delete;
private:
~UsbGetConfigurationFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class UsbListInterfacesFunction : public UsbConnectionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.listInterfaces", USB_LISTINTERFACES)
UsbListInterfacesFunction();
UsbListInterfacesFunction(const UsbListInterfacesFunction&) = delete;
UsbListInterfacesFunction& operator=(const UsbListInterfacesFunction&) =
delete;
private:
~UsbListInterfacesFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class UsbCloseDeviceFunction : public UsbConnectionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.closeDevice", USB_CLOSEDEVICE)
UsbCloseDeviceFunction();
UsbCloseDeviceFunction(const UsbCloseDeviceFunction&) = delete;
UsbCloseDeviceFunction& operator=(const UsbCloseDeviceFunction&) = delete;
private:
~UsbCloseDeviceFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class UsbClaimInterfaceFunction : public UsbConnectionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.claimInterface", USB_CLAIMINTERFACE)
UsbClaimInterfaceFunction();
UsbClaimInterfaceFunction(const UsbClaimInterfaceFunction&) = delete;
UsbClaimInterfaceFunction& operator=(const UsbClaimInterfaceFunction&) =
delete;
private:
~UsbClaimInterfaceFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnComplete(device::mojom::UsbClaimInterfaceResult result);
};
class UsbReleaseInterfaceFunction : public UsbConnectionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.releaseInterface", USB_RELEASEINTERFACE)
UsbReleaseInterfaceFunction();
UsbReleaseInterfaceFunction(const UsbReleaseInterfaceFunction&) = delete;
UsbReleaseInterfaceFunction& operator=(const UsbReleaseInterfaceFunction&) =
delete;
private:
~UsbReleaseInterfaceFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnComplete(bool success);
};
class UsbSetInterfaceAlternateSettingFunction : public UsbConnectionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.setInterfaceAlternateSetting",
USB_SETINTERFACEALTERNATESETTING)
UsbSetInterfaceAlternateSettingFunction();
UsbSetInterfaceAlternateSettingFunction(
const UsbSetInterfaceAlternateSettingFunction&) = delete;
UsbSetInterfaceAlternateSettingFunction& operator=(
const UsbSetInterfaceAlternateSettingFunction&) = delete;
private:
~UsbSetInterfaceAlternateSettingFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnComplete(bool success);
};
class UsbControlTransferFunction : public UsbTransferFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.controlTransfer", USB_CONTROLTRANSFER)
UsbControlTransferFunction();
UsbControlTransferFunction(const UsbControlTransferFunction&) = delete;
UsbControlTransferFunction& operator=(const UsbControlTransferFunction&) =
delete;
private:
~UsbControlTransferFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class UsbBulkTransferFunction : public UsbGenericTransferFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.bulkTransfer", USB_BULKTRANSFER)
UsbBulkTransferFunction();
UsbBulkTransferFunction(const UsbBulkTransferFunction&) = delete;
UsbBulkTransferFunction& operator=(const UsbBulkTransferFunction&) = delete;
private:
~UsbBulkTransferFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class UsbInterruptTransferFunction : public UsbGenericTransferFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.interruptTransfer", USB_INTERRUPTTRANSFER)
UsbInterruptTransferFunction();
UsbInterruptTransferFunction(const UsbInterruptTransferFunction&) = delete;
UsbInterruptTransferFunction& operator=(const UsbInterruptTransferFunction&) =
delete;
private:
~UsbInterruptTransferFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class UsbIsochronousTransferFunction : public UsbTransferFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.isochronousTransfer", USB_ISOCHRONOUSTRANSFER)
UsbIsochronousTransferFunction();
UsbIsochronousTransferFunction(const UsbIsochronousTransferFunction&) =
delete;
UsbIsochronousTransferFunction& operator=(
const UsbIsochronousTransferFunction&) = delete;
private:
~UsbIsochronousTransferFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnTransferInCompleted(
base::span<const uint8_t> data,
std::vector<device::mojom::UsbIsochronousPacketPtr> packets);
void OnTransferOutCompleted(
std::vector<device::mojom::UsbIsochronousPacketPtr> packets);
};
class UsbResetDeviceFunction : public UsbConnectionFunction {
public:
DECLARE_EXTENSION_FUNCTION("usb.resetDevice", USB_RESETDEVICE)
UsbResetDeviceFunction();
UsbResetDeviceFunction(const UsbResetDeviceFunction&) = delete;
UsbResetDeviceFunction& operator=(const UsbResetDeviceFunction&) = delete;
private:
~UsbResetDeviceFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
void OnComplete(bool success);
std::optional<api::usb::ResetDevice::Params> parameters_;
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_USB_USB_API_H_
|