[M130 Migration][VD] Web serial support 75/322675/1
authorzhaosy <shiyusy.zhao@samsung.com>
Mon, 14 Apr 2025 09:38:21 +0000 (17:38 +0800)
committerzhaosy <shiyusy.zhao@samsung.com>
Mon, 14 Apr 2025 09:41:47 +0000 (17:41 +0800)
 - Receive and implement events sent by chromium - electron
   - add "select-serial-port" event listener
   - add "select-serial-port-response" event listener
   - add "serial-port-added" event listener
   - add "serial-port-removed" event listener
 - Implement method used by electron for serial permissions
   - setPermissionCheckHandler and setDevicePermissionHandler

Related patch:
https://review.tizen.org/gerrit/c/platform/framework/web/chromium-efl/+/321911

Refer to:
https://review.tizen.org/gerrit/c/platform/framework/web/wrtjs/+/319816

Change-Id: I8429fcb76eb3c31dc480733b57402fcedd04bed4
Signed-off-by: zhaosy <shiyusy.zhao@samsung.com>
wrt_app/common/web_application_delegate.ts
wrt_app/src/tv/web_application_tv.ts
wrt_app/src/web_application.ts

index 95bd60dc6f77052bb0c6386a462de8a766987d8c..de83bbec7114e60bf9d4c3f053ce625713ea7ef4 100644 (file)
@@ -47,4 +47,5 @@ export class WebApplicationDelegate {
   certificateError(error: string) { return false; }
   setInitValue(appControl: any) { }
   preloadReady() { return true; }
+  setupWebSerialEventListener(webContents: any) { }
 }
index a6cfae281946e8d96c064efad916bdeb4aac40b2..52f80a61841088f86265512c59df5f1251d772b7 100644 (file)
@@ -72,6 +72,66 @@ export class WebApplicationDelegateTV extends WebApplicationDelegate {
     });
   }
 
+  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();
index 44acf03475a123d91864f0b591ba47f20e1da0d7..f5449e08553b1394e8df61492a4762327afa371e 100644 (file)
@@ -123,6 +123,8 @@ export class WebApplication {
           callback(true);
         }
       });
+
+      this.profileDelegate.setupWebSerialEventListener(webContents);
     });
 
     app.on('certificate-error', (event: any, webContents: any, url: string, error: string, certificate: any, callback: any) => {