2017-12-05 10:02:07 +01:00
|
|
|
|
// ************************************************************************
|
|
|
|
|
// ***************************** CEF4Delphi *******************************
|
|
|
|
|
// ************************************************************************
|
|
|
|
|
//
|
2019-10-19 10:58:34 +02:00
|
|
|
|
// CEF4Delphi is based on DCEF3 which uses CEF to embed a chromium-based
|
2017-12-05 10:02:07 +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
|
|
|
|
|
//
|
2020-01-01 12:48:10 +01:00
|
|
|
|
// Copyright <20> 2020 Salvador Diaz Fau. All rights reserved.
|
2017-12-05 10:02:07 +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 uCEFWorkScheduler;
|
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
|
{$IFDEF FPC}
|
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2018-11-22 15:02:21 +01:00
|
|
|
|
{$IFNDEF CPUX64}{$ALIGN ON}{$ENDIF}
|
|
|
|
|
{$MINENUMSIZE 4}
|
2017-12-05 10:02:07 +01:00
|
|
|
|
|
|
|
|
|
{$I cef.inc}
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
{$IFDEF DELPHI16_UP}
|
2019-11-08 23:15:53 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}WinApi.Windows, WinApi.Messages,{$ENDIF} System.Classes,
|
2017-12-05 10:02:07 +01:00
|
|
|
|
{$ELSE}
|
2019-11-08 23:15:53 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}Windows,{$ENDIF} Classes,
|
2018-05-12 14:50:54 +02:00
|
|
|
|
{$IFDEF FPC}
|
|
|
|
|
LCLProc, LCLType, LCLIntf, LResources, LMessages, InterfaceBase,
|
|
|
|
|
{$ELSE}
|
|
|
|
|
Messages,
|
|
|
|
|
{$ENDIF}
|
2017-12-05 10:02:07 +01:00
|
|
|
|
{$ENDIF}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
uCEFConstants, uCEFWorkSchedulerThread;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
|
|
|
|
|
type
|
2018-12-09 11:11:59 +01:00
|
|
|
|
{$IFNDEF FPC}{$IFDEF DELPHI16_UP}[ComponentPlatformsAttribute(pidWin32 or pidWin64)]{$ENDIF}{$ENDIF}
|
2017-12-05 10:02:07 +01:00
|
|
|
|
TCEFWorkScheduler = class(TComponent)
|
|
|
|
|
protected
|
|
|
|
|
FCompHandle : HWND;
|
2018-01-25 21:34:04 +01:00
|
|
|
|
FThread : TCEFWorkSchedulerThread;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
FDepleteWorkCycles : cardinal;
|
|
|
|
|
FDepleteWorkDelay : cardinal;
|
2018-01-25 21:34:04 +01:00
|
|
|
|
FDefaultInterval : integer;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
FStopped : boolean;
|
2018-01-25 21:34:04 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
|
{$WARN SYMBOL_PLATFORM OFF}
|
|
|
|
|
FPriority : TThreadPriority;
|
|
|
|
|
{$WARN SYMBOL_PLATFORM ON}
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
|
|
procedure CreateThread;
|
|
|
|
|
procedure DestroyThread;
|
|
|
|
|
procedure DeallocateWindowHandle;
|
|
|
|
|
procedure DepleteWork;
|
2019-01-08 19:15:25 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}
|
2017-12-05 10:02:07 +01:00
|
|
|
|
procedure WndProc(var aMessage: TMessage);
|
2019-01-08 19:15:25 +01:00
|
|
|
|
{$ENDIF}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
procedure NextPulse(aInterval : integer);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
procedure ScheduleWork(const delay_ms : int64);
|
2018-01-25 21:34:04 +01:00
|
|
|
|
procedure DoWork;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
procedure DoMessageLoopWork;
|
2018-01-25 21:34:04 +01:00
|
|
|
|
|
|
|
|
|
procedure SetDefaultInterval(aValue : integer);
|
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
|
{$WARN SYMBOL_PLATFORM OFF}
|
|
|
|
|
procedure SetPriority(aValue : TThreadPriority);
|
|
|
|
|
{$WARN SYMBOL_PLATFORM ON}
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
|
|
procedure Thread_OnPulse(Sender : TObject);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
|
|
|
|
|
public
|
|
|
|
|
constructor Create(AOwner: TComponent); override;
|
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
procedure AfterConstruction; override;
|
|
|
|
|
procedure ScheduleMessagePumpWork(const delay_ms : int64);
|
|
|
|
|
procedure StopScheduler;
|
|
|
|
|
|
|
|
|
|
published
|
2018-01-25 21:34:04 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
|
{$WARN SYMBOL_PLATFORM OFF}
|
|
|
|
|
property Priority : TThreadPriority read FPriority write SetPriority default tpNormal;
|
|
|
|
|
{$WARN SYMBOL_PLATFORM ON}
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
property DefaultInterval : integer read FDefaultInterval write SetDefaultInterval default CEF_TIMER_MAXDELAY;
|
|
|
|
|
property DepleteWorkCycles : cardinal read FDepleteWorkCycles write FDepleteWorkCycles default CEF_TIMER_DEPLETEWORK_CYCLES;
|
|
|
|
|
property DepleteWorkDelay : cardinal read FDepleteWorkDelay write FDepleteWorkDelay default CEF_TIMER_DEPLETEWORK_DELAY;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-04-07 20:24:55 +02:00
|
|
|
|
var
|
|
|
|
|
GlobalCEFWorkScheduler : TCEFWorkScheduler = nil;
|
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
|
{$IFDEF FPC}
|
|
|
|
|
procedure Register;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2018-06-17 14:18:11 +02:00
|
|
|
|
procedure DestroyGlobalCEFWorkScheduler;
|
|
|
|
|
|
2017-12-05 10:02:07 +01:00
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
|
System.SysUtils, System.Math,
|
|
|
|
|
{$ELSE}
|
|
|
|
|
SysUtils, Math,
|
|
|
|
|
{$ENDIF}
|
2019-11-08 23:15:53 +01:00
|
|
|
|
uCEFMiscFunctions, uCEFApplicationCore, uCEFTypes;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
|
2018-06-17 14:18:11 +02:00
|
|
|
|
procedure DestroyGlobalCEFWorkScheduler;
|
|
|
|
|
begin
|
|
|
|
|
if (GlobalCEFWorkScheduler <> nil) then FreeAndNil(GlobalCEFWorkScheduler);
|
|
|
|
|
end;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
|
|
|
|
|
constructor TCEFWorkScheduler.Create(AOwner: TComponent);
|
|
|
|
|
begin
|
|
|
|
|
inherited Create(AOwner);
|
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
|
FThread := nil;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
FCompHandle := 0;
|
|
|
|
|
FStopped := False;
|
2018-01-25 21:34:04 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
|
{$WARN SYMBOL_PLATFORM OFF}
|
|
|
|
|
FPriority := tpNormal;
|
|
|
|
|
{$WARN SYMBOL_PLATFORM ON}
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
FDefaultInterval := CEF_TIMER_MAXDELAY;
|
|
|
|
|
FDepleteWorkCycles := CEF_TIMER_DEPLETEWORK_CYCLES;
|
|
|
|
|
FDepleteWorkDelay := CEF_TIMER_DEPLETEWORK_DELAY;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
destructor TCEFWorkScheduler.Destroy;
|
|
|
|
|
begin
|
2018-01-25 21:34:04 +01:00
|
|
|
|
DestroyThread;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
DeallocateWindowHandle;
|
|
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCEFWorkScheduler.AfterConstruction;
|
|
|
|
|
begin
|
|
|
|
|
inherited AfterConstruction;
|
|
|
|
|
|
|
|
|
|
if not(csDesigning in ComponentState) then
|
2018-01-25 21:34:04 +01:00
|
|
|
|
begin
|
2019-01-08 19:15:25 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}
|
2018-11-22 15:02:21 +01:00
|
|
|
|
if (GlobalCEFApp <> nil) and
|
|
|
|
|
((GlobalCEFApp.ProcessType = ptBrowser) or GlobalCEFApp.SingleProcess) then
|
|
|
|
|
begin
|
2019-11-10 18:23:39 +01:00
|
|
|
|
FCompHandle := AllocateHWnd({$IFDEF FPC}@{$ENDIF}WndProc);
|
2018-11-22 15:02:21 +01:00
|
|
|
|
end;
|
2019-01-08 19:15:25 +01:00
|
|
|
|
{$ENDIF}
|
2018-05-12 14:50:54 +02:00
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
|
CreateThread;
|
|
|
|
|
end;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
|
procedure TCEFWorkScheduler.CreateThread;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
begin
|
2018-01-25 21:34:04 +01:00
|
|
|
|
FThread := TCEFWorkSchedulerThread.Create;
|
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
|
FThread.Priority := FPriority;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
FThread.DefaultInterval := FDefaultInterval;
|
2018-05-12 14:50:54 +02:00
|
|
|
|
FThread.OnPulse := {$IFDEF FPC}@{$ENDIF}Thread_OnPulse;
|
2018-02-07 14:30:44 +01:00
|
|
|
|
{$IFDEF DELPHI14_UP}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
FThread.Start;
|
|
|
|
|
{$ELSE}
|
2019-11-10 18:23:39 +01:00
|
|
|
|
{$IFNDEF FPC}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
FThread.Resume;
|
2019-11-10 18:23:39 +01:00
|
|
|
|
{$ELSE}
|
|
|
|
|
FThread.Start;
|
|
|
|
|
{$ENDIF}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
{$ENDIF}
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
|
procedure TCEFWorkScheduler.DestroyThread;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
begin
|
2018-01-25 21:34:04 +01:00
|
|
|
|
try
|
|
|
|
|
if (FThread <> nil) then
|
|
|
|
|
begin
|
|
|
|
|
FThread.Terminate;
|
|
|
|
|
FThread.NextPulse(0);
|
|
|
|
|
FThread.WaitFor;
|
|
|
|
|
FreeAndNil(FThread);
|
|
|
|
|
end;
|
|
|
|
|
except
|
|
|
|
|
on e : exception do
|
|
|
|
|
if CustomExceptionHandler('TCEFWorkScheduler.DestroyThread', e) then raise;
|
|
|
|
|
end;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2019-01-08 19:15:25 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
procedure TCEFWorkScheduler.WndProc(var aMessage: TMessage);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
begin
|
2018-01-25 21:34:04 +01:00
|
|
|
|
if (aMessage.Msg = CEF_PUMPHAVEWORK) then
|
|
|
|
|
ScheduleWork(aMessage.lParam)
|
|
|
|
|
else
|
|
|
|
|
aMessage.Result := DefWindowProc(FCompHandle, aMessage.Msg, aMessage.WParam, aMessage.LParam);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
2019-01-08 19:15:25 +01:00
|
|
|
|
{$ENDIF}
|
2017-12-05 10:02:07 +01:00
|
|
|
|
|
|
|
|
|
procedure TCEFWorkScheduler.DeallocateWindowHandle;
|
|
|
|
|
begin
|
|
|
|
|
if (FCompHandle <> 0) then
|
|
|
|
|
begin
|
|
|
|
|
DeallocateHWnd(FCompHandle);
|
|
|
|
|
FCompHandle := 0;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
|
procedure TCEFWorkScheduler.DoMessageLoopWork;
|
|
|
|
|
begin
|
|
|
|
|
if (GlobalCEFApp <> nil) then GlobalCEFApp.DoMessageLoopWork;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCEFWorkScheduler.SetDefaultInterval(aValue : integer);
|
|
|
|
|
begin
|
|
|
|
|
FDefaultInterval := aValue;
|
|
|
|
|
if (FThread <> nil) then FThread.DefaultInterval := aValue;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
|
{$WARN SYMBOL_PLATFORM OFF}
|
|
|
|
|
procedure TCEFWorkScheduler.SetPriority(aValue : TThreadPriority);
|
|
|
|
|
begin
|
|
|
|
|
FPriority := aValue;
|
|
|
|
|
if (FThread <> nil) then FThread.Priority := aValue;
|
|
|
|
|
end;
|
|
|
|
|
{$WARN SYMBOL_PLATFORM ON}
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2017-12-05 10:02:07 +01:00
|
|
|
|
procedure TCEFWorkScheduler.DepleteWork;
|
|
|
|
|
var
|
|
|
|
|
i : cardinal;
|
|
|
|
|
begin
|
|
|
|
|
i := FDepleteWorkCycles;
|
|
|
|
|
|
|
|
|
|
while (i > 0) do
|
|
|
|
|
begin
|
|
|
|
|
DoMessageLoopWork;
|
|
|
|
|
Sleep(FDepleteWorkDelay);
|
|
|
|
|
dec(i);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCEFWorkScheduler.ScheduleMessagePumpWork(const delay_ms : int64);
|
|
|
|
|
begin
|
2019-01-08 19:15:25 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
if not(FStopped) and (FCompHandle <> 0) then
|
|
|
|
|
PostMessage(FCompHandle, CEF_PUMPHAVEWORK, 0, LPARAM(delay_ms));
|
2019-01-08 19:15:25 +01:00
|
|
|
|
{$ENDIF}
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCEFWorkScheduler.StopScheduler;
|
|
|
|
|
begin
|
|
|
|
|
FStopped := True;
|
2018-01-25 21:34:04 +01:00
|
|
|
|
NextPulse(0);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
DepleteWork;
|
|
|
|
|
DeallocateWindowHandle;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
|
procedure TCEFWorkScheduler.Thread_OnPulse(Sender: TObject);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
begin
|
2018-01-25 21:34:04 +01:00
|
|
|
|
if not(FStopped) then DoMessageLoopWork;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCEFWorkScheduler.DoWork;
|
|
|
|
|
begin
|
2018-01-25 21:34:04 +01:00
|
|
|
|
DoMessageLoopWork;
|
|
|
|
|
NextPulse(FDefaultInterval);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TCEFWorkScheduler.ScheduleWork(const delay_ms : int64);
|
|
|
|
|
begin
|
2018-01-25 21:34:04 +01:00
|
|
|
|
if not(FStopped) then
|
2017-12-05 10:02:07 +01:00
|
|
|
|
begin
|
2018-01-25 21:34:04 +01:00
|
|
|
|
if (delay_ms <= 0) then
|
|
|
|
|
DoWork
|
|
|
|
|
else
|
|
|
|
|
NextPulse(delay_ms);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
2018-01-25 21:34:04 +01:00
|
|
|
|
end;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
|
procedure TCEFWorkScheduler.NextPulse(aInterval : integer);
|
|
|
|
|
begin
|
|
|
|
|
if (FThread <> nil) then FThread.NextPulse(aInterval);
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
|
{$IFDEF FPC}
|
|
|
|
|
procedure Register;
|
|
|
|
|
begin
|
2018-06-03 17:18:54 +02:00
|
|
|
|
{$I res/tcefworkscheduler.lrs}
|
2018-05-12 14:50:54 +02:00
|
|
|
|
RegisterComponents('Chromium', [TCEFWorkScheduler]);
|
|
|
|
|
end;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2017-12-05 10:02:07 +01:00
|
|
|
|
end.
|