fix: crash on invalid select-serial-port callback (#28602)

This commit is contained in:
Shelley Vohr 2021-04-12 13:18:39 +00:00 committed by GitHub
parent 6bd13cc98f
commit 2e9ed50bb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View file

@ -124,9 +124,13 @@ void SerialChooserController::OnDeviceChosen(const std::string& port_id) {
std::find_if(ports_.begin(), ports_.end(), [&port_id](const auto& ptr) {
return ptr->token.ToString() == port_id;
});
chooser_context_->GrantPortPermission(requesting_origin_, embedding_origin_,
*it->get());
RunCallback(it->Clone());
if (it != ports_.end()) {
chooser_context_->GrantPortPermission(requesting_origin_,
embedding_origin_, *it->get());
RunCallback(it->Clone());
} else {
RunCallback(/*port=*/nullptr);
}
}
}

View file

@ -1586,6 +1586,14 @@ describe('navigator.serial', () => {
expect(port).to.equal('NotFoundError: No port selected by the user.');
});
it('does not crash when select-serial-port is called with an invalid port', async () => {
w.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
callback('i-do-not-exist');
});
const port = await getPorts();
expect(port).to.equal('NotFoundError: No port selected by the user.');
});
it('returns a port when select-serial-port event is defined', async () => {
w.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
callback(portList[0].portId);