Repository navigation
Expand file tree
/
Copy pathjavascript_bindings.pyx
More file actions
178 lines (129 loc) · 5.12 KB
/
Copy pathjavascript_bindings.pyx
File metadata and controls
178 lines (129 loc) · 5.12 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
# Copyright (c) 2012 CefPython Authors. All rights reserved.
# License: New BSD License.
# Website: http://code.google.com/p/cefpython/
include "imports.pyx"
class JavascriptBindings:
# By default we bind only to top frame.
__bindToFrames = False
__bindToPopups = False
__functions = {}
__properties = {}
__objects = {}
# Many browsers may have the same JavascriptBindings object.
# __browsers = [] # currently not used, no browser is inserted here.
# V8ContextHandler_OnContextCreated inserts frames here that should have javascript bindings,
# it is later needed to do rebinding using Rebind() method. All frames are here, the main too,
# frames may be from different Browser objects.
__frames = {} # frameIdentifier(int64) : tuple(PyBrowser, PyFrame())
def __init__(self, bindToFrames=False, bindToPopups=False):
self.__bindToFrames = bindToFrames
self.__bindToPopups = bindToPopups
def GetBindToFrames(self):
return self.__bindToFrames
def GetBindToPopups(self):
return self.__bindToPopups
def SetFunction(self, name, func):
self.SetProperty(name, func)
def SetObject(self, name, obj):
if not hasattr(obj, "__class__"):
raise Exception("JavascriptBindings.SetObject() failed: name=%s, __class__ attribute missing, this is not an object" % name)
methods = {}
for value in inspect.getmembers(obj, predicate=inspect.ismethod):
key = value[0]
method = value[1]
methods[key] = method
self.__objects[name] = methods
def GetFunction(self, name):
if name in self.__functions:
return self.__functions[name]
def GetFunctions(self):
return self.__functions
def GetObjects(self):
return self.__objects
def GetObjectMethod(self, objectName, methodName):
if objectName in self.__objects:
if methodName in self.__objects[objectName]:
return self.__objects[objectName][methodName]
def SetProperty(self, name, value):
allowed = self.__IsValueAllowed(value) # returns True or string.
if allowed is not True:
raise Exception("JavascriptBindings.SetProperty() failed: name=%s, "
"not allowed type: %s (this may be a type of a nested value)"
% (name, allowed))
valueType = type(value)
if valueType == types.FunctionType or valueType == types.MethodType:
self.__functions[name] = value
else:
self.__properties[name] = value
def AddFrame(self, pyBrowser, pyFrame):
if pyFrame.GetIdentifier() not in self.__frames:
Debug("JavascriptBindings.AddFrame(): frameID = %s" % pyFrame.GetIdentifier())
self.__frames[pyFrame.GetIdentifier()] = (pyBrowser, pyFrame)
def RemoveFrame(self, pyBrowser, pyFrame):
if pyFrame.GetIdentifier() in self.__frames:
Debug("JavascriptBindings.RemoveFrame(): frameID = %s" % pyFrame.GetIdentifier())
del self.__frames[pyFrame.GetIdentifier()]
def Rebind(self):
assert IsCurrentThread(TID_UI), "JavascriptBindings.Rebind() may only be called on UI thread"
cdef CefRefPtr[CefBrowser] cefBrowser
cdef CefRefPtr[CefFrame] cefFrame
cdef CefRefPtr[CefV8Context] v8Context
cdef c_bool sameContext
for frameID in self.__frames:
pyBrowser = self.__frames[frameID][0]
pyFrame = self.__frames[frameID][1]
cefBrowser = GetCefBrowserByInnerWindowID(CheckInnerWindowID(pyBrowser.GetInnerWindowID()))
cefFrame = GetCefFrameByFrameID(CheckFrameID(pyFrame.GetIdentifier()))
v8Context = cefFrame.get().GetV8Context()
sameContext = v8Context.get().IsSame(cef_v8_static.GetCurrentContext())
if not sameContext:
Debug("JavascriptBindings.Rebind(): inside a different context, calling v8Context.Enter()")
assert v8Context.get().Enter(), "v8Context.Enter() failed"
V8ContextHandler_OnContextCreated(cefBrowser, cefFrame, v8Context)
if not sameContext:
assert v8Context.get().Exit(), "v8Context.Exit() failed"
def GetProperties(self):
return self.__properties
@staticmethod
def IsValueAllowed(value):
return JavascriptBindings.__IsValueAllowed(value) is True
@staticmethod
def __IsValueAllowed(value, recursion=False) :
# !! When making changes here also check: Frame.SetProperty()
# as it checks for FunctionType, MethodType.
# - Return value: True - allowed, string - not allowed
# - Not using type().__name__ here as it is not consistent, for int it is "int" but for None it is "NoneType".
valueType = type(value)
if valueType == list:
for val in value:
valueType2 = JavascriptBindings.__IsValueAllowed(val, True)
if valueType2 is not True:
return valueType2.__name__
return True
elif valueType == bool:
return True
elif valueType == float:
return True
elif valueType == int:
return True
elif valueType == type(None):
return True
elif valueType == types.FunctionType or valueType == types.MethodType:
if recursion:
return valueType.__name__
else:
return True
elif valueType == dict:
for key in value:
valueType2 = JavascriptBindings.__IsValueAllowed(value[key], True)
if valueType2 is not True:
return valueType2.__name__
return True
elif valueType == str:
return True
elif valueType == unicode:
return True
elif valueType == tuple:
return True
else:
return valueType.__name__