CEF4Delphi/source/uCEFRenderProcessHandler.pas

389 lines
16 KiB
ObjectPascal
Raw Permalink Normal View History

2017-01-27 16:37:51 +01:00
unit uCEFRenderProcessHandler;
{$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
{$IFDEF DELPHI16_UP}
System.Classes,
{$ELSE}
Classes,
{$ENDIF}
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes, uCEFListValue, uCEFBrowser, uCEFFrame, uCEFRequest,
uCEFv8Context, uCEFv8Exception, uCEFv8StackTrace, uCEFDomNode, uCEFProcessMessage, uCEFApplicationCore;
2017-01-27 16:37:51 +01:00
type
TCefRenderProcessHandlerOwn = class(TCefBaseRefCountedOwn, ICefRenderProcessHandler)
protected
2017-11-25 19:04:15 +01:00
procedure OnWebKitInitialized; virtual; abstract;
2019-06-16 10:31:13 +02:00
procedure OnBrowserCreated(const browser: ICefBrowser; const extra_info: ICefDictionaryValue); virtual; abstract;
2017-11-25 19:04:15 +01:00
procedure OnBrowserDestroyed(const browser: ICefBrowser); virtual; abstract;
function GetLoadHandler: ICefLoadHandler; virtual;
procedure OnContextCreated(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context); virtual; abstract;
procedure OnContextReleased(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context); virtual; abstract;
2018-03-29 20:02:04 +02:00
procedure OnUncaughtException(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context; const V8Exception: ICefV8Exception; const stackTrace: ICefV8StackTrace); virtual; abstract;
2017-11-25 19:04:15 +01:00
procedure OnFocusedNodeChanged(const browser: ICefBrowser; const frame: ICefFrame; const node: ICefDomNode); virtual; abstract;
2019-06-16 10:31:13 +02:00
function OnProcessMessageReceived(const browser: ICefBrowser; const frame: ICefFrame; sourceProcess: TCefProcessId; const aMessage: ICefProcessMessage): Boolean; virtual;
procedure RemoveReferences; virtual; abstract;
public
constructor Create; virtual;
end;
2017-11-25 19:04:15 +01:00
TCefCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
protected
FCefApp : TCefApplicationCore;
FLoadHandler : ICefLoadHandler;
2017-11-25 19:04:15 +01:00
procedure OnWebKitInitialized; override;
2019-06-16 10:31:13 +02:00
procedure OnBrowserCreated(const browser: ICefBrowser; const extra_info: ICefDictionaryValue); override;
2017-11-25 19:04:15 +01:00
procedure OnBrowserDestroyed(const browser: ICefBrowser); override;
function GetLoadHandler: ICefLoadHandler; override;
2017-11-25 19:04:15 +01:00
procedure OnContextCreated(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context); override;
procedure OnContextReleased(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context); override;
2018-03-29 20:02:04 +02:00
procedure OnUncaughtException(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context; const V8Exception: ICefV8Exception; const stackTrace: ICefV8StackTrace); override;
2017-11-25 19:04:15 +01:00
procedure OnFocusedNodeChanged(const browser: ICefBrowser; const frame: ICefFrame; const node: ICefDomNode); override;
2019-06-16 10:31:13 +02:00
function OnProcessMessageReceived(const browser: ICefBrowser; const frame: ICefFrame; sourceProcess: TCefProcessId; const aMessage : ICefProcessMessage): Boolean; override;
2017-11-25 19:04:15 +01:00
procedure RemoveReferences; override;
2017-11-25 19:04:15 +01:00
public
constructor Create(const aCefApp : TCefApplicationCore); reintroduce;
2017-11-25 19:04:15 +01:00
destructor Destroy; override;
end;
2017-01-27 16:37:51 +01:00
implementation
uses
2018-03-29 20:02:04 +02:00
{$IFDEF DELPHI16_UP}
System.SysUtils,
{$ELSE}
SysUtils,
{$ENDIF}
2019-06-16 10:31:13 +02:00
uCEFMiscFunctions, uCEFLibFunctions, uCEFConstants, uCEFLoadHandler, uCEFDictionaryValue;
2017-01-27 16:37:51 +01:00
procedure cef_render_process_handler_on_web_kit_initialized(self: PCefRenderProcessHandler); stdcall;
2017-11-25 19:04:15 +01:00
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2017-11-25 19:04:15 +01:00
TCefRenderProcessHandlerOwn(TempObject).OnWebKitInitialized;
2017-01-27 16:37:51 +01:00
end;
2019-06-16 10:31:13 +02:00
procedure cef_render_process_handler_on_browser_created(self : PCefRenderProcessHandler;
browser : PCefBrowser;
extra_info : PCefDictionaryValue); stdcall;
2017-11-25 19:04:15 +01:00
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2019-06-16 10:31:13 +02:00
TCefRenderProcessHandlerOwn(TempObject).OnBrowserCreated(TCefBrowserRef.UnWrap(browser),
TCefDictionaryValueRef.UnWrap(extra_info));
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
procedure cef_render_process_handler_on_browser_destroyed(self : PCefRenderProcessHandler;
browser : PCefBrowser); stdcall;
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2017-11-25 19:04:15 +01:00
TCefRenderProcessHandlerOwn(TempObject).OnBrowserDestroyed(TCefBrowserRef.UnWrap(browser));
2017-01-27 16:37:51 +01:00
end;
function cef_render_process_handler_get_load_handler(self: PCefRenderProcessHandler): PCefLoadHandler; stdcall;
2017-11-25 19:04:15 +01:00
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2018-03-29 20:02:04 +02:00
Result := nil;
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2018-03-29 20:02:04 +02:00
Result := CefGetData(TCefRenderProcessHandlerOwn(TempObject).GetLoadHandler);
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
procedure cef_render_process_handler_on_context_created(self : PCefRenderProcessHandler;
browser : PCefBrowser;
frame : PCefFrame;
context : PCefv8Context); stdcall;
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2017-11-25 19:04:15 +01:00
TCefRenderProcessHandlerOwn(TempObject).OnContextCreated(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefv8ContextRef.UnWrap(context));
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
procedure cef_render_process_handler_on_context_released(self : PCefRenderProcessHandler;
browser : PCefBrowser;
frame : PCefFrame;
context : PCefv8Context); stdcall;
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2017-11-25 19:04:15 +01:00
TCefRenderProcessHandlerOwn(TempObject).OnContextReleased(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefv8ContextRef.UnWrap(context));
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
procedure cef_render_process_handler_on_uncaught_exception(self : PCefRenderProcessHandler;
browser : PCefBrowser;
frame : PCefFrame;
context : PCefv8Context;
exception : PCefV8Exception;
stackTrace : PCefV8StackTrace); stdcall;
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2017-11-25 19:04:15 +01:00
TCefRenderProcessHandlerOwn(TempObject).OnUncaughtException(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefv8ContextRef.UnWrap(context),
TCefV8ExceptionRef.UnWrap(exception),
TCefV8StackTraceRef.UnWrap(stackTrace));
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
procedure cef_render_process_handler_on_focused_node_changed(self : PCefRenderProcessHandler;
browser : PCefBrowser;
frame : PCefFrame;
node : PCefDomNode); stdcall;
var
TempObject : TObject;
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2017-11-25 19:04:15 +01:00
TCefRenderProcessHandlerOwn(TempObject).OnFocusedNodeChanged(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefDomNodeRef.UnWrap(node));
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
function cef_render_process_handler_on_process_message_received(self : PCefRenderProcessHandler;
browser : PCefBrowser;
2019-06-16 10:31:13 +02:00
frame : PCefFrame;
2017-11-25 19:04:15 +01:00
source_process : TCefProcessId;
message_ : PCefProcessMessage): 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);
2017-11-25 19:04:15 +01:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
2017-11-25 19:04:15 +01:00
Result := Ord(TCefRenderProcessHandlerOwn(TempObject).OnProcessMessageReceived(TCefBrowserRef.UnWrap(browser),
2019-06-16 10:31:13 +02:00
TCefFrameRef.UnWrap(frame),
2017-11-25 19:04:15 +01:00
source_process,
2018-03-29 20:02:04 +02:00
TCefProcessMessageRef.UnWrap(message_)));
2017-01-27 16:37:51 +01:00
end;
// TCefRenderProcessHandlerOwn
2017-01-27 16:37:51 +01:00
constructor TCefRenderProcessHandlerOwn.Create;
begin
inherited CreateData(SizeOf(TCefRenderProcessHandler));
2017-01-27 16:37:51 +01:00
with PCefRenderProcessHandler(FData)^ do
begin
on_web_kit_initialized := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_on_web_kit_initialized;
on_browser_created := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_on_browser_created;
on_browser_destroyed := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_on_browser_destroyed;
get_load_handler := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_get_load_handler;
on_context_created := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_on_context_created;
on_context_released := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_on_context_released;
on_uncaught_exception := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_on_uncaught_exception;
on_focused_node_changed := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_on_focused_node_changed;
on_process_message_received := {$IFDEF FPC}@{$ENDIF}cef_render_process_handler_on_process_message_received;
end;
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
function TCefRenderProcessHandlerOwn.GetLoadHandler: ICefLoadHandler;
2017-01-27 16:37:51 +01:00
begin
Result := nil;
end;
2017-11-25 19:04:15 +01:00
function TCefRenderProcessHandlerOwn.OnProcessMessageReceived(const browser : ICefBrowser;
2019-06-16 10:31:13 +02:00
const frame : ICefFrame;
2017-11-25 19:04:15 +01:00
sourceProcess : TCefProcessId;
const aMessage : ICefProcessMessage): Boolean;
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
Result := False;
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
// TCefCustomRenderProcessHandler
constructor TCefCustomRenderProcessHandler.Create(const aCefApp : TCefApplicationCore);
2017-01-27 16:37:51 +01:00
begin
2017-11-25 19:04:15 +01:00
inherited Create;
2017-01-27 16:37:51 +01:00
2017-11-25 19:04:15 +01:00
FCefApp := aCefApp;
if (FCefApp <> nil) and FCefApp.MustCreateLoadHandler then
FLoadHandler := TCustomRenderLoadHandler.Create(FCefApp)
else
FLoadHandler := nil;
2017-01-27 16:37:51 +01:00
end;
2017-11-25 19:04:15 +01:00
destructor TCefCustomRenderProcessHandler.Destroy;
2017-01-27 16:37:51 +01:00
begin
RemoveReferences;
2017-01-27 16:37:51 +01:00
2017-11-25 19:04:15 +01:00
inherited Destroy;
2017-01-27 16:37:51 +01:00
end;
procedure TCefCustomRenderProcessHandler.RemoveReferences;
begin
if (FLoadHandler <> nil) then
FLoadHandler.RemoveReferences;
FCefApp := nil;
FLoadHandler := nil;
end;
2017-11-25 19:04:15 +01:00
procedure TCefCustomRenderProcessHandler.OnWebKitInitialized;
begin
2018-03-29 20:02:04 +02:00
try
2023-08-03 15:50:13 +02:00
if (FCefApp <> nil) then
IApplicationCoreEvents(FCefApp).doOnWebKitInitialized;
2018-03-29 20:02:04 +02:00
except
on e : exception do
if CustomExceptionHandler('TCefCustomRenderProcessHandler.OnWebKitInitialized', e) then raise;
end;
2017-01-27 16:37:51 +01:00
end;
2019-06-16 10:31:13 +02:00
procedure TCefCustomRenderProcessHandler.OnBrowserCreated(const browser: ICefBrowser; const extra_info: ICefDictionaryValue);
2017-01-27 16:37:51 +01:00
begin
2018-03-29 20:02:04 +02:00
try
2023-08-03 15:50:13 +02:00
if (FCefApp <> nil) then
IApplicationCoreEvents(FCefApp).doOnBrowserCreated(browser, extra_info);
2018-03-29 20:02:04 +02:00
except
on e : exception do
if CustomExceptionHandler('TCefCustomRenderProcessHandler.OnBrowserCreated', e) then raise;
end;
2017-11-25 19:04:15 +01:00
end;
2017-01-27 16:37:51 +01:00
2017-11-25 19:04:15 +01:00
procedure TCefCustomRenderProcessHandler.OnBrowserDestroyed(const browser: ICefBrowser);
begin
2018-03-29 20:02:04 +02:00
try
2023-08-03 15:50:13 +02:00
if (FCefApp <> nil) then
IApplicationCoreEvents(FCefApp).doOnBrowserDestroyed(browser);
2018-03-29 20:02:04 +02:00
except
on e : exception do
if CustomExceptionHandler('TCefCustomRenderProcessHandler.OnBrowserDestroyed', e) then raise;
end;
2017-01-27 16:37:51 +01:00
end;
function TCefCustomRenderProcessHandler.GetLoadHandler: ICefLoadHandler;
begin
if (FLoadHandler <> nil) then
Result := FLoadHandler
else
Result := inherited GetLoadHandler;
end;
2017-11-25 19:04:15 +01:00
procedure TCefCustomRenderProcessHandler.OnContextCreated(const browser : ICefBrowser;
const frame : ICefFrame;
const context : ICefv8Context);
2017-01-27 16:37:51 +01:00
begin
2018-03-29 20:02:04 +02:00
try
2023-08-03 15:50:13 +02:00
if (FCefApp <> nil) then
IApplicationCoreEvents(FCefApp).doOnContextCreated(browser, frame, context);
2018-03-29 20:02:04 +02:00
except
on e : exception do
if CustomExceptionHandler('TCefCustomRenderProcessHandler.OnContextCreated', e) then raise;
end;
2017-11-25 19:04:15 +01:00
end;
2017-01-27 16:37:51 +01:00
2017-11-25 19:04:15 +01:00
procedure TCefCustomRenderProcessHandler.OnContextReleased(const browser : ICefBrowser;
const frame : ICefFrame;
const context : ICefv8Context);
begin
2018-03-29 20:02:04 +02:00
try
2023-08-03 15:50:13 +02:00
if (FCefApp <> nil) then
IApplicationCoreEvents(FCefApp).doOnContextReleased(browser, frame, context);
2018-03-29 20:02:04 +02:00
except
on e : exception do
if CustomExceptionHandler('TCefCustomRenderProcessHandler.OnContextReleased', e) then raise;
end;
2017-01-27 16:37:51 +01:00
end;
2018-03-29 20:02:04 +02:00
procedure TCefCustomRenderProcessHandler.OnUncaughtException(const browser : ICefBrowser;
const frame : ICefFrame;
const context : ICefv8Context;
const V8Exception : ICefV8Exception;
const stackTrace : ICefV8StackTrace);
2017-01-27 16:37:51 +01:00
begin
2018-03-29 20:02:04 +02:00
try
2023-08-03 15:50:13 +02:00
if (FCefApp <> nil) then
IApplicationCoreEvents(FCefApp).doOnUncaughtException(browser, frame, context, V8Exception, stackTrace);
2018-03-29 20:02:04 +02:00
except
on e : exception do
if CustomExceptionHandler('TCefCustomRenderProcessHandler.OnUncaughtException', e) then raise;
end;
2017-11-25 19:04:15 +01:00
end;
2017-01-27 16:37:51 +01:00
2017-11-25 19:04:15 +01:00
procedure TCefCustomRenderProcessHandler.OnFocusedNodeChanged(const browser : ICefBrowser;
const frame : ICefFrame;
const node : ICefDomNode);
begin
2018-03-29 20:02:04 +02:00
try
2023-08-03 15:50:13 +02:00
if (FCefApp <> nil) then
IApplicationCoreEvents(FCefApp).doOnFocusedNodeChanged(browser, frame, node);
2018-03-29 20:02:04 +02:00
except
on e : exception do
if CustomExceptionHandler('TCefCustomRenderProcessHandler.OnFocusedNodeChanged', e) then raise;
end;
2017-01-27 16:37:51 +01:00
end;
function TCefCustomRenderProcessHandler.OnProcessMessageReceived(const browser : ICefBrowser;
const frame : ICefFrame;
sourceProcess : TCefProcessId;
const aMessage : ICefProcessMessage): Boolean;
2017-01-27 16:37:51 +01:00
begin
2019-06-16 10:31:13 +02:00
Result := inherited OnProcessMessageReceived(browser, frame, sourceProcess, aMessage);
2017-01-27 16:37:51 +01:00
2018-03-29 20:02:04 +02:00
try
2023-08-03 15:50:13 +02:00
if (FCefApp <> nil) then
IApplicationCoreEvents(FCefApp).doOnProcessMessageReceived(browser, frame, sourceProcess, aMessage, Result);
2018-03-29 20:02:04 +02:00
except
on e : exception do
if CustomExceptionHandler('TCefCustomRenderProcessHandler.OnProcessMessageReceived', e) then raise;
end;
2017-01-27 16:37:51 +01:00
end;
end.