Skip to content

Commit

Permalink
Merge branch 'download'
Browse files Browse the repository at this point in the history
  • Loading branch information
ataranto committed Mar 7, 2013
2 parents 9fae725 + 8fd4c65 commit 58f6a90
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CefSharp.Example/CefSharp.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BoundObject.cs" />
<Compile Include="DownloadHandler.cs" />
<Compile Include="ExamplePresenter.cs" />
<Compile Include="IExampleView.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
34 changes: 34 additions & 0 deletions CefSharp.Example/DownloadHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CefSharp.Example
{
class DownloadHandler : IDownloadHandler
{
private readonly string _path;
private Stream _stream;

public DownloadHandler(string fileName)
{
_path = Path.Combine(Path.GetTempPath(), fileName);
_stream = File.Create(_path);
}

public bool ReceivedData(byte[] data)
{
_stream.Write(data, 0, data.GetLength(0));
return true;

}
public void Complete()
{
_stream.Dispose();
_stream = null;

Console.WriteLine("Downloaded: {0}", _path);
}
}
}
7 changes: 7 additions & 0 deletions CefSharp.Example/ExamplePresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ void IRequestHandler.OnResourceResponse(IWebBrowser browser, string url, int sta
{

}

bool IRequestHandler.GetDownloadHandler(IWebBrowser browser, string mimeType, string fileName, long contentLength, ref IDownloadHandler handler)
{
handler = new DownloadHandler(fileName);
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;
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.Example/SchemeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ISchemeHandler Create()
}
}

class SchemeHandler: ISchemeHandler
class SchemeHandler : ISchemeHandler
{
private readonly IDictionary<string, string> resources;

Expand Down
12 changes: 12 additions & 0 deletions CefSharp/CefSharp.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@
RelativePath=".\CookieVisitor.cpp"
>
</File>
<File
RelativePath=".\DownloadAdapter.cpp"
>
</File>
<File
RelativePath=".\RenderClientAdapter.cpp"
>
Expand Down Expand Up @@ -298,10 +302,18 @@
RelativePath=".\CookieVisitor.h"
>
</File>
<File
RelativePath=".\DownloadAdapter.h"
>
</File>
<File
RelativePath=".\ICookieVisitor.h"
>
</File>
<File
RelativePath=".\IDownloadHandler.h"
>
</File>
<File
RelativePath=".\IKeyboardHandler.h"
>
Expand Down
19 changes: 19 additions & 0 deletions CefSharp/ClientAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ClientAdapter.h"
#include "CefSharp.h"
#include "StreamAdapter.h"
#include "DownloadAdapter.h"
#include "IWebBrowser.h"
#include "ILifeSpanHandler.h"
#include "ILoadHandler.h"
Expand Down Expand Up @@ -215,6 +216,24 @@ namespace CefSharp
return ret;
}

bool ClientAdapter::GetDownloadHandler(CefRefPtr<CefBrowser> browser, const CefString& mimeType, const CefString& fileName, int64 contentLength, CefRefPtr<CefDownloadHandler>& handler)
{
IRequestHandler^ requestHandler = _browserControl->RequestHandler;
if (requestHandler == nullptr)
{
return false;
}

IDownloadHandler^ downloadHandler;
bool ret = requestHandler->GetDownloadHandler(_browserControl, toClr(mimeType), toClr(fileName), contentLength, downloadHandler);
if (ret)
{
handler = new DownloadAdapter(downloadHandler);
}

return ret;
}

bool ClientAdapter::GetAuthCredentials(CefRefPtr<CefBrowser> browser, bool isProxy, const CefString& host, int port, const CefString& realm, const CefString& scheme, CefString& username, CefString& password)
{
IRequestHandler^ handler = _browserControl->RequestHandler;
Expand Down
2 changes: 2 additions & 0 deletions CefSharp/ClientAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace CefSharp
virtual DECL bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, NavType navType, bool isRedirect) OVERRIDE;
virtual DECL bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, CefRefPtr<CefRequest> request, CefString& redirectUrl, CefRefPtr<CefStreamReader>& resourceStream, CefRefPtr<CefResponse> response, int loadFlags) OVERRIDE;
virtual DECL void OnResourceResponse(CefRefPtr<CefBrowser> browser, const CefString& url, CefRefPtr<CefResponse> response, CefRefPtr<CefContentFilter>& filter) OVERRIDE;
virtual DECL bool GetDownloadHandler(CefRefPtr<CefBrowser> browser, const CefString& mimeType, const CefString& fileName, int64 contentLength, CefRefPtr<CefDownloadHandler>& handler) OVERRIDE;

virtual DECL bool GetAuthCredentials(CefRefPtr<CefBrowser> browser, bool isProxy, const CefString& host, int port, const CefString& realm, const CefString& scheme, CefString& username, CefString& password) OVERRIDE;

// CefDisplayHandler
Expand Down
20 changes: 20 additions & 0 deletions CefSharp/DownloadAdapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "stdafx.h"
#include "DownloadAdapter.h"

using namespace System::Runtime::InteropServices;

namespace CefSharp
{
bool DownloadAdapter::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);
}

void DownloadAdapter::Complete()
{
_handler->Complete();
}
}
21 changes: 21 additions & 0 deletions CefSharp/DownloadAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "stdafx.h"
#pragma once

#include "include/cef_download_handler.h"
#include "IDownloadHandler.h"

namespace CefSharp
{
class DownloadAdapter : public CefDownloadHandler
{
gcroot<IDownloadHandler^> _handler;

public:
DownloadAdapter(IDownloadHandler^ handler) : _handler(handler) { }

virtual bool ReceivedData(void* data, int data_size);
virtual void Complete();

IMPLEMENT_REFCOUNTING(DownloadAdapter);
};
}
12 changes: 12 additions & 0 deletions CefSharp/IDownloadHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "stdafx.h"
#pragma once

namespace CefSharp
{
public interface class IDownloadHandler
{
public:
bool ReceivedData(array<Byte>^ data);
void Complete();
};
}
3 changes: 2 additions & 1 deletion CefSharp/IRequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace CefSharp
bool OnBeforeBrowse(IWebBrowser^ browser, IRequest^ request, NavigationType naigationvType, bool isRedirect);
bool OnBeforeResourceLoad(IWebBrowser^ browser, IRequestResponse^ requestResponse);
void OnResourceResponse(IWebBrowser^ browser, String^ url, int status, String^ statusText, String^ mimeType, WebHeaderCollection^ headers);
bool GetAuthCredentials(IWebBrowser^ browser, bool isProxy, String^ host,int port, String^ realm, String^ scheme,String^% username, String^% password);
bool GetDownloadHandler(IWebBrowser^ browser, String^ mimeType, String^ fileName, Int64 contentLength, IDownloadHandler ^% handler);
bool GetAuthCredentials(IWebBrowser^ browser, bool isProxy, String^ host ,int port, String^ realm, String^ scheme, String^% username, String^% password);
};
}

0 comments on commit 58f6a90

Please sign in to comment.