From c11fe8e4e97179ff4073208c13f9ff29e61bab79 Mon Sep 17 00:00:00 2001 From: Dean North Date: Tue, 29 Apr 2014 13:31:39 +0100 Subject: [PATCH 01/16] Added support for overriding file browse dialogs by implementing the cef DialogHandler --- CefSharp.Core/Internals/ClientAdapter.cpp | 26 +++++++++++++++++++++ CefSharp.Core/Internals/ClientAdapter.h | 12 +++++++++- CefSharp.Core/Internals/StringUtils.cpp | 28 +++++++++++++++++++++++ CefSharp.Core/Internals/StringUtils.h | 15 ++++++++++++ CefSharp.WinForms/WebView.cs | 3 ++- CefSharp.Wpf/WebView.cs | 1 + CefSharp/CefSharp.csproj | 1 + CefSharp/IDialogHandler.cs | 8 +++++++ CefSharp/Internals/IWebBrowserInternal.cs | 1 + CefSharp3.sln | 5 ++++ 10 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 CefSharp/IDialogHandler.cs diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 1c8a4d46b..ece4218ac 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -408,5 +408,31 @@ 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->OnOpenFile(_browserControl, StringUtils::ToClr(title), StringUtils::ToClr(default_file_name), StringUtils::ToClr(accept_types), resultString); + callback->Continue(StringUtils::ToNative(resultString)); + + // Unknown dialog type, so we return "not handled". + return false; + } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index 00d916344..ce231a3a5 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; @@ -62,6 +63,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, @@ -105,6 +107,14 @@ namespace CefSharp JSDialogType dialog_type, const CefString& message_text, const CefString& default_prompt_text, CefRefPtr callback, bool& suppress_message) OVERRIDE; + // CefDialogHandler + virtual 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..c05c68158 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,13 @@ namespace CefSharp return gcnew String(cefStr.c_str()); } + [DebuggerStepThrough] + List^ StringUtils::ToClr(const std::vector& cefStr) + { + //cefStr.c_str() + return gcnew List(); + } + [DebuggerStepThrough] CefString StringUtils::ToNative(String^ str) { @@ -35,6 +43,26 @@ 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) + { + pin_ptr pStr = PtrToStringChars(s); + CefString cefStr(pStr); + result.push_back(cefStr); + } + + 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..561dd8114 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 string. + 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.WinForms/WebView.cs b/CefSharp.WinForms/WebView.cs index b6bc6a2af..d6b873c6c 100644 --- a/CefSharp.WinForms/WebView.cs +++ b/CefSharp.WinForms/WebView.cs @@ -16,7 +16,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/WebView.cs b/CefSharp.Wpf/WebView.cs index 5f2d1bbcf..a4416bb45 100644 --- a/CefSharp.Wpf/WebView.cs +++ b/CefSharp.Wpf/WebView.cs @@ -42,6 +42,7 @@ namespace CefSharp.Wpf public BrowserSettings BrowserSettings { get; set; } public bool IsBrowserInitialized { get; private 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/CefSharp.csproj b/CefSharp/CefSharp.csproj index e8c37f99b..727f77c36 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -86,6 +86,7 @@ + diff --git a/CefSharp/IDialogHandler.cs b/CefSharp/IDialogHandler.cs new file mode 100644 index 000000000..90736d354 --- /dev/null +++ b/CefSharp/IDialogHandler.cs @@ -0,0 +1,8 @@ +using System.Collections.Generic; +namespace CefSharp +{ + public interface IDialogHandler + { + bool OnOpenFile(IWebBrowser browser, string title, string default_file_name, List accept_types, out List result); + } +} diff --git a/CefSharp/Internals/IWebBrowserInternal.cs b/CefSharp/Internals/IWebBrowserInternal.cs index f38870a84..f104aba1d 100644 --- a/CefSharp/Internals/IWebBrowserInternal.cs +++ b/CefSharp/Internals/IWebBrowserInternal.cs @@ -9,6 +9,7 @@ namespace CefSharp.Internals ILifeSpanHandler LifeSpanHandler { get; set; } IKeyboardHandler KeyboardHandler { get; set; } IJsDialogHandler JsDialogHandler { get; set; } + IDialogHandler DialogHandler { get; set; } void OnInitialized(); diff --git a/CefSharp3.sln b/CefSharp3.sln index 92156222f..36238a0d2 100644 --- a/CefSharp3.sln +++ b/CefSharp3.sln @@ -47,6 +47,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CefSharp.Core", "CefSharp.C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CefSharp", "CefSharp\CefSharp.csproj", "{A55848CC-10E7-40CB-ADDB-04740B16DD43}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{A3BF0056-FB83-4682-9130-6EDAB93CB5B9}" + ProjectSection(SolutionItems) = preProject + .nuget\packages.config = .nuget\packages.config + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 From a85b7cc192ce9856b5778192f36a30b88479555b Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 15:18:11 +1000 Subject: [PATCH 02/16] Revert change to solution --- CefSharp3.sln | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CefSharp3.sln b/CefSharp3.sln index 36238a0d2..92156222f 100644 --- a/CefSharp3.sln +++ b/CefSharp3.sln @@ -47,11 +47,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CefSharp.Core", "CefSharp.C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CefSharp", "CefSharp\CefSharp.csproj", "{A55848CC-10E7-40CB-ADDB-04740B16DD43}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{A3BF0056-FB83-4682-9130-6EDAB93CB5B9}" - ProjectSection(SolutionItems) = preProject - .nuget\packages.config = .nuget\packages.config - EndProjectSection -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 From a9ef42c8891a7d06425de4a2627c21658b948cac Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 15:20:31 +1000 Subject: [PATCH 03/16] Rename params to match convention --- CefSharp/IDialogHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CefSharp/IDialogHandler.cs b/CefSharp/IDialogHandler.cs index 90736d354..61bf44bda 100644 --- a/CefSharp/IDialogHandler.cs +++ b/CefSharp/IDialogHandler.cs @@ -3,6 +3,6 @@ namespace CefSharp { public interface IDialogHandler { - bool OnOpenFile(IWebBrowser browser, string title, string default_file_name, List accept_types, out List result); + bool OnOpenFile(IWebBrowser browser, string title, string defaultFileName, List acceptTypes, out List result); } } From 5cb0dac62c0e26e12dcc4bd9a6c847cfc8c22fc3 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 15:21:47 +1000 Subject: [PATCH 04/16] Fix formatting, indentation (spaces not tabs) and column wrapping. --- CefSharp.Core/Internals/ClientAdapter.cpp | 37 +++++++++++------------ CefSharp.Core/Internals/ClientAdapter.h | 15 ++++----- CefSharp.Core/Internals/StringUtils.h | 24 +++++++-------- 3 files changed, 35 insertions(+), 41 deletions(-) diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 4d0c5427c..264553da9 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -425,30 +425,27 @@ namespace CefSharp 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; + 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; - } + if (handler == nullptr) + { + return false; + } - bool result; - bool handled; + bool result; + bool handled; - List^ resultString = nullptr; + List^ resultString = nullptr; - result = handler->OnOpenFile(_browserControl, StringUtils::ToClr(title), StringUtils::ToClr(default_file_name), StringUtils::ToClr(accept_types), resultString); - callback->Continue(StringUtils::ToNative(resultString)); + result = handler->OnOpenFile(_browserControl, StringUtils::ToClr(title), StringUtils::ToClr(default_file_name), StringUtils::ToClr(accept_types), resultString); + callback->Continue(StringUtils::ToNative(resultString)); - // Unknown dialog type, so we return "not handled". - return false; - } + // Unknown dialog type, so we return "not handled". + return false; + } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index c58bd862e..eb0498f15 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -27,7 +27,7 @@ namespace CefSharp public CefFocusHandler, public CefKeyboardHandler, public CefJSDialogHandler, - public CefDialogHandler + public CefDialogHandler { private: gcroot _browserControl; @@ -66,7 +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; } + virtual CefRefPtr GetDialogHandler() OVERRIDE{ return this; } // CefLifeSpanHandler virtual DECL bool OnBeforePopup(CefRefPtr browser, CefRefPtr frame, @@ -112,13 +112,10 @@ namespace CefSharp JSDialogType dialog_type, const CefString& message_text, const CefString& default_prompt_text, CefRefPtr callback, bool& suppress_message) OVERRIDE; - // CefDialogHandler - virtual bool OnFileDialog(CefRefPtr browser, - FileDialogMode mode, - const CefString& title, - const CefString& default_file_name, - const std::vector& accept_types, - CefRefPtr callback) OVERRIDE; + // CefDialogHandler + virtual 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.h b/CefSharp.Core/Internals/StringUtils.h index 561dd8114..fc203f169 100644 --- a/CefSharp.Core/Internals/StringUtils.h +++ b/CefSharp.Core/Internals/StringUtils.h @@ -29,12 +29,12 @@ 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 string. - static List^ ToClr(const std::vector& cefStr); + /// + /// Converts an unmanaged vector of strings to a (managed) .NET List of strings. + /// + /// The vector of strings that should be converted. + /// A .NET string. + 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 @@ -44,12 +44,12 @@ 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); + /// + /// 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. From 119618e22df6ec6bab9d1c074273b5f356b3a741 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 15:28:47 +1000 Subject: [PATCH 05/16] Fix indentation --- CefSharp.Core/Internals/StringUtils.cpp | 44 ++++++++++++------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/CefSharp.Core/Internals/StringUtils.cpp b/CefSharp.Core/Internals/StringUtils.cpp index c05c68158..55dcefa42 100644 --- a/CefSharp.Core/Internals/StringUtils.cpp +++ b/CefSharp.Core/Internals/StringUtils.cpp @@ -23,12 +23,12 @@ namespace CefSharp return gcnew String(cefStr.c_str()); } - [DebuggerStepThrough] - List^ StringUtils::ToClr(const std::vector& cefStr) - { - //cefStr.c_str() - return gcnew List(); - } + [DebuggerStepThrough] + List^ StringUtils::ToClr(const std::vector& cefStr) + { + //cefStr.c_str() + return gcnew List(); + } [DebuggerStepThrough] CefString StringUtils::ToNative(String^ str) @@ -43,25 +43,25 @@ namespace CefSharp return cefStr; } - [DebuggerStepThrough] - std::vector StringUtils::ToNative(List^ str) - { - if (str == nullptr) - { - return std::vector(); - } + [DebuggerStepThrough] + std::vector StringUtils::ToNative(List^ str) + { + if (str == nullptr) + { + return std::vector(); + } - std::vector result = std::vector(); + std::vector result = std::vector(); - for each (String^ s in str) - { - pin_ptr pStr = PtrToStringChars(s); - CefString cefStr(pStr); - result.push_back(cefStr); - } + for each (String^ s in str) + { + pin_ptr pStr = PtrToStringChars(s); + CefString cefStr(pStr); + result.push_back(cefStr); + } - return result; - } + return result; + } [DebuggerStepThrough] void StringUtils::AssignNativeFromClr(cef_string_t& cefStr, String^ str) From 29000d2c40e5f425d5e02627a91fe314e0b748c2 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 15:29:09 +1000 Subject: [PATCH 06/16] Remove code duplication --- CefSharp.Core/Internals/StringUtils.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CefSharp.Core/Internals/StringUtils.cpp b/CefSharp.Core/Internals/StringUtils.cpp index 55dcefa42..0ee52094d 100644 --- a/CefSharp.Core/Internals/StringUtils.cpp +++ b/CefSharp.Core/Internals/StringUtils.cpp @@ -55,9 +55,7 @@ namespace CefSharp for each (String^ s in str) { - pin_ptr pStr = PtrToStringChars(s); - CefString cefStr(pStr); - result.push_back(cefStr); + result.push_back(StringUtils::ToNative(s)); } return result; From 1e4dfe7329207be508f157340b388a93f5f77946 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 16:14:37 +1000 Subject: [PATCH 07/16] Add missing deceleration keyword --- CefSharp.Core/Internals/ClientAdapter.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index eb0498f15..0db007189 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -108,12 +108,12 @@ 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 bool OnFileDialog(CefRefPtr browser, FileDialogMode mode, const CefString& title, + virtual DECL bool OnFileDialog(CefRefPtr browser, FileDialogMode mode, const CefString& title, const CefString& default_file_name, const std::vector& accept_types, CefRefPtr callback) OVERRIDE; From 26ba66ff0e6b9bb5fb0b25d7d354ddebbcbe3374 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 16:15:26 +1000 Subject: [PATCH 08/16] Implement vector to List method (just returned an empty List before) --- CefSharp.Core/Internals/StringUtils.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CefSharp.Core/Internals/StringUtils.cpp b/CefSharp.Core/Internals/StringUtils.cpp index 0ee52094d..b5fcac0c4 100644 --- a/CefSharp.Core/Internals/StringUtils.cpp +++ b/CefSharp.Core/Internals/StringUtils.cpp @@ -26,8 +26,14 @@ namespace CefSharp [DebuggerStepThrough] List^ StringUtils::ToClr(const std::vector& cefStr) { - //cefStr.c_str() - return gcnew List(); + auto result = gcnew List(); + + for each(CefString s in cefStr) + { + result->Add(StringUtils::ToClr(s)); + } + + return result; } [DebuggerStepThrough] From aa651bf71edacdc3b25c200495c845088cec4e5e Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 16:30:19 +1000 Subject: [PATCH 09/16] Return result from IDialogHandler.OnOpenFile --- CefSharp.Core/Internals/ClientAdapter.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 264553da9..5ec5356c5 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -444,8 +444,7 @@ namespace CefSharp result = handler->OnOpenFile(_browserControl, StringUtils::ToClr(title), StringUtils::ToClr(default_file_name), StringUtils::ToClr(accept_types), resultString); callback->Continue(StringUtils::ToNative(resultString)); - // Unknown dialog type, so we return "not handled". - return false; + return result; } } } From bc15305601d24175c0f1157374ab6059acd81424 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 16:33:01 +1000 Subject: [PATCH 10/16] Rename method OnOpenFile to OnFileDialog - match Cef naming (reduce confusion when consulting upstream documentation) --- CefSharp.Core/Internals/ClientAdapter.cpp | 2 +- CefSharp/IDialogHandler.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 5ec5356c5..9af4adedb 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -441,7 +441,7 @@ namespace CefSharp List^ resultString = nullptr; - result = handler->OnOpenFile(_browserControl, StringUtils::ToClr(title), StringUtils::ToClr(default_file_name), StringUtils::ToClr(accept_types), resultString); + result = handler->OnFileDialog(_browserControl, StringUtils::ToClr(title), StringUtils::ToClr(default_file_name), StringUtils::ToClr(accept_types), resultString); callback->Continue(StringUtils::ToNative(resultString)); return result; diff --git a/CefSharp/IDialogHandler.cs b/CefSharp/IDialogHandler.cs index 61bf44bda..d54d847f8 100644 --- a/CefSharp/IDialogHandler.cs +++ b/CefSharp/IDialogHandler.cs @@ -3,6 +3,6 @@ namespace CefSharp { public interface IDialogHandler { - bool OnOpenFile(IWebBrowser browser, string title, string defaultFileName, List acceptTypes, out List result); + bool OnFileDialog(IWebBrowser browser, string title, string defaultFileName, List acceptTypes, out List result); } } From 7a5de124fec6ef54c280627946698015440ebf12 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 2 Jun 2014 16:36:14 +1000 Subject: [PATCH 11/16] Add TempFileDialogHandler for easy testing of DialogHandler - generates a random filename --- CefSharp.Example/CefSharp.Example.csproj | 1 + CefSharp.Example/TempFileDialogHandler.cs | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 CefSharp.Example/TempFileDialogHandler.cs 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..5b1540b5c --- /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, string title, string defaultFileName, List acceptTypes, out List result) + { + result = new List { Path.GetRandomFileName() }; + return true; + } + } +} From 48339c2a14641baacc8689cbc9f532f4c8678506 Mon Sep 17 00:00:00 2001 From: amaitland Date: Tue, 3 Jun 2014 00:25:37 +1000 Subject: [PATCH 12/16] Spaces not tabs --- CefSharp.Example/TempFileDialogHandler.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/CefSharp.Example/TempFileDialogHandler.cs b/CefSharp.Example/TempFileDialogHandler.cs index 5b1540b5c..2f902d98c 100644 --- a/CefSharp.Example/TempFileDialogHandler.cs +++ b/CefSharp.Example/TempFileDialogHandler.cs @@ -3,12 +3,12 @@ using System.IO; namespace CefSharp.Example { - public class TempFileDialogHandler : IDialogHandler - { - public bool OnFileDialog(IWebBrowser browser, string title, string defaultFileName, List acceptTypes, out List result) - { - result = new List { Path.GetRandomFileName() }; - return true; - } - } + public class TempFileDialogHandler : IDialogHandler + { + public bool OnFileDialog(IWebBrowser browser, string title, string defaultFileName, List acceptTypes, out List result) + { + result = new List { Path.GetRandomFileName() }; + return true; + } + } } From 46b7f4ab9cebf50cce7c09a5192db639b8bd0967 Mon Sep 17 00:00:00 2001 From: amaitland Date: Tue, 3 Jun 2014 10:18:20 +1000 Subject: [PATCH 13/16] Amend Xml comment --- CefSharp.Core/Internals/StringUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CefSharp.Core/Internals/StringUtils.h b/CefSharp.Core/Internals/StringUtils.h index fc203f169..2df938a72 100644 --- a/CefSharp.Core/Internals/StringUtils.h +++ b/CefSharp.Core/Internals/StringUtils.h @@ -33,7 +33,7 @@ namespace CefSharp /// Converts an unmanaged vector of strings to a (managed) .NET List of strings. /// /// The vector of strings that should be converted. - /// A .NET string. + /// A .NET List of strings. static List^ ToClr(const std::vector& cefStr); /// From dc5fdaad9f80eba3da2aeb2010e4bef2731af916 Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 6 Jun 2014 07:58:10 +1000 Subject: [PATCH 14/16] Add CefFileDialogMode enum to IDialogHandler.OnFileDialog --- CefSharp.Core/Internals/ClientAdapter.cpp | 2 +- CefSharp.Example/TempFileDialogHandler.cs | 2 +- CefSharp/CefFileDialogMode.cs | 20 ++++++++++++++++++++ CefSharp/CefSharp.csproj | 1 + CefSharp/IDialogHandler.cs | 2 +- 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 CefSharp/CefFileDialogMode.cs diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 39c1aa663..a72ee41b3 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -450,7 +450,7 @@ namespace CefSharp List^ resultString = nullptr; - result = handler->OnFileDialog(_browserControl, StringUtils::ToClr(title), StringUtils::ToClr(default_file_name), StringUtils::ToClr(accept_types), resultString); + 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.Example/TempFileDialogHandler.cs b/CefSharp.Example/TempFileDialogHandler.cs index 2f902d98c..75581a266 100644 --- a/CefSharp.Example/TempFileDialogHandler.cs +++ b/CefSharp.Example/TempFileDialogHandler.cs @@ -5,7 +5,7 @@ namespace CefSharp.Example { public class TempFileDialogHandler : IDialogHandler { - public bool OnFileDialog(IWebBrowser browser, string title, string defaultFileName, List acceptTypes, out List result) + 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/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 d19c5138c..e8085026a 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -86,6 +86,7 @@ + diff --git a/CefSharp/IDialogHandler.cs b/CefSharp/IDialogHandler.cs index d54d847f8..6c673e260 100644 --- a/CefSharp/IDialogHandler.cs +++ b/CefSharp/IDialogHandler.cs @@ -3,6 +3,6 @@ namespace CefSharp { public interface IDialogHandler { - bool OnFileDialog(IWebBrowser browser, string title, string defaultFileName, List acceptTypes, out List result); + bool OnFileDialog(IWebBrowser browser, CefFileDialogMode mode, string title, string defaultFileName, List acceptTypes, out List result); } } From cd13ca7e4288daf6b0de6ea0ead2a1191a95d492 Mon Sep 17 00:00:00 2001 From: jornh Date: Tue, 10 Jun 2014 21:40:04 +0200 Subject: [PATCH 15/16] Make DialogHandler visible to Wpf too --- CefSharp/IWebBrowser.cs | 1 + CefSharp/Internals/IWebBrowserInternal.cs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) 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; } diff --git a/CefSharp/Internals/IWebBrowserInternal.cs b/CefSharp/Internals/IWebBrowserInternal.cs index c7c8815e2..a89d2f294 100644 --- a/CefSharp/Internals/IWebBrowserInternal.cs +++ b/CefSharp/Internals/IWebBrowserInternal.cs @@ -10,7 +10,6 @@ namespace CefSharp.Internals ILifeSpanHandler LifeSpanHandler { get; set; } IKeyboardHandler KeyboardHandler { get; set; } IJsDialogHandler JsDialogHandler { get; set; } - IDialogHandler DialogHandler { get; set; } //end void OnInitialized(); From 234250f914cde8a135a1e9ea828b80ce96916037 Mon Sep 17 00:00:00 2001 From: jornh Date: Tue, 10 Jun 2014 21:41:13 +0200 Subject: [PATCH 16/16] Document IDialogHandler --- CefSharp/IDialogHandler.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CefSharp/IDialogHandler.cs b/CefSharp/IDialogHandler.cs index 6c673e260..cab7f45d3 100644 --- a/CefSharp/IDialogHandler.cs +++ b/CefSharp/IDialogHandler.cs @@ -1,8 +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); } }