-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcefexample.py
More file actions
421 lines (357 loc) · 16.6 KB
/
cefexample.py
File metadata and controls
421 lines (357 loc) · 16.6 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
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
# An example of embedding CEF browser in a PyQt4 application.
# Tested with PyQt 4.10.3 (Qt 4.8.5).
#Implementing CEF with Proxy Settings
import platform
if platform.architecture()[0] != "32bit":
raise Exception("Architecture not supported: %s" % platform.architecture()[0])
import platform
if platform.architecture()[0] != "32bit":
raise Exception("Architecture not supported: %s" \
% platform.architecture()[0])
import os, sys
libcef_dll = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'libcef.dll')
if os.path.exists(libcef_dll):
# Import a local module
if 0x02070000 <= sys.hexversion < 0x03000000:
import cefpython_py27 as cefpython
elif 0x03000000 <= sys.hexversion < 0x04000000:
import cefpython_py32 as cefpython
else:
raise Exception("Unsupported python version: %s" % sys.version)
else:
# Import an installed package
from cefpython3 import cefpython
from PyQt4 import QtGui
from PyQt4 import QtCore
def GetApplicationPath(file=None):
import re, os, platform
# On Windows after downloading file and calling Browser.GoForward(),
# current working directory is set to %UserProfile%.
# Calling os.path.dirname(os.path.realpath(__file__))
# returns for eg. "C:\Users\user\Downloads". A solution
# is to cache path on first call.
if not hasattr(GetApplicationPath, "dir"):
if hasattr(sys, "frozen"):
dir = os.path.dirname(sys.executable)
elif "__file__" in globals():
dir = os.path.dirname(os.path.realpath(__file__))
else:
dir = os.getcwd()
GetApplicationPath.dir = dir
# If file is None return current directory without trailing slash.
if file is None:
file = ""
# Only when relative path.
if not file.startswith("/") and not file.startswith("\\") and (
not re.search(r"^[\w-]+:", file)):
path = GetApplicationPath.dir + os.sep + file
if platform.system() == "Windows":
path = re.sub(r"[/\\]+", re.escape(os.sep), path)
path = re.sub(r"[/\\]+$", "", path)
return path
return str(file)
def ExceptHook(excType, excValue, traceObject):
import traceback, os, time, codecs
# This hook does the following: in case of exception write it to
# the "error.log" file, display it to the console, shutdown CEF
# and exit application immediately by ignoring "finally" (os._exit()).
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
traceObject))
errorFile = GetApplicationPath("error.log")
try:
appEncoding = cefpython.g_applicationSettings["string_encoding"]
except:
appEncoding = "utf-8"
if type(errorMsg) == bytes:
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
try:
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
fp.write("\n[%s] %s\n" % (
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
except:
print("[pyqt.py] WARNING: failed writing to error file: %s" % (
errorFile))
# Convert error message to ascii before printing, otherwise
# you may get error like this:
# | UnicodeEncodeError: 'charmap' codec can't encode characters
errorMsg = errorMsg.encode("ascii", errors="replace")
errorMsg = errorMsg.decode("ascii", errors="replace")
print("\n"+errorMsg+"\n")
cefpython.QuitMessageLoop()
cefpython.Shutdown()
os._exit(1)
class ClientHandler:
mainBrowser = None # May be None for global client callbacks.
def __init__(self):
pass
# -------------------------------------------------------------------------
# RequestHandler
# -------------------------------------------------------------------------
def OnBeforeBrowse(self, browser, frame, request, isRedirect):
print("[wxpython.py] RequestHandler::OnBeforeBrowse()")
print(" url = %s" % request.GetUrl()[:100])
# Handle "magnet:" links.
if request.GetUrl().startswith("magnet:"):
print("[wxpython.p] RequestHandler::OnBeforeBrowse(): "
"magnet link clicked, cancelling browse request")
return True
return False
def OnBeforeResourceLoad(self, browser, frame, request):
print("[wxpython.py] RequestHandler::OnBeforeResourceLoad()")
print(" url = %s" % request.GetUrl()[:100])
return False
def OnResourceRedirect(self, browser, frame, oldUrl, newUrlOut):
print("[wxpython.py] RequestHandler::OnResourceRedirect()")
print(" old url = %s" % oldUrl[:100])
print(" new url = %s" % newUrlOut[0][:100])
def GetAuthCredentials(self, browser, frame, isProxy, host, port, realm,
scheme, callback):
# This callback is called on the IO thread, thus print messages
# may not be visible.
print("[wxpython.py] RequestHandler::GetAuthCredentials()")
print(" host = %s" % host)
print(" realm = %s" % realm)
callback.Continue(username="aanuj", password="ram21gas")
return True
def OnQuotaRequest(self, browser, originUrl, newSize, callback):
print("[wxpython.py] RequestHandler::OnQuotaRequest()")
print(" origin url = %s" % originUrl)
print(" new size = %s" % newSize)
callback.Continue(True)
return True
def GetCookieManager(self, browser, mainUrl):
# Create unique cookie manager for each browser.
# You must set the "unique_request_context_per_browser"
# application setting to True for the cookie manager
# to work.
# Return None to have one global cookie manager for
# all CEF browsers.
if not browser:
# The browser param may be empty in some exceptional
# case, see docs.
return None
cookieManager = browser.GetUserData("cookieManager")
if cookieManager:
return cookieManager
else:
print("[wxpython.py] RequestHandler::GetCookieManager():"\
" created cookie manager")
cookieManager = cefpython.CookieManager.CreateManager("")
browser.SetUserData("cookieManager", cookieManager)
return cookieManager
def OnProtocolExecution(self, browser, url, allowExecutionOut):
# There's no default implementation for OnProtocolExecution on Linux,
# you have to make OS system call on your own. You probably also need
# to use LoadHandler::OnLoadError() when implementing this on Linux.
print("[wxpython.py] RequestHandler::OnProtocolExecution()")
print(" url = %s" % url)
if url.startswith("magnet:"):
print("[wxpython.py] Magnet link allowed!")
allowExecutionOut[0] = True
def _OnBeforePluginLoad(self, browser, url, policyUrl, info):
# This is a global callback set using SetGlobalClientCallback().
# Plugins are loaded on demand, only when website requires it,
# the same plugin may be called multiple times.
# This callback is called on the IO thread, thus print messages
# may not be visible.
print("[wxpython.py] RequestHandler::_OnBeforePluginLoad()")
print(" url = %s" % url)
print(" policy url = %s" % policyUrl)
print(" info.GetName() = %s" % info.GetName())
print(" info.GetPath() = %s" % info.GetPath())
print(" info.GetVersion() = %s" % info.GetVersion())
print(" info.GetDescription() = %s" % info.GetDescription())
# False to allow, True to block plugin.
return False
def _OnCertificateError(self, certError, requestUrl, callback):
# This is a global callback set using SetGlobalClientCallback().
print("[wxpython.py] RequestHandler::_OnCertificateError()")
print(" certError = %s" % certError)
print(" requestUrl = %s" % requestUrl)
if requestUrl == "https://testssl-expire.disig.sk/index.en.html":
print(" Not allowed!")
return False
if requestUrl \
== "https://testssl-expire.disig.sk/index.en.html?allow=1":
print(" Allowed!")
callback.Continue(True)
return True
return False
def OnRendererProcessTerminated(self, browser, status):
print("[wxpython.py] RequestHandler::OnRendererProcessTerminated()")
statuses = {
cefpython.TS_ABNORMAL_TERMINATION: "TS_ABNORMAL_TERMINATION",
cefpython.TS_PROCESS_WAS_KILLED: "TS_PROCESS_WAS_KILLED",
cefpython.TS_PROCESS_CRASHED: "TS_PROCESS_CRASHED"
}
statusName = "Unknown"
if status in statuses:
statusName = statuses[status]
print(" status = %s" % statusName)
def OnPluginCrashed(self, browser, pluginPath):
print("[wxpython.py] RequestHandler::OnPluginCrashed()")
print(" plugin path = %s" % pluginPath)
# -------------------------------------------------------------------------
# LifespanHandler
# -------------------------------------------------------------------------
# ** This callback is executed on the IO thread **
# Empty place-holders: popupFeatures, client.
def OnBeforePopup(self, browser, frame, targetUrl, targetFrameName,
popupFeatures, windowInfo, client, browserSettings,
noJavascriptAccess):
print("[wxpython.py] LifespanHandler::OnBeforePopup()")
print(" targetUrl = %s" % targetUrl)
# Custom browser settings for popups:
# > browserSettings[0] = {"plugins_disabled": True}
# Set WindowInfo object:
# > windowInfo[0] = cefpython.WindowInfo()
# On Windows there are keyboard problems in popups, when popup
# is created using "window.open" or "target=blank". This issue
# occurs only in wxPython. PyGTK or PyQt do not require this fix.
# The solution is to create window explicitilly, and not depend
# on CEF to create window internally.
# If you set allowPopups=True then CEF will create popup window.
# The wx.Frame cannot be created here, as this callback is
# executed on the IO thread. Window should be created on the UI
# thread. One solution is to call cefpython.CreateBrowser()
# which runs asynchronously and can be called on any thread.
# The other solution is to post a task on the UI thread, so
# that cefpython.CreateBrowserSync() can be used.
cefpython.PostTask(cefpython.TID_UI, self._CreatePopup, targetUrl)
allowPopups = False
return not allowPopups
def _CreatePopup(self, url):
frame = MainFrame(url=url, popup=True)
frame.Show()
def _OnAfterCreated(self, browser):
# This is a global callback set using SetGlobalClientCallback().
print("[wxpython.py] LifespanHandler::_OnAfterCreated()")
print(" browserId=%s" % browser.GetIdentifier())
def RunModal(self, browser):
print("[wxpython.py] LifespanHandler::RunModal()")
print(" browserId=%s" % browser.GetIdentifier())
def DoClose(self, browser):
print("[wxpython.py] LifespanHandler::DoClose()")
print(" browserId=%s" % browser.GetIdentifier())
def OnBeforeClose(self, browser):
print("[wxpython.py] LifespanHandler::OnBeforeClose")
print(" browserId=%s" % browser.GetIdentifier())
class MainWindow(QtGui.QMainWindow):
mainFrame = None
def __init__(self):
super(MainWindow, self).__init__(None)
self.createMenu()
self.mainFrame = MainFrame(self)
self.mainFrame.loadCustumUrl("http://www.google.com")
self.setCentralWidget(self.mainFrame)
self.resize(1024, 768)
self.setWindowTitle('PyQT CEF 3 example')
self.setFocusPolicy(QtCore.Qt.StrongFocus)
def createMenu(self):
menubar = self.menuBar()
filemenu = menubar.addMenu("&File")
filemenu.addAction(QtGui.QAction("Open", self))
getSource = QtGui.QAction("Get Source",self)
# getSource.triggered.connect(self.mainFrame.getSourceCode)
filemenu.addAction(getSource)
# filemenu.addAction(QtGui.QAction("Exit", self))
exitAction = QtGui.QAction( '&Exit', self)
filemenu.addAction(exitAction)
exitAction.triggered.connect(self.exitApp)
aboutmenu = menubar.addMenu("&About")
# QtCore.QObject.connect(self.Terminate, QtCore.SIGNAL('triggered()'), self.exitApp)
def exitApp(self):
print "Widget Shout be deleted"
frame = self.mainFrame.browser.GetMainFrame()
html = frame.GetSource("https://www.safelistech.com/time.php")
print html
self.mainFrame.browser.DoClose()
def focusInEvent(self, event):
cefpython.WindowUtils.OnSetFocus(int(self.centralWidget().winId()), 0, 0, 0)
def closeEvent(self, event):
self.mainFrame.browser.CloseBrowser()
class MainFrame(QtGui.QWidget):
browser = None
def __init__(self, parent=None,url=None):
super(MainFrame, self).__init__(parent)
pass
def loadCustumUrl(self,url):
# self.layout = QtGui.QVBoxLayout()
# self.killRef = QtGui.QPushButton()
self.clientHandler = ClientHandler()
cefpython.SetGlobalClientCallback("OnCertificateError",self.clientHandler._OnCertificateError)
cefpython.SetGlobalClientCallback("OnBeforePluginLoad",self.clientHandler._OnBeforePluginLoad)
cefpython.SetGlobalClientCallback("OnAfterCreated",self.clientHandler._OnAfterCreated)
windowInfo = cefpython.WindowInfo()
windowInfo.SetAsChild(int(self.winId()))
self.browser = cefpython.CreateBrowserSync(windowInfo,browserSettings={},navigateUrl="about:None")
self.browser.GetMainFrame().LoadUrl(url)
self.clientHandler.mainBrowser = self.browser
self.browser.SetClientHandler(self.clientHandler)
self.show()
def getSourceCode(self):
frame = self.browser.GetMainFrame()
html = frame.GetSource("https://www.safelistech.com/time.php")
return html
def moveEvent(self, event):
cefpython.WindowUtils.OnSize(int(self.winId()), 0, 0, 0)
def resizeEvent(self, event):
cefpython.WindowUtils.OnSize(int(self.winId()), 0, 0, 0)
class CefApplication(QtGui.QApplication):
timer = None
def __init__(self, args):
super(CefApplication, self).__init__(args)
self.createTimer()
def createTimer(self):
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.onTimer)
self.timer.start(10)
def onTimer(self):
# The proper way of doing message loop should be:
# 1. In createTimer() call self.timer.start(0)
# 2. In onTimer() call MessageLoopWork() only when
# QtGui.QApplication.instance()->hasPendingEvents() returns False.
# But... there is a bug in Qt, hasPendingEvents() returns always true.
cefpython.MessageLoopWork()
def stopTimer(self):
# Stop the timer after Qt message loop ended, calls to MessageLoopWork()
# should not happen anymore.
self.timer.stop()
if __name__ == '__main__':
print("[pyqt.py] PyQt version: %s" % QtCore.PYQT_VERSION_STR)
print("[pyqt.py] QtCore version: %s" % QtCore.qVersion())
# Intercept python exceptions. Exit app immediately when exception
# happens on any of the threads.
sys.excepthook = ExceptHook
cefpython.MessageLoopWork()
# Application settings
settings = {
"debug": True, # cefpython debug messages in console and in log_file
"log_severity": cefpython.LOGSEVERITY_INFO, # LOGSEVERITY_VERBOSE
"log_file": GetApplicationPath("debug.log"), # Set to "" to disable.
"release_dcheck_enabled": True, # Enable only when debugging.
# This directories must be set on Linux
"locales_dir_path": cefpython.GetModuleDirectory()+"/locales",
"resources_dir_path": cefpython.GetModuleDirectory(),
"browser_subprocess_path": "%s/%s" % (
cefpython.GetModuleDirectory(), "subprocess"),
}
# Command line switches set programmatically
switches = {
"proxy-server": "http://ironport1.iitk.ac.in:3128",
# "proxy-auto-detect":True,
# "enable-media-stream": "",
# "--invalid-switch": "" -> Invalid switch name
}
cefpython.Initialize(settings, switches)
app = CefApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
app.exec_()
app.stopTimer()
# Need to destroy QApplication(), otherwise Shutdown() fails.
# Unset main window also just to be safe.
del mainWindow
del app
cefpython.Shutdown()