Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CefGlue.Common/BaseCefBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ protected virtual void Dispose(bool disposing)
/// Return the handler for JavaScript dialogs. If no handler is provided the default implementation will be used.
/// </summary>
public JSDialogHandler JSDialogHandler { get => _adapter.JSDialogHandler; set => _adapter.JSDialogHandler = value; }

/// <summary>
/// Return the handler for browser permission events.
/// </summary>
public PermissionHandler PermissionHandler { get => _adapter.PermissionHandler; set => _adapter.PermissionHandler = value; }

/// <summary>
/// Gets or set the url.
Expand Down
1 change: 1 addition & 0 deletions CefGlue.Common/CommonBrowserAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ protected virtual void InnerDispose() { }
public DisplayHandler DisplayHandler { get; set; }
public RenderHandler RenderHandler { get; set; }
public JSDialogHandler JSDialogHandler { get; set; }
public PermissionHandler PermissionHandler { get; set; }

#endregion

Expand Down
5 changes: 5 additions & 0 deletions CefGlue.Common/CommonCefClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ protected override CefFrameHandler GetFrameHandler()
{
return _frameHandler;
}

protected override CefPermissionHandler GetPermissionHandler()
{
return _owner.PermissionHandler;
}

protected override bool OnProcessMessageReceived(CefBrowser browser, CefFrame frame, CefProcessId sourceProcess, CefProcessMessage message)
{
Expand Down
2 changes: 2 additions & 0 deletions CefGlue.Common/Handlers/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public class FocusHandler : CefFocusHandler { }
public abstract class JSDialogHandler : CefJSDialogHandler { }

public class KeyboardHandler : CefKeyboardHandler { }

public abstract class PermissionHandler : CefPermissionHandler { }

public class LifeSpanHandler : CefLifeSpanHandler
{
Expand Down
1 change: 1 addition & 0 deletions CefGlue.Common/ICefBrowserHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ internal interface ICefBrowserHost
DisplayHandler DisplayHandler { get; set; }
RenderHandler RenderHandler { get; set; }
JSDialogHandler JSDialogHandler { get; set; }
PermissionHandler PermissionHandler { get; set; }
}
}
47 changes: 47 additions & 0 deletions CefGlue.Demo.Avalonia/BrowserView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public BrowserView()
browser.LoadStart += OnBrowserLoadStart;
browser.TitleChanged += OnBrowserTitleChanged;
browser.LifeSpanHandler = new BrowserLifeSpanHandler();
browser.PermissionHandler = new BrowserPermissionHandler();
browserWrapper.Child = browser;
}

Expand Down Expand Up @@ -169,5 +170,51 @@ protected override bool OnBeforePopup(
return true;
}
}

/// <summary>
/// Simplified permission handler for browser requests.
/// </summary>
internal sealed class BrowserPermissionHandler : PermissionHandler
{
protected override bool OnRequestMediaAccessPermission(CefBrowser browser, CefFrame frame,
string requestingOrigin,
CefMediaAccessPermissionTypes requestedPermissions, CefMediaAccessCallback callback)
{
if (browser == null || callback == null) return false;

// Grant all requested media permissions for all sites.
callback.Continue(requestedPermissions);
return true;
}

/// <summary>
/// Called when a permission prompt is shown (e.g., clipboard, geolocation, notifications).
/// Grants permissions for all sites. For testing, you can filter by checking requestingOrigin,
/// e.g., if (requestingOrigin.Contains("test-site.com")) to restrict to specific domains.
/// </summary>
/// <param name="browser">The browser instance initiating the prompt.</param>
/// <param name="promptId">The unique ID of the permission prompt.</param>
/// <param name="requestingOrigin">The origin requesting the permission.</param>
/// <param name="requestedPermissions">The requested permission types.</param>
/// <param name="callback">The callback to invoke with the permission result.</param>
/// <returns>True if the prompt was handled, false otherwise.</returns>
protected override bool OnShowPermissionPrompt(CefBrowser browser, ulong promptId, string requestingOrigin,
CefPermissionRequestTypes requestedPermissions, CefPermissionPromptCallback callback)
{
if (browser == null || callback == null) return false;

// Allow clipboard, geolocation, and notifications for all sites.
var result = CefPermissionRequestResult.Deny;
if (requestedPermissions.HasFlag(CefPermissionRequestTypes.Clipboard) ||
requestedPermissions.HasFlag(CefPermissionRequestTypes.Geolocation) ||
requestedPermissions.HasFlag(CefPermissionRequestTypes.Notifications))
{
result = CefPermissionRequestResult.Accept;
}

callback.Continue(result);
return true;
}
}
}
}
48 changes: 48 additions & 0 deletions CefGlue.Demo.WPF/BrowserView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows.Controls;
using System.Windows.Input;
using Xilium.CefGlue.Common.Events;
using Xilium.CefGlue.Common.Handlers;

namespace Xilium.CefGlue.Demo.WPF
{
Expand All @@ -12,6 +13,53 @@ public BrowserView()
{
InitializeComponent();
//browser.RegisterJavascriptObject(new BindingTestClass(), "boundBeforeLoadObject");
//browser.PermissionHandler = new BrowserPermissionHandler();
}

/// <summary>
/// Simplified permission handler for browser requests.
/// </summary>
internal sealed class BrowserPermissionHandler : PermissionHandler
{
protected override bool OnRequestMediaAccessPermission(CefBrowser browser, CefFrame frame,
string requestingOrigin,
CefMediaAccessPermissionTypes requestedPermissions, CefMediaAccessCallback callback)
{
if (browser == null || callback == null) return false;

// Grant all requested media permissions for all sites.
callback.Continue(requestedPermissions);
return true;
}

/// <summary>
/// Called when a permission prompt is shown (e.g., clipboard, geolocation, notifications).
/// Grants permissions for all sites. For testing, you can filter by checking requestingOrigin,
/// e.g., if (requestingOrigin.Contains("test-site.com")) to restrict to specific domains.
/// </summary>
/// <param name="browser">The browser instance initiating the prompt.</param>
/// <param name="promptId">The unique ID of the permission prompt.</param>
/// <param name="requestingOrigin">The origin requesting the permission.</param>
/// <param name="requestedPermissions">The requested permission types.</param>
/// <param name="callback">The callback to invoke with the permission result.</param>
/// <returns>True if the prompt was handled, false otherwise.</returns>
protected override bool OnShowPermissionPrompt(CefBrowser browser, ulong promptId, string requestingOrigin,
CefPermissionRequestTypes requestedPermissions, CefPermissionPromptCallback callback)
{
if (browser == null || callback == null) return false;

// Allow clipboard, geolocation, and notifications for all sites.
var result = CefPermissionRequestResult.Deny;
if (requestedPermissions.HasFlag(CefPermissionRequestTypes.Clipboard) ||
requestedPermissions.HasFlag(CefPermissionRequestTypes.Geolocation) ||
requestedPermissions.HasFlag(CefPermissionRequestTypes.Notifications))
{
result = CefPermissionRequestResult.Accept;
}

callback.Continue(result);
return true;
}
}

public event Action<string> TitleChanged;
Expand Down
Loading