2018-01-25 22:34:04 +02:00
|
|
|
// ************************************************************************
|
|
|
|
// ***************************** CEF4Delphi *******************************
|
|
|
|
// ************************************************************************
|
|
|
|
//
|
2019-10-19 10:58:34 +02:00
|
|
|
// CEF4Delphi is based on DCEF3 which uses CEF to embed a chromium-based
|
2018-01-25 22:34:04 +02: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
|
|
|
|
//
|
2023-01-07 16:53:04 +02:00
|
|
|
// Copyright � 2023 Salvador Diaz Fau. All rights reserved.
|
2018-01-25 22:34:04 +02: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.
|
|
|
|
*
|
|
|
|
*)
|
|
|
|
|
2019-04-04 10:26:44 +02:00
|
|
|
unit uCEFFMXWorkScheduler;
|
2018-01-25 22:34:04 +02:00
|
|
|
|
|
|
|
{$I cef.inc}
|
|
|
|
|
2022-02-19 19:56:41 +02:00
|
|
|
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
|
|
|
|
2018-01-25 22:34:04 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.Classes, System.Types,
|
|
|
|
FMX.Types, FMX.Controls,
|
2021-05-16 19:42:25 +02:00
|
|
|
uCEFConstants, uCEFWorkSchedulerQueueThread, uCEFWorkSchedulerThread;
|
2018-01-25 22:34:04 +02:00
|
|
|
|
|
|
|
type
|
2022-10-14 16:35:50 +02:00
|
|
|
{$IFNDEF FPC}{$IFDEF DELPHI16_UP}[ComponentPlatformsAttribute(pfidWindows or pfidOSX or pfidLinux)]{$ENDIF}{$ENDIF}
|
2018-01-25 22:34:04 +02:00
|
|
|
TFMXWorkScheduler = class(TComponent)
|
|
|
|
protected
|
|
|
|
FThread : TCEFWorkSchedulerThread;
|
2021-02-11 20:52:23 +02:00
|
|
|
FQueueThread : TCEFWorkSchedulerQueueThread;
|
2018-01-25 22:34:04 +02:00
|
|
|
FDepleteWorkCycles : cardinal;
|
|
|
|
FDepleteWorkDelay : cardinal;
|
|
|
|
FDefaultInterval : integer;
|
|
|
|
FStopped : boolean;
|
2021-05-16 19:42:25 +02:00
|
|
|
FUseQueueThread : boolean;
|
2018-01-25 22:34:04 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
{$WARN SYMBOL_PLATFORM OFF}
|
|
|
|
FPriority : TThreadPriority;
|
|
|
|
{$WARN SYMBOL_PLATFORM ON}
|
|
|
|
{$ENDIF}
|
|
|
|
|
2021-02-11 20:52:23 +02:00
|
|
|
procedure CreateQueueThread;
|
|
|
|
procedure DestroyQueueThread;
|
|
|
|
procedure QueueThread_OnPulse(Sender : TObject; aDelay : integer);
|
|
|
|
|
2018-01-25 22:34:04 +02:00
|
|
|
procedure DestroyThread;
|
|
|
|
procedure DepleteWork;
|
|
|
|
procedure NextPulse(aInterval : integer);
|
|
|
|
procedure DoWork;
|
|
|
|
procedure DoMessageLoopWork;
|
2021-01-20 19:57:20 +02:00
|
|
|
procedure Initialize;
|
2018-01-25 22:34:04 +02: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);
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create(AOwner: TComponent); override;
|
2021-01-20 19:57:20 +02:00
|
|
|
constructor CreateDelayed;
|
2018-01-25 22:34:04 +02:00
|
|
|
destructor Destroy; override;
|
|
|
|
procedure ScheduleMessagePumpWork(const delay_ms : int64);
|
|
|
|
procedure StopScheduler;
|
|
|
|
procedure ScheduleWork(const delay_ms : int64);
|
2021-01-20 19:57:20 +02:00
|
|
|
procedure CreateThread;
|
2018-01-25 22:34:04 +02:00
|
|
|
|
2021-05-16 19:42:25 +02:00
|
|
|
|
2018-01-25 22:34:04 +02:00
|
|
|
published
|
|
|
|
{$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;
|
2021-05-16 19:42:25 +02:00
|
|
|
property UseQueueThread : boolean read FUseQueueThread write FUseQueueThread default False;
|
2018-01-25 22:34:04 +02:00
|
|
|
end;
|
|
|
|
|
2018-04-07 20:24:55 +02:00
|
|
|
var
|
|
|
|
GlobalFMXWorkScheduler : TFMXWorkScheduler = nil;
|
|
|
|
|
2018-06-17 14:18:11 +02:00
|
|
|
procedure DestroyGlobalFMXWorkScheduler;
|
|
|
|
|
2018-01-25 22:34:04 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2018-02-19 14:35:01 +02:00
|
|
|
{$IFDEF MSWINDOWS}WinApi.Windows,{$ENDIF} System.SysUtils, System.Math,
|
2020-01-28 12:36:34 +02:00
|
|
|
FMX.Platform, {$IFDEF MSWINDOWS}FMX.Platform.Win,{$ENDIF} FMX.Forms,
|
2019-11-09 00:15:53 +02:00
|
|
|
uCEFMiscFunctions, uCEFApplicationCore;
|
2018-01-25 22:34:04 +02:00
|
|
|
|
2018-06-17 14:18:11 +02:00
|
|
|
procedure DestroyGlobalFMXWorkScheduler;
|
|
|
|
begin
|
|
|
|
if (GlobalFMXWorkScheduler <> nil) then FreeAndNil(GlobalFMXWorkScheduler);
|
|
|
|
end;
|
|
|
|
|
2018-01-25 22:34:04 +02:00
|
|
|
constructor TFMXWorkScheduler.Create(AOwner: TComponent);
|
|
|
|
begin
|
|
|
|
inherited Create(AOwner);
|
|
|
|
|
2021-01-20 19:57:20 +02:00
|
|
|
Initialize;
|
|
|
|
|
|
|
|
if not(csDesigning in ComponentState) then CreateThread;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TFMXWorkScheduler.CreateDelayed;
|
|
|
|
begin
|
|
|
|
inherited Create(nil);
|
|
|
|
|
|
|
|
Initialize;
|
2018-01-25 22:34:04 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TFMXWorkScheduler.Destroy;
|
|
|
|
begin
|
|
|
|
DestroyThread;
|
2021-02-11 20:52:23 +02:00
|
|
|
DestroyQueueThread;
|
2018-01-25 22:34:04 +02:00
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2021-01-20 19:57:20 +02:00
|
|
|
procedure TFMXWorkScheduler.Initialize;
|
2018-01-25 22:34:04 +02:00
|
|
|
begin
|
2021-05-16 19:42:25 +02:00
|
|
|
FUseQueueThread := False;
|
2021-01-20 19:57:20 +02:00
|
|
|
FThread := nil;
|
2021-02-11 20:52:23 +02:00
|
|
|
FQueueThread := nil;
|
2021-01-20 19:57:20 +02:00
|
|
|
FStopped := False;
|
|
|
|
{$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;
|
2018-01-25 22:34:04 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.CreateThread;
|
|
|
|
begin
|
2021-01-20 19:57:20 +02:00
|
|
|
if (FThread <> nil) then exit;
|
|
|
|
|
2018-01-25 22:34:04 +02:00
|
|
|
FThread := TCEFWorkSchedulerThread.Create;
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
FThread.Priority := FPriority;
|
|
|
|
{$ENDIF}
|
|
|
|
FThread.DefaultInterval := FDefaultInterval;
|
|
|
|
FThread.OnPulse := Thread_OnPulse;
|
|
|
|
FThread.Start;
|
2021-02-11 20:52:23 +02:00
|
|
|
|
2021-05-16 19:42:25 +02:00
|
|
|
if FUseQueueThread then
|
|
|
|
CreateQueueThread;
|
2018-01-25 22:34:04 +02:00
|
|
|
end;
|
|
|
|
|
2021-02-11 20:52:23 +02:00
|
|
|
procedure TFMXWorkScheduler.CreateQueueThread;
|
|
|
|
begin
|
|
|
|
FQueueThread := TCEFWorkSchedulerQueueThread.Create;
|
|
|
|
FQueueThread.OnPulse := {$IFDEF FPC}@{$ENDIF}QueueThread_OnPulse;
|
|
|
|
{$IFDEF DELPHI14_UP}
|
|
|
|
FQueueThread.Start;
|
|
|
|
{$ELSE}
|
|
|
|
{$IFNDEF FPC}
|
|
|
|
FQueueThread.Resume;
|
|
|
|
{$ELSE}
|
|
|
|
FQueueThread.Start;
|
|
|
|
{$ENDIF}
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.DestroyQueueThread;
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
if (FQueueThread <> nil) then
|
|
|
|
begin
|
|
|
|
FQueueThread.Terminate;
|
|
|
|
FQueueThread.StopThread;
|
|
|
|
FQueueThread.WaitFor;
|
|
|
|
FreeAndNil(FQueueThread);
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TFMXWorkScheduler.DestroyQueueThread', e) then raise;
|
|
|
|
end;
|
|
|
|
end;
|
2021-02-11 22:07:22 +02:00
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.QueueThread_OnPulse(Sender : TObject; aDelay : integer);
|
|
|
|
begin
|
|
|
|
ScheduleWork(aDelay);
|
|
|
|
end;
|
2021-02-11 20:52:23 +02:00
|
|
|
|
2018-01-25 22:34:04 +02:00
|
|
|
procedure TFMXWorkScheduler.DestroyThread;
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
if (FThread <> nil) then
|
|
|
|
begin
|
|
|
|
FThread.Terminate;
|
|
|
|
FThread.NextPulse(0);
|
|
|
|
FThread.WaitFor;
|
|
|
|
FreeAndNil(FThread);
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TFMXWorkScheduler.DestroyThread', e) then raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.DoMessageLoopWork;
|
|
|
|
begin
|
|
|
|
if (GlobalCEFApp <> nil) then GlobalCEFApp.DoMessageLoopWork;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.SetDefaultInterval(aValue : integer);
|
|
|
|
begin
|
|
|
|
FDefaultInterval := aValue;
|
|
|
|
if (FThread <> nil) then FThread.DefaultInterval := aValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
{$WARN SYMBOL_PLATFORM OFF}
|
|
|
|
procedure TFMXWorkScheduler.SetPriority(aValue : TThreadPriority);
|
|
|
|
begin
|
|
|
|
FPriority := aValue;
|
|
|
|
if (FThread <> nil) then FThread.Priority := aValue;
|
|
|
|
end;
|
|
|
|
{$WARN SYMBOL_PLATFORM ON}
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.DepleteWork;
|
|
|
|
var
|
|
|
|
i : cardinal;
|
|
|
|
begin
|
|
|
|
i := FDepleteWorkCycles;
|
|
|
|
|
|
|
|
while (i > 0) do
|
|
|
|
begin
|
|
|
|
DoMessageLoopWork;
|
|
|
|
Sleep(FDepleteWorkDelay);
|
|
|
|
dec(i);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.ScheduleMessagePumpWork(const delay_ms : int64);
|
|
|
|
begin
|
2021-02-11 20:52:23 +02:00
|
|
|
if FStopped then exit;
|
|
|
|
|
2021-05-16 19:42:25 +02:00
|
|
|
if FUseQueueThread and (FQueueThread <> nil) and FQueueThread.Ready then
|
|
|
|
FQueueThread.EnqueueValue(integer(delay_ms))
|
|
|
|
else
|
2022-03-06 11:21:37 +02:00
|
|
|
{$IFDEF DELPHI25_UP}
|
2021-12-08 18:45:03 +02:00
|
|
|
TThread.ForceQueue(nil,
|
|
|
|
{$ELSE}
|
|
|
|
TThread.Queue(nil,
|
|
|
|
{$ENDIF}
|
|
|
|
procedure
|
|
|
|
begin
|
|
|
|
ScheduleWork(delay_ms);
|
|
|
|
end);
|
2018-01-25 22:34:04 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.StopScheduler;
|
|
|
|
begin
|
|
|
|
FStopped := True;
|
|
|
|
NextPulse(0);
|
|
|
|
DepleteWork;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.Thread_OnPulse(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if not(FStopped) then DoMessageLoopWork;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.DoWork;
|
|
|
|
begin
|
|
|
|
DoMessageLoopWork;
|
|
|
|
NextPulse(FDefaultInterval);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.ScheduleWork(const delay_ms : int64);
|
|
|
|
begin
|
|
|
|
if not(FStopped) then
|
|
|
|
begin
|
|
|
|
if (delay_ms <= 0) then
|
|
|
|
DoWork
|
|
|
|
else
|
|
|
|
NextPulse(delay_ms);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFMXWorkScheduler.NextPulse(aInterval : integer);
|
|
|
|
begin
|
|
|
|
if (FThread <> nil) then FThread.NextPulse(aInterval);
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|