[FIX] web_editor: prevent creation of RTCPeerConnection when offline

Before this commit, when the browser was offline and when an attempt
to make a RTCPeerConnection was made, a traceback was raised by
firefox:

> InvalidStateError: Can't create RTCPeerConnections when the network is
> down

This commit prevents the creation of the RTCPeerConnection when the
browser is offline.

task-3186872

closes odoo/odoo#134236

X-original-commit: 8f9a3e1ebd05d9df8ec0d82b93967f1a62f879c4
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
Nicolas Bayet
2023-09-05 07:19:51 +00:00
parent 0f213505bb
commit e4bac8c129
2 changed files with 23 additions and 15 deletions
@@ -84,7 +84,7 @@ const baseNotificationMethods = {
'An offer has been received for a non-existent peer connection - client: ' +
notification.fromClientId,
);
console.trace(pc.connectionState);
console.trace(pc && pc.connectionState);
console.groupEnd();
}
return;
@@ -181,8 +181,8 @@ export class PeerToPeer {
return Object.entries(this.clientsInfos)
.filter(
([id, infos]) =>
infos.peerConnection.iceConnectionState === 'connected' &&
infos.dataChannel.readyState === 'open',
infos.peerConnection && infos.peerConnection.iceConnectionState === 'connected' &&
infos.dataChannel && infos.dataChannel.readyState === 'open',
)
.map(([id]) => id);
}
@@ -194,8 +194,8 @@ export class PeerToPeer {
if (!clientInfos) return;
clearTimeout(clientInfos.fallbackTimeout);
clearTimeout(clientInfos.zombieTimeout);
clientInfos.dataChannel.close();
clientInfos.peerConnection.close();
clientInfos.dataChannel && clientInfos.dataChannel.close();
clientInfos.peerConnection && clientInfos.peerConnection.close();
delete this.clientsInfos[clientId];
}
@@ -381,6 +381,10 @@ export class PeerToPeer {
iceCandidateBuffer: [],
backoffFactor: 0,
};
if (!navigator.onLine) {
return this.clientsInfos[clientId];
}
const pc = new RTCPeerConnection(this.options.peerConnectionConfig);
if (makeOffer) {
@@ -425,10 +429,12 @@ export class PeerToPeer {
this.removeClient(clientId);
break;
case 'disconnected':
await this._recoverConnection(clientId, {
delay: 3000,
reason: 'ice connection disconnected',
});
if (navigator.onLine) {
await this._recoverConnection(clientId, {
delay: 3000,
reason: 'ice connection disconnected',
});
}
break;
case 'connected':
this.clientsInfos[clientId].backoffFactor = 0;
@@ -445,10 +451,12 @@ export class PeerToPeer {
this.removeClient(clientId);
break;
case 'disconnected':
await this._recoverConnection(clientId, {
delay: 3000,
reason: 'connection disconnected',
});
if (navigator.onLine) {
await this._recoverConnection(clientId, {
delay: 3000,
reason: 'connection disconnected',
});
}
break;
case 'connected':
case 'completed':
@@ -618,7 +626,7 @@ export class PeerToPeer {
// If there is no connection after 10 seconds, terminate.
clientInfos.zombieTimeout = setTimeout(() => {
if (clientInfos && clientInfos.dataChannel.readyState !== 'open') {
if (clientInfos && clientInfos.dataChannel && clientInfos.dataChannel.readyState !== 'open') {
if (debugShowLog) console.log(`%c KILL ZOMBIE ${clientId}`, 'background: red;');
this.removeClient(clientId);
} else {
@@ -829,7 +829,7 @@ export class Wysiwyg extends Component {
const clientsInfos = Object.values(this.ptp.clientsInfos);
const couldBeDisconnected =
Boolean(clientsInfos.length) &&
clientsInfos.every((x) => PTP_CLIENT_DISCONNECTED_STATES.includes(x.peerConnection.connectionState));
clientsInfos.every((x) => PTP_CLIENT_DISCONNECTED_STATES.includes(x.peerConnection && x.peerConnection.connectionState));
if (couldBeDisconnected) {
this._offlineTimeout = setTimeout(() => {