2017-01-27 16:37:51 +01:00
|
|
|
|
// ************************************************************************
|
|
|
|
|
// ***************************** CEF4Delphi *******************************
|
|
|
|
|
// ************************************************************************
|
|
|
|
|
//
|
2019-10-19 10:58:34 +02:00
|
|
|
|
// CEF4Delphi is based on DCEF3 which uses CEF to embed a chromium-based
|
2017-01-27 16:37:51 +01:00
|
|
|
|
// browser in Delphi applications.
|
|
|
|
|
//
|
|
|
|
|
// The original license of DCEF3 still applies to CEF4Delphi.
|
|
|
|
|
//
|
|
|
|
|
// For more information about CEF4Delphi visit :
|
|
|
|
|
// https://www.briskbard.com/index.php?lang=en&pageid=cef
|
|
|
|
|
//
|
2022-01-01 10:41:21 +01:00
|
|
|
|
// Copyright <20> 2022 Salvador Diaz Fau. All rights reserved.
|
2017-01-27 16:37:51 +01:00
|
|
|
|
//
|
|
|
|
|
// ************************************************************************
|
|
|
|
|
// ************ vvvv Original license and comments below vvvv *************
|
|
|
|
|
// ************************************************************************
|
|
|
|
|
(*
|
|
|
|
|
* Delphi Chromium Embedded 3
|
|
|
|
|
*
|
|
|
|
|
* Usage allowed under the restrictions of the Lesser GNU General Public License
|
|
|
|
|
* or alternatively the restrictions of the Mozilla Public License 1.1
|
|
|
|
|
*
|
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
|
|
|
|
* the specific language governing rights and limitations under the License.
|
|
|
|
|
*
|
|
|
|
|
* Unit owner : Henri Gourvest <hgourvest@gmail.com>
|
|
|
|
|
* Web site : http://www.progdigy.com
|
|
|
|
|
* Repository : http://code.google.com/p/delphichromiumembedded/
|
|
|
|
|
* Group : http://groups.google.com/group/delphichromiumembedded
|
|
|
|
|
*
|
|
|
|
|
* Embarcadero Technologies, Inc is not permitted to use or redistribute
|
|
|
|
|
* this source code without explicit permission.
|
|
|
|
|
*
|
|
|
|
|
*)
|
|
|
|
|
|
|
|
|
|
unit uCEFRequestContextHandler;
|
2021-05-05 14:41:13 +02:00
|
|
|
|
|
|
|
|
|
{$IFDEF FPC}
|
2018-05-12 14:50:54 +02:00
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
|
{$ENDIF}
|
2017-01-27 16:37:51 +01:00
|
|
|
|
|
2017-02-05 20:56:46 +01:00
|
|
|
|
{$I cef.inc}
|
|
|
|
|
|
2022-02-19 18:56:41 +01:00
|
|
|
|
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
|
|
|
|
|
{$MINENUMSIZE 4}
|
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2021-05-05 14:41:13 +02:00
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
{$ELSE}
|
|
|
|
|
SysUtils,
|
|
|
|
|
{$ENDIF}
|
2017-03-16 19:09:42 +01:00
|
|
|
|
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
|
2017-01-27 16:37:51 +01:00
|
|
|
|
|
|
|
|
|
type
|
2017-03-16 19:09:42 +01:00
|
|
|
|
TCefRequestContextHandlerRef = class(TCefBaseRefCountedRef, ICefRequestContextHandler)
|
2017-01-27 16:37:51 +01:00
|
|
|
|
protected
|
2017-10-26 13:23:13 +02:00
|
|
|
|
procedure OnRequestContextInitialized(const request_context: ICefRequestContext);
|
2019-06-16 10:31:13 +02:00
|
|
|
|
procedure GetResourceRequestHandler(const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; is_navigation, is_download: boolean; const request_initiator: ustring; var disable_default_handling: boolean; var aResourceRequestHandler : ICefResourceRequestHandler);
|
2017-01-27 16:37:51 +01:00
|
|
|
|
|
2019-10-11 17:51:16 +02:00
|
|
|
|
procedure RemoveReferences; virtual;
|
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
|
public
|
|
|
|
|
class function UnWrap(data: Pointer): ICefRequestContextHandler;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-16 19:09:42 +01:00
|
|
|
|
TCefRequestContextHandlerOwn = class(TCefBaseRefCountedOwn, ICefRequestContextHandler)
|
2017-01-27 16:37:51 +01:00
|
|
|
|
protected
|
2019-06-16 10:31:13 +02:00
|
|
|
|
procedure OnRequestContextInitialized(const request_context: ICefRequestContext); virtual;
|
|
|
|
|
procedure GetResourceRequestHandler(const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; is_navigation, is_download: boolean; const request_initiator: ustring; var disable_default_handling: boolean; var aResourceRequestHandler : ICefResourceRequestHandler); virtual;
|
2017-01-27 16:37:51 +01:00
|
|
|
|
|
2019-10-11 17:51:16 +02:00
|
|
|
|
procedure RemoveReferences; virtual;
|
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
|
public
|
|
|
|
|
constructor Create; virtual;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-10-11 17:51:16 +02:00
|
|
|
|
TCustomRequestContextHandler = class(TCefRequestContextHandlerOwn)
|
|
|
|
|
protected
|
|
|
|
|
FEvents : Pointer;
|
|
|
|
|
|
|
|
|
|
procedure OnRequestContextInitialized(const request_context: ICefRequestContext); override;
|
|
|
|
|
procedure GetResourceRequestHandler(const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; is_navigation, is_download: boolean; const request_initiator: ustring; var disable_default_handling: boolean; var aResourceRequestHandler : ICefResourceRequestHandler); override;
|
|
|
|
|
|
|
|
|
|
public
|
|
|
|
|
constructor Create(const events : IChromiumEvents); reintroduce; virtual;
|
|
|
|
|
procedure BeforeDestruction; override;
|
|
|
|
|
procedure RemoveReferences; override;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
2019-06-16 10:31:13 +02:00
|
|
|
|
uCEFMiscFunctions, uCEFLibFunctions, uCEFBrowser, uCEFFrame, uCEFRequest,
|
2022-04-06 12:13:07 +02:00
|
|
|
|
uCEFRequestContext, uCEFResourceRequestHandler;
|
2017-10-26 13:23:13 +02:00
|
|
|
|
|
|
|
|
|
// TCefRequestContextHandlerOwn
|
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
|
procedure cef_request_context_handler_on_request_context_initialized(self : PCefRequestContextHandler;
|
|
|
|
|
request_context : PCefRequestContext); stdcall;
|
|
|
|
|
var
|
|
|
|
|
TempObject : TObject;
|
2017-10-26 13:23:13 +02:00
|
|
|
|
begin
|
2021-05-05 14:41:13 +02:00
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefRequestContextHandlerOwn) then
|
2021-05-05 14:41:13 +02:00
|
|
|
|
TCefRequestContextHandlerOwn(TempObject).OnRequestContextInitialized(TCefRequestContextRef.UnWrap(request_context));
|
2017-10-26 13:23:13 +02:00
|
|
|
|
end;
|
2017-01-27 16:37:51 +01:00
|
|
|
|
|
2019-06-16 10:31:13 +02:00
|
|
|
|
function cef_request_context_handler_get_resource_request_handler( self : PCefRequestContextHandler;
|
|
|
|
|
browser : PCefBrowser;
|
|
|
|
|
frame : PCefFrame;
|
|
|
|
|
request : PCefRequest;
|
|
|
|
|
is_navigation : Integer;
|
|
|
|
|
is_download : Integer;
|
|
|
|
|
const request_initiator : PCefString;
|
|
|
|
|
disable_default_handling : PInteger): PCefResourceRequestHandler; stdcall;
|
|
|
|
|
var
|
|
|
|
|
TempObject : TObject;
|
|
|
|
|
TempDisableDefHandling : Boolean;
|
|
|
|
|
TempResourceRequestHandler : ICefResourceRequestHandler;
|
|
|
|
|
begin
|
|
|
|
|
Result := nil;
|
|
|
|
|
TempResourceRequestHandler := nil;
|
2021-05-05 14:41:13 +02:00
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
TempDisableDefHandling := disable_default_handling^ <> 0;
|
|
|
|
|
|
2019-06-16 10:31:13 +02:00
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefRequestContextHandlerOwn) then
|
|
|
|
|
try
|
|
|
|
|
TCefRequestContextHandlerOwn(TempObject).GetResourceRequestHandler(TCefBrowserRef.UnWrap(browser),
|
|
|
|
|
TCefFrameRef.UnWrap(frame),
|
|
|
|
|
TCefRequestRef.UnWrap(request),
|
|
|
|
|
is_navigation <> 0,
|
|
|
|
|
is_download <> 0,
|
|
|
|
|
CefString(request_initiator),
|
|
|
|
|
TempDisableDefHandling,
|
|
|
|
|
TempResourceRequestHandler);
|
|
|
|
|
|
|
|
|
|
Result := CefGetData(TempResourceRequestHandler);
|
|
|
|
|
disable_default_handling^ := Ord(TempDisableDefHandling);
|
|
|
|
|
finally
|
|
|
|
|
TempResourceRequestHandler := nil;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-10-26 13:23:13 +02:00
|
|
|
|
constructor TCefRequestContextHandlerOwn.Create;
|
|
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
|
inherited CreateData(SizeOf(TCefRequestContextHandler));
|
2017-10-26 13:23:13 +02:00
|
|
|
|
|
|
|
|
|
with PCefRequestContextHandler(FData)^ do
|
|
|
|
|
begin
|
2018-05-12 14:50:54 +02:00
|
|
|
|
on_request_context_initialized := {$IFDEF FPC}@{$ENDIF}cef_request_context_handler_on_request_context_initialized;
|
2019-06-16 10:31:13 +02:00
|
|
|
|
get_resource_request_handler := {$IFDEF FPC}@{$ENDIF}cef_request_context_handler_get_resource_request_handler;
|
2017-10-26 13:23:13 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCefRequestContextHandlerOwn.OnRequestContextInitialized(const request_context: ICefRequestContext);
|
|
|
|
|
begin
|
2021-05-05 14:41:13 +02:00
|
|
|
|
//
|
2017-10-26 13:23:13 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-06-16 10:31:13 +02:00
|
|
|
|
procedure TCefRequestContextHandlerOwn.GetResourceRequestHandler(const browser : ICefBrowser;
|
|
|
|
|
const frame : ICefFrame;
|
|
|
|
|
const request : ICefRequest;
|
|
|
|
|
is_navigation : boolean;
|
|
|
|
|
is_download : boolean;
|
|
|
|
|
const request_initiator : ustring;
|
|
|
|
|
var disable_default_handling : boolean;
|
|
|
|
|
var aResourceRequestHandler : ICefResourceRequestHandler);
|
|
|
|
|
begin
|
2021-05-05 14:41:13 +02:00
|
|
|
|
aResourceRequestHandler := nil;
|
2019-06-16 10:31:13 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-10-11 17:51:16 +02:00
|
|
|
|
procedure TCefRequestContextHandlerOwn.RemoveReferences;
|
|
|
|
|
begin
|
2021-05-05 14:41:13 +02:00
|
|
|
|
//
|
2019-10-11 17:51:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
2017-10-26 13:23:13 +02:00
|
|
|
|
// TCefRequestContextHandlerRef
|
|
|
|
|
|
|
|
|
|
procedure TCefRequestContextHandlerRef.OnRequestContextInitialized(const request_context: ICefRequestContext);
|
|
|
|
|
begin
|
2021-05-05 14:41:13 +02:00
|
|
|
|
|
|
|
|
|
PCefRequestContextHandler(FData)^.on_request_context_initialized(PCefRequestContextHandler(FData), CefGetData(request_context));
|
2017-10-26 13:23:13 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-06-16 10:31:13 +02:00
|
|
|
|
procedure TCefRequestContextHandlerRef.GetResourceRequestHandler(const browser : ICefBrowser;
|
|
|
|
|
const frame : ICefFrame;
|
|
|
|
|
const request : ICefRequest;
|
|
|
|
|
is_navigation : boolean;
|
|
|
|
|
is_download : boolean;
|
|
|
|
|
const request_initiator : ustring;
|
|
|
|
|
var disable_default_handling : boolean;
|
|
|
|
|
var aResourceRequestHandler : ICefResourceRequestHandler);
|
|
|
|
|
var
|
|
|
|
|
TempRequestInitiator : TCefString;
|
|
|
|
|
TempDisableDefaultHandling : integer;
|
|
|
|
|
TempResourceRequestHandler : PCefResourceRequestHandler;
|
|
|
|
|
begin
|
|
|
|
|
TempRequestInitiator := CefString(request_initiator);
|
2021-05-05 14:41:13 +02:00
|
|
|
|
TempDisableDefaultHandling := ord(disable_default_handling);
|
|
|
|
|
TempResourceRequestHandler := PCefRequestContextHandler(FData)^.get_resource_request_handler(PCefRequestContextHandler(FData),
|
|
|
|
|
|
|
|
|
|
CefGetData(browser),
|
|
|
|
|
CefGetData(frame),
|
|
|
|
|
CefGetData(request),
|
|
|
|
|
ord(is_navigation),
|
|
|
|
|
ord(is_download),
|
|
|
|
|
@TempRequestInitiator,
|
2019-06-16 10:31:13 +02:00
|
|
|
|
@TempDisableDefaultHandling);
|
2021-05-05 14:41:13 +02:00
|
|
|
|
|
|
|
|
|
disable_default_handling := TempDisableDefaultHandling <> 0;
|
|
|
|
|
|
|
|
|
|
if (TempResourceRequestHandler <> nil) then
|
|
|
|
|
aResourceRequestHandler := TCefResourceRequestHandlerRef.UnWrap(TempResourceRequestHandler)
|
2019-06-16 10:31:13 +02:00
|
|
|
|
else
|
|
|
|
|
aResourceRequestHandler := nil;
|
2021-05-05 14:41:13 +02:00
|
|
|
|
end;
|
2019-06-16 10:31:13 +02:00
|
|
|
|
|
2019-10-11 17:51:16 +02:00
|
|
|
|
procedure TCefRequestContextHandlerRef.RemoveReferences;
|
|
|
|
|
begin
|
2021-05-05 14:41:13 +02:00
|
|
|
|
//
|
2019-10-11 17:51:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
|
class function TCefRequestContextHandlerRef.UnWrap(data: Pointer): ICefRequestContextHandler;
|
|
|
|
|
begin
|
|
|
|
|
if (data <> nil) then
|
|
|
|
|
Result := Create(data) as ICefRequestContextHandler
|
|
|
|
|
else
|
|
|
|
|
Result := nil;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-10-11 17:51:16 +02:00
|
|
|
|
|
|
|
|
|
// TCustomRequestContextHandler
|
|
|
|
|
|
|
|
|
|
constructor TCustomRequestContextHandler.Create(const events : IChromiumEvents);
|
|
|
|
|
begin
|
|
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
|
|
FEvents := Pointer(events);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCustomRequestContextHandler.BeforeDestruction;
|
|
|
|
|
begin
|
|
|
|
|
FEvents := nil;
|
|
|
|
|
|
|
|
|
|
inherited BeforeDestruction;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCustomRequestContextHandler.RemoveReferences;
|
|
|
|
|
begin
|
|
|
|
|
FEvents := nil;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCustomRequestContextHandler.OnRequestContextInitialized(const request_context: ICefRequestContext);
|
|
|
|
|
begin
|
2021-05-05 14:41:13 +02:00
|
|
|
|
try
|
|
|
|
|
if (FEvents <> nil) then
|
|
|
|
|
IChromiumEvents(FEvents).doOnRequestContextInitialized(request_context);
|
|
|
|
|
except
|
|
|
|
|
on e : exception do
|
|
|
|
|
if CustomExceptionHandler('TCustomRequestContextHandler.OnRequestContextInitialized', e) then raise;
|
|
|
|
|
end;
|
2019-10-11 17:51:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCustomRequestContextHandler.GetResourceRequestHandler(const browser : ICefBrowser;
|
|
|
|
|
const frame : ICefFrame;
|
|
|
|
|
const request : ICefRequest;
|
|
|
|
|
is_navigation : boolean;
|
|
|
|
|
is_download : boolean;
|
|
|
|
|
const request_initiator : ustring;
|
|
|
|
|
var disable_default_handling : boolean;
|
|
|
|
|
var aResourceRequestHandler : ICefResourceRequestHandler);
|
|
|
|
|
begin
|
2021-05-05 14:41:13 +02:00
|
|
|
|
try
|
|
|
|
|
if (FEvents <> nil) then
|
|
|
|
|
IChromiumEvents(FEvents).doGetResourceRequestHandler_ReqCtxHdlr(browser,
|
|
|
|
|
frame,
|
|
|
|
|
request,
|
|
|
|
|
is_navigation,
|
|
|
|
|
is_download,
|
|
|
|
|
request_initiator,
|
|
|
|
|
disable_default_handling,
|
|
|
|
|
aResourceRequestHandler);
|
|
|
|
|
except
|
|
|
|
|
on e : exception do
|
|
|
|
|
if CustomExceptionHandler('TCustomRequestContextHandler.GetResourceRequestHandler', e) then raise;
|
|
|
|
|
end;
|
2019-10-11 17:51:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
|
end.
|