2020-12-06 12:28:13 +01:00
|
|
|
unit uTinyBrowser2;
|
|
|
|
|
2023-11-26 19:28:28 +01:00
|
|
|
{$I ..\..\..\source\cef.inc}
|
2021-04-23 14:52:53 +02:00
|
|
|
|
2020-12-06 12:28:13 +01:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2021-04-23 14:52:53 +02:00
|
|
|
{$IFDEF DELPHI16_UP}
|
2021-09-27 12:04:33 +02:00
|
|
|
WinApi.Windows, System.Types, System.SysUtils, Vcl.Forms, Winapi.Messages,
|
2021-04-23 14:52:53 +02:00
|
|
|
{$ELSE}
|
2021-09-27 12:04:33 +02:00
|
|
|
Windows, Types, SysUtils, Forms, Messages,
|
2021-04-23 14:52:53 +02:00
|
|
|
{$ENDIF}
|
2020-12-06 12:28:13 +01:00
|
|
|
uCEFInterfaces, uCEFTypes, uCEFChromiumCore;
|
|
|
|
|
|
|
|
type
|
|
|
|
TTinyBrowser2 = class
|
|
|
|
private
|
|
|
|
FChromium : TChromiumCore;
|
|
|
|
|
2021-04-23 14:52:53 +02:00
|
|
|
function GetClient : ICefClient;
|
|
|
|
|
2020-12-06 12:28:13 +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;
|
|
|
|
procedure AfterConstruction; override;
|
2021-04-23 14:52:53 +02:00
|
|
|
|
|
|
|
property Client : ICefClient read GetClient;
|
2020-12-06 12:28:13 +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 VCL or FMX to create forms from Delphi 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-04-18 19:36:20 +02:00
|
|
|
uCEFApplication, uCEFConstants, uCEFMiscFunctions;
|
2020-12-06 12:28:13 +01:00
|
|
|
|
|
|
|
var
|
|
|
|
TinyBrowser : TTinyBrowser2 = nil;
|
|
|
|
|
|
|
|
procedure GlobalCEFApp_OnContextInitialized;
|
|
|
|
begin
|
|
|
|
TinyBrowser := TTinyBrowser2.Create;
|
|
|
|
end;
|
|
|
|
|
2021-04-23 14:52:53 +02:00
|
|
|
procedure GlobalCEFApp_OnGetDefaultClient(var aClient : ICefClient);
|
|
|
|
begin
|
|
|
|
aClient := TinyBrowser.Client;
|
|
|
|
end;
|
|
|
|
|
2020-12-06 12:28:13 +01:00
|
|
|
procedure CreateGlobalCEFApp;
|
|
|
|
begin
|
|
|
|
GlobalCEFApp := TCefApplication.Create;
|
|
|
|
GlobalCEFApp.MultiThreadedMessageLoop := False;
|
|
|
|
GlobalCEFApp.ExternalMessagePump := False;
|
2022-01-11 11:49:38 +01:00
|
|
|
GlobalCEFApp.ChromeRuntime := True; // Enable this line to enable the "ChromeRuntime" mode. It's in experimental state.
|
2021-04-23 14:52:53 +02:00
|
|
|
GlobalCEFApp.cache := 'cache';
|
2021-07-31 17:24:54 +02:00
|
|
|
GlobalCEFApp.DisablePopupBlocking := True;
|
2020-12-06 12:28:13 +01:00
|
|
|
GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
|
2021-04-23 14:52:53 +02:00
|
|
|
GlobalCEFApp.OnGetDefaultClient := GlobalCEFApp_OnGetDefaultClient; // This event is only used in "ChromeRuntime" mode
|
2020-12-06 12:28:13 +01:00
|
|
|
|
|
|
|
// This is a workaround for the CEF4Delphi issue #324 :
|
|
|
|
// https://github.com/salvadordf/CEF4Delphi/issues/324
|
|
|
|
GlobalCEFApp.DisableFeatures := 'WinUseBrowserSpellChecker';
|
2021-09-27 12:04:33 +02:00
|
|
|
|
|
|
|
GlobalCEFApp.LogFile := 'debug.log';
|
|
|
|
GlobalCEFApp.LogSeverity := LOGSEVERITY_INFO;
|
2020-12-06 12:28:13 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure DestroyTinyBrowser;
|
|
|
|
begin
|
|
|
|
if (TinyBrowser <> nil) then
|
2021-04-23 14:52:53 +02:00
|
|
|
FreeAndNil(TinyBrowser);
|
2020-12-06 12:28:13 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TTinyBrowser2.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
FChromium := nil;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TTinyBrowser2.Destroy;
|
|
|
|
begin
|
|
|
|
if (FChromium <> nil) then
|
2021-04-23 14:52:53 +02:00
|
|
|
FreeAndNil(FChromium);
|
2020-12-06 12:28:13 +01:00
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTinyBrowser2.AfterConstruction;
|
2021-04-18 19:36:20 +02:00
|
|
|
var
|
|
|
|
TempHandle : TCefWindowHandle;
|
|
|
|
TempRect : TRect;
|
2020-12-06 12:28:13 +01:00
|
|
|
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;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
InitializeWindowHandle(TempHandle);
|
|
|
|
FChromium.CreateBrowser(TempHandle, TempRect, 'Tiny Browser 2', nil, nil, True);
|
2020-12-06 12:28:13 +01:00
|
|
|
end;
|
|
|
|
|
2021-04-23 14:52:53 +02:00
|
|
|
function TTinyBrowser2.GetClient : ICefClient;
|
|
|
|
begin
|
|
|
|
if (FChromium <> nil) then
|
|
|
|
Result := FChromium.CefClient
|
|
|
|
else
|
|
|
|
Result := nil;
|
|
|
|
end;
|
|
|
|
|
2020-12-06 12:28:13 +01:00
|
|
|
procedure TTinyBrowser2.Chromium_OnBeforeClose(Sender: TObject; const browser: ICefBrowser);
|
|
|
|
begin
|
|
|
|
GlobalCEFApp.QuitMessageLoop;
|
|
|
|
end;
|
|
|
|
|
|
|
|
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;
|
|
|
|
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.
|