Core - Add IURLRequest #1721 (#2881)

* Core - Add IURLRequest #1721

Add IURLRequest, IURLRequestClient, and GetURLRequest to IFrame

* Rename URL classes to Url, add xmldoc and licenses

* Add UrlRequestClient to CefSharp.Example, more minor cleanup

* Getters

* Rebase tweaks

* Fix filenames, remove unused

* Use UnmanagedMemoryStream instead of byte array for IUrlRequestClient.OnDownloadData
This commit is contained in:
David M Iserovich
2019-10-01 06:11:10 -04:00
committed by amaitland
parent 0279430114
commit d66798b03e
17 changed files with 527 additions and 2 deletions
@@ -290,6 +290,11 @@ IRequest^ Frame::CreateRequest(bool initializePostData)
throw gcnew NotImplementedException();
}
IUrlRequest^ Frame::CreateUrlRequest(IRequest^ request, IUrlRequestClient^ client)
{
throw gcnew NotImplementedException();
}
bool Frame::IsDisposed::get()
{
return _disposed;
@@ -244,6 +244,8 @@ namespace CefSharp
virtual IRequest^ CreateRequest(bool initializePostData);
virtual IUrlRequest^ CreateUrlRequest(IRequest^ request, IUrlRequestClient^ client);
virtual property bool IsDisposed
{
bool get();
+4
View File
@@ -235,6 +235,8 @@
<ClCompile Include="Internals\CefExtensionWrapper.cpp" />
<ClCompile Include="Internals\CefFrameWrapper.cpp" />
<ClCompile Include="Internals\CefSharpBrowserWrapper.cpp" />
<ClCompile Include="Internals\CefUrlRequestClientAdapter.cpp" />
<ClCompile Include="Internals\CefUrlRequestWrapper.cpp" />
<ClCompile Include="Internals\CefValueWrapper.cpp" />
<ClCompile Include="Internals\ClientAdapter.cpp" />
<ClCompile Include="Internals\CookieVisitor.cpp" />
@@ -270,6 +272,8 @@
<ClInclude Include="Internals\CefResourceSkipCallbackWrapper.h" />
<ClInclude Include="Internals\CefRunFileDialogCallbackAdapter.h" />
<ClInclude Include="Internals\CefSchemeRegistrarWrapper.h" />
<ClInclude Include="Internals\CefUrlRequestClientAdapter.h" />
<ClInclude Include="Internals\CefUrlRequestWrapper.h" />
<ClInclude Include="Internals\CefValueWrapper.h" />
<ClInclude Include="Internals\CefWriteHandlerWrapper.h" />
<ClInclude Include="Internals\CefImageWrapper.h" />
@@ -83,6 +83,12 @@
<ClCompile Include="RequestContext.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Internals\CefUrlRequestClientAdapter.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Internals\CefUrlRequestWrapper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vcclr_local.h">
@@ -313,6 +319,12 @@
<ClInclude Include="Internals\CefResourceSkipCallbackWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\CefUrlRequestClientAdapter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\CefUrlRequestWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Internals\CefSharpBrowserWrapper.h">
@@ -12,6 +12,8 @@
#include "Internals\ClientAdapter.h"
#include "Internals\Serialization\Primitives.h"
#include "Internals\Messaging\Messages.h"
#include "Internals\CefURLRequestWrapper.h"
#include "Internals\CefURLRequestClientAdapter.h"
using namespace CefSharp::Internals::Messaging;
using namespace CefSharp::Internals::Serialization;
@@ -407,6 +409,17 @@ IRequest^ CefFrameWrapper::CreateRequest(bool initializePostData)
return gcnew CefRequestWrapper(request);
}
IUrlRequest^ CefFrameWrapper::CreateUrlRequest(IRequest^ request, IUrlRequestClient^ client)
{
ThrowIfDisposed();
auto urlRequest = _frame->CreateURLRequest(
(CefRequestWrapper^)request,
new CefUrlRequestClientAdapter(client));
return gcnew CefUrlRequestWrapper(urlRequest);
}
void CefFrameWrapper::ThrowIfFrameInvalid()
{
if (_frame->IsValid() == false)
+24
View File
@@ -255,6 +255,30 @@ namespace CefSharp
virtual IRequest^ CreateRequest(bool initializePostData);
///
// Create a new URL request that will be treated as originating from this
// frame and the associated browser. This request may be intercepted by the
// client via CefResourceRequestHandler or CefSchemeHandlerFactory. Use
// CefURLRequest::Create instead if you do not want the request to have this
// association, in which case it may be handled differently (see documentation
// on that method). Requests may originate from both the browser process and
// the render process.
//
// For requests originating from the browser process:
// - POST data may only contain a single element of type PDE_TYPE_FILE or
// PDE_TYPE_BYTES.
// For requests originating from the render process:
// - POST data may only contain a single element of type PDE_TYPE_BYTES.
// - If the response contains Content-Disposition or Mime-Type header values
// that would not normally be rendered then the response may receive
// special handling inside the browser (for example, via the file download
// code path instead of the URL request code path).
//
// The |request| object will be marked as read-only after calling this method.
///
/*--cef()--*/
virtual IUrlRequest^ CreateUrlRequest(IRequest^ request, IUrlRequestClient^ client);
void ThrowIfFrameInvalid();
};
}
@@ -0,0 +1,51 @@
// Copyright © 2019 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.
#include "Stdafx.h"
#include "CefUrlRequestClientAdapter.h"
#include "CefUrlRequestWrapper.h"
#include "CefAuthCallbackWrapper.h"
using namespace System::IO;
void CefUrlRequestClientAdapter::OnRequestComplete(CefRefPtr<CefURLRequest> request)
{
_client->OnRequestComplete(gcnew CefUrlRequestWrapper(request));
}
void CefUrlRequestClientAdapter::OnUploadProgress(CefRefPtr<CefURLRequest> request, int64 current, int64 total)
{
_client->OnUploadProgress(gcnew CefUrlRequestWrapper(request), current, total);
}
void CefUrlRequestClientAdapter::OnDownloadProgress(CefRefPtr<CefURLRequest> request, int64 current, int64 total)
{
_client->OnDownloadProgress(gcnew CefUrlRequestWrapper(request), current, total);
}
void CefUrlRequestClientAdapter::OnDownloadData(CefRefPtr<CefURLRequest> request, const void* data, size_t data_length)
{
UnmanagedMemoryStream readStream((Byte*)data, (Int64)data_length, (Int64)data_length, FileAccess::Read);
_client->OnDownloadData(
gcnew CefUrlRequestWrapper(request),
%readStream
);
}
bool CefUrlRequestClientAdapter::GetAuthCredentials(bool isProxy,
const CefString& host,
int port,
const CefString& realm,
const CefString& scheme,
CefRefPtr<CefAuthCallback> callback)
{
return _client->GetAuthCredentials(
isProxy,
StringUtils::ToClr(host),
port,
StringUtils::ToClr(realm),
StringUtils::ToClr(scheme),
gcnew CefAuthCallbackWrapper(callback));
}
@@ -0,0 +1,95 @@
// Copyright © 2019 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_urlrequest.h"
namespace CefSharp
{
namespace Internals
{
/// Interface that should be implemented by the CefUrlRequest client.
/// The methods of this class will be called on the same thread that created
/// the request unless otherwise documented.
public class CefUrlRequestClientAdapter : public CefURLRequestClient
{
private:
gcroot<IUrlRequestClient^> _client;
public:
CefUrlRequestClientAdapter(IUrlRequestClient^ client)
{
_client = client;
}
~CefUrlRequestClientAdapter()
{
delete _client;
_client = nullptr;
}
// Notifies the client that the request has completed. Use the
// CefURLRequest::GetRequestStatus method to determine if the request was
// successful or not.
///
/*--cef()--*/
virtual void OnRequestComplete(CefRefPtr<CefURLRequest> request) OVERRIDE;
///
// Notifies the client of upload progress. |current| denotes the number of
// bytes sent so far and |total| is the total size of uploading data (or -1 if
// chunked upload is enabled). This method will only be called if the
// UR_FLAG_REPORT_UPLOAD_PROGRESS flag is set on the request.
///
/*--cef()--*/
virtual void OnUploadProgress(CefRefPtr<CefURLRequest> request,
int64 current,
int64 total) OVERRIDE;
///ref
// Notifies the client of download progress. |current| denotes the number of
// bytes received up to the call and |total| is the expected total size of the
// response (or -1 if not determined).
///
/*--cef()--*/
virtual void OnDownloadProgress(CefRefPtr<CefURLRequest> request,
int64 current,
int64 total) OVERRIDE;
///
// Called when some part of the response is read. |data| contains the current
// bytes received since the last call. This method will not be called if the
// UR_FLAG_NO_DOWNLOAD_DATA flag is set on the request.
///
/*--cef()--*/
virtual void OnDownloadData(CefRefPtr<CefURLRequest> request,
const void* data,
size_t data_length) OVERRIDE;
///
// Called on the IO thread when the browser needs credentials from the user.
// |isProxy| indicates whether the host is a proxy server. |host| contains the
// hostname and |port| contains the port number. Return true to continue the
// request and call CefAuthCallback::Continue() when the authentication
// information is available. If the request has an associated browser/frame
// then returning false will result in a call to GetAuthCredentials on the
// CefRequestHandler associated with that browser, if any. Otherwise,
// returning false will cancel the request immediately. This method will only
// be called for requests initiated from the browser process.
///
/*--cef(optional_param=realm)--*/
virtual bool GetAuthCredentials(bool isProxy,
const CefString& host,
int port,
const CefString& realm,
const CefString& scheme,
CefRefPtr<CefAuthCallback> callback) OVERRIDE;
IMPLEMENT_REFCOUNTING(CefUrlRequestClientAdapter);
};
}
}
@@ -0,0 +1,29 @@
// Copyright © 2019 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.
#include "Stdafx.h"
#include "CefUrlRequestWrapper.h"
#include "CefResponseWrapper.h"
bool CefUrlRequestWrapper::ResponseWasCached::get()
{
ThrowIfDisposed();
return _urlRequest->ResponseWasCached();
}
IResponse^ CefUrlRequestWrapper::Response::get()
{
ThrowIfDisposed();
return gcnew CefResponseWrapper(_urlRequest->GetResponse());
}
UrlRequestStatus CefUrlRequestWrapper::RequestStatus::get()
{
ThrowIfDisposed();
return (UrlRequestStatus)_urlRequest->GetRequestStatus();
}
@@ -0,0 +1,76 @@
// Copyright © 2019 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_urlrequest.h"
#include "CefWrapper.h"
namespace CefSharp
{
namespace Internals
{
///
// Class used to make a URL request. URL requests are not associated with
// a browser instance so no CefClient callbacks will be executed.
// URL requests can be created on any valid CEF thread in either the browser
// or render process. Once created the methods of the URL request object must
// be accessed on the same thread that created it.
///
/*--cef(source=library)--*/
ref class CefUrlRequestWrapper : public IUrlRequest, public CefWrapper
{
private:
MCefRefPtr<CefURLRequest> _urlRequest;
internal:
CefUrlRequestWrapper::CefUrlRequestWrapper(CefRefPtr<CefURLRequest> &urlRequest)
: _urlRequest(urlRequest)
{
}
!CefUrlRequestWrapper()
{
_urlRequest = NULL;
}
~CefUrlRequestWrapper()
{
this->!CefUrlRequestWrapper();
}
public:
///
// Returns true if the response body was served from the cache. This includes
// responses for which revalidation was required.
///
/*--cef()--*/
virtual property bool ResponseWasCached
{
bool get();
}
///
// Returns the response, or NULL if no response information is available.
// Response information will only be available after the upload has completed.
// The returned object is read-only and should not be modified.
///
/*--cef()--*/
virtual property IResponse^ Response
{
IResponse^ get();
}
///
// Returns the request status.
///
/*--cef(default_retval=UR_UNKNOWN)--*/
virtual property UrlRequestStatus RequestStatus
{
UrlRequestStatus get();
}
};
}
}
+1
View File
@@ -127,6 +127,7 @@
</Compile>
<Compile Include="CefSharpSchemeHandler.cs" />
<Compile Include="CefSharpSchemeHandlerFactory.cs" />
<Compile Include="UrlRequestClient.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
+46
View File
@@ -0,0 +1,46 @@
// Copyright © 2019 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 UrlRequestClient : IUrlRequestClient
{
private Action<IUrlRequest, byte[]> CompleteAction;
private MemoryStream ResponseBody = new MemoryStream();
public UrlRequestClient(Action<IUrlRequest, byte[]> completeAction)
{
CompleteAction = completeAction;
}
public bool GetAuthCredentials(bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
return true;
}
public void OnDownloadData(IUrlRequest request, Stream data)
{
data.CopyTo(ResponseBody);
}
public void OnDownloadProgress(IUrlRequest request, long current, long total)
{
return;
}
public void OnRequestComplete(IUrlRequest request)
{
CompleteAction(request, ResponseBody.ToArray());
}
public void OnUploadProgress(IUrlRequest request, long current, long total)
{
return;
}
}
}
@@ -2,10 +2,14 @@
//
// 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;
using System.Threading;
using CefSharp.OffScreen;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using CefSharp.Example;
namespace CefSharp.Test.OffScreen
{
@@ -127,5 +131,46 @@ namespace CefSharp.Test.OffScreen
}
}
[Fact]
public async Task CanMakeUrlRequest()
{
using (var browser = new ChromiumWebBrowser("https://code.jquery.com/jquery-3.4.1.min.js"))
{
await browser.LoadPageAsync();
var mainFrame = browser.GetMainFrame();
Assert.True(mainFrame.IsValid);
IUrlRequest urlRequest = null;
var t = new TaskCompletionSource<string>();
var wasCached = false;
var requestClient = new UrlRequestClient(
(IUrlRequest request, byte[] responseBody) =>
{
wasCached = request.ResponseWasCached;
t.TrySetResult(System.Text.Encoding.UTF8.GetString(responseBody));
}
);
//Make the request on the CEF UI Thread
await Cef.UIThreadTaskFactory.StartNew(delegate
{
var request = mainFrame.CreateRequest(false);
request.Method = "GET";
request.Url = "https://code.jquery.com/jquery-3.4.1.min.js";
urlRequest = mainFrame.CreateUrlRequest(request, requestClient);
});
var stringResult = await t.Task;
Assert.True(!string.IsNullOrEmpty(stringResult));
Assert.True(wasCached);
}
}
}
}
+5
View File
@@ -102,6 +102,11 @@
<Compile Include="CdmRegistration.cs" />
<Compile Include="DefaultApp.cs" />
<Compile Include="Enums\SchemeOptions.cs" />
<Compile Include="Internals\CommandLineArgDictionary.cs" />
<Compile Include="Internals\ReadOnlyNameValueCollection.cs" />
<Compile Include="Internals\TaskScheduler\LimitedConcurrencyLevelTaskScheduler.cs" />
<Compile Include="IUrlRequest.cs" />
<Compile Include="IUrlRequestClient.cs" />
<Compile Include="ResourceRequestHandlerFactory.cs" />
<Compile Include="ResourceRequestHandlerFactoryItem.cs" />
<Compile Include="Enums\AlphaType.cs" />
+21 -2
View File
@@ -71,7 +71,7 @@ namespace CefSharp
/// Retrieve this frame's HTML source as a string sent to the specified visitor.
/// Use the <see cref="GetSourceAsync"/> method for a Task based async wrapper
/// </summary>
/// <param name="visitor">visitor will recieve string values asynchronously</param>
/// <param name="visitor">visitor will receive string values asynchronously</param>
void GetSource(IStringVisitor visitor);
/// <summary>
@@ -86,7 +86,7 @@ namespace CefSharp
/// Retrieve this frame's display text as a string sent to the specified visitor.
/// Use the <see cref="GetTextAsync"/> method for a Task based async wrapper
/// </summary>
/// <param name="visitor">visitor will recieve string values asynchronously</param>
/// <param name="visitor">visitor will receive string values asynchronously</param>
void GetText(IStringVisitor visitor);
/// <summary>
@@ -182,5 +182,24 @@ namespace CefSharp
/// <param name="initializePostData">Initialize the PostData object when creating this request</param>
/// <returns>A new instance of the request</returns>
IRequest CreateRequest(bool initializePostData = true);
/// <summary>
/// Create a new URL request that will be treated as originating from this frame
/// and the associated browser. This request may be intercepted by the client via
/// <see cref="IResourceRequestHandler"/> or <see cref="ISchemeHandlerFactory"/>.
/// Use IUrlRequest.Create instead if you do not want the request to have
/// this association, in which case it may be handled differently (see documentation on that method).
///
/// Requests may originate from both the browser process and the render process.
/// For requests originating from the browser process: - POST data may only contain a single element
/// of type PDE_TYPE_FILE or PDE_TYPE_BYTES.
/// For requests originating from the render process: - POST data may only contain a single element of type PDE_TYPE_BYTES.
/// - If the response contains Content-Disposition or Mime-Type header values that would not normally be rendered then
/// the response may receive special handling inside the browser
/// for example, via the file download code path instead of the URL request code path).
///
/// The request object will be marked as read-only after calling this method.
/// </summary>
IUrlRequest CreateUrlRequest(IRequest request, IUrlRequestClient client);
}
}
+33
View File
@@ -0,0 +1,33 @@
// Copyright © 2019 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;
namespace CefSharp
{
/// <summary>
/// Class used to make a URL request. URL requests are not associated with
/// a browser instance so no CefClient callbacks will be executed.
/// URL requests can be created on any valid CEF thread in either the browser
/// or render process. Once created the methods of the URL request object must
/// be accessed on the same thread that created it.
/// </summary>
public interface IUrlRequest : IDisposable
{
/// <summary>
/// True if the response was served from the cache.
/// </summary>
bool ResponseWasCached { get; }
/// <summary>
/// The response, or null if no response information is available
/// </summary>
IResponse Response { get; }
/// <summary>
/// The request status.
/// </summary>
UrlRequestStatus RequestStatus { get; }
}
}
+65
View File
@@ -0,0 +1,65 @@
// Copyright © 2019 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
{
/// <summary>
/// Interface that should be implemented by the CefURLRequest client.
/// The methods of this class will be called on the same thread that created the request unless otherwise documented.
/// </summary>
public interface IUrlRequestClient
{
/// <summary>
/// Called on the IO thread when the browser needs credentials from the user.
/// This method will only be called for requests initiated from the browser process.
/// </summary>
/// <param name="isProxy">indicates whether the host is a proxy server.</param>
/// <param name="host">the hostname.</param>
/// <param name="port">the port number.</param>
/// <param name="realm"></param>
/// <param name="scheme"></param>
/// <param name="callback">is a callback for authentication information</param>
/// <returns>
/// Return true to continue the request and call IAuthCallback.Continue() when the authentication information is available.
/// If the request has an associated browser/frame then returning false will result in a call to GetAuthCredentials
/// on the IRequestHandler associated with that browser, if any.
/// Otherwise, returning false will cancel the request immediately.
/// </returns>
bool GetAuthCredentials(bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback);
/// <summary>
/// Called when some part of the response is read. This method will not be called if the UR_FLAG_NO_DOWNLOAD_DATA flag is set on the request.
/// </summary>
/// <param name="request"></param>
/// <param name="data">Contains the current bytes received since the last call.</param>
void OnDownloadData(IUrlRequest request, Stream data);
/// <summary>
/// Notifies the client of download progress.
/// </summary>
/// <param name="request"></param>
/// <param name="current">denotes the number of bytes received up to the call </param>
/// <param name="total">is the expected total size of the response (or -1 if not determined).</param>
void OnDownloadProgress(IUrlRequest request, long current, long total);
/// <summary>
/// Notifies the client that the request has completed. Use the
/// IURLRequest.GetRequestStatus method to determine if the request was
/// successful or not.
/// </summary>
/// <param name="request"></param>
void OnRequestComplete(IUrlRequest request);
/// <summary>
/// Notifies the client of upload progress.
/// This method will only be called if the UR_FLAG_REPORT_UPLOAD_PROGRESS flag is set on the request.
/// </summary>
/// <param name="request"></param>
/// <param name="current">denotes the number of bytes sent so far.</param>
/// <param name="total">is the total size of uploading data (or -1 if chunked upload is enabled).</param>
void OnUploadProgress(IUrlRequest request, long current, long total);
}
}