-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcefapp.py
268 lines (230 loc) · 9.73 KB
/
cefapp.py
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
#!/usr/bin/env vpython3
import os
import sys
import gc
top = os.path.join(os.getcwd(), "bin")
# os.chdir(top)
cefdataroot = os.path.join(os.getcwd(), "bin-cefdata")
for fname in (
cefdataroot,
os.path.join(cefdataroot, "root"),
os.path.join(cefdataroot, "root", "cache"),
os.path.join(cefdataroot, "user-data"),
):
if not os.path.isdir(fname):
os.mkdir(fname)
import ctypes as ct
from cefct import libcefdef
libcefdef.LoadLibrary(os.path.join(top, "libcef" + libcefdef.dllext))
from cefct import libcef as cef
browser = None
c_byte_p = ct.POINTER(ct.c_byte)
def CefMainArgs(argv):
argb = [ct.create_string_buffer(s.encode("ascii")) for s in argv[1:]]
ubp = ct.POINTER(ct.c_ubyte)
cargv = (ubp * (len(argb) + 1))()
for i in range(len(argb)):
v = argb[i]
cargv[i] = ct.cast(v, ubp)
cargv[len(argb)] = None
data = cef.cef_main_args_t()
data.argc = len(argb)
data.argv = ct.cast(cargv, ct.POINTER(ubp))
return data, (argb, cargv)
class App(cef.cef_app_t):
def __init__(self, switches=None):
cef.cef_app_t.__init__(self)
self.bph = cef.cef_browser_process_handler_t()
self.switches = switches
def py_on_before_command_line_processing(self, this, processType, commandLine):
print("App.OnBeforeCommandLineProcessing")
#v = processType
#print('\tprocessType==',v, bool(v))
#v = processType.contents
#if v is not None:
# v = v.ToString()
#print("processType=", v)
def showcl():
cl = commandLine.contents
#print('\tcl.valid=', cl.is_valid(cl))
if cl.is_valid(cl):
s = cl.get_command_line_string(cl)
s = ct.cast(s, ct.POINTER(cef.cef_string_userfree_t))
v = s.contents.ToString(True)
print("\tcommandLine=", v)
s.contents.Free()
if self.switches:
cl = commandLine.contents
def cladd(s1, s2=None):
#print('cladd', s1, '*', s2)
s1 = cef.cef_string_t(s1)
if s2 is None:
cl.append_switch(cl, s1)
else:
s2 = cef.cef_string_t(s2)
cl.append_switch_with_value(cl, s1, s2)
for sw in self.switches:
if type(sw) == str:
cladd(sw)
else:
cladd(sw[0], sw[1])
#cladd("enable-media-stream")
#cladd("autoplay-policy", "no-user-gesture-required")
#cladd("enable-media-stream")
#cladd("disable-dev-shm-usage") # https://github.com/GoogleChrome/puppeteer/issues/1834
#cladd("enable-begin-frame-scheduling") # https://bitbucket.org/chromiumembedded/cef/issues/1368
# Optimize for no gpu usage
#cladd("disable-gpu")
#cladd("disable-gpu-compositing")
#cladd("remote-debugging-port", "9921")
showcl()
return None
# def OnRegisterCustomSchemes(self, this, registrar):
def py_on_register_custom_schemes(self, this, registar):
print("App.OnRegisterCustomSchemes", registar)
return None
# def GetResourceBundleHandler(self, this):
def py_get_resource_bundle_handler(self, this):
return None
print("App.GetResourceBundleHandler")
# def GetBrowserProcessHandler(self, this):
def py_get_browser_process_handler(self, this):
print("App.GetBrowserProcessHandler")
v = ct.addressof(self.bph)
return v
# def GetRenderProcessHandler(self, this):
def py_get_render_process_handler(self, this):
print("App.GetRenderProcessHandler")
return None
class AppSetup:
def __init__(self, app, *args):
print("*" * 20, "AppSetup")
if libcefdef.win:
mainArgs = cef.cef_main_args_t()
mainArgs.instance = ct.windll.kernel32.GetModuleHandleA(None)
mainArgsB = None
else:
mainArgs, mainArgsB = CefMainArgs(args)
# command_line = libcef.command_line_create()
# command_line.contents._init_from_string(command_line, cef_string_t("--show-fps-counter"))
settings = cef.cef_settings_t()
settings.size = ct.sizeof(cef.cef_settings_t)
# settings.no_sandbox = 1
capp = os.path.normpath(os.path.join(top, "cefclient"))
if not os.path.exists(capp):
capp = os.path.normpath(os.path.join(top, "cefsimple"))
if not os.path.exists(capp):
capp = os.path.normpath(os.path.join(top, "pyhelper"))
if not os.path.exists(capp):
print('cefapp not found')
sys.exit(1)
print(f'using cefapp: {capp}')
settings.browser_subprocess_path = cef.cef_string_t(capp)
# settings.framework_dir_path =
settings.chrome_runtime = 0
# settings.multi_threaded_message_loop = 1
# settings.external_message_pump = 1
# settings.windowless_rendering_enabled = 1
# command_line_args_disabled
# cache_path
#settings.resources_dir_path = cef.cef_string_t(os.path.normpath(os.path.join(top, 'Resources')))
settings.locales_dir_path = cef.cef_string_t(os.path.normpath(os.path.join(top, 'locales')))
settings.remote_debugging_port = 20480
settings.root_cache_path = cef.cef_string_t(os.path.join(cefdataroot, "root"))
settings.cache_path = cef.cef_string_t(os.path.join(cefdataroot, "root", "cache"))
settings.user_data_path = cef.cef_string_t(os.path.join(cefdataroot, "user-data"))
settings.log_file = cef.cef_string_t(os.path.join(cefdataroot, "cef.log"))
settings.log_severity = cef.LOGSEVERITY_DEBUG
#settings.log_severity = cef.LOGSEVERITY_DEFAULT
settings.uncaught_exception_stack_size = 200
self.mainArgs = mainArgs
self.mainArgsB = mainArgsB
self.settings = settings
self.app = app
def ShowSettings(self):
print('*'*20, 'settings:')
for fname, ftype in self.settings._fields_:
v = getattr(self.settings, fname)
if ftype == cef.cef_string_t:
v = str(v)
print(' {} = {}'.format(fname, v))
def Execute(self):
print('cef.cef_execute_process', self.mainArgs, self.app, None)
rc = cef.cef_execute_process(self.mainArgs, self.app, None)
print("libcef.execute_process rc=", rc)
if rc != -1:
return rc
print("libcef.initialize")
cef.cef_initialize(self.mainArgs, self.settings, self.app, None)
print("/libcef.initialize")
def Cleanup(self):
print("finish")
gc.collect()
print("libcef.shutdown")
cef.cef_shutdown()
print("done")
class CefLoadHandler(cef.cef_load_handler_t):
def py_on_loading_state_change(self, this, browser, isLoading, canGoBack, canGoForward):
print('CefLoadHandler.on_loading_state_change(browser, {}, {}, {})'.format(
isLoading, canGoBack, canGoForward
))
def py_on_load_start(self, this, browser, frame, transition_type):
print('CefLoadHandler.on_load_start(browser, frame, {})'.format(
transition_type
))
def py_on_load_end(self, this, browser, frame, httpStatusCode):
print('CefLoadHandler.on_load_end(browser, frame, {})'.format(
httpStatusCode
))
def py_on_load_error(self, this, browser, frame, errorCode, errorText, failedUrl):
print('CefLoadHandler.on_load_error(browser, frame, {}, {}, {})'.format(
errorCode, errorText, failedUrl
))
class CefBrowserProcessHandler(cef.cef_browser_process_handler_t):
def py_on_register_custom_preferences(self, this, type, registar):
print('CefBrowserProcessHandler.on_register_custom_preferences')
def py_on_context_initialized(self, this):
print('CefBrowserProcessHandler.on_context_initialized')
def py_on_before_child_process_launch(self, this, command_line):
print('CefBrowserProcessHandler.on_before_child_process_launch')
cl = command_line.contents
print('\tcl.valid', cl.is_valid(cl))
if 0 and cl.is_valid(cl):
s = cl.get_command_line_string(command_line)
print('\tcl.string', s)
def py_on_context_initialized(self, this):
print('CefBrowserProcessHandler.on_context_initialized')
def py_on_schedule_message_pump_work(self, this, delay_ms):
print('CefBrowserProcessHandler.on_schedule_message_pump_work')
def py_get_default_client(self, this):
print('CefBrowserProcessHandler.get_default_client')
class CefLifeSpanHandler(cef.cef_life_span_handler_t):
def py_on_before_popup(self,
this, browser, frame, target_url, target_frame_name, target_disposition, user_gesture, poupFeatures,
windowsInfo, client, settings, extra_info, no_javascript_access
):
print('LifeSpanHandler.OnBeforePopup')
return 0
def py_on_after_created(self, this, browser):
print('LifeSpanHandler.OnAfterCreated')
pass
def py_do_close(self, this, browser):
print('LifeSpanHandler.DoClose')
return 0
def py_on_before_close(self, this, browser):
print('LifeSpanHandler.OnBeforeClose')
# if browser is None:
cef.cef_quit_message_loop()
class Client(cef.cef_client_t):
def __init__(self):
cef.cef_client_t.__init__(self)
self.life_span_handler = CefLifeSpanHandler()
self.load_handler = CefLoadHandler()
def py_get_life_span_handler(self, *args):
#print('Client.py_get_life_span_handler')
ret = ct.addressof(self.life_span_handler)
return ret
def py_get_load_handler(self, *args):
#print('Client.py_get_load_handler')
ret = ct.addressof(self.load_handler)
return ret