Skip to content

Commit 0a117fa

Browse files
committed
Passing arguments to javascript bindings done.
Returning value from python to javascript done.
1 parent ea73364 commit 0a117fa

File tree

12 files changed

+193
-27
lines changed

12 files changed

+193
-27
lines changed

browser.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ include "utils.pyx"
77

88
# Global variables.
99

10-
cdef map[int, CefRefPtr[CefBrowser]] __cefBrowsers # innerWindowID : browser
10+
cdef map[int, CefRefPtr[CefBrowser]] __cefBrowsers # innerWindowID : browser # a pointer would be: new map[int, CefRefPtr[CefBrowser]]()
1111
__pyBrowsers = {}
1212

1313
# This dictionary list of popup browsers is never cleaned, it may contain old inner

cefexample/cefadvanced.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
<h3>Javascript bindings</h3>
4040

41-
<a href="javascript:PyTest1()">window.PyTest1()</a>,
42-
<a href="javascript:PyTest2()">window.PyTest2()</a>
41+
<a href="javascript:alert('Return value from PyTest1: '+PyTest1(100))">window.PyTest1(100)</a><br>
42+
<a href="javascript:alert('Return value from PyTest2: '+PyTest2(100, 'This string was passed from javascript'))">window.PyTest2(100, 'This string was passed from javascript')</a>
4343

4444
<h3>Popups</h3>
4545

cefexample/cefadvanced.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ def CefAdvanced():
5555
cefpython.MessageLoop()
5656
cefpython.Shutdown()
5757

58-
def PyTest1():
59-
print "PyTest1() called"
58+
def PyTest1(arg1):
59+
print "PyTest1(%s) called" % arg1
60+
return 123
6061

61-
def PyTest2():
62-
print "PyTest2() called"
62+
def PyTest2(arg1, arg2):
63+
print "PyTest2(%s, %s) called" % (arg1, arg2)
64+
return "This string was returned from Python function PyTest2()"
6365

6466
def OnLoadStart(browser, frame):
6567
print "OnLoadStart(): frame URL: %s" % frame.GetURL()
@@ -82,10 +84,6 @@ def OnKeyEvent(browser, eventType, keyCode, modifiers, isSystemKey, isAfterJavas
8284
return True
8385
return False
8486

85-
def JavascriptBindings():
86-
# http://code.google.com/p/chromiumembedded/wiki/JavaScriptIntegration
87-
pass
88-
8987
def JavascriptCallbacks():
9088
pass
9189

@@ -103,10 +101,6 @@ def MoveWindow():
103101
#cefwindow.MoveWindow(windowID, xpos=0, ypos=0)
104102
pass
105103

106-
def DeveloperTools():
107-
#browser.ShowDevTools()
108-
pass
109-
110104
def LoadContentFromZip():
111105
# Allow to pack html/css/images to a zip and run content from this file.
112106
# Optionally allow to password protect this zip file.

cefexample/debug.log

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,19 @@
631631
[0712/205256:INFO:CONSOLE(0)] "Uncaught ReferenceError: PyTest1 is not defined," source: file:///D:/cefpython/src/cefexample/cefadvanced.html(1)
632632
[0712/205300:VERBOSE1:cookie_monster.cc(2122)] WARNING: Unsupported cookie scheme: chrome-devtools
633633
[0712/205300:VERBOSE1:cookie_monster.cc(2122)] WARNING: Unsupported cookie scheme: chrome-devtools
634+
[0713/052122:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
635+
[0713/052235:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
636+
[0713/053104:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
637+
[0713/053137:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
638+
[0713/053656:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
639+
[0713/053718:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
640+
[0713/053740:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
641+
[0714/130502:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
642+
[0714/144619:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
643+
[0714/145131:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
644+
[0714/145236:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
645+
[0714/145429:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
646+
[0714/145532:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
647+
[0714/145623:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
648+
[0714/145702:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091
649+
[0714/145823:ERROR:network_change_notifier_win.cc(111)] WSALookupServiceBegin failed with: 10091

cefpython.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ include "virtualkeys.pyx"
1717
include "v8contexthandler.pyx"
1818
include "functionhandler.pyx"
1919

20+
include "v8utils.pyx"
21+
2022
# Global variables.
2123
__debug = False
2224

functionhandler.pyx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44

55
include "imports.pyx"
66
include "utils.pyx"
7+
include "v8utils.pyx"
78

89
cdef cbool FunctionHandler_Execute(
910
CefRefPtr[CefV8Context] cefContext,
1011
CefString& cefFuncName,
11-
CefRefPtr[CefV8Value] cefObject,
12+
CefRefPtr[CefV8Value] cefObject, # receiver ('this' object) of the function.
1213
CefV8ValueList& cefArguments,
1314
CefRefPtr[CefV8Value]& cefRetval,
1415
CefString& cefException) with gil:
1516

17+
cdef vector[CefRefPtr[CefV8Value]].iterator iterator
18+
cdef CefRefPtr[CefV8Value] cefValue
19+
1620
try:
1721
pyBrowser = GetPyBrowserByCefBrowser((<CefV8Context*>(cefContext.get())).GetBrowser())
1822
pyFrame = GetPyFrameByCefFrame((<CefV8Context*>(cefContext.get())).GetFrame())
@@ -37,13 +41,19 @@ cdef cbool FunctionHandler_Execute(
3741
if pyBrowser.IsPopup() and not javascriptBindings.GetBindToPopups():
3842
return <cbool>False
3943

40-
print "FunctionHandler.Execute(name=%s)" % funcName
41-
print "pyFrame.GetIdentifier(): %s" % pyFrame.GetIdentifier()
44+
arguments = []
45+
46+
# cefArguments.
47+
iterator = cefArguments.begin()
48+
while iterator != cefArguments.end():
49+
cefValue = deref(iterator)
50+
arguments.append(V8ValueToPyValue(cefValue))
51+
preinc(iterator)
4252

43-
#jsBindings = pyBrowser.GetJavascriptBindings()
44-
#if jsBindings:
53+
retval = func(*arguments)
54+
cefRetval = PyValueToV8Value(retval)
4555

46-
return <cbool>False
56+
return <cbool>True
4757

4858
except:
4959
(exc_type, exc_value, exc_trace) = sys.exc_info()

imports.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ from cef_frame cimport *
4646
cimport cef_types # cannot cimport *, that would cause name conflicts with constants.
4747
cimport cef_types_win # same as cef_types.
4848
from cef_v8 cimport *
49+
cimport cef_v8_static
4950
from v8functionhandler cimport *

pyinclude/cef_v8.pxd

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from cef_browser cimport CefBrowser
88
from cef_frame cimport CefFrame
99
from cef_string cimport CefString
1010
from libcpp cimport bool as cbool
11+
from libcpp.vector cimport vector
1112
cimport cef_types
1213

1314
cdef extern from "include/cef_v8.h":
@@ -19,19 +20,38 @@ cdef extern from "include/cef_v8.h":
1920

2021
ctypedef vector[CefRefPtr[CefV8Value]] CefV8ValueList
2122

23+
cdef cppclass CefV8Accessor:
24+
pass
25+
2226
cdef cppclass CefV8Handler:
2327
pass
2428

2529
cdef cppclass CefV8Value:
30+
int GetArrayLength()
31+
cbool GetBoolValue()
32+
double GetDoubleValue()
33+
CefString GetFunctionName()
34+
int GetIntValue()
35+
cbool GetKeys(vector[CefString]& keys)
36+
CefString GetStringValue()
37+
CefRefPtr[CefV8Value] GetValue(CefString& key) # object's property by key
38+
CefRefPtr[CefV8Value] GetValue(int index) # arrays index value
39+
cbool HasValue(CefString& key)
40+
cbool HasValue(int index)
41+
cbool IsArray()
42+
cbool IsBool()
43+
cbool IsDate()
44+
cbool IsDouble()
45+
cbool IsFunction()
46+
cbool IsInt()
47+
cbool IsNull()
48+
cbool IsObject()
49+
cbool IsString()
50+
cbool IsUndefined()
2651
cbool SetValue(
2752
CefString& key,
2853
CefRefPtr[CefV8Value] value,
2954
cef_types.cef_v8_propertyattribute_t attribute
3055
)
3156

3257

33-
cdef extern from "include/cef_v8.h" namespace "CefV8Value":
34-
35-
cdef CefRefPtr[CefV8Value] CreateFunction(
36-
CefString& name,
37-
CefRefPtr[CefV8Handler] handler)

pyinclude/cef_v8_static.pxd

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2012 CefPython Authors. All rights reserved.
2+
# License: New BSD License.
3+
# Website: http://code.google.com/p/cefpython/
4+
5+
from cef_ptr cimport CefRefPtr
6+
from cef_v8 cimport CefV8Value
7+
from cef_string cimport CefString
8+
from cef_v8 cimport CefV8Handler
9+
from cef_v8 cimport CefV8Accessor
10+
from cef_base cimport CefBase
11+
from libcpp cimport bool as cbool
12+
13+
# Importing static methods.
14+
# This is in a separate file as we do not want these names to be imported
15+
# into global namespace, you will be using them like this:
16+
#
17+
# cimport cef_v8_static
18+
# cef_v8_static.CreateArray()
19+
20+
cdef extern from "include/cef_v8.h" namespace "CefV8Value":
21+
22+
cdef CefRefPtr[CefV8Value] CreateArray()
23+
cdef CefRefPtr[CefV8Value] CreateBool(cbool value)
24+
cdef CefRefPtr[CefV8Value] CreateDouble(double value)
25+
cdef CefRefPtr[CefV8Value] CreateFunction(
26+
CefString& name,
27+
CefRefPtr[CefV8Handler] handler)
28+
cdef CefRefPtr[CefV8Value] CreateInt(int value)
29+
cdef CefRefPtr[CefV8Value] CreateNull()
30+
cdef CefRefPtr[CefV8Value] CreateObject(CefRefPtr[CefBase] user_data, CefRefPtr[CefV8Accessor] accessor)
31+
cdef CefRefPtr[CefV8Value] CreateString(CefString& value)
32+
# cdef CefRefPtr[CefV8Value] CreateUndefined()

utils.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# License: New BSD License.
33
# Website: http://code.google.com/p/cefpython/
44

5+
# This file has nothing to do with util.h
6+
57
include "imports.pyx"
68
include "browser.pyx"
79
include "frame.pyx"

0 commit comments

Comments
 (0)