mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 11:42:36 +00:00
Merge pull request #391 from amaitland/feature/DownloadHandler
Implement DownloadHandler based on current Cef API fixes #387
This commit is contained in:
@@ -217,6 +217,7 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="AssemblyInfo.cpp" />
|
||||
<ClCompile Include="CefAppWrapper.cpp" />
|
||||
<ClCompile Include="Internals\DownloadAdapter.cpp" />
|
||||
<ClCompile Include="Internals\CefWebPluginInfoWrapper.cpp" />
|
||||
<ClCompile Include="Internals\ClientAdapter.cpp" />
|
||||
<ClCompile Include="Internals\CookieVisitor.cpp" />
|
||||
|
||||
@@ -53,6 +53,9 @@
|
||||
<ClCompile Include="Internals\StringVisitor.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Internals\DownloadAdapter.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="vcclr_local.h">
|
||||
|
||||
@@ -274,23 +274,13 @@ namespace CefSharp
|
||||
|
||||
CefRefPtr<CefDownloadHandler> 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<CefBrowser> browser, CefRefPtr<CefFrame> frame, bool isProxy,
|
||||
|
||||
@@ -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<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item,
|
||||
const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> 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<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item,
|
||||
CefRefPtr<CefDownloadItemCallback> callback)
|
||||
{
|
||||
if (_handler->OnDownloadUpdated(GetDownloadItem(download_item)))
|
||||
{
|
||||
callback->Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
CefSharp::DownloadItem^ DownloadAdapter::GetDownloadItem(CefRefPtr<CefDownloadItem> 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<DateTime> DownloadAdapter::ConvertCefTimeToNullableDateTime(CefTime time)
|
||||
{
|
||||
auto epoch = time.GetDoubleT();
|
||||
if(epoch == 0)
|
||||
{
|
||||
return Nullable<DateTime>();
|
||||
}
|
||||
return Nullable<DateTime>(DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<IDownloadHandler^> _handler;
|
||||
DownloadItem^ DownloadAdapter::GetDownloadItem(CefRefPtr<CefDownloadItem> download_item);
|
||||
Nullable<DateTime> ConvertCefTimeToNullableDateTime(CefTime time);
|
||||
|
||||
public:
|
||||
DownloadAdapter(IDownloadHandler^ handler) :
|
||||
_handler(handler)
|
||||
{
|
||||
}
|
||||
|
||||
~DownloadAdapter()
|
||||
{
|
||||
_handler = nullptr;
|
||||
}
|
||||
DownloadAdapter(IDownloadHandler^ handler) : _handler(handler) { }
|
||||
~DownloadAdapter();
|
||||
|
||||
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;
|
||||
|
||||
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<Byte>^ bytes = gcnew array<Byte>(data_size);
|
||||
Marshal::Copy(IntPtr(data), bytes, 0, data_size);
|
||||
|
||||
return _handler->ReceivedData(bytes);
|
||||
}
|
||||
|
||||
virtual void Complete()
|
||||
{
|
||||
_handler->Complete();
|
||||
}
|
||||
|
||||
const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> callback) OVERRIDE;
|
||||
virtual void OnDownloadUpdated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item,
|
||||
CefRefPtr<CefDownloadItemCallback> callback) OVERRIDE;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(DownloadAdapter);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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; }
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -89,6 +89,7 @@
|
||||
<Compile Include="CefFileDialogMode.cs" />
|
||||
<Compile Include="IDialogHandler.cs" />
|
||||
<Compile Include="AddressChangedEventArgs.cs" />
|
||||
<Compile Include="DownloadItem.cs" />
|
||||
<Compile Include="CefEventFlags.cs" />
|
||||
<Compile Include="IStringVisitor.cs" />
|
||||
<Compile Include="LoadErrorEventArgs.cs" />
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
public class DownloadItem
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns true if this object is valid. Do not call any other methods if this function returns false.
|
||||
/// </summary>
|
||||
public bool IsValid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the download is in progress.
|
||||
/// </summary>
|
||||
public bool IsInProgress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the download is complete.
|
||||
/// </summary>
|
||||
public bool IsComplete { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the download has been canceled or interrupted.
|
||||
/// </summary>
|
||||
public bool IsCancelled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a simple speed estimate in bytes/s.
|
||||
/// </summary>
|
||||
public Int64 CurrentSpeed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the rough percent complete or -1 if the receive total size is unknown.
|
||||
/// </summary>
|
||||
public int PercentComplete { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the total number of bytes.
|
||||
/// </summary>
|
||||
public Int64 TotalBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of received bytes.
|
||||
/// </summary>
|
||||
public Int64 ReceivedBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the time that the download started
|
||||
/// </summary>
|
||||
public DateTime? StartTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the time that the download ended
|
||||
/// </summary>
|
||||
public DateTime? EndTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the full path to the downloaded or downloading file.
|
||||
/// </summary>
|
||||
public string FullPath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the unique identifier for this download.
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the URL.
|
||||
/// </summary>
|
||||
public string Url { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the suggested file name.
|
||||
/// </summary>
|
||||
public string SuggestedFileName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the content disposition.
|
||||
/// </summary>
|
||||
public string ContentDisposition { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the mime type.
|
||||
/// </summary>
|
||||
public string MimeType { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,20 @@
|
||||
{
|
||||
public interface IDownloadHandler
|
||||
{
|
||||
bool OnBeforeDownload(string suggestedName, out string downloadPath, out bool showDialog);
|
||||
bool ReceivedData(byte[] data);
|
||||
void Complete();
|
||||
/// <summary>
|
||||
/// Called before a download begins.
|
||||
/// </summary>
|
||||
/// <param name="downloadItem">Represents the file being downloaded.</param>
|
||||
/// <param name="downloadPath">Path where the file will be saved if <see cref="showDialog"/> is False.</param>
|
||||
/// <param name="showDialog">Display a dialog allowing the user to specify a custom path and filename.</param>
|
||||
/// <returns>Return True to continue the download otherwise return False to cancel the download</returns>
|
||||
bool OnBeforeDownload(DownloadItem downloadItem, out string downloadPath, out bool showDialog);
|
||||
|
||||
/// <summary>
|
||||
/// Called when a download's status or progress information has been updated. This may be called multiple times before and after <see cref="OnBeforeDownload"/>.
|
||||
/// </summary>
|
||||
/// <param name="downloadItem">Represents the file being downloaded.</param>
|
||||
/// <returns>Return True to cancel, otherwise False to allow the download to continue.</returns>
|
||||
bool OnDownloadUpdated(DownloadItem downloadItem);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// Called when a server indicates via the 'Content-Disposition' header that a response represents a file to download.
|
||||
/// </summary>
|
||||
/// <param name="browser">the browser object</param>
|
||||
/// <param name="handler">Set instance of IDownloadHandler that will recieve the file contents</param>
|
||||
/// <returns>Return true to download the file or false to cancel the file download</returns>
|
||||
bool GetDownloadHandler(IWebBrowser browser, out IDownloadHandler handler);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the browser needs credentials from the user.
|
||||
/// </summary>
|
||||
|
||||
@@ -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; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user