Files
lserp_cs_6.0/插件库/Lskj.Control/BrowserSetting2/BsClient.cs
T

234 lines
7.7 KiB
C#

using Lskj.Business;
using Lskj.Model;
using Lskj.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xilium.CefGlue;
namespace Lskj.Control.BrowserSetting2
{
public class BsClient : CefClient
{
public FrmWebBrowser2 frmWebBrowser;
public event EventHandler OnCreated;
private readonly CefLifeSpanHandler lifeSpanHandler;
private readonly CefDownloadHandler downloadHandler;
private readonly CefKeyboardHandler keyboardHandler;
private readonly CefFocusHandler focusHandler;
private readonly CefRequestHandler requestHandler;
private readonly CefContextMenuHandler contextMenuHandler;
public BsClient(int RightKeyFlag = 0, bool AllowDownload = true)
{
if (SystemInfo.Instance.BrowserRight) RightKeyFlag = 1;
lifeSpanHandler = new BsLifeSpanHandler(this);
downloadHandler = new BsDownloadHandler(AllowDownload);
keyboardHandler = new BsKeyboardHandler();
focusHandler = new BsFocusHandler();
requestHandler = new BsRequestHandler();
contextMenuHandler = new BsContextMenuHandler(RightKeyFlag);
}
protected override CefLifeSpanHandler GetLifeSpanHandler()
{
return lifeSpanHandler;
}
protected override CefDownloadHandler GetDownloadHandler()
{
return downloadHandler;
}
protected override CefKeyboardHandler GetKeyboardHandler()
{
return keyboardHandler;
}
protected override CefFocusHandler GetFocusHandler()
{
return focusHandler;
}
protected override CefRequestHandler GetRequestHandler()
{
return requestHandler;
}
protected override CefContextMenuHandler GetContextMenuHandler()
{
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"))
{
string arg0 = message.Arguments.GetString(0);
string arg1 = message.Arguments.GetString(1);
string arg2 = message.Arguments.GetString(2);
string arg3 = message.Arguments.GetString(3);
string arg4 = message.Arguments.GetString(4);
PostToUiThread(
ERPInfo.Instance.MainControl,
delegate
{
BrowserSetting.BsOpenModuleHandler.OpenModule(
arg0,
arg1,
arg2,
arg3,
arg4);
});
}
else if (message.Name.Equals("OpenOnRightClick"))
{
string arg0 = message.Arguments.GetString(0);
string arg1 = message.Arguments.GetString(1);
string arg2 = message.Arguments.GetString(2);
string arg3 = message.Arguments.GetString(3);
PostToUiThread(
ERPInfo.Instance.MainControl,
delegate
{
BrowserSetting.BsOpenModuleHandler.OpenRightModule(
arg0,
arg1,
arg2,
arg3);
});
}
else if (message.Name.Equals("CloseWin"))
{
var browserControl = frmWebBrowser;
PostToUiThread(
browserControl,
delegate
{
// Resolve the owning form only after returning to the
// WinForms UI thread.
var parent = browserControl.Parent;
var baseForm = parent == null
? null
: parent.Parent as BaseForm;
if (baseForm == null || baseForm.IsDisposed)
return;
baseForm.Close();
});
}
return base.OnProcessMessageReceived(browser, frame, sourceProcess, message);
}
private static void PostToUiThread(
System.Windows.Forms.Control control,
Action action)
{
if (control == null || action == null || control.IsDisposed ||
!control.IsHandleCreated)
return;
Action guardedAction = delegate
{
if (control.IsDisposed)
return;
try
{
action();
}
catch (Exception exception)
{
ReportProcessMessageFailure(exception);
}
};
try
{
// Never keep CEF's UI thread blocked while WinForms opens a
// modal module that may create another CEF browser.
control.BeginInvoke(guardedAction);
}
catch (Exception exception)
{
ReportProcessMessageFailure(exception);
}
}
private static void ReportProcessMessageFailure(Exception exception)
{
try
{
LogHelper.Instance.WriteError(exception);
}
catch
{
// Never propagate logging failures into a native CEF callback.
}
Action showError = delegate
{
try
{
MessageUtil.Show(exception);
}
catch
{
}
};
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 callback is reporting.
}
}
public void Created(CefBrowser cefBrowser)
{
if (frmWebBrowser != null && OnCreated != null)
{
OnCreated(cefBrowser, EventArgs.Empty);
}
}
public bool Closing(CefBrowser browser)
{
var control = frmWebBrowser;
return control != null && control.BrowserClosing(browser);
}
public void Closed(CefBrowser browser)
{
var control = frmWebBrowser;
if (control != null)
{
control.BrowserClosed(browser);
if (control.cefBrowserSettings == null)
{
frmWebBrowser = null;
OnCreated = null;
}
}
}
}
}