Made the "Callbacks injection" recursive.

Before this, only the first level of child frames had the possibility to use
the Callbacks object. Now is injected recursively in all of them.

This is an extension of coverage for [Issue #683](http://code.google.com/p/phantomjs/issues/detail?id=683).
1.7
Ivan De Marino 2012-09-18 19:07:02 +01:00
parent e5ebcf90df
commit 26583740c8
2 changed files with 15 additions and 10 deletions

View File

@ -7,7 +7,7 @@ p.onCallback = (msg) ->
"Hello there, I'm coming to you from the 'phantom' context instead"
p.evaluate ->
callbackResponse = phantomCallback("Hello, I'm coming to you from the 'page' context")
callbackResponse = callPhantom("Hello, I'm coming to you from the 'page' context")
console.log "Received by the 'page' context: " + callbackResponse
phantom.exit()

View File

@ -1253,6 +1253,18 @@ QString WebPage::focusedFrameName() const
return m_customWebPage->currentFrame()->frameName();
}
static void injectCallbacksObjIntoFrames(QWebFrame *frame, WebpageCallbacks *callbacksObject)
{
// Decorate the window object in this frame
frame->addToJavaScriptWindowObject(CALLBACKS_OBJECT_NAME, callbacksObject, QScriptEngine::QtOwnership);
frame->evaluateJavaScript(CALLBACKS_OBJECT_INJECTION);
// Decorate the window object in the child frames (recursively)
foreach (QWebFrame *childFrame, frame->childFrames()) {
injectCallbacksObjIntoFrames(childFrame, callbacksObject);
}
}
void WebPage::handleJavaScriptWindowObjectCleared()
{
// Create Callbacks Holder object, if not already present for this page
@ -1263,15 +1275,8 @@ void WebPage::handleJavaScriptWindowObjectCleared()
// Reset focus on the Main Frame
m_mainFrame->setFocus();
// Decorate the window object in the Main Frame
m_mainFrame->addToJavaScriptWindowObject(CALLBACKS_OBJECT_NAME, m_callbacks, QScriptEngine::QtOwnership);
m_mainFrame->evaluateJavaScript(CALLBACKS_OBJECT_INJECTION);
// Decorate the window object in the Main Frame's Child Frames
foreach (QWebFrame *childFrame, m_mainFrame->childFrames()) {
childFrame->addToJavaScriptWindowObject(CALLBACKS_OBJECT_NAME, m_callbacks, QScriptEngine::QtOwnership);
childFrame->evaluateJavaScript(CALLBACKS_OBJECT_INJECTION);
}
// Inject the Callbacks object in the frame and child-frames (recursively)
injectCallbacksObjIntoFrames(m_mainFrame, m_callbacks);
}
void WebPage::initCompletions()