2017-12-05 10:02:07 +01:00
|
|
|
|
// ************************************************************************
|
|
|
|
|
// ***************************** CEF4Delphi *******************************
|
|
|
|
|
// ************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// CEF4Delphi is based on DCEF3 which uses CEF3 to embed a chromium-based
|
|
|
|
|
// 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
|
|
|
|
|
//
|
2018-01-06 15:25:32 +01:00
|
|
|
|
// Copyright <20> 2018 Salvador D<>az 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;
|
|
|
|
|
|
|
|
|
|
{$IFNDEF CPUX64}
|
|
|
|
|
{$ALIGN ON}
|
|
|
|
|
{$MINENUMSIZE 4}
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
|
|
{$I cef.inc}
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
{$IFDEF DELPHI16_UP}
|
2018-02-19 13:35:01 +01:00
|
|
|
|
{$IFDEF MSWINDOWS}WinApi.Windows, WinApi.Messages, Vcl.Controls, Vcl.Graphics, Vcl.Forms,{$ENDIF} System.Classes,
|
2017-12-05 10:02:07 +01:00
|
|
|
|
{$ELSE}
|
|
|
|
|
Windows, Messages, Classes, Controls, Graphics, Forms,
|
|
|
|
|
{$ENDIF}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
uCEFConstants, uCEFWorkSchedulerThread;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
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;
|
2017-12-05 10:02:07 +01:00
|
|
|
|
procedure WndProc(var aMessage: TMessage);
|
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;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
|
System.SysUtils, System.Math,
|
|
|
|
|
{$ELSE}
|
|
|
|
|
SysUtils, Math,
|
|
|
|
|
{$ENDIF}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
uCEFMiscFunctions, uCEFApplication;
|
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
|
|
|
|
|
FCompHandle := AllocateHWnd(WndProc);
|
|
|
|
|
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;
|
|
|
|
|
FThread.OnPulse := Thread_OnPulse;
|
2018-02-07 14:30:44 +01:00
|
|
|
|
{$IFDEF DELPHI14_UP}
|
2018-01-25 21:34:04 +01:00
|
|
|
|
FThread.Start;
|
|
|
|
|
{$ELSE}
|
|
|
|
|
FThread.Resume;
|
|
|
|
|
{$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;
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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
|
2018-01-25 21:34:04 +01:00
|
|
|
|
if not(FStopped) and (FCompHandle <> 0) then
|
|
|
|
|
PostMessage(FCompHandle, CEF_PUMPHAVEWORK, 0, LPARAM(delay_ms));
|
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;
|
|
|
|
|
|
|
|
|
|
end.
|