2018-11-17 11:50:34 +01:00
|
|
|
unit uCEFUrlRequestClientComponent;
|
|
|
|
|
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
{$I cef.inc}
|
|
|
|
|
2022-02-19 18:56:41 +01:00
|
|
|
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
|
|
|
|
2018-11-17 11:50:34 +01:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
{$IFDEF MSWINDOWS}WinApi.Windows, WinApi.Messages, WinApi.ActiveX,{$ENDIF}
|
2019-11-08 23:15:53 +01:00
|
|
|
System.Classes, System.Math,
|
2018-11-17 11:50:34 +01:00
|
|
|
{$ELSE}
|
2019-11-08 23:15:53 +01:00
|
|
|
{$IFDEF MSWINDOWS}Windows, ActiveX,{$ENDIF} Classes, Math,
|
2018-11-17 11:50:34 +01:00
|
|
|
{$IFDEF FPC}
|
|
|
|
LCLProc, LCLType, LCLIntf, LResources, LMessages, InterfaceBase,
|
|
|
|
{$ELSE}
|
|
|
|
Messages,
|
|
|
|
{$ENDIF}
|
|
|
|
{$ENDIF}
|
2022-10-14 16:35:50 +02:00
|
|
|
uCEFTypes, uCEFInterfaces, uCEFConstants, uCEFUrlRequestClientEvents, uCEFUrlrequestClient, uCEFUrlRequest;
|
2018-11-17 11:50:34 +01:00
|
|
|
|
|
|
|
type
|
2022-10-14 16:35:50 +02:00
|
|
|
{$IFNDEF FPC}{$IFDEF DELPHI16_UP}[ComponentPlatformsAttribute(pfidWindows or pfidOSX or pfidLinux)]{$ENDIF}{$ENDIF}
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The TCEFUrlRequestClientComponent class puts together all CEF URL request procedures, functions, properties and events in one place.
|
|
|
|
/// </summary>
|
2018-11-17 11:50:34 +01:00
|
|
|
TCEFUrlRequestClientComponent = class(TComponent, ICEFUrlRequestClientEvents)
|
|
|
|
protected
|
|
|
|
FClient : ICefUrlrequestClient;
|
|
|
|
FThreadID : TCefThreadId;
|
2023-11-29 13:04:33 +01:00
|
|
|
FComponentID : integer;
|
2018-11-17 11:50:34 +01:00
|
|
|
|
|
|
|
FOnRequestComplete : TOnRequestComplete;
|
|
|
|
FOnUploadProgress : TOnUploadProgress;
|
|
|
|
FOnDownloadProgress : TOnDownloadProgress;
|
|
|
|
FOnDownloadData : TOnDownloadData;
|
|
|
|
FOnGetAuthCredentials : TOnGetAuthCredentials;
|
|
|
|
FOnCreateURLRequest : TNotifyEvent;
|
|
|
|
|
2023-11-29 13:04:33 +01:00
|
|
|
function GetComponentID : integer;
|
|
|
|
|
2018-11-17 11:50:34 +01:00
|
|
|
// ICefUrlrequestClient
|
|
|
|
procedure doOnRequestComplete(const request: ICefUrlRequest);
|
|
|
|
procedure doOnUploadProgress(const request: ICefUrlRequest; current, total: Int64);
|
|
|
|
procedure doOnDownloadProgress(const request: ICefUrlRequest; current, total: Int64);
|
|
|
|
procedure doOnDownloadData(const request: ICefUrlRequest; data: Pointer; dataLength: NativeUInt);
|
|
|
|
function doOnGetAuthCredentials(isProxy: Boolean; const host: ustring; port: Integer; const realm, scheme: ustring; const callback: ICefAuthCallback): Boolean;
|
|
|
|
|
|
|
|
// Custom
|
|
|
|
procedure doOnCreateURLRequest;
|
|
|
|
|
2018-12-06 11:49:08 +01:00
|
|
|
procedure DestroyRequestClient;
|
|
|
|
|
2018-11-17 11:50:34 +01:00
|
|
|
public
|
|
|
|
constructor Create(AOwner: TComponent); override;
|
|
|
|
procedure AfterConstruction; override;
|
2018-12-06 11:49:08 +01:00
|
|
|
procedure BeforeDestruction; override;
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Create the URLRequest in the context of TCEFUrlRequestClientComponent.ThreadId, which is the CEF UI thread by default.
|
|
|
|
/// </summary>
|
2018-11-17 11:50:34 +01:00
|
|
|
procedure AddURLRequest;
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the client.
|
|
|
|
/// </summary>
|
2018-11-17 11:50:34 +01:00
|
|
|
property Client : ICefUrlrequestClient read FClient;
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// CEF thread used to create the URLRequest. Most of the client events will be executed on the same thread.
|
|
|
|
/// </summary>
|
2018-11-17 11:50:34 +01:00
|
|
|
property ThreadID : TCefThreadId read FThreadID write FThreadID;
|
|
|
|
|
|
|
|
published
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Notifies the client that the request has completed. Use the
|
|
|
|
/// ICefUrlRequest.GetRequestStatus function to determine if the request
|
|
|
|
/// was successful or not.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>This event will be called on the TCEFUrlRequestClientComponent.ThreadId thread, which is the CEF UI thread by default.</para>
|
|
|
|
/// <para><see href="https://bitbucket.org/chromiumembedded/cef/src/master/include/capi/cef_urlrequest_capi.h">CEF source file: /include/capi/cef_urlrequest_capi.h (cef_urlrequest_client_t)</see></para>
|
|
|
|
/// </remarks>
|
2018-11-17 11:50:34 +01:00
|
|
|
property OnRequestComplete : TOnRequestComplete read FOnRequestComplete write FOnRequestComplete;
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// 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 function will only be called if the
|
|
|
|
/// UR_FLAG_REPORT_UPLOAD_PROGRESS flag is set on the request.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>This event will be called on the TCEFUrlRequestClientComponent.ThreadId thread, which is the CEF UI thread by default.</para>
|
|
|
|
/// <para><see href="https://bitbucket.org/chromiumembedded/cef/src/master/include/capi/cef_urlrequest_capi.h">CEF source file: /include/capi/cef_urlrequest_capi.h (cef_urlrequest_client_t)</see></para>
|
|
|
|
/// </remarks>
|
2018-11-17 11:50:34 +01:00
|
|
|
property OnUploadProgress : TOnUploadProgress read FOnUploadProgress write FOnUploadProgress;
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// 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).
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>This event will be called on the TCEFUrlRequestClientComponent.ThreadId thread, which is the CEF UI thread by default.</para>
|
|
|
|
/// <para><see href="https://bitbucket.org/chromiumembedded/cef/src/master/include/capi/cef_urlrequest_capi.h">CEF source file: /include/capi/cef_urlrequest_capi.h (cef_urlrequest_client_t)</see></para>
|
|
|
|
/// </remarks>
|
2018-11-17 11:50:34 +01:00
|
|
|
property OnDownloadProgress : TOnDownloadProgress read FOnDownloadProgress write FOnDownloadProgress;
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Called when some part of the response is read. |data| contains the current
|
|
|
|
/// bytes received since the last call. This function will not be called if
|
|
|
|
/// the UR_FLAG_NO_DOWNLOAD_DATA flag is set on the request.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>This event will be called on the TCEFUrlRequestClientComponent.ThreadId thread, which is the CEF UI thread by default.</para>
|
|
|
|
/// <para><see href="https://bitbucket.org/chromiumembedded/cef/src/master/include/capi/cef_urlrequest_capi.h">CEF source file: /include/capi/cef_urlrequest_capi.h (cef_urlrequest_client_t)</see></para>
|
|
|
|
/// </remarks>
|
2018-11-17 11:50:34 +01:00
|
|
|
property OnDownloadData : TOnDownloadData read FOnDownloadData write FOnDownloadData;
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// 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 (1) to
|
|
|
|
/// continue the request and call ICefAuthCallback.cont() when the
|
|
|
|
/// authentication information is available. If the request has an associated
|
|
|
|
/// browser/frame then returning false (0) will result in a call to
|
|
|
|
/// GetAuthCredentials on the ICefRequestHandler associated with that
|
|
|
|
/// browser, if any. Otherwise, returning false (0) will cancel the request
|
|
|
|
/// immediately. This function will only be called for requests initiated from
|
|
|
|
/// the browser process.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>This event will be called on the browser process CEF IO thread.</para>
|
|
|
|
/// <para><see href="https://bitbucket.org/chromiumembedded/cef/src/master/include/capi/cef_urlrequest_capi.h">CEF source file: /include/capi/cef_urlrequest_capi.h (cef_urlrequest_client_t)</see></para>
|
|
|
|
/// </remarks>
|
2018-11-17 11:50:34 +01:00
|
|
|
property OnGetAuthCredentials : TOnGetAuthCredentials read FOnGetAuthCredentials write FOnGetAuthCredentials;
|
2023-08-07 20:21:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Event triggered when the URLRequest has been created.
|
|
|
|
/// </summary>
|
2018-11-17 11:50:34 +01:00
|
|
|
property OnCreateURLRequest : TNotifyEvent read FOnCreateURLRequest write FOnCreateURLRequest;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{$IFDEF FPC}
|
|
|
|
procedure Register;
|
|
|
|
{$ENDIF}
|
|
|
|
|
2019-10-21 11:17:47 +02:00
|
|
|
// *********************************************************
|
|
|
|
// ********************** ATTENTION ! **********************
|
|
|
|
// *********************************************************
|
|
|
|
// ** **
|
|
|
|
// ** MANY OF THE EVENTS IN CEF4DELPHI COMPONENTS LIKE **
|
|
|
|
// ** TCHROMIUM, TFMXCHROMIUM OR TCEFAPPLICATION ARE **
|
|
|
|
// ** EXECUTED IN A CEF THREAD BY DEFAULT. **
|
|
|
|
// ** **
|
|
|
|
// ** WINDOWS CONTROLS MUST BE CREATED AND DESTROYED IN **
|
|
|
|
// ** THE SAME THREAD TO AVOID ERRORS. **
|
|
|
|
// ** SOME OF THEM RECREATE THE HANDLERS IF THEY ARE **
|
|
|
|
// ** MODIFIED AND CAN CAUSE THE SAME ERRORS. **
|
|
|
|
// ** **
|
|
|
|
// ** DON'T CREATE, MODIFY OR DESTROY WINDOWS CONTROLS **
|
|
|
|
// ** INSIDE THE CEF4DELPHI EVENTS AND USE **
|
|
|
|
// ** SYNCHRONIZATION OBJECTS TO PROTECT VARIABLES AND **
|
|
|
|
// ** FIELDS IF THEY ARE ALSO USED IN THE MAIN THREAD. **
|
|
|
|
// ** **
|
|
|
|
// ** READ THIS FOR MORE INFORMATION : **
|
|
|
|
// ** https://www.briskbard.com/index.php?pageid=cef **
|
|
|
|
// ** **
|
|
|
|
// ** USE OUR FORUMS FOR MORE QUESTIONS : **
|
|
|
|
// ** https://www.briskbard.com/forum/ **
|
|
|
|
// ** **
|
|
|
|
// *********************************************************
|
|
|
|
// *********************************************************
|
|
|
|
|
2018-11-17 11:50:34 +01:00
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
System.SysUtils,
|
|
|
|
{$ELSE}
|
|
|
|
SysUtils,
|
|
|
|
{$ENDIF}
|
2023-11-29 13:04:33 +01:00
|
|
|
uCEFRequest, uCEFTask, uCEFMiscFunctions, uCEFApplicationCore;
|
2018-11-17 11:50:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
constructor TCEFUrlRequestClientComponent.Create(AOwner: TComponent);
|
|
|
|
begin
|
|
|
|
inherited Create(AOwner);
|
|
|
|
|
|
|
|
FClient := nil;
|
|
|
|
FThreadID := TID_UI;
|
|
|
|
FOnRequestComplete := nil;
|
|
|
|
FOnUploadProgress := nil;
|
|
|
|
FOnDownloadProgress := nil;
|
|
|
|
FOnDownloadData := nil;
|
|
|
|
FOnGetAuthCredentials := nil;
|
|
|
|
FOnCreateURLRequest := nil;
|
2023-11-29 13:04:33 +01:00
|
|
|
FComponentID := 0;
|
2018-11-17 11:50:34 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFUrlRequestClientComponent.AfterConstruction;
|
|
|
|
begin
|
|
|
|
inherited AfterConstruction;
|
|
|
|
|
|
|
|
if not(csDesigning in ComponentState) then
|
|
|
|
FClient := TCustomCefUrlrequestClient.Create(self);
|
2023-11-29 13:04:33 +01:00
|
|
|
|
|
|
|
if assigned(GlobalCEFApp) then
|
|
|
|
FComponentID := GlobalCEFApp.NextComponentID;
|
2018-11-17 11:50:34 +01:00
|
|
|
end;
|
|
|
|
|
2018-12-06 11:49:08 +01:00
|
|
|
procedure TCEFUrlRequestClientComponent.BeforeDestruction;
|
|
|
|
begin
|
2023-11-29 13:04:33 +01:00
|
|
|
if assigned(GlobalCEFApp) then
|
|
|
|
GlobalCEFApp.RemoveComponentID(FComponentID);
|
|
|
|
|
2018-12-06 11:49:08 +01:00
|
|
|
DestroyRequestClient;
|
|
|
|
|
|
|
|
inherited BeforeDestruction;
|
|
|
|
end;
|
|
|
|
|
2023-11-29 13:04:33 +01:00
|
|
|
function TCEFUrlRequestClientComponent.GetComponentID : integer;
|
|
|
|
begin
|
|
|
|
Result := FComponentID;
|
|
|
|
end;
|
|
|
|
|
2018-12-06 11:49:08 +01:00
|
|
|
procedure TCEFUrlRequestClientComponent.DestroyRequestClient;
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
if (FClient <> nil) then
|
|
|
|
begin
|
|
|
|
FClient.RemoveReferences;
|
|
|
|
FClient := nil;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCEFUrlRequestClientComponent.DestroyRequestClient', e) then raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-11-17 11:50:34 +01:00
|
|
|
procedure TCEFUrlRequestClientComponent.doOnRequestComplete(const request: ICefUrlRequest);
|
|
|
|
begin
|
|
|
|
if assigned(FOnRequestComplete) then FOnRequestComplete(self, request);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFUrlRequestClientComponent.doOnUploadProgress(const request: ICefUrlRequest; current, total: Int64);
|
|
|
|
begin
|
|
|
|
if assigned(FOnUploadProgress) then FOnUploadProgress(self, request, current, total);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFUrlRequestClientComponent.doOnDownloadProgress(const request: ICefUrlRequest; current, total: Int64);
|
|
|
|
begin
|
|
|
|
if assigned(FOnDownloadProgress) then FOnDownloadProgress(self, request, current, total);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFUrlRequestClientComponent.doOnDownloadData(const request: ICefUrlRequest; data: Pointer; dataLength: NativeUInt);
|
|
|
|
begin
|
|
|
|
if assigned(FOnDownloadData) then FOnDownloadData(self, request, data, datalength);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCEFUrlRequestClientComponent.doOnGetAuthCredentials(isProxy: Boolean; const host: ustring; port: Integer; const realm, scheme: ustring; const callback: ICefAuthCallback): Boolean;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
|
|
|
|
if assigned(FOnGetAuthCredentials) then FOnGetAuthCredentials(self, isProxy, host, port, realm, scheme, callback, Result);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFUrlRequestClientComponent.doOnCreateURLRequest;
|
|
|
|
begin
|
|
|
|
if assigned(FOnCreateURLRequest) then FOnCreateURLRequest(self);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFUrlRequestClientComponent.AddURLRequest;
|
|
|
|
var
|
|
|
|
TempTask : ICefTask;
|
|
|
|
begin
|
|
|
|
TempTask := TCefURLRequestTask.Create(self);
|
|
|
|
CefPostTask(FThreadID, TempTask);
|
|
|
|
end;
|
|
|
|
|
|
|
|
{$IFDEF FPC}
|
|
|
|
procedure Register;
|
|
|
|
begin
|
|
|
|
{$I res/tcefurlrequestclientcomponent.lrs}
|
|
|
|
RegisterComponents('Chromium', [TCEFUrlRequestClientComponent]);
|
|
|
|
end;
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
end.
|