|
| 1 | +//---------------------------------------- |
| 2 | +// Copyright © yanghy. All Rights Reserved. |
| 3 | + |
| 4 | +// Licensed under Lazarus.modifiedLGPL |
| 5 | +//---------------------------------------- |
| 6 | + |
| 7 | +unit uCEF_LCL_Task; |
| 8 | + |
| 9 | +{$mode objfpc}{$H+} |
| 10 | +{$I cef.inc} |
| 11 | + |
| 12 | +interface |
| 13 | + |
| 14 | +uses |
| 15 | + Classes, SysUtils, uCEFInterfaces, uCEFTypes, |
| 16 | + uCEFTask, |
| 17 | + uCEFMiscFunctions; |
| 18 | + |
| 19 | +type |
| 20 | + |
| 21 | + // TTask |
| 22 | + TTaskExecute = procedure() of object; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Implement this interface for asynchronous task execution. If the task is |
| 26 | + /// posted successfully and if the associated message loop is still running then |
| 27 | + /// the execute() function will be called on the target thread. If the task |
| 28 | + /// fails to post then the task object may be destroyed on the source thread |
| 29 | + /// instead of the target thread. For this reason be cautious when performing |
| 30 | + /// work in the task object destructor. |
| 31 | + /// </summary> |
| 32 | + /// <remarks> |
| 33 | + /// <para><see cref="uCEFTypes|TCefTask">Implements TCefTask</see></para> |
| 34 | + /// <para><see href="https://bitbucket.org/chromiumembedded/cef/src/master/include/capi/cef_task_capi.h">CEF source file: /include/capi/cef_task_capi.h (cef_task_t)</see></para> |
| 35 | + /// </remarks> |
| 36 | + TTask = class(TCefTaskOwn) |
| 37 | + protected |
| 38 | + FOnly: boolean; |
| 39 | + FOnExecute: TTaskExecute; |
| 40 | + /// <summary> |
| 41 | + /// Method that will be executed on the target thread. |
| 42 | + /// </summary> |
| 43 | + procedure Execute; override; |
| 44 | + public |
| 45 | + constructor Create; override; |
| 46 | + destructor Destroy; override; |
| 47 | + function OnlyPostTask(threadId: TCefThreadId): Boolean; |
| 48 | + function OnlyPostDelayedTask(threadId: TCefThreadId; Delay: int64): Boolean; |
| 49 | + published |
| 50 | + /// <summary> |
| 51 | + /// Method that will be executed on the target thread. |
| 52 | + /// </summary> |
| 53 | + property OnExecute: TTaskExecute read FOnExecute write FOnExecute; |
| 54 | + end; |
| 55 | + |
| 56 | +implementation |
| 57 | + |
| 58 | +constructor TTask.Create(); |
| 59 | +begin |
| 60 | + inherited Create; |
| 61 | + FOnExecute := nil; |
| 62 | + FOnly := True; |
| 63 | +end; |
| 64 | + |
| 65 | +destructor TTask.Destroy; |
| 66 | +begin |
| 67 | + inherited Destroy; |
| 68 | + FOnExecute := nil; |
| 69 | +end; |
| 70 | + |
| 71 | +function TTask.OnlyPostTask(threadId: TCefThreadId): Boolean; |
| 72 | +begin |
| 73 | + Result := False; |
| 74 | + if FOnly and assigned(FOnExecute) then |
| 75 | + begin |
| 76 | + CefPostTask(threadId, self); |
| 77 | + FOnly := False; |
| 78 | + Result := True; |
| 79 | + end; |
| 80 | +end; |
| 81 | + |
| 82 | +function TTask.OnlyPostDelayedTask(threadId: TCefThreadId; Delay: int64): Boolean; |
| 83 | +begin |
| 84 | + Result := False; |
| 85 | + if FOnly and assigned(FOnExecute) then |
| 86 | + begin |
| 87 | + CefPostDelayedTask(threadId, self, Delay); |
| 88 | + FOnly := False; |
| 89 | + Result := True; |
| 90 | + end; |
| 91 | +end; |
| 92 | + |
| 93 | +procedure TTask.Execute; |
| 94 | +begin |
| 95 | + if assigned(FOnExecute) then |
| 96 | + begin |
| 97 | + FOnExecute(); |
| 98 | + end; |
| 99 | +end; |
| 100 | + |
| 101 | +end. |
0 commit comments