From b4e0493b310fe6a0552e05bae1555799db9c98ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Fri, 22 Jul 2022 15:38:07 +0200 Subject: [PATCH] SEBWIN-510: Changed initialization of JavaScript API. --- SafeExamBrowser.Browser/BrowserControl.cs | 36 ++------- SafeExamBrowser.Browser/BrowserWindow.cs | 13 +-- .../Handlers/DownloadHandler.cs | 2 +- .../Handlers/RenderProcessMessageHandler.cs | 51 ++++++++++++ .../Handlers/ResourceHandler.cs | 2 - .../SafeExamBrowser.Browser.csproj | 5 ++ .../Wrapper/CefSharpBrowserControl.cs | 25 ++++++ .../Wrapper/CefSharpPopupControl.cs | 24 ++++++ .../Events/ContextCreatedEventHandler.cs | 14 ++++ .../Events/ContextReleasedEventHandler.cs | 14 ++++ .../Events/FocusedNodeChangedEventHandler.cs | 14 ++++ .../Events/UncaughtExceptionEventHandler.cs | 14 ++++ .../RenderProcessMessageHandlerSwitch.cs | 81 +++++++++++++++++++ .../Wrapper/ICefSharpControl.cs | 8 ++ 14 files changed, 261 insertions(+), 42 deletions(-) create mode 100644 SafeExamBrowser.Browser/Handlers/RenderProcessMessageHandler.cs create mode 100644 SafeExamBrowser.Browser/Wrapper/Events/ContextCreatedEventHandler.cs create mode 100644 SafeExamBrowser.Browser/Wrapper/Events/ContextReleasedEventHandler.cs create mode 100644 SafeExamBrowser.Browser/Wrapper/Events/FocusedNodeChangedEventHandler.cs create mode 100644 SafeExamBrowser.Browser/Wrapper/Events/UncaughtExceptionEventHandler.cs create mode 100644 SafeExamBrowser.Browser/Wrapper/Handlers/RenderProcessMessageHandlerSwitch.cs diff --git a/SafeExamBrowser.Browser/BrowserControl.cs b/SafeExamBrowser.Browser/BrowserControl.cs index 2e42ffdd..563e41d1 100644 --- a/SafeExamBrowser.Browser/BrowserControl.cs +++ b/SafeExamBrowser.Browser/BrowserControl.cs @@ -7,14 +7,9 @@ */ using System; -using System.Threading.Tasks; using CefSharp; -using SafeExamBrowser.Browser.Content; using SafeExamBrowser.Browser.Wrapper; using SafeExamBrowser.Browser.Wrapper.Events; -using SafeExamBrowser.Configuration.Contracts; -using SafeExamBrowser.Configuration.Contracts.Cryptography; -using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.UserInterface.Contracts.Browser; using SafeExamBrowser.UserInterface.Contracts.Browser.Data; using SafeExamBrowser.UserInterface.Contracts.Browser.Events; @@ -23,14 +18,12 @@ namespace SafeExamBrowser.Browser { internal class BrowserControl : IBrowserControl { - private readonly AppConfig appConfig; - private readonly ContentLoader contentLoader; private readonly ICefSharpControl control; private readonly IDialogHandler dialogHandler; private readonly IDisplayHandler displayHandler; private readonly IDownloadHandler downloadHandler; private readonly IKeyboardHandler keyboardHandler; - private readonly IKeyGenerator generator; + private readonly IRenderProcessMessageHandler renderProcessMessageHandler; private readonly IRequestHandler requestHandler; public string Address => control.Address; @@ -44,24 +37,20 @@ namespace SafeExamBrowser.Browser public event TitleChangedEventHandler TitleChanged; public BrowserControl( - AppConfig appConfig, ICefSharpControl control, IDialogHandler dialogHandler, IDisplayHandler displayHandler, IDownloadHandler downloadHandler, IKeyboardHandler keyboardHandler, - IKeyGenerator generator, - IRequestHandler requestHandler, - IText text) + IRenderProcessMessageHandler renderProcessMessageHandler, + IRequestHandler requestHandler) { - this.appConfig = appConfig; - this.contentLoader = new ContentLoader(text); this.control = control; this.dialogHandler = dialogHandler; this.displayHandler = displayHandler; this.downloadHandler = downloadHandler; this.keyboardHandler = keyboardHandler; - this.generator = generator; + this.renderProcessMessageHandler = renderProcessMessageHandler; this.requestHandler = requestHandler; } @@ -97,10 +86,12 @@ namespace SafeExamBrowser.Browser control.BeforeBrowse += (w, b, f, r, u, i, a) => a.Value = requestHandler.OnBeforeBrowse(w, b, f, r, u, i); control.BeforeDownload += (w, b, d, c) => downloadHandler.OnBeforeDownload(w, b, d, c); control.CanDownload += (w, b, u, r, a) => a.Value = downloadHandler.CanDownload(w, b, u, r); + control.ContextCreated += (w, b, f) => renderProcessMessageHandler.OnContextCreated(w, b, f); + control.ContextReleased += (w, b, f) => renderProcessMessageHandler.OnContextReleased(w, b, f); control.DownloadUpdated += (w, b, d, c) => downloadHandler.OnDownloadUpdated(w, b, d, c); control.FaviconUrlChanged += (w, b, u) => displayHandler.OnFaviconUrlChange(w, b, u); control.FileDialogRequested += (w, b, m, t, d, f, c) => dialogHandler.OnFileDialog(w, b, m, t, d, f, c); - control.FrameLoadStart += Control_FrameLoadStart; + control.FocusedNodeChanged += (w, b, f, n) => renderProcessMessageHandler.OnFocusedNodeChanged(w, b, f, n); control.IsBrowserInitializedChanged += Control_IsBrowserInitializedChanged; control.KeyEvent += (w, b, t, k, n, m, s) => keyboardHandler.OnKeyEvent(w, b, t, k, n, m, s); control.LoadError += (o, e) => LoadFailed?.Invoke((int) e.ErrorCode, e.ErrorText, e.Frame.IsMain, e.FailedUrl); @@ -110,6 +101,7 @@ namespace SafeExamBrowser.Browser control.PreKeyEvent += (IWebBrowser w, IBrowser b, KeyType t, int k, int n, CefEventFlags m, bool i, ref bool s, GenericEventArgs a) => a.Value = keyboardHandler.OnPreKeyEvent(w, b, t, k, n, m, i, ref s); control.ResourceRequestHandlerRequired += (IWebBrowser w, IBrowser b, IFrame f, IRequest r, bool n, bool d, string i, ref bool h, ResourceRequestEventArgs a) => a.Handler = requestHandler.GetResourceRequestHandler(w, b, f, r, n, d, i, ref h); control.TitleChanged += (o, e) => TitleChanged?.Invoke(e.Title); + control.UncaughtExceptionEvent += (w, b, f, e) => renderProcessMessageHandler.OnUncaughtException(w, b, f, e); } public void NavigateBackwards() @@ -142,18 +134,6 @@ namespace SafeExamBrowser.Browser control.BrowserCore.SetZoomLevel(level); } - private void Control_FrameLoadStart(object sender, FrameLoadStartEventArgs e) - { - Task.Run(() => - { - var browserExamKey = generator.CalculateBrowserExamKeyHash(e.Url); - var configurationKey = generator.CalculateConfigurationKeyHash(e.Url); - var api = contentLoader.LoadApi(browserExamKey, configurationKey, appConfig.ProgramBuildVersion); - - e.Frame.ExecuteJavaScriptAsync(api); - }); - } - private void Control_IsBrowserInitializedChanged(object sender, EventArgs e) { if (control.IsBrowserInitialized) diff --git a/SafeExamBrowser.Browser/BrowserWindow.cs b/SafeExamBrowser.Browser/BrowserWindow.cs index 0bfdc006..04323176 100644 --- a/SafeExamBrowser.Browser/BrowserWindow.cs +++ b/SafeExamBrowser.Browser/BrowserWindow.cs @@ -145,12 +145,12 @@ namespace SafeExamBrowser.Browser internal void InitializeControl() { var cefSharpControl = default(ICefSharpControl); - var contextMenuHandler = new ContextMenuHandler(); var dialogHandler = new DialogHandler(); var displayHandler = new DisplayHandler(); var downloadLogger = logger.CloneFor($"{nameof(DownloadHandler)} #{Id}"); var downloadHandler = new DownloadHandler(appConfig, downloadLogger, settings, WindowSettings); var keyboardHandler = new KeyboardHandler(); + var renderProcessMessageHandler = new RenderProcessMessageHandler(appConfig, keyGenerator, text); var requestFilter = new RequestFilter(); var requestLogger = logger.CloneFor($"{nameof(RequestHandler)} #{Id}"); var resourceHandler = new ResourceHandler(appConfig, requestFilter, keyGenerator, logger, settings, WindowSettings, text); @@ -187,16 +187,7 @@ namespace SafeExamBrowser.Browser InitializeRequestFilter(requestFilter); - Control = new BrowserControl( - appConfig, - cefSharpControl, - dialogHandler, - displayHandler, - downloadHandler, - keyboardHandler, - keyGenerator, - requestHandler, - text); + Control = new BrowserControl(cefSharpControl, dialogHandler, displayHandler, downloadHandler, keyboardHandler, renderProcessMessageHandler, requestHandler); Control.AddressChanged += Control_AddressChanged; Control.LoadFailed += Control_LoadFailed; Control.LoadingStateChanged += Control_LoadingStateChanged; diff --git a/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs b/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs index bd9547bc..5b21c2fc 100644 --- a/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs @@ -100,7 +100,7 @@ namespace SafeExamBrowser.Browser.Handlers { logger.Debug($"Download of '{downloadItem.FullPath}' {(downloadItem.IsComplete ? "is complete" : "was cancelled")}."); - if (callbacks.TryRemove(downloadItem.Id, out DownloadFinishedCallback finished) && finished != null) + if (callbacks.TryRemove(downloadItem.Id, out var finished) && finished != null) { Task.Run(() => finished.Invoke(downloadItem.IsComplete, downloadItem.Url, downloadItem.FullPath)); } diff --git a/SafeExamBrowser.Browser/Handlers/RenderProcessMessageHandler.cs b/SafeExamBrowser.Browser/Handlers/RenderProcessMessageHandler.cs new file mode 100644 index 00000000..145cff0a --- /dev/null +++ b/SafeExamBrowser.Browser/Handlers/RenderProcessMessageHandler.cs @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using CefSharp; +using SafeExamBrowser.Browser.Content; +using SafeExamBrowser.Configuration.Contracts; +using SafeExamBrowser.Configuration.Contracts.Cryptography; +using SafeExamBrowser.I18n.Contracts; + +namespace SafeExamBrowser.Browser.Handlers +{ + internal class RenderProcessMessageHandler : IRenderProcessMessageHandler + { + private readonly AppConfig appConfig; + private readonly ContentLoader contentLoader; + private readonly IKeyGenerator keyGenerator; + + internal RenderProcessMessageHandler(AppConfig appConfig, IKeyGenerator keyGenerator, IText text) + { + this.appConfig = appConfig; + this.contentLoader = new ContentLoader(text); + this.keyGenerator = keyGenerator; + } + + public void OnContextCreated(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame) + { + var browserExamKey = keyGenerator.CalculateBrowserExamKeyHash(frame.Url); + var configurationKey = keyGenerator.CalculateConfigurationKeyHash(frame.Url); + var api = contentLoader.LoadApi(browserExamKey, configurationKey, appConfig.ProgramBuildVersion); + + frame.ExecuteJavaScriptAsync(api); + } + + public void OnContextReleased(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame) + { + } + + public void OnFocusedNodeChanged(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IDomNode node) + { + } + + public void OnUncaughtException(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, JavascriptException exception) + { + } + } +} diff --git a/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs b/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs index 53210f8c..284760df 100644 --- a/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs @@ -38,7 +38,6 @@ namespace SafeExamBrowser.Browser.Handlers private readonly IKeyGenerator keyGenerator; private readonly ILogger logger; private readonly BrowserSettings settings; - private readonly IText text; private readonly WindowSettings windowSettings; private IResourceHandler contentHandler; @@ -63,7 +62,6 @@ namespace SafeExamBrowser.Browser.Handlers this.logger = logger; this.settings = settings; this.windowSettings = windowSettings; - this.text = text; } protected override IResourceHandler GetResourceHandler(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IRequest request) diff --git a/SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj b/SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj index 0c49d797..1aa931c9 100644 --- a/SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj +++ b/SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj @@ -117,9 +117,12 @@ + + + @@ -127,11 +130,13 @@ + + diff --git a/SafeExamBrowser.Browser/Wrapper/CefSharpBrowserControl.cs b/SafeExamBrowser.Browser/Wrapper/CefSharpBrowserControl.cs index dbe4a14c..8c1e5b7d 100644 --- a/SafeExamBrowser.Browser/Wrapper/CefSharpBrowserControl.cs +++ b/SafeExamBrowser.Browser/Wrapper/CefSharpBrowserControl.cs @@ -21,14 +21,18 @@ namespace SafeExamBrowser.Browser.Wrapper public event BeforeBrowseEventHandler BeforeBrowse; public event BeforeDownloadEventHandler BeforeDownload; public event CanDownloadEventHandler CanDownload; + public event ContextCreatedEventHandler ContextCreated; + public event ContextReleasedEventHandler ContextReleased; public event DownloadUpdatedEventHandler DownloadUpdated; public event FaviconUrlChangedEventHandler FaviconUrlChanged; public event FileDialogRequestedEventHandler FileDialogRequested; + public event FocusedNodeChangedEventHandler FocusedNodeChanged; public event KeyEventHandler KeyEvent; public event LoadingProgressChangedEventHandler LoadingProgressChanged; public event OpenUrlFromTabEventHandler OpenUrlFromTab; public event PreKeyEventHandler PreKeyEvent; public event ResourceRequestEventHandler ResourceRequestHandlerRequired; + public event UncaughtExceptionEventHandler UncaughtExceptionEvent; public CefSharpBrowserControl(ILifeSpanHandler lifeSpanHandler, string url) : base(url) { @@ -38,6 +42,7 @@ namespace SafeExamBrowser.Browser.Wrapper KeyboardHandler = new KeyboardHandlerSwitch(); LifeSpanHandler = lifeSpanHandler; MenuHandler = new ContextMenuHandler(); + RenderProcessMessageHandler = new RenderProcessMessageHandlerSwitch(); RequestHandler = new RequestHandlerSwitch(); } @@ -71,6 +76,16 @@ namespace SafeExamBrowser.Browser.Wrapper CanDownload?.Invoke(webBrowser, browser, url, requestMethod, args); } + public void OnContextCreated(IWebBrowser webBrowser, IBrowser browser, IFrame frame) + { + ContextCreated?.Invoke(webBrowser, browser, frame); + } + + public void OnContextReleased(IWebBrowser webBrowser, IBrowser browser, IFrame frame) + { + ContextReleased?.Invoke(webBrowser, browser, frame); + } + public void OnDownloadUpdated(IWebBrowser webBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { DownloadUpdated?.Invoke(webBrowser, browser, downloadItem, callback); @@ -86,6 +101,11 @@ namespace SafeExamBrowser.Browser.Wrapper FileDialogRequested?.Invoke(webBrowser, browser, mode, title, defaultFilePath, acceptFilters, callback); } + public void OnFocusedNodeChanged(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IDomNode node) + { + FocusedNodeChanged?.Invoke(webBrowser, browser, frame, node); + } + public void OnKeyEvent(IWebBrowser webBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey) { KeyEvent?.Invoke(webBrowser, browser, type, windowsKeyCode, nativeKeyCode, modifiers, isSystemKey); @@ -105,5 +125,10 @@ namespace SafeExamBrowser.Browser.Wrapper { PreKeyEvent?.Invoke(webBrowser, browser, type, windowsKeyCode, nativeKeyCode, modifiers, isSystemKey, ref isKeyboardShortcut, args); } + + public void OnUncaughtException(IWebBrowser webBrowser, IBrowser browser, IFrame frame, JavascriptException exception) + { + UncaughtExceptionEvent?.Invoke(webBrowser, browser, frame, exception); + } } } diff --git a/SafeExamBrowser.Browser/Wrapper/CefSharpPopupControl.cs b/SafeExamBrowser.Browser/Wrapper/CefSharpPopupControl.cs index 2b4a3fed..2b98ab8e 100644 --- a/SafeExamBrowser.Browser/Wrapper/CefSharpPopupControl.cs +++ b/SafeExamBrowser.Browser/Wrapper/CefSharpPopupControl.cs @@ -19,14 +19,18 @@ namespace SafeExamBrowser.Browser.Wrapper public event BeforeBrowseEventHandler BeforeBrowse; public event BeforeDownloadEventHandler BeforeDownload; public event CanDownloadEventHandler CanDownload; + public event ContextCreatedEventHandler ContextCreated; + public event ContextReleasedEventHandler ContextReleased; public event DownloadUpdatedEventHandler DownloadUpdated; public event FaviconUrlChangedEventHandler FaviconUrlChanged; public event FileDialogRequestedEventHandler FileDialogRequested; + public event FocusedNodeChangedEventHandler FocusedNodeChanged; public event KeyEventHandler KeyEvent; public event LoadingProgressChangedEventHandler LoadingProgressChanged; public event OpenUrlFromTabEventHandler OpenUrlFromTab; public event PreKeyEventHandler PreKeyEvent; public event ResourceRequestEventHandler ResourceRequestHandlerRequired; + public event UncaughtExceptionEventHandler UncaughtExceptionEvent; void ICefSharpControl.Dispose(bool disposing) { @@ -66,6 +70,16 @@ namespace SafeExamBrowser.Browser.Wrapper CanDownload?.Invoke(webBrowser, browser, url, requestMethod, args); } + public void OnContextCreated(IWebBrowser webBrowser, IBrowser browser, IFrame frame) + { + ContextCreated?.Invoke(webBrowser, browser, frame); + } + + public void OnContextReleased(IWebBrowser webBrowser, IBrowser browser, IFrame frame) + { + ContextReleased?.Invoke(webBrowser, browser, frame); + } + public void OnDownloadUpdated(IWebBrowser webBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { DownloadUpdated?.Invoke(webBrowser, browser, downloadItem, callback); @@ -81,6 +95,11 @@ namespace SafeExamBrowser.Browser.Wrapper FileDialogRequested?.Invoke(webBrowser, browser, mode, title, defaultFilePath, acceptFilters, callback); } + public void OnFocusedNodeChanged(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IDomNode node) + { + FocusedNodeChanged?.Invoke(webBrowser, browser, frame, node); + } + public void OnKeyEvent(IWebBrowser webBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey) { KeyEvent?.Invoke(webBrowser, browser, type, windowsKeyCode, nativeKeyCode, modifiers, isSystemKey); @@ -100,5 +119,10 @@ namespace SafeExamBrowser.Browser.Wrapper { PreKeyEvent?.Invoke(webBrowser, browser, type, windowsKeyCode, nativeKeyCode, modifiers, isSystemKey, ref isKeyboardShortcut, args); } + + public void OnUncaughtException(IWebBrowser webBrowser, IBrowser browser, IFrame frame, JavascriptException exception) + { + UncaughtExceptionEvent?.Invoke(webBrowser, browser, frame, exception); + } } } diff --git a/SafeExamBrowser.Browser/Wrapper/Events/ContextCreatedEventHandler.cs b/SafeExamBrowser.Browser/Wrapper/Events/ContextCreatedEventHandler.cs new file mode 100644 index 00000000..ebd0bd48 --- /dev/null +++ b/SafeExamBrowser.Browser/Wrapper/Events/ContextCreatedEventHandler.cs @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using CefSharp; + +namespace SafeExamBrowser.Browser.Wrapper.Events +{ + internal delegate void ContextCreatedEventHandler(IWebBrowser webBrowser, IBrowser browser, IFrame frame); +} diff --git a/SafeExamBrowser.Browser/Wrapper/Events/ContextReleasedEventHandler.cs b/SafeExamBrowser.Browser/Wrapper/Events/ContextReleasedEventHandler.cs new file mode 100644 index 00000000..f2fdcd8f --- /dev/null +++ b/SafeExamBrowser.Browser/Wrapper/Events/ContextReleasedEventHandler.cs @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using CefSharp; + +namespace SafeExamBrowser.Browser.Wrapper.Events +{ + internal delegate void ContextReleasedEventHandler(IWebBrowser webBrowser, IBrowser browser, IFrame frame); +} diff --git a/SafeExamBrowser.Browser/Wrapper/Events/FocusedNodeChangedEventHandler.cs b/SafeExamBrowser.Browser/Wrapper/Events/FocusedNodeChangedEventHandler.cs new file mode 100644 index 00000000..bfafe7f3 --- /dev/null +++ b/SafeExamBrowser.Browser/Wrapper/Events/FocusedNodeChangedEventHandler.cs @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using CefSharp; + +namespace SafeExamBrowser.Browser.Wrapper.Events +{ + internal delegate void FocusedNodeChangedEventHandler(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IDomNode node); +} diff --git a/SafeExamBrowser.Browser/Wrapper/Events/UncaughtExceptionEventHandler.cs b/SafeExamBrowser.Browser/Wrapper/Events/UncaughtExceptionEventHandler.cs new file mode 100644 index 00000000..971209bf --- /dev/null +++ b/SafeExamBrowser.Browser/Wrapper/Events/UncaughtExceptionEventHandler.cs @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using CefSharp; + +namespace SafeExamBrowser.Browser.Wrapper.Events +{ + internal delegate void UncaughtExceptionEventHandler(IWebBrowser webBrowser, IBrowser browser, IFrame frame, JavascriptException exception); +} diff --git a/SafeExamBrowser.Browser/Wrapper/Handlers/RenderProcessMessageHandlerSwitch.cs b/SafeExamBrowser.Browser/Wrapper/Handlers/RenderProcessMessageHandlerSwitch.cs new file mode 100644 index 00000000..ca6b67bc --- /dev/null +++ b/SafeExamBrowser.Browser/Wrapper/Handlers/RenderProcessMessageHandlerSwitch.cs @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using CefSharp; +using CefSharp.WinForms; +using CefSharp.WinForms.Host; + +namespace SafeExamBrowser.Browser.Wrapper.Handlers +{ + internal class RenderProcessMessageHandlerSwitch : IRenderProcessMessageHandler + { + public void OnContextCreated(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame) + { + if (browser.IsPopup) + { + var control = ChromiumHostControl.FromBrowser(browser) as CefSharpPopupControl; + + control?.OnContextCreated(chromiumWebBrowser, browser, frame); + } + else + { + var control = ChromiumWebBrowser.FromBrowser(browser) as CefSharpBrowserControl; + + control?.OnContextCreated(chromiumWebBrowser, browser, frame); + } + } + + public void OnContextReleased(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame) + { + if (browser.IsPopup) + { + var control = ChromiumHostControl.FromBrowser(browser) as CefSharpPopupControl; + + control?.OnContextReleased(chromiumWebBrowser, browser, frame); + } + else + { + var control = ChromiumWebBrowser.FromBrowser(browser) as CefSharpBrowserControl; + + control?.OnContextReleased(chromiumWebBrowser, browser, frame); + } + } + + public void OnFocusedNodeChanged(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IDomNode node) + { + if (browser.IsPopup) + { + var control = ChromiumHostControl.FromBrowser(browser) as CefSharpPopupControl; + + control?.OnFocusedNodeChanged(chromiumWebBrowser, browser, frame, node); + } + else + { + var control = ChromiumWebBrowser.FromBrowser(browser) as CefSharpBrowserControl; + + control?.OnFocusedNodeChanged(chromiumWebBrowser, browser, frame, node); + } + } + + public void OnUncaughtException(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, JavascriptException exception) + { + if (browser.IsPopup) + { + var control = ChromiumHostControl.FromBrowser(browser) as CefSharpPopupControl; + + control?.OnUncaughtException(chromiumWebBrowser, browser, frame, exception); + } + else + { + var control = ChromiumWebBrowser.FromBrowser(browser) as CefSharpBrowserControl; + + control?.OnUncaughtException(chromiumWebBrowser, browser, frame, exception); + } + } + } +} diff --git a/SafeExamBrowser.Browser/Wrapper/ICefSharpControl.cs b/SafeExamBrowser.Browser/Wrapper/ICefSharpControl.cs index 80949804..71886441 100644 --- a/SafeExamBrowser.Browser/Wrapper/ICefSharpControl.cs +++ b/SafeExamBrowser.Browser/Wrapper/ICefSharpControl.cs @@ -22,14 +22,18 @@ namespace SafeExamBrowser.Browser.Wrapper event BeforeBrowseEventHandler BeforeBrowse; event BeforeDownloadEventHandler BeforeDownload; event CanDownloadEventHandler CanDownload; + event ContextCreatedEventHandler ContextCreated; + event ContextReleasedEventHandler ContextReleased; event DownloadUpdatedEventHandler DownloadUpdated; event FaviconUrlChangedEventHandler FaviconUrlChanged; event FileDialogRequestedEventHandler FileDialogRequested; + event FocusedNodeChangedEventHandler FocusedNodeChanged; event KeyEventHandler KeyEvent; event LoadingProgressChangedEventHandler LoadingProgressChanged; event OpenUrlFromTabEventHandler OpenUrlFromTab; event PreKeyEventHandler PreKeyEvent; event ResourceRequestEventHandler ResourceRequestHandlerRequired; + event UncaughtExceptionEventHandler UncaughtExceptionEvent; void Dispose(bool disposing); void GetAuthCredentials(IWebBrowser webBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback, GenericEventArgs args); @@ -38,12 +42,16 @@ namespace SafeExamBrowser.Browser.Wrapper void OnBeforeBrowse(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect, GenericEventArgs args); void OnBeforeDownload(IWebBrowser webBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback); void OnCanDownload(IWebBrowser webBrowser, IBrowser browser, string url, string requestMethod, GenericEventArgs args); + void OnContextCreated(IWebBrowser webBrowser, IBrowser browser, IFrame frame); + void OnContextReleased(IWebBrowser webBrowser, IBrowser browser, IFrame frame); void OnDownloadUpdated(IWebBrowser webBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback); void OnFaviconUrlChange(IWebBrowser webBrowser, IBrowser browser, IList urls); void OnFileDialog(IWebBrowser webBrowser, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List acceptFilters, IFileDialogCallback callback); + void OnFocusedNodeChanged(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IDomNode node); void OnKeyEvent(IWebBrowser webBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey); void OnLoadingProgressChange(IWebBrowser webBrowser, IBrowser browser, double progress); void OnOpenUrlFromTab(IWebBrowser webBrowser, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture, GenericEventArgs args); void OnPreKeyEvent(IWebBrowser webBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut, GenericEventArgs args); + void OnUncaughtException(IWebBrowser webBrowser, IBrowser browser, IFrame frame, JavascriptException exception); } }