diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index 2aa65ddc2..6b349028c 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -262,6 +262,7 @@ + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index a30b3497c..c56177158 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -247,6 +247,9 @@ Header Files + + Header Files + diff --git a/CefSharp.Core/Internals/CefResponseFilterAdapter.h b/CefSharp.Core/Internals/CefResponseFilterAdapter.h new file mode 100644 index 000000000..ab6a0874b --- /dev/null +++ b/CefSharp.Core/Internals/CefResponseFilterAdapter.h @@ -0,0 +1,75 @@ +// Copyright © 2010-2015 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_response_filter.h" + +using namespace System::IO; + +namespace CefSharp +{ + namespace Internals + { + public class CefResponseFilterAdapter : public CefResponseFilter + { + private: + gcroot _filter; + + public: + CefResponseFilterAdapter(IResponseFilter^ filter) : + _filter(filter) + { + + } + + ~CefResponseFilterAdapter() + { + _filter = nullptr; + } + + virtual bool InitFilter() OVERRIDE + { + return _filter->InitFilter(); + } + + // Called to filter a chunk of data. |data_in| is the input buffer containing + // |data_in_size| bytes of pre-filter data (|data_in| will be NULL if + // |data_in_size| is zero). |data_out| is the output buffer that can accept up + // to |data_out_size| bytes of filtered output data. Set |data_in_read| to the + // number of bytes that were read from |data_in|. Set |data_out_written| to + // the number of bytes that were written into |data_out|. If some or all of + // the pre-filter data was read successfully but more data is needed in order + // to continue filtering (filtered output is pending) return + // RESPONSE_FILTER_NEED_MORE_DATA. If some or all of the pre-filter data was + // read successfully and all available filtered output has been written return + // RESPONSE_FILTER_DONE. If an error occurs during filtering return + // RESPONSE_FILTER_ERROR. This method will be called repeatedly until there is + // no more data to filter (resource response is complete), |data_in_read| + // matches |data_in_size| (all available pre-filter bytes have been read), and + // the method returns RESPONSE_FILTER_DONE or RESPONSE_FILTER_ERROR. Do not + // keep a reference to the buffers passed to this method. + /*--cef(optional_param=data_in,default_retval=RESPONSE_FILTER_ERROR)--*/ + virtual FilterStatus Filter(void* dataIn, size_t dataInSize, size_t& dataInRead, void* dataOut, size_t dataOutSize, size_t& dataOutWritten) OVERRIDE + { + UnmanagedMemoryStream readStream((Byte*)dataIn, (Int64)dataInSize, (Int64)dataInSize, FileAccess::Read); + UnmanagedMemoryStream writeStream((Byte*)dataOut, (Int64)dataOutSize, (Int64)dataOutSize, FileAccess::Write); + + Int64 dataInReadPtr = 0; + Int64 dataOutWrittenPtr = 0; + + auto status = _filter->Filter(%readStream, (Int64)dataInSize, dataInReadPtr, %writeStream, (Int64)dataOutSize, dataOutWrittenPtr); + + dataInRead = dataInReadPtr; + dataOutWritten = dataOutWrittenPtr; + + return (FilterStatus)status; + } + + IMPLEMENT_REFCOUNTING(CefResponseFilterAdapter); + }; + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index f90b29e86..c26c7fd27 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -30,6 +30,7 @@ #include "Serialization\JsObjectsSerialization.h" #include "Serialization\ObjectsSerialization.h" #include "Messaging\Messages.h" +#include "CefResponseFilterAdapter.h" using namespace CefSharp::Internals::Messaging; using namespace CefSharp::Internals::Serialization; @@ -618,6 +619,30 @@ namespace CefSharp return handler->OnResourceResponse(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, %responseWrapper); } + CefRefPtr ClientAdapter::GetResourceResponseFilter(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr response) + { + auto handler = _browserControl->RequestHandler; + + if (handler == nullptr) + { + return NULL; + } + + auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());; + CefFrameWrapper frameWrapper(frame); + CefRequestWrapper requestWrapper(request); + CefResponseWrapper responseWrapper(response); + + auto filter = handler->GetResourceResponseFilter(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, %responseWrapper); + + if (filter == nullptr) + { + return NULL; + } + + return new CefResponseFilterAdapter(filter); + } + void ClientAdapter::OnResourceLoadComplete(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr response, URLRequestStatus status, int64 receivedContentLength) { auto handler = _browserControl->RequestHandler; diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index 8eb54367d..158625fae 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -126,6 +126,7 @@ namespace CefSharp virtual DECL bool OnQuotaRequest(CefRefPtr browser, const CefString& originUrl, int64 newSize, CefRefPtr callback) OVERRIDE; virtual DECL void OnResourceRedirect(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefString& newUrl) OVERRIDE; virtual DECL bool OnResourceResponse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr response) OVERRIDE; + virtual DECL CefRefPtr GetResourceResponseFilter(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr response) OVERRIDE; virtual DECL void OnResourceLoadComplete(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr response, URLRequestStatus status, int64 receivedContentLength) OVERRIDE; virtual DECL void OnProtocolExecution(CefRefPtr browser, const CefString& url, bool& allowOSExecution) OVERRIDE; virtual DECL void OnPluginCrashed(CefRefPtr browser, const CefString& plugin_path) OVERRIDE; diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index 05e27d487..60944ced3 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -74,6 +74,7 @@ + diff --git a/CefSharp.Example/RequestHandler.cs b/CefSharp.Example/RequestHandler.cs index 6677aca62..6b1a9ad1e 100644 --- a/CefSharp.Example/RequestHandler.cs +++ b/CefSharp.Example/RequestHandler.cs @@ -178,6 +178,17 @@ namespace CefSharp.Example return false; } + IResponseFilter IRequestHandler.GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) + { + var url = new Uri(request.Url); + if (url.Scheme == CefSharpSchemeHandlerFactory.SchemeName) + { + return new ResponseFilter(); + } + + return null; + } + void IRequestHandler.OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { diff --git a/CefSharp.Example/ResponseFilter.cs b/CefSharp.Example/ResponseFilter.cs new file mode 100644 index 000000000..153545c71 --- /dev/null +++ b/CefSharp.Example/ResponseFilter.cs @@ -0,0 +1,27 @@ +// Copyright © 2010-2015 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. + +using System; +using System.IO; + +namespace CefSharp.Example +{ + public class ResponseFilter : IResponseFilter + { + bool IResponseFilter.InitFilter() + { + return true; + } + + FilterStatus IResponseFilter.Filter(Stream dataIn, long dataInSize, out long dataInRead, Stream dataOut, long dataOutSize, out long dataOutWritten) + { + dataInRead = dataInSize; + dataOutWritten = Math.Min(dataInRead, dataOutSize); + + dataIn.CopyTo(dataOut); + + return FilterStatus.Done; + } + } +} diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index a78e12a37..c89c2b524 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -79,12 +79,14 @@ + + diff --git a/CefSharp/FilterStatus.cs b/CefSharp/FilterStatus.cs new file mode 100644 index 000000000..77367686c --- /dev/null +++ b/CefSharp/FilterStatus.cs @@ -0,0 +1,28 @@ +// Copyright © 2010-2015 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. + +namespace CefSharp +{ + /// + /// Return values for IResponseFilter + /// + public enum FilterStatus + { + /// + /// Some or all of the pre-filter data was read successfully but more data is + /// needed in order to continue filtering (filtered output is pending). + /// + NeedMoreData, + + /// + /// Some or all of the pre-filter data was read successfully and all available filtered output has been written. + /// + Done, + + /// + /// An error occurred during filtering. + /// + Error + } +} diff --git a/CefSharp/IRequestHandler.cs b/CefSharp/IRequestHandler.cs index a6f022659..6f59f2956 100644 --- a/CefSharp/IRequestHandler.cs +++ b/CefSharp/IRequestHandler.cs @@ -171,6 +171,17 @@ namespace CefSharp /// bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response); + /// + /// Called on the CEF IO thread to optionally filter resource response content. + /// + /// The ChromiumWebBrowser control + /// the browser object + /// The frame that is being redirected. + /// the request object - cannot be modified in this callback + /// the response object - cannot be modified in this callback + /// Return an IResponseFilter to intercept this response, otherwise return null + IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response); + /// /// Called on the CEF IO thread when a resource load has completed. /// diff --git a/CefSharp/IResponseFilter.cs b/CefSharp/IResponseFilter.cs new file mode 100644 index 000000000..2c7947185 --- /dev/null +++ b/CefSharp/IResponseFilter.cs @@ -0,0 +1,46 @@ +// Copyright © 2010-2015 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. + +using System.IO; + +namespace CefSharp +{ + public interface IResponseFilter + { + /// + /// Initialize the response filter. Will only be called a single time. + /// The filter will not be installed if this method returns false. + /// + /// The filter will not be installed if this method returns false. + bool InitFilter(); + + /// + /// Called to filter a chunk of data. |data_in| is the input buffer containing + /// |data_in_size| bytes of pre-filter data (|data_in| will be NULL if + /// |data_in_size| is zero). |data_out| is the output buffer that can accept up + /// to |data_out_size| bytes of filtered output data. Set |data_in_read| to the + /// number of bytes that were read from |data_in|. Set |data_out_written| to + /// the number of bytes that were written into |data_out|. If some or all of + /// the pre-filter data was read successfully but more data is needed in order + /// to continue filtering (filtered output is pending) return + /// RESPONSE_FILTER_NEED_MORE_DATA. If some or all of the pre-filter data was + /// read successfully and all available filtered output has been written return + /// RESPONSE_FILTER_DONE. If an error occurs during filtering return + /// RESPONSE_FILTER_ERROR. This method will be called repeatedly until there is + /// no more data to filter (resource response is complete), |data_in_read| + /// matches |data_in_size| (all available pre-filter bytes have been read), and + /// the method returns RESPONSE_FILTER_DONE or RESPONSE_FILTER_ERROR. Do not + /// keep a reference to the buffers passed to this method. + /// optional_param=data_in,default_retval=RESPONSE_FILTER_ERROR + /// + /// + /// + /// + /// + /// + /// + /// + FilterStatus Filter(Stream dataIn, long dataInSize, out long dataInRead, Stream dataOut, long dataOutSize, out long dataOutWritten); + } +}