How to find QWebFrame calling C++ API from Javascript
-
Hi,
new here - working on a port of an application from Webkit/GTK to Webkit/QT 4.7.is it possible to find a reference to the frame that is executing a JS call to a custom QObject API in C++ via slot function?
the page has multiple frames which may be calling the C++ API. I would like to store the frame reference so that I can call frame->evaluateJavaScript() later.
tried using page->currentFrame() but this returns the frame with focus, not the frame executing the JS.
any help would be appreciated,
thanks. -
I cannot think of any direct way. But objects are added by frames right? It is convoluted but couldn't you have a caller-proxy objects that keep a reference to the frame it has been added, and pass that as argument to the object executing javascript?
-
Hi Benjamin - thanks for the suggestion.
I would need to create a new custom qobject for each new frame and use these to store the frame reference. The object would have to be deleted on a frame delete - do you know if there a signal/event for a frame delete, I can't see any in the docs?
I seen there is an 'own' parameter for QWebFrame::addToJavaScriptWindowObject() to set the ownership - maybe if I set this to script then the delete would be handled?
-
Yep, that is what I would do, set the ownership to the script engine itself.
-
Adding the QObject using ScriptOwnership is really a bad idea, the destructor is never called!! I'm figuring that this is not working at least in Qt 4.5.3
-
[quote author="DavidGOrtega" date="1294760299"]Adding the QObject using ScriptOwnership is really a bad idea, the destructor is never called!! I'm figuring that this is not working at least in Qt 4.5.3[/quote]
I know about at least one big project using ScriptOwnership with 4.7 for security check on tear down. I think they would have complained if that was not working :)
-
after some further test I did find something funny using QScriptEngine::ScriptOwnership - on a page reload the object I attached to the main frame gets garbage collected!
as an alternative I set the custom object parent to the frame and use default ownership when calling addToJavaScriptWindowObject(). This way the object gets deleted when the frame is deleted which is what I want.
-
[quote author="willc" date="1294934025"]after some further test I did find something funny using QScriptEngine::ScriptOwnership - on a page reload the object I attached to the main frame gets garbage collected!
as an alternative I set the custom object parent to the frame and use default ownership when calling addToJavaScriptWindowObject(). This way the object gets deleted when the frame is deleted which is what I want.
[/quote]Or, you can call addToJavaScriptWindowObject in a slot connected to the javaScriptWindowObjectCleared signal.
-
@willc, yep, that is intended. Custom objects are not supposed to live past the lifetime of the content. You can do as peppe says to get them from page to page.
-
yeah - I am doing that too..
does anyone know the expected behaviour when an new frame (iframe) is created?
I have connected a function to the page "frameCreated" signal - this is called for the new frame and I call newFrame->addToJavaScriptWindowObject() to add my custom object.
I also connect to the javaScriptWindowObjectCleared signal for the new frame.
Sometimes this signal goes off immediately resulting in a second call to addToJavaScriptWindowObject() - is this expected?