});
}
+ setupWebSerialEventListener(webContents: any) {
+ if (!this.tv.webSerialSupport())
+ return;
+
+ console.log('setup web serial event listener');
+ webContents.session.on('select-serial-port', (event: any, ports: any, webContents: any, callback: any) => {
+ event.preventDefault();
+ let ports_ = []
+ for (let i = 0; i < ports.length; i++) {
+ console.log(ports[i].portId);
+ console.log(ports[i].displayName);
+ ports_[i] = {
+ portId: ports[i].portId,
+ displayName: ports[i].displayName
+ }
+ }
+ const id = ++this.webApplication.pendingID;
+ let data = {
+ id: id,
+ ports: ports_
+ };
+ let jsonData = JSON.stringify(data);
+ this.webApplication.pendingCallbacks.set(id, callback);
+ this.tv.handleSerialDeviceChooser(jsonData);
+ });
+
+ webContents.session.on('serial-port-added', (event: any, port: any) => {
+ port['isAdd'] = true;
+ this.tv.handleSerialDeviceUpdate(JSON.stringify(port));
+ });
+
+ webContents.session.on('serial-port-removed', (event: any, port: any) => {
+ port['isAdd'] = false;
+ this.tv.handleSerialDeviceUpdate(JSON.stringify(port));
+ });
+
+ webContents.session.setPermissionCheckHandler((webContents: any, permission: any, requestingOrigin: any, details: any) => {
+ if (permission === 'serial' && details.securityOrigin === 'file:///') {
+ return true;
+ }
+ });
+
+ webContents.session.setDevicePermissionHandler((details: any) => {
+ if (details.deviceType === 'serial' && details.origin === 'file://') {
+ return true;
+ }
+ });
+
+ wrt.on('select-serial-port-response', (event: any, id: number, portId: string, isPositive: boolean) => {
+ let callback = this.webApplication.pendingCallbacks.get(id);
+ if (typeof callback === 'function') {
+ if (isPositive)
+ callback(portId);
+ else
+ callback("");
+ this.webApplication.pendingCallbacks.delete(id);
+ }
+ });
+ }
+
onDidFinishLoad() {
if (this.inspectorSrc) {
this.showInspectorGuide();