Skip to content

Commit d66798b

Browse files
davidmiamaitland
authored andcommitted
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
1 parent 0279430 commit d66798b

17 files changed

+527
-2
lines changed

CefSharp.BrowserSubprocess.Core/Wrapper/Frame.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ IRequest^ Frame::CreateRequest(bool initializePostData)
290290
throw gcnew NotImplementedException();
291291
}
292292

293+
IUrlRequest^ Frame::CreateUrlRequest(IRequest^ request, IUrlRequestClient^ client)
294+
{
295+
throw gcnew NotImplementedException();
296+
}
297+
293298
bool Frame::IsDisposed::get()
294299
{
295300
return _disposed;

CefSharp.BrowserSubprocess.Core/Wrapper/Frame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ namespace CefSharp
244244

245245
virtual IRequest^ CreateRequest(bool initializePostData);
246246

247+
virtual IUrlRequest^ CreateUrlRequest(IRequest^ request, IUrlRequestClient^ client);
248+
247249
virtual property bool IsDisposed
248250
{
249251
bool get();

CefSharp.Core/CefSharp.Core.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@
235235
<ClCompile Include="Internals\CefExtensionWrapper.cpp" />
236236
<ClCompile Include="Internals\CefFrameWrapper.cpp" />
237237
<ClCompile Include="Internals\CefSharpBrowserWrapper.cpp" />
238+
<ClCompile Include="Internals\CefUrlRequestClientAdapter.cpp" />
239+
<ClCompile Include="Internals\CefUrlRequestWrapper.cpp" />
238240
<ClCompile Include="Internals\CefValueWrapper.cpp" />
239241
<ClCompile Include="Internals\ClientAdapter.cpp" />
240242
<ClCompile Include="Internals\CookieVisitor.cpp" />
@@ -270,6 +272,8 @@
270272
<ClInclude Include="Internals\CefResourceSkipCallbackWrapper.h" />
271273
<ClInclude Include="Internals\CefRunFileDialogCallbackAdapter.h" />
272274
<ClInclude Include="Internals\CefSchemeRegistrarWrapper.h" />
275+
<ClInclude Include="Internals\CefUrlRequestClientAdapter.h" />
276+
<ClInclude Include="Internals\CefUrlRequestWrapper.h" />
273277
<ClInclude Include="Internals\CefValueWrapper.h" />
274278
<ClInclude Include="Internals\CefWriteHandlerWrapper.h" />
275279
<ClInclude Include="Internals\CefImageWrapper.h" />

CefSharp.Core/CefSharp.Core.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@
8383
<ClCompile Include="RequestContext.cpp">
8484
<Filter>Source Files</Filter>
8585
</ClCompile>
86+
<ClCompile Include="Internals\CefUrlRequestClientAdapter.cpp">
87+
<Filter>Source Files</Filter>
88+
</ClCompile>
89+
<ClCompile Include="Internals\CefUrlRequestWrapper.cpp">
90+
<Filter>Source Files</Filter>
91+
</ClCompile>
8692
</ItemGroup>
8793
<ItemGroup>
8894
<ClInclude Include="vcclr_local.h">
@@ -313,6 +319,12 @@
313319
<ClInclude Include="Internals\CefResourceSkipCallbackWrapper.h">
314320
<Filter>Header Files</Filter>
315321
</ClInclude>
322+
<ClInclude Include="Internals\CefUrlRequestClientAdapter.h">
323+
<Filter>Header Files</Filter>
324+
</ClInclude>
325+
<ClInclude Include="Internals\CefUrlRequestWrapper.h">
326+
<Filter>Header Files</Filter>
327+
</ClInclude>
316328
</ItemGroup>
317329
<ItemGroup>
318330
<ClInclude Include="Internals\CefSharpBrowserWrapper.h">

CefSharp.Core/Internals/CefFrameWrapper.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "Internals\ClientAdapter.h"
1313
#include "Internals\Serialization\Primitives.h"
1414
#include "Internals\Messaging\Messages.h"
15+
#include "Internals\CefURLRequestWrapper.h"
16+
#include "Internals\CefURLRequestClientAdapter.h"
1517

1618
using namespace CefSharp::Internals::Messaging;
1719
using namespace CefSharp::Internals::Serialization;
@@ -407,6 +409,17 @@ IRequest^ CefFrameWrapper::CreateRequest(bool initializePostData)
407409
return gcnew CefRequestWrapper(request);
408410
}
409411

412+
IUrlRequest^ CefFrameWrapper::CreateUrlRequest(IRequest^ request, IUrlRequestClient^ client)
413+
{
414+
ThrowIfDisposed();
415+
416+
auto urlRequest = _frame->CreateURLRequest(
417+
(CefRequestWrapper^)request,
418+
new CefUrlRequestClientAdapter(client));
419+
420+
return gcnew CefUrlRequestWrapper(urlRequest);
421+
}
422+
410423
void CefFrameWrapper::ThrowIfFrameInvalid()
411424
{
412425
if (_frame->IsValid() == false)

CefSharp.Core/Internals/CefFrameWrapper.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,30 @@ namespace CefSharp
255255

256256
virtual IRequest^ CreateRequest(bool initializePostData);
257257

258+
///
259+
// Create a new URL request that will be treated as originating from this
260+
// frame and the associated browser. This request may be intercepted by the
261+
// client via CefResourceRequestHandler or CefSchemeHandlerFactory. Use
262+
// CefURLRequest::Create instead if you do not want the request to have this
263+
// association, in which case it may be handled differently (see documentation
264+
// on that method). Requests may originate from both the browser process and
265+
// the render process.
266+
//
267+
// For requests originating from the browser process:
268+
// - POST data may only contain a single element of type PDE_TYPE_FILE or
269+
// PDE_TYPE_BYTES.
270+
// For requests originating from the render process:
271+
// - POST data may only contain a single element of type PDE_TYPE_BYTES.
272+
// - If the response contains Content-Disposition or Mime-Type header values
273+
// that would not normally be rendered then the response may receive
274+
// special handling inside the browser (for example, via the file download
275+
// code path instead of the URL request code path).
276+
//
277+
// The |request| object will be marked as read-only after calling this method.
278+
///
279+
/*--cef()--*/
280+
virtual IUrlRequest^ CreateUrlRequest(IRequest^ request, IUrlRequestClient^ client);
281+
258282
void ThrowIfFrameInvalid();
259283
};
260284
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright © 2019 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#include "Stdafx.h"
6+
#include "CefUrlRequestClientAdapter.h"
7+
#include "CefUrlRequestWrapper.h"
8+
#include "CefAuthCallbackWrapper.h"
9+
10+
using namespace System::IO;
11+
12+
void CefUrlRequestClientAdapter::OnRequestComplete(CefRefPtr<CefURLRequest> request)
13+
{
14+
_client->OnRequestComplete(gcnew CefUrlRequestWrapper(request));
15+
}
16+
17+
void CefUrlRequestClientAdapter::OnUploadProgress(CefRefPtr<CefURLRequest> request, int64 current, int64 total)
18+
{
19+
_client->OnUploadProgress(gcnew CefUrlRequestWrapper(request), current, total);
20+
}
21+
22+
void CefUrlRequestClientAdapter::OnDownloadProgress(CefRefPtr<CefURLRequest> request, int64 current, int64 total)
23+
{
24+
_client->OnDownloadProgress(gcnew CefUrlRequestWrapper(request), current, total);
25+
}
26+
27+
void CefUrlRequestClientAdapter::OnDownloadData(CefRefPtr<CefURLRequest> request, const void* data, size_t data_length)
28+
{
29+
UnmanagedMemoryStream readStream((Byte*)data, (Int64)data_length, (Int64)data_length, FileAccess::Read);
30+
_client->OnDownloadData(
31+
gcnew CefUrlRequestWrapper(request),
32+
%readStream
33+
);
34+
}
35+
36+
bool CefUrlRequestClientAdapter::GetAuthCredentials(bool isProxy,
37+
const CefString& host,
38+
int port,
39+
const CefString& realm,
40+
const CefString& scheme,
41+
CefRefPtr<CefAuthCallback> callback)
42+
{
43+
return _client->GetAuthCredentials(
44+
isProxy,
45+
StringUtils::ToClr(host),
46+
port,
47+
StringUtils::ToClr(realm),
48+
StringUtils::ToClr(scheme),
49+
gcnew CefAuthCallbackWrapper(callback));
50+
}
51+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright © 2019 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "Stdafx.h"
8+
#include "Include\cef_urlrequest.h"
9+
10+
namespace CefSharp
11+
{
12+
namespace Internals
13+
{
14+
15+
/// Interface that should be implemented by the CefUrlRequest client.
16+
/// The methods of this class will be called on the same thread that created
17+
/// the request unless otherwise documented.
18+
public class CefUrlRequestClientAdapter : public CefURLRequestClient
19+
{
20+
private:
21+
gcroot<IUrlRequestClient^> _client;
22+
23+
public:
24+
CefUrlRequestClientAdapter(IUrlRequestClient^ client)
25+
{
26+
_client = client;
27+
}
28+
29+
~CefUrlRequestClientAdapter()
30+
{
31+
delete _client;
32+
_client = nullptr;
33+
}
34+
35+
// Notifies the client that the request has completed. Use the
36+
// CefURLRequest::GetRequestStatus method to determine if the request was
37+
// successful or not.
38+
///
39+
/*--cef()--*/
40+
virtual void OnRequestComplete(CefRefPtr<CefURLRequest> request) OVERRIDE;
41+
42+
///
43+
// Notifies the client of upload progress. |current| denotes the number of
44+
// bytes sent so far and |total| is the total size of uploading data (or -1 if
45+
// chunked upload is enabled). This method will only be called if the
46+
// UR_FLAG_REPORT_UPLOAD_PROGRESS flag is set on the request.
47+
///
48+
/*--cef()--*/
49+
virtual void OnUploadProgress(CefRefPtr<CefURLRequest> request,
50+
int64 current,
51+
int64 total) OVERRIDE;
52+
53+
///ref
54+
// Notifies the client of download progress. |current| denotes the number of
55+
// bytes received up to the call and |total| is the expected total size of the
56+
// response (or -1 if not determined).
57+
///
58+
/*--cef()--*/
59+
virtual void OnDownloadProgress(CefRefPtr<CefURLRequest> request,
60+
int64 current,
61+
int64 total) OVERRIDE;
62+
63+
///
64+
// Called when some part of the response is read. |data| contains the current
65+
// bytes received since the last call. This method will not be called if the
66+
// UR_FLAG_NO_DOWNLOAD_DATA flag is set on the request.
67+
///
68+
/*--cef()--*/
69+
virtual void OnDownloadData(CefRefPtr<CefURLRequest> request,
70+
const void* data,
71+
size_t data_length) OVERRIDE;
72+
73+
///
74+
// Called on the IO thread when the browser needs credentials from the user.
75+
// |isProxy| indicates whether the host is a proxy server. |host| contains the
76+
// hostname and |port| contains the port number. Return true to continue the
77+
// request and call CefAuthCallback::Continue() when the authentication
78+
// information is available. If the request has an associated browser/frame
79+
// then returning false will result in a call to GetAuthCredentials on the
80+
// CefRequestHandler associated with that browser, if any. Otherwise,
81+
// returning false will cancel the request immediately. This method will only
82+
// be called for requests initiated from the browser process.
83+
///
84+
/*--cef(optional_param=realm)--*/
85+
virtual bool GetAuthCredentials(bool isProxy,
86+
const CefString& host,
87+
int port,
88+
const CefString& realm,
89+
const CefString& scheme,
90+
CefRefPtr<CefAuthCallback> callback) OVERRIDE;
91+
92+
IMPLEMENT_REFCOUNTING(CefUrlRequestClientAdapter);
93+
};
94+
}
95+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright © 2019 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#include "Stdafx.h"
6+
#include "CefUrlRequestWrapper.h"
7+
#include "CefResponseWrapper.h"
8+
9+
bool CefUrlRequestWrapper::ResponseWasCached::get()
10+
{
11+
ThrowIfDisposed();
12+
13+
return _urlRequest->ResponseWasCached();
14+
}
15+
16+
IResponse^ CefUrlRequestWrapper::Response::get()
17+
{
18+
ThrowIfDisposed();
19+
20+
return gcnew CefResponseWrapper(_urlRequest->GetResponse());
21+
}
22+
23+
UrlRequestStatus CefUrlRequestWrapper::RequestStatus::get()
24+
{
25+
ThrowIfDisposed();
26+
27+
return (UrlRequestStatus)_urlRequest->GetRequestStatus();
28+
}
29+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright © 2019 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "Stdafx.h"
8+
9+
#include "include\cef_urlrequest.h"
10+
#include "CefWrapper.h"
11+
12+
namespace CefSharp
13+
{
14+
namespace Internals
15+
{
16+
///
17+
// Class used to make a URL request. URL requests are not associated with
18+
// a browser instance so no CefClient callbacks will be executed.
19+
// URL requests can be created on any valid CEF thread in either the browser
20+
// or render process. Once created the methods of the URL request object must
21+
// be accessed on the same thread that created it.
22+
///
23+
/*--cef(source=library)--*/
24+
ref class CefUrlRequestWrapper : public IUrlRequest, public CefWrapper
25+
{
26+
private:
27+
MCefRefPtr<CefURLRequest> _urlRequest;
28+
internal:
29+
CefUrlRequestWrapper::CefUrlRequestWrapper(CefRefPtr<CefURLRequest> &urlRequest)
30+
: _urlRequest(urlRequest)
31+
{
32+
}
33+
34+
!CefUrlRequestWrapper()
35+
{
36+
_urlRequest = NULL;
37+
}
38+
39+
~CefUrlRequestWrapper()
40+
{
41+
this->!CefUrlRequestWrapper();
42+
}
43+
44+
public:
45+
///
46+
// Returns true if the response body was served from the cache. This includes
47+
// responses for which revalidation was required.
48+
///
49+
/*--cef()--*/
50+
virtual property bool ResponseWasCached
51+
{
52+
bool get();
53+
}
54+
55+
///
56+
// Returns the response, or NULL if no response information is available.
57+
// Response information will only be available after the upload has completed.
58+
// The returned object is read-only and should not be modified.
59+
///
60+
/*--cef()--*/
61+
virtual property IResponse^ Response
62+
{
63+
IResponse^ get();
64+
}
65+
66+
///
67+
// Returns the request status.
68+
///
69+
/*--cef(default_retval=UR_UNKNOWN)--*/
70+
virtual property UrlRequestStatus RequestStatus
71+
{
72+
UrlRequestStatus get();
73+
}
74+
};
75+
}
76+
}

0 commit comments

Comments
 (0)