-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuCefUtilFunc.pas
476 lines (409 loc) · 15.5 KB
/
uCefUtilFunc.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
unit uCefUtilFunc;
interface
uses
System.SysUtils, System.Classes, System.Types,
//
uCEFInterfaces, uCEFTypes;
type
ECefError = class(Exception);
ECefRenderError = class(ECefError);
TElementParams = record
Tag: string;
Id: string;
Name: string;
Class_: string;
AttrName: string;
AttrValue: string;
Text: string;
Center: Boolean;
constructor Create(const ATag, AId, AName, AClass, AAttrName, AAttrValue, AText: string);
class function CreateId(const AId: string; const ATag: string = ''): TElementParams; static;
class function CreateTagText(const ATag, ATextRegExp: string): TElementParams; static;
class function CreateTagName(const ATag, AName: string): TElementParams; static;
class function CreateTagClass(const ATag, AClass: string): TElementParams; static;
class function CreateTagAttr(const ATag, AAttrName, AAttrValue: string): TElementParams; static;
class function CreateCefListValue(const A: ICefListValue): TElementParams; static;
procedure SaveToCefListValue(const A: ICefListValue);
function IsEmpty: Boolean;
end;
function CefSendProcessMessageCurrentContextToBrowser(const AMsg: ICefProcessMessage): Boolean;
function ElemFilt(const ATag, AId, AName, AClass, AAttrName, AAttrValue, AText: string): TElementParams;
function ElemById(const AId: string; const ATag: string = ''): TElementParams;
function ElemByAttr(const ATag, AAttrName, AAttrValue: string): TElementParams;
function ElemByCefList(const A: ICefListValue): TElementParams;
function CefAppMessageNew: ICefProcessMessage;
function CefAppMessageArgs(var AArgs: ICefListValue): ICefProcessMessage;
function CefAppMessageType(const AType: Integer; var AArgs: ICefListValue): ICefProcessMessage;
function CefAppMessageTypeVal(const AType: Integer; const AValue: Integer): ICefProcessMessage; overload;
function CefAppMessageTypeVal(const AType: Integer; const AValue: string): ICefProcessMessage; overload;
function CefAppMessageTypeElem(const AType: Integer; const AElem: TElementParams;
var AArgs: ICefListValue): ICefProcessMessage; overload;
function CefAppMessageTypeElem(const AType: Integer;
const AElem: TElementParams): ICefProcessMessage; overload;
function CefAppMessageTypeElem(const AType: Integer; const AElem: TElementParams;
const AValue2: string; var AArgs: ICefListValue): ICefProcessMessage; overload;
function CefAppMessageResultNew(const AResult: string): ICefProcessMessage; overload;
function CefAppMessageResultNew(const AResult: Boolean): ICefProcessMessage; overload;
function CefAppMessageResultNew(const AResult: ICefListValue): ICefProcessMessage; overload;
function CefAppMessageTypeExecCallback(const ACallbackId: Integer; var AArgs: ICefListValue): ICefProcessMessage; overload;
function CefAppMessageTypeExecCallback(const ACallbackId: Integer): ICefProcessMessage; overload;
procedure CefExecJsCallback(const ABrowser: ICefBrowser; const AId: Integer);
function CefStringMapToDictValue(const A: ICefStringMap): ICefDictionaryValue;
//function CefListValueToJson(const A: ICefListValue; const APad: string): string;
function CefListValueToJsonStr(const A: ICefListValue): string;
procedure ContextSetPreferenceIfCan(const AContext: ICefRequestContext; const AName: string; const AValue: ICefValue); overload;
procedure ContextSetPreferenceIfCan(const AContext: ICefRequestContext; const AName, AValue: string); overload;
procedure ContextSetPreferenceIfCan(const AContext: ICefRequestContext; const AName: string; const AValue: Boolean); overload;
procedure ContextWebRtcDisable(const AContext: ICefRequestContext);
implementation
uses
//
uCEFStringMap, uCEFDictionaryValue, uCefTask, uCEFValue, uCefv8Context,
uCEFMiscFunctions, uCEFConstants, uCEFJson,
//
uGlobalFunctions,
//
uCEFProcessMessage, uCefUtilConst;
function CefAppMessageNew: ICefProcessMessage;
begin
Result := TCefProcessMessageRef.New(MYAPP_CEF_MESSAGE_NAME);
end;
function CefAppMessageArgs(var AArgs: ICefListValue): ICefProcessMessage;
begin
Result := CefAppMessageNew();
AArgs := Result.ArgumentList;
end;
function CefAppMessageType(const AType: Integer; var AArgs: ICefListValue): ICefProcessMessage;
begin
Result := CefAppMessageArgs(AArgs);
AArgs.SetInt(IDX_TYPE, AType);
end;
function CefAppMessageTypeVal(const AType: Integer; const AValue: Integer): ICefProcessMessage;
var args: ICefListValue;
begin
Result := CefAppMessageType(AType, args);
args.SetInt(IDX_VALUE, AValue);
end;
function CefAppMessageTypeVal(const AType: Integer; const AValue: string): ICefProcessMessage;
var args: ICefListValue;
begin
Result := CefAppMessageType(AType, args);
args.SetString(IDX_VALUE, AValue);
end;
function CefAppMessageTypeElem(const AType: Integer; const AElem: TElementParams;
var AArgs: ICefListValue): ICefProcessMessage;
begin
Result := CefAppMessageType(AType, AArgs);
AElem.SaveToCefListValue(AArgs);
end;
function CefAppMessageTypeElem(const AType: Integer;
const AElem: TElementParams): ICefProcessMessage;
var tmp: ICefListValue;
begin
Result := CefAppMessageType(AType, tmp);
AElem.SaveToCefListValue(tmp);
end;
function CefAppMessageTypeElem(const AType: Integer; const AElem: TElementParams;
const AValue2: string; var AArgs: ICefListValue): ICefProcessMessage;
begin
Result := CefAppMessageTypeElem(AType, AElem, AArgs);
AArgs.SetString(IDX_VALUE2, AValue2);
end;
function CefAppMessageResultNew(const AResult: string): ICefProcessMessage;
var arg: ICefListValue;
begin
Result := CefAppMessageArgs(arg);
arg.SetString(IDX_RESULT, AResult)
end;
function CefAppMessageResultNew(const AResult: Boolean): ICefProcessMessage;
var arg: ICefListValue;
begin
Result := CefAppMessageArgs(arg);
arg.SetBool(IDX_RESULT, AResult)
end;
function CefAppMessageResultNew(const AResult: ICefListValue): ICefProcessMessage;
var arg: ICefListValue;
begin
Result := CefAppMessageArgs(arg);
arg.SetList(IDX_RESULT, AResult)
end;
function CefAppMessageTypeExecCallback(const ACallbackId: Integer; var AArgs: ICefListValue): ICefProcessMessage;
begin
Result := CefAppMessageType(VAL_EXEC_CALLBACK, AArgs);
AArgs.SetInt(IDX_CALLBACK_ID, ACallbackId)
end;
function CefAppMessageTypeExecCallback(const ACallbackId: Integer): ICefProcessMessage;
var tmp: ICefListValue;
begin
Result := CefAppMessageTypeExecCallback(ACallbackId, tmp);
end;
function CefStringMapToDictValue(const A: ICefStringMap): ICefDictionaryValue;
var
j: Integer;
s: string;
begin
Result := TCefDictionaryValueRef.New;
for j := 0 to A.Size - 1 do
begin
s := A.Key[j];
Result.SetString(s, A.Value[j]);
end;
end;
function CefBinaryToStr(const A: ICefBinaryValue): string;
var buf, text: TBytes;
begin
SetLength(buf, A.GetSize);
A.GetData(@buf[0], Length(buf), 0);
SetLength(text, Length(buf) * 2);
BinToHex(buf, 0, text, 0, Length(buf));
Result := TEncoding.ANSI.GetString(text);
end;
(*
function CefDictValueToJson(const A: ICefDictionaryValue; const APad: string): string;
var
j, l: Integer;
keys: TStringList;
z: string;
v: ICefValue;
procedure put(const s: string);
begin
Result := Result + APad + '"' + z + '": ' + s + IfElse(j=l,'', ',') + #13#10
end;
begin
Result := '';
keys := TStringList.Create;
l := A.GetSize - 1;
j := 0;
try
A.GetKeys(keys);
for z in keys do
begin
v := A.GetValue(z);
case v.GetType of
VTYPE_INVALID: put('"!!!INVALID"');
VTYPE_NULL: put('null');
VTYPE_BOOL: put(IfElse(v.GetBool, 'true', 'false'));
VTYPE_INT: put(IntToStr(v.GetInt));
VTYPE_DOUBLE: put(v.GetDouble.ToString);
VTYPE_STRING: put('"' + v.GetString + '"');
VTYPE_BINARY: put('"' + CefBinaryToStr(v.GetBinary) + '"');
VTYPE_DICTIONARY: put('{'#13#10 + CefDictValueToJson(v.GetDictionary, APad + #9) + APad + '}');
VTYPE_LIST: put('['#13#10 + CefListValueToJson(v.GetList, APad + #9) + APad + ']');
end;
Inc(j);
end;
finally
keys.Free
end;
end;
function CefListValueToJson(const A: ICefListValue; const APad: string): string;
var
j, l: Integer;
v: ICefValue;
procedure put(const s: string);
begin
Result := Result + APad + s + IfElse(j=l,'', ',') + #13#10
end;
begin
Result := '';
l := A.GetSize;
if l = 0 then
Exit;
Dec(l);
for j := 0 to l do
begin
v := A.GetValue(j);
case v.GetType of
VTYPE_INVALID: put('"!!!INVALID"');
VTYPE_NULL: put('null');
VTYPE_BOOL: put(IfElse(v.GetBool, 'true', 'false'));
VTYPE_INT: put(IntToStr(v.GetInt));
VTYPE_DOUBLE: put(v.GetDouble.ToString);
VTYPE_STRING: put('"' + v.GetString + '"');
VTYPE_BINARY: put('"' + CefBinaryToStr(v.GetBinary) + '"');
VTYPE_DICTIONARY: put('{'#13#10 + CefDictValueToJson(v.GetDictionary, APad + #9) + APad + '}');
VTYPE_LIST: put('['#13#10 + CefListValueToJson(v.GetList, APad + #9) + APad + ']');
end;
end;
end;
function CefListValueToJsonStr(const A: ICefListValue): string;
begin
if A = nil then
Exit('nil');
Result := '['#13#10 + CefListValueToJson(A, '') + ']'#13#10
end;
*)
function CefListValueToJsonStr(const A: ICefListValue): string;
var val: ICefValue;
begin
val := TCefValueRef.New;
val.SetList(A);
Result := TCEFJson.Write(val, JSON_WRITER_PRETTY_PRINT) // JSON_WRITER_PRETTY_PRINT
end;
{ TElementParams }
constructor TElementParams.Create(const ATag, AId, AName, AClass, AAttrName,
AAttrValue, AText: string);
begin
Tag := ATag;
Id := AId;
Name := AName;
Class_ := AClass;
AttrName := AAttrName;
AttrValue := AAttrValue;
Text := AText;
Center := False;
end;
class function TElementParams.CreateCefListValue(
const A: ICefListValue): TElementParams;
begin
Result := TElementParams.Create(A.GetString(IDX_TAG), A.GetString(IDX_ID),
A.GetString(IDX_NAME), A.GetString(IDX_CLASS),
A.GetString(IDX_ATTR), A.GetString(IDX_VALUE), A.GetString(IDX_TEXT))
end;
class function TElementParams.CreateId(const AId, ATag: string): TElementParams;
begin
Result := TElementParams.Create(ATag, AId, '', '', '', '', '')
end;
class function TElementParams.CreateTagAttr(const ATag, AAttrName,
AAttrValue: string): TElementParams;
begin
Result := TElementParams.Create(ATag, '', '', '', AAttrName, AAttrValue, '')
end;
class function TElementParams.CreateTagClass(const ATag, AClass: string): TElementParams;
begin
Result := TElementParams.Create(ATag, '', '', AClass, '', '', '')
end;
class function TElementParams.CreateTagName(const ATag, AName: string): TElementParams;
begin
Result := TElementParams.Create(ATag, '', AName, '', '', '', '')
end;
class function TElementParams.CreateTagText(const ATag,
ATextRegExp: string): TElementParams;
begin
Result := TElementParams.Create(ATag, '', '', '', '', '', ATextRegExp)
end;
function TElementParams.IsEmpty: Boolean;
begin
Result :=
Tag.IsEmpty and
Id.IsEmpty and
Name.IsEmpty and
Class_.IsEmpty and
AttrName.IsEmpty and
AttrValue.IsEmpty
end;
procedure TElementParams.SaveToCefListValue(const A: ICefListValue);
begin
if Tag <> '' then
A.SetString(IDX_TAG, Tag);
if Id <> '' then
A.SetString(IDX_ID, Id);
if Name <> '' then
A.SetString(IDX_NAME, Name);
if Class_ <> '' then
A.SetString(IDX_CLASS, Class_);
if AttrName <> '' then
A.SetString(IDX_ATTR, AttrName);
if AttrValue <> '' then
A.SetString(IDX_VALUE, AttrValue);
if Text <> '' then
A.SetString(IDX_TEXT, Text);
end;
function ElemById(const AId, ATag: string): TElementParams;
begin
Result := TElementParams.CreateId(AId, ATag)
end;
function ElemFilt(const ATag, AId, AName, AClass, AAttrName, AAttrValue, AText: string): TElementParams;
begin
Result := TElementParams.Create(ATag, AId, AName, AClass, AAttrName, AAttrValue, AText)
end;
function ElemByAttr(const ATag, AAttrName, AAttrValue: string): TElementParams;
begin
Result := TElementParams.Create(ATag, '', '', '', AAttrName, AAttrValue, '')
end;
function ElemByCefList(const A: ICefListValue): TElementParams;
begin
Result := TElementParams.CreateCefListValue(A)
end;
procedure ContextWebRtcDisable(const AContext: ICefRequestContext);
begin
//ContextSetPreferenceIfCan(AContext, 'disable-webrtc', True);
//ContextSetPreferenceIfCan(AContext, 'enable-webrtc', False);
// Disables leaking of IPs via WebRTC on standard CEF builds.
// This options obsoleted from Chromium M50, but still exists in source. (Actually they are no more exist on fresh builds (55+?.)
ContextSetPreferenceIfCan(AContext, 'webrtc.multiple_routes_enabled', false);
ContextSetPreferenceIfCan(AContext, 'webrtc.nonproxied_udp_enabled', false);
// Values:
// See webrtc_ip_handling_policy.h for description.
// default
// default_public_and_private_interfaces
// default_public_interface_only
// disable_non_proxied_udp
ContextSetPreferenceIfCan(AContext, 'webrtc.ip_handling_policy', 'disable_non_proxied_udp');
// media.device_id_salt is chrome-only preference, so there is no way to randomize device ids.
end;
function CefSendProcessMessageBrowser(const ABrowser: ICefBrowser;
const ATarget: TCefProcessId; const AMsg: ICefProcessMessage): Boolean;
begin
ABrowser.MainFrame.SendProcessMessage(ATarget, AMsg);
Result := True;
end;
function CefSendProcessMessageBrowserToRender(const ABrowser: ICefBrowser;
const AMsg: ICefProcessMessage): Boolean;
begin
Result := CefSendProcessMessageBrowser(ABrowser, PID_RENDERER, AMsg);
end;
function CefSendProcessMessageCurrentContext(const ATarget: TCefProcessId; const AMsg: ICefProcessMessage): Boolean;
var curctx: ICefv8Context;
begin
curctx := TCefv8ContextRef.Current;
if Assigned(curctx) then
Result := CefSendProcessMessageBrowser(curctx.Browser, ATarget, AMsg)
else
Result := False
end;
function CefSendProcessMessageCurrentContextToRender(const AMsg: ICefProcessMessage): Boolean;
begin
Result := CefSendProcessMessageCurrentContext(PID_RENDERER, AMsg);
end;
function CefSendProcessMessageCurrentContextToBrowser(const AMsg: ICefProcessMessage): Boolean;
begin
Result := CefSendProcessMessageCurrentContext(PID_BROWSER, AMsg);
end;
procedure CefExecJsCallback(const ABrowser: ICefBrowser; const AId: Integer);
var
msg: ICefProcessMessage;
begin
if aid < 1 then
Exit;
msg := CefAppMessageTypeExecCallback(AId);
CefSendProcessMessageBrowserToRender(ABrowser, msg)
end;
procedure ContextSetPreferenceIfCan(const AContext: ICefRequestContext; const AName, AValue: string);
var cefval: ICefValue;
begin
cefval := TCefValueRef.New;
cefval.SetString(AValue);
ContextSetPreferenceIfCan(AContext, AName, cefval)
end;
procedure ContextSetPreferenceIfCan(const AContext: ICefRequestContext; const AName: string; const AValue: ICefValue);
begin
TCefFastTask.New(TID_UI, procedure
var
//res: Boolean;
err: ustring;
begin
{res := }AContext.SetPreference(AName, AValue, err);
// if not res then
// gApp.Log.Error('fail context.SetPreference %s "%s"', [AName, err]);
end
);
end;
procedure ContextSetPreferenceIfCan(const AContext: ICefRequestContext; const AName: string; const AValue: Boolean);
var cefval: ICefValue;
begin
cefval := TCefValueRef.New;
cefval.SetBool(AValue);
ContextSetPreferenceIfCan(AContext, AName, cefval)
end;
end.