forked from omegakode/PBWebview2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebView2_Helper.pb
482 lines (366 loc) · 14 KB
/
WebView2_Helper.pb
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
477
478
479
480
481
482
;WebView2_Helper.pb
EnableExplicit
;- VERSION
#WV2_VERSION = "0.2.0"
Prototype wv2_EventHandler_Invoke(this.i, sender.i, args.i)
Prototype wv2_EventHandler_Finalize(this.i)
;-
;- WV2_EVENT_HANDLER_CLASS
Structure WV2_EVENT_HANDLER_CLASS
*parent.WV2_EVENT_HANDLER_CLASS
finalize.wv2_EventHandler_Finalize
EndStructure
;- WV2_EVENT_HANDLER
Structure WV2_EVENT_HANDLER
vt.i
*class.WV2_EVENT_HANDLER_CLASS
refCount.l
mutex.i
context.i
invokeHandler.wv2_EventHandler_Invoke
EndStructure
;- WV2_EVENT_HANDLER_VTBL
Structure WV2_EVENT_HANDLER_VTBL Extends IUnknownVtbl
Invoke.i
GetContext.i
SetContext.i
LockMutex.i
UnlockMutex.i
EndStructure
;- IWV2EventHandler
Interface IWV2EventHandler Extends IUnknown
Invoke(sender.i, args.i)
GetContext()
SetContext(context.i)
LockMutex()
UnlockMutex()
EndInterface
;-
;- WV2_EVENT_HANDLER_WAITABLE_CLASS
Structure WV2_EVENT_HANDLER_WAITABLE_CLASS Extends WV2_EVENT_HANDLER_CLASS
EndStructure
;- WV2_EVENT_HANDLER_WAITABLE
Structure WV2_EVENT_HANDLER_WAITABLE Extends WV2_EVENT_HANDLER
_event.i ;api event
args.i
EndStructure
;- WV2_EVENT_HANDLER_WAITABLE_VTBL
Structure WV2_EVENT_HANDLER_WAITABLE_VTBL Extends WV2_EVENT_HANDLER_VTBL
WaitForCompletion.i
StopWaiting.i
GetArgs.i
SetArgs.i
EndStructure
;- IWV2EventHandlerWaitable
Interface IWV2EventHandlerWaitable Extends IWV2EventHandler
WaitForCompletion(timeout.l = #INFINITE)
StopWaiting()
GetArgs()
SetArgs(args.i)
EndInterface
;-
;- DECLARES
Declare.s wv2_GetBrowserVersion(browserExecutableFolder.s)
Declare wv2_Controller_put_Bounds(wvc.ICoreWebView2Controller, *bounds.RECT)
Declare wv2_EventHandler_New(invokeHandler.wv2_EventHandler_Invoke, context.i)
Declare wv2_EventHandler_Finalize(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandler_Free(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandler_QueryInterface(*this.WV2_EVENT_HANDLER, *id.IID, *obj.INTEGER)
Declare wv2_EventHandler_AddRef(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandler_Release(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandler_Invoke(*this.WV2_EVENT_HANDLER, sender.i, args.i)
Declare wv2_EventHandler_GetContext(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandler_SetContext(*this.WV2_EVENT_HANDLER, context.i)
Declare wv2_EventHandler_LockMutex(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandler_UnlockMutex(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandler_LockMutex(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandler_UnlockMutex(*this.WV2_EVENT_HANDLER)
Declare wv2_EventHandlerWaitable_Finalize(*this.WV2_EVENT_HANDLER_WAITABLE)
Declare wv2_EventHandlerWaitable_WaitForCompletion(*this.WV2_EVENT_HANDLER_WAITABLE, timeout.l = #INFINITE)
Declare wv2_EventHandlerWaitable_StopWaiting(*this.WV2_EVENT_HANDLER_WAITABLE)
Declare wv2_EventHandlerWaitable_GetArgs(*this.WV2_EVENT_HANDLER_WAITABLE)
Declare wv2_EventHandlerWaitable_SetArgs(*this.WV2_EVENT_HANDLER_WAITABLE, args.i)
Declare wv2_CreateCoreWebView2EnvironmentWithOptionsSync(browserExecutableFolder.s, userDataFolder.s, environmentOptions.i)
Declare wv2_Environment_Created_Sync(*this.WV2_EVENT_HANDLER, result.l, environment.ICoreWebView2Environment)
Declare wv2_Environment_CreateCoreWebView2ControllerSync(environment.ICoreWebView2Environment, parentWindow.i)
Declare wv2_Controller_Created_Sync(*this.WV2_EVENT_HANDLER, result.l, controller.ICoreWebView2Controller)
Declare wv2_Core_ScriptExecuted_Sync(*this.WV2_EVENT_HANDLER, errorCode.l, resultObjectAsJson.i)
Declare.s wv2_Core_ExecuteScriptSync(wvCore.ICoreWebView2, script.s, *errorCode.LONG = #Null)
Declare.s wv2_CreateJSHostObjectProxy(hostObjName.s)
;-
Procedure wv2_EventHandler_Class()
Static.WV2_EVENT_HANDLER_CLASS *class
If *class = #Null
*class = AllocateMemory(SizeOf(WV2_EVENT_HANDLER_CLASS))
*class\parent = #Null
*class\finalize = @wv2_EventHandler_Finalize()
EndIf
ProcedureReturn *class
EndProcedure
Procedure wv2_EventHandler_VTable()
Static.WV2_EVENT_HANDLER_VTBL *vt
If *vt = #Null
*vt = AllocateMemory(SizeOf(WV2_EVENT_HANDLER_VTBL))
*vt\QueryInterface = @wv2_EventHandler_QueryInterface()
*vt\AddRef = @wv2_EventHandler_AddRef()
*vt\Release = @wv2_EventHandler_Release()
*vt\Invoke = @wv2_EventHandler_Invoke()
*vt\GetContext = @wv2_EventHandler_GetContext()
*vt\SetContext = @wv2_EventHandler_SetContext()
*vt\LockMutex = @wv2_EventHandler_LockMutex()
*vt\UnlockMutex = @wv2_EventHandler_UnlockMutex()
EndIf
ProcedureReturn *vt
EndProcedure
Procedure wv2_EventHandler_Init(*this.WV2_EVENT_HANDLER, invokeHandler.wv2_EventHandler_Invoke, context.i)
*this\invokeHandler = invokeHandler
*this\context = context
*this\mutex = CreateMutex()
*this\refCount = 1
EndProcedure
Procedure wv2_EventHandler_Finalize(*this.WV2_EVENT_HANDLER)
FreeMutex(*this\mutex)
FreeMemory(*this)
EndProcedure
Procedure wv2_EventHandler_New(invokeHandler.wv2_EventHandler_Invoke, context.i)
Protected.WV2_EVENT_HANDLER *this
*this = AllocateMemory(SizeOf(WV2_EVENT_HANDLER))
*this\vt = wv2_EventHandler_VTable()
*this\class = wv2_EventHandler_Class()
wv2_EventHandler_Init(*this, invokeHandler, context)
ProcedureReturn *this
EndProcedure
Procedure wv2_EventHandler_QueryInterface(*this.WV2_EVENT_HANDLER, *iid.IID, *obj.INTEGER)
If CompareMemory(*iid, ?IID_IUnknown, SizeOf(IID))
*obj\i = *this
wv2_EventHandler_AddRef(*this)
ProcedureReturn #S_OK
Else
*obj\i = #Null
ProcedureReturn #E_NOINTERFACE
EndIf
EndProcedure
Procedure wv2_EventHandler_AddRef(*this.WV2_EVENT_HANDLER)
*this\refCount = *this\refCount + 1
EndProcedure
Procedure wv2_EventHandler_Release(*this.WV2_EVENT_HANDLER)
*this\refCount = *this\refCount - 1
If *this\refCount = 0
*this\class\finalize(*this)
EndIf
ProcedureReturn *this\refCount
EndProcedure
Procedure wv2_EventHandler_Invoke(*this.WV2_EVENT_HANDLER, sender.i, args.i)
If *this\invokeHandler : *this\invokeHandler(*this, sender, args) : EndIf
EndProcedure
Procedure wv2_EventHandler_GetContext(*this.WV2_EVENT_HANDLER)
ProcedureReturn *this\context
EndProcedure
Procedure wv2_EventHandler_SetContext(*this.WV2_EVENT_HANDLER, context.i)
*this\context = context
EndProcedure
Procedure wv2_EventHandler_LockMutex(*this.WV2_EVENT_HANDLER)
LockMutex(*this\mutex)
EndProcedure
Procedure wv2_EventHandler_UnlockMutex(*this.WV2_EVENT_HANDLER)
UnlockMutex(*this\mutex)
EndProcedure
;-
Procedure wv2_EventHandlerWaitable_Class()
Static.WV2_EVENT_HANDLER_WAITABLE_CLASS *class
If *class = #Null
*class = AllocateMemory(SizeOf(WV2_EVENT_HANDLER_WAITABLE_CLASS))
*class\parent = wv2_EventHandler_Class()
*class\finalize = @wv2_EventHandlerWaitable_Finalize()
EndIf
ProcedureReturn *class
EndProcedure
Procedure wv2_EventHandlerWaitable_VTable()
Static.WV2_EVENT_HANDLER_WAITABLE_VTBL *vt
If *vt = #Null
*vt = AllocateMemory(SizeOf(WV2_EVENT_HANDLER_WAITABLE_VTBL))
CopyMemory(wv2_EventHandler_VTable(), *vt, SizeOf(WV2_EVENT_HANDLER_VTBL))
*vt\WaitForCompletion = @wv2_EventHandlerWaitable_WaitForCompletion()
*vt\StopWaiting = @wv2_EventHandlerWaitable_StopWaiting()
*vt\GetArgs = @wv2_EventHandlerWaitable_GetArgs()
*vt\SetArgs = @wv2_EventHandlerWaitable_SetArgs()
EndIf
ProcedureReturn *vt
EndProcedure
Procedure wv2_EventHandlerWaitable_Init(*this.WV2_EVENT_HANDLER_WAITABLE, invokeHandler.wv2_EventHandler_Invoke, context.i)
wv2_EventHandler_Init(*this, invokeHandler, context)
*this\_event = CreateEvent_(#Null, #False, #False, #Null)
EndProcedure
Procedure wv2_EventHandlerWaitable_Finalize(*this.WV2_EVENT_HANDLER_WAITABLE)
CloseHandle_(*this\_event)
If *this\class\parent
*this\class\parent\finalize(*this)
EndIf
EndProcedure
Procedure wv2_EventHandlerWaitable_New(invokeHandler.wv2_EventHandler_Invoke, context.i)
Protected.WV2_EVENT_HANDLER_WAITABLE *this
*this = AllocateMemory(SizeOf(WV2_EVENT_HANDLER_WAITABLE))
*this\vt = wv2_EventHandlerWaitable_VTable()
*this\class = wv2_EventHandlerWaitable_Class()
wv2_EventHandlerWaitable_Init(*this, invokeHandler, context)
ProcedureReturn *this
EndProcedure
Procedure wv2_EventHandlerWaitable_WaitForCompletion(*this.WV2_EVENT_HANDLER_WAITABLE, timeout.l = #INFINITE)
Protected.i arrHand
Protected.l idx
PokeI(@arrHand, *this\_event)
ole32::CoWaitForMultipleHandles(#COWAIT_DISPATCH_WINDOW_MESSAGES | #COWAIT_DISPATCH_CALLS | #COWAIT_INPUTAVAILABLE, timeout, 1, @arrHand, @idx)
EndProcedure
Procedure wv2_EventHandlerWaitable_StopWaiting(*this.WV2_EVENT_HANDLER_WAITABLE)
SetEvent_(*this\_event)
EndProcedure
Procedure wv2_EventHandlerWaitable_GetArgs(*this.WV2_EVENT_HANDLER_WAITABLE)
ProcedureReturn *this\args
EndProcedure
Procedure wv2_EventHandlerWaitable_SetArgs(*this.WV2_EVENT_HANDLER_WAITABLE, args.i)
*this\args = args
EndProcedure
;-
Procedure wv2_DeleteUserDataFolder(folder.s, processId.l, timeout.l)
Protected.i processHandle
Protected.l waitResult, elapsed, delResult
processHandle = OpenProcess_(#SYNCHRONIZE , #True, processId)
If processHandle
waitResult = WaitForSingleObject_(processHandle, timeout)
CloseHandle_(processHandle)
If waitResult = #WAIT_OBJECT_0
elapsed = 0
delResult = #False
While (delResult = #False And elapsed <= timeout)
delResult = DeleteDirectory(folder, "", #PB_FileSystem_Recursive | #PB_FileSystem_Force)
Delay(1)
elapsed + 1
Wend
ProcedureReturn delResult
Else
ProcedureReturn #False
EndIf
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure.s wv2_CreateJSHostObjectProxy(hostObjName.s)
ProcedureReturn "var " + hostObjName + " = new Proxy({}, {get: (target, prop) => window.chrome.webview.hostObjects.sync." + hostObjName + "[prop]});"
EndProcedure
Procedure.s wv2_GetBrowserVersion(browserExecutableFolder.s)
Protected.i vi
Protected.s vs
If GetAvailableCoreWebView2BrowserVersionString(browserExecutableFolder, @vi) = #S_OK
If vi
vs = PeekS(vi)
CoTaskMemFree_(vi)
EndIf
EndIf
ProcedureReturn vs
EndProcedure
Procedure.s wv2_GetRuntimeVersionFromRegistry()
Protected.i buf, hkey
Protected.s subkey, rtVersion
Protected.l bufLen, type
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
subkey = "SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"
CompilerElse
subkey = "SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"
CompilerEndIf
If RegOpenKey_(#HKEY_LOCAL_MACHINE, subkey, @hkey) = 0
;Get len
If RegQueryValueEx_(hkey, "pv", #Null, @type, #Null, @bufLen) = 0
If bufLen > 0 And type = #REG_SZ
bufLen = bufLen + SizeOf(CHARACTER)
buf = AllocateMemory(buflen)
;Get data
If RegQueryValueEx_(hkey, "pv", #Null, #Null, buf, @bufLen) = 0
rtVersion = PeekS(buf)
EndIf
FreeMemory(buf)
EndIf
EndIf
RegCloseKey_(hkey)
EndIf
ProcedureReturn rtVersion
EndProcedure
Procedure wv2_Controller_put_Bounds(wvc.ICoreWebView2Controller, *bounds.RECT)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
wvc\put_Bounds(*bounds)
CompilerElse
wvc\put_Bounds(*bounds\left, *bounds\top, *bounds\right, *bounds\bottom)
CompilerEndIf
EndProcedure
Procedure wv2_Controller_On_WM_MOVE_MOVING(wvc.ICoreWebView2Controller)
If wvc
wvc\NotifyParentWindowPositionChanged()
EndIf
EndProcedure
Procedure wv2_Environment_Created_Sync(this.IWV2EventHandlerWaitable, result.l, environment.ICoreWebView2Environment)
Protected.ICoreWebView2Environment env
If result = #S_OK
environment\QueryInterface(?IID_ICoreWebView2Environment, @env)
this\SetArgs(env)
EndIf
this\StopWaiting()
EndProcedure
Procedure wv2_CreateCoreWebView2EnvironmentWithOptionsSync(browserExecutableFolder.s, userDataFolder.s, environmentOptions.i)
Protected.IWV2EventHandlerWaitable evEnvCreated
Protected.ICoreWebView2Environment environment
evEnvCreated = wv2_EventHandlerWaitable_New(@wv2_Environment_Created_Sync(), 0)
CreateCoreWebView2EnvironmentWithOptions(browserExecutableFolder, userDataFolder, environmentOptions, evEnvCreated)
evEnvCreated\WaitForCompletion()
environment = evEnvCreated\GetArgs()
evEnvCreated\Release()
ProcedureReturn environment
EndProcedure
Procedure wv2_Controller_Created_Sync(this.IWV2EventHandlerWaitable, result.l, controller.ICoreWebView2Controller)
Protected.ICoreWebView2Controller cont
If result = #S_OK
controller\QueryInterface(?IID_ICoreWebView2Controller, @cont)
this\SetArgs(cont)
EndIf
this\StopWaiting()
EndProcedure
Procedure wv2_Environment_CreateCoreWebView2ControllerSync(environment.ICoreWebView2Environment, parentWindow.i)
Protected.IWV2EventHandlerWaitable evControllerCreated
Protected.ICoreWebView2Controller controller
evControllerCreated = wv2_EventHandlerWaitable_New(@wv2_Controller_Created_Sync(), 0)
environment\CreateCoreWebView2Controller(parentWindow, evControllerCreated)
evControllerCreated\WaitForCompletion()
controller = evControllerCreated\GetArgs()
evControllerCreated\Release()
ProcedureReturn controller
EndProcedure
Procedure wv2_Core_ScriptExecuted_Sync(this.IWV2EventHandlerWaitable, errorCode.l, resultObjectAsJson.i)
Protected.VECTOR_INT *args
this\LockMutex()
*args = AllocateMemory(SizeOf(Integer) * 2)
*args\item[0] = errorCode
*args\item[1] = AllocateMemory((lstrlen_(resultObjectAsJson) + 1) * SizeOf(Character))
lstrcpy_(*args\item[1], resultObjectAsJson)
this\SetArgs(*args)
this\StopWaiting()
this\UnlockMutex()
EndProcedure
;Executes a script synchronously, returns the script result as a json string.
;script : script to execute.
;*errorCode : address of a variable that will receive the script error code.
Procedure.s wv2_Core_ExecuteScriptSync(wvCore.ICoreWebView2, script.s, *errorCode.LONG = #Null)
Protected.IWV2EventHandlerWaitable evScriptExecuted
Protected.s ret
Protected.VECTOR_INT *args
If *errorCode : *errorCode\l = 0 : EndIf
evScriptExecuted = wv2_EventHandlerWaitable_New(@wv2_Core_ScriptExecuted_Sync(), 0)
wvCore\ExecuteScript(script, evScriptExecuted)
evScriptExecuted\WaitForCompletion()
*args = evScriptExecuted\GetArgs()
If *args
If *errorCode : *errorCode\l = *args\item[0] : EndIf
ret = PeekS(*args\item[1], -1, #PB_Unicode)
FreeMemory(*args\item[1])
FreeMemory(*args)
EndIf
evScriptExecuted\Release()
ProcedureReturn ret
EndProcedure