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
|
|
|
|
//
|
2019-01-08 20:15:25 +02:00
|
|
|
// Copyright � 2019 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.
|
|
|
|
*
|
|
|
|
*)
|
|
|
|
|
|
|
|
unit uCEFWorkSchedulerThread;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
|
|
|
|
2018-11-22 16:02:21 +02:00
|
|
|
{$IFNDEF CPUX64}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
2018-01-25 22:34:04 +02:00
|
|
|
|
|
|
|
{$I cef.inc}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
System.Classes, System.SyncObjs,
|
|
|
|
{$ELSE}
|
|
|
|
Classes, SyncObjs,
|
|
|
|
{$ENDIF}
|
|
|
|
uCEFConstants;
|
|
|
|
|
|
|
|
type
|
|
|
|
TCEFWorkSchedulerThread = class(TThread)
|
|
|
|
protected
|
|
|
|
FCritSect : TCriticalSection;
|
|
|
|
FInterval : integer;
|
|
|
|
FEvent : TEvent;
|
|
|
|
FOnPulse : TNotifyEvent;
|
|
|
|
FPulsing : boolean;
|
|
|
|
FMustReset : boolean;
|
|
|
|
FDefaultInterval : integer;
|
|
|
|
|
|
|
|
function Lock : boolean;
|
|
|
|
procedure Unlock;
|
|
|
|
function CanPulse(var aInterval : integer) : boolean;
|
|
|
|
procedure DoOnPulseEvent;
|
|
|
|
procedure EventTimeOut;
|
|
|
|
procedure SignaledEvent;
|
|
|
|
procedure Execute; override;
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
procedure AfterConstruction; override;
|
|
|
|
procedure NextPulse(aInterval : integer);
|
|
|
|
|
|
|
|
property DefaultInterval : integer read FDefaultInterval write FDefaultInterval default CEF_TIMER_MAXDELAY;
|
|
|
|
property OnPulse : TNotifyEvent read FOnPulse write FOnPulse;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
{$IFDEF DELPHI16_UP}
|
2019-11-09 00:15:53 +02:00
|
|
|
System.SysUtils, System.Math;
|
2018-01-25 22:34:04 +02:00
|
|
|
{$ELSE}
|
2019-11-09 00:15:53 +02:00
|
|
|
SysUtils, Math;
|
2018-01-25 22:34:04 +02:00
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
constructor TCEFWorkSchedulerThread.Create;
|
|
|
|
begin
|
|
|
|
FOnPulse := nil;
|
|
|
|
FCritSect := nil;
|
|
|
|
FPulsing := False;
|
|
|
|
FEvent := nil;
|
|
|
|
FDefaultInterval := CEF_TIMER_MAXDELAY;
|
|
|
|
FInterval := FDefaultInterval;
|
|
|
|
FMustReset := False;
|
|
|
|
|
|
|
|
inherited Create(True);
|
|
|
|
|
|
|
|
FreeOnTerminate := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TCEFWorkSchedulerThread.Destroy;
|
|
|
|
begin
|
|
|
|
if (FEvent <> nil) then FreeAndNil(FEvent);
|
|
|
|
if (FCritSect <> nil) then FreeAndNil(FCritSect);
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.AfterConstruction;
|
|
|
|
begin
|
|
|
|
inherited AfterConstruction;
|
|
|
|
|
|
|
|
FEvent := TEvent.Create(nil, False, False, '');
|
|
|
|
FCritSect := TCriticalSection.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.DoOnPulseEvent;
|
|
|
|
begin
|
|
|
|
if assigned(FOnPulse) then FOnPulse(self);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCEFWorkSchedulerThread.Lock : boolean;
|
|
|
|
begin
|
|
|
|
if not(Terminated) and (FCritSect <> nil) then
|
|
|
|
begin
|
|
|
|
FCritSect.Acquire;
|
|
|
|
Result := True;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.Unlock;
|
|
|
|
begin
|
|
|
|
if (FCritSect <> nil) then FCritSect.Release;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.NextPulse(aInterval : integer);
|
|
|
|
begin
|
|
|
|
if Lock then
|
|
|
|
try
|
|
|
|
FInterval := min(aInterval, CEF_TIMER_MAXDELAY);
|
|
|
|
FMustReset := True;
|
|
|
|
|
|
|
|
if FPulsing then
|
|
|
|
begin
|
|
|
|
FPulsing := False;
|
|
|
|
FEvent.SetEvent;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
Unlock;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.EventTimeOut;
|
|
|
|
begin
|
|
|
|
if Lock then
|
|
|
|
try
|
|
|
|
if FMustReset then
|
|
|
|
begin
|
|
|
|
FInterval := FDefaultInterval;
|
|
|
|
FMustReset := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
FPulsing := False;
|
|
|
|
finally
|
|
|
|
Unlock;
|
2018-05-12 14:50:54 +02:00
|
|
|
if not(Terminated) then Synchronize({$IFDEF FPC}self, @{$ENDIF}DoOnPulseEvent);
|
2018-01-25 22:34:04 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.SignaledEvent;
|
|
|
|
begin
|
|
|
|
if Lock then
|
|
|
|
try
|
|
|
|
FPulsing := False;
|
|
|
|
finally
|
|
|
|
Unlock;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCEFWorkSchedulerThread.CanPulse(var aInterval : integer) : boolean;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
|
|
|
|
if Lock then
|
|
|
|
try
|
|
|
|
aInterval := FInterval;
|
|
|
|
|
|
|
|
if (aInterval > 0) then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
FPulsing := True;
|
|
|
|
FEvent.ResetEvent;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
Unlock;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.Execute;
|
|
|
|
var
|
|
|
|
TempInterval : integer;
|
|
|
|
begin
|
|
|
|
while CanPulse(TempInterval) do
|
|
|
|
if (FEvent.WaitFor(TempInterval) = wrTimeout) then
|
|
|
|
EventTimeOut
|
|
|
|
else
|
|
|
|
SignaledEvent;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|