|
| 1 | +import types |
| 2 | + |
| 3 | +class JavascriptBindings: |
| 4 | + |
| 5 | + # By default we bind only to top frame. |
| 6 | + __bindToFrames = False |
| 7 | + __bindToPopups = False |
| 8 | + __functions = {} |
| 9 | + __properties = {} |
| 10 | + __v8properties = {} |
| 11 | + |
| 12 | + def __init__(self, bindToFrames=False, bindToPopups=False): |
| 13 | + |
| 14 | + self.__bindToFrames = bindToFrames |
| 15 | + self.__bindToPopups = bindToPopups |
| 16 | + |
| 17 | + def SetFunction(self, name, func): |
| 18 | + |
| 19 | + self.__functions[name] = func |
| 20 | + |
| 21 | + def SetProperty(self, name, value): |
| 22 | + |
| 23 | + allowed = self.__IsTypeAllowed(value) # returns True or string. |
| 24 | + if allowed != True: |
| 25 | + raise Exception("JavascriptBindings.SetProperty(): not allowed type: %s" % allowed) |
| 26 | + self.__properties[name] = value |
| 27 | + |
| 28 | + def GetProperty(self, name): |
| 29 | + |
| 30 | + # We must query CefV8Value for the real current value of property, |
| 31 | + # it could have been changed in javascript, use self.__v8properties. |
| 32 | + pass |
| 33 | + |
| 34 | + def __IsTypeAllowed(self, value): |
| 35 | + |
| 36 | + # Return value: True - allowed, string - not allowed |
| 37 | + |
| 38 | + # Not using type().__name__ here as it is not consistent, for int it is "int" but for None it is "NoneType". |
| 39 | + valueType = type(value) |
| 40 | + if valueType == types.ListType: |
| 41 | + for val in value: |
| 42 | + valueType2 = self.__IsTypeAllowed(val): |
| 43 | + if valueType2 != True: |
| 44 | + return valueType2.__name__ |
| 45 | + return True |
| 46 | + elif valueType == types.BoolType: |
| 47 | + return True |
| 48 | + elif valueType == types.DoubleType: |
| 49 | + return True |
| 50 | + elif valueType == types.IntType: |
| 51 | + return True |
| 52 | + elif valueType == types.NoneType: |
| 53 | + return True |
| 54 | + elif valueType == types.DictType: |
| 55 | + for key in value: |
| 56 | + valueType2 = self.__IsTypeAllowed(value[key]): |
| 57 | + if valueType2 != True: |
| 58 | + return valueType2.__name__ |
| 59 | + return True |
| 60 | + elif valueType == types.StringType: |
| 61 | + return True |
| 62 | + else: |
| 63 | + return valueType.__name__ |
| 64 | + return valueType.__name__ |
0 commit comments