CEF4Delphi/source/uCEFJsDialogHandler.pas

269 lines
11 KiB
ObjectPascal
Raw Normal View History

2017-01-27 16:37:51 +01:00
// ************************************************************************
// ***************************** CEF4Delphi *******************************
// ************************************************************************
//
// 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
//
// 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 uCEFJsDialogHandler;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
{$IFNDEF CPUX64}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
2017-01-27 16:37:51 +01:00
2017-02-05 20:56:46 +01:00
{$I cef.inc}
2017-01-27 16:37:51 +01:00
interface
uses
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
2017-01-27 16:37:51 +01:00
type
TCefJsDialogHandlerOwn = class(TCefBaseRefCountedOwn, ICefJsDialogHandler)
2017-01-27 16:37:51 +01:00
protected
function OnJsdialog(const browser: ICefBrowser; const originUrl: ustring; dialogType: TCefJsDialogType; const messageText, defaultPromptText: ustring; const callback: ICefJsDialogCallback; out suppressMessage: Boolean): Boolean; virtual;
function OnBeforeUnloadDialog(const browser: ICefBrowser; const messageText: ustring; isReload: Boolean; const callback: ICefJsDialogCallback): Boolean; virtual;
2017-01-27 16:37:51 +01:00
procedure OnResetDialogState(const browser: ICefBrowser); virtual;
procedure OnDialogClosed(const browser: ICefBrowser); virtual;
procedure RemoveReferences; virtual;
2017-01-27 16:37:51 +01:00
public
constructor Create; virtual;
end;
TCustomJsDialogHandler = class(TCefJsDialogHandlerOwn)
protected
FEvents : Pointer;
2017-01-27 16:37:51 +01:00
function OnJsdialog(const browser: ICefBrowser; const originUrl: ustring; dialogType: TCefJsDialogType; const messageText, defaultPromptText: ustring; const callback: ICefJsDialogCallback; out suppressMessage: Boolean): Boolean; override;
function OnBeforeUnloadDialog(const browser: ICefBrowser; const messageText: ustring; isReload: Boolean; const callback: ICefJsDialogCallback): Boolean; override;
procedure OnResetDialogState(const browser: ICefBrowser); override;
procedure OnDialogClosed(const browser: ICefBrowser); 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
{$IFDEF DELPHI16_UP}
System.SysUtils,
{$ELSE}
SysUtils,
{$ENDIF}
2017-01-27 16:37:51 +01:00
uCEFMiscFunctions, uCEFLibFunctions, uCEFBrowser, uCEFJsDialogCallback;
2018-03-29 20:02:04 +02:00
function cef_jsdialog_handler_on_jsdialog( self : PCefJsDialogHandler;
browser : PCefBrowser;
const origin_url : PCefString;
dialog_type : TCefJsDialogType;
const message_text : PCefString;
const default_prompt_text : PCefString;
callback : PCefJsDialogCallback;
suppress_message : PInteger): Integer; stdcall;
2017-01-27 16:37:51 +01:00
var
2018-03-29 20:02:04 +02:00
TempSuppress : Boolean;
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2018-03-29 20:02:04 +02:00
Result := Ord(False);
TempSuppress := suppress_message^ <> 0;
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefJsDialogHandlerOwn) then
Result := Ord(TCefJsDialogHandlerOwn(TempObject).OnJsdialog(TCefBrowserRef.UnWrap(browser),
CefString(origin_url),
dialog_type,
CefString(message_text),
CefString(default_prompt_text),
TCefJsDialogCallbackRef.UnWrap(callback),
TempSuppress));
suppress_message^ := Ord(TempSuppress);
2017-01-27 16:37:51 +01:00
end;
2018-03-29 20:02:04 +02:00
function cef_jsdialog_handler_on_before_unload_dialog( self : PCefJsDialogHandler;
browser : PCefBrowser;
const message_text : PCefString;
is_reload : Integer;
callback : PCefJsDialogCallback): Integer; stdcall;
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2018-03-29 20:02:04 +02:00
Result := Ord(False);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefJsDialogHandlerOwn) then
Result := Ord(TCefJsDialogHandlerOwn(TempObject).OnBeforeUnloadDialog(TCefBrowserRef.UnWrap(browser),
CefString(message_text),
is_reload <> 0,
TCefJsDialogCallbackRef.UnWrap(callback)));
2017-01-27 16:37:51 +01:00
end;
2018-03-29 20:02:04 +02:00
procedure cef_jsdialog_handler_on_reset_dialog_state(self : PCefJsDialogHandler;
browser : PCefBrowser); 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 TCefJsDialogHandlerOwn) then
TCefJsDialogHandlerOwn(TempObject).OnResetDialogState(TCefBrowserRef.UnWrap(browser));
2017-01-27 16:37:51 +01:00
end;
2018-03-29 20:02:04 +02:00
procedure cef_jsdialog_handler_on_dialog_closed(self : PCefJsDialogHandler;
browser : PCefBrowser); 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 TCefJsDialogHandlerOwn) then
TCefJsDialogHandlerOwn(TempObject).OnDialogClosed(TCefBrowserRef.UnWrap(browser));
2017-01-27 16:37:51 +01:00
end;
constructor TCefJsDialogHandlerOwn.Create;
begin
inherited CreateData(SizeOf(TCefJsDialogHandler));
2018-03-29 20:02:04 +02:00
2017-01-27 16:37:51 +01:00
with PCefJsDialogHandler(FData)^ do
2018-03-29 20:02:04 +02:00
begin
on_jsdialog := {$IFDEF FPC}@{$ENDIF}cef_jsdialog_handler_on_jsdialog;
on_before_unload_dialog := {$IFDEF FPC}@{$ENDIF}cef_jsdialog_handler_on_before_unload_dialog;
on_reset_dialog_state := {$IFDEF FPC}@{$ENDIF}cef_jsdialog_handler_on_reset_dialog_state;
on_dialog_closed := {$IFDEF FPC}@{$ENDIF}cef_jsdialog_handler_on_dialog_closed;
2018-03-29 20:02:04 +02:00
end;
2017-01-27 16:37:51 +01:00
end;
2018-03-29 20:02:04 +02:00
function TCefJsDialogHandlerOwn.OnJsdialog(const browser : ICefBrowser;
const originUrl : ustring;
dialogType : TCefJsDialogType;
const messageText : ustring;
const defaultPromptText : ustring;
const callback : ICefJsDialogCallback;
out suppressMessage : Boolean): Boolean;
2017-01-27 16:37:51 +01:00
begin
Result := False;
suppressMessage := False;
2017-01-27 16:37:51 +01:00
end;
2018-03-29 20:02:04 +02:00
function TCefJsDialogHandlerOwn.OnBeforeUnloadDialog(const browser : ICefBrowser;
const messageText : ustring;
isReload : Boolean;
const callback : ICefJsDialogCallback): Boolean;
2017-01-27 16:37:51 +01:00
begin
Result := False;
end;
procedure TCefJsDialogHandlerOwn.OnDialogClosed(const browser: ICefBrowser);
begin
2018-03-29 20:02:04 +02:00
//
2017-01-27 16:37:51 +01:00
end;
procedure TCefJsDialogHandlerOwn.OnResetDialogState(const browser: ICefBrowser);
begin
2018-03-29 20:02:04 +02:00
//
2017-01-27 16:37:51 +01:00
end;
procedure TCefJsDialogHandlerOwn.RemoveReferences;
begin
//
end;
2017-01-27 16:37:51 +01:00
// TCustomJsDialogHandler
constructor TCustomJsDialogHandler.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 TCustomJsDialogHandler.Destroy;
begin
RemoveReferences;
inherited Destroy;
end;
procedure TCustomJsDialogHandler.RemoveReferences;
begin
FEvents := nil;
end;
function TCustomJsDialogHandler.OnBeforeUnloadDialog(const browser : ICefBrowser;
const messageText : ustring;
isReload : Boolean;
const callback : ICefJsDialogCallback): Boolean;
2017-01-27 16:37:51 +01:00
begin
if (FEvents <> nil) then
Result := IChromiumEvents(FEvents).doOnBeforeUnloadDialog(browser, messageText, isReload, callback)
else
Result := inherited OnBeforeUnloadDialog(browser, messageText, isReload, callback);
2017-01-27 16:37:51 +01:00
end;
procedure TCustomJsDialogHandler.OnDialogClosed(const browser: ICefBrowser);
begin
if (FEvents <> nil) then IChromiumEvents(FEvents).doOnDialogClosed(browser);
2017-01-27 16:37:51 +01:00
end;
function TCustomJsDialogHandler.OnJsdialog(const browser : ICefBrowser;
const originUrl : ustring;
dialogType : TCefJsDialogType;
const messageText : ustring;
const defaultPromptText : ustring;
const callback : ICefJsDialogCallback;
out suppressMessage : Boolean): Boolean;
2017-01-27 16:37:51 +01:00
begin
suppressMessage := False;
if (FEvents <> nil) then
Result := IChromiumEvents(FEvents).doOnJsdialog(browser, originUrl, dialogType, messageText, defaultPromptText, callback, suppressMessage)
else
Result := inherited OnJsdialog(browser, originUrl, dialogType, messageText, defaultPromptText, callback, suppressMessage);
2017-01-27 16:37:51 +01:00
end;
procedure TCustomJsDialogHandler.OnResetDialogState(const browser: ICefBrowser);
begin
if (FEvents <> nil) then IChromiumEvents(FEvents).doOnResetDialogState(browser);
2017-01-27 16:37:51 +01:00
end;
end.