mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 04:00:07 +00:00
reworked IBeforeResourceLoad with a cleaner API
users now manipulate a RequestResponse object rather than setting ref arguments
This commit is contained in:
committed by
Dmitry Azaraev
parent
eea6c8f477
commit
78ca2c17e3
@@ -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(
|
||||
"<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>"));
|
||||
mimeType = "text/html";
|
||||
requestResponse.RespondWith(resourceStream, "text/html");
|
||||
}
|
||||
Console.WriteLine(request.Url);
|
||||
return ReturnValue.Continue;
|
||||
}
|
||||
|
||||
private void ExitToolStripMenuItemClick(object sender, EventArgs e)
|
||||
|
||||
@@ -232,6 +232,10 @@
|
||||
RelativePath=".\Request.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\RequestResponse.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\RtzCountdownEvent.cpp"
|
||||
>
|
||||
@@ -314,6 +318,10 @@
|
||||
RelativePath=".\Request.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\RequestResponse.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\resource.h"
|
||||
>
|
||||
|
||||
+14
-10
@@ -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<StreamAdapter> adapter = new StreamAdapter(mResourceStream);
|
||||
CefRefPtr<StreamAdapter> adapter = new StreamAdapter(requestResponse->ResponseStream);
|
||||
resourceStream = CefStreamReader::CreateForHandler(static_cast<CefRefPtr<CefReadHandler>>(adapter));
|
||||
pin_ptr<const wchar_t> 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;
|
||||
|
||||
@@ -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 <c>ReturnValue.Continue</c>.
|
||||
///
|
||||
/// To redirect the resource to a new url populate the <c>redirectUrl</c> value
|
||||
/// and return <c>ReturnValue.Continue</c>.
|
||||
///
|
||||
/// To specify data for the resource return a Stream object in <c>resourceStream</c>,
|
||||
/// set <c>mimeType</c> to the resource stream's mime type, and return <c>ReturnValue.Continue</c>.
|
||||
///
|
||||
/// To cancel loading of the resource return <c>ReturnValue.Handled</c>.
|
||||
///
|
||||
/// Any modifications to request will be observed. If the URL in <c>request</c> is changed and
|
||||
/// <c>redirectUrl</c> is also set, the URL in <c>request</c> 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);
|
||||
};
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user