if (stateChangeHandler_ != null && (ie || async_)) {
if (context == null) {
context = Context.getCurrentContext();
}
final Scriptable scope = stateChangeHandler_.getParentScope();
final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
final int nbExecutions;
if (async_ && STATE_LOADING == state) {
// quite strange but IE and Mozilla seem both to fire state loading twice
// in async mode (at least with HTML of the unit tests)
nbExecutions = 2;
}
else {
nbExecutions = 1;
}
final Scriptable thisValue;
if (getBrowserVersion().hasFeature(BrowserVersionFeatures.XMLHTTPREQUEST_HANDLER_THIS_IS_FUNCTION)) {
thisValue = stateChangeHandler_;
}
else {
thisValue = this;
}
for (int i = 0; i < nbExecutions; i++) {
LOG.debug("Calling onreadystatechange handler for state " + state);
jsEngine.callFunction(containingPage_, stateChangeHandler_, context,
scope, thisValue, ArrayUtils.EMPTY_OBJECT_ARRAY);
LOG.debug("onreadystatechange handler: " + context.decompileFunction(stateChangeHandler_, 4));
LOG.debug("Calling onreadystatechange handler for state " + state + ". Done.");
}
}
// Firefox has a separate onload handler, too.
if (!ie && loadHandler_ != null && state == STATE_COMPLETED) {
if (context == null) {
context = Context.getCurrentContext();
}
final Scriptable scope = loadHandler_.getParentScope();
final JavaScriptEngine jsEngine = containingPage_.getWebClient().getJavaScriptEngine();
jsEngine.callFunction(containingPage_, loadHandler_, context, scope, this, ArrayUtils.EMPTY_OBJECT_ARRAY);
}
}