Added SubProcess and PopupBrowser demos for QT6 in Linux

This commit is contained in:
Salvador Díaz Fau
2025-12-17 15:49:22 +01:00
parent 81ec2fea92
commit 1ba5073e42
23 changed files with 1826 additions and 5 deletions
@@ -0,0 +1,2 @@
rmdir /S /Q lib
rmdir /S /Q backup
Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="12"/>
<General>
<SessionStorage Value="InProjectDir"/>
<Title Value="PopupBrowser"/>
<Scaled Value="True"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<XPManifest>
<DpiAware Value="True"/>
</XPManifest>
<Icon Value="0"/>
</General>
<MacroValues Count="1">
<Macro1 Name="LCLWidgetType" Value="qt6"/>
</MacroValues>
<BuildModes>
<Item Name="Default" Default="True"/>
<SharedMatrixOptions Count="1">
<Item1 ID="204233492819" Modes="Default" Type="IDEMacro" MacroName="LCLWidgetType" Value="qt6"/>
</SharedMatrixOptions>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<RequiredPackages>
<Item>
<PackageName Value="CEF4Delphi_Lazarus"/>
</Item>
<Item>
<PackageName Value="LCL"/>
</Item>
</RequiredPackages>
<Units>
<Unit>
<Filename Value="PopupBrowser.lpr"/>
<IsPartOfProject Value="True"/>
</Unit>
<Unit>
<Filename Value="umainform.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="MainForm"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="uMainForm"/>
</Unit>
<Unit>
<Filename Value="interfaces.pas"/>
<IsPartOfProject Value="True"/>
</Unit>
<Unit>
<Filename Value="uchildform.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="ChildForm"/>
<ResourceBaseClass Value="Form"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="../../../bin/PopupBrowser"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf3"/>
</Debugging>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
<Other>
<CustomOptions Value="-dUseCThreads"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions>
<Item>
<Name Value="EAbort"/>
</Item>
<Item>
<Name Value="ECodetoolError"/>
</Item>
<Item>
<Name Value="EFOpenError"/>
</Item>
</Exceptions>
</Debugging>
</CONFIG>
@@ -0,0 +1,43 @@
program PopupBrowser;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
{$IFDEF HASAMIGA}
athreads,
{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, uMainForm
{ you can add units after this },
uCEFApplication, uchildform;
{$R *.res}
begin
CreateGlobalCEFApp;
if StartMainProcess then
begin
// The LCL Widgetset must be initialized after the CEF initialization and
// only in the browser process.
CustomWidgetSetInitialization;
RequireDerivedFormResource:=True;
Application.Scaled:=True;
{$PUSH}{$WARN 5044 OFF}
Application.MainFormOnTaskbar:=True;
{$POP}
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.CreateForm(TChildForm, ChildForm);
Application.Run;
CustomWidgetSetFinalization;
end;
DestroyGlobalCEFApp;
end.
@@ -0,0 +1,66 @@
{
/***************************************************************************
Interfaces.pp - determines what interface to use
-------------------
Initial Revision : Thu July 1st CST 1999
***************************************************************************/
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit interfaces;
{$mode objfpc}
{$H+}
interface
uses
{$IFDEF UNIX}{$IFNDEF DisableCWString}cwstring,{$ENDIF}{$ENDIF}
InterfaceBase;
procedure CustomWidgetSetInitialization;
procedure CustomWidgetSetFinalization;
implementation
uses
qtint, Forms, xlib;
function CustomX11ErrorHandler(Display:PDisplay; ErrorEv:PXErrorEvent):longint;cdecl;
begin
{$IFDEF DEBUG}
XError := ErrorEv^.error_code;
WriteLn('Error: ' + IntToStr(XError));
{$ENDIF}
Result := 0;
end;
function CustomXIOErrorHandler(Display:PDisplay):longint;cdecl;
begin
Result := 0;
end;
procedure CustomWidgetSetInitialization;
begin
CreateWidgetset(TQtWidgetSet);
// Install xlib error handlers so that the application won't be terminated
// on non-fatal errors. Must be done after initializing QT.
XSetErrorHandler(@CustomX11ErrorHandler);
XSetIOErrorHandler(@CustomXIOErrorHandler);
end;
procedure CustomWidgetSetFinalization;
begin
FreeWidgetSet;
end;
end.
@@ -0,0 +1,34 @@
object ChildForm: TChildForm
Left = 1520
Height = 240
Top = 250
Width = 320
Caption = 'ChildForm'
ClientHeight = 240
ClientWidth = 320
LCLVersion = '4.4.0.0'
OnClose = FormClose
OnCloseQuery = FormCloseQuery
OnCreate = FormCreate
OnDestroy = FormDestroy
object CEFLinkedWindowParent1: TCEFLinkedWindowParent
Left = 0
Height = 240
Top = 0
Width = 320
Align = alClient
TabStop = True
TabOrder = 0
OnEnter = CEFLinkedWindowParent1Enter
OnExit = CEFLinkedWindowParent1Exit
Chromium = Chromium1
end
object Chromium1: TChromium
OnGotFocus = Chromium1GotFocus
OnTitleChange = Chromium1TitleChange
OnBeforePopup = Chromium1BeforePopup
OnBeforeClose = Chromium1BeforeClose
Left = 134
Top = 97
end
end
@@ -0,0 +1,240 @@
unit uchildform;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
LMessages, SyncObjs,
uCEFChromium, uCEFTypes, uCEFInterfaces, uCEFConstants, uCEFWindowParent,
uCEFWinControl, uCEFLinkedWindowParent, uCEFChromiumEvents;
type
{ TChildForm }
TChildForm = class(TForm)
CEFLinkedWindowParent1: TCEFLinkedWindowParent;
Chromium1: TChromium;
procedure CEFLinkedWindowParent1Enter(Sender: TObject);
procedure CEFLinkedWindowParent1Exit(Sender: TObject);
procedure Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser);
procedure Chromium1BeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; popup_id: Integer; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean; var Result: Boolean);
procedure Chromium1GotFocus(Sender: TObject; const browser: ICefBrowser);
procedure Chromium1TitleChange(Sender: TObject; const browser: ICefBrowser; const title: ustring);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
protected
FCanClose : boolean;
FClosing : boolean;
FClientInitialized : boolean;
FPopupFeatures : TCefPopupFeatures;
FCaption : ustring;
procedure WMMove(var aMessage: TLMMove); message LM_MOVE;
procedure WMSize(var aMessage: TLMSize); message LM_SIZE;
procedure WMWindowPosChanged(var aMessage: TLMWindowPosChanged); message LM_WINDOWPOSCHANGED;
procedure BrowserCloseFormMsg(Data: PtrInt);
procedure BrowserSetFocusMsg(Data: PtrInt);
procedure BrowserTitleChangeMsg(Data: PtrInt);
public
function CreateClientHandler(var windowInfo : TCefWindowInfo; var client : ICefClient; const targetFrameName : string; const popupFeatures : TCefPopupFeatures) : boolean;
procedure ApplyPopupFeatures;
procedure SendCompMessage(aMsg : cardinal; aData : PtrInt = 0);
property ClientInitialized : boolean read FClientInitialized;
property Closing : boolean read FClosing;
end;
var
ChildForm: TChildForm;
implementation
{$R *.lfm}
uses
Math,
uCEFMiscFunctions, uCEFApplication, uCEFWindowInfoWrapper, uMainForm;
{ TChildForm }
function TChildForm.CreateClientHandler(var windowInfo : TCefWindowInfo;
var client : ICefClient;
const targetFrameName : string;
const popupFeatures : TCefPopupFeatures) : boolean;
var
TempRect : TRect;
begin
if Chromium1.CreateClientHandler(client, False) then
begin
Result := True;
FClientInitialized := True;
FPopupFeatures := popupFeatures;
TempRect := CEFLinkedWindowParent1.BoundsRect;
if (FPopupFeatures.widthset <> 0) then TempRect.Right := max(FPopupFeatures.width, 100);
if (FPopupFeatures.heightset <> 0) then TempRect.Bottom := max(FPopupFeatures.height, 100);
TCEFWindowInfoWrapper.AsChild(windowInfo, CEFLinkedWindowParent1.Handle, TempRect);
end
else
Result := False;
end;
procedure TChildForm.ApplyPopupFeatures;
begin
if (FPopupFeatures.xset <> 0) then Chromium1.SetFormLeftTo(FPopupFeatures.x);
if (FPopupFeatures.yset <> 0) then Chromium1.SetFormTopTo(FPopupFeatures.y);
if (FPopupFeatures.widthset <> 0) then Chromium1.ResizeFormWidthTo(FPopupFeatures.width);
if (FPopupFeatures.heightset <> 0) then Chromium1.ResizeFormHeightTo(FPopupFeatures.height);
end;
// This is a workaround for the CEF issue #2026
// https://bitbucket.org/chromiumembedded/cef/issues/2026/multiple-major-keyboard-focus-issues-on
// We use CEFLinkedWindowParent1.OnEnter, CEFLinkedWindowParent1.OnExit and
// TChromium.OnGotFocus to avoid most of the focus issues.
// CEFLinkedWindowParent1.TabStop must be TRUE.
procedure TChildForm.CEFLinkedWindowParent1Enter(Sender: TObject);
begin
if not(csDesigning in ComponentState) and
Chromium1.Initialized and
not(Chromium1.FrameIsFocused) then
Chromium1.SetFocus(True);
end;
procedure TChildForm.CEFLinkedWindowParent1Exit(Sender: TObject);
begin
if not(csDesigning in ComponentState) then
Chromium1.SendCaptureLostEvent;
end;
procedure TChildForm.Chromium1BeforeClose(Sender: TObject;
const browser: ICefBrowser);
begin
// We must wait until all browsers trigger the TChromium.OnBeforeClose event
// in order to close the application safely or we will have shutdown issues.
FCanClose := True;
SendCompMessage(CEF_BEFORECLOSE);
end;
procedure TChildForm.Chromium1BeforePopup(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; popup_id: Integer;
const targetUrl, targetFrameName: ustring;
targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean;
const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo;
var client: ICefClient; var settings: TCefBrowserSettings;
var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean;
var Result: Boolean);
begin
case targetDisposition of
CEF_WOD_NEW_FOREGROUND_TAB,
CEF_WOD_NEW_BACKGROUND_TAB,
CEF_WOD_NEW_WINDOW : Result := True; // For simplicity, this demo blocks new tabs and new windows.
CEF_WOD_NEW_POPUP : Result := not(TMainForm(Owner).CreateClientHandler(windowInfo, client, targetFrameName, popupFeatures));
else Result := False;
end;
end;
procedure TChildForm.Chromium1GotFocus(Sender: TObject;
const browser: ICefBrowser);
begin
SendCompMessage(CEF_SETFOCUS);
end;
procedure TChildForm.Chromium1TitleChange(Sender: TObject;
const browser: ICefBrowser; const title: ustring);
begin
FCaption := title;
SendCompMessage(CEF_TITLECHANGE);
end;
procedure TChildForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
CloseAction := caFree;
end;
procedure TChildForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := FCanClose;
if not(FClosing) then
begin
FClosing := True;
Visible := False;
Chromium1.CloseBrowser(True);
FreeAndNil(CEFLinkedWindowParent1);
end;
end;
procedure TChildForm.FormCreate(Sender: TObject);
begin
FCanClose := False;
FClosing := False;
FClientInitialized := False;
// CEF requires a native widget
CEFLinkedWindowParent1.SetQTWidgetAsNative;
end;
procedure TChildForm.FormDestroy(Sender: TObject);
begin
if FClientInitialized then
TMainForm(Owner).SendCompMessage(CEF_CHILDDESTROYED);
end;
procedure TChildForm.WMMove(var aMessage : TLMMove);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TChildForm.WMSize(var aMessage: TLMSize);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TChildForm.WMWindowPosChanged(var aMessage: TLMWindowPosChanged);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TChildForm.BrowserCloseFormMsg(Data: PtrInt);
begin
Close;
end;
procedure TChildForm.BrowserSetFocusMsg(Data: PtrInt);
begin
CEFLinkedWindowParent1.SetFocus;
end;
procedure TChildForm.BrowserTitleChangeMsg(Data: PtrInt);
begin
Caption := UTF8Encode(FCaption);
end;
procedure TChildForm.SendCompMessage(aMsg : cardinal; aData : PtrInt);
begin
case aMsg of
CEF_BEFORECLOSE : Application.QueueAsyncCall(@BrowserCloseFormMsg, aData);
CEF_SETFOCUS : Application.QueueAsyncCall(@BrowserSetFocusMsg, aData);
CEF_TITLECHANGE : Application.QueueAsyncCall(@BrowserTitleChangeMsg, aData);
end;
end;
end.
@@ -0,0 +1,126 @@
object MainForm: TMainForm
Left = 342
Height = 768
Top = 31
Width = 1024
Caption = 'Simple Browser. Initializing...'
ClientHeight = 768
ClientWidth = 1024
Position = poScreenCenter
LCLVersion = '4.4.0.0'
OnActivate = FormActivate
OnCloseQuery = FormCloseQuery
OnCreate = FormCreate
object CEFLinkedWindowParent1: TCEFLinkedWindowParent
Left = 0
Height = 742
Top = 26
Width = 1024
Align = alClient
TabStop = True
TabOrder = 1
OnEnter = CEFLinkedWindowParent1Enter
OnExit = CEFLinkedWindowParent1Exit
Chromium = Chromium1
end
object AddressPnl: TPanel
Left = 0
Height = 26
Top = 0
Width = 1024
Align = alTop
BevelOuter = bvNone
ClientHeight = 26
ClientWidth = 1024
TabOrder = 0
object AddressCb: TComboBox
Left = 0
Height = 26
Top = 0
Width = 965
Align = alClient
ItemHeight = 0
ItemIndex = 0
Items.Strings = (
'https://www.google.com'
'https://www.bing.com'
'https://duckduckgo.com'
'https://www.qwant.com'
'https://yandex.com'
'https://www.startpage.com'
'https://www.ecosia.org'
'https://www.baidu.com'
'https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending'
'https://www.w3schools.com/js/tryit.asp?filename=tryjs_win_close'
'https://www.w3schools.com/js/tryit.asp?filename=tryjs_alert'
'https://www.w3schools.com/js/tryit.asp?filename=tryjs_loc_assign'
'https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_style_backgroundcolor'
'https://www.w3schools.com/Tags/tryit.asp?filename=tryhtml_iframe_name'
'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_file'
'https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_state_throw_error'
'https://www.htmlquick.com/es/reference/tags/input-file.html'
'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file'
'https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory'
'https://www.w3schools.com/html/html5_video.asp'
'http://html5test.com/'
'https://webrtc.github.io/samples/src/content/devices/input-output/'
'https://test.webrtc.org/'
'https://www.browserleaks.com/webrtc'
'https://shaka-player-demo.appspot.com/demo/'
'http://webglsamples.org/'
'https://get.webgl.org/'
'https://www.briskbard.com'
'https://www.youtube.com'
'https://html5demos.com/drag/'
'https://frames-per-second.appspot.com/'
'https://www.sede.fnmt.gob.es/certificados/persona-fisica/verificar-estado'
'https://www.kirupa.com/html5/accessing_your_webcam_in_html5.htm'
'https://www.xdumaine.com/enumerateDevices/test/'
'https://dagrs.berkeley.edu/sites/default/files/2020-01/sample.pdf'
'https://codepen.io/udaymanvar/pen/MWaePBY'
'https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept'
'chrome://version/'
'chrome://settings/'
'chrome://flags/'
'chrome://net-internals/'
'chrome://tracing/'
'chrome://appcache-internals/'
'chrome://blob-internals/'
'chrome://view-http-cache/'
'chrome://credits/'
'chrome://histograms/'
'chrome://media-internals/'
'chrome://kill'
'chrome://crash'
'chrome://hang'
'chrome://shorthang'
'chrome://gpuclean'
'chrome://gpucrash'
'chrome://gpuhang'
'chrome://extensions-support'
'chrome://process-internals'
)
TabOrder = 0
Text = 'https://www.google.com'
end
object GoBtn: TButton
Left = 965
Height = 26
Top = 0
Width = 59
Align = alRight
Caption = 'Go'
TabOrder = 1
OnClick = GoBtnClick
end
end
object Chromium1: TChromium
OnGotFocus = Chromium1GotFocus
OnBeforePopup = Chromium1BeforePopup
OnAfterCreated = Chromium1AfterCreated
OnBeforeClose = Chromium1BeforeClose
OnOpenUrlFromTab = Chromium1OpenUrlFromTab
Left = 106
Top = 69
end
end
@@ -0,0 +1,455 @@
unit uMainForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, SyncObjs, Dialogs, ExtCtrls,
LMessages, StdCtrls,
uCEFChromium, uCEFLinkedWindowParent, uCEFInterfaces, uCEFChromiumEvents,
uCEFTypes, uchildform;
const
CEF_CREATENEXTCHILD = $A50;
CEF_CHILDDESTROYED = $A51;
CEF_CLOSECHILD = $A52;
CEF_SETFOCUS = $A53;
CEF_TITLECHANGE = $A54;
type
{ TMainForm }
TMainForm = class(TForm)
GoBtn: TButton;
CEFLinkedWindowParent1: TCEFLinkedWindowParent;
Chromium1: TChromium;
AddressCb: TComboBox;
AddressPnl: TPanel;
procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
procedure Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser);
procedure Chromium1BeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; popup_id: Integer; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean; var Result: Boolean);
procedure Chromium1GotFocus(Sender: TObject; const browser: ICefBrowser);
procedure Chromium1OpenUrlFromTab(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; out Result: Boolean);
procedure FormActivate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormCreate(Sender: TObject);
procedure GoBtnClick(Sender: TObject);
procedure CEFLinkedWindowParent1Enter(Sender: TObject);
procedure CEFLinkedWindowParent1Exit(Sender: TObject);
private
protected
FChildForm : TChildForm;
FCriticalSection : TCriticalSection;
FCanClose : boolean; // Set to True in TChromium.OnBeforeClose
FClosingMainForm : boolean; // Set to True in the CloseQuery event.
FClosingChildren : boolean; // Set to True in the CloseQuery event.
function GetPopupChildCount : integer;
// CEF needs to handle these messages to call TChromium.NotifyMoveOrResizeStarted
procedure WMMove(var Message: TLMMove); message LM_MOVE;
procedure WMSize(var Message: TLMSize); message LM_SIZE;
procedure WMWindowPosChanged(var Message: TLMWindowPosChanged); message LM_WINDOWPOSCHANGED;
procedure ClosePopupChildren;
procedure CreateHiddenChildForm;
procedure BrowserCreatedMsg(Data: PtrInt);
procedure BrowserCreateNextChildMsg(Data: PtrInt);
procedure BrowserChildDestroyedMsg(Data: PtrInt);
procedure BrowserCloseFormMsg(Data: PtrInt);
procedure BrowserSetFocusMsg(Data: PtrInt);
property PopupChildCount : integer read GetPopupChildCount;
public
function CreateClientHandler(var windowInfo : TCefWindowInfo; var client : ICefClient; const targetFrameName : string; const popupFeatures : TCefPopupFeatures) : boolean;
procedure SendCompMessage(aMsg : cardinal; aData: PtrInt = 0);
end;
var
MainForm: TMainForm;
procedure CreateGlobalCEFApp;
function StartMainProcess: boolean;
implementation
{$R *.lfm}
// This is a demo with the simplest web browser you can build using CEF4Delphi and
// it doesn't show any sign of progress like other web browsers do.
// Remember that it may take a few seconds to load if Windows update, your antivirus or
// any other windows service is using your hard drive.
// Depending on your internet connection it may take longer than expected.
// Please check that your firewall or antivirus are not blocking this application
// or the domain "google.com". If you don't live in the US, you'll be redirected to
// another domain which will take a little time too.
// This demo uses a TChromium and a TCEFLinkedWindowParent
// We need to use TCEFLinkedWindowParent in Linux to update the browser
// visibility and size automatically.
// Most of the TChromium events are executed in a CEF thread and this causes
// issues with most QT API functions. If you need to update the GUI, store the
// TChromium event parameters and use SendCompMessage (Application.QueueAsyncCall)
// to do it in the main application thread.
// Destruction steps
// =================
// 1. FormCloseQuery sets CanClose to FALSE, destroys CEFLinkedWindowParent1 and calls TChromium.CloseBrowser which triggers the TChromium.OnBeforeClose event.
// 2. TChromium.OnBeforeClose sets FCanClose := True and sends CEF_BEFORECLOSE to close the form.
uses
Math,
uCEFMiscFunctions, uCEFApplication, uCEFLinuxFunctions, uCEFConstants;
var
MainAppEvent : TEventObject;
{GlobalCEFApp functions}
{%Region}
procedure GlobalCEFApp_OnContextInitialized();
begin
MainAppEvent.SetEvent;
end;
procedure CreateGlobalCEFApp;
begin
GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.LogFile := 'debug.log';
GlobalCEFApp.LogSeverity := LOGSEVERITY_INFO;
GlobalCEFApp.RootCache := 'RootCache';
GlobalCEFApp.Cache := IncludeTrailingPathDelimiter(GlobalCEFApp.RootCache) + 'cache';
GlobalCEFApp.SetCurrentDir := True;
GlobalCEFApp.DisableZygote := True;
GlobalCEFApp.EnableGPU := True;
GlobalCEFApp.EnablePrintPreview := True;
GlobalCEFApp.OnContextInitialized := @GlobalCEFApp_OnContextInitialized;
end;
function StartMainProcess: boolean;
begin
Result := False;
if GlobalCEFApp.StartMainProcess then
begin
// Wait until the context is initialized before initializing GTK.
if (MainAppEvent.WaitFor(10000) = wrTimeout) then
CefDebugLog('CEF initialization failure!')
else
Result := True;
end;
end;
{%Endregion}
{TForm events}
{%Region}
procedure TMainForm.FormCreate(Sender: TObject);
begin
FClosingChildren := False;
FClosingMainForm := False;
FCanClose := False;
FCriticalSection := TCriticalSection.Create;
// CEF requires a native widget
CEFLinkedWindowParent1.SetQTWidgetAsNative;
// CEF can't find the HTML if we load file:///filename.html in Linux so we
// add the full path manually.
// The "Click me to open a file" button in PopupBrowser.html will not work
// because of this limitation.
AddressCb.Text := 'file://' + IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0))) + 'PopupBrowser.html';
Chromium1.DefaultURL := UTF8Decode(AddressCb.Text);
CreateHiddenChildForm;
end;
procedure TMainForm.FormActivate(Sender: TObject);
var
TempRect : TRect;
begin
TempRect := Rect(0, 0, CEFLinkedWindowParent1.Width, CEFLinkedWindowParent1.Height);
Chromium1.CreateBrowser(CEFLinkedWindowParent1.Handle, TempRect);
end;
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
FClosingChildren := True;
Visible := False;
if (PopupChildCount > 0) then
begin
ClosePopupChildren;
CanClose := False;
end
else
begin
CanClose := FCanClose;
if not(FClosingMainForm) then
begin
FClosingMainForm := True;
Chromium1.CloseBrowser(True);
FreeAndNil(CEFLinkedWindowParent1);
end;
end;
end;
{%Endregion}
{TCEFLinkedWindowParent events}
{%Region}
procedure TMainForm.CEFLinkedWindowParent1Enter(Sender: TObject);
begin
if not(csDesigning in ComponentState) and
Chromium1.Initialized and
not(Chromium1.FrameIsFocused) then
Chromium1.SetFocus(True);
end;
// This is a workaround for the CEF issue #2026
// https://bitbucket.org/chromiumembedded/cef/issues/2026/multiple-major-keyboard-focus-issues-on
// We use CEFLinkedWindowParent1.OnEnter, CEFLinkedWindowParent1.OnExit and
// TChromium.OnGotFocus to avoid most of the focus issues.
// CEFLinkedWindowParent1.TabStop must be TRUE.
procedure TMainForm.CEFLinkedWindowParent1Exit(Sender: TObject);
begin
if not(csDesigning in ComponentState) then
Chromium1.SendCaptureLostEvent;
end;
{%Endregion}
{Message handlers}
{%Region}
procedure TMainForm.BrowserCreatedMsg(Data: PtrInt);
begin
Caption := 'PopupBrowser';
AddressPnl.Enabled := True;
Chromium1.UpdateXWindowVisibility(True);
CEFLinkedWindowParent1.UpdateSize;
CEFLinkedWindowParent1.InvalidateChildren;
end;
procedure TMainForm.BrowserCreateNextChildMsg(Data: PtrInt);
begin
try
FCriticalSection.Acquire;
if (FChildForm <> nil) then
begin
FChildForm.ApplyPopupFeatures;
FChildForm.Show;
end;
CreateHiddenChildForm;
finally
FCriticalSection.Release;
end;
end;
procedure TMainForm.BrowserChildDestroyedMsg(Data: PtrInt);
begin
if FClosingChildren and (PopupChildCount = 0) then Close;
end;
procedure TMainForm.BrowserCloseFormMsg(Data: PtrInt);
begin
Close;
end;
procedure TMainForm.BrowserSetFocusMsg(Data: PtrInt);
begin
if assigned(CEFLinkedWindowParent1) then
CEFLinkedWindowParent1.SetFocus;
end;
procedure TMainForm.WMMove(var Message: TLMMove);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TMainForm.WMSize(var Message: TLMSize);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TMainForm.WMWindowPosChanged(var Message: TLMWindowPosChanged);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
{%Endregion}
{Misc functions}
{%Region}
function TMainForm.GetPopupChildCount : integer;
var
i : integer;
TempForm : TCustomForm;
begin
Result := 0;
i := pred(screen.CustomFormCount);
while (i >= 0) do
begin
TempForm := screen.CustomForms[i];
if (TempForm is TChildForm) and
TChildForm(TempForm).ClientInitialized then
inc(Result);
dec(i);
end;
end;
procedure TMainForm.ClosePopupChildren;
var
i : integer;
TempForm : TCustomForm;
begin
i := pred(screen.CustomFormCount);
while (i >= 0) do
begin
TempForm := screen.CustomForms[i];
if (TempForm is TChildForm) and
TChildForm(TempForm).ClientInitialized and
not(TChildForm(TempForm).Closing) then
TempForm.Close;
dec(i);
end;
end;
procedure TMainForm.CreateHiddenChildForm;
var
TempSize : TCefSize;
begin
// Linux requires a fully formed window in order to add a Chromium browser so
// we show the next popup window outside the visible screen space and then we
// hide it.
FChildForm := TChildForm.Create(self);
TempSize.width := FChildForm.Width;
TempSize.height := FChildForm.Height;
FChildForm.Width := 0;
FChildForm.Height := 0;
FChildForm.Show;
FChildForm.Hide;
FChildForm.Width := TempSize.width;
FChildForm.Height := TempSize.height;
// Center the child form on the screen by default
FChildForm.Top := (screen.Height - FChildForm.Height) div 2;
FChildForm.Left := (screen.Width - FChildForm.Width) div 2;
end;
function TMainForm.CreateClientHandler(var windowInfo : TCefWindowInfo;
var client : ICefClient;
const targetFrameName : string;
const popupFeatures : TCefPopupFeatures) : boolean;
begin
try
FCriticalSection.Acquire;
if (FChildForm <> nil) and
FChildForm.CreateClientHandler(windowInfo, client, targetFrameName, popupFeatures) then
begin
SendCompMessage(CEF_CREATENEXTCHILD);
Result := True;
end;
finally
FCriticalSection.Release;
end;
end;
procedure TMainForm.SendCompMessage(aMsg : cardinal; aData: PtrInt);
begin
case aMsg of
CEF_AFTERCREATED : Application.QueueAsyncCall(@BrowserCreatedMsg, aData);
CEF_CREATENEXTCHILD : Application.QueueAsyncCall(@BrowserCreateNextChildMsg, aData);
CEF_CHILDDESTROYED : Application.QueueAsyncCall(@BrowserChildDestroyedMsg, aData);
CEF_BEFORECLOSE : Application.QueueAsyncCall(@BrowserCloseFormMsg, aData);
CEF_SETFOCUS : Application.QueueAsyncCall(@BrowserSetFocusMsg, aData);
end;
end;
procedure TMainForm.GoBtnClick(Sender: TObject);
begin
Chromium1.LoadURL(UTF8Decode(AddressCb.Text));
end;
{%Endregion}
{TChromium events}
{%Region}
procedure TMainForm.Chromium1AfterCreated(Sender: TObject;
const browser: ICefBrowser);
begin
// Now the browser is fully initialized we can initialize the UI.
SendCompMessage(CEF_AFTERCREATED);
end;
procedure TMainForm.Chromium1BeforeClose(Sender: TObject;
const browser: ICefBrowser);
begin
// We must wait until all browsers trigger the TChromium.OnBeforeClose event
// in order to close the application safely or we will have shutdown issues.
FCanClose := True;
SendCompMessage(CEF_BEFORECLOSE);
end;
procedure TMainForm.Chromium1BeforePopup(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; popup_id: Integer;
const targetUrl, targetFrameName: ustring;
targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean;
const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo;
var client: ICefClient; var settings: TCefBrowserSettings;
var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean;
var Result: Boolean);
begin
case targetDisposition of
CEF_WOD_NEW_FOREGROUND_TAB,
CEF_WOD_NEW_BACKGROUND_TAB,
CEF_WOD_NEW_WINDOW : Result := True; // For simplicity, this demo blocks new tabs and new windows.
CEF_WOD_NEW_POPUP : Result := not(CreateClientHandler(windowInfo, client, targetFrameName, popupFeatures));
else Result := False;
end;
end;
procedure TMainForm.Chromium1GotFocus(Sender: TObject;
const browser: ICefBrowser);
begin
SendCompMessage(CEF_SETFOCUS);
end;
procedure TMainForm.Chromium1OpenUrlFromTab(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl: ustring;
targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; out
Result: Boolean);
begin
// For simplicity, this demo blocks all popup windows and new tabs
Result := (targetDisposition in [CEF_WOD_NEW_FOREGROUND_TAB, CEF_WOD_NEW_BACKGROUND_TAB, CEF_WOD_NEW_POPUP, CEF_WOD_NEW_WINDOW]);
end;
{%Endregion}
initialization
MainAppEvent := TEventObject.Create(nil, True, False, 'MainAppEvent');
finalization
if assigned(MainAppEvent) then
FreeAndNil(MainAppEvent);
end.
@@ -7,7 +7,7 @@ object MainForm: TMainForm
ClientHeight = 768
ClientWidth = 1024
Position = poScreenCenter
LCLVersion = '4.2.0.0'
LCLVersion = '4.4.0.0'
OnActivate = FormActivate
OnCloseQuery = FormCloseQuery
OnCreate = FormCreate
@@ -80,6 +80,8 @@ object MainForm: TMainForm
'https://codepen.io/udaymanvar/pen/MWaePBY'
'https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept'
'chrome://version/'
'chrome://settings/'
'chrome://flags/'
'chrome://net-internals/'
'chrome://tracing/'
'chrome://appcache-internals/'
@@ -115,11 +115,11 @@ begin
GlobalCEFApp.LogFile := 'debug.log';
GlobalCEFApp.LogSeverity := LOGSEVERITY_INFO;
GlobalCEFApp.RootCache := 'RootCache';
GlobalCEFApp.Cache := GlobalCEFApp.RootCache + '/cache';
GlobalCEFApp.Cache := IncludeTrailingPathDelimiter(GlobalCEFApp.RootCache) + 'cache';
GlobalCEFApp.SetCurrentDir := True;
GlobalCEFApp.DisableZygote := True;
GlobalCEFApp.EnableGPU := True;
GlobalCEFApp.GTKVersion := gtkVersion3;
GlobalCEFApp.EnableGPU := True;
GlobalCEFApp.EnablePrintPreview := True;
GlobalCEFApp.OnContextInitialized := @GlobalCEFApp_OnContextInitialized;
end;
@@ -0,0 +1,2 @@
rmdir /S /Q lib
rmdir /S /Q backup
Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="12"/>
<General>
<SessionStorage Value="InProjectDir"/>
<Title Value="SubProcess"/>
<Scaled Value="True"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<XPManifest>
<DpiAware Value="True"/>
</XPManifest>
<Icon Value="0"/>
</General>
<MacroValues Count="1">
<Macro1 Name="LCLWidgetType" Value="qt6"/>
</MacroValues>
<BuildModes>
<Item Name="Default" Default="True"/>
<SharedMatrixOptions Count="1">
<Item1 ID="204233492819" Modes="Default" Type="IDEMacro" MacroName="LCLWidgetType" Value="qt6"/>
</SharedMatrixOptions>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<RequiredPackages>
<Item>
<PackageName Value="CEF4Delphi_Lazarus"/>
</Item>
<Item>
<PackageName Value="LCL"/>
</Item>
</RequiredPackages>
<Units>
<Unit>
<Filename Value="SubProcess.lpr"/>
<IsPartOfProject Value="True"/>
</Unit>
<Unit>
<Filename Value="umainform.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="MainForm"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="uMainForm"/>
</Unit>
<Unit>
<Filename Value="interfaces.pas"/>
<IsPartOfProject Value="True"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="../../../bin/SubProcess"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf3"/>
</Debugging>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
<Other>
<CustomOptions Value="-dUseCThreads"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions>
<Item>
<Name Value="EAbort"/>
</Item>
<Item>
<Name Value="ECodetoolError"/>
</Item>
<Item>
<Name Value="EFOpenError"/>
</Item>
</Exceptions>
</Debugging>
</CONFIG>
@@ -0,0 +1,42 @@
program SubProcess;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
{$IFDEF HASAMIGA}
athreads,
{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, uMainForm
{ you can add units after this },
uCEFApplication;
{$R *.res}
begin
CreateGlobalCEFApp;
if StartMainProcess then
begin
// The LCL Widgetset must be initialized after the CEF initialization and
// only in the browser process.
CustomWidgetSetInitialization;
RequireDerivedFormResource:=True;
Application.Scaled:=True;
{$PUSH}{$WARN 5044 OFF}
Application.MainFormOnTaskbar:=True;
{$POP}
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
CustomWidgetSetFinalization;
end;
DestroyGlobalCEFApp;
end.
Binary file not shown.
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="11"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="SubProcess_sp"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
<Modes Count="0"/>
</RunParams>
<RequiredPackages Count="1">
<Item1>
<PackageName Value="CEF4Delphi_Lazarus"/>
</Item1>
</RequiredPackages>
<Units Count="1">
<Unit0>
<Filename Value="SubProcess_sp.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="../../../bin/SubProcess_sp"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Other>
<CustomOptions Value="-dUseCThreads"/>
<OtherDefines Count="1">
<Define0 Value="UseCThreads"/>
</OtherDefines>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>
@@ -0,0 +1,34 @@
program SubProcess_sp;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
uCEFApplicationCore;
begin
GlobalCEFApp := TCefApplicationCore.Create;
// The main process and the subprocess *MUST* have the same GlobalCEFApp
// properties and events, specially FrameworkDirPath, ResourcesDirPath,
// LocalesDirPath, cache and UserDataPath paths.
// The demos are compiled into the BIN directory. Make sure SubProcess.exe
// and SubProcess_sp.exe are in that directory or this demo won't work.
// In case you want to use custom directories for the CEF3 binaries, cache
// and user data.
{
GlobalCEFApp.FrameworkDirPath := 'cef';
GlobalCEFApp.ResourcesDirPath := 'cef';
GlobalCEFApp.LocalesDirPath := 'cef\locales';
GlobalCEFApp.cache := 'cef\cache';
GlobalCEFApp.UserDataPath := 'cef\User Data';
}
GlobalCEFApp.StartSubProcess;
DestroyGlobalCEFApp;
end.
@@ -0,0 +1,66 @@
{
/***************************************************************************
Interfaces.pp - determines what interface to use
-------------------
Initial Revision : Thu July 1st CST 1999
***************************************************************************/
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit interfaces;
{$mode objfpc}
{$H+}
interface
uses
{$IFDEF UNIX}{$IFNDEF DisableCWString}cwstring,{$ENDIF}{$ENDIF}
InterfaceBase;
procedure CustomWidgetSetInitialization;
procedure CustomWidgetSetFinalization;
implementation
uses
qtint, Forms, xlib;
function CustomX11ErrorHandler(Display:PDisplay; ErrorEv:PXErrorEvent):longint;cdecl;
begin
{$IFDEF DEBUG}
XError := ErrorEv^.error_code;
WriteLn('Error: ' + IntToStr(XError));
{$ENDIF}
Result := 0;
end;
function CustomXIOErrorHandler(Display:PDisplay):longint;cdecl;
begin
Result := 0;
end;
procedure CustomWidgetSetInitialization;
begin
CreateWidgetset(TQtWidgetSet);
// Install xlib error handlers so that the application won't be terminated
// on non-fatal errors. Must be done after initializing QT.
XSetErrorHandler(@CustomX11ErrorHandler);
XSetIOErrorHandler(@CustomXIOErrorHandler);
end;
procedure CustomWidgetSetFinalization;
begin
FreeWidgetSet;
end;
end.
@@ -0,0 +1,126 @@
object MainForm: TMainForm
Left = 342
Height = 768
Top = 31
Width = 1024
Caption = 'Simple Browser. Initializing...'
ClientHeight = 768
ClientWidth = 1024
Position = poScreenCenter
LCLVersion = '4.4.0.0'
OnActivate = FormActivate
OnCloseQuery = FormCloseQuery
OnCreate = FormCreate
object CEFLinkedWindowParent1: TCEFLinkedWindowParent
Left = 0
Height = 742
Top = 26
Width = 1024
Align = alClient
TabStop = True
TabOrder = 1
OnEnter = CEFLinkedWindowParent1Enter
OnExit = CEFLinkedWindowParent1Exit
Chromium = Chromium1
end
object AddressPnl: TPanel
Left = 0
Height = 26
Top = 0
Width = 1024
Align = alTop
BevelOuter = bvNone
ClientHeight = 26
ClientWidth = 1024
TabOrder = 0
object AddressCb: TComboBox
Left = 0
Height = 26
Top = 0
Width = 965
Align = alClient
ItemHeight = 0
ItemIndex = 0
Items.Strings = (
'https://www.google.com'
'https://www.bing.com'
'https://duckduckgo.com'
'https://www.qwant.com'
'https://yandex.com'
'https://www.startpage.com'
'https://www.ecosia.org'
'https://www.baidu.com'
'https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending'
'https://www.w3schools.com/js/tryit.asp?filename=tryjs_win_close'
'https://www.w3schools.com/js/tryit.asp?filename=tryjs_alert'
'https://www.w3schools.com/js/tryit.asp?filename=tryjs_loc_assign'
'https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_style_backgroundcolor'
'https://www.w3schools.com/Tags/tryit.asp?filename=tryhtml_iframe_name'
'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_file'
'https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_state_throw_error'
'https://www.htmlquick.com/es/reference/tags/input-file.html'
'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file'
'https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory'
'https://www.w3schools.com/html/html5_video.asp'
'http://html5test.com/'
'https://webrtc.github.io/samples/src/content/devices/input-output/'
'https://test.webrtc.org/'
'https://www.browserleaks.com/webrtc'
'https://shaka-player-demo.appspot.com/demo/'
'http://webglsamples.org/'
'https://get.webgl.org/'
'https://www.briskbard.com'
'https://www.youtube.com'
'https://html5demos.com/drag/'
'https://frames-per-second.appspot.com/'
'https://www.sede.fnmt.gob.es/certificados/persona-fisica/verificar-estado'
'https://www.kirupa.com/html5/accessing_your_webcam_in_html5.htm'
'https://www.xdumaine.com/enumerateDevices/test/'
'https://dagrs.berkeley.edu/sites/default/files/2020-01/sample.pdf'
'https://codepen.io/udaymanvar/pen/MWaePBY'
'https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept'
'chrome://version/'
'chrome://settings/'
'chrome://flags/'
'chrome://net-internals/'
'chrome://tracing/'
'chrome://appcache-internals/'
'chrome://blob-internals/'
'chrome://view-http-cache/'
'chrome://credits/'
'chrome://histograms/'
'chrome://media-internals/'
'chrome://kill'
'chrome://crash'
'chrome://hang'
'chrome://shorthang'
'chrome://gpuclean'
'chrome://gpucrash'
'chrome://gpuhang'
'chrome://extensions-support'
'chrome://process-internals'
)
TabOrder = 0
Text = 'https://www.google.com'
end
object GoBtn: TButton
Left = 965
Height = 26
Top = 0
Width = 59
Align = alRight
Caption = 'Go'
TabOrder = 1
OnClick = GoBtnClick
end
end
object Chromium1: TChromium
OnGotFocus = Chromium1GotFocus
OnBeforePopup = Chromium1BeforePopup
OnAfterCreated = Chromium1AfterCreated
OnBeforeClose = Chromium1BeforeClose
OnOpenUrlFromTab = Chromium1OpenUrlFromTab
Left = 106
Top = 69
end
end
@@ -0,0 +1,318 @@
unit uMainForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, SyncObjs, Dialogs, ExtCtrls,
LMessages, StdCtrls,
uCEFChromium, uCEFLinkedWindowParent, uCEFInterfaces, uCEFChromiumEvents,
uCEFTypes;
const
CEF_SETFOCUS = 1;
type
{ TMainForm }
TMainForm = class(TForm)
GoBtn: TButton;
CEFLinkedWindowParent1: TCEFLinkedWindowParent;
Chromium1: TChromium;
AddressCb: TComboBox;
AddressPnl: TPanel;
procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
procedure Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser);
procedure Chromium1BeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; popup_id: Integer; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean; var Result: Boolean);
procedure Chromium1GotFocus(Sender: TObject; const browser: ICefBrowser);
procedure Chromium1OpenUrlFromTab(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; out Result: Boolean);
procedure FormActivate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormCreate(Sender: TObject);
procedure GoBtnClick(Sender: TObject);
procedure CEFLinkedWindowParent1Enter(Sender: TObject);
procedure CEFLinkedWindowParent1Exit(Sender: TObject);
private
protected
// Variables to control when can we destroy the form safely
FCanClose : boolean; // Set to True in TChromium.OnBeforeClose
FClosing : boolean; // Set to True in the CloseQuery event.
// CEF needs to handle these messages to call TChromium.NotifyMoveOrResizeStarted
procedure WMMove(var Message: TLMMove); message LM_MOVE;
procedure WMSize(var Message: TLMSize); message LM_SIZE;
procedure WMWindowPosChanged(var Message: TLMWindowPosChanged); message LM_WINDOWPOSCHANGED;
procedure SendCompMessage(aMsg : cardinal; aData: PtrInt = 0);
procedure BrowserCreatedMsg(Data: PtrInt);
procedure BrowserCloseFormMsg(Data: PtrInt);
procedure BrowserSetFocusMsg(Data: PtrInt);
public
end;
var
MainForm: TMainForm;
procedure CreateGlobalCEFApp;
function StartMainProcess: boolean;
implementation
{$R *.lfm}
// This is a demo with the simplest web browser you can build using CEF4Delphi and
// it doesn't show any sign of progress like other web browsers do.
// Remember that it may take a few seconds to load if Windows update, your antivirus or
// any other windows service is using your hard drive.
// Depending on your internet connection it may take longer than expected.
// Please check that your firewall or antivirus are not blocking this application
// or the domain "google.com". If you don't live in the US, you'll be redirected to
// another domain which will take a little time too.
// This demo uses a TChromium and a TCEFLinkedWindowParent
// We need to use TCEFLinkedWindowParent in Linux to update the browser
// visibility and size automatically.
// Most of the TChromium events are executed in a CEF thread and this causes
// issues with most QT API functions. If you need to update the GUI, store the
// TChromium event parameters and use SendCompMessage (Application.QueueAsyncCall)
// to do it in the main application thread.
// Destruction steps
// =================
// 1. FormCloseQuery sets CanClose to FALSE, destroys CEFLinkedWindowParent1 and calls TChromium.CloseBrowser which triggers the TChromium.OnBeforeClose event.
// 2. TChromium.OnBeforeClose sets FCanClose := True and sends CEF_BEFORECLOSE to close the form.
uses
Math,
uCEFMiscFunctions, uCEFApplication, uCEFLinuxFunctions, uCEFConstants;
var
MainAppEvent : TEventObject;
{GlobalCEFApp functions}
{%Region}
procedure GlobalCEFApp_OnContextInitialized();
begin
MainAppEvent.SetEvent;
end;
procedure CreateGlobalCEFApp;
begin
GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.LogFile := 'debug.log';
GlobalCEFApp.LogSeverity := LOGSEVERITY_INFO;
GlobalCEFApp.RootCache := 'RootCache';
GlobalCEFApp.Cache := IncludeTrailingPathDelimiter(GlobalCEFApp.RootCache) + 'cache';
GlobalCEFApp.SetCurrentDir := True;
GlobalCEFApp.DisableZygote := True;
GlobalCEFApp.EnableGPU := True;
GlobalCEFApp.EnablePrintPreview := True;
GlobalCEFApp.BrowserSubprocessPath := 'SubProcess_sp'; // This is the name of the executable used by CEF for the Chromium subprocesses
GlobalCEFApp.OnContextInitialized := @GlobalCEFApp_OnContextInitialized;
end;
function StartMainProcess: boolean;
begin
Result := False;
if GlobalCEFApp.StartMainProcess then
begin
// Wait until the context is initialized before initializing GTK.
if (MainAppEvent.WaitFor(10000) = wrTimeout) then
CefDebugLog('CEF initialization failure!')
else
Result := True;
end;
end;
{%Endregion}
{TForm events}
{%Region}
procedure TMainForm.FormCreate(Sender: TObject);
begin
FCanClose := False;
FClosing := False;
// CEF requires a native widget
CEFLinkedWindowParent1.SetQTWidgetAsNative;
Chromium1.DefaultURL := UTF8Decode(AddressCb.Text);
end;
procedure TMainForm.FormActivate(Sender: TObject);
var
TempRect : TRect;
begin
TempRect := Rect(0, 0, CEFLinkedWindowParent1.Width, CEFLinkedWindowParent1.Height);
Chromium1.CreateBrowser(CEFLinkedWindowParent1.Handle, TempRect);
end;
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if not Chromium1.Initialized then
begin
FCanClose := True;
FClosing := True;
end;
CanClose := FCanClose;
if not(FClosing) then
begin
FClosing := True;
Visible := False;
Chromium1.CloseBrowser(True);
FreeAndNil(CEFLinkedWindowParent1);
end;
end;
{%Endregion}
{TCEFLinkedWindowParent events}
{%Region}
procedure TMainForm.CEFLinkedWindowParent1Enter(Sender: TObject);
begin
if not(csDesigning in ComponentState) and
Chromium1.Initialized and
not(Chromium1.FrameIsFocused) then
Chromium1.SetFocus(True);
end;
// This is a workaround for the CEF issue #2026
// https://bitbucket.org/chromiumembedded/cef/issues/2026/multiple-major-keyboard-focus-issues-on
// We use CEFLinkedWindowParent1.OnEnter, CEFLinkedWindowParent1.OnExit and
// TChromium.OnGotFocus to avoid most of the focus issues.
// CEFLinkedWindowParent1.TabStop must be TRUE.
procedure TMainForm.CEFLinkedWindowParent1Exit(Sender: TObject);
begin
if not(csDesigning in ComponentState) then
Chromium1.SendCaptureLostEvent;
end;
{%Endregion}
{Message handlers}
{%Region}
procedure TMainForm.BrowserCreatedMsg(Data: PtrInt);
begin
Caption := 'SubProcess Browser';
AddressPnl.Enabled := True;
Chromium1.UpdateXWindowVisibility(True);
CEFLinkedWindowParent1.UpdateSize;
CEFLinkedWindowParent1.InvalidateChildren;
end;
procedure TMainForm.BrowserCloseFormMsg(Data: PtrInt);
begin
Close;
end;
procedure TMainForm.BrowserSetFocusMsg(Data: PtrInt);
begin
if assigned(CEFLinkedWindowParent1) then
CEFLinkedWindowParent1.SetFocus;
end;
procedure TMainForm.WMMove(var Message: TLMMove);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TMainForm.WMSize(var Message: TLMSize);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
procedure TMainForm.WMWindowPosChanged(var Message: TLMWindowPosChanged);
begin
inherited;
Chromium1.NotifyMoveOrResizeStarted;
end;
{%Endregion}
{Misc functions}
{%Region}
procedure TMainForm.SendCompMessage(aMsg : cardinal; aData: PtrInt);
begin
case aMsg of
CEF_AFTERCREATED : Application.QueueAsyncCall(@BrowserCreatedMsg, 0);
CEF_BEFORECLOSE : Application.QueueAsyncCall(@BrowserCloseFormMsg, 0);
CEF_SETFOCUS : Application.QueueAsyncCall(@BrowserSetFocusMsg, 0);
end;
end;
procedure TMainForm.GoBtnClick(Sender: TObject);
begin
Chromium1.LoadURL(UTF8Decode(AddressCb.Text));
end;
{%Endregion}
{TChromium events}
{%Region}
procedure TMainForm.Chromium1AfterCreated(Sender: TObject;
const browser: ICefBrowser);
begin
// Now the browser is fully initialized we can initialize the UI.
SendCompMessage(CEF_AFTERCREATED);
end;
procedure TMainForm.Chromium1BeforeClose(Sender: TObject;
const browser: ICefBrowser);
begin
// We must wait until all browsers trigger the TChromium.OnBeforeClose event
// in order to close the application safely or we will have shutdown issues.
FCanClose := True;
SendCompMessage(CEF_BEFORECLOSE);
end;
procedure TMainForm.Chromium1BeforePopup(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; popup_id: Integer;
const targetUrl, targetFrameName: ustring;
targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean;
const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo;
var client: ICefClient; var settings: TCefBrowserSettings;
var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean;
var Result: Boolean);
begin
// For simplicity, this demo blocks all popup windows and new tabs
Result := (targetDisposition in [CEF_WOD_NEW_FOREGROUND_TAB, CEF_WOD_NEW_BACKGROUND_TAB, CEF_WOD_NEW_POPUP, CEF_WOD_NEW_WINDOW]);
end;
procedure TMainForm.Chromium1GotFocus(Sender: TObject;
const browser: ICefBrowser);
begin
SendCompMessage(CEF_SETFOCUS);
end;
procedure TMainForm.Chromium1OpenUrlFromTab(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl: ustring;
targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; out
Result: Boolean);
begin
// For simplicity, this demo blocks all popup windows and new tabs
Result := (targetDisposition in [CEF_WOD_NEW_FOREGROUND_TAB, CEF_WOD_NEW_BACKGROUND_TAB, CEF_WOD_NEW_POPUP, CEF_WOD_NEW_WINDOW]);
end;
{%Endregion}
initialization
MainAppEvent := TEventObject.Create(nil, True, False, 'MainAppEvent');
finalization
if assigned(MainAppEvent) then
FreeAndNil(MainAppEvent);
end.
+1 -1
View File
@@ -2,7 +2,7 @@
"UpdateLazPackages" : [
{
"ForceNotify" : true,
"InternalVersion" : 830,
"InternalVersion" : 831,
"Name" : "cef4delphi_lazarus.lpk",
"Version" : "143.0.10"
}