SafeDispatch/Safedispatch_4_0/maptab/CefSharpChromiumWebBrowser.cs

186 lines
6.0 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using CefSharp.WinForms;
using System.Windows.Forms;
using System.Diagnostics;
using CefSharp;
using Safedispatch_4_0;
using SafeMobileLib;
namespace Dispatcher.maptab
{
public partial class CefSharpChromiumWebBrowser : UserControl
{
private readonly String URL = @"https://maps.google.com";
private ChromiumWebBrowser browser;
private static readonly bool DebuggingSubProcess = Debugger.IsAttached;
private static Boolean IsLoaded = false;
public CefSharpChromiumWebBrowser(String url)
{
this.URL = url;
this.Dock = DockStyle.Fill;
InitializeComponent();
//InitializeCefSharp();
browser = InstantiateBrowser(url);
this.Controls.Add(browser);
}
private void InitializeCefSharp()
{
var settings = new CefSettings();
CefSharpSettings.FocusedNodeChangedEnabled = true;
CefSharpSettings.SubprocessExitIfParentProcessClosed = true;
CefSharpSettings.WcfEnabled = true;
settings.CefCommandLineArgs.Add("ignore-certificate-errors");
settings.IgnoreCertificateErrors = true;
// Enable Precise Memory Info
settings.CefCommandLineArgs.Add("enable-precise-memory-info", "1");
if (MainForm2.MapDebugConsole)
settings.CefCommandLineArgs.Add("remote-debugging-port", "9222");
// initialize the browser so the settings arguments are passed to the instances
if (!Cef.IsInitialized)
Cef.Initialize(settings);
}
private ChromiumWebBrowser InstantiateBrowser(String url)
{
// create a Chromium Web Browser
ChromiumWebBrowser browser = new ChromiumWebBrowser(url)
{
Dock = DockStyle.Fill
};
ChromiumCallBack callbackObject = new ChromiumCallBack();
callbackObject.OnCallBackMethodCalled += delegate (String addressURL)
{
if (OnURLResponseReceived != null)
OnURLResponseReceived(addressURL);
};
// add a callback object to the browser in order for
//browser.RegisterJsObject("callbackObj", callbackObject);
browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
browser.JavascriptObjectRepository.Register("callbackObj", callbackObject, false);
browser.LoadingStateChanged += delegate (object sender, LoadingStateChangedEventArgs e)
{
// the web page had finished loading
if (e.CanReload && !e.IsLoading && OnWebPageLoaded != null)
{
IsLoaded = true;
OnWebPageLoaded();
}
this.Invoke((MethodInvoker)delegate
{
//pbLoading.Visible = false;
});
};
browser.ConsoleMessage += delegate (object sender, ConsoleMessageEventArgs args)
{};
browser.StatusMessage += delegate (object sender, StatusMessageEventArgs args)
{};
browser.TitleChanged += delegate (object sender, TitleChangedEventArgs args)
{};
browser.AddressChanged += delegate (object sender, AddressChangedEventArgs args)
{
if (OnBrowserURLChanged != null)
OnBrowserURLChanged(args.Address);
};
browser.IsBrowserInitializedChanged += delegate (object sender, EventArgs args)
{};
return browser;
}
/// <summary>
/// Load a specific webpage into the browser
/// </summary>
/// <param name="webPageURL">The web page URL which needs to be loaded</param>
public void LoadWebPage(String webPageURL)
{
if (browser.IsBrowserInitialized && IsLoaded)
browser.Load(webPageURL);
}
/// <summary>
/// Execute a javascript asyncronous
/// </summary>
/// <param name="script">Script text which needs to be executed</param>
public void ExecuteScript(string script)
{
try
{
Utils.WriteLine($"ExecuteScript {script}", ConsoleColor.White);
/*
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("ExecuteScript" + script);
Console.ForegroundColor = ConsoleColor.Gray;*/
if(browser.IsBrowserInitialized)
browser.ExecuteScriptAsync(script);
else
Utils.WriteLine($"Browser is not initialised to execute script {script}", ConsoleColor.White);
}
catch (Exception ex)
{
Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
}
}
/// <summary>
/// Close the CEF Library
/// </summary>
public void Stop()
{
browser.Dispose();
Cef.Shutdown();
}
/// <summary>
/// Register a javascript object to which the Javascript will do queries or raise events
/// </summary>
/// <param name="key">Name of the method and the key with will be used by Javascript</param>
/// <param name="callbackObject">Javascript object which will be passed to the javascript</param>
//public void RegisterJsObject(String key, Object callbackObject)
//{
// browser.RegisterJsObject(key, callbackObject);
//}
public delegate void WebPageLoaded();
public event WebPageLoaded OnWebPageLoaded;
public delegate void BrowserURLChanged(String currentURL);
public event BrowserURLChanged OnBrowserURLChanged;
public delegate void URLResponseReceived(String urlResponse);
public event URLResponseReceived OnURLResponseReceived;
}
}