// Copyright © 2021 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System.IO; namespace CefSharp.Fluent { /// /// Called before a download begins in response to a user-initiated action /// (e.g. alt + link click or link click that returns a `Content-Disposition: /// attachment` response from the server). /// /// the ChromiumWebBrowser control /// The browser instance /// is the target download URL /// is the target method (GET, POST, etc) /// Return true to proceed with the download or false to cancel the download. public delegate bool CanDownloadDelegate(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod); /// /// Called before a download begins. /// /// the ChromiumWebBrowser control /// The browser instance /// Represents the file being downloaded. /// Callback interface used to asynchronously continue a download. /// Return true and execute either /// asynchronously or in this method to continue or cancel the download. /// Return false to proceed with default handling (cancel with Alloy style, /// download shelf with Chrome style). public delegate bool OnBeforeDownloadDelegate(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback); /// /// Called when a download's status or progress information has been updated. This may be called multiple times before and after . /// /// the ChromiumWebBrowser control /// The browser instance /// Represents the file being downloaded. /// The callback used to Cancel/Pause/Resume the process public delegate void OnDownloadUpdatedDelegate(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback); /// /// A implementation used by /// to provide a fluent means of creating a . /// public class DownloadHandler : Handler.DownloadHandler { private CanDownloadDelegate canDownload; private OnBeforeDownloadDelegate onBeforeDownload; private OnDownloadUpdatedDelegate onDownloadUpdated; /// /// Create a new DownloadHandler Builder /// /// Fluent DownloadHandler Builder public static DownloadHandlerBuilder Create() { return new DownloadHandlerBuilder(); } /// /// Creates a new instances /// where all downloads are automatically downloaded to the specified folder. /// No dialog is dispolayed to the user. /// /// folder where files are download. /// optional delegate for download updates, track progress, completion etc. /// instance. public static IDownloadHandler UseFolder(string folder, OnDownloadUpdatedDelegate downloadUpdated = null) { return Create() .OnBeforeDownload((chromiumWebBrowser, browser, item, callback) => { using (callback) { var path = Path.Combine(folder, item.SuggestedFileName); callback.Continue(path, showDialog: false); } return true; }) .OnDownloadUpdated(downloadUpdated) .Build(); } /// /// Creates a new instances /// where a default "Save As" dialog is displayed to the user. /// /// optional delegate for download updates, track progress, completion etc. /// instance. public static IDownloadHandler AskUser(OnDownloadUpdatedDelegate downloadUpdated = null) { return Create() .OnBeforeDownload((chromiumWebBrowser, browser, item, callback) => { using (callback) { callback.Continue("", showDialog: true); } return true; }) .OnDownloadUpdated(downloadUpdated) .Build(); } /// /// Use to create a new instance of the fluent builder /// internal DownloadHandler() { } internal void SetCanDownload(CanDownloadDelegate action) { canDownload = action; } internal void SetOnBeforeDownload(OnBeforeDownloadDelegate action) { onBeforeDownload = action; } internal void SetOnDownloadUpdated(OnDownloadUpdatedDelegate action) { onDownloadUpdated = action; } /// protected override bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod) { return canDownload?.Invoke(chromiumWebBrowser, browser, url, requestMethod) ?? true; } /// protected override bool OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { return onBeforeDownload?.Invoke(chromiumWebBrowser, browser, downloadItem, callback) ?? false; } /// protected override void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { onDownloadUpdated?.Invoke(chromiumWebBrowser, browser, downloadItem, callback); } } }