From fce5f06362fb85fe63792e376e2bfd747d46c689 Mon Sep 17 00:00:00 2001 From: amaitland Date: Sun, 11 Jan 2015 13:45:27 +1000 Subject: [PATCH] Remove unused resultString param, CefJSDialogCallback->Continue is used for multiple callbacks, in this case only the success param is required. Change isReload to a normal param (was an out param), so the user can be notified correctly Add some rough documentation to IJsDialogHandlerAdd some additional comments to JsDialogHandler for demonstrating code to users Add JsDialogHandler that implements the default behavior Assign in the WinForms example Fix typo in IRequestHandler --- CefSharp.Core/Internals/ClientAdapter.cpp | 8 ++--- CefSharp.Example/CefSharp.Example.csproj | 1 + CefSharp.Example/JsDialogHandler.cs | 34 +++++++++++++++++++ .../BrowserTabUserControl.cs | 1 + CefSharp/IJsDialogHandler.cs | 13 ++++++- CefSharp/IRequestHandler.cs | 2 +- 6 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 CefSharp.Example/JsDialogHandler.cs diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index c3d7d4e6d..0d90ff4a4 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -455,14 +455,12 @@ namespace CefSharp return false; } - bool result; - bool handled = false; + bool allowUnload; - String^ resultString = nullptr; - handled = handler->OnJSBeforeUnload(_browserControl, StringUtils::ToClr(message_text), is_reload, resultString); + auto handled = handler->OnJSBeforeUnload(_browserControl, StringUtils::ToClr(message_text), is_reload, allowUnload); if (handled) { - callback->Continue(result, StringUtils::ToNative(resultString)); + callback->Continue(allowUnload, CefString()); } return handled; diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index c60d65e32..2c8d312fa 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -85,6 +85,7 @@ + diff --git a/CefSharp.Example/JsDialogHandler.cs b/CefSharp.Example/JsDialogHandler.cs new file mode 100644 index 000000000..df8536f82 --- /dev/null +++ b/CefSharp.Example/JsDialogHandler.cs @@ -0,0 +1,34 @@ +namespace CefSharp.Example +{ + public class JsDialogHandler : IJsDialogHandler + { + public bool OnJSAlert(IWebBrowser browser, string url, string message) + { + return false; + } + + public bool OnJSConfirm(IWebBrowser browser, string url, string message, out bool retval) + { + retval = false; + + return false; + } + + public bool OnJSPrompt(IWebBrowser browser, string url, string message, string defaultValue, out bool retval, out string result) + { + retval = false; + result = null; + + return false; + } + + public bool OnJSBeforeUnload(IWebBrowser browser, string message, bool isReload, out bool allowUnload) + { + //NOTE: Setting allowUnload to false will cancel the unload request + allowUnload = false; + + //NOTE: Returning false will trigger the default behaviour, you need to return true to handle yourself. + return false; + } + } +} diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index 28095a5b4..dbdd9b95b 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -25,6 +25,7 @@ namespace CefSharp.WinForms.Example browser.MenuHandler = new MenuHandler(); browser.RequestHandler = new RequestHandler(); + browser.JsDialogHandler = new JsDialogHandler(); //browser.FocusHandler = new FocusHandler(browser, urlTextBox); browser.NavStateChanged += OnBrowserNavStateChanged; browser.ConsoleMessage += OnBrowserConsoleMessage; diff --git a/CefSharp/IJsDialogHandler.cs b/CefSharp/IJsDialogHandler.cs index 267453cb0..b4df276fc 100644 --- a/CefSharp/IJsDialogHandler.cs +++ b/CefSharp/IJsDialogHandler.cs @@ -9,6 +9,17 @@ namespace CefSharp bool OnJSAlert(IWebBrowser browser, string url, string message); bool OnJSConfirm(IWebBrowser browser, string url, string message, out bool retval); bool OnJSPrompt(IWebBrowser browser, string url, string message, string defaultValue, out bool retval, out string result); - bool OnJSBeforeUnload(IWebBrowser browser, string message, out bool isReload, out string result); + + /// + /// When leaving the page a Javascript dialog is displayed asking for user confirmation. + /// Returning True allows you to implement a custom dialog or programatically handle. + /// To cancel the unload return True and set allowUnload to False. + /// + /// the browser object + /// message (optional) + /// indicates a page reload + /// True to allow unload, otherwise False + /// Return false to use the default dialog implementation otherwise return true to handle + bool OnJSBeforeUnload(IWebBrowser browser, string message, bool isReload, out bool allowUnload); } } diff --git a/CefSharp/IRequestHandler.cs b/CefSharp/IRequestHandler.cs index c82e4ba56..4eb66e713 100644 --- a/CefSharp/IRequestHandler.cs +++ b/CefSharp/IRequestHandler.cs @@ -40,7 +40,7 @@ namespace CefSharp /// the browser object /// the request object - can be modified in this callback. /// the request object - can be modified in this callback. - /// To cancel loading of the resource return true or false o allow the resource to load normally. + /// To cancel loading of the resource return true or false to allow the resource to load normally. bool OnBeforeResourceLoad(IWebBrowser browser, IRequest request, IResponse response); ///