From dd1e354e2fa7f62e403e617a54c75d4e26a64299 Mon Sep 17 00:00:00 2001 From: SugarChes Date: Wed, 29 Jul 2026 16:50:07 +0800 Subject: [PATCH] fix(browser): contain CEF callback failures --- .../Lskj.Control/BrowserSetting/BsClient.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/插件库/Lskj.Control/BrowserSetting/BsClient.cs b/插件库/Lskj.Control/BrowserSetting/BsClient.cs index d818c5d..ca3db4e 100644 --- a/插件库/Lskj.Control/BrowserSetting/BsClient.cs +++ b/插件库/Lskj.Control/BrowserSetting/BsClient.cs @@ -1,5 +1,6 @@ using Lskj.Business; using Lskj.Model; +using Lskj.Util; using System; using System.Collections.Generic; using System.Linq; @@ -56,6 +57,19 @@ namespace Lskj.Control.BrowserSetting return contextMenuHandler; } protected override bool OnProcessMessageReceived(CefBrowser browser, CefFrame frame, CefProcessId sourceProcess, CefProcessMessage message) + { + try + { + return OnProcessMessageReceivedCore(browser, frame, sourceProcess, message); + } + catch (Exception exception) + { + ReportProcessMessageFailure(exception); + return true; + } + } + + private bool OnProcessMessageReceivedCore(CefBrowser browser, CefFrame frame, CefProcessId sourceProcess, CefProcessMessage message) { if (message.Name.Equals("OpenModule")) { @@ -150,6 +164,46 @@ namespace Lskj.Control.BrowserSetting return base.OnProcessMessageReceived(browser, frame, sourceProcess, message); } + + private static void ReportProcessMessageFailure(Exception exception) + { + try + { + LogHelper.Instance.WriteError(exception); + } + catch + { + // Logging must never rethrow across the native CEF callback. + } + + Action showError = () => + { + try + { + MessageUtil.Show(exception); + } + catch + { + // Error reporting must never terminate the browser callback. + } + }; + + try + { + System.Windows.Forms.Control mainControl = ERPInfo.Instance.MainControl; + if (mainControl == null || mainControl.IsDisposed || !mainControl.IsHandleCreated) + return; + + if (mainControl.InvokeRequired) + mainControl.BeginInvoke(showError); + else + showError(); + } + catch + { + // The main window may close while the CEF callback is reporting. + } + } public void Created(CefBrowser cefBrowser) { if (OnCreated != null)