1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2024-11-24 08:02:15 +02:00

Partial fix for issue #291

- Create the CefApp handlers only when they are needed in SingleProcess mode.
- Added several "RemoveReferences" procedures to disable events when the application is shutting down in the CefApp handlers.
This commit is contained in:
Salvador Diaz Fau 2020-11-10 15:02:49 +01:00
parent 9ac740613c
commit e72db93dd2
9 changed files with 164 additions and 65 deletions

View File

@ -65,6 +65,8 @@ type
procedure GetBrowserProcessHandler(var aHandler : ICefBrowserProcessHandler); virtual; abstract;
procedure GetRenderProcessHandler(var aHandler : ICefRenderProcessHandler); virtual; abstract;
procedure RemoveReferences; virtual; abstract;
public
constructor Create; virtual;
end;
@ -83,10 +85,11 @@ type
procedure GetRenderProcessHandler(var aHandler : ICefRenderProcessHandler); override;
procedure InitializeVars;
procedure RemoveReferences; override;
public
constructor Create(const aCefApp : TCefApplicationCore); reintroduce;
procedure BeforeDestruction; override;
destructor Destroy; override;
end;
@ -104,9 +107,9 @@ uses
// TCefAppOwn
procedure cef_app_on_before_command_line_processing(self: PCefApp;
const process_type: PCefString;
command_line: PCefCommandLine); stdcall;
procedure cef_app_on_before_command_line_processing( self : PCefApp;
const process_type : PCefString;
command_line : PCefCommandLine); stdcall;
var
TempObject : TObject;
begin
@ -117,7 +120,8 @@ begin
TCefCommandLineRef.UnWrap(command_line));
end;
procedure cef_app_on_register_custom_schemes(self: PCefApp; registrar: PCefSchemeRegistrar); stdcall;
procedure cef_app_on_register_custom_schemes(self : PCefApp;
registrar : PCefSchemeRegistrar); stdcall;
var
TempWrapper : TCefSchemeRegistrarRef;
TempObject : TObject;
@ -216,10 +220,10 @@ constructor TCustomCefApp.Create(const aCefApp : TCefApplicationCore);
begin
inherited Create;
FCefApp := aCefApp;
InitializeVars;
FCefApp := aCefApp;
if (FCefApp <> nil) then
begin
if FCefApp.MustCreateBrowserProcessHandler then
@ -233,22 +237,35 @@ begin
end;
end;
procedure TCustomCefApp.BeforeDestruction;
destructor TCustomCefApp.Destroy;
begin
FCefApp := nil;
RemoveReferences;
InitializeVars;
inherited BeforeDestruction;
inherited Destroy;
end;
procedure TCustomCefApp.InitializeVars;
begin
FCefApp := nil;
FResourceBundleHandler := nil;
FBrowserProcessHandler := nil;
FRenderProcessHandler := nil;
end;
procedure TCustomCefApp.RemoveReferences;
begin
if (FResourceBundleHandler <> nil) then
FResourceBundleHandler.RemoveReferences;
if (FBrowserProcessHandler <> nil) then
FBrowserProcessHandler.RemoveReferences;
if (FRenderProcessHandler <> nil) then
FRenderProcessHandler.RemoveReferences;
InitializeVars;
end;
procedure TCustomCefApp.OnBeforeCommandLineProcessing(const processType: ustring; const commandLine: ICefCommandLine);
begin
try

View File

@ -80,6 +80,7 @@ type
FDestroyAppWindows : boolean;
procedure BeforeInitSubProcess; override;
public
constructor Create;
destructor Destroy; override;

View File

@ -803,7 +803,11 @@ begin
LoadCEFlibrary then
begin
TempApp := TCustomCefApp.Create(self);
Result := InitializeLibrary(TempApp);
if InitializeLibrary(TempApp) then
Result := True
else
TempApp.RemoveReferences;
end;
except
on e : exception do
@ -824,14 +828,24 @@ begin
TempApp := nil;
try
if CheckCEFLibrary and LoadCEFlibrary then
begin
if (FProcessType <> ptBrowser) then
BeforeInitSubProcess;
try
if CheckCEFLibrary and LoadCEFlibrary then
begin
if (FProcessType <> ptBrowser) then
BeforeInitSubProcess;
TempApp := TCustomCefApp.Create(self);
Result := (ExecuteProcess(TempApp) < 0) and InitializeLibrary(TempApp);
end;
TempApp := TCustomCefApp.Create(self);
if (ExecuteProcess(TempApp) < 0) and
InitializeLibrary(TempApp) then
Result := True
else
TempApp.RemoveReferences;
end;
except
on e : exception do
if CustomExceptionHandler('TCefApplicationCore.SingleExeProcessing', e) then raise;
end;
finally
TempApp := nil;
end;
@ -1048,13 +1062,22 @@ begin
TempApp := nil;
try
if not(FSingleProcess) and
(ProcessType <> ptBrowser) and
LoadCEFlibrary then
begin
TempApp := TCustomCefApp.Create(self);
Result := (ExecuteProcess(TempApp) >= 0);
end;
try
if not(FSingleProcess) and
(ProcessType <> ptBrowser) and
LoadCEFlibrary then
begin
TempApp := TCustomCefApp.Create(self);
if (ExecuteProcess(TempApp) >= 0) then
Result := True
else
TempApp.RemoveReferences;
end;
except
on e : exception do
if CustomExceptionHandler('TCefApplicationCore.StartSubProcess', e) then raise;
end;
finally
TempApp := nil;
end;
@ -1924,8 +1947,7 @@ end;
function TCefApplicationCore.GetMustCreateResourceBundleHandler : boolean;
begin
Result := FSingleProcess or
((FProcessType in [ptBrowser, ptRenderer]) and
Result := ((FSingleProcess or (FProcessType in [ptBrowser, ptRenderer])) and
(FMustCreateResourceBundleHandler or
assigned(FOnGetLocalizedString) or
assigned(FOnGetDataResource) or
@ -1934,8 +1956,7 @@ end;
function TCefApplicationCore.GetMustCreateBrowserProcessHandler : boolean;
begin
Result := FSingleProcess or
((FProcessType = ptBrowser) and
Result := ((FSingleProcess or (FProcessType = ptBrowser)) and
(FMustCreateBrowserProcessHandler or
assigned(FOnContextInitialized) or
assigned(FOnBeforeChildProcessLaunch) or
@ -1944,8 +1965,7 @@ end;
function TCefApplicationCore.GetMustCreateRenderProcessHandler : boolean;
begin
Result := FSingleProcess or
((FProcessType = ptRenderer) and
Result := ((FSingleProcess or (FProcessType = ptRenderer)) and
(FMustCreateRenderProcessHandler or
MustCreateLoadHandler or
assigned(FOnWebKitInitialized) or
@ -1960,8 +1980,7 @@ end;
function TCefApplicationCore.GetMustCreateLoadHandler : boolean;
begin
Result := FSingleProcess or
((FProcessType = ptRenderer) and
Result := ((FSingleProcess or (FProcessType = ptRenderer)) and
(FMustCreateLoadHandler or
assigned(FOnLoadingStateChange) or
assigned(FOnLoadStart) or

View File

@ -59,6 +59,8 @@ type
procedure GetPrintHandler(var aHandler : ICefPrintHandler); virtual;
procedure OnScheduleMessagePumpWork(const delayMs: Int64); virtual; abstract;
procedure RemoveReferences; virtual; abstract;
public
constructor Create; virtual;
end;
@ -71,6 +73,8 @@ type
procedure OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine); override;
procedure OnScheduleMessagePumpWork(const delayMs: Int64); override;
procedure RemoveReferences; override;
public
constructor Create(const aCefApp : TCefApplicationCore); reintroduce;
destructor Destroy; override;
@ -92,7 +96,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefBrowserProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefBrowserProcessHandlerOwn) then
TCefBrowserProcessHandlerOwn(TempObject).OnContextInitialized;
end;
@ -103,7 +108,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefBrowserProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefBrowserProcessHandlerOwn) then
TCefBrowserProcessHandlerOwn(TempObject).OnBeforeChildProcessLaunch(TCefCommandLineRef.UnWrap(command_line));
end;
@ -115,7 +121,8 @@ begin
Result := nil;
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefBrowserProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefBrowserProcessHandlerOwn) then
try
TCefBrowserProcessHandlerOwn(TempObject).GetPrintHandler(TempHandler);
if (TempHandler <> nil) then Result := TempHandler.Wrap;
@ -131,7 +138,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefBrowserProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefBrowserProcessHandlerOwn) then
TCefBrowserProcessHandlerOwn(TempObject).OnScheduleMessagePumpWork(delay_ms);
end;
@ -154,6 +162,7 @@ begin
end;
// TCefCustomBrowserProcessHandler
@ -166,12 +175,17 @@ end;
destructor TCefCustomBrowserProcessHandler.Destroy;
begin
FCefApp := nil;
RemoveReferences;
inherited Destroy;
end;
procedure TCefCustomBrowserProcessHandler.OnContextInitialized;
procedure TCefCustomBrowserProcessHandler.RemoveReferences;
begin
FCefApp := nil;
end;
procedure TCefCustomBrowserProcessHandler.OnContextInitialized;
begin
try
if (FCefApp <> nil) then FCefApp.Internal_OnContextInitialized;

View File

@ -1422,6 +1422,8 @@ type
function GetLocalizedString(stringId: Integer; var stringVal: ustring): Boolean;
function GetDataResource(resourceId: Integer; var data: Pointer; var dataSize: NativeUInt): Boolean;
function GetDataResourceForScale(resourceId: Integer; scaleFactor: TCefScaleFactor; var data: Pointer; var dataSize: NativeUInt): Boolean;
procedure RemoveReferences; // custom procedure to clear all references
end;
// TCefBrowserProcessHandler
@ -1432,6 +1434,8 @@ type
procedure OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine);
procedure GetPrintHandler(var aHandler : ICefPrintHandler);
procedure OnScheduleMessagePumpWork(const delayMs: Int64);
procedure RemoveReferences; // custom procedure to clear all references
end;
// TCefRenderProcessHandler
@ -1447,6 +1451,8 @@ type
procedure OnUncaughtException(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context; const V8Exception: ICefV8Exception; const stackTrace: ICefV8StackTrace);
procedure OnFocusedNodeChanged(const browser: ICefBrowser; const frame: ICefFrame; const node: ICefDomNode);
function OnProcessMessageReceived(const browser: ICefBrowser; const frame: ICefFrame; sourceProcess: TCefProcessId; const aMessage: ICefProcessMessage): Boolean;
procedure RemoveReferences; // custom procedure to clear all references
end;
// TCefApp
@ -1458,6 +1464,8 @@ type
procedure GetResourceBundleHandler(var aHandler : ICefResourceBundleHandler);
procedure GetBrowserProcessHandler(var aHandler : ICefBrowserProcessHandler);
procedure GetRenderProcessHandler(var aHandler : ICefRenderProcessHandler);
procedure RemoveReferences; // custom procedure to clear all references
end;
// TCefCompletionCallback

View File

@ -90,6 +90,8 @@ type
procedure OnLoadEnd(const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer); override;
procedure OnLoadError(const browser: ICefBrowser; const frame: ICefFrame; errorCode: TCefErrorCode; const errorText, failedUrl: ustring); override;
procedure RemoveReferences; override;
public
constructor Create(const aCefApp : TCefApplicationCore); reintroduce; virtual;
destructor Destroy; override;
@ -273,11 +275,16 @@ end;
destructor TCustomRenderLoadHandler.Destroy;
begin
FCefApp := nil;
RemoveReferences;
inherited Destroy;
end;
procedure TCustomRenderLoadHandler.RemoveReferences;
begin
FCefApp := nil;
end;
procedure TCustomRenderLoadHandler.OnLoadEnd(const browser : ICefBrowser;
const frame : ICefFrame;
httpStatusCode : Integer);

View File

@ -70,6 +70,8 @@ type
procedure OnFocusedNodeChanged(const browser: ICefBrowser; const frame: ICefFrame; const node: ICefDomNode); virtual; abstract;
function OnProcessMessageReceived(const browser: ICefBrowser; const frame: ICefFrame; sourceProcess: TCefProcessId; const aMessage: ICefProcessMessage): Boolean; virtual;
procedure RemoveReferences; virtual; abstract;
public
constructor Create; virtual;
end;
@ -89,6 +91,8 @@ type
procedure OnFocusedNodeChanged(const browser: ICefBrowser; const frame: ICefFrame; const node: ICefDomNode); override;
function OnProcessMessageReceived(const browser: ICefBrowser; const frame: ICefFrame; sourceProcess: TCefProcessId; const aMessage : ICefProcessMessage): Boolean; override;
procedure RemoveReferences; override;
public
constructor Create(const aCefApp : TCefApplicationCore); reintroduce;
destructor Destroy; override;
@ -110,7 +114,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
TCefRenderProcessHandlerOwn(TempObject).OnWebKitInitialized;
end;
@ -122,7 +127,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
TCefRenderProcessHandlerOwn(TempObject).OnBrowserCreated(TCefBrowserRef.UnWrap(browser),
TCefDictionaryValueRef.UnWrap(extra_info));
end;
@ -134,7 +140,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
TCefRenderProcessHandlerOwn(TempObject).OnBrowserDestroyed(TCefBrowserRef.UnWrap(browser));
end;
@ -145,7 +152,8 @@ begin
Result := nil;
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
Result := CefGetData(TCefRenderProcessHandlerOwn(TempObject).GetLoadHandler);
end;
@ -158,7 +166,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
TCefRenderProcessHandlerOwn(TempObject).OnContextCreated(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefv8ContextRef.UnWrap(context));
@ -173,7 +182,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
TCefRenderProcessHandlerOwn(TempObject).OnContextReleased(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefv8ContextRef.UnWrap(context));
@ -190,7 +200,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
TCefRenderProcessHandlerOwn(TempObject).OnUncaughtException(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefv8ContextRef.UnWrap(context),
@ -207,7 +218,8 @@ var
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
TCefRenderProcessHandlerOwn(TempObject).OnFocusedNodeChanged(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefDomNodeRef.UnWrap(node));
@ -224,7 +236,8 @@ begin
Result := Ord(False);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefRenderProcessHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefRenderProcessHandlerOwn) then
Result := Ord(TCefRenderProcessHandlerOwn(TempObject).OnProcessMessageReceived(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
source_process,
@ -274,22 +287,30 @@ constructor TCefCustomRenderProcessHandler.Create(const aCefApp : TCefApplicatio
begin
inherited Create;
RemoveReferences;
FCefApp := aCefApp;
if (FCefApp <> nil) and FCefApp.MustCreateLoadHandler then
FLoadHandler := TCustomRenderLoadHandler.Create(FCefApp)
else
FLoadHandler := nil;
FLoadHandler := TCustomRenderLoadHandler.Create(FCefApp);
end;
destructor TCefCustomRenderProcessHandler.Destroy;
begin
FCefApp := nil;
FLoadHandler := nil;
RemoveReferences;
inherited Destroy;
end;
procedure TCefCustomRenderProcessHandler.RemoveReferences;
begin
if (FLoadHandler <> nil) then
FLoadHandler.RemoveReferences;
FCefApp := nil;
FLoadHandler := nil;
end;
procedure TCefCustomRenderProcessHandler.OnWebKitInitialized;
begin
try
@ -378,10 +399,10 @@ begin
end;
end;
function TCefCustomRenderProcessHandler.OnProcessMessageReceived(const browser : ICefBrowser;
const frame : ICefFrame;
sourceProcess : TCefProcessId;
const aMessage : ICefProcessMessage): Boolean;
function TCefCustomRenderProcessHandler.OnProcessMessageReceived(const browser : ICefBrowser;
const frame : ICefFrame;
sourceProcess : TCefProcessId;
const aMessage : ICefProcessMessage): Boolean;
begin
Result := inherited OnProcessMessageReceived(browser, frame, sourceProcess, aMessage);

View File

@ -58,6 +58,8 @@ type
function GetDataResource(resourceId: Integer; var data: Pointer; var dataSize: NativeUInt): Boolean; virtual; abstract;
function GetDataResourceForScale(resourceId: Integer; scaleFactor: TCefScaleFactor; var data: Pointer; var dataSize: NativeUInt): Boolean; virtual; abstract;
procedure RemoveReferences; virtual; abstract;
public
constructor Create; virtual;
end;
@ -70,6 +72,8 @@ type
function GetDataResource(resourceId: Integer; var data: Pointer; var dataSize: NativeUInt): Boolean; override;
function GetDataResourceForScale(resourceId: Integer; scaleFactor: TCefScaleFactor; var data: Pointer; var dataSize: NativeUInt): Boolean; override;
procedure RemoveReferences; override;
public
constructor Create(const aCefApp : TCefApplicationCore); reintroduce;
destructor Destroy; override;
@ -95,7 +99,8 @@ begin
Result := Ord(False);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefResourceBundleHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefResourceBundleHandlerOwn) then
begin
TempString := '';
Result := Ord(TCefResourceBundleHandlerOwn(TempObject).GetLocalizedString(string_id, TempString));
@ -118,7 +123,8 @@ begin
Result := Ord(False);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefResourceBundleHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefResourceBundleHandlerOwn) then
Result := Ord(TCefResourceBundleHandlerOwn(TempObject).GetDataResource(resource_id, data, data_size));
end;
@ -133,7 +139,8 @@ begin
Result := Ord(False);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefResourceBundleHandlerOwn) then
if (TempObject <> nil) and
(TempObject is TCefResourceBundleHandlerOwn) then
Result := Ord(TCefResourceBundleHandlerOwn(TempObject).GetDataResourceForScale(resource_id, scale_factor, data, data_size));
end;
@ -162,11 +169,16 @@ end;
destructor TCefCustomResourceBundleHandler.Destroy;
begin
FCefApp := nil;
RemoveReferences;
inherited Destroy;
end;
procedure TCefCustomResourceBundleHandler.RemoveReferences;
begin
FCefApp := nil;
end;
function TCefCustomResourceBundleHandler.GetLocalizedString( stringid : Integer;
var stringVal : ustring): Boolean;
begin

View File

@ -2,7 +2,7 @@
"UpdateLazPackages" : [
{
"ForceNotify" : true,
"InternalVersion" : 197,
"InternalVersion" : 198,
"Name" : "cef4delphi_lazarus.lpk",
"Version" : "86.0.21.0"
}