-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathguiChromium.pas
252 lines (211 loc) · 5.7 KB
/
guiChromium.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
unit guiChromium;
{$ifdef fpc}
{$mode delphi}{$H+}
{$endif}
interface
uses
Classes, SysUtils, phpUtils, Forms, regGUI, Controls,
zendTypes,
ZENDAPI,
phpTypes,
PHPAPI,
php4delphi,
uPhpEvents,
uPHPMod,
Graphics, Dialogs, dwsHashtables,
ceflib, cefvcl;
procedure InitializeGuiChromium(PHPEngine: TPHPEngine);
procedure chromium_allowedcall(ht: integer; return_value: pzval;
return_value_ptr: pzval; this_ptr: pzval; return_value_used: integer;
TSRMLS_DC: pointer); cdecl;
procedure chromium_settings(ht: integer; return_value: pzval;
return_value_ptr: pzval; this_ptr: pzval; return_value_used: integer;
TSRMLS_DC: pointer); cdecl;
procedure chromium_load(ht: integer; return_value: pzval; return_value_ptr: pzval;
this_ptr: pzval; return_value_used: integer; TSRMLS_DC: pointer); cdecl;
type
TExtension = class(TCefv8HandlerOwn)
private
protected
function Execute(const Name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var Exception: ustring): boolean; override;
end;
implementation
var
AllowedCall: TStringHashTable;
procedure chromium_settings;
var
p: pzval_array;
id: integer;
s: ansistring;
begin
if ht < 10 then
begin
zend_wrong_param_count(TSRMLS_DC);
Exit;
end;
zend_get_parameters_my(ht, p, TSRMLS_DC);
CefCache := Z_STRVAL(p[0]^);
CefUserAgent := Z_STRVAL(p[1]^);
CefProductVersion := Z_STRVAL(p[2]^);
CefLocale := Z_STRVAL(p[3]^);
CefLogFile := Z_STRVAL(p[4]^);
CefExtraPluginPaths := Z_STRVAL(p[5]^);
CefLocalStorageQuota := Z_LVAL(p[6]^);
CefSessionStorageQuota := Z_LVAL(p[7]^);
CefJavaScriptFlags := Z_STRVAL(p[8]^);
CefAutoDetectProxySettings := Z_BVAL(p[9]^);
dispose_pzval_array(p);
end;
procedure chromium_allowedcall;
var
p: pzval_array;
i: integer;
s: ansistring;
tmp: ^ppzval;
arr: PHashTable;
begin
if ht < 1 then
begin
zend_wrong_param_count(TSRMLS_DC);
Exit;
end;
zend_get_parameters_my(ht, p, TSRMLS_DC);
AllowedCall.Clear;
if p[0]^^._type = IS_ARRAY then
arr := p[0]^^.Value.ht
else
arr := nil;
if arr <> nil then
begin
New(tmp);
for i := zend_hash_num_elements(arr) - 1 downto 0 do
begin
zend_hash_index_find(arr, i, tmp);
AllowedCall.SetValue(LowerCase(Z_STRVAL(tmp^^)), '');
end;
Dispose(tmp);
end;
dispose_pzval_array(p);
end;
procedure chromium_load;
begin
CefLoadLibDefault;
end;
procedure V8_ZVAL(Value: ICefv8Value; arg: pzval);
var
S: ansistring;
begin
if Value.IsUndefined or Value.IsNull then
ZVAL_NULL(arg)
else if Value.IsBool then
ZVAL_BOOL(arg, Value.GetBoolValue)
else if Value.IsInt then
ZVAL_LONG(arg, Value.GetIntValue)
else if Value.IsDouble then
ZVAL_DOUBLE(arg, Value.GetDoubleValue)
else if Value.IsDate then
ZVAL_DOUBLE(arg, Value.GetDateValue)
else if Value.IsString then
begin
S := Value.GetStringValue;
if S = '' then
ZVAL_EMPTY_STRING(arg)
else
ZVAL_STRINGL(arg, PAnsiChar(S), Length(S), True);
end
else
ZVAL_NULL(arg);
end;
function ZVAL_V8(arg: pzval): ICefv8Value;
begin
case arg._type of
IS_LONG: Result := TCefv8ValueRef.CreateInt(arg.Value.lval);
IS_DOUBLE: Result := TCefv8ValueRef.CreateDouble(arg.Value.dval);
IS_BOOL: Result := TCefv8ValueRef.CreateBool(boolean(arg.Value.lval));
IS_STRING: Result := TCefv8ValueRef.CreateString(Z_STRVAL(arg));
else
Result := TCefv8ValueRef.CreateNull;
end;
end;
function TExtension.Execute(const Name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var Exception: ustring): boolean;
var
S: ansistring;
Args: pzval_array_ex;
Return, Func: pzval;
i: integer;
begin
{if name = 'eval' then
begin
if length(arguments) > 0 then
begin
phpMOD.RunCode( arguments[0].GetStringValue );
end;
end else}
if Name = 'call' then
begin
if length(arguments) > 0 then
begin
S := arguments[0].GetStringValue;
if (AllowedCall.FSize = 0) or (not AllowedCall.HasKey(LowerCase(S))) then
begin
Result := False;
exit;
end;
Func := MAKE_STD_ZVAL;
Return := MAKE_STD_ZVAL;
ZVAL_STRING(Func, PAnsiChar(S), True);
SetLength(args, length(arguments) - 1);
for i := 0 to high(args) do
begin
args[i] := MAKE_STD_ZVAL;
V8_ZVAL(arguments[i + 1], args[i]);
end;
call_user_function(
GetExecutorGlobals.function_table,
nil,
Func,
Return,
Length(Args),
Args,
phpMOD.psvPHP.TSRMLS_D
);
for i := 0 to high(args) do
begin
_zval_dtor(args[0], nil, 0);
end;
retval := ZVAL_V8(Return);
Result := True;
_zval_dtor(Func, nil, 0);
_zval_dtor(Return, nil, 0);
end;
end;
end;
const
code =
'var PHP;' + 'if (!PHP)' + ' PHP = {};' + '(function() {' +
(* ' PHP.eval = function(code) {'+
' native function eval(code);'+
' return eval(code);'+
' };'+ *)
' PHP.call = function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15) {' +
' native function call(arguments);' + ' return call.apply(null, arguments);' +
' };' + '})();';
// after load chromium callback
procedure callback_CefLoadLib;
begin
CefRegisterExtension('v8/PHP', code, TExtension.Create as ICefV8Handler);
end;
procedure InitializeGuiChromium(PHPEngine: TPHPEngine);
begin
PHPEngine.AddFunction('chromium_settings', @chromium_settings);
PHPEngine.AddFunction('chromium_load', @chromium_load);
PHPEngine.AddFunction('chromium_allowedcall', @chromium_allowedcall);
CefLoadLibAfter := callback_CefLoadLib;
end;
initialization
AllowedCall := TStringHashTable.Create(256);
end.