# Copyright (c) 2012 CEF Python, see the Authors file.# All rights reserved. Licensed under BSD 3-clause license.# Project website: https://github.com/cztomczak/cefpythoninclude "cefpython.pyx"cdef class JavascriptBindings:# By default binding only to top frame.cdef public py_bool bindToFramescdef public py_bool bindToPopupscdef public dict functionscdef public dict propertiescdef public dict objectsdef __init__(self, bindToFrames=False, bindToPopups=False):self.functions = {}self.properties = {}self.objects = {}self.bindToFrames = bool(bindToFrames)self.bindToPopups = bool(bindToPopups)cpdef py_bool GetBindToFrames(self):return bool(self.bindToFrames)cpdef py_bool GetBindToPopups(self):return bool(self.bindToPopups)cpdef py_void SetFunction(self, py_string name, object func):self.SetProperty(name, func)cpdef py_void SetObject(self, py_string name, object obj):if not hasattr(obj, "__class__"):raise Exception("JavascriptBindings.SetObject() failed: name=%s, ""__class__ attribute missing, this is not an object" % name)cdef dict methods = {}cdef py_string keycdef object methodcdef object predicate = inspect.ismethodif isinstance(obj, (PyBrowser, PyFrame)):predicate = inspect.isbuiltinfor value in inspect.getmembers(obj, predicate=predicate):key = value[0]method = value[1]methods[key] = methodself.objects[name] = methodscpdef object GetFunction(self, py_string name):if name in self.functions:return self.functions[name]cpdef dict GetFunctions(self):return self.functionscpdef dict GetObjects(self):return self.objectscpdef object GetObjectMethod(self, py_string objectName, py_string methodName):if objectName in self.objects:if methodName in self.objects[objectName]:return self.objects[objectName][methodName]cpdef object GetFunctionOrMethod(self, py_string name):# Name can be "someFunc" or "object.someMethod".cdef list wordsif "." in name:words = name.split(".")return self.GetObjectMethod(words[0], words[1])else:return self.GetFunction(name)cpdef py_void SetProperty(self, py_string name, object value):cdef object allowed = self.IsValueAllowedRecursively(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))cdef object valueType = type(value)if IsFunctionOrMethod(valueType):self.functions[name] = valueelse:self.properties[name] = valuecpdef py_void Rebind(self):# Rebind() is called for both first-time binding and rebinding.cdef PyBrowser pyBrowsercdef dict functionscdef dict propertiescdef dict objectscdef dict methodsfor browserId, pyBrowser in g_pyBrowsers.iteritems():if pyBrowser.GetJavascriptBindings() != self:continue# Send to the Renderer process: functions, properties,# objects and its methods, bindToFrames.functions = {}for funcName in self.functions:functions[funcName] = Noneproperties = self.propertiesobjects = {}for objectName in self.objects:methods = {}for methodName in self.objects[objectName]:methods[methodName] = Noneobjects[objectName] = methodspyBrowser.SendProcessMessage(cef_types.PID_RENDERER,0, "DoJavascriptBindings", [{"functions": functions,"properties": properties,"objects": objects,"bindToFrames": self.bindToFrames}])cpdef dict GetProperties(self):return self.properties@staticmethoddef IsValueAllowed(object value):return JavascriptBindings.IsValueAllowedRecursively(value) is True@staticmethoddef IsValueAllowedRecursively(object value, py_bool recursion=False):# When making changes here modify also Frame.SetProperty() as it# checks for FunctionType, MethodType.cdef object valueType = type(value)cdef object valueType2cdef object keyif valueType == list:for val in value:valueType2 = JavascriptBindings.IsValueAllowedRecursively(val, True)if valueType2 is not True:return valueType2.__name__return Trueelif valueType == bool:return Trueelif valueType == float:return Trueelif valueType == int:return Trueelif valueType == long:return Trueelif valueType == type(None):return Trueelif IsFunctionOrMethod(valueType):if recursion:return valueType.__name__else:return Trueelif valueType == dict:for key in value:valueType2 = JavascriptBindings.IsValueAllowedRecursively(value[key], True)if valueType2 is not True:return valueType2.__name__return Trueelif valueType == str or valueType == bytes:return Trueelif PY_MAJOR_VERSION < 3 and valueType == unicode:# The unicode type is not defined in Python 3.return Trueelif valueType == tuple:return Trueelse:return valueType.__name__
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。