# Copyright (c) 2012-2014 The CEF Python authors. All rights reserved.
# License: New BSD License.
# Website: http://code.google.com/p/cefpython/
V8_PROPERTY_ATTRIBUTE_NONE = cef_types.V8_PROPERTY_ATTRIBUTE_NONE
V8_PROPERTY_ATTRIBUTE_READONLY = cef_types.V8_PROPERTY_ATTRIBUTE_READONLY
V8_PROPERTY_ATTRIBUTE_DONTENUM = cef_types.V8_PROPERTY_ATTRIBUTE_DONTENUM
V8_PROPERTY_ATTRIBUTE_DONTDELETE = cef_types.V8_PROPERTY_ATTRIBUTE_DONTDELETE
cdef public void V8ContextHandler_OnContextCreated(
CefRefPtr[CefBrowser] cefBrowser,
CefRefPtr[CefFrame] cefFrame,
CefRefPtr[CefV8Context] cefContext
) except * with gil:
# This handler may also be called by JavascriptBindings.Rebind().
# This handler may be called multiple times for the same frame - rebinding.
cdef PyBrowser pyBrowser
cdef PyFrame pyFrame
cdef JavascriptBindings javascriptBindings
cdef dict javascriptFunctions
cdef dict javascriptProperties
cdef dict javascriptObjects
cdef CefRefPtr[V8FunctionHandler] functionHandler
cdef CefRefPtr[CefV8Handler] v8Handler
cdef CefRefPtr[CefV8Value] v8Window
cdef CefRefPtr[CefV8Value] v8Function
cdef CefRefPtr[CefV8Value] v8Object
cdef CefRefPtr[CefV8Value] v8Method
cdef CefString cefFunctionName
cdef CefString cefPropertyName
cdef CefString cefMethodName
cdef CefString cefObjectName
cdef object key
cdef object value
cdef py_string functionName
cdef py_string objectName
cdef object clientCallback
try:
pyBrowser = GetPyBrowser(cefBrowser)
pyBrowser.SetUserData("__v8ContextCreated", True)
pyFrame = GetPyFrame(cefFrame)
javascriptBindings = pyBrowser.GetJavascriptBindings()
if not javascriptBindings:
return
javascriptFunctions = javascriptBindings.GetFunctions()
javascriptProperties = javascriptBindings.GetProperties()
javascriptObjects = javascriptBindings.GetObjects()
if not javascriptFunctions and not javascriptProperties and not javascriptObjects:
return
# This checks GetBindToFrames/GetBindToPopups must also be made in both:
# FunctionHandler_Execute() and OnContextCreated(), so that calling
# a non-existent property on window object throws an error.
if not pyFrame.IsMain() and not javascriptBindings.GetBindToFrames():
return
# This check is probably not needed, as GetPyBrowser() will already pass bindings=None,
# if this is a popup window and bindToPopups is False.
if pyBrowser.IsPopup() and not javascriptBindings.GetBindToPopups():
return
v8Window = cefContext.get().GetGlobal()
if javascriptProperties:
for key,value in javascriptProperties.items():
key = str(key)
PyToCefString(key, cefPropertyName)
v8Window.get().SetValue(
cefPropertyName,
PyToV8Value(value, cefContext),
V8_PROPERTY_ATTRIBUTE_NONE)
if javascriptFunctions or javascriptObjects:
functionHandler =