This repository has been archived on 2025-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
xerobrowser/WebBrowser/DownloadHandler.cs
Diamond Creeper 81286909c6 Update
Updated cefsharp and removed flash.
But there is now a bug where the browser crashed when trying to download anything.
2023-05-18 17:03:55 +12:00

97 lines
No EOL
3.2 KiB
C#

// 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.ComponentModel;
using System.IO;
using System.Windows.Forms;
using CefSharp;
using Timer = System.Threading.Timer;
// ReSharper disable NotAccessedField.Local
namespace XeroBrowser
{
public class DownloadHandler : IDownloadHandler
{
public event EventHandler<DownloadItem> OnBeforeDownloadFired;
public event EventHandler<DownloadItem> OnDownloadUpdatedFired;
private readonly FrmBrowser _frmref;
public DownloadHandler(FrmBrowser frm)
{
_frmref = frm;
}
private readonly DownloadProgress _downloadProgress = new DownloadProgress();
private 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, FormClosingEventArgs e)
{
_downloadItem.IsCancelled = true;
}
private Timer _fivesecondtimer;
public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
OnDownloadUpdatedFired?.Invoke(this, downloadItem);
if (downloadItem.IsComplete)
{
_fivesecondtimer = new Timer(tcallback, null, 1000 * 5, 1000 * 5);
}
_frmref.Invoke((Action)delegate {
_downloadProgress.ProgressBar1.Value = downloadItem.PercentComplete;
_downloadProgress.ProgressPrecentage.Text = downloadItem.PercentComplete + @"% " + (downloadItem.ReceivedBytes / 0x100000) + @"MB / " + (downloadItem.TotalBytes / 0x100000) + @"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){}
}
public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)
{
throw new NotImplementedException();
}
}
}