diff --git a/CefSharp.Example/Browser.cs b/CefSharp.Example/Browser.cs
index 4453cf78a..f52f0d822 100644
--- a/CefSharp.Example/Browser.cs
+++ b/CefSharp.Example/Browser.cs
@@ -95,16 +95,16 @@ namespace CefSharp.Example
}
}
- public ReturnValue HandleBeforeResourceLoad(CefWebBrowser browserControl, IRequest request, ref string redirectUrl, ref Stream resourceStream, ref string mimeType, int loadFlags)
- {
+ public void HandleBeforeResourceLoad(CefWebBrowser browserControl, IRequestResponse requestResponse)
+ {
+ IRequest request = requestResponse.Request;
if(request.Url.StartsWith("http://test/resource/load"))
{
- resourceStream = new MemoryStream(Encoding.UTF8.GetBytes(
+ Stream resourceStream = new MemoryStream(Encoding.UTF8.GetBytes(
"
Success
This document is loaded from a System.IO.Stream
"));
- mimeType = "text/html";
+ requestResponse.RespondWith(resourceStream, "text/html");
}
Console.WriteLine(request.Url);
- return ReturnValue.Continue;
}
private void ExitToolStripMenuItemClick(object sender, EventArgs e)
diff --git a/CefSharp/CefSharp.vcproj b/CefSharp/CefSharp.vcproj
index 75d906b6a..d265a8a9f 100644
--- a/CefSharp/CefSharp.vcproj
+++ b/CefSharp/CefSharp.vcproj
@@ -232,6 +232,10 @@
RelativePath=".\Request.cpp"
>
+
+
@@ -314,6 +318,10 @@
RelativePath=".\Request.h"
>
+
+
diff --git a/CefSharp/HandlerAdapter.cpp b/CefSharp/HandlerAdapter.cpp
index 8f0777d2d..a43656bb0 100644
--- a/CefSharp/HandlerAdapter.cpp
+++ b/CefSharp/HandlerAdapter.cpp
@@ -79,21 +79,25 @@ namespace CefSharp
IBeforeResourceLoad^ handler = _browserControl->BeforeResourceLoadHandler;
if(handler != nullptr)
{
- String^ mRedirectUrl = nullptr;
- Stream^ mResourceStream = nullptr;
- String^ mMimeType = nullptr;
CefRequestWrapper^ wrapper = gcnew CefRequestWrapper(request);
+ RequestResponse^ requestResponse = gcnew RequestResponse(wrapper);
- handler->HandleBeforeResourceLoad(_browserControl, wrapper, mRedirectUrl,
- mResourceStream, mMimeType, loadFlags);
+ handler->HandleBeforeResourceLoad(_browserControl, requestResponse);
- if(mResourceStream != nullptr)
+ if(requestResponse->Action == ResponseAction::Respond)
{
- CefRefPtr adapter = new StreamAdapter(mResourceStream);
+ CefRefPtr adapter = new StreamAdapter(requestResponse->ResponseStream);
resourceStream = CefStreamReader::CreateForHandler(static_cast>(adapter));
- pin_ptr pStr = PtrToStringChars(mMimeType);
- CefString str(pStr);
- mimeType = str;
+ mimeType = convertFromString(requestResponse->MimeType);
+ return RV_CONTINUE;
+ }
+ else if(requestResponse->Action == ResponseAction::Cancel)
+ {
+ return RV_HANDLED;
+ }
+ else if(requestResponse->Action == ResponseAction::Redirect)
+ {
+ redirectUrl = convertFromString(requestResponse->RedirectUrl);
}
}
return RV_CONTINUE;
diff --git a/CefSharp/IBeforeResourceLoad.h b/CefSharp/IBeforeResourceLoad.h
index 8fffb405d..44f9a3937 100644
--- a/CefSharp/IBeforeResourceLoad.h
+++ b/CefSharp/IBeforeResourceLoad.h
@@ -3,7 +3,7 @@
#include "CefWebBrowser.h"
#include "Request.h"
-#include "ReturnValue.h"
+#include "RequestResponse.h"
using namespace System;
using namespace System::IO;
@@ -14,21 +14,8 @@ namespace CefSharp
{
public:
- /// Event called before a resource is loaded.
- ///
- /// To allow the resource to load normally return ReturnValue.Continue.
- ///
- /// To redirect the resource to a new url populate the redirectUrl value
- /// and return ReturnValue.Continue.
- ///
- /// To specify data for the resource return a Stream object in resourceStream,
- /// set mimeType to the resource stream's mime type, and return ReturnValue.Continue.
- ///
- /// To cancel loading of the resource return ReturnValue.Handled.
- ///
- /// Any modifications to request will be observed. If the URL in request is changed and
- /// redirectUrl is also set, the URL in request will be used. */
- ReturnValue HandleBeforeResourceLoad(CefWebBrowser^ browserControl, IRequest^ request,
- String^% redirectUrl, Stream^% resourceStream, String^% mimeType, int loadFlags);
+ /// called before all resource loads.
+ /// allows the Request to be manipulated or an alternate response to be provided
+ void HandleBeforeResourceLoad(CefWebBrowser^ browserControl, IRequestResponse^ requestResponse);
};
}
\ No newline at end of file
diff --git a/CefSharp/RequestResponse.cpp b/CefSharp/RequestResponse.cpp
new file mode 100644
index 000000000..3bd278332
--- /dev/null
+++ b/CefSharp/RequestResponse.cpp
@@ -0,0 +1,34 @@
+#include "stdafx.h"
+#include "RequestResponse.h"
+
+namespace CefSharp
+{
+ void RequestResponse::Cancel()
+ {
+ _action = ResponseAction::Cancel;
+ }
+
+ void RequestResponse::Redirect(String^ url)
+ {
+ _redirectUrl = url;
+ _action = ResponseAction::Redirect;
+ }
+
+ void RequestResponse::RespondWith(Stream^ stream, String^ mimeType)
+ {
+ if(String::IsNullOrEmpty(mimeType))
+ {
+ throw gcnew ArgumentException("must provide a mime type", "mimeType");
+ }
+
+ if(stream == nullptr)
+ {
+ throw gcnew ArgumentNullException("stream");
+ }
+
+ _responseStream = stream;
+ _mimeType = mimeType;
+ _action = ResponseAction::Respond;
+ }
+
+}
\ No newline at end of file
diff --git a/CefSharp/RequestResponse.h b/CefSharp/RequestResponse.h
new file mode 100644
index 000000000..49d6c55b8
--- /dev/null
+++ b/CefSharp/RequestResponse.h
@@ -0,0 +1,57 @@
+#include "stdafx.h"
+#pragma once
+
+#include "Request.h"
+
+using namespace System;
+using namespace System::IO;
+
+namespace CefSharp
+{
+
+ public interface class IRequestResponse
+ {
+ /// cancel the request, return nothing
+ void Cancel();
+ /// the current request
+ property IRequest^ Request { IRequest^ get(); };
+ /// respond with redirection to the provided URL
+ void Redirect(String^ url);
+ /// respond with data from Stream
+ void RespondWith(Stream^ stream, String^ mimeType);
+ };
+
+ enum class ResponseAction
+ {
+ Continue, Cancel, Redirect, Respond
+ };
+
+
+ ref class RequestResponse : IRequestResponse
+ {
+ IRequest^ _request;
+ Stream^ _responseStream;
+ String^ _mimeType;
+ String^ _redirectUrl;
+ ResponseAction _action;
+
+ internal:
+
+ RequestResponse(IRequest^ request) :
+ _action(ResponseAction::Continue),
+ _request(request) {}
+
+ property Stream^ ResponseStream { Stream^ get() { return _responseStream; } }
+ property String^ MimeType { String^ get() { return _mimeType; } }
+ property String^ RedirectUrl { String^ get() { return _redirectUrl; } }
+ property ResponseAction Action { ResponseAction get() { return _action; } }
+
+ public:
+
+ virtual void Cancel();
+ virtual property IRequest^ Request { IRequest^ get() { return _request; } }
+ virtual void Redirect(String^ url);
+ virtual void RespondWith(Stream^ stream, String^ mimeType);
+ };
+
+}
\ No newline at end of file