From 32292ea47df5724985edf89b1fd11d34dfb0117c Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 6 Jun 2014 11:26:27 +1000 Subject: [PATCH] Implement DownloadHandler based on current Cef API. Still needs some work --- CefSharp.Core/Internals/DownloadAdapter.h | 36 ++++++++++++++++------- CefSharp.Example/DownloadHandler.cs | 18 ++++-------- CefSharp/CefSharp.csproj | 1 + CefSharp/DownloadItem.cs | 19 ++++++++++++ CefSharp/IDownloadHandler.cs | 6 ++-- 5 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 CefSharp/DownloadItem.cs diff --git a/CefSharp.Core/Internals/DownloadAdapter.h b/CefSharp.Core/Internals/DownloadAdapter.h index cebd153f3..3ee59a712 100644 --- a/CefSharp.Core/Internals/DownloadAdapter.h +++ b/CefSharp.Core/Internals/DownloadAdapter.h @@ -31,28 +31,44 @@ namespace CefSharp virtual void OnBeforeDownload(CefRefPtr browser, CefRefPtr download_item, const CefString& suggested_name, CefRefPtr 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 browser, CefRefPtr download_item, CefRefPtr callback) OVERRIDE { - array^ bytes = gcnew array(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 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); diff --git a/CefSharp.Example/DownloadHandler.cs b/CefSharp.Example/DownloadHandler.cs index 335a9e8cd..3b1963d2e 100644 --- a/CefSharp.Example/DownloadHandler.cs +++ b/CefSharp.Example/DownloadHandler.cs @@ -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; } } } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index e8085026a..071ce7c44 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -89,6 +89,7 @@ + diff --git a/CefSharp/DownloadItem.cs b/CefSharp/DownloadItem.cs new file mode 100644 index 000000000..ad318cfd9 --- /dev/null +++ b/CefSharp/DownloadItem.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; } + } +} diff --git a/CefSharp/IDownloadHandler.cs b/CefSharp/IDownloadHandler.cs index 79bdd55bd..f49da0e09 100644 --- a/CefSharp/IDownloadHandler.cs +++ b/CefSharp/IDownloadHandler.cs @@ -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); } }