-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/file dialog handler #1
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using CefSharp.OutOfProcess.Interface; | ||
|
|
||
| namespace CefSharp.OutOfProcess.BrowserProcess.CallbackProxies | ||
| { | ||
| internal class CallbackProxyBase<T> : IDisposable | ||
| { | ||
| private int id = 0; | ||
| private readonly Dictionary<int, T> callbacks = new Dictionary<int, T>(); | ||
| private protected readonly IOutOfProcessHostRpc host; | ||
|
|
||
| public CallbackProxyBase(IOutOfProcessHostRpc host) | ||
| { | ||
| this.host = host ?? throw new ArgumentNullException(nameof(host)); | ||
| } | ||
|
|
||
| protected int CreateCallback(T callback) | ||
| { | ||
| int lid = id++; | ||
| callbacks.Add(lid, callback); | ||
| return lid; | ||
| } | ||
|
|
||
| protected T GetCallback(int id) | ||
| { | ||
| T cb = callbacks[id]; | ||
| callbacks.Remove(id); | ||
| return cb; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| foreach (var cbs in callbacks) | ||
| { | ||
| if (cbs.Value is IDisposable d) | ||
| { | ||
| d.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| callbacks.Clear(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| using System.Collections.Generic; | ||
| using CefSharp.OutOfProcess.Interface; | ||
| using CefSharp.Handler; | ||
| using System.Linq; | ||
| using CefSharp.OutOfProcess.Interface.Callbacks; | ||
|
|
||
| namespace CefSharp.OutOfProcess.BrowserProcess.CallbackProxies | ||
| { | ||
|
|
||
| internal sealed class DialogHandlerProxy : CallbackProxyBase<IFileDialogCallback>, IDialogHandler | ||
| { | ||
| public DialogHandlerProxy(IOutOfProcessHostRpc host) | ||
| : base(host) | ||
| { | ||
|
|
||
| } | ||
|
|
||
| public void Callback(FileDialogCallbackDetails details) | ||
| { | ||
| var cb = GetCallback(details.CallbackId); | ||
|
|
||
| if (details.Continue) | ||
| { | ||
| cb.Continue(details.Files.ToList()); | ||
| } | ||
| else | ||
| { | ||
| cb.Cancel(); | ||
| } | ||
| } | ||
|
|
||
| bool IDialogHandler.OnFileDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List<string> acceptFilters, IFileDialogCallback callback) | ||
| { | ||
| var result = host.OnFileDialog(((OutOfProcessChromiumWebBrowser)chromiumWebBrowser).Id, mode.ToString(), title, defaultFilePath, acceptFilters.ToArray(), CreateCallback(callback)); | ||
| return result.Result; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| namespace CefSharp.OutOfProcess | ||
| { | ||
| using System; | ||
| using CefSharp.OutOfProcess.Internal; | ||
|
|
||
| internal class CallbackProxyBase : IDisposable | ||
| { | ||
| private protected readonly OutOfProcessHost outOfProcessHost; | ||
| private protected readonly int callback; | ||
| private protected readonly IChromiumWebBrowserInternal chromiumWebBrowser; | ||
| private bool disposedValue; | ||
|
|
||
| public CallbackProxyBase(OutOfProcessHost outOfProcessHost, int callback, IChromiumWebBrowserInternal chromiumWebBrowser) | ||
| { | ||
| this.chromiumWebBrowser = chromiumWebBrowser; | ||
| this.outOfProcessHost = outOfProcessHost; | ||
| this.callback = callback; | ||
| } | ||
|
|
||
| public bool IsDisposed => disposedValue; | ||
|
|
||
| protected virtual void Dispose(bool disposing) | ||
| { | ||
| if (!disposedValue) | ||
| { | ||
| if (disposing) | ||
| { | ||
| // TODO: dispose managed state (managed objects) | ||
|
||
| } | ||
|
|
||
| // TODO: free unmanaged resources (unmanaged objects) and override finalizer | ||
| // TODO: set large fields to null | ||
| disposedValue = true; | ||
| } | ||
| } | ||
|
|
||
| void IDisposable.Dispose() | ||
| { | ||
| // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
|
||
| Dispose(disposing: true); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| namespace CefSharp.OutOfProcess | ||
| { | ||
| using System.Collections.Generic; | ||
| using CefSharp.OutOfProcess.Interface.Callbacks; | ||
| using CefSharp.OutOfProcess.Internal; | ||
|
|
||
| internal sealed class FileDialogCallbackProxy : CallbackProxyBase, IFileDialogCallback | ||
| { | ||
| public FileDialogCallbackProxy(OutOfProcessHost outOfProcessHost, int callback, IChromiumWebBrowserInternal chromiumWebBrowser) | ||
| : base(outOfProcessHost, callback, chromiumWebBrowser) | ||
| { | ||
| } | ||
|
|
||
| public void Cancel() | ||
| { | ||
| outOfProcessHost.InvokeFileDialogCallback(new FileDialogCallbackDetails() | ||
| { | ||
| CallbackId = callback, | ||
| BrowserId = chromiumWebBrowser.Id, | ||
| Continue = false, | ||
| }); | ||
| } | ||
|
|
||
| public void Continue(List<string> filePaths) | ||
| { | ||
| outOfProcessHost.InvokeFileDialogCallback(new FileDialogCallbackDetails() | ||
| { | ||
| CallbackId = callback, | ||
| BrowserId = chromiumWebBrowser.Id, | ||
| Continue = true, | ||
| Files = filePaths.ToArray(), | ||
| }); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <PlatformTarget>AnyCPU</PlatformTarget> | ||
|
||
| <RootNamespace>CefSharp.OutOfProcess</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
|
|
@@ -10,7 +11,6 @@ | |
| <PackageReference Include="PInvoke.User32" Version="0.7.104" /> | ||
| <PackageReference Include="StreamJsonRpc" Version="2.11.35" /> | ||
| <ProjectReference Include="..\CefSharp.OutOfProcess.Interface\CefSharp.OutOfProcess.Interface.csproj" /> | ||
|
|
||
| <ProjectReference Include="..\CefSharp.Dom\lib\PuppeteerSharp\CefSharp.Dom.OutOfProcess.csproj" /> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing line :) |
||
| </ItemGroup> | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| namespace CefSharp.OutOfProcess.Handler | ||
| { | ||
| using System.Collections.Generic; | ||
| using CefSharp.OutOfProcess.Interface.Callbacks; | ||
|
|
||
| public interface IDialogHandler | ||
| { | ||
| // | ||
| // Summary: | ||
| // Runs a file chooser dialog. | ||
| // | ||
| // Parameters: | ||
| // chromiumWebBrowser: | ||
| // the ChromiumWebBrowser control | ||
| // | ||
| // browser: | ||
| // the browser object | ||
| // | ||
| // mode: | ||
| // represents the type of dialog to display | ||
| // | ||
| // 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). | ||
| // | ||
| // defaultFilePath: | ||
| // is the path with optional directory and/or file name component that should be | ||
| // initially selected in the dialog. | ||
| // | ||
| // acceptFilters: | ||
| // are used to restrict the selectable file types and may any combination of (a) | ||
| // valid lower-cased MIME types (e.g. "text/*" or "image/*"), (b) individual file | ||
| // extensions (e.g. ".txt" or ".png"), (c) combined description and file extension | ||
| // delimited using "|" and ";" (e.g. "Image Types|.png;.gif;.jpg"). | ||
| // | ||
| // callback: | ||
| // Callback interface for asynchronous continuation of file dialog requests. | ||
| // | ||
| // Returns: | ||
| // To display a custom dialog return true. To display the default dialog return | ||
| // false. | ||
| bool OnFileDialog(IChromiumWebBrowser chromiumWebBrowser, string mode, string title, string defaultFilePath, IEnumerable<string> acceptFilters, IFileDialogCallback callback); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace CefSharp.OutOfProcess.Interface.Callbacks | ||
| { | ||
| public class CallbackDetails | ||
| { | ||
| public int BrowserId { get; set; } | ||
|
|
||
| public int CallbackId { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace CefSharp.OutOfProcess.Interface.Callbacks | ||
| { | ||
| public class FileDialogCallbackDetails : CallbackDetails | ||
| { | ||
| public string[] Files { get; set; } | ||
|
|
||
| public bool Continue { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace CefSharp.OutOfProcess.Interface.Callbacks | ||
| { | ||
| using System.Collections.Generic; | ||
|
|
||
| public interface IFileDialogCallback | ||
| { | ||
| bool IsDisposed { get; } | ||
| void Cancel(); | ||
| void Continue(List<string> filePaths); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.