Skip to content

Commit

Permalink
SEBWIN-926: Implemented custom drag handler to suppress drag&drop beh…
Browse files Browse the repository at this point in the history
…avior.
  • Loading branch information
dbuechel committed Nov 13, 2024
1 parent 7d10de0 commit fb6e3d9
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 1 deletion.
5 changes: 5 additions & 0 deletions SafeExamBrowser.Browser/BrowserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class BrowserControl : IBrowserControl
private readonly IDialogHandler dialogHandler;
private readonly IDisplayHandler displayHandler;
private readonly IDownloadHandler downloadHandler;
private readonly IDragHandler dragHandler;
private readonly IFocusHandler focusHandler;
private readonly IJsDialogHandler javaScriptDialogHandler;
private readonly IKeyboardHandler keyboardHandler;
Expand All @@ -49,6 +50,7 @@ public BrowserControl(
IDialogHandler dialogHandler,
IDisplayHandler displayHandler,
IDownloadHandler downloadHandler,
IDragHandler dragHandler,
IFocusHandler focusHandler,
IJsDialogHandler javaScriptDialogHandler,
IKeyboardHandler keyboardHandler,
Expand All @@ -61,6 +63,7 @@ public BrowserControl(
this.dialogHandler = dialogHandler;
this.displayHandler = displayHandler;
this.downloadHandler = downloadHandler;
this.dragHandler = dragHandler;
this.focusHandler = focusHandler;
this.javaScriptDialogHandler = javaScriptDialogHandler;
this.keyboardHandler = keyboardHandler;
Expand Down Expand Up @@ -133,6 +136,8 @@ public void Initialize()
control.ContextReleased += (w, b, f) => renderProcessMessageHandler.OnContextReleased(w, b, f);
control.DialogClosed += (w, b) => javaScriptDialogHandler.OnDialogClosed(w, b);
control.DownloadUpdated += (w, b, d, c) => downloadHandler.OnDownloadUpdated(w, b, d, c);
control.DragEnterCefSharp += (w, b, d, m, a) => a.Value = dragHandler.OnDragEnter(w, b, d, m);
control.DraggableRegionsChanged += (w, b, f, r) => dragHandler.OnDraggableRegionsChanged(w, b, f, r);
control.FaviconUrlChanged += (w, b, u) => displayHandler.OnFaviconUrlChange(w, b, u);
control.FileDialogRequested += (w, b, m, t, p, f, e, d, c) => dialogHandler.OnFileDialog(w, b, m, t, p, f, e, d, c);
control.FocusedNodeChanged += (w, b, f, n) => renderProcessMessageHandler.OnFocusedNodeChanged(w, b, f, n);
Expand Down
15 changes: 14 additions & 1 deletion SafeExamBrowser.Browser/BrowserWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ internal void InitializeControl()
var displayHandler = new DisplayHandler();
var downloadLogger = logger.CloneFor($"{nameof(DownloadHandler)} #{Id}");
var downloadHandler = new DownloadHandler(appConfig, downloadLogger, settings, WindowSettings);
var dragHandler = new DragHandler();
var focusHandler = new FocusHandler();
var javaScriptDialogHandler = new JavaScriptDialogHandler();
var keyboardHandler = new KeyboardHandler();
Expand Down Expand Up @@ -198,7 +199,19 @@ internal void InitializeControl()

InitializeRequestFilter(requestFilter);

Control = new BrowserControl(clipboard, cefSharpControl, dialogHandler, displayHandler, downloadHandler, focusHandler, javaScriptDialogHandler, keyboardHandler, controlLogger, renderHandler, requestHandler);
Control = new BrowserControl(
clipboard,
cefSharpControl,
dialogHandler,
displayHandler,
downloadHandler,
dragHandler,
focusHandler,
javaScriptDialogHandler,
keyboardHandler,
controlLogger,
renderHandler,
requestHandler);
Control.AddressChanged += Control_AddressChanged;
Control.LoadFailed += Control_LoadFailed;
Control.LoadingStateChanged += Control_LoadingStateChanged;
Expand Down
26 changes: 26 additions & 0 deletions SafeExamBrowser.Browser/Handlers/DragHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 System.Collections.Generic;
using CefSharp;
using CefSharp.Enums;

namespace SafeExamBrowser.Browser.Handlers
{
internal class DragHandler : IDragHandler
{
public bool OnDragEnter(IWebBrowser chromiumWebBrowser, IBrowser browser, IDragData dragData, DragOperationsMask mask)
{
return true;
}

public void OnDraggableRegionsChanged(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IList<DraggableRegion> regions)
{
}
}
}
4 changes: 4 additions & 0 deletions SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Handlers\DialogHandler.cs" />
<Compile Include="Handlers\DisplayHandler.cs" />
<Compile Include="Handlers\DownloadHandler.cs" />
<Compile Include="Handlers\DragHandler.cs" />
<Compile Include="Handlers\FocusHandler.cs" />
<Compile Include="Handlers\KeyboardHandler.cs" />
<Compile Include="Handlers\RenderProcessMessageHandler.cs" />
Expand All @@ -128,6 +129,8 @@
<Compile Include="Wrapper\Events\ContextReleasedEventHandler.cs" />
<Compile Include="Wrapper\Events\DialogClosedEventHandler.cs" />
<Compile Include="Wrapper\Events\DownloadUpdatedEventHandler.cs" />
<Compile Include="Wrapper\Events\DragEnterEventHandler.cs" />
<Compile Include="Wrapper\Events\DraggableRegionsChangedEventHandler.cs" />
<Compile Include="Wrapper\Events\FaviconUrlChangedEventHandler.cs" />
<Compile Include="Wrapper\Events\FileDialogRequestedEventHandler.cs" />
<Compile Include="Wrapper\Events\FocusedNodeChangedEventHandler.cs" />
Expand All @@ -148,6 +151,7 @@
<Compile Include="Wrapper\Handlers\DialogHandlerSwitch.cs" />
<Compile Include="Wrapper\Handlers\DisplayHandlerSwitch.cs" />
<Compile Include="Wrapper\Handlers\DownloadHandlerSwitch.cs" />
<Compile Include="Wrapper\Handlers\DragHandlerSwitch.cs" />
<Compile Include="Wrapper\Handlers\FocusHandlerSwitch.cs" />
<Compile Include="Handlers\JavaScriptDialogHandler.cs" />
<Compile Include="Wrapper\Handlers\JavaScriptDialogHandlerSwitch.cs" />
Expand Down
14 changes: 14 additions & 0 deletions SafeExamBrowser.Browser/Wrapper/CefSharpBrowserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using System.Collections.Generic;
using CefSharp;
using CefSharp.Enums;
using CefSharp.WinForms;
using SafeExamBrowser.Browser.Handlers;
using SafeExamBrowser.Browser.Wrapper.Events;
Expand All @@ -26,6 +27,8 @@ internal class CefSharpBrowserControl : ChromiumWebBrowser, ICefSharpControl
public event ContextReleasedEventHandler ContextReleased;
public event DialogClosedEventHandler DialogClosed;
public event DownloadUpdatedEventHandler DownloadUpdated;
public event DragEnterEventHandler DragEnterCefSharp;
public event DraggableRegionsChangedEventHandler DraggableRegionsChanged;
public event FaviconUrlChangedEventHandler FaviconUrlChanged;
public event FileDialogRequestedEventHandler FileDialogRequested;
public event FocusedNodeChangedEventHandler FocusedNodeChanged;
Expand All @@ -46,6 +49,7 @@ public CefSharpBrowserControl(ILifeSpanHandler lifeSpanHandler, string url) : ba
DialogHandler = new DialogHandlerSwitch();
DisplayHandler = new DisplayHandlerSwitch();
DownloadHandler = new DownloadHandlerSwitch();
DragHandler = new DragHandlerSwitch();
FocusHandler = new FocusHandlerSwitch();
JsDialogHandler = new JavaScriptDialogHandlerSwitch();
KeyboardHandler = new KeyboardHandlerSwitch();
Expand Down Expand Up @@ -110,6 +114,16 @@ public void OnDownloadUpdated(IWebBrowser webBrowser, IBrowser browser, Download
DownloadUpdated?.Invoke(webBrowser, browser, downloadItem, callback);
}

public void OnDragEnter(IWebBrowser webBrowser, IBrowser browser, IDragData dragData, DragOperationsMask mask, GenericEventArgs args)
{
DragEnterCefSharp?.Invoke(webBrowser, browser, dragData, mask, args);
}

public void OnDraggableRegionsChanged(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IList<DraggableRegion> regions)
{
DraggableRegionsChanged?.Invoke(webBrowser, browser, frame, regions);
}

public void OnFaviconUrlChange(IWebBrowser webBrowser, IBrowser browser, IList<string> urls)
{
FaviconUrlChanged?.Invoke(webBrowser, browser, urls);
Expand Down
13 changes: 13 additions & 0 deletions SafeExamBrowser.Browser/Wrapper/CefSharpPopupControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using System.Collections.Generic;
using CefSharp;
using CefSharp.Enums;
using CefSharp.WinForms.Host;
using SafeExamBrowser.Browser.Wrapper.Events;

Expand All @@ -24,6 +25,8 @@ internal class CefSharpPopupControl : ChromiumHostControl, ICefSharpControl
public event ContextReleasedEventHandler ContextReleased;
public event DialogClosedEventHandler DialogClosed;
public event DownloadUpdatedEventHandler DownloadUpdated;
public event DragEnterEventHandler DragEnterCefSharp;
public event DraggableRegionsChangedEventHandler DraggableRegionsChanged;
public event FaviconUrlChangedEventHandler FaviconUrlChanged;
public event FileDialogRequestedEventHandler FileDialogRequested;
public event FocusedNodeChangedEventHandler FocusedNodeChanged;
Expand Down Expand Up @@ -102,6 +105,16 @@ public void OnDownloadUpdated(IWebBrowser webBrowser, IBrowser browser, Download
DownloadUpdated?.Invoke(webBrowser, browser, downloadItem, callback);
}

public void OnDragEnter(IWebBrowser webBrowser, IBrowser browser, IDragData dragData, DragOperationsMask mask, GenericEventArgs args)
{
DragEnterCefSharp?.Invoke(webBrowser, browser, dragData, mask, args);
}

public void OnDraggableRegionsChanged(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IList<DraggableRegion> regions)
{
DraggableRegionsChanged?.Invoke(webBrowser, browser, frame, regions);
}

public void OnFaviconUrlChange(IWebBrowser webBrowser, IBrowser browser, IList<string> urls)
{
FaviconUrlChanged?.Invoke(webBrowser, browser, urls);
Expand Down
15 changes: 15 additions & 0 deletions SafeExamBrowser.Browser/Wrapper/Events/DragEnterEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.Enums;

namespace SafeExamBrowser.Browser.Wrapper.Events
{
internal delegate void DragEnterEventHandler(IWebBrowser webBrowser, IBrowser browser, IDragData dragData, DragOperationsMask mask, GenericEventArgs args);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 System.Collections.Generic;
using CefSharp;

namespace SafeExamBrowser.Browser.Wrapper.Events
{
internal delegate void DraggableRegionsChangedEventHandler(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IList<DraggableRegion> regions);
}
56 changes: 56 additions & 0 deletions SafeExamBrowser.Browser/Wrapper/Handlers/DragHandlerSwitch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 System.Collections.Generic;
using CefSharp;
using CefSharp.Enums;
using CefSharp.WinForms;
using CefSharp.WinForms.Host;
using SafeExamBrowser.Browser.Wrapper.Events;

namespace SafeExamBrowser.Browser.Wrapper.Handlers
{
internal class DragHandlerSwitch : IDragHandler
{
public bool OnDragEnter(IWebBrowser webBrowser, IBrowser browser, IDragData dragData, DragOperationsMask mask)
{
var args = new GenericEventArgs();

if (browser.IsPopup)
{
var control = ChromiumHostControl.FromBrowser(browser) as CefSharpPopupControl;

control?.OnDragEnter(webBrowser, browser, dragData, mask, args);
}
else
{
var control = ChromiumWebBrowser.FromBrowser(browser) as CefSharpBrowserControl;

control?.OnDragEnter(webBrowser, browser, dragData, mask, args);
}

return args.Value;
}

public void OnDraggableRegionsChanged(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IList<DraggableRegion> regions)
{
if (browser.IsPopup)
{
var control = ChromiumHostControl.FromBrowser(browser) as CefSharpPopupControl;

control?.OnDraggableRegionsChanged(webBrowser, browser, frame, regions);
}
else
{
var control = ChromiumWebBrowser.FromBrowser(browser) as CefSharpBrowserControl;

control?.OnDraggableRegionsChanged(webBrowser, browser, frame, regions);
}
}
}
}
5 changes: 5 additions & 0 deletions SafeExamBrowser.Browser/Wrapper/ICefSharpControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.ComponentModel;
using System.Windows.Forms;
using CefSharp;
using CefSharp.Enums;
using CefSharp.WinForms;
using SafeExamBrowser.Browser.Wrapper.Events;
using KeyEventHandler = SafeExamBrowser.Browser.Wrapper.Events.KeyEventHandler;
Expand All @@ -27,6 +28,8 @@ internal interface ICefSharpControl : IChromiumWebBrowserBase, IWinFormsChromium
event ContextReleasedEventHandler ContextReleased;
event DialogClosedEventHandler DialogClosed;
event DownloadUpdatedEventHandler DownloadUpdated;
event DragEnterEventHandler DragEnterCefSharp;
event DraggableRegionsChangedEventHandler DraggableRegionsChanged;
event FaviconUrlChangedEventHandler FaviconUrlChanged;
event FileDialogRequestedEventHandler FileDialogRequested;
event FocusedNodeChangedEventHandler FocusedNodeChanged;
Expand Down Expand Up @@ -54,6 +57,8 @@ internal interface ICefSharpControl : IChromiumWebBrowserBase, IWinFormsChromium
void OnContextReleased(IWebBrowser webBrowser, IBrowser browser, IFrame frame);
void OnDialogClosed(IWebBrowser webBrowser, IBrowser browser);
void OnDownloadUpdated(IWebBrowser webBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback);
void OnDragEnter(IWebBrowser webBrowser, IBrowser browser, IDragData dragData, DragOperationsMask mask, GenericEventArgs args);
void OnDraggableRegionsChanged(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IList<DraggableRegion> regions);
void OnFaviconUrlChange(IWebBrowser webBrowser, IBrowser browser, IList<string> urls);
void OnFileDialog(IWebBrowser webBrowser, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, IReadOnlyCollection<string> acceptFilters, IReadOnlyCollection<string> acceptExtensions, IReadOnlyCollection<string> acceptDescriptions, IFileDialogCallback callback);
void OnFocusedNodeChanged(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IDomNode node);
Expand Down

0 comments on commit fb6e3d9

Please sign in to comment.