1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-02-02 10:25:26 +02:00

GlobalCEFApp initialization changes

Added a custom BrowserProcessHandler to GlobalCEFApp to know when the global context is initialized and it's allowed to set the custom cookies directory and to create browsers.
This commit is contained in:
Salvador Díaz Fau 2017-10-28 10:53:51 +02:00
parent ed8d551d85
commit bb83979092
4 changed files with 201 additions and 93 deletions

View File

@ -52,7 +52,7 @@ uses
{$ELSE}
Windows, Classes,
{$ENDIF}
uCEFTypes, uCEFInterfaces, uCEFBaseRefCounted, uCEFSchemeRegistrar;
uCEFTypes, uCEFInterfaces, uCEFBaseRefCounted, uCEFSchemeRegistrar, uCEFBrowserProcessHandler;
const
CEF_SUPPORTED_VERSION_MAJOR = 3;
@ -125,6 +125,7 @@ type
FUpdateChromeVer : boolean;
FShowMessageDlg : boolean;
FSetCurrentDir : boolean;
FGlobalContextInitialized : boolean;
FChromeVersionInfo : TFileVersionInfo;
FLibHandle : THandle;
FOnRegisterCustomSchemes : TOnRegisterCustomSchemes;
@ -135,6 +136,12 @@ type
FDeviceScaleFactor : single;
FCheckDevToolsResources : boolean;
// ICefBrowserProcessHandler
FOnContextInitializedEvent : TOnContextInitializedEvent;
FOnBeforeChildProcessLaunchEvent : TOnBeforeChildProcessLaunchEvent;
FOnRenderProcessThreadCreatedEvent : TOnRenderProcessThreadCreatedEvent;
FOnScheduleMessagePumpWorkEvent : TOnScheduleMessagePumpWorkEvent;
procedure SetFrameworkDirPath(const aValue : ustring);
procedure SetResourcesDirPath(const aValue : ustring);
procedure SetLocalesDirPath(const aValue : ustring);
@ -206,12 +213,16 @@ type
function StartSubProcess : boolean;
procedure UpdateDeviceScaleFactor;
// Internal procedures. Only TInternalApp should use them.
// Internal procedures. Only TInternalApp and TCefCustomBrowserProcessHandler should use them.
procedure Internal_OnBeforeCommandLineProcessing(const processType: ustring; const commandLine: ICefCommandLine);
procedure Internal_OnRegisterCustomSchemes(const registrar: TCefSchemeRegistrarRef);
procedure Internal_OnGetResourceBundleHandler(var aCefResourceBundleHandler : ICefResourceBundleHandler);
procedure Internal_OnGetBrowserProcessHandler(var aCefBrowserProcessHandler : ICefBrowserProcessHandler);
procedure Internal_OnGetRenderProcessHandler(var aCefRenderProcessHandler : ICefRenderProcessHandler);
procedure Internal_OnContextInitialized;
procedure Internal_OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine);
procedure Internal_OnRenderProcessThreadCreated(const extraInfo: ICefListValue);
procedure Internal_OnScheduleMessagePumpWork(const delayMs: Int64);
property Cache : ustring read FCache write FCache;
property Cookies : ustring read FCookies write FCookies;
@ -252,6 +263,7 @@ type
property CheckCEFFiles : boolean read FCheckCEFFiles write FCheckCEFFiles;
property ShowMessageDlg : boolean read FShowMessageDlg write FShowMessageDlg;
property SetCurrentDir : boolean read FSetCurrentDir write FSetCurrentDir;
property GlobalContextInitialized : boolean read FGlobalContextInitialized;
property ChromeMajorVer : uint16 read FChromeVersionInfo.MajorVer;
property ChromeMinorVer : uint16 read FChromeVersionInfo.MinorVer;
property ChromeRelease : uint16 read FChromeVersionInfo.Release;
@ -261,7 +273,6 @@ type
property ChromeElfPath : string read GetChromeElfPath;
property OnRegCustomSchemes : TOnRegisterCustomSchemes read FOnRegisterCustomSchemes write FOnRegisterCustomSchemes;
property ResourceBundleHandler : ICefResourceBundleHandler read FResourceBundleHandler write FResourceBundleHandler;
property BrowserProcessHandler : ICefBrowserProcessHandler read FBrowserProcessHandler write FBrowserProcessHandler;
property RenderProcessHandler : ICefRenderProcessHandler read FRenderProcessHandler write FRenderProcessHandler;
property SmoothScrolling : boolean read FSmoothScrolling write FSmoothScrolling;
property FastUnload : boolean read FFastUnload write FFastUnload;
@ -274,6 +285,10 @@ type
property CheckDevToolsResources : boolean read FCheckDevToolsResources write FCheckDevToolsResources;
property LocalesRequired : ustring read FLocalesRequired write FLocalesRequired;
property CustomFlashPath : ustring read FCustomFlashPath write FCustomFlashPath;
property OnContextInitialized : TOnContextInitializedEvent read FOnContextInitializedEvent write FOnContextInitializedEvent;
property OnBeforeChildProcessLaunch : TOnBeforeChildProcessLaunchEvent read FOnBeforeChildProcessLaunchEvent write FOnBeforeChildProcessLaunchEvent;
property OnRenderProcessThreadCreated : TOnRenderProcessThreadCreatedEvent read FOnRenderProcessThreadCreatedEvent write FOnRenderProcessThreadCreatedEvent;
property OnScheduleMessagePumpWork : TOnScheduleMessagePumpWorkEvent read FOnScheduleMessagePumpWorkEvent write FOnScheduleMessagePumpWorkEvent;
end;
TCefAppOwn = class(TCefBaseRefCountedOwn, ICefApp)
@ -303,6 +318,20 @@ type
destructor Destroy; override;
end;
TCefCustomBrowserProcessHandler = class(TCefBrowserProcessHandlerOwn)
protected
FCefApp : TCefApplication;
procedure OnContextInitialized; override;
procedure OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine); override;
procedure OnRenderProcessThreadCreated(const extraInfo: ICefListValue); override;
procedure OnScheduleMessagePumpWork(const delayMs: Int64); override;
public
constructor Create(const aCefApp : TCefApplication); reintroduce;
destructor Destroy; override;
end;
var
GlobalCEFApp : TCefApplication = nil;
@ -376,10 +405,17 @@ begin
FLibLoaded := False;
FShowMessageDlg := True;
FSetCurrentDir := False;
FGlobalContextInitialized := False;
FUpdateChromeVer := aUpdateChromeVer;
FCheckDevToolsResources := True;
FLocalesRequired := '';
// ICefBrowserProcessHandler
FOnContextInitializedEvent := nil;
FOnBeforeChildProcessLaunchEvent := nil;
FOnRenderProcessThreadCreatedEvent := nil;
FOnScheduleMessagePumpWorkEvent := nil;
UpdateDeviceScaleFactor;
FAppSettings.size := SizeOf(TCefSettings);
@ -413,6 +449,7 @@ begin
FCustomCommandLines := TStringList.Create;
FCustomCommandLineValues := TStringList.Create;
FBrowserProcessHandler := TCefCustomBrowserProcessHandler.Create(self);
end;
procedure TCefApplication.AddCustomCommandLine(const aCommandLine, aValue : string);
@ -705,8 +742,7 @@ begin
InitializeSettings(FAppSettings);
Result := (cef_initialize(@HInstance, @FAppSettings, aApp.Wrap, FWindowsSandboxInfo) <> 0) and
InitializeCookies;
Result := (cef_initialize(@HInstance, @FAppSettings, aApp.Wrap, FWindowsSandboxInfo) <> 0);
except
on e : exception do
if CustomExceptionHandler('TCefApplication.InitializeLibrary', e) then raise;
@ -815,6 +851,29 @@ begin
if FShowMessageDlg then MessageDlg(aError, mtError, [mbOk], 0);
end;
procedure TCefApplication.Internal_OnContextInitialized;
begin
InitializeCookies;
FGlobalContextInitialized := True;
if assigned(FOnContextInitializedEvent) then FOnContextInitializedEvent;
end;
procedure TCefApplication.Internal_OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine);
begin
if assigned(FOnBeforeChildProcessLaunchEvent) then FOnBeforeChildProcessLaunchEvent(commandLine);
end;
procedure TCefApplication.Internal_OnRenderProcessThreadCreated(const extraInfo: ICefListValue);
begin
if assigned(FOnRenderProcessThreadCreatedEvent) then FOnRenderProcessThreadCreatedEvent(extraInfo);
end;
procedure TCefApplication.Internal_OnScheduleMessagePumpWork(const delayMs: Int64);
begin
if assigned(FOnScheduleMessagePumpWorkEvent) then FOnScheduleMessagePumpWorkEvent(delayMs);
end;
procedure TCefApplication.Internal_OnBeforeCommandLineProcessing(const processType : ustring;
const commandLine : ICefCommandLine);
var
@ -1650,4 +1709,40 @@ begin
inherited Destroy;
end;
// TCefCustomBrowserProcessHandler
constructor TCefCustomBrowserProcessHandler.Create(const aCefApp : TCefApplication);
begin
inherited Create;
FCefApp := aCefApp;
end;
destructor TCefCustomBrowserProcessHandler.Destroy;
begin
FCefApp := nil;
inherited Destroy;
end;
procedure TCefCustomBrowserProcessHandler.OnContextInitialized;
begin
if (FCefApp <> nil) then FCefApp.Internal_OnContextInitialized;
end;
procedure TCefCustomBrowserProcessHandler.OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine);
begin
if (FCefApp <> nil) then FCefApp.Internal_OnBeforeChildProcessLaunch(commandLine);
end;
procedure TCefCustomBrowserProcessHandler.OnRenderProcessThreadCreated(const extraInfo: ICefListValue);
begin
if (FCefApp <> nil) then FCefApp.Internal_OnRenderProcessThreadCreated(extraInfo);
end;
procedure TCefCustomBrowserProcessHandler.OnScheduleMessagePumpWork(const delayMs: Int64);
begin
if (FCefApp <> nil) then FCefApp.Internal_OnScheduleMessagePumpWork(delayMs);
end;
end.

View File

@ -55,7 +55,8 @@ type
procedure OnContextInitialized; virtual;
procedure OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine); virtual;
procedure OnRenderProcessThreadCreated(const extraInfo: ICefListValue); virtual;
procedure OnScheduleMessagePumpWork(delayMs: Int64); virtual;
procedure OnScheduleMessagePumpWork(const delayMs: Int64); virtual;
public
constructor Create; virtual;
end;
@ -119,7 +120,7 @@ begin
end;
procedure TCefBrowserProcessHandlerOwn.OnScheduleMessagePumpWork(delayMs: Int64);
procedure TCefBrowserProcessHandlerOwn.OnScheduleMessagePumpWork(const delayMs: Int64);
begin
end;

View File

@ -924,11 +924,19 @@ begin
Result := False;
try
// GlobalCEFApp.GlobalContextInitialized has to be TRUE before creating any browser
// even if you use a custom request context.
// If you create a browser in the initialization of your app, make sure you call this
// function when GlobalCEFApp.GlobalContextInitialized is TRUE.
// Use the GlobalCEFApp.OnContextInitialized event to know when
// GlobalCEFApp.GlobalContextInitialized is set to TRUE.
if not(csDesigning in ComponentState) and
not(FClosing) and
(FBrowser = nil) and
(FBrowserId = 0) and
(GlobalCEFApp <> nil) and
GlobalCEFApp.GlobalContextInitialized and
CreateClientHandler(aParentHandle = 0) then
begin
GetSettings(FBrowserSettings);

View File

@ -122,6 +122,10 @@ type
TOnUncaughtExceptionEvent = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context; const exception: ICefV8Exception; const stackTrace: ICefV8StackTrace) {$IFNDEF DELPHI12_UP}of object{$ENDIF};
TOnFocusedNodeChangedEvent = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure(const browser: ICefBrowser; const frame: ICefFrame; const node: ICefDomNode) {$IFNDEF DELPHI12_UP}of object{$ENDIF};
TOnProcessMessageReceivedEvent = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure(const browser: ICefBrowser; sourceProcess: TCefProcessId; const message: ICefProcessMessage) {$IFNDEF DELPHI12_UP}of object{$ENDIF};
TOnContextInitializedEvent = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure() {$IFNDEF DELPHI12_UP}of object{$ENDIF};
TOnBeforeChildProcessLaunchEvent = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure(const commandLine: ICefCommandLine) {$IFNDEF DELPHI12_UP}of object{$ENDIF};
TOnRenderProcessThreadCreatedEvent = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure(const extraInfo: ICefListValue) {$IFNDEF DELPHI12_UP}of object{$ENDIF};
TOnScheduleMessagePumpWorkEvent = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure(const delayMs: Int64) {$IFNDEF DELPHI12_UP}of object{$ENDIF};
TCefCompletionCallbackProc = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure;
TCefSetCookieCallbackProc = {$IFDEF DELPHI12_UP}reference to{$ENDIF} procedure(success: Boolean);
@ -857,7 +861,7 @@ type
procedure OnContextInitialized;
procedure OnBeforeChildProcessLaunch(const commandLine: ICefCommandLine);
procedure OnRenderProcessThreadCreated(const extraInfo: ICefListValue);
procedure OnScheduleMessagePumpWork(delayMs: Int64);
procedure OnScheduleMessagePumpWork(const delayMs: Int64);
end;
ICefRenderProcessHandler = interface(ICefBaseRefCounted)