mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 12:09:40 +00:00
Implement DownloadHandler based on current Cef API. Still needs some work
This commit is contained in:
@@ -31,28 +31,44 @@ namespace CefSharp
|
||||
virtual void OnBeforeDownload(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item,
|
||||
const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> callback) OVERRIDE
|
||||
{
|
||||
// TODO: Could consider making more of the stuff in CefDownloadItem available here to the OnBeforeDownload
|
||||
// handler.
|
||||
String^ download_path;
|
||||
bool show_dialog;
|
||||
auto downloadItem = GetDownloadItem(download_item);
|
||||
downloadItem->SuggestedFileName = StringUtils::ToClr(suggested_name);
|
||||
|
||||
if (_handler->OnBeforeDownload(StringUtils::ToClr(suggested_name), download_path, show_dialog))
|
||||
if (_handler->OnBeforeDownload(downloadItem, download_path, show_dialog))
|
||||
{
|
||||
callback->Continue(StringUtils::ToNative(download_path), show_dialog);
|
||||
}
|
||||
};
|
||||
|
||||
virtual bool ReceivedData(void* data, int data_size)
|
||||
virtual void OnDownloadUpdated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item, CefRefPtr<CefDownloadItemCallback> callback) OVERRIDE
|
||||
{
|
||||
array<Byte>^ bytes = gcnew array<Byte>(data_size);
|
||||
Marshal::Copy(IntPtr(data), bytes, 0, data_size);
|
||||
|
||||
return _handler->ReceivedData(bytes);
|
||||
|
||||
if (_handler->OnDownloadUpdated(GetDownloadItem(download_item)))
|
||||
{
|
||||
callback->Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Complete()
|
||||
private:
|
||||
CefSharp::DownloadItem^ GetDownloadItem(CefRefPtr<CefDownloadItem> download_item)
|
||||
{
|
||||
_handler->Complete();
|
||||
auto item = gcnew CefSharp::DownloadItem();
|
||||
item->Id = download_item->GetId();
|
||||
item->Url = StringUtils::ToClr(download_item->GetURL());
|
||||
item->MimeType = StringUtils::ToClr(download_item->GetMimeType());
|
||||
item->ContentDisposition = StringUtils::ToClr(download_item->GetContentDisposition());
|
||||
item->TotalBytes = download_item->GetTotalBytes();
|
||||
item->IsComplete = download_item->IsComplete();
|
||||
item->IsInProgress = download_item->IsInProgress();
|
||||
item->IsCancelled = download_item->IsCanceled();
|
||||
item->PercentComplete = download_item->GetPercentComplete();
|
||||
item->SuggestedFileName = StringUtils::ToClr(download_item->GetSuggestedFileName());
|
||||
item->FullPath = StringUtils::ToClr(download_item->GetFullPath());
|
||||
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
IMPLEMENT_REFCOUNTING(DownloadAdapter);
|
||||
|
||||
@@ -1,26 +1,18 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace CefSharp.Example
|
||||
namespace CefSharp.Example
|
||||
{
|
||||
internal class DownloadHandler : IDownloadHandler
|
||||
{
|
||||
public bool OnBeforeDownload(string suggestedName, out string downloadPath, out bool showDialog)
|
||||
public bool OnBeforeDownload(DownloadItem downloadItem, out string downloadPath, out bool showDialog)
|
||||
{
|
||||
downloadPath = Path.GetTempPath();
|
||||
downloadPath = downloadItem.SuggestedFileName;
|
||||
showDialog = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ReceivedData(byte[] data)
|
||||
public bool OnDownloadUpdated(DownloadItem downloadItem)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Complete()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +89,7 @@
|
||||
<Compile Include="CefFileDialogMode.cs" />
|
||||
<Compile Include="IDialogHandler.cs" />
|
||||
<Compile Include="AddressChangedEventArgs.cs" />
|
||||
<Compile Include="DownloadItem.cs" />
|
||||
<Compile Include="IStringVisitor.cs" />
|
||||
<Compile Include="FrameLoadStartEventArgs.cs" />
|
||||
<Compile Include="KeyType.cs" />
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
public class DownloadItem
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string MimeType { get; set; }
|
||||
public string ContentDisposition { get; set; }
|
||||
public Int64 TotalBytes { get; set; }
|
||||
public int PercentComplete { get; set; }
|
||||
public bool IsCancelled { get; set; }
|
||||
public bool IsComplete { get; set; }
|
||||
public bool IsInProgress { get; set; }
|
||||
public string SuggestedFileName { get; set; }
|
||||
public string FullPath { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
{
|
||||
public interface IDownloadHandler
|
||||
{
|
||||
bool OnBeforeDownload(string suggestedName, out string downloadPath, out bool showDialog);
|
||||
bool ReceivedData(byte[] data);
|
||||
void Complete();
|
||||
bool OnBeforeDownload(DownloadItem downloadItem, out string downloadPath, out bool showDialog);
|
||||
|
||||
bool OnDownloadUpdated(DownloadItem downloadItem);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user