forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_request_cef1.pyx
More file actions
209 lines (188 loc) · 7.98 KB
/
web_request_cef1.pyx
File metadata and controls
209 lines (188 loc) · 7.98 KB
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
# Copyright (c) 2012-2014 The CEF Python authors. All rights reserved.
# License: New BSD License.
# Website: http://code.google.com/p/cefpython/
# TODO: fix CefWebURLRequest memory corruption and restore weakrefs
# for PyWebRequest object. Right now getting memory corruption
# when CefRefPtr[CefWebURLRequest] is released after the request
# is completed. The memory corruption manifests itself with the
# "Segmentation Fault" error message or the strange "C function
# name could not be determined in the current C stack frame".
# See this topic on cython-users group:
# https://groups.google.com/d/topic/cython-users/FJZwHhqaCSI/discussion
# After CefWebURLRequest memory corruption is fixed restore weakrefs:
# 1. cdef object g_pyWebRequests = weakref.WeakValueDictionary()
# 2. Add property "cdef object __weakref__" in PyWebRequest
# When using normal dictionary for g_pyWebRequest then the memory
# corruption doesn't occur, but the PyWebRequest and CefWebURLRequest
# objects are never released, thus you have memory leaks, for now
# there is no other solution. See this topic on the CEF Forum:
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10710
cdef object g_pyWebRequests = {}
cdef int g_webRequestMaxId = 0
# ------------------------------------------------------------------------------
# WebRequest
# ------------------------------------------------------------------------------
# Static methods are not allowed in cdef classes,
# that's why we need a wrapper for PyWebRequest.
class WebRequest:
State = {
"Unsent": cef_types.WUR_STATE_UNSENT,
"Started": cef_types.WUR_STATE_STARTED,
"HeadersReceived": cef_types.WUR_STATE_HEADERS_RECEIVED,
"Loading": cef_types.WUR_STATE_LOADING,
"Done": cef_types.WUR_STATE_DONE,
"Error": cef_types.WUR_STATE_ERROR,
"Abort": cef_types.WUR_STATE_ABORT,
}
def __init__(self):
raise Exception("You cannot instantiate WebRequest directly, "
"use WebRequest.CreateWebRequest() static method")
@staticmethod
def CreateWebRequest(request, webRequestClient):
if not isinstance(request, PyRequest):
raise Exception("Invalid request object")
return CreatePyWebRequest(request, webRequestClient)
# ------------------------------------------------------------------------------
# PyWebRequest
# ------------------------------------------------------------------------------
cdef PyWebRequest CreatePyWebRequest(PyRequest request,
object webRequestClient):
global g_pyWebRequests
cdef PyWebRequest webRequest = PyWebRequest(request, webRequestClient)
assert webRequest.webRequestId, "webRequest.webRequestId empty"
g_pyWebRequests[webRequest.webRequestId] = webRequest
return webRequest
cdef PyWebRequest GetPyWebRequest(int webRequestId):
global g_pyWebRequests
if webRequestId in g_pyWebRequests:
return g_pyWebRequests[webRequestId]
return None
cdef class PyWebRequest:
# cdef object __weakref__ # see g_pyWebRequests
cdef int webRequestId
cdef CefRefPtr[CefWebURLRequest] requester
cdef object pyWebRequestClient
def __init__(self, PyRequest pyRequest, object pyWebRequestClient):
global g_webRequestMaxId
g_webRequestMaxId += 1
self.webRequestId = g_webRequestMaxId
cdef CefRefPtr[WebRequestClient] cppWebRequestClient = (
<CefRefPtr[WebRequestClient]?>new WebRequestClient(
self.webRequestId))
self.pyWebRequestClient = pyWebRequestClient
self.requester = <CefRefPtr[CefWebURLRequest]?>(
cef_web_urlrequest_static.CreateWebURLRequest(
pyRequest.cefRequest,
<CefRefPtr[CefWebURLRequestClient]?>(
cppWebRequestClient)))
cdef object GetCallback(self, str funcName):
if hasattr(self.pyWebRequestClient, funcName) and (
callable(getattr(self.pyWebRequestClient, funcName))):
return getattr(self.pyWebRequestClient, funcName)
cpdef py_void Cancel(self):
self.requester.get().Cancel()
cpdef int GetState(self) except *:
return <int>self.requester.get().GetState()
# ------------------------------------------------------------------------------
# WebRequestClient
# ------------------------------------------------------------------------------
cdef public void WebRequestClient_OnStateChange(
int webRequestId,
CefRefPtr[CefWebURLRequest] requester,
cef_types.cef_weburlrequest_state_t state
) except * with gil:
cdef PyWebRequest webRequest
cdef object callback
try:
webRequest = GetPyWebRequest(webRequestId)
if webRequest:
callback = webRequest.GetCallback("OnStateChange")
if callback:
callback(webRequest, <int>state)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void WebRequestClient_OnRedirect(
int webRequestId,
CefRefPtr[CefWebURLRequest] requester,
CefRefPtr[CefRequest] request,
CefRefPtr[CefResponse] response
) except * with gil:
cdef PyWebRequest webRequest
cdef object callback
try:
webRequest = GetPyWebRequest(webRequestId)
if webRequest:
callback = webRequest.GetCallback("OnRedirect")
if callback:
callback(webRequest, webRequest.pyRequest,
CreatePyResponse(response))
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void WebRequestClient_OnHeadersReceived(
int webRequestId,
CefRefPtr[CefWebURLRequest] requester,
CefRefPtr[CefResponse] response
) except * with gil:
cdef PyWebRequest webRequest
cdef object callback
try:
webRequest = GetPyWebRequest(webRequestId)
if webRequest:
callback = webRequest.GetCallback("OnHeadersReceived")
if callback:
callback(webRequest, CreatePyResponse(response))
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void WebRequestClient_OnProgress(
int webRequestId,
CefRefPtr[CefWebURLRequest] requester,
uint64_t bytesSent,
uint64_t totalBytesToBeSent
) except * with gil:
cdef PyWebRequest webRequest
cdef object callback
try:
webRequest = GetPyWebRequest(webRequestId)
if webRequest:
callback = webRequest.GetCallback("OnProgress")
if callback:
callback(webRequest, bytesSent, totalBytesToBeSent)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void WebRequestClient_OnData(
int webRequestId,
CefRefPtr[CefWebURLRequest] requester,
void* data,
int dataLength
) except * with gil:
cdef PyWebRequest webRequest
cdef object callback
try:
webRequest = GetPyWebRequest(webRequestId)
if webRequest:
callback = webRequest.GetCallback("OnData")
if callback:
callback(webRequest, VoidPtrToString(data, dataLength))
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void WebRequestClient_OnError(
int webRequestId,
CefRefPtr[CefWebURLRequest] requester,
int errorCode
) except * with gil:
cdef PyWebRequest webRequest
cdef object callback
try:
webRequest = GetPyWebRequest(webRequestId)
if webRequest:
callback = webRequest.GetCallback("OnError")
if callback:
callback(webRequest, errorCode)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)