2017-01-27 16:37:51 +01:00
|
|
|
unit uCEFDownloadHandler;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
|
|
|
|
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
|
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
|
|
|
TCefDownloadHandlerOwn = class(TCefBaseRefCountedOwn, ICefDownloadHandler)
|
2017-01-27 16:37:51 +01:00
|
|
|
protected
|
2022-05-03 12:43:16 +02:00
|
|
|
function CanDownload(const browser: ICefBrowser; const url, request_method: ustring): boolean; virtual;
|
2024-05-28 15:50:48 +02:00
|
|
|
function OnBeforeDownload(const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const suggestedName: ustring; const callback: ICefBeforeDownloadCallback): boolean; virtual;
|
2017-01-27 16:37:51 +01:00
|
|
|
procedure OnDownloadUpdated(const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const callback: ICefDownloadItemCallback); virtual;
|
|
|
|
|
2018-02-03 17:52:48 +01:00
|
|
|
procedure RemoveReferences; virtual;
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TCustomDownloadHandler = class(TCefDownloadHandlerOwn)
|
|
|
|
protected
|
2018-02-03 17:52:48 +01:00
|
|
|
FEvents : Pointer;
|
2017-01-27 16:37:51 +01:00
|
|
|
|
2022-05-03 12:43:16 +02:00
|
|
|
function CanDownload(const browser: ICefBrowser; const url, request_method: ustring): boolean; override;
|
2024-05-28 15:50:48 +02:00
|
|
|
function OnBeforeDownload(const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const suggestedName: ustring; const callback: ICefBeforeDownloadCallback): boolean; override;
|
2017-01-27 16:37:51 +01:00
|
|
|
procedure OnDownloadUpdated(const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const callback: ICefDownloadItemCallback); override;
|
|
|
|
|
2018-02-03 17:52:48 +01:00
|
|
|
procedure RemoveReferences; override;
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
public
|
2019-06-19 16:53:26 +02:00
|
|
|
constructor Create(const events : IChromiumEvents); reintroduce; virtual;
|
2017-06-11 17:48:20 +02:00
|
|
|
destructor Destroy; override;
|
2017-01-27 16:37:51 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2023-09-26 14:52:33 +02:00
|
|
|
uCEFMiscFunctions, uCEFLibFunctions, uCEFBrowser, uCEFDownloadItem, uCEFBeforeDownloadCallback,
|
2017-01-27 16:37:51 +01:00
|
|
|
uCEFDownloadItemCallback;
|
|
|
|
|
2022-05-03 12:43:16 +02:00
|
|
|
function cef_download_handler_can_download( self : PCefDownloadHandler;
|
|
|
|
browser : PCefBrowser;
|
|
|
|
const url : PCefString;
|
|
|
|
const request_method : PCefString): integer; stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
begin
|
|
|
|
Result := Ord(True);
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefDownloadHandlerOwn) then
|
|
|
|
Result := Ord(TCefDownloadHandlerOwn(TempObject).CanDownload(TCefBrowserRef.UnWrap(browser),
|
|
|
|
CefString(url),
|
|
|
|
CefString(request_method)));
|
|
|
|
end;
|
|
|
|
|
2024-05-28 15:50:48 +02:00
|
|
|
function cef_download_handler_on_before_download( self : PCefDownloadHandler;
|
2018-03-29 20:02:04 +02:00
|
|
|
browser : PCefBrowser;
|
|
|
|
download_item : PCefDownloadItem;
|
|
|
|
const suggested_name : PCefString;
|
2024-05-28 15:50:48 +02:00
|
|
|
callback : PCefBeforeDownloadCallback): Integer; stdcall;
|
2018-03-29 20:02:04 +02:00
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 16:37:51 +01:00
|
|
|
begin
|
2024-05-28 15:50:48 +02:00
|
|
|
Result := Ord(False);
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefDownloadHandlerOwn) then
|
2024-05-28 15:50:48 +02:00
|
|
|
Result := Ord(TCefDownloadHandlerOwn(TempObject).OnBeforeDownload(TCefBrowserRef.UnWrap(browser),
|
|
|
|
TCefDownLoadItemRef.UnWrap(download_item),
|
|
|
|
CefString(suggested_name),
|
|
|
|
TCefBeforeDownloadCallbackRef.UnWrap(callback)));
|
2017-01-27 16:37:51 +01:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_download_handler_on_download_updated(self : PCefDownloadHandler;
|
|
|
|
browser : PCefBrowser;
|
|
|
|
download_item : PCefDownloadItem;
|
|
|
|
callback : PCefDownloadItemCallback); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 16:37:51 +01:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefDownloadHandlerOwn) then
|
|
|
|
TCefDownloadHandlerOwn(TempObject).OnDownloadUpdated(TCefBrowserRef.UnWrap(browser),
|
|
|
|
TCefDownLoadItemRef.UnWrap(download_item),
|
|
|
|
TCefDownloadItemCallbackRef.UnWrap(callback));
|
2017-01-27 16:37:51 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TCefDownloadHandlerOwn.Create;
|
|
|
|
begin
|
|
|
|
inherited CreateData(SizeOf(TCefDownloadHandler));
|
2018-02-03 17:52:48 +01:00
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
with PCefDownloadHandler(FData)^ do
|
2018-02-03 17:52:48 +01:00
|
|
|
begin
|
2022-05-03 12:43:16 +02:00
|
|
|
can_download := {$IFDEF FPC}@{$ENDIF}cef_download_handler_can_download;
|
2018-05-12 14:50:54 +02:00
|
|
|
on_before_download := {$IFDEF FPC}@{$ENDIF}cef_download_handler_on_before_download;
|
|
|
|
on_download_updated := {$IFDEF FPC}@{$ENDIF}cef_download_handler_on_download_updated;
|
2018-02-03 17:52:48 +01:00
|
|
|
end;
|
2017-01-27 16:37:51 +01:00
|
|
|
end;
|
|
|
|
|
2022-05-03 12:43:16 +02:00
|
|
|
function TCefDownloadHandlerOwn.CanDownload(const browser: ICefBrowser; const url, request_method: ustring): boolean;
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
2024-05-28 15:50:48 +02:00
|
|
|
function TCefDownloadHandlerOwn.OnBeforeDownload(const browser : ICefBrowser;
|
2018-03-29 20:02:04 +02:00
|
|
|
const downloadItem : ICefDownloadItem;
|
|
|
|
const suggestedName : ustring;
|
2024-05-28 15:50:48 +02:00
|
|
|
const callback : ICefBeforeDownloadCallback): boolean;
|
2017-01-27 16:37:51 +01:00
|
|
|
begin
|
2024-05-28 15:50:48 +02:00
|
|
|
Result := False;
|
2017-01-27 16:37:51 +01:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure TCefDownloadHandlerOwn.OnDownloadUpdated(const browser : ICefBrowser;
|
|
|
|
const downloadItem : ICefDownloadItem;
|
|
|
|
const callback : ICefDownloadItemCallback);
|
2017-01-27 16:37:51 +01:00
|
|
|
begin
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2018-02-03 17:52:48 +01:00
|
|
|
procedure TCefDownloadHandlerOwn.RemoveReferences;
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
2017-01-27 16:37:51 +01:00
|
|
|
// TCustomDownloadHandler
|
|
|
|
|
2019-06-19 16:53:26 +02:00
|
|
|
constructor TCustomDownloadHandler.Create(const events : IChromiumEvents);
|
2017-01-27 16:37:51 +01:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2017-06-11 17:48:20 +02:00
|
|
|
|
2019-06-19 16:53:26 +02:00
|
|
|
FEvents := Pointer(events);
|
2017-01-27 16:37:51 +01:00
|
|
|
end;
|
|
|
|
|
2017-06-11 17:48:20 +02:00
|
|
|
destructor TCustomDownloadHandler.Destroy;
|
|
|
|
begin
|
2018-02-03 17:52:48 +01:00
|
|
|
RemoveReferences;
|
2017-06-11 17:48:20 +02:00
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2018-02-03 17:52:48 +01:00
|
|
|
procedure TCustomDownloadHandler.RemoveReferences;
|
2017-01-27 16:37:51 +01:00
|
|
|
begin
|
2018-02-03 17:52:48 +01:00
|
|
|
FEvents := nil;
|
2017-01-27 16:37:51 +01:00
|
|
|
end;
|
|
|
|
|
2022-05-03 12:43:16 +02:00
|
|
|
function TCustomDownloadHandler.CanDownload(const browser : ICefBrowser;
|
|
|
|
const url : ustring;
|
|
|
|
const request_method : ustring): boolean;
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
|
|
|
|
if (FEvents <> nil) then
|
|
|
|
Result := IChromiumEvents(FEvents).doOnCanDownload(browser, url, request_method);
|
|
|
|
end;
|
|
|
|
|
2024-05-28 15:50:48 +02:00
|
|
|
function TCustomDownloadHandler.OnBeforeDownload(const browser : ICefBrowser;
|
2018-02-03 17:52:48 +01:00
|
|
|
const downloadItem : ICefDownloadItem;
|
|
|
|
const suggestedName : ustring;
|
2024-05-28 15:50:48 +02:00
|
|
|
const callback : ICefBeforeDownloadCallback): boolean;
|
2018-02-03 17:52:48 +01:00
|
|
|
begin
|
2024-05-28 15:50:48 +02:00
|
|
|
Result := (FEvents <> nil) and
|
|
|
|
IChromiumEvents(FEvents).doOnBeforeDownload(browser, downloadItem, suggestedName, callback);
|
2018-02-03 17:52:48 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomDownloadHandler.OnDownloadUpdated(const browser : ICefBrowser;
|
|
|
|
const downloadItem : ICefDownloadItem;
|
|
|
|
const callback : ICefDownloadItemCallback);
|
2017-01-27 16:37:51 +01:00
|
|
|
begin
|
2022-05-03 12:43:16 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IChromiumEvents(FEvents).doOnDownloadUpdated(browser, downloadItem, callback);
|
2017-01-27 16:37:51 +01:00
|
|
|
end;
|
|
|
|
|
2024-05-01 18:19:19 +02:00
|
|
|
end.
|