Skip to content

Commit 6a8abe2

Browse files
CzarekCzarek
authored andcommitted
Implemented Display Handler in CEF Python 3.
1 parent 03b49df commit 6a8abe2

11 files changed

Lines changed: 444 additions & 11 deletions

File tree

cefpython/browser.pyx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,47 +156,57 @@ cdef class PyBrowser:
156156
free(self.imageBuffer)
157157

158158
cpdef py_void SetClientCallback(self, py_string name, object callback):
159+
IF CEF_VERSION == 1:
160+
self.SetClientCallback_CEF1(name, callback)
161+
ELIF CEF_VERSION == 3:
162+
self.SetClientCallback_CEF3(name, callback)
163+
164+
cpdef py_void SetClientCallback_CEF1(self,
165+
py_string name, object callback):
159166
if not self.allowedClientCallbacks:
160167
# CefLoadHandler.
161168
self.allowedClientCallbacks += ["OnLoadEnd", "OnLoadError",
162169
"OnLoadStart"]
163-
164170
# CefKeyboardHandler.
165171
self.allowedClientCallbacks += ["OnKeyEvent"]
166-
167172
# CefV8ContextHandler.
168173
self.allowedClientCallbacks += ["OnContextCreated",
169174
"OnContextReleased" ,"OnUncaughtException"]
170-
171175
# CefRequestHandler.
172176
self.allowedClientCallbacks += ["OnBeforeBrowse",
173177
"OnBeforeResourceLoad", "OnResourceRedirect",
174178
"OnResourceResponse", "OnProtocolExecution",
175179
"GetDownloadHandler", "GetAuthCredentials",
176180
"GetCookieManager"]
177-
178181
# CefDisplayHandler.
179182
self.allowedClientCallbacks += ["OnAddressChange",
180183
"OnConsoleMessage", "OnContentsSizeChange",
181184
"OnNavStateChange", "OnStatusMessage", "OnTitleChange",
182185
"OnTooltip"]
183-
184186
# LifespanHandler.
185187
self.allowedClientCallbacks += ["DoClose", "OnAfterCreated",
186188
"OnBeforeClose", "RunModal"]
187-
188189
# RenderHandler
189190
self.allowedClientCallbacks += ["GetViewRect", "GetScreenRect",
190191
"GetScreenPoint", "OnPopupShow", "OnPopupSize",
191192
"OnPaint", "OnCursorChange"]
192-
193193
# DragHandler
194194
self.allowedClientCallbacks += ["OnDragStart", "OnDragEnter"]
195-
196195
if name not in self.allowedClientCallbacks:
197196
raise Exception("Browser.SetClientCallback() failed: unknown "
198197
"callback: %s" % name)
198+
self.clientCallbacks[name] = callback
199199

200+
cpdef py_void SetClientCallback_CEF3(self,
201+
py_string name, object callback):
202+
if not self.allowedClientCallbacks:
203+
# CefDisplayHandler.
204+
self.allowedClientCallbacks += ["OnLoadingStateChange",
205+
"OnAddressChange", "OnTitleChange", "OnTooltip",
206+
"OnStatusMessage", "OnConsoleMessage"]
207+
if name not in self.allowedClientCallbacks:
208+
raise Exception("Browser.SetClientCallback() failed: unknown "
209+
"callback: %s" % name)
200210
self.clientCallbacks[name] = callback
201211

202212
cpdef py_void SetClientHandler(self, object clientHandler):

cefpython/cef3/client_handler/client_handler.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,88 @@ void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
214214
REQUIRE_UI_THREAD();
215215
LifespanHandler_OnBeforeClose(browser);
216216
}
217+
218+
// --------------------------------------------------------------------------
219+
// CefDisplayHandler
220+
// --------------------------------------------------------------------------
221+
222+
///
223+
// Implement this interface to handle events related to browser display state.
224+
// The methods of this class will be called on the UI thread.
225+
///
226+
227+
///
228+
// Called when the loading state has changed.
229+
///
230+
/*--cef()--*/
231+
void ClientHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
232+
bool isLoading,
233+
bool canGoBack,
234+
bool canGoForward) {
235+
REQUIRE_UI_THREAD();
236+
DisplayHandler_OnLoadingStateChange(browser, isLoading, canGoBack,
237+
canGoForward);
238+
}
239+
240+
///
241+
// Called when a frame's address has changed.
242+
///
243+
/*--cef()--*/
244+
void ClientHandler::OnAddressChange(CefRefPtr<CefBrowser> browser,
245+
CefRefPtr<CefFrame> frame,
246+
const CefString& url) {
247+
REQUIRE_UI_THREAD();
248+
DisplayHandler_OnAddressChange(browser, frame, url);
249+
}
250+
251+
///
252+
// Called when the page title changes.
253+
///
254+
/*--cef(optional_param=title)--*/
255+
void ClientHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
256+
const CefString& title) {
257+
REQUIRE_UI_THREAD();
258+
DisplayHandler_OnTitleChange(browser, title);
259+
}
260+
261+
///
262+
// Called when the browser is about to display a tooltip. |text| contains the
263+
// text that will be displayed in the tooltip. To handle the display of the
264+
// tooltip yourself return true. Otherwise, you can optionally modify |text|
265+
// and then return false to allow the browser to display the tooltip.
266+
// When window rendering is disabled the application is responsible for
267+
// drawing tooltips and the return value is ignored.
268+
///
269+
/*--cef(optional_param=text)--*/
270+
bool ClientHandler::OnTooltip(CefRefPtr<CefBrowser> browser,
271+
CefString& text) {
272+
REQUIRE_UI_THREAD();
273+
return DisplayHandler_OnTooltip(browser, text);
274+
// return false;
275+
}
276+
277+
///
278+
// Called when the browser receives a status message. |text| contains the text
279+
// that will be displayed in the status message and |type| indicates the
280+
// status message type.
281+
///
282+
/*--cef(optional_param=value)--*/
283+
void ClientHandler::OnStatusMessage(CefRefPtr<CefBrowser> browser,
284+
const CefString& value) {
285+
REQUIRE_UI_THREAD();
286+
DisplayHandler_OnStatusMessage(browser, value);
287+
}
288+
289+
///
290+
// Called to display a console message. Return true to stop the message from
291+
// being output to the console.
292+
///
293+
/*--cef(optional_param=message,optional_param=source)--*/
294+
bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
295+
const CefString& message,
296+
const CefString& source,
297+
int line) {
298+
REQUIRE_UI_THREAD();
299+
return DisplayHandler_OnConsoleMessage(browser, message, source, line);
300+
// return false;
301+
}

cefpython/cef3/client_handler/client_handler.h

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
class ClientHandler :
1414
public CefClient,
15-
public CefLifeSpanHandler
15+
public CefLifeSpanHandler,
16+
public CefDisplayHandler
1617
{
1718
public:
1819
ClientHandler(){}
@@ -41,7 +42,7 @@ class ClientHandler :
4142
///
4243
/*--cef()--*/
4344
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
44-
return NULL;
45+
return this;
4546
}
4647

4748
///
@@ -257,6 +258,70 @@ class ClientHandler :
257258
/*--cef()--*/
258259
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
259260

261+
// --------------------------------------------------------------------------
262+
// CefDisplayHandler
263+
// --------------------------------------------------------------------------
264+
265+
///
266+
// Implement this interface to handle events related to browser display state.
267+
// The methods of this class will be called on the UI thread.
268+
///
269+
270+
///
271+
// Called when the loading state has changed.
272+
///
273+
/*--cef()--*/
274+
virtual void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
275+
bool isLoading,
276+
bool canGoBack,
277+
bool canGoForward) OVERRIDE;
278+
279+
///
280+
// Called when a frame's address has changed.
281+
///
282+
/*--cef()--*/
283+
virtual void OnAddressChange(CefRefPtr<CefBrowser> browser,
284+
CefRefPtr<CefFrame> frame,
285+
const CefString& url) OVERRIDE;
286+
287+
///
288+
// Called when the page title changes.
289+
///
290+
/*--cef(optional_param=title)--*/
291+
virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
292+
const CefString& title) OVERRIDE;
293+
294+
///
295+
// Called when the browser is about to display a tooltip. |text| contains the
296+
// text that will be displayed in the tooltip. To handle the display of the
297+
// tooltip yourself return true. Otherwise, you can optionally modify |text|
298+
// and then return false to allow the browser to display the tooltip.
299+
// When window rendering is disabled the application is responsible for
300+
// drawing tooltips and the return value is ignored.
301+
///
302+
/*--cef(optional_param=text)--*/
303+
virtual bool OnTooltip(CefRefPtr<CefBrowser> browser,
304+
CefString& text) OVERRIDE;
305+
306+
///
307+
// Called when the browser receives a status message. |text| contains the text
308+
// that will be displayed in the status message and |type| indicates the
309+
// status message type.
310+
///
311+
/*--cef(optional_param=value)--*/
312+
virtual void OnStatusMessage(CefRefPtr<CefBrowser> browser,
313+
const CefString& value) OVERRIDE;
314+
315+
///
316+
// Called to display a console message. Return true to stop the message from
317+
// being output to the console.
318+
///
319+
/*--cef(optional_param=message,optional_param=source)--*/
320+
virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
321+
const CefString& message,
322+
const CefString& source,
323+
int line) OVERRIDE;
324+
260325
private:
261326

262327
// Include the default reference counting implementation.

cefpython/cef3/linux/binaries_32bit/wxpython.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,41 @@ <h3>Python callbacks</h3>
149149
<a href="javascript:external.TestPythonCallback(JSCallback3)">
150150
external.TestPythonCallback(JSCallback3)</a>
151151

152+
153+
154+
<h3>Display handler</h3>
155+
156+
See messages in the console.
157+
158+
<pre>
159+
def OnLoadingStateChange(self, browser, isLoading, canGoBack,
160+
canGoForward):
161+
print("ClientHandler::OnLoadingStateChange()")
162+
print("isLoading = %s, canGoBack = %s, canGoForward = %s" \
163+
% (isLoading, canGoBack, canGoForward))
164+
165+
def OnAddressChange(self, browser, frame, url):
166+
print("ClientHandler::OnAddressChange()")
167+
print("url = %s" % url)
168+
169+
def OnTitleChange(self, browser, title):
170+
print("ClientHandler::OnTitleChange()")
171+
print("title = %s" % title)
172+
173+
def OnTooltip(self, browser, text):
174+
print("ClientHandler::OnTooltip()")
175+
print("text = %s")
176+
177+
def OnStatusMessage(self, browser, value):
178+
print("ClientHandler::OnStatusMessage()")
179+
print("value = %s" % value)
180+
181+
def OnConsoleMessage(self, browser, message, source, line):
182+
print("ClientHandler::OnConsoleMessage()")
183+
print("message = %s" % message)
184+
print("source = %s" % source)
185+
print("line = %s" % line)
186+
</pre>
187+
152188
</body>
153189
</html>

cefpython/cef3/linux/binaries_32bit/wxpython.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def __init__(self):
104104
browserSettings={"plugins_disabled": False},
105105
navigateUrl="file://"+GetApplicationPath("wxpython.html"))
106106

107+
self.browser.SetClientHandler(ClientHandler())
108+
107109
jsBindings = cefpython.JavascriptBindings(
108110
bindToFrames=False, bindToPopups=True)
109111
jsBindings.SetFunction("PyPrint", PyPrint)
@@ -178,6 +180,40 @@ def PyCallback(self, *args):
178180
self.mainBrowser.GetMainFrame().ExecuteJavascript(
179181
"window.alert(\"%s\")" % message)
180182

183+
class ClientHandler:
184+
# -------------------------------------------------------------------------
185+
# DisplayHandler
186+
# -------------------------------------------------------------------------
187+
def OnLoadingStateChange(self, browser, isLoading, canGoBack,
188+
canGoForward):
189+
print("ClientHandler::OnLoadingStateChange()")
190+
print("isLoading = %s, canGoBack = %s, canGoForward = %s" \
191+
% (isLoading, canGoBack, canGoForward))
192+
193+
def OnAddressChange(self, browser, frame, url):
194+
print("ClientHandler::OnAddressChange()")
195+
print("url = %s" % url)
196+
197+
def OnTitleChange(self, browser, title):
198+
print("ClientHandler::OnTitleChange()")
199+
print("title = %s" % title)
200+
201+
def OnTooltip(self, browser, text):
202+
print("ClientHandler::OnTooltip()")
203+
print("text = %s")
204+
# OnTooltip seems not to work on Linux, reported bug on the CEF forum:
205+
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10898
206+
207+
def OnStatusMessage(self, browser, value):
208+
print("ClientHandler::OnStatusMessage()")
209+
print("value = %s" % value)
210+
211+
def OnConsoleMessage(self, browser, message, source, line):
212+
print("ClientHandler::OnConsoleMessage()")
213+
print("message = %s" % message)
214+
print("source = %s" % source)
215+
print("line = %s" % line)
216+
181217
class MyApp(wx.App):
182218
timer = None
183219
timerID = 1

cefpython/cef3/linux/binaries_64bit/wxpython.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,41 @@ <h3>Python callbacks</h3>
149149
<a href="javascript:external.TestPythonCallback(JSCallback3)">
150150
external.TestPythonCallback(JSCallback3)</a>
151151

152+
153+
154+
<h3>Display handler</h3>
155+
156+
See messages in the console.
157+
158+
<pre>
159+
def OnLoadingStateChange(self, browser, isLoading, canGoBack,
160+
canGoForward):
161+
print("ClientHandler::OnLoadingStateChange()")
162+
print("isLoading = %s, canGoBack = %s, canGoForward = %s" \
163+
% (isLoading, canGoBack, canGoForward))
164+
165+
def OnAddressChange(self, browser, frame, url):
166+
print("ClientHandler::OnAddressChange()")
167+
print("url = %s" % url)
168+
169+
def OnTitleChange(self, browser, title):
170+
print("ClientHandler::OnTitleChange()")
171+
print("title = %s" % title)
172+
173+
def OnTooltip(self, browser, text):
174+
print("ClientHandler::OnTooltip()")
175+
print("text = %s")
176+
177+
def OnStatusMessage(self, browser, value):
178+
print("ClientHandler::OnStatusMessage()")
179+
print("value = %s" % value)
180+
181+
def OnConsoleMessage(self, browser, message, source, line):
182+
print("ClientHandler::OnConsoleMessage()")
183+
print("message = %s" % message)
184+
print("source = %s" % source)
185+
print("line = %s" % line)
186+
</pre>
187+
152188
</body>
153189
</html>

0 commit comments

Comments
 (0)