CEF4Delphi/source/uCEFRenderProcessHandler.pas

418 lines
18 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
//
2023-01-07 15:53:04 +01:00
// Copyright <20> 2023 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 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
if (FCefApp <> nil) then FCefApp.Internal_OnWebKitInitialized;
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
2019-06-16 10:31:13 +02:00
if (FCefApp <> nil) then FCefApp.Internal_OnBrowserCreated(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
if (FCefApp <> nil) then FCefApp.Internal_OnBrowserDestroyed(browser);
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
if (FCefApp <> nil) then FCefApp.Internal_OnContextCreated(browser, frame, context);
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
if (FCefApp <> nil) then FCefApp.Internal_OnContextReleased(browser, frame, context);
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
if (FCefApp <> nil) then FCefApp.Internal_OnUncaughtException(browser, frame, context, V8Exception, stackTrace);
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
if (FCefApp <> nil) then FCefApp.Internal_OnFocusedNodeChanged(browser, frame, node);
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
2019-06-16 10:31:13 +02:00
if (FCefApp <> nil) then FCefApp.Internal_OnProcessMessageReceived(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.