-
-
Notifications
You must be signed in to change notification settings - Fork 473
/
request_handler.pyx
455 lines (419 loc) · 17 KB
/
request_handler.pyx
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
# Copyright (c) 2013 CEF Python, see the Authors file.
# All rights reserved. Licensed under BSD 3-clause license.
# Project website: https://github.com/cztomczak/cefpython
include "../cefpython.pyx"
include "../browser.pyx"
# cef_termination_status_t
cimport cef_types
TS_ABNORMAL_TERMINATION = cef_types.TS_ABNORMAL_TERMINATION
TS_PROCESS_WAS_KILLED = cef_types.TS_PROCESS_WAS_KILLED
TS_PROCESS_CRASHED = cef_types.TS_PROCESS_CRASHED
# -----------------------------------------------------------------------------
# PyAuthCallback
# -----------------------------------------------------------------------------
cdef PyAuthCallback CreatePyAuthCallback(
CefRefPtr[CefAuthCallback] cefCallback):
cdef PyAuthCallback pyCallback = PyAuthCallback()
pyCallback.cefCallback = cefCallback
return pyCallback
cdef class PyAuthCallback:
cdef CefRefPtr[CefAuthCallback] cefCallback
cpdef py_void Continue(self, py_string username, py_string password):
self.cefCallback.get().Continue(
PyToCefStringValue(username),
PyToCefStringValue(password))
cpdef py_void Cancel(self):
self.cefCallback.get().Cancel()
# -----------------------------------------------------------------------------
# PyRequestCallback
# -----------------------------------------------------------------------------
cdef PyRequestCallback CreatePyRequestCallback(
CefRefPtr[CefRequestCallback] cefCallback):
cdef PyRequestCallback pyCallback = PyRequestCallback()
pyCallback.cefCallback = cefCallback
return pyCallback
cdef class PyRequestCallback:
cdef CefRefPtr[CefRequestCallback] cefCallback
cpdef py_void Continue(self, py_bool allow):
self.cefCallback.get().Continue(bool(allow))
cpdef py_void Cancel(self):
self.cefCallback.get().Cancel()
# -----------------------------------------------------------------------------
# RequestHandler
# -----------------------------------------------------------------------------
cdef public cpp_bool RequestHandler_OnBeforeBrowse(
CefRefPtr[CefBrowser] cefBrowser,
CefRefPtr[CefFrame] cefFrame,
CefRefPtr[CefRequest] cefRequest,
cpp_bool cefIsRedirect
) except * with gil:
cdef PyBrowser pyBrowser
cdef PyFrame pyFrame
cdef PyRequest pyRequest
cdef py_bool pyIsRedirect
cdef object clientCallback
cdef py_bool returnValue
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnBeforeBrowse")
pyFrame = GetPyFrame(cefFrame)
pyRequest = CreatePyRequest(cefRequest)
pyIsRedirect = bool(cefIsRedirect)
clientCallback = pyBrowser.GetClientCallback("OnBeforeBrowse")
if clientCallback:
returnValue = clientCallback(
browser=pyBrowser,
frame=pyFrame,
request=pyRequest,
is_redirect=pyIsRedirect)
return bool(returnValue)
else:
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RequestHandler_OnBeforeResourceLoad(
CefRefPtr[CefBrowser] cefBrowser,
CefRefPtr[CefFrame] cefFrame,
CefRefPtr[CefRequest] cefRequest
) except * with gil:
cdef PyBrowser pyBrowser
cdef PyFrame pyFrame
cdef PyRequest pyRequest
cdef object clientCallback
cdef py_bool returnValue
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnBeforeResourceLoad")
pyFrame = GetPyFrame(cefFrame)
pyRequest = CreatePyRequest(cefRequest)
clientCallback = pyBrowser.GetClientCallback("OnBeforeResourceLoad")
if clientCallback:
returnValue = clientCallback(
browser=pyBrowser,
frame=pyFrame,
request=pyRequest)
return bool(returnValue)
else:
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public CefRefPtr[CefResourceHandler] RequestHandler_GetResourceHandler(
CefRefPtr[CefBrowser] cefBrowser,
CefRefPtr[CefFrame] cefFrame,
CefRefPtr[CefRequest] cefRequest
) except * with gil:
cdef PyBrowser pyBrowser
cdef PyFrame pyFrame
cdef PyRequest pyRequest
cdef object clientCallback
cdef object returnValue
try:
pyBrowser = GetPyBrowser(cefBrowser, "GetResourceHandler")
pyFrame = GetPyFrame(cefFrame)
pyRequest = CreatePyRequest(cefRequest)
clientCallback = pyBrowser.GetClientCallback("GetResourceHandler")
if clientCallback:
returnValue = clientCallback(
browser=pyBrowser,
frame=pyFrame,
request=pyRequest)
if returnValue:
return CreateResourceHandler(returnValue)
else:
return <CefRefPtr[CefResourceHandler]>NULL
else:
return <CefRefPtr[CefResourceHandler]>NULL
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RequestHandler_OnResourceRedirect(
CefRefPtr[CefBrowser] cefBrowser,
CefRefPtr[CefFrame] cefFrame,
const CefString& cefOldUrl,
CefString& cefNewUrl,
CefRefPtr[CefRequest] cefRequest,
CefRefPtr[CefResponse] cefResponse
) except * with gil:
cdef PyBrowser pyBrowser
cdef PyFrame pyFrame
cdef str pyOldUrl
cdef list pyNewUrlOut
cdef PyRequest pyRequest
cdef PyResponse pyResponse
cdef object clientCallback
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnResourceRedirect")
pyFrame = GetPyFrame(cefFrame)
pyOldUrl = CefToPyString(cefOldUrl)
pyNewUrlOut = [CefToPyString(cefNewUrl)]
pyRequest = CreatePyRequest(cefRequest)
pyResponse = CreatePyResponse(cefResponse)
clientCallback = pyBrowser.GetClientCallback("OnResourceRedirect")
if clientCallback:
clientCallback(
browser=pyBrowser,
frame=pyFrame,
old_url=pyOldUrl,
new_url_out=pyNewUrlOut,
request=pyRequest,
response=pyResponse)
if pyNewUrlOut[0]:
PyToCefString(pyNewUrlOut[0], cefNewUrl)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RequestHandler_GetAuthCredentials(
CefRefPtr[CefBrowser] cefBrowser,
CefRefPtr[CefFrame] cefFrame,
cpp_bool cefIsProxy,
const CefString& cefHost,
int cefPort,
const CefString& cefRealm,
const CefString& cefScheme,
CefRefPtr[CefAuthCallback] cefAuthCallback
) except * with gil:
cdef PyBrowser pyBrowser
cdef PyFrame pyFrame
cdef py_bool pyIsProxy
cdef str pyHost
cdef int pyPort
cdef str pyRealm
cdef str pyScheme
cdef PyAuthCallback pyAuthCallback
cdef py_bool returnValue
cdef list pyUsernameOut
cdef list pyPasswordOut
cdef object clientCallback
try:
pyBrowser = GetPyBrowser(cefBrowser, "GetAuthCredentials")
pyFrame = GetPyFrame(cefFrame)
pyIsProxy = bool(cefIsProxy)
pyHost = CefToPyString(cefHost)
pyPort = int(cefPort)
pyRealm = CefToPyString(cefRealm)
pyScheme = CefToPyString(cefScheme)
pyAuthCallback = CreatePyAuthCallback(cefAuthCallback)
pyUsernameOut = [""]
pyPasswordOut = [""]
clientCallback = pyBrowser.GetClientCallback("GetAuthCredentials")
if clientCallback:
returnValue = clientCallback(
browser=pyBrowser,
frame=pyFrame,
is_proxy=pyIsProxy,
host=pyHost,
port=pyPort,
realm=pyRealm,
scheme=pyScheme,
callback=pyAuthCallback)
return bool(returnValue)
else:
# TODO: port it from CEF 1, copy the cef1/http_authentication/.
# --
# Default implementation for Windows.
# IF UNAME_SYSNAME == "Windows":
# returnValue = HttpAuthenticationDialog(
# pyBrowser,
# pyIsProxy, pyHost, pyPort, pyRealm, pyScheme,
# pyUsernameOut, pyPasswordOut)
# if returnValue:
# pyAuthCallback.Continue(pyUsernameOut[0], pyPasswordOut[0])
# return True
# return False
# ELSE:
# return False
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RequestHandler_OnQuotaRequest(
CefRefPtr[CefBrowser] cefBrowser,
const CefString& cefOriginUrl,
int64 newSize,
CefRefPtr[CefRequestCallback] cefRequestCallback
) except * with gil:
cdef PyBrowser pyBrowser
cdef py_string pyOriginUrl
cdef py_bool returnValue
cdef object clientCallback
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnQuotaRequest")
pyOriginUrl = CefToPyString(cefOriginUrl)
clientCallback = pyBrowser.GetClientCallback("OnQuotaRequest")
if clientCallback:
returnValue = clientCallback(
browser=pyBrowser,
origin_url=pyOriginUrl,
new_size=newSize,
callback=CreatePyRequestCallback(cefRequestCallback))
return bool(returnValue)
else:
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public CefRefPtr[CefCookieManager] RequestHandler_GetCookieManager(
CefRefPtr[CefBrowser] cefBrowser,
const CefString& cefMainUrl
) except * with gil:
# In CEF the GetCookieManager callback belongs to
# CefRequestContextHandler.
# In an exceptional case the browser parameter may be None
# due to limitation in CEF API. No workaround as of now.
cdef PyBrowser pyBrowser
cdef str pyMainUrl
cdef object clientCallback
cdef PyCookieManager returnValue
try:
pyBrowser = GetPyBrowser(cefBrowser, "GetCookieManager")
pyMainUrl = CefToPyString(cefMainUrl)
if pyBrowser:
# Browser may be empty.
clientCallback = pyBrowser.GetClientCallback("GetCookieManager")
if clientCallback:
returnValue = clientCallback(
browser=pyBrowser,
main_url=pyMainUrl)
if returnValue:
if isinstance(returnValue, PyCookieManager):
return returnValue.cefCookieManager
else:
raise Exception("Expected a CookieManager object")
return <CefRefPtr[CefCookieManager]>NULL
else:
return <CefRefPtr[CefCookieManager]>NULL
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RequestHandler_OnProtocolExecution(
CefRefPtr[CefBrowser] cefBrowser,
const CefString& cefUrl,
cpp_bool& cefAllowOSExecution
) except * with gil:
cdef PyBrowser pyBrowser
cdef str pyUrl
cdef list pyAllowOSExecutionOut
cdef object clientCallback
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnProtocolExecution")
pyUrl = CefToPyString(cefUrl)
pyAllowOSExecutionOut = [bool(cefAllowOSExecution)]
clientCallback = pyBrowser.GetClientCallback("OnProtocolExecution")
if clientCallback:
clientCallback(
browser=pyBrowser,
url=pyUrl,
allow_execution_out=pyAllowOSExecutionOut)
# Since Cython 0.17.4 assigning a value to an argument
# passed by reference will throw an error, the fix is to
# to use "(&arg)[0] =" instead of "arg =", see this topic:
# https://groups.google.com/forum/#!msg/cython-users/j58Sp3QMrD4/y9vJy9YBi_kJ
# For CefRefPtr you should use swap() method instead.
(&cefAllowOSExecution)[0] = 1
#(&cefAllowOSExecution)[0] = <cpp_bool>bool(pyAllowOSExecutionOut[0])
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RequestHandler_OnBeforePluginLoad(
CefRefPtr[CefBrowser] browser,
const CefString& mime_type,
const CefString& plugin_url,
cpp_bool is_main_frame,
const CefString& top_origin_url,
CefRefPtr[CefWebPluginInfo] plugin_info,
cef_types.cef_plugin_policy_t* plugin_policy
) except * with gil:
cdef PyBrowser pyBrowser
cdef PyWebPluginInfo pyInfo
cdef py_bool returnValue
cdef object clientCallback
try:
# OnBeforePluginLoad is called from RequestContexthandler.
# The Browser object might not be available, because it is
# being set synchronously during CreateBrowserSync, after
# Browser is created. From testing it always works, however
# better be safe.
if not browser.get():
Debug("WARNING: RequestHandler_OnBeforePluginLoad() failed,"
" Browser object is not available")
return False
py_browser = GetPyBrowser(browser, "OnBeforePluginLoad")
py_plugin_info = CreatePyWebPluginInfo(plugin_info)
clientCallback = GetGlobalClientCallback("OnBeforePluginLoad")
if clientCallback:
returnValue = clientCallback(
browser=py_browser,
mime_type=CefToPyString(mime_type),
plugin_url=CefToPyString(plugin_url),
is_main_frame=bool(is_main_frame),
top_origin_url=CefToPyString(top_origin_url),
plugin_info=py_plugin_info)
if returnValue:
plugin_policy[0] = cef_types.PLUGIN_POLICY_DISABLE
return bool(returnValue)
else:
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RequestHandler_OnCertificateError(
int certError,
const CefString& cefRequestUrl,
CefRefPtr[CefRequestCallback] cefCertCallback
) except * with gil:
cdef py_bool returnValue
cdef object clientCallback
try:
clientCallback = GetGlobalClientCallback("OnCertificateError")
if clientCallback:
returnValue = clientCallback(
cert_error=certError,
request_url=CefToPyString(cefRequestUrl),
callback=CreatePyRequestCallback(cefCertCallback))
return bool(returnValue)
else:
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RequestHandler_OnRendererProcessTerminated(
CefRefPtr[CefBrowser] cefBrowser,
cef_types.cef_termination_status_t cefStatus
) except * with gil:
# TODO: proccess may crash during browser creation. Let this callback
# to be set either through cefpython.SetGlobalClientCallback()
# or PyBrowser.SetClientCallback(). Modify the
# PyBrowser.GetClientCallback() implementation to return a global
# callback first if set.
cdef PyBrowser pyBrowser
cdef object clientCallback
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnRendererProcessTerminated")
clientCallback = pyBrowser.GetClientCallback(
"OnRendererProcessTerminated")
if clientCallback:
clientCallback(browser=pyBrowser, status=cefStatus)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RequestHandler_OnPluginCrashed(
CefRefPtr[CefBrowser] cefBrowser,
const CefString& cefPluginPath
) except * with gil:
# TODO: plugin may crash during browser creation. Let this callback
# to be set either through cefpython.SetGlobalClientCallback()
# or PyBrowser.SetClientCallback(). Modify the
# PyBrowser.GetClientCallback() implementation to return a global
# callback first if set.
cdef PyBrowser pyBrowser
cdef object clientCallback
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnPluginCrashed")
clientCallback = pyBrowser.GetClientCallback("OnPluginCrashed")
if clientCallback:
clientCallback(
browser=pyBrowser,
plugin_path=CefToPyString(cefPluginPath))
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)