// Copyright © 2013 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; using System.Threading.Tasks; using WebBrowser; using CefSharp; using System.IO; using System.Threading; using System.Linq.Expressions; using System.ComponentModel; namespace DiamondCreeperBrowser { public class DownloadHandler : IDownloadHandler { public event EventHandler OnBeforeDownloadFired; public event EventHandler OnDownloadUpdatedFired; public frmBrowser frmref; public DownloadHandler(frmBrowser frm) { frmref = frm; } public DownloadProgress downloadProgress = new DownloadProgress(); public DownloadItem downloadItem; public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { OnBeforeDownloadFired?.Invoke(this, downloadItem); this.downloadItem = downloadItem; try { frmref.Invoke((Action)delegate { downloadProgress.Show(); downloadProgress.FormClosing += DownloadProgress_FormClosing; }); } catch (ObjectDisposedException){}; if (!callback.IsDisposed) { using (callback) { callback.Continue(downloadItem.SuggestedFileName, showDialog: true); } } } private void DownloadProgress_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { downloadItem.IsCancelled = true; } public Timer fivesecondtimer; public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { OnDownloadUpdatedFired?.Invoke(this, downloadItem); if (downloadItem.IsComplete) { fivesecondtimer = new Timer(new TimerCallback(tcallback), null, 1000 * 5, 1000 * 5); } frmref.Invoke((Action)delegate { downloadProgress.ProgressBar1.Value = downloadItem.PercentComplete; downloadProgress.ProgressPrecentage.Text = downloadItem.PercentComplete.ToString() + "% " + (downloadItem.ReceivedBytes / 0x100000).ToString() + "MB / " + (downloadItem.TotalBytes / 0x100000).ToString() + "MB"; downloadProgress.ProgressFileName.Text = Path.GetFileName(downloadItem.FullPath); downloadProgress.Update(); try { } catch (InvalidAsynchronousStateException){}; }); } private void tcallback(object state) { try { frmref.Invoke((Action)delegate { downloadProgress.Close(); }); } catch (InvalidOperationException){}; } } }