-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuCefWebActionBase.pas
368 lines (315 loc) · 9.48 KB
/
uCefWebActionBase.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
unit uCefWebActionBase;
interface
uses
Sysutils, Classes, System.SyncObjs,
//
uCEFInterfaces, uCEFTypes, uCEFChromium,
uReLog3, uLoggerInterface;
type
TCefWebActionBase = class abstract
private
FChromium: TChromium;
//---
function GetIsSucccess: Boolean;
function GetLocationURL: string;
protected
FDestroying: Boolean;
FLog: TReLog3;
FLogger: ILoggerInterface;
FAbortEvent: TEvent;
FEventObjOwn: Boolean;
FActionName: string;
FTimeout: Integer;
FAborted: Boolean;
FFail: Boolean;
FAutoSetFail: Boolean;
FIgnoreFail: Boolean;
FErrorStr: string;
//---
function DoNavStop: Boolean;
function DoStartEvent: Boolean; virtual; abstract;
procedure SetChromium(const Value: TChromium);
function GetChromium: TChromium; virtual;
public
constructor Create(const AName: string; const ALogger: ILoggerInterface;
const AWeb: TChromium; const ATimeout: Integer; const AAbortEvent: TEvent);
destructor Destroy; override;
procedure BeforeDestruction; override;
function Start: Boolean; virtual;
function Sleep(const A: Integer; const AWaitObj: TEvent = nil;
const AAbortOnFail: Boolean = True): TWaitResult; overload;
function Sleep(const A: string; const AWaitObj: TEvent = nil;
const AAbortOnFail: Boolean = True): TWaitResult; overload;
function Wait: TWaitResult; virtual;
procedure Abort; virtual;
procedure AbortMsg(const AErrorMessage: string); overload;
procedure AbortMsg(const AErrorMessageFormat: string; const AArgs: array of const); overload;
procedure FailMsg(const AErrorMessage: string); overload;
procedure FailMsg(const AErrorMessageFormat: string; const AArgs: array of const); overload;
{$HINTS OFF}
procedure LogLog(const ALevel: TLogLevel; const AFormat: string; const AArgs: array of const); overload;
procedure LogLog(const ALevel: TLogLevel; const A: string); overload;
procedure LogDebug(const A: string); overload;
procedure LogDebug(const AFormat: string; const AArgs: array of const); overload;
procedure LogInfo(const A: string); overload;
procedure LogInfo(const AFormat: string; const AArgs: array of const); overload;
procedure LogError(const A: string); overload;
procedure LogError(const AFormat: string; const AArgs: array of const); overload;
procedure LogSuccess(const A: string); overload;
procedure LogSuccess(const AFormat: string; const AArgs: array of const); overload;
{$HINTS ON}
property IsAborted: Boolean read FAborted;
property IsFail: Boolean read FFail write FFail;
property IsSuccess: Boolean read GetIsSucccess;
property LocationURL: string read GetLocationURL;
property Chromium: TChromium read GetChromium write SetChromium;
property Logger: ILoggerInterface read FLogger;
property AbortEvent: TEvent read FAbortEvent;
property ErrorStr: string read FErrorStr;
property IgnoreFail: Boolean read FIgnoreFail;
property ActionName: string read FActionName;
end;
function SleepEvents(E1, E2: TEvent; const A: Integer): TWaitResult; overload;
function SleepEvents(E1, E2: TEvent; const A: Integer; var AFired: THandleObject): TWaitResult; overload;
implementation
uses
AcedBinary,
//
uGlobalFunctions, uStringUtils,
//
uCefUtilConst;
const
TIMEOUT_DEF = 1000;
WAIT_RESULT_STR: array[TWaitResult] of string = ('wrSignaled', 'wrTimeout',
'wrAbandoned', 'wrError', 'wrIOCompletion');
procedure SwapEvents(var E1, E2: TEvent);
begin
G_Swap32(E1, E2);
end;
function SleepEvents(E1, E2: TEvent; const A: Integer; var AFired: THandleObject): TWaitResult;
var arr: THandleObjectArray;
begin
if E1 = nil then
begin
SwapEvents(E1, E2);
if E1 = nil then
Exit(wrSignaled);
end;
if Assigned(E2) then
begin
SetLength(arr, 2);
arr[0] := E1;
arr[1] := E2;
Result := TEvent.WaitForMultiple(arr, A, False, AFired)
end
else
begin
AFired := E1;
Result := E1.WaitFor(A)
end
end;
function SleepEvents(E1, E2: TEvent; const A: Integer): TWaitResult;
var tmp: THandleObject;
begin
Result := SleepEvents(E1, E2, A, tmp)
end;
{ TCefWebActionBase }
constructor TCefWebActionBase.Create(const AName: string; const ALogger: ILoggerInterface;
const AWeb: TChromium; const ATimeout: Integer; const AAbortEvent: TEvent);
var logprefix: string;
begin
inherited Create;
FChromium := AWeb;
FActionName := AName;
if Assigned(ALogger) then
begin
logprefix := '';
if FActionName <> '' then
logprefix := '/' + FActionName;
FLog := TReLog3.Create(logprefix, ALogger, '');
FLogger := FLog;
end;
FTimeout := ATimeout * 1000;
FAbortEvent := AAbortEvent;
if not Assigned(FAbortEvent) then
begin
FAbortEvent := TEvent.Create;
FAbortEvent.ResetEvent();
FEventObjOwn := True;
end;
end;
destructor TCefWebActionBase.Destroy;
begin
FLog := nil;
FLogger := nil;
FChromium := nil;
if FEventObjOwn then
FAbortEvent.Free;
if FFail then
LogDebug('fail')
else
if IsAborted then
LogDebug('abort')
// else
// if IsSuccess then
// LogDebug('success')
else
LogDebug('done');
inherited;
end;
procedure TCefWebActionBase.Abort;
begin
LogDebug('setabort');
FAborted := True;
FAbortEvent.SetEvent()
end;
function TCefWebActionBase.DoNavStop: Boolean;
var B: TChromium;
begin
B := Chromium;
if B = nil then
Exit(False);
if B.IsLoading then
begin
B.StopLoad();
LogInfo('stop loading...');
Result := Sleep(NAV_WAIT_TIMEOUT) = wrTimeout;
Exit
end;
Result := True
end;
procedure TCefWebActionBase.LogLog(const ALevel: TLogLevel; const A: string);
begin
if Assigned(FLog) then
FLog.Log(ALevel, A)
end;
procedure TCefWebActionBase.LogLog(const ALevel: TLogLevel; const AFormat: string;
const AArgs: array of const);
begin
if Assigned(FLog) then
FLog.Log(ALevel, AFormat, AArgs)
end;
procedure TCefWebActionBase.LogSuccess(const A: string);
begin
LogLog(TLogLevel.logSuccess, A)
end;
procedure TCefWebActionBase.LogSuccess(const AFormat: string;
const AArgs: array of const);
begin
LogLog(TLogLevel.logSuccess, AFormat, AArgs)
end;
procedure TCefWebActionBase.LogDebug(const AFormat: string;
const AArgs: array of const);
begin
LogLog(TLogLevel.logDebug, AFormat, AArgs)
end;
procedure TCefWebActionBase.LogDebug(const A: string);
begin
LogLog(TLogLevel.logDebug, A)
end;
procedure TCefWebActionBase.LogError(const AFormat: string;
const AArgs: array of const);
begin
LogLog(TLogLevel.logError, AFormat, AArgs)
end;
procedure TCefWebActionBase.LogError(const A: string);
begin
LogLog(TLogLevel.logError, A)
end;
procedure TCefWebActionBase.LogInfo(const AFormat: string;
const AArgs: array of const);
begin
LogLog(TLogLevel.logInfo, AFormat, AArgs)
end;
procedure TCefWebActionBase.LogInfo(const A: string);
begin
LogLog(TLogLevel.logInfo, A)
end;
procedure TCefWebActionBase.AbortMsg(const AErrorMessage: string);
begin
LogError(AErrorMessage);
Abort()
end;
procedure TCefWebActionBase.AbortMsg(const AErrorMessageFormat: string;
const AArgs: array of const);
begin
AbortMsg(Format(AErrorMessageFormat, AArgs))
end;
procedure TCefWebActionBase.BeforeDestruction;
begin
inherited;
FDestroying := True;
end;
procedure TCefWebActionBase.FailMsg(const AErrorMessage: string);
begin
LogError(AErrorMessage);
FFail := True;
//Abort()
end;
procedure TCefWebActionBase.FailMsg(const AErrorMessageFormat: string;
const AArgs: array of const);
begin
FailMsg(Format(AErrorMessageFormat, AArgs))
end;
function TCefWebActionBase.GetChromium: TChromium;
begin
Result := FChromium
end;
function TCefWebActionBase.GetIsSucccess: Boolean;
begin
Result := (not FFail) and (not FAborted)
end;
function TCefWebActionBase.GetLocationURL: string;
begin
Result := Chromium.Browser.MainFrame.Url
end;
function TCefWebActionBase.Start: Boolean;
begin
Result := DoStartEvent();
if FAutoSetFail and not Result then
FFail := True
end;
function TCefWebActionBase.Sleep(const A: Integer;
const AWaitObj: TEvent; const AAbortOnFail: Boolean): TWaitResult;
var fired: THandleObject;
begin
LogDebug('.sleep ' + A.ToString);
if IsFail and AAbortOnFail then
begin
LogDebug('AbortOnFail');
Exit(wrError);
end;
if IsAborted then
begin
LogDebug('IsAborted');
Exit(wrAbandoned);
end;
Result := SleepEvents(FAbortEvent, AWaitObj, A, fired);
if (FAbortEvent <> nil) and (fired = FAbortEvent) then
LogDebug(WAIT_RESULT_STR[Result] + ' ~AbortEvent')
else
if (AWaitObj <> nil) and (fired = AWaitObj) then
LogDebug(WAIT_RESULT_STR[Result] + ' ~WaitObj')
else
LogDebug(WAIT_RESULT_STR[Result] + ' ~' + Cardinal(fired).ToString);
if Result = wrSignaled then
if FAbortEvent = fired then
begin
FAborted := True;
Result := wrAbandoned
end;
end;
procedure TCefWebActionBase.SetChromium(const Value: TChromium);
begin
FChromium := Value;
end;
function TCefWebActionBase.Sleep(const A: string;
const AWaitObj: TEvent; const AAbortOnFail: Boolean): TWaitResult;
begin
Result := Sleep(RandomRangeStr(A), AWaitObj, AAbortOnFail)
end;
function TCefWebActionBase.Wait: TWaitResult;
begin
Result := Sleep(IfElse(FTimeout < 1, TIMEOUT_DEF, FTimeout), nil)
end;
end.