diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj
index 8fd4292b5..77da727f9 100644
--- a/CefSharp.Core/CefSharp.Core.vcxproj
+++ b/CefSharp.Core/CefSharp.Core.vcxproj
@@ -217,6 +217,7 @@
+
diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters
index af68469fc..591e0962e 100644
--- a/CefSharp.Core/CefSharp.Core.vcxproj.filters
+++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters
@@ -53,6 +53,9 @@
Source Files
+
+ Source Files
+
diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp
index 9d623e294..1df7d77c0 100644
--- a/CefSharp.Core/Internals/ClientAdapter.cpp
+++ b/CefSharp.Core/Internals/ClientAdapter.cpp
@@ -274,23 +274,13 @@ namespace CefSharp
CefRefPtr ClientAdapter::GetDownloadHandler()
{
- IRequestHandler^ requestHandler = _browserControl->RequestHandler;
- if (requestHandler == nullptr)
- {
- return false;
- }
-
- IDownloadHandler^ downloadHandler;
- bool ret = requestHandler->GetDownloadHandler(_browserControl, downloadHandler);
-
- if (ret)
- {
- return new DownloadAdapter(downloadHandler);
- }
- else
+ IDownloadHandler^ downloadHandler = _browserControl->DownloadHandler;
+ if (downloadHandler == nullptr)
{
return nullptr;
}
+
+ return new DownloadAdapter(downloadHandler);
}
bool ClientAdapter::GetAuthCredentials(CefRefPtr browser, CefRefPtr frame, bool isProxy,
diff --git a/CefSharp.Core/Internals/DownloadAdapter.cpp b/CefSharp.Core/Internals/DownloadAdapter.cpp
new file mode 100644
index 000000000..274aa8010
--- /dev/null
+++ b/CefSharp.Core/Internals/DownloadAdapter.cpp
@@ -0,0 +1,83 @@
+// Copyright © 2010-2014 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.
+
+#pragma once
+
+#include "Stdafx.h"
+#include "include/cef_download_handler.h"
+#include "DownloadAdapter.h"
+
+using namespace System;
+using namespace CefSharp;
+
+namespace CefSharp
+{
+ namespace Internals
+ {
+ DownloadAdapter::~DownloadAdapter()
+ {
+ _handler = nullptr;
+ }
+
+ void DownloadAdapter::OnBeforeDownload(CefRefPtr browser, CefRefPtr download_item,
+ const CefString& suggested_name, CefRefPtr callback)
+ {
+ String^ download_path;
+ bool show_dialog;
+ auto downloadItem = GetDownloadItem(download_item);
+ downloadItem->SuggestedFileName = StringUtils::ToClr(suggested_name);
+
+ if (_handler->OnBeforeDownload(downloadItem, download_path, show_dialog))
+ {
+ callback->Continue(StringUtils::ToNative(download_path), show_dialog);
+ }
+ };
+
+ void DownloadAdapter::OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item,
+ CefRefPtr callback)
+ {
+ if (_handler->OnDownloadUpdated(GetDownloadItem(download_item)))
+ {
+ callback->Cancel();
+ }
+ }
+
+ CefSharp::DownloadItem^ DownloadAdapter::GetDownloadItem(CefRefPtr download_item)
+ {
+ auto item = gcnew CefSharp::DownloadItem();
+ item->IsValid = download_item->IsValid();
+ //NOTE: Description for IsValid says `Do not call any other methods if this function returns false.` so only load if IsValid = true
+ if(item->IsValid)
+ {
+ item->IsInProgress = download_item->IsInProgress();
+ item->IsComplete = download_item->IsComplete();
+ item->IsCancelled = download_item->IsCanceled();
+ item->CurrentSpeed = download_item->GetCurrentSpeed();
+ item->PercentComplete = download_item->GetPercentComplete();
+ item->TotalBytes = download_item->GetTotalBytes();
+ item->ReceivedBytes = download_item->GetReceivedBytes();
+ item->StartTime = ConvertCefTimeToNullableDateTime(download_item->GetStartTime());
+ item->EndTime = ConvertCefTimeToNullableDateTime(download_item->GetEndTime());
+ item->FullPath = StringUtils::ToClr(download_item->GetFullPath());
+ item->Id = download_item->GetId();
+ item->Url = StringUtils::ToClr(download_item->GetURL());
+ item->SuggestedFileName = StringUtils::ToClr(download_item->GetSuggestedFileName());
+ item->ContentDisposition = StringUtils::ToClr(download_item->GetContentDisposition());
+ item->MimeType = StringUtils::ToClr(download_item->GetMimeType());
+ }
+
+ return item;
+ }
+
+ Nullable DownloadAdapter::ConvertCefTimeToNullableDateTime(CefTime time)
+ {
+ auto epoch = time.GetDoubleT();
+ if(epoch == 0)
+ {
+ return Nullable();
+ }
+ return Nullable(DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime());
+ }
+ }
+}
diff --git a/CefSharp.Core/Internals/DownloadAdapter.h b/CefSharp.Core/Internals/DownloadAdapter.h
index cebd153f3..752929a9b 100644
--- a/CefSharp.Core/Internals/DownloadAdapter.h
+++ b/CefSharp.Core/Internals/DownloadAdapter.h
@@ -7,7 +7,8 @@
#include "Stdafx.h"
#include "include/cef_download_handler.h"
-using namespace System::Runtime::InteropServices;
+using namespace System;
+using namespace CefSharp;
namespace CefSharp
{
@@ -15,47 +16,21 @@ namespace CefSharp
{
private class DownloadAdapter : public CefDownloadHandler
{
+ private:
gcroot _handler;
+ DownloadItem^ DownloadAdapter::GetDownloadItem(CefRefPtr download_item);
+ Nullable ConvertCefTimeToNullableDateTime(CefTime time);
public:
- DownloadAdapter(IDownloadHandler^ handler) :
- _handler(handler)
- {
- }
-
- ~DownloadAdapter()
- {
- _handler = nullptr;
- }
+ DownloadAdapter(IDownloadHandler^ handler) : _handler(handler) { }
+ ~DownloadAdapter();
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;
-
- if (_handler->OnBeforeDownload(StringUtils::ToClr(suggested_name), download_path, show_dialog))
- {
- callback->Continue(StringUtils::ToNative(download_path), show_dialog);
- }
- };
-
- virtual bool ReceivedData(void* data, int data_size)
- {
- array^ bytes = gcnew array(data_size);
- Marshal::Copy(IntPtr(data), bytes, 0, data_size);
-
- return _handler->ReceivedData(bytes);
- }
-
- virtual void Complete()
- {
- _handler->Complete();
- }
-
+ const CefString& suggested_name, CefRefPtr callback) OVERRIDE;
+ virtual void OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item,
+ CefRefPtr callback) OVERRIDE;
+
IMPLEMENT_REFCOUNTING(DownloadAdapter);
};
}
-}
+}
\ No newline at end of file
diff --git a/CefSharp.Example/DownloadHandler.cs b/CefSharp.Example/DownloadHandler.cs
index 0cbad1821..2ab419824 100644
--- a/CefSharp.Example/DownloadHandler.cs
+++ b/CefSharp.Example/DownloadHandler.cs
@@ -1,23 +1,18 @@
namespace CefSharp.Example
{
- internal class DownloadHandler : IDownloadHandler
+ public class DownloadHandler : IDownloadHandler
{
- bool IDownloadHandler.OnBeforeDownload(string suggestedName, out string downloadPath, out bool showDialog)
+ public bool OnBeforeDownload(DownloadItem downloadItem, out string downloadPath, out bool showDialog)
{
- downloadPath = suggestedName;
+ downloadPath = downloadItem.SuggestedFileName;
showDialog = true;
return true;
}
- bool IDownloadHandler.ReceivedData(byte[] data)
+ public bool OnDownloadUpdated(DownloadItem downloadItem)
{
return false;
}
-
- void IDownloadHandler.Complete()
- {
-
- }
}
}
diff --git a/CefSharp.Example/RequestHandler.cs b/CefSharp.Example/RequestHandler.cs
index fd1b8b4af..2ada2e881 100644
--- a/CefSharp.Example/RequestHandler.cs
+++ b/CefSharp.Example/RequestHandler.cs
@@ -34,13 +34,6 @@ namespace CefSharp.Example
return false;
}
- bool IRequestHandler.GetDownloadHandler(IWebBrowser browser, out IDownloadHandler handler)
- {
- handler = new DownloadHandler();
-
- return true;
- }
-
bool IRequestHandler.GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password)
{
return false;
diff --git a/CefSharp.WinForms.Example/BrowserForm.cs b/CefSharp.WinForms.Example/BrowserForm.cs
index f8ff1d6cb..5b74bf3a9 100644
--- a/CefSharp.WinForms.Example/BrowserForm.cs
+++ b/CefSharp.WinForms.Example/BrowserForm.cs
@@ -25,6 +25,7 @@ namespace CefSharp.WinForms.Example
};
toolStripContainer.ContentPanel.Controls.Add(browser);
+ browser.DownloadHandler = new DownloadHandler();
browser.MenuHandler = new MenuHandler();
browser.NavStateChanged += OnBrowserNavStateChanged;
browser.ConsoleMessage += OnBrowserConsoleMessage;
diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs
index 7cb4dbf41..f84d0a8a6 100644
--- a/CefSharp.WinForms/ChromiumWebBrowser.cs
+++ b/CefSharp.WinForms/ChromiumWebBrowser.cs
@@ -25,6 +25,7 @@ namespace CefSharp.WinForms
public IJsDialogHandler JsDialogHandler { get; set; }
public IKeyboardHandler KeyboardHandler { get; set; }
public IRequestHandler RequestHandler { get; set; }
+ public IDownloadHandler DownloadHandler { get; set; }
public ILifeSpanHandler LifeSpanHandler { get; set; }
public IMenuHandler MenuHandler { get; set; }
diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs
index cffee2dfa..5e3ba5a17 100644
--- a/CefSharp.Wpf/ChromiumWebBrowser.cs
+++ b/CefSharp.Wpf/ChromiumWebBrowser.cs
@@ -47,6 +47,7 @@ namespace CefSharp.Wpf
public IJsDialogHandler JsDialogHandler { get; set; }
public IKeyboardHandler KeyboardHandler { get; set; }
public IRequestHandler RequestHandler { get; set; }
+ public IDownloadHandler DownloadHandler { get; set; }
public ILifeSpanHandler LifeSpanHandler { get; set; }
public event ConsoleMessageEventHandler ConsoleMessage;
diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj
index f9b9991cf..4280a28e1 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..13cb97dc8
--- /dev/null
+++ b/CefSharp/DownloadItem.cs
@@ -0,0 +1,87 @@
+using System;
+
+namespace CefSharp
+{
+ public class DownloadItem
+ {
+ ///
+ /// Returns true if this object is valid. Do not call any other methods if this function returns false.
+ ///
+ public bool IsValid { get; set; }
+
+ ///
+ /// Returns true if the download is in progress.
+ ///
+ public bool IsInProgress { get; set; }
+
+ ///
+ /// Returns true if the download is complete.
+ ///
+ public bool IsComplete { get; set; }
+
+ ///
+ /// Returns true if the download has been canceled or interrupted.
+ ///
+ public bool IsCancelled { get; set; }
+
+ ///
+ /// Returns a simple speed estimate in bytes/s.
+ ///
+ public Int64 CurrentSpeed { get; set; }
+
+ ///
+ /// Returns the rough percent complete or -1 if the receive total size is unknown.
+ ///
+ public int PercentComplete { get; set; }
+
+ ///
+ /// Returns the total number of bytes.
+ ///
+ public Int64 TotalBytes { get; set; }
+
+ ///
+ /// Returns the number of received bytes.
+ ///
+ public Int64 ReceivedBytes { get; set; }
+
+ ///
+ /// Returns the time that the download started
+ ///
+ public DateTime? StartTime { get; set; }
+
+ ///
+ /// Returns the time that the download ended
+ ///
+ public DateTime? EndTime { get; set; }
+
+ ///
+ /// Returns the full path to the downloaded or downloading file.
+ ///
+ public string FullPath { get; set; }
+
+ ///
+ /// Returns the unique identifier for this download.
+ ///
+ public int Id { get; set; }
+
+ ///
+ /// Returns the URL.
+ ///
+ public string Url { get; set; }
+
+ ///
+ /// Returns the suggested file name.
+ ///
+ public string SuggestedFileName { get; set; }
+
+ ///
+ /// Returns the content disposition.
+ ///
+ public string ContentDisposition { get; set; }
+
+ ///
+ /// Returns the mime type.
+ ///
+ public string MimeType { get; set; }
+ }
+}
diff --git a/CefSharp/IDownloadHandler.cs b/CefSharp/IDownloadHandler.cs
index 79bdd55bd..004d68842 100644
--- a/CefSharp/IDownloadHandler.cs
+++ b/CefSharp/IDownloadHandler.cs
@@ -2,8 +2,20 @@
{
public interface IDownloadHandler
{
- bool OnBeforeDownload(string suggestedName, out string downloadPath, out bool showDialog);
- bool ReceivedData(byte[] data);
- void Complete();
+ ///
+ /// Called before a download begins.
+ ///
+ /// Represents the file being downloaded.
+ /// Path where the file will be saved if is False.
+ /// Display a dialog allowing the user to specify a custom path and filename.
+ /// Return True to continue the download otherwise return False to cancel the download
+ bool OnBeforeDownload(DownloadItem downloadItem, out string downloadPath, out bool showDialog);
+
+ ///
+ /// Called when a download's status or progress information has been updated. This may be called multiple times before and after .
+ ///
+ /// Represents the file being downloaded.
+ /// Return True to cancel, otherwise False to allow the download to continue.
+ bool OnDownloadUpdated(DownloadItem downloadItem);
}
}
diff --git a/CefSharp/IRequestHandler.cs b/CefSharp/IRequestHandler.cs
index 7c49f0de0..add1dbf1b 100644
--- a/CefSharp/IRequestHandler.cs
+++ b/CefSharp/IRequestHandler.cs
@@ -32,14 +32,6 @@
// TODO: Investigate how we can support in CEF3.
//void OnResourceResponse(IWebBrowser browser, string url, int status, string statusText, string mimeType, WebHeaderCollection headers);
- ///
- /// Called when a server indicates via the 'Content-Disposition' header that a response represents a file to download.
- ///
- /// the browser object
- /// Set instance of IDownloadHandler that will recieve the file contents
- /// Return true to download the file or false to cancel the file download
- bool GetDownloadHandler(IWebBrowser browser, out IDownloadHandler handler);
-
///
/// Called when the browser needs credentials from the user.
///
diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs
index 43678f84c..a6b8183d0 100644
--- a/CefSharp/IWebBrowser.cs
+++ b/CefSharp/IWebBrowser.cs
@@ -69,7 +69,7 @@ namespace CefSharp
ILifeSpanHandler LifeSpanHandler { get; set; }
IKeyboardHandler KeyboardHandler { get; set; }
IJsDialogHandler JsDialogHandler { get; set; }
-
+ IDownloadHandler DownloadHandler { get; set; }
bool IsBrowserInitialized { get; }
///