Skip to content

Commit

Permalink
serial: Expose connection state for Bluetooth serial ports
Browse files Browse the repository at this point in the history
Relevant spec change: WICG/serial#197

This CL adds a connected attribute to the SerialPort interface
that exposes the logical connection state of the serial port.
The behavior for Bluetooth serial ports is changed so that the
connect and disconnect events are dispatched when the connection
state of the Bluetooth device changes. The behavior is unchanged
for wired serial ports.

Intent to Prototype:
https://groups.google.com/a/chromium.org/g/blink-dev/c/dkz0IdP7kao

Bug: 1488031
Change-Id: I34b88246e62202c24eee08ad4a997aff7eadeebb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5039155
Reviewed-by: Alex Gough <ajgo@chromium.org>
Reviewed-by: John Abd-El-Malek <jam@chromium.org>
Commit-Queue: Matt Reynolds <mattreynolds@chromium.org>
Reviewed-by: Rick Byers <rbyers@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1268876}
  • Loading branch information
Matt Reynolds authored and BruceDai committed Mar 25, 2024
1 parent 9d08b06 commit e9449aa
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions resources/chromium/fake-serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ class FakeSerialService {
portInfo.hasUsbProductId = true;
portInfo.usbProductId = info.usbProductId;
}
portInfo.connected = true;
if (info?.connected !== undefined) {
portInfo.connected = info.connected;
}

let token = ++this.nextToken_;
portInfo.token = {high: 0n, low: BigInt(token)};
Expand All @@ -366,8 +370,10 @@ class FakeSerialService {
};
this.ports_.set(token, record);

for (let client of this.clients_) {
client.onPortAdded(portInfo);
if (portInfo.connected) {
for (let client of this.clients_) {
client.onPortConnectedStateChanged(portInfo);
}
}

return token;
Expand All @@ -381,8 +387,26 @@ class FakeSerialService {

this.ports_.delete(token);

record.portInfo.connected = false;
for (let client of this.clients_) {
client.onPortConnectedStateChanged(record.portInfo);
}
}

setPortConnectedState(token, connected) {
let record = this.ports_.get(token);
if (record === undefined) {
return;
}

let was_connected = record.portInfo.connected;
if (was_connected === connected) {
return;
}

record.portInfo.connected = connected;
for (let client of this.clients_) {
client.onPortRemoved(record.portInfo);
client.onPortConnectedStateChanged(record.portInfo);
}
}

Expand Down

0 comments on commit e9449aa

Please sign in to comment.