CEF4Delphi/source/uCEFDownloadHandler.pas

185 lines
7.2 KiB
ObjectPascal
Raw Normal View History

2017-01-27 16:37:51 +01:00
unit uCEFDownloadHandler;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
2017-02-05 20:56:46 +01:00
{$I cef.inc}
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
2017-01-27 16:37:51 +01:00
interface
uses
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
2017-01-27 16:37:51 +01:00
type
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;
procedure RemoveReferences; virtual;
2017-01-27 16:37:51 +01:00
public
constructor Create; virtual;
end;
TCustomDownloadHandler = class(TCefDownloadHandlerOwn)
protected
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;
procedure RemoveReferences; override;
2017-01-27 16:37:51 +01:00
public
constructor Create(const events : IChromiumEvents); reintroduce; virtual;
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));
2017-01-27 16:37:51 +01:00
with PCefDownloadHandler(FData)^ do
begin
2022-05-03 12:43:16 +02:00
can_download := {$IFDEF FPC}@{$ENDIF}cef_download_handler_can_download;
on_before_download := {$IFDEF FPC}@{$ENDIF}cef_download_handler_on_before_download;
on_download_updated := {$IFDEF FPC}@{$ENDIF}cef_download_handler_on_download_updated;
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;
procedure TCefDownloadHandlerOwn.RemoveReferences;
begin
//
end;
2017-01-27 16:37:51 +01:00
// TCustomDownloadHandler
constructor TCustomDownloadHandler.Create(const events : IChromiumEvents);
2017-01-27 16:37:51 +01:00
begin
inherited Create;
FEvents := Pointer(events);
2017-01-27 16:37:51 +01:00
end;
destructor TCustomDownloadHandler.Destroy;
begin
RemoveReferences;
inherited Destroy;
end;
procedure TCustomDownloadHandler.RemoveReferences;
2017-01-27 16:37:51 +01:00
begin
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;
const downloadItem : ICefDownloadItem;
const suggestedName : ustring;
2024-05-28 15:50:48 +02:00
const callback : ICefBeforeDownloadCallback): boolean;
begin
2024-05-28 15:50:48 +02:00
Result := (FEvents <> nil) and
IChromiumEvents(FEvents).doOnBeforeDownload(browser, downloadItem, suggestedName, callback);
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;
end.