Skip to content

feat/jsDialog handler #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: feat/downloadHandler
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected override void OnContextInitialized()
_outOfProcessServer.NotifyContextInitialized(threadId, Cef.CefSharpVersion, Cef.CefVersion, Cef.ChromiumVersion);
_outOfProcessServer.BeforeDownloadCallback += _outOfProcessServer_BeforeDownloadCallback;
_outOfProcessServer.DownloadCallback += _outOfProcessServer_DownloadCallback;
_outOfProcessServer.JsDialogCallback += _outOfProcessServer_JsDialogCallback;
}

private void _outOfProcessServer_DownloadCallback(object sender, DownloadCallbackDetails e)
Expand All @@ -54,6 +55,11 @@ private void _outOfProcessServer_BeforeDownloadCallback(object sender, BeforeDow
((DownloadHandlerProxy)GetBrowser(e.BrowserId).DownloadHandler)?.BeforeDownloadCallback(e);
}

private void _outOfProcessServer_JsDialogCallback(object sender, JsDialogCallbackDetails e)
{
((JsDialogHandlerProxy)GetBrowser(e.BrowserId).JsDialogHandler)?.Callback(e);
}

private OutOfProcessChromiumWebBrowser GetBrowser(int id) => _browsers.FirstOrDefault(x => x.Id == id);

protected override void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using CefSharp.OutOfProcess.Interface;
using CefSharp.OutOfProcess.Interface.Callbacks;
using System;

namespace CefSharp.OutOfProcess.BrowserProcess.CallbackProxies
{
internal sealed class JsDialogHandlerProxy : CallbackProxyBase<object>, IJsDialogHandler
{
public JsDialogHandlerProxy(IOutOfProcessHostRpc host)
: base(host)
{

}

public void Callback(JsDialogCallbackDetails details)
{
var cb = (CefSharp.OutOfProcess.Interface.Callbacks.IJsDialogCallback)GetCallback(details.CallbackId);

if (details.UserInput != string.Empty)
{
cb.Continue(details.Success, details.UserInput);
}
else
{
cb.Continue(details.Success);
}
}

public bool OnBeforeUnloadDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string messageText, bool isReload, IJsDialogCallback callback)
{
var result = host.OnBeforeUnloadDialog(((OutOfProcessChromiumWebBrowser)chromiumWebBrowser).Id, messageText, isReload, CreateCallback(callback));
return result;
}

public void OnDialogClosed(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
host.OnDialogClosed(((OutOfProcessChromiumWebBrowser)chromiumWebBrowser).Id);
}

public bool OnJSDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
{
var result = host.OnJSDialog(((OutOfProcessChromiumWebBrowser)chromiumWebBrowser).Id, originUrl, ParseEnum<Interface.Callbacks.CefJsDialogType>(Enum.GetName(typeof(CefJsDialogType), dialogType)), messageText, defaultPromptText, CreateCallback(callback), ref suppressMessage);
return result;
}

public void OnResetDialogState(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
host.OnResetDialogState(((OutOfProcessChromiumWebBrowser)chromiumWebBrowser).Id);
}

public static T ParseEnum<T>(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace CefSharp.OutOfProcess
{
using CefSharp.OutOfProcess.Interface.Callbacks;
using CefSharp.OutOfProcess.Internal;

internal sealed class JsDialogCallbackProxy : CallbackProxyBase, IJsDialogCallback
{
public JsDialogCallbackProxy(OutOfProcessHost outOfProcessHost, int callback, IChromiumWebBrowserInternal chromiumWebBrowser)
: base(outOfProcessHost, callback, chromiumWebBrowser)
{
}

void IJsDialogCallback.Continue(bool success, string userInput)
{
outOfProcessHost.InvokeJsDialogCallback(new JsDialogCallbackDetails()
{
CallbackId = callback,
BrowserId = chromiumWebBrowser.Id,
Success = success,
UserInput = userInput,
});
}

void IJsDialogCallback.Continue(bool success)
{
outOfProcessHost.InvokeJsDialogCallback(new JsDialogCallbackDetails()
{
CallbackId = callback,
BrowserId = chromiumWebBrowser.Id,
Success = success,
});
}
}
}
16 changes: 16 additions & 0 deletions CefSharp.OutOfProcess.Core/Handler/IJsDialogHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CefSharp.OutOfProcess.Handler
{
using CefSharp.OutOfProcess.Interface.Callbacks;

public interface IJsDialogHandler
{

bool OnJSDialog(IChromiumWebBrowser chromiumWebBrowser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage);

bool OnBeforeUnloadDialog(IChromiumWebBrowser chromiumWebBrowser, string messageText, bool isReload, IJsDialogCallback callback);

void OnResetDialogState(IChromiumWebBrowser chromiumWebBrowser);

void OnDialogClosed(IChromiumWebBrowser chromiumWebBrowser);
}
}
2 changes: 2 additions & 0 deletions CefSharp.OutOfProcess.Core/IChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,7 @@ public interface IChromiumWebBrowser : IDisposable
Task<Response> GoForwardAsync(NavigationOptions options = null);

Handler.IDownloadHandler DownloadHandler { get; set; }

Handler.IJsDialogHandler JsDialogHandler { get; set; }
}
}
40 changes: 40 additions & 0 deletions CefSharp.OutOfProcess.Core/OutOfProcessHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private OutOfProcessHost(string outOfProcessHostExePath, string cachePath = null

public event EventHandler<BeforeDownloadCallbackDetails> BeforeDownloadCallback;

public event EventHandler<JsDialogCallbackDetails> JsDialogCallback;


/// <summary>
/// UI Thread assocuated with this <see cref="OutOfProcessHost"/>
Expand Down Expand Up @@ -287,5 +289,43 @@ Task<bool> IOutOfProcessHostRpc.OnCanDownloadAsync(int browserId, string url, st
internal void InvokeBeforeDownloadCallback(BeforeDownloadCallbackDetails callbackDetails) => BeforeDownloadCallback.Invoke(this, callbackDetails);

internal void InvokeDownloadCallback(DownloadCallbackDetails callbackDetails) => DownloadCallback.Invoke(this, callbackDetails);

internal void InvokeJsDialogCallback(JsDialogCallbackDetails callbackDetails) => JsDialogCallback.Invoke(this, callbackDetails);

public bool OnBeforeUnloadDialog(int browserId, string messageText, bool isReload, int callback)
{
if (GetBrowser(browserId) is IChromiumWebBrowserInternal chromiumWebBrowser)
{
return chromiumWebBrowser?.JsDialogHandler?.OnBeforeUnloadDialog(chromiumWebBrowser, messageText, isReload, new JsDialogCallbackProxy(this, callback, chromiumWebBrowser)) ?? false;
}

return false;
}

public void OnDialogClosed(int browserId)
{
if (GetBrowser(browserId) is IChromiumWebBrowserInternal chromiumWebBrowser)
{
chromiumWebBrowser?.JsDialogHandler?.OnDialogClosed(chromiumWebBrowser);
}
}

public bool OnJSDialog(int browserId, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, int callback, ref bool suppressMessage)
{
if (GetBrowser(browserId) is IChromiumWebBrowserInternal chromiumWebBrowser)
{
return chromiumWebBrowser?.JsDialogHandler?.OnJSDialog(chromiumWebBrowser,originUrl, dialogType, messageText,defaultPromptText,new JsDialogCallbackProxy(this, callback,chromiumWebBrowser), ref suppressMessage) ?? false;
}

return false;
}

public void OnResetDialogState(int browserId)
{
if (GetBrowser(browserId) is IChromiumWebBrowserInternal chromiumWebBrowser)
{
chromiumWebBrowser?.JsDialogHandler?.OnResetDialogState(chromiumWebBrowser);
}
}
}
}
21 changes: 21 additions & 0 deletions CefSharp.OutOfProcess.Interface/Callbacks/CefJSDialogType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace CefSharp.OutOfProcess.Interface.Callbacks
{
/// <summary>
/// Supported JavaScript dialog types.
/// </summary>
public enum CefJsDialogType
{
/// <summary>
/// Alert Dialog
/// </summary>
Alert = 0,
/// <summary>
/// Confirm Dialog
/// </summary>
Confirm,
/// <summary>
/// Prompt Dialog
/// </summary>
Prompt
}
}
13 changes: 13 additions & 0 deletions CefSharp.OutOfProcess.Interface/Callbacks/IJsDialogCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CefSharp.OutOfProcess.Interface.Callbacks
{
public interface IJsDialogCallback : IDisposable
{
bool IsDisposed { get; }
void Continue(bool success);
void Continue(bool success, string userInput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CefSharp.OutOfProcess.Interface.Callbacks
{
public class JsDialogCallbackDetails : CallbackDetails
{
public string UserInput { get; set; }

public bool Success { get; set; }
}
}
9 changes: 9 additions & 0 deletions CefSharp.OutOfProcess.Interface/IOutOfProcessHostRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,14 @@ public interface IOutOfProcessHostRpc

event EventHandler<BeforeDownloadCallbackDetails> BeforeDownloadCallback;
event EventHandler<DownloadCallbackDetails> DownloadCallback;
event EventHandler<JsDialogCallbackDetails> JsDialogCallback;

bool OnBeforeUnloadDialog(int browserId, string messageText, bool isReload, int callback);

void OnDialogClosed(int browserId);

bool OnJSDialog(int browserId, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, int callback, ref bool suppressMessage);

void OnResetDialogState(int browserId);
}
}
2 changes: 2 additions & 0 deletions CefSharp.OutOfProcess.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public IDevToolsContext DevToolsContext

Handler.IDownloadHandler IChromiumWebBrowser.DownloadHandler { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

Handler.IJsDialogHandler IChromiumWebBrowser.JsDialogHandler { get; set; }

/// <inheritdoc/>
protected override void OnHandleCreated(EventArgs e)
{
Expand Down
3 changes: 3 additions & 0 deletions CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,9 @@ public IChromiumWebBrowser WebBrowser

Handler.IDownloadHandler IChromiumWebBrowser.DownloadHandler { get; set; }


Handler.IJsDialogHandler IChromiumWebBrowser.JsDialogHandler { get; set; }

/// <summary>
/// The WebBrowser property
/// </summary>
Expand Down