CEF4Delphi/source/uCEFApp.pas

314 lines
9.7 KiB
ObjectPascal
Raw Normal View History

2017-11-25 19:04:15 +01:00
// ************************************************************************
// ***************************** CEF4Delphi *******************************
// ************************************************************************
//
// CEF4Delphi is based on DCEF3 which uses CEF to embed a chromium-based
2017-11-25 19:04:15 +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
//
// Copyright <20> 2020 Salvador Diaz Fau. All rights reserved.
2017-11-25 19:04:15 +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 uCEFApp;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
{$IFNDEF CPUX64}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
2017-11-25 19:04:15 +01:00
{$I cef.inc}
interface
uses
{$IFDEF DELPHI16_UP}
System.Classes, System.UITypes,
2017-11-25 19:04:15 +01:00
{$ELSE}
Classes,
2017-11-25 19:04:15 +01:00
{$ENDIF}
uCEFTypes, uCEFInterfaces, uCEFBaseRefCounted, uCEFSchemeRegistrar, uCEFApplicationCore;
2017-11-25 19:04:15 +01:00
type
TCefAppOwn = class(TCefBaseRefCountedOwn, ICefApp)
protected
procedure OnBeforeCommandLineProcessing(const processType: ustring; const commandLine: ICefCommandLine); virtual; abstract;
procedure OnRegisterCustomSchemes(const registrar: TCefSchemeRegistrarRef); virtual; abstract;
procedure GetResourceBundleHandler(var aHandler : ICefResourceBundleHandler); virtual; abstract;
procedure GetBrowserProcessHandler(var aHandler : ICefBrowserProcessHandler); virtual; abstract;
procedure GetRenderProcessHandler(var aHandler : ICefRenderProcessHandler); virtual; abstract;
procedure RemoveReferences; virtual; abstract;
2017-11-25 19:04:15 +01:00
public
constructor Create; virtual;
end;
TCustomCefApp = class(TCefAppOwn)
protected
FCefApp : TCefApplicationCore;
2018-03-29 20:02:04 +02:00
FResourceBundleHandler : ICefResourceBundleHandler;
FBrowserProcessHandler : ICefBrowserProcessHandler;
FRenderProcessHandler : ICefRenderProcessHandler;
2017-11-25 19:04:15 +01:00
procedure OnBeforeCommandLineProcessing(const processType: ustring; const commandLine: ICefCommandLine); override;
procedure OnRegisterCustomSchemes(const registrar: TCefSchemeRegistrarRef); override;
procedure GetResourceBundleHandler(var aHandler : ICefResourceBundleHandler); override;
procedure GetBrowserProcessHandler(var aHandler : ICefBrowserProcessHandler); override;
procedure GetRenderProcessHandler(var aHandler : ICefRenderProcessHandler); override;
2018-03-29 20:02:04 +02:00
procedure InitializeVars;
procedure RemoveReferences; override;
2018-03-29 20:02:04 +02:00
2017-11-25 19:04:15 +01:00
public
constructor Create(const aCefApp : TCefApplicationCore); reintroduce;
destructor Destroy; override;
2017-11-25 19:04:15 +01:00
end;
implementation
uses
{$IFDEF DELPHI16_UP}
System.SysUtils,
{$ELSE}
SysUtils,
{$ENDIF}
uCEFMiscFunctions, uCEFCommandLine, uCEFConstants,
2018-03-29 20:02:04 +02:00
uCEFBrowserProcessHandler, uCEFResourceBundleHandler, uCEFRenderProcessHandler;
2017-11-25 19:04:15 +01:00
// TCefAppOwn
procedure cef_app_on_before_command_line_processing( self : PCefApp;
const process_type : PCefString;
command_line : PCefCommandLine); stdcall;
2017-11-25 19:04:15 +01:00
var
TempObject : TObject;
begin
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefAppOwn) then
2018-03-29 20:02:04 +02:00
TCefAppOwn(TempObject).OnBeforeCommandLineProcessing(CefString(process_type),
TCefCommandLineRef.UnWrap(command_line));
2017-11-25 19:04:15 +01:00
end;
procedure cef_app_on_register_custom_schemes(self : PCefApp;
registrar : PCefSchemeRegistrar); stdcall;
2017-11-25 19:04:15 +01:00
var
TempWrapper : TCefSchemeRegistrarRef;
TempObject : TObject;
begin
TempWrapper := nil;
try
try
TempWrapper := TCefSchemeRegistrarRef.Create(registrar);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefAppOwn) then
TCefAppOwn(TempObject).OnRegisterCustomSchemes(TempWrapper);
except
on e : exception do
if CustomExceptionHandler('cef_app_on_register_custom_schemes', e) then raise;
end;
finally
if (TempWrapper <> nil) then FreeAndNil(TempWrapper);
end;
end;
function cef_app_get_resource_bundle_handler(self: PCefApp): PCefResourceBundleHandler; stdcall;
var
TempObject : TObject;
TempHandler : ICefResourceBundleHandler;
begin
Result := nil;
TempHandler := nil;
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefAppOwn) then
2018-03-29 20:02:04 +02:00
try
2017-11-25 19:04:15 +01:00
TCefAppOwn(TempObject).GetResourceBundleHandler(TempHandler);
2018-03-29 20:02:04 +02:00
if (TempHandler <> nil) then Result := TempHandler.Wrap;
finally
TempHandler := nil;
2017-11-25 19:04:15 +01:00
end;
end;
function cef_app_get_browser_process_handler(self: PCefApp): PCefBrowserProcessHandler; stdcall;
var
TempObject : TObject;
TempHandler : ICefBrowserProcessHandler;
begin
Result := nil;
TempHandler := nil;
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefAppOwn) then
2018-03-29 20:02:04 +02:00
try
2017-11-25 19:04:15 +01:00
TCefAppOwn(TempObject).GetBrowserProcessHandler(TempHandler);
2018-03-29 20:02:04 +02:00
if (TempHandler <> nil) then Result := TempHandler.Wrap;
finally
TempHandler := nil;
2017-11-25 19:04:15 +01:00
end;
end;
function cef_app_get_render_process_handler(self: PCefApp): PCefRenderProcessHandler; stdcall;
var
TempObject : TObject;
TempHandler : ICefRenderProcessHandler;
begin
Result := nil;
TempHandler := nil;
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefAppOwn) then
2018-03-29 20:02:04 +02:00
try
2017-11-25 19:04:15 +01:00
TCefAppOwn(TempObject).GetRenderProcessHandler(TempHandler);
2018-03-29 20:02:04 +02:00
if (TempHandler <> nil) then Result := TempHandler.Wrap;
finally
TempHandler := nil;
2017-11-25 19:04:15 +01:00
end;
end;
constructor TCefAppOwn.Create;
begin
inherited CreateData(SizeOf(TCefApp));
with PCefApp(FData)^ do
begin
on_before_command_line_processing := {$IFDEF FPC}@{$ENDIF}cef_app_on_before_command_line_processing;
on_register_custom_schemes := {$IFDEF FPC}@{$ENDIF}cef_app_on_register_custom_schemes;
get_resource_bundle_handler := {$IFDEF FPC}@{$ENDIF}cef_app_get_resource_bundle_handler;
get_browser_process_handler := {$IFDEF FPC}@{$ENDIF}cef_app_get_browser_process_handler;
get_render_process_handler := {$IFDEF FPC}@{$ENDIF}cef_app_get_render_process_handler;
2017-11-25 19:04:15 +01:00
end;
end;
// TCustomCefApp
constructor TCustomCefApp.Create(const aCefApp : TCefApplicationCore);
2017-11-25 19:04:15 +01:00
begin
inherited Create;
2018-03-29 20:02:04 +02:00
InitializeVars;
FCefApp := aCefApp;
2018-03-29 20:02:04 +02:00
if (FCefApp <> nil) then
begin
if FCefApp.MustCreateBrowserProcessHandler then
FBrowserProcessHandler := TCefCustomBrowserProcessHandler.Create(FCefApp);
if FCefApp.MustCreateResourceBundleHandler then
FResourceBundleHandler := TCefCustomResourceBundleHandler.Create(FCefApp);
if FCefApp.MustCreateRenderProcessHandler then
FRenderProcessHandler := TCefCustomRenderProcessHandler.Create(FCefApp);
end;
2017-11-25 19:04:15 +01:00
end;
destructor TCustomCefApp.Destroy;
2017-11-25 19:04:15 +01:00
begin
RemoveReferences;
2018-03-29 20:02:04 +02:00
inherited Destroy;
2018-03-29 20:02:04 +02:00
end;
procedure TCustomCefApp.InitializeVars;
begin
FCefApp := nil;
2018-03-29 20:02:04 +02:00
FResourceBundleHandler := nil;
FBrowserProcessHandler := nil;
FRenderProcessHandler := nil;
2017-11-25 19:04:15 +01:00
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;
2017-11-25 19:04:15 +01:00
procedure TCustomCefApp.OnBeforeCommandLineProcessing(const processType: ustring; const commandLine: ICefCommandLine);
begin
2018-03-29 20:02:04 +02:00
try
if (FCefApp <> nil) then FCefApp.Internal_OnBeforeCommandLineProcessing(processType, commandLine);
except
on e : exception do
if CustomExceptionHandler('TCustomCefApp.OnBeforeCommandLineProcessing', e) then raise;
end;
2017-11-25 19:04:15 +01:00
end;
procedure TCustomCefApp.OnRegisterCustomSchemes(const registrar: TCefSchemeRegistrarRef);
begin
2018-03-29 20:02:04 +02:00
try
if (FCefApp <> nil) then FCefApp.Internal_OnRegisterCustomSchemes(registrar);
except
on e : exception do
if CustomExceptionHandler('TCustomCefApp.OnRegisterCustomSchemes', e) then raise;
end;
2017-11-25 19:04:15 +01:00
end;
procedure TCustomCefApp.GetResourceBundleHandler(var aHandler : ICefResourceBundleHandler);
begin
2018-03-29 20:02:04 +02:00
if (FResourceBundleHandler <> nil) then
aHandler := FResourceBundleHandler
2017-11-25 19:04:15 +01:00
else
aHandler := nil;
end;
procedure TCustomCefApp.GetBrowserProcessHandler(var aHandler : ICefBrowserProcessHandler);
begin
2018-03-29 20:02:04 +02:00
if (FBrowserProcessHandler <> nil) then
aHandler := FBrowserProcessHandler
2017-11-25 19:04:15 +01:00
else
aHandler := nil;
end;
procedure TCustomCefApp.GetRenderProcessHandler(var aHandler : ICefRenderProcessHandler);
begin
2018-03-29 20:02:04 +02:00
if (FRenderProcessHandler <> nil) then
aHandler := FRenderProcessHandler
2017-11-25 19:04:15 +01:00
else
aHandler := nil;
end;
end.