2023-11-27 18:21:07 +01:00
|
|
|
unit uTinyBrowser2;
|
2020-12-13 18:36:10 +01:00
|
|
|
|
2021-05-01 10:06:25 +02:00
|
|
|
{$MODE Delphi}
|
2020-12-13 18:36:10 +01:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2021-05-01 10:06:25 +02:00
|
|
|
Types, SysUtils,
|
2020-12-13 18:36:10 +01:00
|
|
|
uCEFInterfaces, uCEFTypes, uCEFChromiumCore;
|
|
|
|
|
|
|
|
type
|
|
|
|
TTinyBrowser2 = class
|
|
|
|
private
|
2021-05-01 10:06:25 +02:00
|
|
|
FChromium : TChromiumCore;
|
|
|
|
|
|
|
|
function GetClient : ICefClient;
|
2020-12-13 18:36:10 +01:00
|
|
|
|
|
|
|
procedure Chromium_OnBeforeClose(Sender: TObject; const browser: ICefBrowser);
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
2021-05-01 10:06:25 +02:00
|
|
|
procedure AfterConstruction; override;
|
|
|
|
|
|
|
|
property Client : ICefClient read GetClient;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure CreateGlobalCEFApp;
|
|
|
|
procedure DestroyTinyBrowser;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
// This demo is similar to the cefsimple demo in the official CEF project.
|
|
|
|
|
|
|
|
// It doesn't use LCL to create forms from Lazarus code.
|
|
|
|
// It just uses CEF to create a browser window as if it was a popup browser window.
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// GlobalCEFApp.QuitMessageLoop when the browser has been destroyed.
|
|
|
|
|
|
|
|
// The destruction steps are much simpler for that reason.
|
|
|
|
// In this demo it's only necessary to implement the TChromium.OnClose and TChromium.OnBeforeClose events.
|
|
|
|
// The TChromium.OnClose event only sets aAction to cbaClose to continue closing the browser.
|
|
|
|
// The TChromium.OnBeforeClose event calls GlobalCEFApp.QuitMessageLoop because the browser has been destroyed
|
|
|
|
// and it's necessary to close the message loop.
|
|
|
|
|
|
|
|
uses
|
2021-05-01 10:06:25 +02:00
|
|
|
uCEFApplication, uCEFConstants, uCEFMiscFunctions;
|
2020-12-13 18:36:10 +01:00
|
|
|
|
|
|
|
var
|
|
|
|
TinyBrowser : TTinyBrowser2 = nil;
|
|
|
|
|
|
|
|
procedure GlobalCEFApp_OnContextInitialized;
|
|
|
|
begin
|
|
|
|
TinyBrowser := TTinyBrowser2.Create;
|
2021-05-01 10:06:25 +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
|
|
|
|
GlobalCEFApp := TCefApplication.Create;
|
|
|
|
GlobalCEFApp.MultiThreadedMessageLoop := False;
|
2021-05-01 10:06:25 +02:00
|
|
|
GlobalCEFApp.ExternalMessagePump := False;
|
2021-07-31 17:24:54 +02:00
|
|
|
GlobalCEFApp.DisablePopupBlocking := True;
|
2023-12-23 18:58:40 +01:00
|
|
|
GlobalCEFApp.cache := 'cache';
|
|
|
|
GlobalCEFApp.SetCurrentDir := True;
|
2020-12-13 18:36:10 +01:00
|
|
|
GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
|
2024-09-03 17:26:03 +02:00
|
|
|
GlobalCEFApp.OnGetDefaultClient := GlobalCEFApp_OnGetDefaultClient;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure DestroyTinyBrowser;
|
|
|
|
begin
|
|
|
|
if (TinyBrowser <> nil) then
|
|
|
|
begin
|
|
|
|
TinyBrowser.Free;
|
|
|
|
TinyBrowser := nil;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TTinyBrowser2.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
FChromium := nil;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TTinyBrowser2.Destroy;
|
|
|
|
begin
|
|
|
|
if (FChromium <> nil) then
|
|
|
|
begin
|
|
|
|
FChromium.Free;
|
|
|
|
FChromium := nil;
|
|
|
|
end;
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2021-05-01 10:06:25 +02:00
|
|
|
procedure TTinyBrowser2.AfterConstruction;
|
|
|
|
var
|
|
|
|
TempHandle : TCefWindowHandle;
|
|
|
|
TempRect : TRect;
|
2020-12-13 18:36:10 +01:00
|
|
|
begin
|
|
|
|
inherited AfterConstruction;
|
|
|
|
|
|
|
|
FChromium := TChromiumCore.Create(nil);
|
|
|
|
FChromium.DefaultURL := 'https://www.google.com';
|
|
|
|
FChromium.OnBeforeClose := Chromium_OnBeforeClose;
|
2025-03-05 11:47:06 +01:00
|
|
|
|
|
|
|
// The MultiBrowserMode store all the browser references in TChromium.
|
|
|
|
// The first browser reference is the browser in the main form.
|
|
|
|
// When MiniBrowser allows CEF to create child popup browsers it will also
|
|
|
|
// store their reference inside TChromium and you can use all the TChromium's
|
|
|
|
// methods and properties to manipulate those browsers.
|
|
|
|
// To do that call TChromium.SelectBrowser with the browser ID that will be
|
|
|
|
// used when you call any method or property in TChromium.
|
|
|
|
FChromium.MultiBrowserMode := True;
|
2020-12-13 18:36:10 +01:00
|
|
|
|
2021-05-01 10:06:25 +02:00
|
|
|
InitializeWindowHandle(TempHandle);
|
|
|
|
FChromium.CreateBrowser(TempHandle, TempRect, 'Tiny Browser 2', nil, nil, True);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTinyBrowser2.GetClient : ICefClient;
|
2020-12-13 18:36:10 +01:00
|
|
|
begin
|
2021-05-01 10:06:25 +02:00
|
|
|
if (FChromium <> nil) then
|
|
|
|
Result := FChromium.CefClient
|
|
|
|
else
|
|
|
|
Result := nil;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTinyBrowser2.Chromium_OnBeforeClose(Sender: TObject; const browser: ICefBrowser);
|
|
|
|
begin
|
2025-03-05 11:47:06 +01:00
|
|
|
// The main browser is being destroyed
|
|
|
|
if (FChromium.BrowserId = 0) then
|
|
|
|
GlobalCEFApp.QuitMessageLoop;
|
2020-12-13 18:36:10 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|