2020-12-13 18:36:10 +01:00
|
|
|
unit uTinyBrowser2;
|
|
|
|
|
|
|
|
{$MODE Delphi}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2021-04-18 19:36:20 +02:00
|
|
|
SysUtils, Classes,
|
2020-12-13 18:36:10 +01:00
|
|
|
uCEFInterfaces, uCEFTypes, uCEFChromiumCore;
|
|
|
|
|
|
|
|
type
|
|
|
|
TTinyBrowser2 = class
|
|
|
|
private
|
2021-04-23 14:52:53 +02:00
|
|
|
FChromium : TChromiumCore;
|
|
|
|
|
|
|
|
function GetClient : ICefClient;
|
2020-12-13 18:36:10 +01:00
|
|
|
|
|
|
|
procedure Chromium_OnBeforeClose(Sender: TObject; const browser: ICefBrowser);
|
|
|
|
procedure Chromium_OnBeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; 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 Chromium_OnOpenUrlFromTab(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; out Result: Boolean);
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
2021-04-23 14:52:53 +02:00
|
|
|
procedure AfterConstruction; override;
|
|
|
|
|
|
|
|
property Client : ICefClient read GetClient;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure CreateGlobalCEFApp;
|
|
|
|
procedure DestroyTinyBrowser;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2022-06-25 16:41:34 +02:00
|
|
|
// ATTENTION
|
|
|
|
// =========
|
|
|
|
// Since CEF 102 the Linux demos with the GlobalCEFApp.MultiThreadedMessageLoop
|
|
|
|
// property set to FALSE and using GTK2 require a custom build of CEF binaries
|
|
|
|
// with use_gtk=false set via GN_DEFINES.
|
|
|
|
|
2020-12-13 18:36:10 +01:00
|
|
|
// This demo is similar to the cefsimple demo in the official CEF project.
|
|
|
|
|
|
|
|
// It doesn't use LCL to create forms from Lazarus code.
|
2021-02-21 11:11:08 +01:00
|
|
|
// It just uses CEF to create a browser window as if it was a popup browser
|
|
|
|
// window.
|
2020-12-13 18:36:10 +01:00
|
|
|
|
2021-02-21 11:11:08 +01:00
|
|
|
// This browser doesn't use multiple threads to handle the browser and it
|
|
|
|
// doesn't use an external message pump. For this reason we have to call
|
|
|
|
// GlobalCEFApp.RunMessageLoop to let CEF handle the message loop and
|
2020-12-13 18:36:10 +01:00
|
|
|
// GlobalCEFApp.QuitMessageLoop when the browser has been destroyed.
|
|
|
|
|
|
|
|
// The destruction steps are much simpler for that reason.
|
2021-02-21 11:11:08 +01:00
|
|
|
// In this demo it's only necessary to implement the TChromium.OnBeforeClose
|
2021-04-18 19:36:20 +02:00
|
|
|
// event. The TChromium.OnBeforeClose event calls GlobalCEFApp.QuitMessageLoop
|
2021-02-21 11:11:08 +01:00
|
|
|
// because the browser has been destroyed and it's necessary to close the
|
|
|
|
// message loop.
|
2020-12-13 18:36:10 +01:00
|
|
|
|
|
|
|
uses
|
2021-04-23 14:52:53 +02:00
|
|
|
xlib,
|
2021-04-18 19:36:20 +02:00
|
|
|
uCEFApplication, uCEFConstants, uCEFMiscFunctions;
|
2020-12-13 18:36:10 +01:00
|
|
|
|
|
|
|
var
|
|
|
|
TinyBrowser : TTinyBrowser2 = nil;
|
|
|
|
|
2021-04-23 14:52:53 +02:00
|
|
|
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;
|
|
|
|
|
2020-12-13 18:36:10 +01:00
|
|
|
procedure GlobalCEFApp_OnContextInitialized;
|
|
|
|
begin
|
|
|
|
TinyBrowser := TTinyBrowser2.Create;
|
2021-04-23 14:52:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure GlobalCEFApp_OnGetDefaultClient(var aClient : ICefClient);
|
|
|
|
begin
|
|
|
|
aClient := TinyBrowser.Client;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure CreateGlobalCEFApp;
|
|
|
|
begin
|
2021-04-23 14:52:53 +02:00
|
|
|
// Install xlib error handlers so that the application won't be terminated
|
|
|
|
// on non-fatal errors. Must be done after initializing GTK.
|
|
|
|
XSetErrorHandler(@CustomX11ErrorHandler);
|
|
|
|
XSetIOErrorHandler(@CustomXIOErrorHandler);
|
|
|
|
|
2020-12-13 18:36:10 +01:00
|
|
|
GlobalCEFApp := TCefApplication.Create;
|
|
|
|
GlobalCEFApp.MultiThreadedMessageLoop := False;
|
2021-04-23 14:52:53 +02:00
|
|
|
GlobalCEFApp.ExternalMessagePump := False;
|
|
|
|
//GlobalCEFApp.ChromeRuntime := True; // Enable this line to enable the "ChromeRuntime" mode. It's in experimental state.
|
|
|
|
GlobalCEFApp.cache := 'cache';
|
|
|
|
GlobalCEFApp.DisableZygote := True;
|
|
|
|
GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
|
|
|
|
GlobalCEFApp.OnGetDefaultClient := GlobalCEFApp_OnGetDefaultClient; // This event is only used in "ChromeRuntime" mode
|
2020-12-13 18:36:10 +01:00
|
|
|
|
|
|
|
// Add a debug log in the BIN directory
|
2021-04-23 14:52:53 +02:00
|
|
|
//GlobalCEFApp.LogFile := 'cef.log';
|
|
|
|
//GlobalCEFApp.LogSeverity := LOGSEVERITY_VERBOSE;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure DestroyTinyBrowser;
|
|
|
|
begin
|
|
|
|
if (TinyBrowser <> nil) then
|
2021-04-23 14:52:53 +02:00
|
|
|
FreeAndNil(TinyBrowser);
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TTinyBrowser2.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
FChromium := nil;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
2021-02-21 11:11:08 +01:00
|
|
|
destructor TTinyBrowser2.Destroy;
|
2020-12-13 18:36:10 +01:00
|
|
|
begin
|
2021-02-21 11:11:08 +01:00
|
|
|
if (FChromium <> nil) then
|
|
|
|
FreeAndNil(FChromium);
|
|
|
|
|
|
|
|
inherited Destroy;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure TTinyBrowser2.AfterConstruction;
|
|
|
|
var
|
|
|
|
TempHandle : TCefWindowHandle;
|
|
|
|
TempRect : TRect;
|
|
|
|
begin
|
|
|
|
inherited AfterConstruction;
|
|
|
|
|
|
|
|
FChromium := TChromiumCore.Create(nil);
|
|
|
|
FChromium.DefaultURL := 'https://www.google.com';
|
|
|
|
FChromium.OnBeforeClose := Chromium_OnBeforeClose;
|
|
|
|
FChromium.OnBeforePopup := Chromium_OnBeforePopup;
|
|
|
|
FChromium.OnOpenUrlFromTab := Chromium_OnOpenUrlFromTab;
|
|
|
|
|
|
|
|
InitializeWindowHandle(TempHandle);
|
|
|
|
FChromium.CreateBrowser(TempHandle, TempRect, 'Tiny Browser 2', nil, nil, True);
|
2021-04-23 14:52:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TTinyBrowser2.GetClient : ICefClient;
|
|
|
|
begin
|
|
|
|
if (FChromium <> nil) then
|
|
|
|
Result := FChromium.CefClient
|
|
|
|
else
|
|
|
|
Result := nil;
|
2021-04-18 19:36:20 +02:00
|
|
|
end;
|
|
|
|
|
2021-02-21 11:11:08 +01:00
|
|
|
procedure TTinyBrowser2.Chromium_OnBeforeClose(Sender: TObject;
|
|
|
|
const browser: ICefBrowser);
|
2020-12-13 18:36:10 +01:00
|
|
|
begin
|
|
|
|
GlobalCEFApp.QuitMessageLoop;
|
|
|
|
end;
|
|
|
|
|
2021-02-21 11:11:08 +01:00
|
|
|
procedure TTinyBrowser2.Chromium_OnBeforePopup(Sender: TObject;
|
|
|
|
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
|
|
|
|
targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition;
|
|
|
|
userGesture: Boolean; const popupFeatures: TCefPopupFeatures;
|
|
|
|
var windowInfo: TCefWindowInfo; var client: ICefClient;
|
|
|
|
var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue;
|
2020-12-13 18:36:10 +01:00
|
|
|
var noJavascriptAccess: Boolean; var Result: Boolean);
|
|
|
|
begin
|
|
|
|
// For simplicity, this demo blocks all popup windows and new tabs
|
|
|
|
Result := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTinyBrowser2.Chromium_OnOpenUrlFromTab(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 [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|