diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 4c07a73a2..a72ee41b3 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -433,5 +433,27 @@ namespace CefSharp // Unknown dialog type, so we return "not handled". return false; } + + bool ClientAdapter::OnFileDialog(CefRefPtr browser, FileDialogMode mode, const CefString& title, + const CefString& default_file_name, const std::vector& accept_types, + CefRefPtr callback) + { + IDialogHandler^ handler = _browserControl->DialogHandler; + + if (handler == nullptr) + { + return false; + } + + bool result; + bool handled; + + List^ resultString = nullptr; + + result = handler->OnFileDialog(_browserControl, (CefFileDialogMode)mode, StringUtils::ToClr(title), StringUtils::ToClr(default_file_name), StringUtils::ToClr(accept_types), resultString); + callback->Continue(StringUtils::ToNative(resultString)); + + return result; + } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index 564e94bfa..e6609bbd5 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -26,7 +26,8 @@ namespace CefSharp public CefContextMenuHandler, public CefFocusHandler, public CefKeyboardHandler, - public CefJSDialogHandler + public CefJSDialogHandler, + public CefDialogHandler { private: gcroot _browserControl; @@ -65,6 +66,7 @@ namespace CefSharp virtual CefRefPtr GetFocusHandler() OVERRIDE{ return this; } virtual CefRefPtr GetKeyboardHandler() OVERRIDE{ return this; } virtual CefRefPtr GetJSDialogHandler() OVERRIDE{ return this; } + virtual CefRefPtr GetDialogHandler() OVERRIDE{ return this; } // CefLifeSpanHandler virtual DECL bool OnBeforePopup(CefRefPtr browser, CefRefPtr frame, @@ -107,10 +109,15 @@ namespace CefSharp virtual DECL bool OnKeyEvent(CefRefPtr browser, const CefKeyEvent& event, CefEventHandle os_event) OVERRIDE; // CefJSDialogHandler - virtual bool OnJSDialog(CefRefPtr browser, const CefString& origin_url, const CefString& accept_lang, + virtual DECL bool OnJSDialog(CefRefPtr browser, const CefString& origin_url, const CefString& accept_lang, JSDialogType dialog_type, const CefString& message_text, const CefString& default_prompt_text, CefRefPtr callback, bool& suppress_message) OVERRIDE; + // CefDialogHandler + virtual DECL bool OnFileDialog(CefRefPtr browser, FileDialogMode mode, const CefString& title, + const CefString& default_file_name, const std::vector& accept_types, + CefRefPtr callback) OVERRIDE; + IMPLEMENT_LOCKING(ClientAdapter); IMPLEMENT_REFCOUNTING(ClientAdapter); }; diff --git a/CefSharp.Core/Internals/StringUtils.cpp b/CefSharp.Core/Internals/StringUtils.cpp index 251100297..b5fcac0c4 100644 --- a/CefSharp.Core/Internals/StringUtils.cpp +++ b/CefSharp.Core/Internals/StringUtils.cpp @@ -5,6 +5,7 @@ #include "Stdafx.h" using namespace System::Diagnostics; +using namespace System::Collections::Generic; namespace CefSharp { @@ -22,6 +23,19 @@ namespace CefSharp return gcnew String(cefStr.c_str()); } + [DebuggerStepThrough] + List^ StringUtils::ToClr(const std::vector& cefStr) + { + auto result = gcnew List(); + + for each(CefString s in cefStr) + { + result->Add(StringUtils::ToClr(s)); + } + + return result; + } + [DebuggerStepThrough] CefString StringUtils::ToNative(String^ str) { @@ -35,6 +49,24 @@ namespace CefSharp return cefStr; } + [DebuggerStepThrough] + std::vector StringUtils::ToNative(List^ str) + { + if (str == nullptr) + { + return std::vector(); + } + + std::vector result = std::vector(); + + for each (String^ s in str) + { + result.push_back(StringUtils::ToNative(s)); + } + + return result; + } + [DebuggerStepThrough] void StringUtils::AssignNativeFromClr(cef_string_t& cefStr, String^ str) { diff --git a/CefSharp.Core/Internals/StringUtils.h b/CefSharp.Core/Internals/StringUtils.h index aaca65ddf..2df938a72 100644 --- a/CefSharp.Core/Internals/StringUtils.h +++ b/CefSharp.Core/Internals/StringUtils.h @@ -6,6 +6,7 @@ #pragma once using namespace System; +using namespace System::Collections::Generic; namespace CefSharp { @@ -28,6 +29,13 @@ namespace CefSharp /// A .NET string. static String^ ToClr(const CefString& cefStr); + /// + /// Converts an unmanaged vector of strings to a (managed) .NET List of strings. + /// + /// The vector of strings that should be converted. + /// A .NET List of strings. + static List^ ToClr(const std::vector& cefStr); + /// /// Converts a .NET string to native (unmanaged) format. Note that this method does not allocate a new copy of the // string, but rather returns a pointer to the memory in the existing managed String object. @@ -36,6 +44,13 @@ namespace CefSharp /// An unmanaged representation of the provided string, or an empty string if the input string is a nullptr. static CefString ToNative(String^ str); + /// + /// Converts a .NET List of strings to native (unmanaged) format. + /// + /// The List of strings that should be converted. + /// An unmanaged representation of the provided List of strings, or an empty List if the input is a nullptr. + static std::vector ToNative(List^ str); + /// /// Assigns the provided cef_string_t object from the given .NET string. /// diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index 1d6ee49eb..f6591c4cc 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -84,6 +84,7 @@ + diff --git a/CefSharp.Example/TempFileDialogHandler.cs b/CefSharp.Example/TempFileDialogHandler.cs new file mode 100644 index 000000000..75581a266 --- /dev/null +++ b/CefSharp.Example/TempFileDialogHandler.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.IO; + +namespace CefSharp.Example +{ + public class TempFileDialogHandler : IDialogHandler + { + public bool OnFileDialog(IWebBrowser browser, CefFileDialogMode mode, string title, string defaultFileName, List acceptTypes, out List result) + { + result = new List { Path.GetRandomFileName() }; + return true; + } + } +} diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 2bc9878bf..1923f3347 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -20,7 +20,8 @@ namespace CefSharp.WinForms public bool IsLoading { get; set; } public string TooltipText { get; set; } public string Address { get; set; } - + + public IDialogHandler DialogHandler { get; set; } public IJsDialogHandler JsDialogHandler { get; set; } public IKeyboardHandler KeyboardHandler { get; set; } public IRequestHandler RequestHandler { get; set; } diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 55d5001ee..886190ea0 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -43,6 +43,7 @@ namespace CefSharp.Wpf private readonly List disposables = new List(); public BrowserSettings BrowserSettings { get; set; } + public IDialogHandler DialogHandler { get; set; } public IJsDialogHandler JsDialogHandler { get; set; } public IKeyboardHandler KeyboardHandler { get; set; } public IRequestHandler RequestHandler { get; set; } diff --git a/CefSharp/CefFileDialogMode.cs b/CefSharp/CefFileDialogMode.cs new file mode 100644 index 000000000..98c893d55 --- /dev/null +++ b/CefSharp/CefFileDialogMode.cs @@ -0,0 +1,20 @@ +namespace CefSharp +{ + public enum CefFileDialogMode + { + /// + /// Requires that the file exists before allowing the user to pick it. + /// + Open = 0, + + /// + /// Like Open, but allows picking multiple files to open. + /// + OpenMultiple, + + /// + /// Allows picking a nonexistent file, and prompts to overwrite if the file already exists. + /// + Save + } +} diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 11533361a..e8085026a 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -86,6 +86,8 @@ + + diff --git a/CefSharp/IDialogHandler.cs b/CefSharp/IDialogHandler.cs new file mode 100644 index 000000000..cab7f45d3 --- /dev/null +++ b/CefSharp/IDialogHandler.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +namespace CefSharp +{ + /// + /// Implement this interface to handle dialog events. The methods of this class will be called on the browser process UI thread. + /// + public interface IDialogHandler + { + /// + /// Runs a file chooser dialog. + /// + /// + /// To test assign something like TempFileDialogHandler (from CefSharp.Example) to DialogHandler e.g. + /// + /// browser.DialogHandler = new TempFileDialogHandler(); + /// + /// Example URL to use for file browsing http://www.cs.tut.fi/~jkorpela/forms/file.html#example + /// Simply click browse, the space next to the browse button should be populated with a randomly generated filename. + /// + /// the browser object + /// represents the type of dialog to display + /// the title to be used for the dialog. It may be empty to show the default title ("Open" or "Save" + /// depending on the mode). + /// the default file name to select in the dialog. + /// a list of valid lower-cased MIME types or file extensions specified in an input element and + /// is used to restrict selectable files to such types. + /// the filename(s) the dialog returns + /// To display a custom dialog return true. To display the default dialog return false. + bool OnFileDialog(IWebBrowser browser, CefFileDialogMode mode, string title, string defaultFileName, List acceptTypes, out List result); + } +} diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index 63444a1da..6c0d542b6 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -72,6 +72,7 @@ namespace CefSharp /// The timeout after which the Javascript code execution should be aborted. object EvaluateScript(string script, TimeSpan? timeout = null); + IDialogHandler DialogHandler { get; set; } IRequestHandler RequestHandler { get; set; } bool IsBrowserInitialized { get; }