CEF4Delphi/source/uCEFLifeSpanHandler.pas

302 lines
12 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
//
2021-01-01 11:11:30 +01:00
// Copyright <20> 2021 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 uCEFLifeSpanHandler;
{$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
TCefLifeSpanHandlerOwn = class(TCefBaseRefCountedOwn, ICefLifeSpanHandler)
2017-01-27 16:37:51 +01:00
protected
2019-06-16 10:31:13 +02:00
function OnBeforePopup(const browser: ICefBrowser; const frame: ICefFrame; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean): Boolean; virtual;
2017-01-27 16:37:51 +01:00
procedure OnAfterCreated(const browser: ICefBrowser); virtual;
function DoClose(const browser: ICefBrowser): Boolean; virtual;
2018-03-29 20:02:04 +02:00
procedure OnBeforeClose(const browser: ICefBrowser); virtual;
2017-01-27 16:37:51 +01:00
procedure RemoveReferences; virtual;
2017-01-27 16:37:51 +01:00
public
constructor Create; virtual;
end;
TCustomLifeSpanHandler = class(TCefLifeSpanHandlerOwn)
protected
FEvents : Pointer;
2017-01-27 16:37:51 +01:00
2019-06-16 10:31:13 +02:00
function OnBeforePopup(const browser: ICefBrowser; const frame: ICefFrame; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean): Boolean; override;
2017-01-27 16:37:51 +01:00
procedure OnAfterCreated(const browser: ICefBrowser); override;
function DoClose(const browser: ICefBrowser): Boolean; override;
2018-03-29 20:02:04 +02:00
procedure OnBeforeClose(const browser: ICefBrowser); override;
2017-01-27 16:37:51 +01:00
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}
2019-06-16 10:31:13 +02:00
uCEFMiscFunctions, uCEFLibFunctions, uCEFClient, uCEFBrowser, uCEFFrame, uCEFDictionaryValue;
2017-01-27 16:37:51 +01:00
function cef_life_span_handler_on_before_popup( self : PCefLifeSpanHandler;
browser : PCefBrowser;
frame : PCefFrame;
const target_url : PCefString;
const target_frame_name : PCefString;
target_disposition : TCefWindowOpenDisposition;
user_gesture : Integer;
const popupFeatures : PCefPopupFeatures;
windowInfo : PCefWindowInfo;
var client : PCefClient;
settings : PCefBrowserSettings;
2019-06-16 10:31:13 +02:00
var extra_info : PCefDictionaryValue;
no_javascript_access : PInteger): Integer; stdcall;
2017-01-27 16:37:51 +01:00
var
2019-06-16 10:31:13 +02:00
TempClient : ICefClient;
TempExtraInfo : ICefDictionaryValue;
TempNoJS : boolean;
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2018-03-29 20:02:04 +02:00
try
Result := Ord(False);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefLifeSpanHandlerOwn) then
begin
2019-06-16 10:31:13 +02:00
TempNoJS := (no_javascript_access^ <> 0);
TempClient := TCefClientRef.UnWrap(client);
TempExtraInfo := TCefDictionaryValueRef.UnWrap(extra_info);
Result := Ord(TCefLifeSpanHandlerOwn(TempObject).OnBeforePopup(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
CefString(target_url),
CefString(target_frame_name),
target_disposition,
user_gesture <> 0,
popupFeatures^,
windowInfo^,
TempClient,
settings^,
TempExtraInfo,
TempNoJS));
2018-03-29 20:02:04 +02:00
no_javascript_access^ := Ord(TempNoJS);
if (TempClient = nil) then
client := nil
else
if not(TempClient.SameAs(client)) then
client := TempClient.Wrap;
2019-06-16 10:31:13 +02:00
if (TempExtraInfo = nil) then
extra_info := nil
else
if not(TempExtraInfo.SameAs(extra_info)) then
extra_info := TempExtraInfo.Wrap;
2018-03-29 20:02:04 +02:00
end;
finally
2019-06-16 10:31:13 +02:00
TempClient := nil;
TempExtraInfo := nil;
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
procedure cef_life_span_handler_on_after_created(self : PCefLifeSpanHandler;
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 TCefLifeSpanHandlerOwn) then
TCefLifeSpanHandlerOwn(TempObject).OnAfterCreated(TCefBrowserRef.UnWrap(browser));
2017-01-27 16:37:51 +01:00
end;
2018-03-29 20:02:04 +02:00
procedure cef_life_span_handler_on_before_close(self : PCefLifeSpanHandler;
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 TCefLifeSpanHandlerOwn) then
TCefLifeSpanHandlerOwn(TempObject).OnBeforeClose(TCefBrowserRef.UnWrap(browser));
2017-01-27 16:37:51 +01:00
end;
2018-03-29 20:02:04 +02:00
function cef_life_span_handler_do_close(self : PCefLifeSpanHandler;
browser : PCefBrowser): 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 TCefLifeSpanHandlerOwn) then
Result := Ord(TCefLifeSpanHandlerOwn(TempObject).DoClose(TCefBrowserRef.UnWrap(browser)));
2017-01-27 16:37:51 +01:00
end;
constructor TCefLifeSpanHandlerOwn.Create;
begin
inherited CreateData(SizeOf(TCefLifeSpanHandler));
2017-01-27 16:37:51 +01:00
with PCefLifeSpanHandler(FData)^ do
begin
on_before_popup := {$IFDEF FPC}@{$ENDIF}cef_life_span_handler_on_before_popup;
on_after_created := {$IFDEF FPC}@{$ENDIF}cef_life_span_handler_on_after_created;
do_close := {$IFDEF FPC}@{$ENDIF}cef_life_span_handler_do_close;
on_before_close := {$IFDEF FPC}@{$ENDIF}cef_life_span_handler_on_before_close;
end;
2017-01-27 16:37:51 +01:00
end;
procedure TCefLifeSpanHandlerOwn.OnAfterCreated(const browser: ICefBrowser);
begin
2018-03-29 20:02:04 +02:00
//
2017-01-27 16:37:51 +01:00
end;
procedure TCefLifeSpanHandlerOwn.OnBeforeClose(const browser: ICefBrowser);
begin
2018-03-29 20:02:04 +02:00
//
2017-01-27 16:37:51 +01:00
end;
function TCefLifeSpanHandlerOwn.OnBeforePopup(const browser : ICefBrowser;
const frame : ICefFrame;
const targetUrl : ustring;
const targetFrameName : ustring;
targetDisposition : TCefWindowOpenDisposition;
userGesture : Boolean;
2018-03-29 20:02:04 +02:00
const popupFeatures : TCefPopupFeatures;
var windowInfo : TCefWindowInfo;
var client : ICefClient;
var settings : TCefBrowserSettings;
2019-06-16 10:31:13 +02:00
var extra_info : ICefDictionaryValue;
var noJavascriptAccess : Boolean): Boolean;
2017-01-27 16:37:51 +01:00
begin
Result := False;
end;
function TCefLifeSpanHandlerOwn.DoClose(const browser: ICefBrowser): Boolean;
begin
Result := False;
end;
procedure TCefLifeSpanHandlerOwn.RemoveReferences;
begin
//
end;
2017-01-27 16:37:51 +01:00
// TCustomLifeSpanHandler
constructor TCustomLifeSpanHandler.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 TCustomLifeSpanHandler.Destroy;
begin
RemoveReferences;
inherited Destroy;
end;
procedure TCustomLifeSpanHandler.RemoveReferences;
begin
FEvents := nil;
end;
2017-01-27 16:37:51 +01:00
function TCustomLifeSpanHandler.DoClose(const browser: ICefBrowser): Boolean;
begin
if (FEvents <> nil) then
Result := IChromiumEvents(FEvents).doOnClose(browser)
else
Result := inherited DoClose(browser);
2017-01-27 16:37:51 +01:00
end;
procedure TCustomLifeSpanHandler.OnAfterCreated(const browser: ICefBrowser);
begin
if (FEvents <> nil) then IChromiumEvents(FEvents).doOnAfterCreated(browser);
2017-01-27 16:37:51 +01:00
end;
procedure TCustomLifeSpanHandler.OnBeforeClose(const browser: ICefBrowser);
begin
if (FEvents <> nil) then IChromiumEvents(FEvents).doOnBeforeClose(browser);
2017-01-27 16:37:51 +01:00
end;
function TCustomLifeSpanHandler.OnBeforePopup(const browser : ICefBrowser;
const frame : ICefFrame;
const targetUrl : ustring;
const targetFrameName : ustring;
targetDisposition : TCefWindowOpenDisposition;
userGesture : Boolean;
2018-03-29 20:02:04 +02:00
const popupFeatures : TCefPopupFeatures;
var windowInfo : TCefWindowInfo;
var client : ICefClient;
var settings : TCefBrowserSettings;
2019-06-16 10:31:13 +02:00
var extra_info : ICefDictionaryValue;
var noJavascriptAccess : Boolean): Boolean;
2017-01-27 16:37:51 +01:00
begin
if (FEvents <> nil) then
Result := IChromiumEvents(FEvents).doOnBeforePopup(browser, frame, targetUrl, targetFrameName,
targetDisposition, userGesture, popupFeatures,
2019-06-16 10:31:13 +02:00
windowInfo, client, settings, extra_info, noJavascriptAccess)
else
Result := inherited OnBeforePopup(browser, frame, targetUrl, targetFrameName,
targetDisposition, userGesture, popupFeatures,
2019-06-16 10:31:13 +02:00
windowInfo, client, settings, extra_info, noJavascriptAccess);
2017-01-27 16:37:51 +01:00
end;
end.