Skip to content

Commit e234b00

Browse files
CzarekCzarek
authored andcommitted
Javascript bindings for CEF 3 are almost ready, a bit of
work is still required. Fix to the compile.py script. Updated the wxpython script, added js bindings. Updated makefiles to display warnings and treat warnings as errors.
1 parent d688c76 commit e234b00

25 files changed

+536
-114
lines changed

cefpython/browser.pyx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ cdef class PyBrowser:
102102

103103
cdef public dict clientCallbacks
104104
cdef public list allowedClientCallbacks
105-
IF CEF_VERSION == 1:
106-
cdef public JavascriptBindings javascriptBindings
105+
cdef public JavascriptBindings javascriptBindings
107106
cdef public dict userData
108107

109108
# Properties used by ToggleFullscreen().
@@ -211,17 +210,18 @@ cdef class PyBrowser:
211210
cpdef dict GetClientCallbacksDict(self):
212211
return self.clientCallbacks
213212

214-
IF CEF_VERSION == 1:
215-
216-
cpdef py_void SetJavascriptBindings(self, JavascriptBindings bindings):
217-
self.javascriptBindings = bindings
213+
cpdef py_void SetJavascriptBindings(self, JavascriptBindings bindings):
214+
self.javascriptBindings = bindings
215+
IF CEF_VERSION == 1:
218216
if self.GetUserData("__v8ContextCreated"):
219217
Debug("Browser.SetJavascriptBindings(): v8 context already"
220218
"created, calling Rebind()")
221219
self.javascriptBindings.Rebind()
220+
ELIF CEF_VERSION == 3:
221+
self.javascriptBindings.Rebind()
222222

223-
cpdef JavascriptBindings GetJavascriptBindings(self):
224-
return self.javascriptBindings
223+
cpdef JavascriptBindings GetJavascriptBindings(self):
224+
return self.javascriptBindings
225225

226226
# --------------
227227
# CEF API.
@@ -629,9 +629,24 @@ cdef class PyBrowser:
629629
# virtual void HandleKeyEventBeforeTextInputClient(CefEventHandle keyEvent) =0;
630630
# virtual void HandleKeyEventAfterTextInputClient(CefEventHandle keyEvent) =0;
631631

632-
cdef cpp_bool SendProcessMessage(self, cef_process_id_t targetProcess,
633-
py_string name) except *:
634-
cdef CefRefPtr[CefProcessMessage] msg = CefProcessMessage_Create(
635-
PyToCefStringValue(name))
636-
return self.GetCefBrowser().get().SendProcessMessage(targetProcess,
637-
msg)
632+
cdef void SendProcessMessage(self, cef_process_id_t targetProcess,
633+
py_string messageName, list pyArguments
634+
) except *:
635+
cdef CefRefPtr[CefProcessMessage] message = \
636+
CefProcessMessage_Create(PyToCefStringValue(messageName))
637+
# This does not work, no idea why, the CEF implementation
638+
# seems not to allow it, both Assign() and swap() do not work:
639+
# | message.get().GetArgumentList().Assign(arguments.get())
640+
# | message.get().GetArgumentList().swap(arguments)
641+
cdef CefRefPtr[CefListValue] messageArguments = \
642+
message.get().GetArgumentList()
643+
PyListToExistingCefListValue(pyArguments, messageArguments)
644+
Debug("SendProcessMessage(): message=%s, arguments size=%d" % (
645+
messageName,
646+
message.get().GetArgumentList().get().GetSize()))
647+
cdef cpp_bool success = \
648+
self.GetCefBrowser().get().SendProcessMessage(
649+
targetProcess, message)
650+
if not success:
651+
raise Exception("Browser.SendProcessMessage() failed: "\
652+
"messageName=%s" % messageName)

cefpython/cef1/client_handler/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# -g - extra debug information
2+
# -O1 - more precise backtraces
3+
# -fPIC - required when using -shared option
4+
# -Wall - show important warnings
5+
# -Werror - treat warnings as errors
6+
17
CC = g++
28
CCFLAGS = -g
39

cefpython/cef1/v8function_handler/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# -g - extra debug information
2+
# -O1 - more precise backtraces
3+
# -fPIC - required when using -shared option
4+
# -Wall - show important warnings
5+
# -Werror - treat warnings as errors
6+
17
CC = g++
28
CCFLAGS = -g
39

cefpython/cef3/cefpython_public_api.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
#pragma warning(disable:4190)
55
#endif
66

7+
// To be able to use 'public' declarations you need to include Python.h and cefpython.h.
8+
// This include must be before including CEF, otherwise you get errors like:
9+
// | /usr/include/python2.7/pyconfig.h:1161:0: warning: "_POSIX_C_SOURCE" redefined
10+
#include "Python.h"
11+
712
// All the imports that are required when including "cefpython.h".
813
#include "include/cef_client.h"
914
// #include "include/cef_web_urlrequest.h"
1015
// #include "include/cef_cookie.h"
1116
#include "util.h"
1217

13-
// To be able to use 'public' declarations you need to include Python.h and cefpython.h.
14-
#include "Python.h"
15-
1618
// Python 3.2 fix - DL_IMPORT is not defined in Python.h
1719
#ifndef DL_IMPORT /* declarations for DLL import/export */
1820
#define DL_IMPORT(RTYPE) RTYPE
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
# -g - extra debug information
2+
# -O1 - more precise backtraces
3+
# -fPIC - required when using -shared option, required for use with Cython
4+
# -Wall - show important warnings
5+
# -Werror - treat warnings as errors
6+
7+
# Cython compiler options:
8+
# -fPIC -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro
9+
110
CC = g++
2-
CCFLAGS = -g
11+
CCFLAGS = -g -fPIC -Wall -Werror
312

413
SRC = client_handler.cpp
514
OBJ = $(SRC:.cpp=.o)
@@ -8,7 +17,7 @@ OUT = libclient_handler.a
817
INC = -I./../ -I/usr/include/python2.7 -I/usr/include/gtk-2.0 -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/gtk-2.0/include -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/atk-1.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include
918

1019
.cpp.o:
11-
$(CC) -fPIC $(INC) $(CCFLAGS) -c $< -o $@
20+
$(CC) $(CCFLAGS) $(INC) -c $< -o $@
1221

1322
$(OUT): $(OBJ)
1423
ar rcs $(OUT) $(OBJ)

cefpython/cef3/client_handler/client_handler.cpp

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
#include "cefpython_public_api.h"
77
#include <stdio.h>
88

9-
// Declared "inline" to get rid of the "already defined" errors when linking.
9+
// Defined as "inline" to get rid of the "already defined" errors
10+
// when linking.
1011
inline void DebugLog(const char* szString)
1112
{
1213
// TODO: get the log_file option from CefSettings.
14+
printf("cefpython: %s\n", szString);
1315
FILE* pFile = fopen("debug.log", "a");
1416
fprintf(pFile, "cefpython_app: %s\n", szString);
1517
fclose(pFile);
@@ -18,32 +20,51 @@ inline void DebugLog(const char* szString)
1820
bool ClientHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
1921
CefProcessId source_process,
2022
CefRefPtr<CefProcessMessage> message) {
21-
std::string messageName = message.get()->GetName().ToString();
22-
printf("Browser: OnProcessMessageReceived(): %s\n", messageName.c_str());
23+
std::string messageName = message->GetName().ToString();
24+
std::string logMessage = "Browser: OnProcessMessageReceived(): ";
25+
logMessage.append(messageName.c_str());
26+
DebugLog(logMessage.c_str());
2327
if (messageName == "OnContextCreated") {
24-
CefRefPtr<CefListValue> args = message.get()->GetArgumentList();
25-
if (args.get()->GetSize() == 1
26-
&& args.get()->GetType(0) == VTYPE_INT) {
27-
int64 frameIdentifier = args.get()->GetInt(0);
28-
V8ContextHandler_OnContextCreated(browser, frameIdentifier);
28+
CefRefPtr<CefListValue> args = message->GetArgumentList();
29+
if (args->GetSize() == 1 && args->GetType(0) == VTYPE_INT) {
30+
int64 frameId = args->GetInt(0);
31+
CefRefPtr<CefFrame> frame = browser->GetFrame(frameId);
32+
V8ContextHandler_OnContextCreated(browser, frame);
2933
return true;
3034
} else {
31-
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments,"\
35+
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments," \
3236
" messageName=OnContextCreated");
3337
return false;
3438
}
3539
} else if (messageName == "OnContextReleased") {
36-
CefRefPtr<CefListValue> args = message.get()->GetArgumentList();
37-
if (args.get()->GetSize() == 1
38-
&& args.get()->GetType(0) == VTYPE_INT) {
39-
int64 frameIdentifier = args.get()->GetInt(0);
40-
V8ContextHandler_OnContextReleased(browser, frameIdentifier);
40+
CefRefPtr<CefListValue> args = message->GetArgumentList();
41+
if (args->GetSize() == 1 && args->GetType(0) == VTYPE_INT) {
42+
int64 frameId = args->GetInt(0);
43+
CefRefPtr<CefFrame> frame = browser->GetFrame(frameId);
44+
V8ContextHandler_OnContextReleased(browser, frame);
4145
return true;
4246
} else {
43-
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments,"\
47+
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments," \
4448
" messageName=OnContextReleased");
4549
return false;
4650
}
51+
} else if (messageName == "V8FunctionHandler::Execute") {
52+
CefRefPtr<CefListValue> args = message->GetArgumentList();
53+
if (args->GetSize() == 3
54+
&& args->GetType(0) == VTYPE_INT // frameId
55+
&& args->GetType(1) == VTYPE_STRING // funcName
56+
&& args->GetType(2) == VTYPE_LIST) { // funcArgs
57+
int64 frameId = args->GetInt(0);
58+
CefRefPtr<CefFrame> frame = browser->GetFrame(frameId);
59+
CefString funcName = args->GetString(1);
60+
CefRefPtr<CefListValue> funcArgs = args->GetList(2);
61+
V8FunctionHandler_Execute(browser, frame, funcName, funcArgs);
62+
return true;
63+
} else {
64+
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments," \
65+
" messageName=V8FunctionHandler::Execute");
66+
return false;
67+
}
4768
}
4869
return false;
4970
}

cefpython/cef3/client_handler/client_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class ClientHandler :
137137
CefRefPtr<CefProcessMessage> message)
138138
OVERRIDE;
139139

140-
protected:
140+
private:
141141

142142
// Include the default reference counting implementation.
143143
IMPLEMENT_REFCOUNTING(ClientHandler);

cefpython/cef3/linux/binaries_32bit/wxpython.html

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@ <h3>Popup</h3>
3030

3131

3232

33-
<h3>HTML5 video & accelerated content</h3>
34-
<a href="http://www.youtube.com/watch?v=siOHh0uzcuY&html5=True">
35-
HTML 5 video</a><br>
36-
<a href="http://mudcu.be/labs/JS1k/BreathingGalaxies.html">
37-
Accelerated canvas</a><br>
38-
<a href="http://www.webkit.org/blog-files/3d-transforms/poster-circle.html">
39-
Accelerated layers</a><br>
33+
<h3>HTML5 video and accelerated content</h3>
34+
<a href="http://www.youtube.com/watch?v=siOHh0uzcuY&html5=True">
35+
HTML 5 video</a><br>
36+
<a href="http://mudcu.be/labs/JS1k/BreathingGalaxies.html">
37+
Accelerated canvas</a><br>
38+
<a href="http://www.webkit.org/blog-files/3d-transforms/poster-circle.html">
39+
Accelerated layers</a><br>
40+
41+
42+
43+
<h3>Javascript bindings</h3>
44+
<a href="javascript:external.Print('printing in python console from js')">
45+
external.Print('printing in python console from js')</a>
46+
4047

4148

4249
<br><br><br><br><br><br><br><br>

cefpython/cef3/linux/binaries_32bit/wxpython.py

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

107+
jsBindings = cefpython.JavascriptBindings(
108+
bindToFrames=False, bindToPopups=False)
109+
jsBindings.SetObject("external", JavascriptBindings(self.browser))
110+
self.browser.SetJavascriptBindings(jsBindings)
111+
107112
self.Bind(wx.EVT_CLOSE, self.OnClose)
108113
if USE_EVT_IDLE:
109114
# Bind EVT_IDLE only for the main application frame.
@@ -128,6 +133,15 @@ def OnClose(self, event):
128133
def OnIdle(self, event):
129134
cefpython.MessageLoopWork()
130135

136+
class JavascriptBindings:
137+
mainBrowser = None
138+
139+
def __init__(self, mainBrowser):
140+
self.mainBrowser = mainBrowser
141+
142+
def Print(self, message):
143+
print(message)
144+
131145
class MyApp(wx.App):
132146
timer = None
133147
timerID = 1

cefpython/cef3/linux/binaries_64bit/wxpython.html

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@ <h3>Popup</h3>
3030

3131

3232

33-
<h3>HTML5 video & accelerated content</h3>
34-
<a href="http://www.youtube.com/watch?v=siOHh0uzcuY&html5=True">
35-
HTML 5 video</a><br>
36-
<a href="http://mudcu.be/labs/JS1k/BreathingGalaxies.html">
37-
Accelerated canvas</a><br>
38-
<a href="http://www.webkit.org/blog-files/3d-transforms/poster-circle.html">
39-
Accelerated layers</a><br>
33+
<h3>HTML5 video and accelerated content</h3>
34+
<a href="http://www.youtube.com/watch?v=siOHh0uzcuY&html5=True">
35+
HTML 5 video</a><br>
36+
<a href="http://mudcu.be/labs/JS1k/BreathingGalaxies.html">
37+
Accelerated canvas</a><br>
38+
<a href="http://www.webkit.org/blog-files/3d-transforms/poster-circle.html">
39+
Accelerated layers</a><br>
40+
41+
42+
43+
<h3>Javascript bindings</h3>
44+
<a href="javascript:external.Print('printing in python console from js')">
45+
external.Print('printing in python console from js')</a>
46+
4047

4148

4249
<br><br><br><br><br><br><br><br>

0 commit comments

Comments
 (0)