@@ -10,18 +10,11 @@ cdef class JavascriptBindings:
1010 cdef public dict properties
1111 cdef public dict objects
1212
13- # V8ContextHandler_OnContextCreated inserts frames here that should have
14- # javascript bindings, it is later needed to do rebinding using Rebind()
15- # method. All frames are here, the main too, frames may be from different
16- # Browser objects.
17- cdef public dict frames # frameIdentifier(int64) : tuple(PyBrowser, PyFrame())
18-
1913 def __init__ (self , bindToFrames = False , bindToPopups = False ):
2014 self .functions = {}
2115 self .properties = {}
2216 self .objects = {}
23- self .frames = {}
24-
17+
2518 self .bindToFrames = int (bindToFrames)
2619 self .bindToPopups = int (bindToPopups)
2720
@@ -78,41 +71,60 @@ cdef class JavascriptBindings:
7871 else :
7972 self .properties[name] = value
8073
81- cdef py_void AddFrame(self , PyBrowser pyBrowser, PyFrame pyFrame):
82- if pyFrame.GetIdentifier() not in self .frames:
83- self .frames[pyFrame.GetIdentifier()] = (pyBrowser, pyFrame)
84-
85- cdef py_void RemoveFrame(self , PyBrowser pyBrowser, PyFrame pyFrame):
86- if pyFrame.GetIdentifier() in self .frames:
87- del self .frames[pyFrame.GetIdentifier()]
88-
89- cpdef py_void Rebind(self ):
90- assert IsThread(TID_UI), (
91- " JavascriptBindings.Rebind() may only be called on UI thread" )
92-
93- cdef CefRefPtr[CefBrowser] cefBrowser
94- cdef CefRefPtr[CefFrame] cefFrame
95- cdef CefRefPtr[CefV8Context] v8Context
96- cdef cpp_bool sameContext
97- cdef PyBrowser pyBrowser
98- cdef PyFrame pyFrame
99-
100- for frameId in self .frames:
101- pyBrowser = self .frames[frameId][0 ]
102- pyFrame = self .frames[frameId][1 ]
103- cefBrowser = pyBrowser.GetCefBrowser()
104- cefFrame = pyFrame.GetCefFrame()
105- v8Context = cefFrame.get().GetV8Context()
106-
107- sameContext = v8Context.get().IsSame(cef_v8_static.GetCurrentContext())
108- if not sameContext:
109- Debug(" JavascriptBindings.Rebind(): inside a different context, calling v8Context.Enter()" )
110- assert v8Context.get().Enter(), " v8Context.Enter() failed"
111-
112- V8ContextHandler_OnContextCreated(cefBrowser, cefFrame, v8Context)
113-
114- if not sameContext:
115- assert v8Context.get().Exit(), " v8Context.Exit() failed"
74+
75+ IF CEF_VERSION == 1 :
76+ cpdef py_void Rebind(self ):
77+ # Rebind may also be used for first-time bindings, in
78+ # a case when v8 process/thread was created too fast,
79+ # see Browser.SetJavascriptBindings() that checks whether
80+ # OnContextCreated() event already happened, if so it will
81+ # call Rebind() to do the javascript bindings.
82+ assert IsThread(TID_UI), (
83+ " JavascriptBindings.Rebind() may only be called on UI thread" )
84+ cdef CefRefPtr[CefBrowser] cefBrowser
85+ cdef CefRefPtr[CefFrame] cefFrame
86+ cdef CefRefPtr[CefV8Context] v8Context
87+ cdef cpp_bool sameContext
88+ cdef PyBrowser pyBrowser
89+ cdef PyFrame pyFrame
90+ cdef list frames
91+ global g_pyBrowsers
92+ for pyBrowser in g_pyBrowsers:
93+ # These javascript bindings may have been binded
94+ # to many browsers.
95+ if pyBrowser.GetJavascriptBindings() != self :
96+ continue
97+ if self .bindToFrames:
98+ frames = pyBrowser.GetFrames()
99+ else :
100+ frames = [pyBrowser.GetMainFrame()]
101+ for frameId in self .frames:
102+ pyBrowser = self .frames[frameId][0 ]
103+ pyFrame = self .frames[frameId][1 ]
104+ cefBrowser = pyBrowser.GetCefBrowser()
105+ cefFrame = pyFrame.GetCefFrame()
106+ v8Context = cefFrame.get().GetV8Context()
107+ sameContext = v8Context.get().IsSame(cef_v8_static.GetCurrentContext())
108+ if not sameContext:
109+ Debug(" JavascriptBindings.Rebind(): inside a different context, calling v8Context.Enter()" )
110+ assert v8Context.get().Enter(), " v8Context.Enter() failed"
111+ V8ContextHandler_OnContextCreated(cefBrowser, cefFrame, v8Context)
112+ if not sameContext:
113+ assert v8Context.get().Exit(), " v8Context.Exit() failed"
114+
115+ ELIF CEF_VERSION == 3 :
116+ cpdef py_void Rebind(self ):
117+ # Rebind may also be used for first-time bindings, in
118+ # a case when v8 process/thread was created too fast,
119+ # see Browser.SetJavascriptBindings() that checks whether
120+ # OnContextCreated() event already happened, if so it will
121+ # call Rebind() to do the javascript bindings.
122+ cdef PyBrowser pyBrowser
123+ global g_pyBrowsers
124+ for pyBrowser in g_pyBrowsers:
125+ # Send to renderer process: properties, functions,
126+ # objects and its methods, bindToFrames.
127+ pass
116128
117129 cpdef dict GetProperties(self ):
118130 return self .properties
0 commit comments