Skip to content

Conversation

@thegecko
Copy link
Contributor

@thegecko thegecko commented Nov 12, 2023

This is a new feature to add USB, HID and Serial device access to the desktop by extending the existing WebUSB, WebHID and WebSerial functionality already offered in the web version #152310.

Since Electron version 25, Web-* device access has been possible using the functionality described here: https://www.electronjs.org/docs/latest/tutorial/devices

This ability is awesome because it allows access to functionality in Chrome already shipped as part of VS Code and means extension developers no longer have to include external packages such as node-usb or SerialPort. In many cases this approach should be more stable and reliable, too (e.g. chrome uses winusb for device access over libusb as used in node-usb).

To implement this addition, I've wired up a QuickPick dropdown for the user to choose the requested device leveraging Electron's APIs. Unfortunately the cleanest way I've been able to find to communicate between the electron-main and browser process is via the globals API. Happy to hear of a better way.

USB Selector:

Screenshot 2023-11-12 at 16 52 46

Serial Selector:

Screenshot 2023-11-12 at 17 00 18

HID Selector:

Screenshot 2023-11-12 at 17 01 59

Please note: As this utilises the existing features in chrome, obtaining a concrete device after authorising it is only possible in the desktop web extension host. Therefore this feature is only available to pure web extensions (those without a main entrypoint in package.json).

Notes on creating an extension to test this

To test this, create a VS Code Web Extension. This will create an extension which runs in the browser extensionHost, the only host which can see the browser navigator object.

Ensure there is only a browser entrypoint. Including a main entrypoint will run the extension on the local extensionHost.

Ensure there is no extensionKind specified. the UI extensionKind leads to errors with commands not being found.

In the VS Code extension, call one of the existing experimental commands for requesting device access:

  • workbench.experimental.requestUsbDevice
  • workbench.experimental.requestSerialPort
  • workbench.experimental.requestHidDevice

This returns a string ID for the device (and authorizes the device for use).

Use this ID to then lookup the concrete device you want to use in the the relevant navigator object:

@bpasero bpasero self-assigned this Nov 14, 2023
@bpasero bpasero added this to the Backlog milestone Nov 14, 2023
@thegecko
Copy link
Contributor Author

thegecko commented Mar 2, 2024

Anything required to progress this PR?

@bpasero
Copy link
Member

bpasero commented Dec 18, 2024

Is there an associated issue with sufficient upvotes and motion to justify this change? And an explainer about the scenarios this would enable?

@bpasero bpasero added the info-needed Issue requires more information from poster label Dec 18, 2024
@thegecko
Copy link
Contributor Author

thegecko commented Dec 18, 2024

Is there an associated issue with sufficient upvotes and motion to justify this change? And an explainer about the scenarios this would enable?

Issue raised here: #236450

@bpasero bpasero removed the info-needed Issue requires more information from poster label Dec 18, 2024
@bpasero
Copy link
Member

bpasero commented Dec 18, 2024

There are many conflicts in this PR btw.

@thegecko
Copy link
Contributor Author

There are many conflicts in this PR btw.

TBF, there weren't conflicts when I opened it a year ago and I got tired of keeping it up to date with no feedback.

If there is interest in this feature I'll revisit the PR.

@thegecko
Copy link
Contributor Author

There are many conflicts in this PR btw.

Resolved

@thegecko
Copy link
Contributor Author

thegecko commented Dec 24, 2024

The electron events (select-usb-device, select-hid-device, select-serial-port) which handle calls to the navigator device requests need to be wired into a UI for selection.

As these run in different parts of VS Code, I've had to implement an IPC channel to send a device list for rendering and return a selected item. There isn't an existing pattern to do this, so advice on my proposal would be appreciated.

@bpasero bpasero added keep Issues we should not close as out of scope under-discussion Issue is under discussion for relevance, priority, approach labels Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

keep Issues we should not close as out of scope under-discussion Issue is under discussion for relevance, priority, approach

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants