Merge pull request #380 from amaitland/feature/DialogHandler

Implement CefDialogHandler based on #342
This commit is contained in:
Per Lundberg
2014-06-11 00:19:15 +03:00
12 changed files with 150 additions and 3 deletions
+22
View File
@@ -433,5 +433,27 @@ namespace CefSharp
// Unknown dialog type, so we return "not handled".
return false;
}
bool ClientAdapter::OnFileDialog(CefRefPtr<CefBrowser> browser, FileDialogMode mode, const CefString& title,
const CefString& default_file_name, const std::vector<CefString>& accept_types,
CefRefPtr<CefFileDialogCallback> callback)
{
IDialogHandler^ handler = _browserControl->DialogHandler;
if (handler == nullptr)
{
return false;
}
bool result;
bool handled;
List<System::String ^>^ 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;
}
}
}
+9 -2
View File
@@ -26,7 +26,8 @@ namespace CefSharp
public CefContextMenuHandler,
public CefFocusHandler,
public CefKeyboardHandler,
public CefJSDialogHandler
public CefJSDialogHandler,
public CefDialogHandler
{
private:
gcroot<IWebBrowserInternal^> _browserControl;
@@ -65,6 +66,7 @@ namespace CefSharp
virtual CefRefPtr<CefFocusHandler> GetFocusHandler() OVERRIDE{ return this; }
virtual CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() OVERRIDE{ return this; }
virtual CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() OVERRIDE{ return this; }
virtual CefRefPtr<CefDialogHandler> GetDialogHandler() OVERRIDE{ return this; }
// CefLifeSpanHandler
virtual DECL bool OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
@@ -107,10 +109,15 @@ namespace CefSharp
virtual DECL bool OnKeyEvent(CefRefPtr<CefBrowser> browser, const CefKeyEvent& event, CefEventHandle os_event) OVERRIDE;
// CefJSDialogHandler
virtual bool OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin_url, const CefString& accept_lang,
virtual DECL bool OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin_url, const CefString& accept_lang,
JSDialogType dialog_type, const CefString& message_text, const CefString& default_prompt_text,
CefRefPtr<CefJSDialogCallback> callback, bool& suppress_message) OVERRIDE;
// CefDialogHandler
virtual DECL bool OnFileDialog(CefRefPtr<CefBrowser> browser, FileDialogMode mode, const CefString& title,
const CefString& default_file_name, const std::vector<CefString>& accept_types,
CefRefPtr<CefFileDialogCallback> callback) OVERRIDE;
IMPLEMENT_LOCKING(ClientAdapter);
IMPLEMENT_REFCOUNTING(ClientAdapter);
};
+32
View File
@@ -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<String^>^ StringUtils::ToClr(const std::vector<CefString>& cefStr)
{
auto result = gcnew List<String^>();
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<CefString> StringUtils::ToNative(List<String^>^ str)
{
if (str == nullptr)
{
return std::vector<CefString>();
}
std::vector<CefString> result = std::vector<CefString>();
for each (String^ s in str)
{
result.push_back(StringUtils::ToNative(s));
}
return result;
}
[DebuggerStepThrough]
void StringUtils::AssignNativeFromClr(cef_string_t& cefStr, String^ str)
{
+15
View File
@@ -6,6 +6,7 @@
#pragma once
using namespace System;
using namespace System::Collections::Generic;
namespace CefSharp
{
@@ -28,6 +29,13 @@ namespace CefSharp
/// <returns>A .NET string.</returns>
static String^ ToClr(const CefString& cefStr);
/// <summary>
/// Converts an unmanaged vector of strings to a (managed) .NET List of strings.
/// </summary>
/// <param name="cefStr">The vector of strings that should be converted.</param>
/// <returns>A .NET List of strings.</returns>
static List<String^>^ ToClr(const std::vector<CefString>& cefStr);
/// <summary>
/// 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
/// <returns>An unmanaged representation of the provided string, or an empty string if the input string is a nullptr.</returns>
static CefString ToNative(String^ str);
/// <summary>
/// Converts a .NET List of strings to native (unmanaged) format.
/// </summary>
/// <param name="str">The List of strings that should be converted.</param>
/// <returns>An unmanaged representation of the provided List of strings, or an empty List if the input is a nullptr.</returns>
static std::vector<CefString> ToNative(List<String^>^ str);
/// <summary>
/// Assigns the provided cef_string_t object from the given .NET string.
/// </summary>
+1
View File
@@ -84,6 +84,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BoundObject.cs" />
<Compile Include="TempFileDialogHandler.cs" />
<Compile Include="DownloadHandler.cs" />
<Compile Include="ExamplePresenter.cs" />
<Compile Include="ExtensionMethods\PropertyChangedExtensionMethods.cs" />
+14
View File
@@ -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<string> acceptTypes, out List<string> result)
{
result = new List<string> { Path.GetRandomFileName() };
return true;
}
}
}
+2 -1
View File
@@ -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; }
+1
View File
@@ -43,6 +43,7 @@ namespace CefSharp.Wpf
private readonly List<IDisposable> disposables = new List<IDisposable>();
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; }
+20
View File
@@ -0,0 +1,20 @@
namespace CefSharp
{
public enum CefFileDialogMode
{
/// <summary>
/// Requires that the file exists before allowing the user to pick it.
/// </summary>
Open = 0,
/// <summary>
/// Like Open, but allows picking multiple files to open.
/// </summary>
OpenMultiple,
/// <summary>
/// Allows picking a nonexistent file, and prompts to overwrite if the file already exists.
/// </summary>
Save
}
}
+2
View File
@@ -86,6 +86,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CefFileDialogMode.cs" />
<Compile Include="IDialogHandler.cs" />
<Compile Include="AddressChangedEventArgs.cs" />
<Compile Include="IStringVisitor.cs" />
<Compile Include="FrameLoadStartEventArgs.cs" />
+31
View File
@@ -0,0 +1,31 @@
using System.Collections.Generic;
namespace CefSharp
{
/// <summary>
/// Implement this interface to handle dialog events. The methods of this class will be called on the browser process UI thread.
/// </summary>
public interface IDialogHandler
{
/// <summary>
/// Runs a file chooser dialog.
/// </summary>
/// <example>
/// To test assign something like TempFileDialogHandler (from CefSharp.Example) to DialogHandler e.g.
/// <code>
/// browser.DialogHandler = new TempFileDialogHandler();
/// </code>
/// 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.
/// </example>
/// <param name="browser">the browser object</param>
/// <param name="mode">represents the type of dialog to display</param>
/// <param name="title">the title to be used for the dialog. It may be empty to show the default title ("Open" or "Save"
/// depending on the mode).</param>
/// <param name="defaultFileName">the default file name to select in the dialog.</param>
/// <param name="acceptTypes">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.</param>
/// <param name="result">the filename(s) the dialog returns</param>
/// <returns>To display a custom dialog return true. To display the default dialog return false.</returns>
bool OnFileDialog(IWebBrowser browser, CefFileDialogMode mode, string title, string defaultFileName, List<string> acceptTypes, out List<string> result);
}
}
+1
View File
@@ -72,6 +72,7 @@ namespace CefSharp
/// <param name="timeout">The timeout after which the Javascript code execution should be aborted.</param>
object EvaluateScript(string script, TimeSpan? timeout = null);
IDialogHandler DialogHandler { get; set; }
IRequestHandler RequestHandler { get; set; }
bool IsBrowserInitialized { get; }