2017-01-27 17:29:37 +01:00
|
|
|
unit uCEFBrowserProcessHandler;
|
2021-04-18 19:36:20 +02:00
|
|
|
|
|
|
|
{$IFDEF FPC}
|
2018-05-12 14:50:54 +02:00
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
2017-01-27 17:29:37 +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 17:29:37 +01:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2020-11-19 18:55:17 +01:00
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
System.Classes,
|
|
|
|
{$ELSE}
|
|
|
|
Classes,
|
|
|
|
{$ENDIF}
|
2022-12-16 11:29:15 +01:00
|
|
|
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes, uCEFApplicationCore, uCEFPreferenceRegistrar;
|
2017-01-27 17:29:37 +01:00
|
|
|
|
|
|
|
type
|
2017-03-16 19:09:42 +01:00
|
|
|
TCefBrowserProcessHandlerOwn = class(TCefBaseRefCountedOwn, ICefBrowserProcessHandler)
|
2017-10-28 10:53:51 +02:00
|
|
|
protected
|
2022-12-16 11:29:15 +01:00
|
|
|
procedure OnRegisterCustomPreferences(type_: TCefPreferencesType; registrar: PCefPreferenceRegistrar); virtual; abstract;
|
2017-11-25 19:04:15 +01:00
|
|
|
procedure OnContextInitialized; virtual; abstract;
|
|
|
|
procedure OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine); virtual; abstract;
|
2023-12-15 18:06:46 +01:00
|
|
|
procedure OnAlreadyRunningAppRelaunch(const commandLine: ICefCommandLine; const current_directory: ustring; var aResult: boolean); virtual; abstract;
|
2017-11-25 19:04:15 +01:00
|
|
|
procedure OnScheduleMessagePumpWork(const delayMs: Int64); virtual; abstract;
|
2020-11-19 18:55:17 +01:00
|
|
|
procedure GetDefaultClient(var aClient : ICefClient); virtual;
|
2024-02-24 12:01:31 +01:00
|
|
|
procedure GetDefaultRequestContextHandler(var aRequestContextHandler : ICefRequestContextHandler); virtual;
|
2017-10-28 10:53:51 +02:00
|
|
|
|
2020-11-10 15:02:49 +01:00
|
|
|
procedure RemoveReferences; virtual; abstract;
|
|
|
|
|
2017-10-28 10:53:51 +02:00
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
2017-01-27 17:29:37 +01:00
|
|
|
end;
|
|
|
|
|
2017-11-25 19:04:15 +01:00
|
|
|
TCefCustomBrowserProcessHandler = class(TCefBrowserProcessHandlerOwn)
|
2021-04-18 19:36:20 +02:00
|
|
|
protected
|
|
|
|
FCefApp : TCefApplicationCore;
|
2020-11-19 18:55:17 +01:00
|
|
|
|
2022-12-16 11:29:15 +01:00
|
|
|
procedure OnRegisterCustomPreferences(type_: TCefPreferencesType; registrar: PCefPreferenceRegistrar); override;
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure OnContextInitialized; override;
|
|
|
|
procedure OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine); override;
|
2023-12-15 18:06:46 +01:00
|
|
|
procedure OnAlreadyRunningAppRelaunch(const commandLine: ICefCommandLine; const current_directory: ustring; var aResult: boolean); override;
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure OnScheduleMessagePumpWork(const delayMs: Int64); override;
|
|
|
|
procedure GetDefaultClient(var aClient : ICefClient); override;
|
2024-02-24 12:01:31 +01:00
|
|
|
procedure GetDefaultRequestContextHandler(var aRequestContextHandler : ICefRequestContextHandler); override;
|
2020-11-19 18:55:17 +01:00
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure RemoveReferences; override;
|
2020-11-19 18:55:17 +01:00
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
public
|
|
|
|
constructor Create(const aCefApp : TCefApplicationCore); reintroduce;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
2020-11-19 18:55:17 +01:00
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
implementation
|
2020-11-19 18:55:17 +01:00
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
uses
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
System.SysUtils,
|
|
|
|
{$ELSE}
|
|
|
|
SysUtils,
|
|
|
|
{$ENDIF}
|
|
|
|
uCEFMiscFunctions, uCEFLibFunctions, uCEFCommandLine, uCEFListValue, uCEFConstants, uCEFStringList;
|
2017-01-27 17:29:37 +01:00
|
|
|
|
2022-12-16 11:29:15 +01:00
|
|
|
procedure cef_browser_process_handler_on_register_custom_preferences(self: PCefBrowserProcessHandler; type_: TCefPreferencesType; registrar: PCefPreferenceRegistrar); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
begin
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
// We have to wrap registrar inside TCefBrowserProcessHandlerOwn.OnRegisterCustomPreferences to avoid a circular reference
|
|
|
|
if (TempObject <> nil) and
|
|
|
|
(TempObject is TCefBrowserProcessHandlerOwn) then
|
|
|
|
TCefBrowserProcessHandlerOwn(TempObject).OnRegisterCustomPreferences(type_, registrar);
|
|
|
|
end;
|
|
|
|
|
2017-01-27 17:29:37 +01:00
|
|
|
procedure cef_browser_process_handler_on_context_initialized(self: PCefBrowserProcessHandler); stdcall;
|
2017-11-25 19:04:15 +01:00
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 17:29:37 +01:00
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
2020-11-10 15:02:49 +01:00
|
|
|
if (TempObject <> nil) and
|
|
|
|
(TempObject is TCefBrowserProcessHandlerOwn) then
|
2017-11-25 19:04:15 +01:00
|
|
|
TCefBrowserProcessHandlerOwn(TempObject).OnContextInitialized;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure cef_browser_process_handler_on_before_child_process_launch(self : PCefBrowserProcessHandler;
|
|
|
|
command_line : PCefCommandLine); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
2020-11-10 15:02:49 +01:00
|
|
|
if (TempObject <> nil) and
|
|
|
|
(TempObject is TCefBrowserProcessHandlerOwn) then
|
2017-11-25 19:04:15 +01:00
|
|
|
TCefBrowserProcessHandlerOwn(TempObject).OnBeforeChildProcessLaunch(TCefCommandLineRef.UnWrap(command_line));
|
2017-01-27 17:29:37 +01:00
|
|
|
end;
|
|
|
|
|
2023-12-15 18:06:46 +01:00
|
|
|
function cef_browser_process_handler_on_already_running_app_relaunch( self : PCefBrowserProcessHandler;
|
|
|
|
command_line : PCefCommandLine;
|
|
|
|
const current_directory : PCefString): integer; stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
TempResult : boolean;
|
|
|
|
begin
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
TempResult := False;
|
|
|
|
|
|
|
|
if (TempObject <> nil) and
|
|
|
|
(TempObject is TCefBrowserProcessHandlerOwn) then
|
|
|
|
TCefBrowserProcessHandlerOwn(TempObject).OnAlreadyRunningAppRelaunch(TCefCommandLineRef.UnWrap(command_line),
|
|
|
|
CefString(current_directory),
|
|
|
|
TempResult);
|
|
|
|
|
|
|
|
Result := Ord(TempResult);
|
|
|
|
end;
|
|
|
|
|
2017-11-25 19:04:15 +01:00
|
|
|
procedure cef_browser_process_handler_on_schedule_message_pump_work(self : PCefBrowserProcessHandler;
|
|
|
|
delay_ms : Int64); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 16:24:27 +01:00
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
2020-11-10 15:02:49 +01:00
|
|
|
if (TempObject <> nil) and
|
|
|
|
(TempObject is TCefBrowserProcessHandlerOwn) then
|
2017-11-25 19:04:15 +01:00
|
|
|
TCefBrowserProcessHandlerOwn(TempObject).OnScheduleMessagePumpWork(delay_ms);
|
2017-01-27 17:29:37 +01:00
|
|
|
end;
|
|
|
|
|
2020-11-19 18:55:17 +01:00
|
|
|
function cef_browser_process_handler_get_default_client(self: PCefBrowserProcessHandler): PCefClient; stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
TempClient : ICefClient;
|
|
|
|
begin
|
|
|
|
Result := nil;
|
2021-04-18 19:36:20 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
2020-11-19 18:55:17 +01:00
|
|
|
if (TempObject <> nil) and
|
|
|
|
(TempObject is TCefBrowserProcessHandlerOwn) then
|
|
|
|
try
|
|
|
|
TempClient := nil;
|
|
|
|
TCefBrowserProcessHandlerOwn(TempObject).GetDefaultClient(TempClient);
|
2021-04-18 19:36:20 +02:00
|
|
|
if (TempClient <> nil) then Result := TempClient.Wrap;
|
|
|
|
finally
|
|
|
|
TempClient := nil;
|
2020-11-19 18:55:17 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-01-27 17:29:37 +01:00
|
|
|
constructor TCefBrowserProcessHandlerOwn.Create;
|
|
|
|
begin
|
|
|
|
inherited CreateData(SizeOf(TCefBrowserProcessHandler));
|
|
|
|
|
|
|
|
with PCefBrowserProcessHandler(FData)^ do
|
|
|
|
begin
|
2022-12-16 11:29:15 +01:00
|
|
|
on_register_custom_preferences := {$IFDEF FPC}@{$ENDIF}cef_browser_process_handler_on_register_custom_preferences;
|
2018-05-12 14:50:54 +02:00
|
|
|
on_context_initialized := {$IFDEF FPC}@{$ENDIF}cef_browser_process_handler_on_context_initialized;
|
|
|
|
on_before_child_process_launch := {$IFDEF FPC}@{$ENDIF}cef_browser_process_handler_on_before_child_process_launch;
|
2023-12-15 18:06:46 +01:00
|
|
|
on_already_running_app_relaunch := {$IFDEF FPC}@{$ENDIF}cef_browser_process_handler_on_already_running_app_relaunch;
|
2018-05-12 14:50:54 +02:00
|
|
|
on_schedule_message_pump_work := {$IFDEF FPC}@{$ENDIF}cef_browser_process_handler_on_schedule_message_pump_work;
|
2020-11-19 18:55:17 +01:00
|
|
|
get_default_client := {$IFDEF FPC}@{$ENDIF}cef_browser_process_handler_get_default_client;
|
2017-01-27 17:29:37 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure TCefBrowserProcessHandlerOwn.GetDefaultClient(var aClient : ICefClient);
|
2017-11-25 19:04:15 +01:00
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
aClient := nil;
|
2017-11-25 19:04:15 +01:00
|
|
|
end;
|
|
|
|
|
2024-02-24 12:01:31 +01:00
|
|
|
procedure TCefBrowserProcessHandlerOwn.GetDefaultRequestContextHandler(var aRequestContextHandler : ICefRequestContextHandler);
|
|
|
|
begin
|
|
|
|
aRequestContextHandler := nil;
|
|
|
|
end;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
|
|
|
|
// TCefCustomBrowserProcessHandler
|
|
|
|
|
|
|
|
|
|
|
|
constructor TCefCustomBrowserProcessHandler.Create(const aCefApp : TCefApplicationCore);
|
2020-11-19 18:55:17 +01:00
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
FCefApp := aCefApp;
|
2020-11-19 18:55:17 +01:00
|
|
|
end;
|
2017-11-25 19:04:15 +01:00
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
destructor TCefCustomBrowserProcessHandler.Destroy;
|
|
|
|
begin
|
|
|
|
RemoveReferences;
|
2020-11-10 15:02:49 +01:00
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
inherited Destroy;
|
2017-01-27 17:29:37 +01:00
|
|
|
end;
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure TCefCustomBrowserProcessHandler.RemoveReferences;
|
2020-11-19 18:55:17 +01:00
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
FCefApp := nil;
|
2020-11-19 18:55:17 +01:00
|
|
|
end;
|
|
|
|
|
2022-12-16 11:29:15 +01:00
|
|
|
procedure TCefCustomBrowserProcessHandler.OnRegisterCustomPreferences(type_ : TCefPreferencesType;
|
|
|
|
registrar : PCefPreferenceRegistrar);
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
if (FCefApp <> nil) then
|
2023-08-03 15:58:57 +02:00
|
|
|
IApplicationCoreEvents(FCefApp).doOnRegisterCustomPreferences(type_, registrar);
|
2022-12-16 11:29:15 +01:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCefCustomBrowserProcessHandler.OnRegisterCustomPreferences', e) then raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-11-10 15:02:49 +01:00
|
|
|
procedure TCefCustomBrowserProcessHandler.OnContextInitialized;
|
2021-01-01 17:51:09 +01:00
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
try
|
2023-08-03 15:50:13 +02:00
|
|
|
if (FCefApp <> nil) then
|
2023-08-03 15:58:57 +02:00
|
|
|
IApplicationCoreEvents(FCefApp).doOnContextInitialized;
|
2021-04-18 19:36:20 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCefCustomBrowserProcessHandler.OnContextInitialized', e) then raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCefCustomBrowserProcessHandler.OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine);
|
|
|
|
begin
|
|
|
|
try
|
2023-08-03 15:50:13 +02:00
|
|
|
if (FCefApp <> nil) then
|
2023-08-03 15:58:57 +02:00
|
|
|
IApplicationCoreEvents(FCefApp).doOnBeforeChildProcessLaunch(commandLine);
|
2021-04-18 19:36:20 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCefCustomBrowserProcessHandler.OnBeforeChildProcessLaunch', e) then raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-12-15 18:06:46 +01:00
|
|
|
procedure TCefCustomBrowserProcessHandler.OnAlreadyRunningAppRelaunch(const commandLine: ICefCommandLine; const current_directory: ustring; var aResult: boolean);
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
if (FCefApp <> nil) then
|
|
|
|
IApplicationCoreEvents(FCefApp).doOnAlreadyRunningAppRelaunch(commandLine, current_directory, aResult);
|
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCefCustomBrowserProcessHandler.OnAlreadyRunningAppRelaunch', e) then raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure TCefCustomBrowserProcessHandler.OnScheduleMessagePumpWork(const delayMs: Int64);
|
|
|
|
begin
|
|
|
|
try
|
2023-08-03 15:50:13 +02:00
|
|
|
if (FCefApp <> nil) then
|
2023-08-03 15:58:57 +02:00
|
|
|
IApplicationCoreEvents(FCefApp).doOnScheduleMessagePumpWork(delayMs);
|
2021-04-18 19:36:20 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCefCustomBrowserProcessHandler.OnScheduleMessagePumpWork', e) then raise;
|
|
|
|
end;
|
2017-01-27 17:29:37 +01:00
|
|
|
end;
|
|
|
|
|
2020-11-19 18:55:17 +01:00
|
|
|
procedure TCefCustomBrowserProcessHandler.GetDefaultClient(var aClient : ICefClient);
|
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
try
|
2023-08-03 15:50:13 +02:00
|
|
|
if (FCefApp <> nil) then
|
2023-08-03 15:58:57 +02:00
|
|
|
IApplicationCoreEvents(FCefApp).doGetDefaultClient(aClient);
|
2021-04-18 19:36:20 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCefCustomBrowserProcessHandler.GetDefaultClient', e) then raise;
|
|
|
|
end;
|
2020-11-19 18:55:17 +01:00
|
|
|
end;
|
|
|
|
|
2024-02-24 12:01:31 +01:00
|
|
|
procedure TCefCustomBrowserProcessHandler.GetDefaultRequestContextHandler(var aRequestContextHandler : ICefRequestContextHandler);
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
if (FCefApp <> nil) then
|
|
|
|
IApplicationCoreEvents(FCefApp).doGetDefaultRequestContextHandler(aRequestContextHandler);
|
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCefCustomBrowserProcessHandler.GetDefaultRequestContextHandler', e) then raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-01-27 17:29:37 +01:00
|
|
|
end.
|